/
registry_add_gar.go
121 lines (98 loc) · 3.75 KB
/
registry_add_gar.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
package cmd
import (
"strings"
"github.com/pkg/errors"
"github.com/replicatedhq/replicated/cli/print"
"github.com/replicatedhq/replicated/pkg/kotsclient"
"github.com/replicatedhq/replicated/pkg/types"
"github.com/spf13/cobra"
)
// Google Artifact Registry
func (r *runners) InitRegistryAddGAR(parent *cobra.Command) {
cmd := &cobra.Command{
Use: "gar",
Short: "Add a Google Artifact Registry",
Long: `Add a Google Artifact Registry using a service account key`,
SilenceUsage: true,
}
parent.AddCommand(cmd)
cmd.Flags().StringVar(&r.args.addRegistryEndpoint, "endpoint", "", "The GAR endpoint")
cmd.Flags().StringVar(&r.args.addRegistryAuthType, "authtype", "serviceaccount", "Auth type for the registry")
cmd.Flags().StringVar(&r.args.addRegistryServiceAccountKey, "serviceaccountkey", "", "The service account key to authenticate to the registry with")
cmd.Flags().BoolVar(&r.args.addRegistryServiceAccountKeyFromStdIn, "serviceaccountkey-stdin", false, "Take the service account key from stdin")
cmd.Flags().StringVar(&r.args.addRegistryToken, "token", "", "The token to use to auth to the registry with")
cmd.Flags().BoolVar(&r.args.addRegistryTokenFromStdIn, "token-stdin", false, "Take the token from stdin")
cmd.Flags().StringVar(&r.outputFormat, "output", "table", "The output format to use. One of: json|table (default: table)")
cmd.RunE = r.registryAddGAR
}
func (r *runners) registryAddGAR(cmd *cobra.Command, args []string) error {
if r.args.addRegistryServiceAccountKeyFromStdIn {
var err error
serviceAccountKey, err := r.readPasswordFromStdIn("Service Account Key")
if err != nil {
return errors.Wrap(err, "read secret service account key from stdin")
}
r.args.addRegistryServiceAccountKey = serviceAccountKey
}
if r.args.addRegistryTokenFromStdIn {
var err error
token, err := r.readPasswordFromStdIn("Token")
if err != nil {
return errors.Wrap(err, "read token from stdin")
}
r.args.addRegistryToken = token
}
addRegistryRequest, errs := r.validateRegistryAddGAR()
if len(errs) > 0 {
joinedErrs := []string{}
for _, err := range errs {
joinedErrs = append(joinedErrs, err.Error())
}
return errors.New(strings.Join(joinedErrs, ", "))
}
addRegistryRequest.SkipValidation = r.args.addRegistrySkipValidation
kotsRestClient := kotsclient.VendorV3Client{HTTPClient: *r.platformAPI}
registry, err := kotsRestClient.AddKOTSRegistry(addRegistryRequest)
if err != nil {
return errors.Wrap(err, "add registry")
}
registries := []types.Registry{
*registry,
}
return print.Registries(r.outputFormat, r.w, registries)
}
func (r *runners) validateRegistryAddGAR() (kotsclient.AddKOTSRegistryRequest, []error) {
req := kotsclient.AddKOTSRegistryRequest{
Provider: "gar",
AuthType: "serviceaccount",
Username: "_json_key",
}
errs := []error{}
if r.args.addRegistryEndpoint == "" {
errs = append(errs, errors.New("endpoint must be specified"))
} else {
req.Endpoint = r.args.addRegistryEndpoint
}
supportedAuthTypes := []string{"serviceaccount", "token"}
if !contains(supportedAuthTypes, r.args.addRegistryAuthType) {
errs = append(errs, errors.New("authtype must be one of: serviceaccount, token"))
} else {
req.AuthType = r.args.addRegistryAuthType
}
if req.AuthType == "serviceaccount" {
if r.args.addRegistryServiceAccountKey == "" {
errs = append(errs, errors.New("serviceaccountkey or serviceaccountkey-stdin must be specified"))
} else {
req.Username = "_json_key"
req.Password = r.args.addRegistryServiceAccountKey
}
} else if req.AuthType == "token" {
if r.args.addRegistryToken == "" {
errs = append(errs, errors.New("token is required"))
} else {
req.Username = "oauth2accesstoken"
req.Password = r.args.addRegistryToken
}
}
return req, errs
}