Skip to content

Commit

Permalink
sync(cmd/access): pull from indent core
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 945ecd7e32ab5e9f8d9dae3be32f76544c3faf5a
  • Loading branch information
indent-bot committed Jan 10, 2024
1 parent b2ce589 commit fe812b1
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 2 deletions.
7 changes: 6 additions & 1 deletion cmd/access/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
configcmd "go.indent.com/access/cmd/access/cmd/config"
"go.indent.com/access/cmd/access/cmd/petitions"
"go.indent.com/access/cmd/access/cmd/resources"
"go.indent.com/access/cmd/access/cmd/tokens"
"go.indent.com/indent-go/pkg/cliutil"
)

Expand All @@ -25,12 +26,16 @@ func NewRoot(logger *zap.Logger) *cobra.Command {
rootCmd.AddCommand(configcmd.NewCmdConfig(f))
rootCmd.AddCommand(petitions.NewCmdPetitions(f))
rootCmd.AddCommand(resources.NewCmdResources(f))
rootCmd.AddCommand(tokens.NewCmdTokens(f))

flags := rootCmd.PersistentFlags()
flags.StringVarP(&config.Space, "space", "s", config.Space, "Space to perform operations in")
flags.BoolVar(&config.Staging, "staging", config.Staging, "Use staging environment for request")
flags.BoolVarP(&config.Verbose, "verbose", "v", config.Verbose, "Include debug messages and additional context in logs")
flags.BoolVar(&config.Headless, "headless", config.Headless, "Run in headless mode (no browser login prompt)")
f.Setup()

rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
f.Setup()
}
return rootCmd
}
130 changes: 130 additions & 0 deletions cmd/access/cmd/tokens/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package tokens

import (
"context"
"strconv"

"github.com/spf13/cobra"
"go.uber.org/zap"

indentv1 "go.indent.com/indent-go/api/indent/v1"
"go.indent.com/indent-go/pkg/cliutil"
"go.indent.com/indent-go/pkg/common"
)

const (
// defaultServiceAccount is the default service account to use when creating tokens.
defaultServiceAccount = "access-cli-default-service-account"

// defaultServiceAccountDisplayName is the default service account display name to use when creating tokens.
defaultServiceAccountDisplayName = "Default Service Account"

// defaultExpiryDays is the default number of days until a token expires.
defaultExpiryDays = 25
)

// NewCreateOptions returns a CreateOptions with the defaults set.
func NewCreateOptions() *CreateOptions {
return &CreateOptions{
CreateTokenRequest: new(indentv1.CreateTokenRequest),

Check failure on line 29 in cmd/access/cmd/tokens/create.go

View workflow job for this annotation

GitHub Actions / build

undefined: indentv1.CreateTokenRequest
}
}

// CreateOptions defines how a token is created.
type CreateOptions struct {
*indentv1.CreateTokenRequest

Check failure on line 35 in cmd/access/cmd/tokens/create.go

View workflow job for this annotation

GitHub Actions / build

undefined: indentv1.CreateTokenRequest

// CreateAccessToken indicates that an access token should be created.
CreateAccessToken bool
}

