Skip to content

Commit

Permalink
Merge branch '2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
brianstrauch committed Sep 10, 2021
2 parents 37f5705 + 9876553 commit 348f099
Show file tree
Hide file tree
Showing 100 changed files with 1,649 additions and 570 deletions.
2 changes: 1 addition & 1 deletion cmd/lint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var (
"Schema Registry", "ZooKeeper", "ZooKeeper™", "cku",
}
vocabWords = []string{
"ack", "acks", "acl", "acls", "apac", "api", "auth", "avro", "aws", "backoff", "ccloud", "cku", "cli", "codec",
"ack", "acks", "acl", "acls", "apac", "api", "apikey", "apisecret", "auth", "avro", "aws", "backoff", "ccloud", "cku", "cli", "codec",
"config", "configs", "connect", "connect-catalog", "consumer.config", "crn", "csu", "decrypt", "deserializer",
"deserializers", "env", "eu", "formatter", "gcp", "geo", "gzip", "hostname", "html", "https", "iam", "init",
"io", "json", "jsonschema", "kafka", "ksql", "lifecycle", "lz4", "mds", "multi-zone", "netrc", "pem",
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
github.com/confluentinc/mds-sdk-go/mdsv1 v0.0.27
github.com/confluentinc/mds-sdk-go/mdsv2alpha1 v0.0.27
github.com/confluentinc/properties v0.0.0-20190814194548-42c10394a787
github.com/confluentinc/schema-registry-sdk-go v0.0.9
github.com/confluentinc/schema-registry-sdk-go v0.0.11
github.com/davecgh/go-spew v1.1.1
github.com/dghubble/sling v1.3.0
github.com/eapache/go-resiliency v1.2.0 // indirect
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,8 @@ github.com/confluentinc/proto-go-setter v0.0.0-20180912191759-fb17e76fc076/go.mo
github.com/confluentinc/protoc-gen-ccloud v0.0.3 h1:i2+J04C4iW1hoe0tvzWH8wXg05rmxN4G7Qz1LpusEBE=
github.com/confluentinc/protoc-gen-ccloud v0.0.3/go.mod h1:aZRVEnVpDqGKlGNaR75eWMcMzXOFDDps7ByPJiPPwxg=
github.com/confluentinc/prototools v0.0.3/go.mod h1:BeYyZpaHqLMJ3uFf5aGl30F6xnideWJLd0HsGm9QY2U=
github.com/confluentinc/schema-registry-sdk-go v0.0.9 h1:Ux5Ht1rk5soi+Zf/cw1z0Je76qMrV0LqyoXfRYZolFk=
github.com/confluentinc/schema-registry-sdk-go v0.0.9/go.mod h1:+3hPLr84tBmDdQV1XSvo3H9FZwL3+Nx9MGbm17QyXq0=
github.com/confluentinc/schema-registry-sdk-go v0.0.11 h1:chWZyfO6QxpfUFkF9aQfM5FF08SuIlGVXNVLLlSeNjw=
github.com/confluentinc/schema-registry-sdk-go v0.0.11/go.mod h1:+3hPLr84tBmDdQV1XSvo3H9FZwL3+Nx9MGbm17QyXq0=
github.com/containerd/continuity v0.0.0-20190426062206-aaeac12a7ffc/go.mod h1:GL3xCUCBDV3CZiTSEKksMWbLE66hEyuu9qyDOOqM47Y=
github.com/containerd/continuity v0.0.0-20191127005431-f65d91d395eb/go.mod h1:GL3xCUCBDV3CZiTSEKksMWbLE66hEyuu9qyDOOqM47Y=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
Expand Down Expand Up @@ -1494,7 +1494,6 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 h1:SQFwaSi55rU7vdNs9Yr0Z324VNlrF+0wMqRXT4St8ck=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20170830134202-bb24a47a89ea/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180816055513-1c9583448a9c/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
30 changes: 3 additions & 27 deletions internal/cmd/admin/command_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@ import (
)

var (
listFields = []string{"Id", "ResourceId", "Email", "FirstName", "LastName", "Status", "AuthenticationMethod"}
humanLabels = []string{"Id", "Resource ID", "Email", "First Name", "Last Name", "Status", "Authentication Method"}
listFields = []string{"ResourceId", "Email", "FirstName", "LastName", "Status", "AuthenticationMethod"}
humanLabels = []string{"Resource ID", "Email", "First Name", "Last Name", "Status", "Authentication Method"}
humanLabelMap = map[string]string{
"Id": "Id",
"ResourceId": "Resource ID",
"Email": "Email",
"FirstName": "First Name",
"LastName": "Last Name",
"Status": "Status",
"AuthenticationMethod": "Authentication Method",
}
structuredLabels = []string{"id", "resource_id", "email", "first_name", "last_name", "status", "authentication_method"}
structuredLabels = []string{"resource_id", "email", "first_name", "last_name", "status", "authentication_method"}
structuredLabelMap = map[string]string{
"Id": "id",
"ResourceId": "resource_id",
"Email": "email",
"FirstName": "first_name",
Expand All @@ -57,7 +55,6 @@ type userCommand struct {
}

type userStruct struct {
Id int32
ResourceId string
Email string
FirstName string
Expand Down Expand Up @@ -109,11 +106,6 @@ func (c userCommand) describe(cmd *cobra.Command, args []string) error {
return err
}

userId, err := c.getUserIdWithResourceId(resourceId)
if err != nil {
return err
}

// Avoid panics if new types of statuses are added in the future
userStatus := "Unknown"
if val, ok := statusMap[userProfile.UserStatus]; ok {
Expand All @@ -128,7 +120,6 @@ func (c userCommand) describe(cmd *cobra.Command, args []string) error {
}

return output.DescribeObject(cmd, &userStruct{
Id: userId,
ResourceId: userProfile.ResourceId,
Email: userProfile.Email,
FirstName: userProfile.FirstName,
Expand Down Expand Up @@ -182,7 +173,6 @@ func (c userCommand) list(cmd *cobra.Command, _ []string) error {
}

outputWriter.AddElement(&userStruct{
Id: user.Id,
ResourceId: userProfile.ResourceId,
Email: userProfile.Email,
FirstName: userProfile.FirstName,
Expand Down Expand Up @@ -244,17 +234,3 @@ func (c userCommand) delete(cmd *cobra.Command, args []string) error {
utils.Println(cmd, fmt.Sprintf(errors.DeletedUserMsg, resourceId))
return nil
}

func (c userCommand) getUserIdWithResourceId(resourceId string) (int32, error) {
var userId int32
users, err := c.Client.User.List(context.Background())
if err != nil {
return 0, err
}
for _, user := range users {
if user.ResourceId == resourceId {
userId = user.Id
}
}
return userId, nil
}
90 changes: 34 additions & 56 deletions internal/cmd/api-key/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package apikey
import (
"context"
"fmt"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -53,9 +52,9 @@ type command struct {
}

var (
listFields = []string{"Key", "Description", "UserId", "UserResourceId", "UserEmail", "ResourceType", "ResourceId", "Created"}
listHumanLabels = []string{"Key", "Description", "Owner", "Owner Resource Id", "Owner Email", "Resource Type", "Resource ID", "Created"}
listStructuredLabels = []string{"key", "description", "owner", "owner_resource_id", "owner_email", "resource_type", "resource_id", "created"}
listFields = []string{"Key", "Description", "UserResourceId", "UserEmail", "ResourceType", "ResourceId", "Created"}
listHumanLabels = []string{"Key", "Description", "Owner Resource Id", "Owner Email", "Resource Type", "Resource ID", "Created"}
listStructuredLabels = []string{"key", "description", "owner_resource_id", "owner_email", "resource_type", "resource_id", "created"}
createFields = []string{"Key", "Secret"}
createHumanRenames = map[string]string{"Key": "API Key"}
createStructuredRenames = map[string]string{"Key": "key", "Secret": "secret"}
Expand Down Expand Up @@ -103,6 +102,7 @@ func (c *command) init() {
createCmd := &cobra.Command{
Use: "create",
Short: "Create API keys for a given resource.",
Long: "Create API keys for a given resource. A resource is some Confluent product or service for which an API key can be created, for example ksqlDB application ID, or \"cloud\" to create a Cloud API key.",
Args: cobra.NoArgs,
RunE: pcmd.NewCLIRunE(c.create),
Example: examples.BuildExampleString(
Expand Down Expand Up @@ -176,7 +176,6 @@ func (c *command) list(cmd *cobra.Command, _ []string) error {
type keyDisplay struct {
Key string
Description string
UserId int32
UserResourceId string
UserEmail string
ResourceType string
Expand All @@ -194,7 +193,7 @@ func (c *command) list(cmd *cobra.Command, _ []string) error {
logicalClusters = []*schedv1.ApiKey_Cluster{{Id: resourceId, Type: resourceType}}
}

serviceAccountId, err := cmd.Flags().GetString("service-account")
serviceAccountID, err := cmd.Flags().GetString("service-account")
if err != nil {
return err
}
Expand All @@ -211,21 +210,17 @@ func (c *command) list(cmd *cobra.Command, _ []string) error {

userId := int32(0)
serviceAccount := false
if serviceAccountId != "" {
if serviceAccountID != "" { // if user inputs resource ID, get corresponding numeric ID
serviceAccount = true
if isResourceId(serviceAccountId) { // if user inputs resource ID, get corresponding numeric ID
userIdMap, err := c.mapResourceIdToUserId(allUsers)
if err != nil {
return err
}
var ok bool
userId, ok = userIdMap[serviceAccountId]
if !ok {
return errors.NewErrorWithSuggestions(fmt.Sprintf(errors.ServiceAccountNotFoundErrorMsg, serviceAccountId), errors.ServiceAccountNotFoundSuggestions)
}
} else { // if user inputs numeric ID, convert it to int32
userIdp, _ := strconv.Atoi(serviceAccountId)
userId = int32(userIdp)
validFormat := strings.HasPrefix(serviceAccountID, "sa-")
if !validFormat {
return errors.New(errors.BadServiceAccountIDErrorMsg)
}
userIdMap := c.mapResourceIdToUserId(allUsers)
var ok bool
userId, ok = userIdMap[serviceAccountID]
if !ok {
return errors.NewErrorWithSuggestions(fmt.Sprintf(errors.ServiceAccountNotFoundErrorMsg, serviceAccountID), errors.ServiceAccountNotFoundSuggestions)
}
}

Expand All @@ -251,10 +246,7 @@ func (c *command) list(cmd *cobra.Command, _ []string) error {

serviceAccountsMap := getServiceAccountsMap(serviceAccounts)
usersMap := getUsersMap(users)
resourceIdMap, err := c.mapUserIdToResourceId(allUsers)
if err != nil {
return err
}
resourceIdMap := c.mapUserIdToResourceId(allUsers)

for _, apiKey := range apiKeys {
// ignore keys owned by Confluent-internal user (healthcheck, etc)
Expand Down Expand Up @@ -300,7 +292,6 @@ func (c *command) list(cmd *cobra.Command, _ []string) error {
outputWriter.AddElement(&keyDisplay{
Key: outputKey,
Description: apiKey.Description,
UserId: apiKey.UserId,
UserResourceId: userResourceId,
UserEmail: email,
ResourceType: pcmd.CloudResourceType,
Expand All @@ -316,7 +307,6 @@ func (c *command) list(cmd *cobra.Command, _ []string) error {
outputWriter.AddElement(&keyDisplay{
Key: outputKey,
Description: apiKey.Description,
UserId: apiKey.UserId,
UserResourceId: userResourceId,
UserEmail: email,
ResourceType: lc.Type,
Expand Down Expand Up @@ -378,7 +368,7 @@ func (c *command) create(cmd *cobra.Command, _ []string) error {
if err != nil {
return err
}
serviceAccountId, err := cmd.Flags().GetString("service-account")
serviceAccountID, err := cmd.Flags().GetString("service-account")
if err != nil {
return err
}
Expand All @@ -389,11 +379,12 @@ func (c *command) create(cmd *cobra.Command, _ []string) error {
}

key := &schedv1.ApiKey{
Description: description,
AccountId: c.EnvironmentId(),
UserResourceId: serviceAccountID,
Description: description,
AccountId: c.EnvironmentId(),
}

key, err = c.completeKeyId(key, serviceAccountId) // get corresponding numeric/resource ID if the cmd has a service-account flag
key, err = c.completeKeyUserId(key) // get corresponding numeric ID if the cmd has a service-account flag
if err != nil {
return err
}
Expand Down Expand Up @@ -668,27 +659,19 @@ func (c *command) getAllUsers() ([]*orgv1.User, error) {
return append(serviceAccounts, adminUsers...), nil
}

func (c *command) completeKeyId(key *schedv1.ApiKey, Id string) (*schedv1.ApiKey, error) {
if Id != "" { // it has a service-account flag
key.ServiceAccount = true
func (c *command) completeKeyUserId(key *schedv1.ApiKey) (*schedv1.ApiKey, error) {
if key.UserResourceId != "" { // it has a service-account flag
validFormat := strings.HasPrefix(key.UserResourceId, "sa-")
if !validFormat {
return nil, errors.New(errors.BadServiceAccountIDErrorMsg)
}
users, err := c.getAllUsers()
if err != nil {
return key, err
}
idp, err := strconv.Atoi(Id)
if err != nil { // it's a resource id
key.UserResourceId = Id
for _, user := range users {
if Id == user.ResourceId {
key.UserId = user.Id
}
}
} else { // it's a numeric id
key.UserId = int32(idp)
for _, user := range users {
if int32(idp) == user.Id {
key.UserResourceId = user.ResourceId
}
for _, user := range users {
if key.UserResourceId == user.ResourceId {
key.UserId = user.Id
}
}
} else {
Expand All @@ -697,23 +680,18 @@ func (c *command) completeKeyId(key *schedv1.ApiKey, Id string) (*schedv1.ApiKey
return key, nil
}

func (c *command) mapUserIdToResourceId(users []*orgv1.User) (map[int32]string, error) {
func (c *command) mapUserIdToResourceId(users []*orgv1.User) map[int32]string {
idMap := make(map[int32]string)
for _, user := range users {
idMap[user.Id] = user.ResourceId
}
return idMap, nil
return idMap
}

func (c *command) mapResourceIdToUserId(users []*orgv1.User) (map[string]int32, error) {
func (c *command) mapResourceIdToUserId(users []*orgv1.User) map[string]int32 {
idMap := make(map[string]int32)
for _, user := range users {
idMap[user.ResourceId] = user.Id
}
return idMap, nil
}

func isResourceId(Id string) bool {
_, err := strconv.Atoi(Id)
return err != nil
return idMap
}
Loading

0 comments on commit 348f099

Please sign in to comment.