-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathcommand.go
More file actions
258 lines (215 loc) · 7.22 KB
/
command.go
File metadata and controls
258 lines (215 loc) · 7.22 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
package apikey
import (
"fmt"
"strings"
"github.com/spf13/cobra"
ccloudv1 "github.com/confluentinc/ccloud-sdk-go-v1-public"
apikeysv2 "github.com/confluentinc/ccloud-sdk-go-v2/apikeys/v2"
"github.com/confluentinc/cli/v4/pkg/ccloudv2"
pcmd "github.com/confluentinc/cli/v4/pkg/cmd"
"github.com/confluentinc/cli/v4/pkg/errors"
"github.com/confluentinc/cli/v4/pkg/kafka"
"github.com/confluentinc/cli/v4/pkg/keystore"
presource "github.com/confluentinc/cli/v4/pkg/resource"
)
type command struct {
*pcmd.AuthenticatedCLICommand
keystore *keystore.ConfigKeyStore
}
const (
deleteOperation = "deleting"
getOperation = "getting"
updateOperation = "updating"
)
const (
apiKeyNotValidForClusterSuggestions = "Specify the cluster this API key belongs to using the `--resource` flag. Alternatively, first execute the `confluent kafka cluster use` command to set the context to the proper cluster for this key and retry the `confluent api-key store` command."
apiKeyUseFailedErrorMsg = "unable to set active API key"
apiKeyUseFailedSuggestions = "If you did not create this API key with the CLI or created it on another computer, you must first store the API key and secret locally with `confluent api-key store %s <secret>`."
nonKafkaNotImplementedErrorMsg = "functionality not yet available for non-Kafka cluster resources"
refuseToOverrideSecretSuggestions = "If you would like to override the existing secret stored for API key \"%s\", use the `--force` flag."
unableToStoreApiKeyErrorMsg = "unable to store API key locally: %w"
)
func New(prerunner pcmd.PreRunner) *cobra.Command {
cmd := &cobra.Command{
Use: "api-key",
Short: "Manage API keys.",
Annotations: map[string]string{pcmd.RunRequirement: pcmd.RequireNonAPIKeyCloudLogin},
}
c := &command{AuthenticatedCLICommand: pcmd.NewAuthenticatedCLICommand(cmd, prerunner)}
cmd.AddCommand(c.newCreateCommand())
cmd.AddCommand(c.newDeleteCommand())
cmd.AddCommand(c.newDescribeCommand())
cmd.AddCommand(c.newListCommand())
cmd.AddCommand(c.newStoreCommand())
cmd.AddCommand(c.newUpdateCommand())
cmd.AddCommand(c.newUseCommand())
return cmd
}
func (c *command) addResourceFlag(cmd *cobra.Command, isStore bool) {
description := "The ID of the resource the API key is for."
if !isStore {
description += ` Use "cloud" for a Cloud API key, "global" for a Global API key, "flink" for a Flink API key, or "tableflow" for a Tableflow API key.`
}
cmd.Flags().String("resource", "", description)
pcmd.RegisterFlagCompletionFunc(cmd, "resource", func(cmd *cobra.Command, args []string) []string {
if err := c.PersistentPreRunE(cmd, args); err != nil {
return nil
}
environmentId, err := c.Context.EnvironmentId()
if err != nil {
return nil
}
kafkaClusters, err := c.V2Client.ListKafkaClusters(environmentId)
if err != nil {
return nil
}
schemaRegistryClusters, err := c.V2Client.GetSchemaRegistryClustersByEnvironment(environmentId)
if err != nil {
return nil
}
ksqlClusters, err := c.V2Client.ListKsqlClusters(environmentId)
if err != nil {
return nil
}
suggestions := make([]string, len(kafkaClusters)+len(schemaRegistryClusters)+len(ksqlClusters))
i := 0
for _, cluster := range kafkaClusters {
suggestions[i] = fmt.Sprintf("%s\t%s", cluster.GetId(), cluster.Spec.GetDisplayName())
i++
}
for _, cluster := range schemaRegistryClusters {
suggestions[i] = fmt.Sprintf("%s\t%s", cluster.GetId(), cluster.Spec.GetDisplayName())
i++
}
for _, cluster := range ksqlClusters {
suggestions[i] = fmt.Sprintf("%s\t%s", cluster.GetId(), cluster.Spec.GetDisplayName())
i++
}
if !isStore {
suggestions = append(suggestions, "cloud")
suggestions = append(suggestions, "global")
suggestions = append(suggestions, "flink")
suggestions = append(suggestions, "tableflow")
}
return suggestions
})
}
func (c *command) setKeyStoreIfNil() {
if c.keystore == nil {
c.keystore = &keystore.ConfigKeyStore{Config: c.Config}
}
}
func (c *command) validArgs(cmd *cobra.Command, args []string) []string {
if len(args) > 0 {
return nil
}
return c.validArgsMultiple(cmd, args)
}
func (c *command) validArgsMultiple(cmd *cobra.Command, args []string) []string {
if err := c.PersistentPreRunE(cmd, args); err != nil {
return nil
}
return pcmd.AutocompleteApiKeys(c.V2Client)
}
func (c *command) getAllUsers() ([]*ccloudv1.User, error) {
users, err := c.Client.User.GetServiceAccounts()
if err != nil {
return nil, err
}
user, err := c.Client.Auth.User()
if err != nil {
return nil, err
}
if auditLog := user.GetOrganization().GetAuditLog(); auditLog.GetServiceAccountId() != 0 {
serviceAccount, err := c.Client.User.GetServiceAccount(auditLog.GetServiceAccountId())
if err != nil {
// ignore 403s so we can still get other users
if !strings.Contains(err.Error(), "Forbidden Access") {
return nil, err
}
} else {
users = append(users, serviceAccount)
}
}
adminUsers, err := c.Client.User.List()
if err != nil {
return nil, err
}
users = append(users, adminUsers...)
if currentUser := c.Context.GetUser(); currentUser != nil {
users = append(users, currentUser)
}
return users, nil
}
func (c *command) resolveResourceId(cmd *cobra.Command, v2Client *ccloudv2.Client) (string, string, string, error) {
resource, err := cmd.Flags().GetString("resource")
if err != nil {
return "", "", "", err
}
if resource == "" {
return "", "", "", nil
}
resourceType := presource.LookupType(resource)
var clusterId string
var apiKey string
switch resourceType {
case presource.Cloud, presource.Flink, presource.Tableflow, presource.Global:
break
case presource.KafkaCluster:
cluster, err := kafka.FindCluster(c.V2Client, c.Context, resource)
if err != nil {
return "", "", "", errors.CatchResourceNotFoundError(err, resource)
}
clusterId = cluster.ID
apiKey = cluster.APIKey
case presource.KsqlCluster:
environmentId, err := c.Context.EnvironmentId()
if err != nil {
return "", "", "", err
}
cluster, err := v2Client.DescribeKsqlCluster(resource, environmentId)
if err != nil {
return "", "", "", errors.CatchResourceNotFoundError(err, resource)
}
clusterId = cluster.GetId()
case presource.SchemaRegistryCluster:
environmentId, err := c.Context.EnvironmentId()
if err != nil {
return "", "", "", err
}
cluster, err := v2Client.GetSchemaRegistryClusterById(resource, environmentId)
if err != nil {
return "", "", "", errors.CatchResourceNotFoundError(err, resource)
}
clusterId = cluster.GetId()
default:
return "", "", "", fmt.Errorf(`unsupported resource type for resource "%s"`, resource)
}
return resourceType, clusterId, apiKey, nil
}
func getResourceType(resource apikeysv2.ObjectReference) string {
switch resource.GetKind() {
case "Cloud":
return "cloud"
case "Global":
return "global"
case "Cluster":
if getResourceApi(resource) == "cmk" {
return "kafka"
}
case "ksqlDB":
return "ksql"
case "Region":
if getResourceApi(resource) == "fcpm" {
return "flink-region"
}
case "SchemaRegistry":
return "schema-registry"
case "Tableflow":
return "tableflow"
}
return ""
}
func getResourceApi(resource apikeysv2.ObjectReference) string {
return strings.Split(resource.GetApiVersion(), "/")[0]
}