Skip to content

Commit

Permalink
feat(cli): Add ID option to 'proj role create-token' (#4632) (#4636)
Browse files Browse the repository at this point in the history
* feat: Add ID option to 'proj role create-token'

Also add some more informative output to the same command

Signed-off-by: Tim Etchells <tetchell@redhat.com>

* Parse token on client side, add --token-only flag

Signed-off-by: Tim Etchells <tetchell@redhat.com>
  • Loading branch information
tetchel committed Oct 27, 2020
1 parent 0850bcc commit be60425
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
57 changes: 53 additions & 4 deletions cmd/argocd/commands/project_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"os"
"strconv"
"text/tabwriter"
"time"

"github.com/argoproj/gitops-engine/pkg/utils/errors"
"github.com/argoproj/gitops-engine/pkg/utils/io"
timeutil "github.com/argoproj/pkg/time"
jwtgo "github.com/dgrijalva/jwt-go"
"github.com/spf13/cobra"

argocdclient "github.com/argoproj/argo-cd/pkg/apiclient"
Expand Down Expand Up @@ -195,10 +197,20 @@ func NewProjectRoleDeleteCommand(clientOpts *argocdclient.ClientOptions) *cobra.
return command
}

func tokenTimeToString(t int64) string {
tokenTimeToString := "Never"
if t > 0 {
tokenTimeToString = time.Unix(t, 0).Format(time.RFC3339)
}
return tokenTimeToString
}

// NewProjectRoleCreateTokenCommand returns a new instance of an `argocd proj role create-token` command
func NewProjectRoleCreateTokenCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
var (
expiresIn string
expiresIn string
outputTokenOnly bool
tokenID string
)
var command = &cobra.Command{
Use: "create-token PROJECT ROLE-NAME",
Expand All @@ -212,14 +224,51 @@ func NewProjectRoleCreateTokenCommand(clientOpts *argocdclient.ClientOptions) *c
roleName := args[1]
conn, projIf := argocdclient.NewClientOrDie(clientOpts).NewProjectClientOrDie()
defer io.Close(conn)
if expiresIn == "" {
expiresIn = "0s"
}
duration, err := timeutil.ParseDuration(expiresIn)
errors.CheckError(err)
token, err := projIf.CreateToken(context.Background(), &projectpkg.ProjectTokenCreateRequest{Project: projName, Role: roleName, ExpiresIn: int64(duration.Seconds())})
tokenResponse, err := projIf.CreateToken(context.Background(), &projectpkg.ProjectTokenCreateRequest{
Project: projName,
Role: roleName,
ExpiresIn: int64(duration.Seconds()),
Id: tokenID,
})
errors.CheckError(err)
fmt.Println(token.Token)

token, err := jwtgo.Parse(tokenResponse.Token, nil)
if token == nil {
err = fmt.Errorf("received malformed token %v", err)
errors.CheckError(err)
return
}

claims := token.Claims.(jwtgo.MapClaims)
issuedAt := int64(claims["iat"].(float64))
expiresAt := int64(0)
if expires, ok := claims["exp"]; ok {
expiresAt = int64(expires.(float64))
}
id := claims["jti"].(string)
subject := claims["sub"].(string)

if !outputTokenOnly {
fmt.Printf("Create token succeeded for %s.\n", subject)
fmt.Printf(" ID: %s\n Issued At: %s\n Expires At: %s\n",
id, tokenTimeToString(issuedAt), tokenTimeToString(expiresAt),
)
fmt.Println(" Token: " + tokenResponse.Token)
} else {
fmt.Println(tokenResponse.Token)
}
},
}
command.Flags().StringVarP(&expiresIn, "expires-in", "e", "0s", "Duration before the token will expire. (Default: No expiration)")
command.Flags().StringVarP(&expiresIn, "expires-in", "e", "",
"Duration before the token will expire, eg \"12h\", \"7d\". (Default: No expiration)",
)
command.Flags().StringVarP(&tokenID, "id", "i", "", "Token unique identifier. (Default: Random UUID)")
command.Flags().BoolVarP(&outputTokenOnly, "token-only", "t", false, "Output token only - for use in scripts.")

return command
}
Expand Down
4 changes: 3 additions & 1 deletion docs/user-guide/commands/argocd_proj_role_create-token.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ argocd proj role create-token PROJECT ROLE-NAME [flags]
### Options

```
-e, --expires-in string Duration before the token will expire. (Default: No expiration) (default "0s")
-e, --expires-in string Duration before the token will expire, eg "12h", "7d". (Default: No expiration)
-h, --help help for create-token
-i, --id string Token unique identifier. (Default: Random UUID)
-t, --token-only Output token only - for use in scripts.
```

### Options inherited from parent commands
Expand Down

0 comments on commit be60425

Please sign in to comment.