-
Notifications
You must be signed in to change notification settings - Fork 3
/
doc_io.go
249 lines (227 loc) · 6.4 KB
/
doc_io.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2023 Cisco Systems, Inc. and its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package apidocs
import (
"context"
"encoding/json"
"fmt"
"github.com/cisco-open/go-lanai/cmd/lanai-cli/cmdutils"
"github.com/ghodss/yaml"
"mime"
"net/http"
"os"
"path"
"path/filepath"
"strings"
)
const (
schemaPrefixGitHub = "github://"
schemaPrefixHttp = "http://"
schemaPrefixHttps = "https://"
kDefaultGitHubPAT = "default"
)
const (
extYaml = ".yaml"
extYamlAlt = ".yml"
extJson = ".json"
extJson5 = ".json5"
)
var (
cache = map[string]*apidoc{}
githubPatCache map[string]string
)
type apidoc struct {
source string
value map[string]interface{}
}
func writeApiDocLocal(_ context.Context, doc *apidoc) (string, error) {
// create file or open and truncate
absPath, file, e := cmdutils.OpenFile(doc.source, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if e != nil {
return "", fmt.Errorf("unable to write API doc to file [%s]: %v", doc.source, e)
}
defer func() { _ = file.Close() }()
switch fileExt := strings.ToLower(path.Ext(absPath)); fileExt {
case extYaml, extYamlAlt:
var data []byte
data, e = yaml.Marshal(doc.value)
if e == nil {
_, e = file.Write(data)
}
case extJson, extJson5:
e = json.NewEncoder(file).Encode(doc.value)
default:
return "", fmt.Errorf("unsupported file extension for OAS document: %s", fileExt)
}
if e != nil {
return "", fmt.Errorf("cannot save document to [%s]: %v", absPath, e)
}
return absPath, nil
}
func loadApiDocs(ctx context.Context, paths []string) ([]*apidoc, error) {
docs := make([]*apidoc, len(paths), len(paths)*2)
for i, p := range paths {
doc, e := loadApiDoc(ctx, p)
if e != nil {
return nil, e
}
docs[i] = doc
}
return docs, nil
}
func loadApiDoc(ctx context.Context, path string) (doc *apidoc, err error) {
defer func() {
if err == nil && doc != nil {
cache[doc.source] = doc
}
}()
switch {
case strings.HasPrefix(path, schemaPrefixHttp) || strings.HasPrefix(path, schemaPrefixHttps):
return loadApiDocHttp(ctx, path)
case strings.HasPrefix(path, schemaPrefixGitHub):
return loadApiDocGitHub(ctx, path)
default:
return loadApiDocLocal(ctx, path)
}
}
func loadApiDocLocal(_ context.Context, fPath string) (*apidoc, error) {
absPath, e := filepath.Abs(path.Join(cmdutils.GlobalArgs.WorkingDir, fPath))
if e != nil {
return nil, fmt.Errorf("unable to resolve absolute path of file [%s]: %v", fPath, e)
}
if cached, ok := cache[absPath]; ok && cached != nil {
return cached, nil
}
doc := apidoc{
source: absPath,
}
switch fileExt := strings.ToLower(path.Ext(fPath)); fileExt {
case extYaml, extYamlAlt:
_, e = cmdutils.BindYamlFile(&doc.value, fPath)
case extJson, extJson5:
_, e = cmdutils.BindJsonFile(&doc.value, fPath)
default:
return nil, fmt.Errorf("unsupported file extension for OAS document: %s", fileExt)
}
if e != nil {
return nil, e
}
return &doc, nil
}
func loadApiDocGitHub(ctx context.Context, rawUrl string) (*apidoc, error) {
rawUrl = strings.Replace(rawUrl, schemaPrefixGitHub, schemaPrefixHttps, 1)
return loadApiDocHttp(ctx, rawUrl, func(r *http.Request) {
token := githubAccessToken(ctx, r.URL.Host)
if len(token) != 0 {
r.Header.Set("Authorization", fmt.Sprintf("token %s", token))
}
})
}
func loadApiDocHttp(ctx context.Context, rawUrl string, opts ...func(r *http.Request)) (*apidoc, error) {
req, e := http.NewRequestWithContext(ctx, http.MethodGet, rawUrl, nil)
if e != nil {
return nil, fmt.Errorf("invalid URL [%s]: %v", rawUrl, e)
}
urlStr := req.URL.String()
if cached, ok := cache[urlStr]; ok && cached != nil {
return cached, nil
}
// apply options
for _, fn := range opts {
fn(req)
}
// send request and check result
resp, e := http.DefaultClient.Do(req)
if e != nil {
return nil, fmt.Errorf("unable to GET requested URL [%s]: %v", urlStr, e)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, fmt.Errorf("unable to Get requested URL [%s]: Status %d", urlStr, resp.StatusCode)
}
// parse format
doc := apidoc{
source: urlStr,
}
switch ext := contentTypeAsExt(req, resp); ext {
case extYaml, extYamlAlt:
e = cmdutils.BindYaml(resp.Body, &doc.value)
case extJson, extJson5:
e = json.NewDecoder(resp.Body).Decode(&doc.value)
default:
return nil, fmt.Errorf("unsupported file format for OAS document: %s", ext)
}
if e != nil {
return nil, e
}
return &doc, nil
}
func contentTypeAsExt(req *http.Request, resp *http.Response) string {
ct := resp.Header.Get("Content-Type")
fileExt := strings.ToLower(path.Ext(req.URL.EscapedPath()))
if mt, _, e := mime.ParseMediaType(ct); e == nil {
switch {
case mt == "application/json":
fileExt = extJson
case strings.HasSuffix(mt, "yaml") || strings.HasSuffix(mt, "yml"):
fileExt = extYamlAlt
}
}
return fileExt
}
func githubAccessToken(ctx context.Context, host string) string {
// populate inmemory cache if possible
if githubPatCache == nil {
if e := populateGithubPatCache(); e != nil {
logger.WithContext(ctx).Warnf("invalid GitHub access token: %v", e)
return ""
}
}
token, ok := githubPatCache[host]
if !ok {
return githubPatCache[kDefaultGitHubPAT]
}
return token
}
func populateGithubPatCache() error {
githubPatCache = make(map[string]string)
// parse from ResolveConfig
for _, v := range ResolveConf.GitHubTokens {
token := os.ExpandEnv(v.Token)
if len(token) == 0 {
continue
}
host := strings.ToLower(v.Host)
if len(host) == 0 {
host = kDefaultGitHubPAT
}
githubPatCache[host] = token
}
// parse from ResolveArguments
for _, arg := range ResolveArgs.GitHubPATs {
split := strings.SplitN(arg, "@", 2)
val := os.ExpandEnv(split[0])
if len(val) == 0 {
continue
}
if len(split) == 1 {
githubPatCache[kDefaultGitHubPAT] = val
} else {
githubPatCache[split[1]] = val
}
}
return nil
}