Skip to content

Commit

Permalink
Add support for generating service tokens with write access
Browse files Browse the repository at this point in the history
  • Loading branch information
Piccirello committed Oct 25, 2021
1 parent b0ec5d2 commit 6d18c5e
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 6 deletions.
4 changes: 3 additions & 1 deletion pkg/cmd/configs_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func createConfigsTokens(cmd *cobra.Command, args []string) {
jsonFlag := utils.OutputJSON
plain := utils.GetBoolFlag(cmd, "plain")
copy := utils.GetBoolFlag(cmd, "copy")
access := cmd.Flag("access").Value.String()
localConfig := configuration.LocalConfig(cmd)

utils.RequireValue("token", localConfig.Token.Value)
Expand All @@ -122,7 +123,7 @@ func createConfigsTokens(cmd *cobra.Command, args []string) {
expireAt = time.Now().Add(maxAge)
}

configToken, err := http.CreateConfigServiceToken(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value, localConfig.EnclaveProject.Value, localConfig.EnclaveConfig.Value, name, expireAt)
configToken, err := http.CreateConfigServiceToken(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value, localConfig.EnclaveProject.Value, localConfig.EnclaveConfig.Value, name, expireAt, access)
if !err.IsNil() {
utils.HandleError(err.Unwrap(), err.Message)
}
Expand Down Expand Up @@ -184,6 +185,7 @@ func init() {
configsTokensCreateCmd.Flags().Bool("plain", false, "print only the token, without formatting")
configsTokensCreateCmd.Flags().Bool("copy", false, "copy the token to your clipboard")
configsTokensCreateCmd.Flags().Duration("max-age", 0, "token will expire after specified duration, (e.g. '3h', '15m')")
configsTokensCreateCmd.Flags().String("access", "read", "the token's access. one of [\"read\", \"read/write\"]")
configsTokensCmd.AddCommand(configsTokensCreateCmd)

configsTokensRevokeCmd.Flags().String("slug", "", "service token slug")
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/enclave_configs_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func init() {
enclaveConfigsTokensCreateCmd.Flags().StringP("config", "c", "", "enclave config (e.g. dev)")
enclaveConfigsTokensCreateCmd.Flags().Bool("plain", false, "print only the token, without formatting")
enclaveConfigsTokensCreateCmd.Flags().Bool("copy", false, "copy the token to your clipboard")
enclaveConfigsTokensCreateCmd.Flags().String("access", "read", "the token's access. one of [\"read\", \"read/write\"]")
enclaveConfigsTokensCmd.AddCommand(enclaveConfigsTokensCreateCmd)

enclaveConfigsTokensRevokeCmd.Flags().String("slug", "", "service token slug")
Expand Down
3 changes: 2 additions & 1 deletion pkg/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,11 +932,12 @@ func GetConfigServiceTokens(host string, verifyTLS bool, apiKey string, project
}

// CreateConfigServiceToken create a config service token
func CreateConfigServiceToken(host string, verifyTLS bool, apiKey string, project string, config string, name string, expireAt time.Time) (models.ConfigServiceToken, Error) {
func CreateConfigServiceToken(host string, verifyTLS bool, apiKey string, project string, config string, name string, expireAt time.Time, access string) (models.ConfigServiceToken, Error) {
postBody := map[string]interface{}{"name": name}
if !expireAt.IsZero() {
postBody["expire_at"] = expireAt.Unix()
}
postBody["access"] = access

body, err := json.Marshal(postBody)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/models/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,5 @@ type ConfigServiceToken struct {
Project string `json:"project"`
Environment string `json:"environment"`
Config string `json:"config"`
Access string `json:"access"`
}
3 changes: 3 additions & 0 deletions pkg/models/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ func ParseConfigServiceToken(token map[string]interface{}) ConfigServiceToken {
if token["expires_at"] != nil {
parsedToken.ExpiresAt = token["expires_at"].(string)
}
if token["access"] != nil {
parsedToken.Access = token["access"].(string)
}

return parsedToken
}
8 changes: 4 additions & 4 deletions pkg/printer/enclave.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,9 @@ func ConfigServiceTokensInfo(tokens []models.ConfigServiceToken, number int, jso

rows := [][]string{}
for _, token := range tokens {
rows = append(rows, []string{token.Name, token.Slug, token.Project, token.Environment, token.Config, token.CreatedAt, token.ExpiresAt})
rows = append(rows, []string{token.Name, token.Slug, token.Project, token.Environment, token.Config, token.CreatedAt, token.ExpiresAt, token.Access})
}
Table([]string{"name", "slug", "project", "environment", "config", "created at", "expires at"}, rows, TableOptions())
Table([]string{"name", "slug", "project", "environment", "config", "created at", "expires at", "access"}, rows, TableOptions())
}

// ConfigServiceTokenInfo print config service token info
Expand Down Expand Up @@ -358,6 +358,6 @@ func ConfigServiceToken(token models.ConfigServiceToken, jsonFlag bool, plain bo
return
}

rows := [][]string{{token.Name, token.Token, token.Slug, token.Project, token.Environment, token.Config, token.CreatedAt, token.ExpiresAt}}
Table([]string{"name", "token", "slug", "project", "environment", "config", "created at", "expires at"}, rows, TableOptions())
rows := [][]string{{token.Name, token.Token, token.Slug, token.Project, token.Environment, token.Config, token.CreatedAt, token.ExpiresAt, token.Access}}
Table([]string{"name", "token", "slug", "project", "environment", "config", "created at", "expires at", "access"}, rows, TableOptions())
}

0 comments on commit 6d18c5e

Please sign in to comment.