-
Notifications
You must be signed in to change notification settings - Fork 10
/
provision.go
570 lines (494 loc) · 16.8 KB
/
provision.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
// Copyright 2018 Google LLC
//
// 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.
package provision
import (
"bytes"
"crypto/rsa"
"crypto/tls"
"encoding/json"
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/apigee/apigee-remote-service-cli/v2/apigee"
"github.com/apigee/apigee-remote-service-cli/v2/shared"
"github.com/apigee/apigee-remote-service-envoy/v2/server"
"github.com/apigee/apigee-remote-service-golib/v2/errorset"
"github.com/lestrrat-go/jwx/jwk"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
const (
kvmName = "remote-service"
cacheName = "remote-service"
encryptKVM = true
authProxyName = "remote-service"
tokenProxyName = "remote-token"
remoteServiceProxy = "remote-service-gcp"
remoteTokenProxy = "remote-token-gcp"
)
// default durations for the proxy verification retry
var (
duration time.Duration = 180 * time.Second
interval time.Duration = 5 * time.Second
)
type provision struct {
*shared.RootArgs
forceProxyInstall bool
virtualHosts string
rotate int
runtimeType string
analyticsServiceAccount string
policySecretData map[string]string
analyticsSecretData map[string]string
}
// Cmd returns base command
func Cmd(rootArgs *shared.RootArgs, printf shared.FormatFn) *cobra.Command {
p := &provision{RootArgs: rootArgs}
c := &cobra.Command{
Use: "provision",
Short: "Provision your Apigee environment for remote services",
Long: `The provision command will set up your Apigee environment for remote services. This includes creating
and installing a remote-service kvm with certificates, creating credentials, and deploying a remote-service proxy
to your organization and environment.`,
Args: cobra.NoArgs,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := rootArgs.Resolve(false, true); err != nil {
return err
}
if !p.IsGCPManaged && p.rotate > 0 {
return fmt.Errorf(`--rotate only valid for hybrid, use 'token rotate-cert' for others`)
}
return nil
},
RunE: func(cmd *cobra.Command, _ []string) error {
if p.IsGCPManaged {
err := p.retrieveRuntimeType()
if err != nil {
return errors.Wrapf(err, "getting runtime type")
}
}
return p.run(printf)
},
}
c.Flags().StringVarP(&rootArgs.ManagementBase, "management", "m",
shared.DefaultManagementBase, "Apigee management base URL")
c.Flags().BoolVarP(&rootArgs.IsLegacySaaS, "legacy", "", false,
"Apigee SaaS (sets management and runtime URL)")
c.Flags().BoolVarP(&rootArgs.IsOPDK, "opdk", "", false,
"Apigee opdk")
c.Flags().StringVarP(&rootArgs.Token, "token", "t", "",
"Apigee OAuth or SAML token (overrides any other given credentials)")
c.Flags().StringVarP(&rootArgs.Username, "username", "u", "",
"Apigee username (legacy or opdk only)")
c.Flags().StringVarP(&rootArgs.Password, "password", "p", "",
"Apigee password (legacy or opdk only)")
c.Flags().StringVarP(&rootArgs.MFAToken, "mfa", "", "",
"Apigee multi-factor authorization token (legacy only)")
c.Flags().StringVarP(&p.analyticsServiceAccount, "analytics-sa", "", "",
"path to the service account json file (for GCP-managed analytics only)")
c.Flags().BoolVarP(&p.forceProxyInstall, "force-proxy-install", "f", false,
"force new proxy install (upgrades proxy)")
c.Flags().StringVarP(&p.virtualHosts, "virtual-hosts", "", "default,secure",
"override proxy virtualHosts")
c.Flags().StringVarP(&p.Namespace, "namespace", "n", "apigee",
"emit configuration in the specified namespace")
c.Flags().IntVarP(&p.rotate, "rotate", "", 0, "if n > 0, generate new private key and keep n public keys (hybrid only)")
return c
}
func (p *provision) run(printf shared.FormatFn) error {
var cred *keySecret
var verbosef = shared.NoPrintf
if p.Verbose {
verbosef = shared.Errorf
}
tempDir, err := os.MkdirTemp("", "apigee")
if err != nil {
return errors.Wrap(err, "creating temp dir")
}
defer os.RemoveAll(tempDir)
replaceVH := func(proxyDir string) error {
proxiesFile := filepath.Join(proxyDir, "proxies", "default.xml")
bytes, err := os.ReadFile(proxiesFile)
if err != nil {
return errors.Wrapf(err, "reading file %s", proxiesFile)
}
newVH := ""
for _, vh := range strings.Split(p.virtualHosts, ",") {
if strings.TrimSpace(vh) != "" {
newVH = newVH + fmt.Sprintf(virtualHostReplacementFmt, vh)
}
}
// remove all "secure" virtualhost
bytes = []byte(strings.ReplaceAll(string(bytes), virtualHostDeleteText, ""))
// replace the "default" virtualhost
bytes = []byte(strings.Replace(string(bytes), virtualHostReplaceText, newVH, 1))
if err := os.WriteFile(proxiesFile, bytes, 0); err != nil {
return errors.Wrapf(err, "writing file %s", proxiesFile)
}
return nil
}
replaceInFile := func(file, old, new string) error {
bytes, err := os.ReadFile(file)
if err != nil {
return errors.Wrapf(err, "reading file %s", file)
}
bytes = []byte(strings.Replace(string(bytes), old, new, 1))
if err := os.WriteFile(file, bytes, 0); err != nil {
return errors.Wrapf(err, "writing file %s", file)
}
return nil
}
// replace the version using the build info
replaceVersion := func(proxyDir string) error {
calloutFile := filepath.Join(proxyDir, "policies", "Send-Version.xml")
oldValue := `"version":"{{version}}"`
newValue := fmt.Sprintf(`"version":"%s"`, shared.BuildInfo.Version)
if err := replaceInFile(calloutFile, oldValue, newValue); err != nil {
return err
}
return nil
}
replaceVHAndAuthTarget := func(proxyDir string) error {
if err := replaceVH(proxyDir); err != nil {
return err
}
if err := replaceVersion(proxyDir); err != nil {
return err
}
if p.IsOPDK {
// OPDK must target local internal proxy
authFile := filepath.Join(proxyDir, "policies", "Authenticate-Call.xml")
oldTarget := "https://edgemicroservices.apigee.net"
newTarget := p.RuntimeBase
if err := replaceInFile(authFile, oldTarget, newTarget); err != nil {
return err
}
// OPDK must have org.noncps = true for products callout
calloutFile := filepath.Join(proxyDir, "policies", "JavaCallout.xml")
oldValue := "</Properties>"
newValue := `<Property name="org.noncps">true</Property>
</Properties>`
if err := replaceInFile(calloutFile, oldValue, newValue); err != nil {
return err
}
}
return nil
}
if p.IsOPDK {
if err := p.deployInternalProxy(replaceVH, tempDir, verbosef); err != nil {
return errors.Wrap(err, "deploying internal proxy")
}
}
// deploy remote-service proxy
var customizedProxy string
if p.IsGCPManaged {
customizedProxy, err = getCustomizedProxy(tempDir, remoteServiceProxy, replaceVersion)
} else {
customizedProxy, err = getCustomizedProxy(tempDir, legacyServiceProxy, replaceVHAndAuthTarget)
}
if err != nil {
return err
}
if err := p.checkAndDeployProxy(authProxyName, customizedProxy, p.forceProxyInstall, verbosef); err != nil {
return errors.Wrapf(err, "deploying runtime proxy %s", authProxyName)
}
// Deploy remote-token proxy
if p.IsGCPManaged {
customizedProxy, err = getCustomizedProxy(tempDir, remoteTokenProxy, nil)
} else {
customizedProxy, err = getCustomizedProxy(tempDir, legacyTokenProxy, replaceVH)
}
if err != nil {
return err
}
if err := p.checkAndDeployProxy(tokenProxyName, customizedProxy, false, verbosef); err != nil {
return errors.Wrapf(err, "deploying token proxy %s", tokenProxyName)
}
if !p.IsGCPManaged {
cred, err = p.createLegacyCredential(verbosef) // TODO: on missing or force new cred
if err != nil {
return errors.Wrapf(err, "generating credential")
}
if err := p.getOrCreateKVM(cred, verbosef); err != nil {
return errors.Wrapf(err, "retrieving or creating kvm")
}
}
config := p.ServerConfig
if config == nil {
config = p.createConfig(cred)
}
if p.IsGCPManaged && (config.Tenant.PrivateKey == nil || p.rotate > 0) {
var keyID string
var privateKey *rsa.PrivateKey
var jwks jwk.Set
var err error
if p.isCloud() { // attempt to fetch secrets from propertysets
keyID, privateKey, jwks, err = p.policySecretsFromPropertyset()
}
if err != nil || privateKey == nil {
verbosef("no existing policy secret, creating new ones")
keyID, privateKey, jwks, err = p.CreateNewKey()
if err != nil {
return err
}
}
config.Tenant.PrivateKey = privateKey
config.Tenant.PrivateKeyID = keyID
if jwks, err = p.RotateJWKS(jwks, p.rotate); err != nil {
return err
}
config.Tenant.JWKS = jwks
}
var verifyErrors error
if p.IsGCPManaged {
verifyErrors = p.verifyWithRetry(config, verbosef)
// creates the policy secrets if is GCP managed
if err := p.createPolicySecretData(config, verbosef); err != nil {
return errors.Wrapf(err, "creating policy secret data")
}
// create the analytics secrets if is GCP managed
if err := p.createAnalyticsSecretData(config, verbosef); err != nil {
return errors.Wrapf(err, "creating analytics secret data")
}
if len(p.analyticsSecretData) == 0 {
shared.Errorf("\nWARNING: No analytics service account given via --analytics-sa or config.yaml.")
shared.Errorf("\nIMPORTANT: Please make sure the application default credentials where the adapter is run are correctly configured.")
}
} else {
verifyErrors = p.verifyWithoutRetry(config, verbosef)
}
if err := p.printConfig(config, printf, verifyErrors, verbosef); err != nil {
return errors.Wrapf(err, "generating config")
}
if verifyErrors == nil {
verbosef("provisioning verified OK")
}
// return possible errors if not hybrid
if !p.IsGCPManaged || p.isCloud() {
return verifyErrors
}
// output this warning for hybrid
if p.rotate > 0 {
shared.Errorf("\nIMPORTANT: Provisioned config with rotated secrets needs to be applied onto the k8s cluster to take effect.")
} else {
shared.Errorf("\nIMPORTANT: Provisioned config needs to be applied onto the k8s cluster to take effect.")
}
return verifyErrors
}
// retrieveRuntimeType fetches the organization information from the management base and extracts the runtime type
func (p *provision) retrieveRuntimeType() error {
req, err := p.ApigeeClient.NewRequestNoEnv(http.MethodGet, "", nil)
if err != nil {
return err
}
org := &apigee.Organization{}
if _, err := p.ApigeeClient.Do(req, org); err != nil {
return err
}
p.runtimeType = org.RuntimeType
return nil
}
// isCloud determines whether it is NG SaaS
func (p *provision) isCloud() bool {
return p.IsGCPManaged && p.runtimeType == "CLOUD"
}
// policySecretsFromPropertyset retrieves the policy secret from the remote-service propertyset
// the returned values will be empty or nil if such propertyset does not exist or there is other error fetching it
func (p *provision) policySecretsFromPropertyset() (keyID string, privateKey *rsa.PrivateKey, jwks jwk.Set, err error) {
req, err := p.ApigeeClient.NewRequest(http.MethodGet, fmt.Sprintf(propertysetGETOrPUTURL, "remote-service"), nil)
if err != nil {
return
}
buf := new(bytes.Buffer)
_, err = p.ApigeeClient.Do(req, buf)
if err != nil {
return
}
// read the response into a map
m, err := server.ReadProperties(buf)
if err != nil {
return
}
// extracts the jwks from the map
jwksStr, ok := m["crt"]
if !ok {
err = fmt.Errorf("crt not found in remote-service propertyset")
return
}
jwks = jwk.NewSet()
err = json.Unmarshal([]byte(jwksStr), jwks)
if err != nil {
return
}
// extracts the private key from the map
pkStr, ok := m["key"]
if !ok {
err = fmt.Errorf("key not found in remote-service propertyset")
return
}
privateKey, err = server.LoadPrivateKey([]byte(strings.ReplaceAll(pkStr, `\n`, "\n")))
if err != nil {
return
}
// extracts the key id from the map
keyID, ok = m[server.SecretPropsKIDKey]
if !ok {
err = fmt.Errorf("kid not found in remote-service propertyset")
return
}
return
}
func (p *provision) createAuthorizedClient(config *server.Config) (*http.Client, error) {
// add authorization to transport
tr := http.DefaultTransport
if config.Tenant.TLS.AllowUnverifiedSSLCert {
tr = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
tr, err := server.AuthorizationRoundTripper(config, tr)
if err != nil {
return nil, err
}
return &http.Client{
Timeout: config.Tenant.ClientTimeout,
Transport: tr,
}, nil
}
func (p *provision) verifyWithRetry(config *server.Config, verbosef shared.FormatFn) error {
var verifyErrors error
timeout := time.After(duration)
tick := time.Tick(interval)
for {
select {
case <-timeout:
if verifyErrors != nil {
shared.Errorf("\nWARNING: Apigee may not be provisioned properly.")
shared.Errorf("Unable to verify proxy endpoint(s). Errors:\n")
for _, err := range errorset.Errors(verifyErrors) {
shared.Errorf(" %s", err)
}
shared.Errorf("\n")
}
return verifyErrors
case <-tick:
verifyErrors = p.verify(config, verbosef)
if verifyErrors == nil {
return nil
}
verbosef("verifying proxies failed, trying again...")
}
}
}
func (p *provision) verifyWithoutRetry(config *server.Config, verbosef shared.FormatFn) error {
verifyErrors := p.verify(config, verbosef)
if verifyErrors != nil {
shared.Errorf("\nWARNING: Apigee may not be provisioned properly.")
shared.Errorf("Unable to verify proxy endpoint(s). Errors:\n")
for _, err := range errorset.Errors(verifyErrors) {
shared.Errorf(" %s", err)
}
shared.Errorf("\n")
}
return verifyErrors
}
func (p *provision) verify(config *server.Config, verbosef shared.FormatFn) error {
client, err := p.createAuthorizedClient(config)
if err != nil {
return err
}
var verifyErrors error
if p.IsLegacySaaS || p.IsOPDK {
verbosef("verifying internal proxy...")
verifyErrors = p.verifyInternalProxy(client, verbosef)
}
verbosef("verifying remote-service proxy...")
verifyErrors = errorset.Append(verifyErrors, p.verifyRemoteServiceProxy(client, verbosef))
return verifyErrors
}
// verify GET /certs
// verify GET /products
// verify POST /verifyApiKey
// verify POST /quotas
func (p *provision) verifyRemoteServiceProxy(client *http.Client, printf shared.FormatFn) error {
verifyGET := func(targetURL string) error {
req, err := http.NewRequest(http.MethodGet, targetURL, nil)
if err != nil {
return errors.Wrapf(err, "creating request")
}
res, err := client.Do(req)
if res != nil {
defer res.Body.Close()
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusInternalServerError && res.StatusCode != http.StatusUnauthorized {
return fmt.Errorf("GET request to %q returns %d", targetURL, res.StatusCode)
}
}
return err
}
var res *http.Response
var verifyErrors error
err := verifyGET(p.GetCertsURL())
verifyErrors = errorset.Append(verifyErrors, err)
err = verifyGET(p.GetProductsURL())
verifyErrors = errorset.Append(verifyErrors, err)
verifyAPIKeyURL := p.GetVerifyAPIKeyURL()
req, err := http.NewRequest(http.MethodPost, verifyAPIKeyURL, strings.NewReader(`{ "apiKey": "x" }`))
if err == nil {
req.Header.Add("Content-Type", "application/json")
res, err = client.Do(req)
if res != nil {
defer res.Body.Close()
if res.StatusCode != http.StatusUnauthorized && res.StatusCode != http.StatusInternalServerError { // 401 or 500 is ok, either the secret is not there or we didn't use a valid api key
verifyErrors = errorset.Append(verifyErrors, fmt.Errorf("POST request to %q returns %d", verifyAPIKeyURL, res.StatusCode))
}
}
}
if err != nil {
verifyErrors = errorset.Append(verifyErrors, err)
}
quotasURL := p.GetQuotasURL()
req, err = http.NewRequest(http.MethodPost, quotasURL, strings.NewReader("{}"))
if err == nil {
req.Header.Add("Content-Type", "application/json")
res, err = client.Do(req)
if res != nil {
defer res.Body.Close()
if res.StatusCode != http.StatusUnauthorized && res.StatusCode != http.StatusOK {
verifyErrors = errorset.Append(verifyErrors, fmt.Errorf("POST request to %q returns %d", quotasURL, res.StatusCode))
}
}
}
if err != nil {
verifyErrors = errorset.Append(verifyErrors, err)
}
return verifyErrors
}
type keySecret struct {
Key string `json:"key"`
Secret string `json:"secret"`
}