// NewCmdCreate returns a command that creates tokens.
func NewCmdCreate(f cliutil.Factory) *cobra.Command {
opts := NewCreateOptions()
cmd := &cobra.Command{
Use: "create [service_account]",
Short: "Creates new refresh tokens",
Long: `Creates new refresh tokens for a service account`,
Run: func(cmd *cobra.Command, args []string) {
logger := f.Logger()
client := f.API(cmd.Context()).Accounts()

Check failure on line 50 in cmd/access/cmd/tokens/create.go

View workflow job for this annotation

GitHub Actions / build

f.API(cmd.Context()).Accounts undefined (type cliutil.APIClient has no field or method Accounts)

opts.ServiceAccountId = serviceAccountID(cmd.Context(), logger, f, client, args)

Check failure on line 52 in cmd/access/cmd/tokens/create.go

View workflow job for this annotation

GitHub Actions / build

opts.ServiceAccountId undefined (type *CreateOptions has no field or method ServiceAccountId)
logger = logger.With(zap.Uint64("serviceAccountID", opts.ServiceAccountId))

Check failure on line 53 in cmd/access/cmd/tokens/create.go

View workflow job for this annotation

GitHub Actions / build

opts.ServiceAccountId undefined (type *CreateOptions has no field or method ServiceAccountId)

logger.Debug("Creating token")
opts.SpaceName = f.Config().Space

Check failure on line 56 in cmd/access/cmd/tokens/create.go

View workflow job for this annotation

GitHub Actions / build

opts.SpaceName undefined (type *CreateOptions has no field or method SpaceName)
token, err := client.CreateToken(cmd.Context(), opts.CreateTokenRequest)
if err != nil {
logger.Fatal("Failed to create token", zap.Error(err))
}

logger = logger.With(zap.String("refreshToken", token.GetRefreshToken()))
logger.Info("Refresh token created")

if opts.CreateAccessToken {
logger.Debug("Creating access token")
tokenClient := f.API(cmd.Context()).Tokens()

Check failure on line 67 in cmd/access/cmd/tokens/create.go

View workflow job for this annotation

GitHub Actions / build

f.API(cmd.Context()).Tokens undefined (type cliutil.APIClient has no field or method Tokens)
accessToken, err := tokenClient.ExchangeToken(cmd.Context(), &indentv1.ExchangeTokenRequest{

Check failure on line 68 in cmd/access/cmd/tokens/create.go

View workflow job for this annotation

GitHub Actions / build

undefined: indentv1.ExchangeTokenRequest
RefreshToken: token.GetRefreshToken(),
})
if err != nil {
logger.Fatal("Failed to create access token", zap.Error(err))
}

logger = logger.With(zap.String("accessToken", accessToken.GetAccessToken()))
logger.Info("Access token created")
}
},
}

flags := cmd.Flags()
flags.Uint64Var(&opts.ExpiryDays, "expiry-days", defaultExpiryDays, "Number of days until token expires")

Check failure on line 82 in cmd/access/cmd/tokens/create.go

View workflow job for this annotation

GitHub Actions / build

opts.ExpiryDays undefined (type *CreateOptions has no field or method ExpiryDays)
flags.BoolVar(&opts.CreateAccessToken, "access-token", false, "Create an access token also")
return cmd
}

func serviceAccountID(ctx context.Context, logger *zap.Logger, f cliutil.Factory, client indentv1.AccountAPIClient,
args []string) (svcAccountID uint64) {
var err error
if len(args) != 0 {
if svcAccountID, err = strconv.ParseUint(args[0], common.Base10, common.BitSize64); err != nil {

Check failure on line 91 in cmd/access/cmd/tokens/create.go

View workflow job for this annotation

GitHub Actions / build

undefined: common.Base10
logger.Fatal("Failed to parse service account ID", zap.Error(err))
}
}

// if service account is specified, use it
if svcAccountID != 0 {
return svcAccountID
}

// use default service account if none specified
logger.Debug("Looking up default service account")
space := f.Config().Space
var resp *indentv1.ListServiceAccountsResponse
if resp, err = client.ListServicesAccounts(ctx, &indentv1.ListServiceAccountsRequest{
SpaceName: space,
}); err != nil {
logger.Fatal("Failed to list service accounts", zap.Error(err))
}

for _, serviceAccount := range resp.GetAccounts() {
meta := serviceAccount.GetMeta()
if meta.GetSpace() == space && meta.GetName() == defaultServiceAccount {
return serviceAccount.GetServiceAccountId()
}
}

logger.Debug("Creating default service account")
var svcAcct *indentv1.ServiceAccount
if svcAcct, err = client.CreateServiceAccount(ctx, &indentv1.CreateServiceAccountRequest{
SpaceName: space,
Name: defaultServiceAccount,
DisplayName: defaultServiceAccountDisplayName,
}); err != nil {
logger.Fatal("Failed to create default service account", zap.Error(err))
}
svcAccountID = svcAcct.GetServiceAccountId()
logger.Debug("Default service account created", zap.Uint64("svcAccountID", svcAccountID))
return svcAccountID
}
19 changes: 19 additions & 0 deletions cmd/access/cmd/tokens/tokens.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Package tokens is a command to manage tokens.
package tokens

import (
"github.com/spf13/cobra"

"go.indent.com/indent-go/pkg/cliutil"
)

// NewCmdTokens returns a set of commands used to manage tokens.
func NewCmdTokens(f cliutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "tokens",
Aliases: []string{"t"},
Short: "Manage tokens",
}
cmd.AddCommand(NewCmdCreate(f))
return cmd
}
2 changes: 1 addition & 1 deletion cmd/access/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func newLogger() *zap.Logger {
cfg := zap.NewProductionConfig()
cfg := zap.NewDevelopmentConfig()
cfg.DisableCaller = true
cfg.DisableStacktrace = true
cfg.Encoding = "console"
Expand Down

0 comments on commit fe812b1

Please sign in to comment.