-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathimportAPIPolicy.go
More file actions
276 lines (229 loc) · 8.64 KB
/
importAPIPolicy.go
File metadata and controls
276 lines (229 loc) · 8.64 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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.
*/
package impl
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/go-resty/resty/v2"
"github.com/wso2/product-apim-tooling/import-export-cli/utils"
)
func ImportAPIPolicyToEnv(accessOAuthToken, importEnvironment, importPath string) error {
publisherEndpoint := utils.GetPublisherEndpointOfEnv(importEnvironment, utils.MainConfigFilePath)
if _, err := os.Stat(importPath); err != nil {
if !os.IsNotExist(err) {
return err
}
}
publisherEndpoint = utils.AppendSlashToString(publisherEndpoint)
uri := publisherEndpoint + "operation-policies/import"
err := importAPIPolicy(uri, importPath, accessOAuthToken, true)
return err
}
func importAPIPolicy(endpoint string, importPath string, accessToken string, isOauth bool) error {
exportDirectory := filepath.Join(utils.ExportDirectory, utils.ExportedPoliciesDirName, utils.ExportedAPIPoliciesDirName)
resolvedPolicyFilePath, err := resolvePolicyImportFilePath(importPath, exportDirectory)
if err != nil {
return err
}
utils.Logln(utils.LogPrefixInfo+"Policy Location:", resolvedPolicyFilePath)
utils.Logln(utils.LogPrefixInfo + "Creating workspace")
tmpPath, err := utils.GetTempCloneFromDirOrZip(resolvedPolicyFilePath)
if err != nil {
return err
}
defer func() {
utils.Logln(utils.LogPrefixInfo+"Deleting", tmpPath)
err := os.RemoveAll(tmpPath)
if err != nil {
utils.Logln(utils.LogPrefixError + err.Error())
}
}()
files, err := ioutil.ReadDir(tmpPath)
if err != nil {
return err
}
// Make the file name platform independent
policyName := filepath.Base(tmpPath)
for _, file := range files {
originalFilePath := tmpPath + "/" + file.Name()
ext := filepath.Ext(originalFilePath)
expectedPolicyFileName := policyName + ext
isExt := ext == ".yaml" || ext == ".yml" || ext == ".json" || ext == ".j2" || ext == ".gotmpl"
if isExt && expectedPolicyFileName != file.Name() {
errorTxt := file.Name() + " should be equivalent to the policy name " + policyName
utils.HandleErrorAndExit("Policy Directory name and policy files are not consistent", errors.New(errorTxt))
}
}
utils.Logln(utils.LogPrefixInfo + "Substituting environment variables in API Policy files...")
err = replaceEnvVariablesInPolicies(tmpPath)
if err != nil {
return err
}
// if policyFilePath contains a directory, zip it. Otherwise, leave it as it is.
policyFilePath, err, cleanupFunc := utils.CreateZipFileFromProject(tmpPath, false)
if err != nil {
return err
}
//cleanup the temporary artifacts once consuming the zip file
if cleanupFunc != nil {
defer cleanupFunc()
}
resp, err := executeAPIPolicyImportRequest(endpoint, policyFilePath, accessToken, isOauth)
if err != nil {
utils.Logln(utils.LogPrefixError, err)
return err
}
var errorResponse utils.HttpErrorResponse
if resp.StatusCode() == http.StatusCreated || resp.StatusCode() == http.StatusOK {
// 201 Created or 200 OK
fmt.Println("Successfully Imported API Policy.")
return nil
} else if resp.StatusCode() == http.StatusConflict {
err := json.Unmarshal(resp.Body(), &errorResponse)
if err != nil {
return err
}
fmt.Println("Error importing API Policy due to: ", errorResponse.Description)
fmt.Println("Please change the Policy name and re-import")
if err != nil {
return err
}
return errors.New(errorResponse.Status)
} else {
fmt.Println("Error importing API Policy.")
fmt.Println("Status: " + resp.Status())
fmt.Println("Response:", resp.IsSuccess())
err := json.Unmarshal(resp.Body(), &errorResponse)
if err != nil {
return err
}
return errors.New(errorResponse.Status)
}
}
func executeAPIPolicyImportRequest(uri string, importPath string, accessToken string, isOAuthToken bool) (*resty.Response, error) {
fileParamName := "file"
headers := make(map[string]string)
if isOAuthToken {
headers[utils.HeaderAuthorization] = utils.HeaderValueAuthBearerPrefix + " " + accessToken
} else {
headers[utils.HeaderAuthorization] = utils.HeaderValueAuthBasicPrefix + " " + accessToken
}
headers[utils.HeaderAccept] = utils.HeaderValueApplicationJSON
headers[utils.HeaderConnection] = utils.HeaderValueKeepAlive
return utils.InvokePOSTRequestWithFile(uri, headers, fileParamName, importPath)
}
// resolveImportFilePath resolves the archive/directory for importing API policy
// First will resolve in given path, if not found will try to load from exported directory
func resolvePolicyImportFilePath(file, defaultExportDirectory string) (string, error) {
// check current path
utils.Logln(utils.LogPrefixInfo + "Resolving for Policy path...")
if _, err := os.Stat(file); os.IsNotExist(err) {
// if the file not in given path it might be inside exported directory
utils.Logln(utils.LogPrefixInfo+"Looking for Policy in", defaultExportDirectory)
file = filepath.Join(defaultExportDirectory, file)
if _, err := os.Stat(file); os.IsNotExist(err) {
return "", err
}
}
absPath, err := filepath.Abs(file)
if err != nil {
return "", err
}
return absPath, nil
}
// Substitutes environment variables in the project files.
func replaceEnvVariablesInPolicies(policyFilePath string) error {
for _, replacePath := range utils.EnvReplaceFilePaths {
absFile := filepath.Join(policyFilePath, replacePath)
// check if the path exists. If exists, proceed with processing. Otherwise, continue with the next items
if fi, err := os.Stat(absFile); err != nil {
if !os.IsNotExist(err) {
return err
}
} else {
switch mode := fi.Mode(); {
case mode.IsDir():
utils.Logln(utils.LogPrefixInfo+"Substituting env variables of files in folder path: ", absFile)
if strings.EqualFold(replacePath, utils.InitProjectSequences) {
err = utils.EnvSubstituteInFolder(absFile, utils.EnvReplacePoliciesFileExtensions)
} else {
err = utils.EnvSubstituteInFolder(absFile, nil)
}
case mode.IsRegular():
utils.Logln(utils.LogPrefixInfo+"Substituting env of file: ", absFile)
err = utils.EnvSubstituteInFile(absFile, nil)
}
if err != nil {
return err
}
}
}
return nil
}
// GetAPIPolicyId Get the ID of an API Policy if available
// @param accessToken : Token to call the Publisher Rest API
// @param environment : Environment where API policy needs to be located
// @param policyName : Name of the API policy
// @param policyVersion : Version of the API policy
// @return apiId, error
func GetAPIPolicyId(accessToken, environment, policyName, policyVersion string) (string, error) {
apiPolicyEndpoint := utils.GetPublisherEndpointOfEnv(environment, utils.MainConfigFilePath)
// Prepping headers
headers := make(map[string]string)
headers[utils.HeaderAuthorization] = utils.HeaderValueAuthBearerPrefix + " " + accessToken
queryParams := `query=name:` + policyName + `&version:` + policyVersion
apiPolicyEndpoint = utils.AppendSlashToString(apiPolicyEndpoint)
apiPolicyResource := "operation-policies"
url := apiPolicyEndpoint + apiPolicyResource
utils.Logln(utils.LogPrefixInfo+"GetAPIPolicy: URL:", url)
resp, err := utils.InvokeGETRequestWithQueryParamsString(url, queryParams, headers)
if err != nil {
return "", err
}
if resp.StatusCode() == http.StatusOK {
policyData := &utils.APIPoliciesList{}
data := []byte(resp.Body())
err = json.Unmarshal(data, &policyData)
if policyData.List[0].Id != "" {
return policyData.List[0].Id, err
}
return "", errors.New("Requested API Policy is not available in the Publisher. Policy: " + policyName +
" Version: " + policyVersion)
} else if resp.StatusCode() == http.StatusNotFound {
var errorResponse utils.HttpErrorResponse
err := json.Unmarshal(resp.Body(), &errorResponse)
if err != nil {
return "", err
}
return "", errors.New(errorResponse.Description)
} else {
utils.Logf("Error: %s\n", resp.Error())
utils.Logf("Body: %s\n", resp.Body())
if resp.StatusCode() == http.StatusUnauthorized {
// 401 Unauthorized
return "", fmt.Errorf("Authorization failed while getting API Policy " + policyName)
}
return "", errors.New("Request didn't respond 200 OK for getting API Policy. Status: " + resp.Status())
}
}