Skip to content

Commit b6df885

Browse files
authored
refactor(cli): Consolidates API Token and Robot Account into Token on attestations (#777)
Signed-off-by: Javier Rodriguez <[email protected]>
1 parent bdf3810 commit b6df885

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

app/cli/cmd/attestation.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ import (
2424
)
2525

2626
var (
27-
robotAccount string
27+
attAPIToken string
2828
useAttestationRemoteState bool
2929
GracefulExit bool
3030
// attestationID is the unique identifier of the in-progress attestation
3131
// this is required when use-attestation-remote-state is enabled
3232
attestationID string
3333
)
3434

35+
// Legacy env variable
3536
const robotAccountEnvVarName = "CHAINLOOP_ROBOT_ACCOUNT"
3637

3738
func newAttestationCmd() *cobra.Command {
@@ -54,15 +55,24 @@ func newAttestationCmd() *cobra.Command {
5455
return cmd.MarkFlagRequired("attestation-id")
5556
}
5657

58+
if os.Getenv(tokenEnvVarName) != "" && os.Getenv(robotAccountEnvVarName) != "" {
59+
return fmt.Errorf("both %s and %s env variables cannot be set at the same time", tokenEnvVarName, robotAccountEnvVarName)
60+
}
61+
5762
return nil
5863
},
5964
}
6065

61-
cmd.PersistentFlags().StringVarP(&robotAccount, "token", "t", "", fmt.Sprintf("robot account token. NOTE: You can also use the env variable %s", robotAccountEnvVarName))
66+
cmd.PersistentFlags().StringVarP(&attAPIToken, "token", "t", "", fmt.Sprintf("auth token. NOTE: You can also use the env variable %s", tokenEnvVarName))
6267
// We do not use viper in this case because we do not want this token to be saved in the config file
6368
// Instead we load the env variable manually
64-
if robotAccount == "" {
65-
robotAccount = os.Getenv(robotAccountEnvVarName)
69+
if attAPIToken == "" {
70+
// Check first the new env variable
71+
attAPIToken = os.Getenv(tokenEnvVarName)
72+
// If it stills not set, use the legacy one for some time
73+
if attAPIToken == "" {
74+
attAPIToken = os.Getenv(robotAccountEnvVarName)
75+
}
6676
}
6777

6878
cmd.PersistentFlags().BoolVar(&GracefulExit, "graceful-exit", false, "exit 0 in case of error. NOTE: this flag will be removed once Chainloop reaches 1.0")

app/cli/cmd/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ func newGracefulError(err error) error {
3636

3737
var ErrAttestationNotInitialized = errors.New("attestation not yet initialized, execute the init command first")
3838
var ErrAttestationAlreadyExist = errors.New("attestation already initialized, to override it use the --replace flag`")
39-
var ErrRobotAccountRequired = errors.New("robot account token required, please provide it via the pre-defined env variable or command flag")
39+
var ErrAttestationTokenRequired = errors.New("token required, please provide it via the pre-defined env variable or command flag")

app/cli/cmd/root.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const (
4646
useWorkflowRobotAccount = "withWorkflowRobotAccount"
4747
appName = "chainloop"
4848
//nolint:gosec
49-
apiTokenEnvVarName = "CHAINLOOP_API_TOKEN"
49+
tokenEnvVarName = "CHAINLOOP_TOKEN"
5050
)
5151

5252
func NewRootCmd(l zerolog.Logger) *cobra.Command {
@@ -104,11 +104,11 @@ func NewRootCmd(l zerolog.Logger) *cobra.Command {
104104
rootCmd.PersistentFlags().StringVarP(&flagOutputFormat, "output", "o", "table", "Output format, valid options are json and table")
105105

106106
// Override the oauth authentication requirement for the CLI by providing an API token
107-
rootCmd.PersistentFlags().StringVarP(&apiToken, "token", "t", "", fmt.Sprintf("API token. NOTE: Alternatively use the env variable %s", apiTokenEnvVarName))
107+
rootCmd.PersistentFlags().StringVarP(&apiToken, "token", "t", "", fmt.Sprintf("API token. NOTE: Alternatively use the env variable %s", tokenEnvVarName))
108108
// We do not use viper in this case because we do not want this token to be saved in the config file
109109
// Instead we load the env variable manually
110110
if apiToken == "" {
111-
apiToken = os.Getenv(apiTokenEnvVarName)
111+
apiToken = os.Getenv(tokenEnvVarName)
112112
}
113113

114114
rootCmd.AddCommand(newWorkflowCmd(), newAuthCmd(), NewVersionCmd(),
@@ -195,13 +195,11 @@ func loadControlplaneAuthToken(cmd *cobra.Command) (string, error) {
195195
// If the CMD uses a robot account instead of the regular auth token we override it
196196
// TODO: the attestation CLI should get split from this one
197197
if _, ok := cmd.Annotations[useWorkflowRobotAccount]; ok {
198-
if robotAccount != "" {
199-
logger.Debug().Msg("loaded token from robot account")
200-
} else {
201-
return "", newGracefulError(ErrRobotAccountRequired)
198+
if attAPIToken == "" {
199+
return "", newGracefulError(ErrAttestationTokenRequired)
202200
}
203201

204-
return robotAccount, nil
202+
return attAPIToken, nil
205203
}
206204

207205
// override if token is passed as a flag/env variable

docs/docs/getting-started/attestation-crafting.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,19 @@ To create an attestation two things are required, the Chainloop crafting tool an
6464

6565
The crafting tool is currently bundled within Chainloop command line tool. To install it just follow the [installation](installation) instructions.
6666

67-
The robot account was created during the [previous step](./workflow-definition#robot-account-creation) and it's required during all the stages of the crafting process. It can be provided via the `--token` flag or the `$CHAINLOOP_ROBOT_ACCOUNT` environment variable.
67+
The robot account was created during the [previous step](./workflow-definition#robot-account-creation) and it's required during all the stages of the crafting process. It can be provided via the `--token` flag or the `$CHAINLOOP_TOKEN` environment variable.
6868

6969
### Initialization
7070

7171
```bash
72-
$ export CHAINLOOP_ROBOT_ACCOUNT=deadbeef
72+
$ export CHAINLOOP_TOKEN=deadbeef
7373
```
7474

7575
#### Options
7676

7777
`chainloop attestation init` supports the following options
7878

79-
- `--token` robot account provided by the SecOps team. Alternatively, you can set the `CHAINLOOP_ROBOT_ACCOUNT` environment variable.
79+
- `--token` auth token provided by the SecOps team. Alternatively, you can set the `CHAINLOOP_TOKEN` environment variable.
8080
- `--revision` of the contract (default: `latest`)
8181
- `--dry-run`; do not store the attestation in the Control plane, and do not fail if the runner context or required env variables can not be resolved. Useful for development (default: `false`)
8282

docs/docs/reference/operator/api-tokens.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Available Commands:
3939
revoke revoke API token
4040
```
4141

42-
and then they can be used by the CLI by either setting `CHAINLOOP_API_TOKEN` environment variable or by using the `--token` flag, for example
42+
and then they can be used by the CLI by either setting `CHAINLOOP_TOKEN` environment variable or by using the `--token` flag, for example
4343

4444
```
4545
chainloop workflow list --token <your-token>

0 commit comments

Comments
 (0)