Skip to content

Commit 73de092

Browse files
committed
Add API authentication commands for SDK/Terraform integration
Add new `stackit auth api` commands that provide separate credential storage for the STACKIT Terraform Provider and SDK. This allows using different accounts for CLI usage vs SDK/Provider usage. New commands: - stackit auth api login - stackit auth api logout - stackit auth api get-access-token - stackit auth api status The implementation adds a storage context system to isolate credentials between CLI and API contexts, with tokens stored in the OS keychain (with fallback to local storage).
1 parent c7dada1 commit 73de092

23 files changed

+1618
-73
lines changed

docs/stackit_auth.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ stackit auth [flags]
3131

3232
* [stackit](./stackit.md) - Manage STACKIT resources using the command line
3333
* [stackit auth activate-service-account](./stackit_auth_activate-service-account.md) - Authenticates using a service account
34+
* [stackit auth api](./stackit_auth_api.md) - Manages authentication for the STACKIT Terraform Provider and SDK
3435
* [stackit auth get-access-token](./stackit_auth_get-access-token.md) - Prints a short-lived access token.
3536
* [stackit auth login](./stackit_auth_login.md) - Logs in to the STACKIT CLI
3637
* [stackit auth logout](./stackit_auth_logout.md) - Logs the user account out of the STACKIT CLI

docs/stackit_auth_api.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## stackit auth api
2+
3+
Manages authentication for the STACKIT Terraform Provider and SDK
4+
5+
### Synopsis
6+
7+
Manages authentication for the STACKIT Terraform Provider and SDK.
8+
9+
These commands allow you to authenticate with your personal STACKIT account
10+
and share the credentials with the STACKIT Terraform Provider and SDK.
11+
This provides an alternative to using service accounts for local development.
12+
13+
```
14+
stackit auth api [flags]
15+
```
16+
17+
### Options
18+
19+
```
20+
-h, --help Help for "stackit auth api"
21+
```
22+
23+
### Options inherited from parent commands
24+
25+
```
26+
-y, --assume-yes If set, skips all confirmation prompts
27+
--async If set, runs the command asynchronously
28+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
29+
-p, --project-id string Project ID
30+
--region string Target region for region-specific requests
31+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
32+
```
33+
34+
### SEE ALSO
35+
36+
* [stackit auth](./stackit_auth.md) - Authenticates the STACKIT CLI
37+
* [stackit auth api get-access-token](./stackit_auth_api_get-access-token.md) - Prints a short-lived access token for the STACKIT Terraform Provider and SDK
38+
* [stackit auth api login](./stackit_auth_api_login.md) - Logs in for the STACKIT Terraform Provider and SDK
39+
* [stackit auth api logout](./stackit_auth_api_logout.md) - Logs out from the STACKIT Terraform Provider and SDK
40+
* [stackit auth api status](./stackit_auth_api_status.md) - Shows authentication status for the STACKIT Terraform Provider and SDK
41+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## stackit auth api get-access-token
2+
3+
Prints a short-lived access token for the STACKIT Terraform Provider and SDK
4+
5+
### Synopsis
6+
7+
Prints a short-lived access token for the STACKIT Terraform Provider and SDK which can be used e.g. for API calls.
8+
9+
```
10+
stackit auth api get-access-token [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Print a short-lived access token for the STACKIT Terraform Provider and SDK
17+
$ stackit auth api get-access-token
18+
```
19+
20+
### Options
21+
22+
```
23+
-h, --help Help for "stackit auth api get-access-token"
24+
```
25+
26+
### Options inherited from parent commands
27+
28+
```
29+
-y, --assume-yes If set, skips all confirmation prompts
30+
--async If set, runs the command asynchronously
31+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
32+
-p, --project-id string Project ID
33+
--region string Target region for region-specific requests
34+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
35+
```
36+
37+
### SEE ALSO
38+
39+
* [stackit auth api](./stackit_auth_api.md) - Manages authentication for the STACKIT Terraform Provider and SDK
40+

docs/stackit_auth_api_login.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## stackit auth api login
2+
3+
Logs in for the STACKIT Terraform Provider and SDK
4+
5+
### Synopsis
6+
7+
Logs in for the STACKIT Terraform Provider and SDK using a user account.
8+
The authentication is done via a web-based authorization flow, where the command will open a browser window in which you can login to your STACKIT account.
9+
The credentials are stored separately from the CLI authentication and will be used by the STACKIT Terraform Provider and SDK.
10+
11+
```
12+
stackit auth api login [flags]
13+
```
14+
15+
### Examples
16+
17+
```
18+
Login for the STACKIT Terraform Provider and SDK. This command will open a browser window where you can login to your STACKIT account
19+
$ stackit auth api login
20+
```
21+
22+
### Options
23+
24+
```
25+
-h, --help Help for "stackit auth api login"
26+
```
27+
28+
### Options inherited from parent commands
29+
30+
```
31+
-y, --assume-yes If set, skips all confirmation prompts
32+
--async If set, runs the command asynchronously
33+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
34+
-p, --project-id string Project ID
35+
--region string Target region for region-specific requests
36+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
37+
```
38+
39+
### SEE ALSO
40+
41+
* [stackit auth api](./stackit_auth_api.md) - Manages authentication for the STACKIT Terraform Provider and SDK
42+

docs/stackit_auth_api_logout.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## stackit auth api logout
2+
3+
Logs out from the STACKIT Terraform Provider and SDK
4+
5+
### Synopsis
6+
7+
Logs out from the STACKIT Terraform Provider and SDK. This does not affect CLI authentication.
8+
9+
```
10+
stackit auth api logout [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Log out from the STACKIT Terraform Provider and SDK
17+
$ stackit auth api logout
18+
```
19+
20+
### Options
21+
22+
```
23+
-h, --help Help for "stackit auth api logout"
24+
```
25+
26+
### Options inherited from parent commands
27+
28+
```
29+
-y, --assume-yes If set, skips all confirmation prompts
30+
--async If set, runs the command asynchronously
31+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
32+
-p, --project-id string Project ID
33+
--region string Target region for region-specific requests
34+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
35+
```
36+
37+
### SEE ALSO
38+
39+
* [stackit auth api](./stackit_auth_api.md) - Manages authentication for the STACKIT Terraform Provider and SDK
40+

docs/stackit_auth_api_status.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## stackit auth api status
2+
3+
Shows authentication status for the STACKIT Terraform Provider and SDK
4+
5+
### Synopsis
6+
7+
Shows authentication status for the STACKIT Terraform Provider and SDK, including whether you are authenticated and with which account.
8+
9+
```
10+
stackit auth api status [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Show authentication status for the STACKIT Terraform Provider and SDK
17+
$ stackit auth api status
18+
```
19+
20+
### Options
21+
22+
```
23+
-h, --help Help for "stackit auth api status"
24+
```
25+
26+
### Options inherited from parent commands
27+
28+
```
29+
-y, --assume-yes If set, skips all confirmation prompts
30+
--async If set, runs the command asynchronously
31+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
32+
-p, --project-id string Project ID
33+
--region string Target region for region-specific requests
34+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
35+
```
36+
37+
### SEE ALSO
38+
39+
* [stackit auth api](./stackit_auth_api.md) - Manages authentication for the STACKIT Terraform Provider and SDK
40+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package getaccesstoken
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
6+
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
7+
cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
8+
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
9+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
12+
)
13+
14+
type inputModel struct {
15+
*globalflags.GlobalFlagModel
16+
}
17+
18+
func NewCmd(p *types.CmdParams) *cobra.Command {
19+
cmd := &cobra.Command{
20+
Use: "get-access-token",
21+
Short: "Prints a short-lived access token for the STACKIT Terraform Provider and SDK",
22+
Long: "Prints a short-lived access token for the STACKIT Terraform Provider and SDK which can be used e.g. for API calls.",
23+
Args: args.NoArgs,
24+
Example: examples.Build(
25+
examples.NewExample(
26+
`Print a short-lived access token for the STACKIT Terraform Provider and SDK`,
27+
"$ stackit auth api get-access-token"),
28+
),
29+
RunE: func(cmd *cobra.Command, args []string) error {
30+
model, err := parseInput(p.Printer, cmd, args)
31+
if err != nil {
32+
return err
33+
}
34+
35+
userSessionExpired, err := auth.UserSessionExpiredWithContext(auth.StorageContextAPI)
36+
if err != nil {
37+
return err
38+
}
39+
if userSessionExpired {
40+
return &cliErr.SessionExpiredError{}
41+
}
42+
43+
accessToken, err := auth.GetValidAccessTokenWithContext(p.Printer, auth.StorageContextAPI)
44+
if err != nil {
45+
p.Printer.Debug(print.ErrorLevel, "get valid access token: %v", err)
46+
return &cliErr.SessionExpiredError{}
47+
}
48+
49+
result := map[string]string{
50+
"access_token": accessToken,
51+
}
52+
return p.Printer.OutputResult(model.OutputFormat, result, func() error {
53+
p.Printer.Outputln(accessToken)
54+
return nil
55+
})
56+
},
57+
}
58+
59+
// hide project id flag from help command because it could mislead users
60+
cmd.SetHelpFunc(func(command *cobra.Command, strings []string) {
61+
cobra.CheckErr(command.Flags().MarkHidden(globalflags.ProjectIdFlag))
62+
command.Parent().HelpFunc()(command, strings)
63+
})
64+
65+
return cmd
66+
}
67+
68+
func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
69+
globalFlags := globalflags.Parse(p, cmd)
70+
71+
model := inputModel{
72+
GlobalFlagModel: globalFlags,
73+
}
74+
75+
p.DebugInputModel(model)
76+
return &model, nil
77+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package login
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
8+
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
9+
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
11+
)
12+
13+
func NewCmd(p *types.CmdParams) *cobra.Command {
14+
cmd := &cobra.Command{
15+
Use: "login",
16+
Short: "Logs in for the STACKIT Terraform Provider and SDK",
17+
Long: fmt.Sprintf("%s\n%s\n%s",
18+
"Logs in for the STACKIT Terraform Provider and SDK using a user account.",
19+
"The authentication is done via a web-based authorization flow, where the command will open a browser window in which you can login to your STACKIT account.",
20+
"The credentials are stored separately from the CLI authentication and will be used by the STACKIT Terraform Provider and SDK."),
21+
Args: args.NoArgs,
22+
Example: examples.Build(
23+
examples.NewExample(
24+
`Login for the STACKIT Terraform Provider and SDK. This command will open a browser window where you can login to your STACKIT account`,
25+
"$ stackit auth api login"),
26+
),
27+
RunE: func(_ *cobra.Command, _ []string) error {
28+
err := auth.AuthorizeUser(p.Printer, auth.StorageContextAPI, false)
29+
if err != nil {
30+
return fmt.Errorf("authorization failed: %w", err)
31+
}
32+
33+
p.Printer.Outputln("Successfully logged in for STACKIT Terraform Provider and SDK.\n")
34+
35+
return nil
36+
},
37+
}
38+
return cmd
39+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package logout
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
8+
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
9+
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
11+
)
12+
13+
func NewCmd(p *types.CmdParams) *cobra.Command {
14+
cmd := &cobra.Command{
15+
Use: "logout",
16+
Short: "Logs out from the STACKIT Terraform Provider and SDK",
17+
Long: "Logs out from the STACKIT Terraform Provider and SDK. This does not affect CLI authentication.",
18+
Args: args.NoArgs,
19+
Example: examples.Build(
20+
examples.NewExample(
21+
`Log out from the STACKIT Terraform Provider and SDK`,
22+
"$ stackit auth api logout"),
23+
),
24+
RunE: func(_ *cobra.Command, _ []string) error {
25+
err := auth.LogoutUserWithContext(auth.StorageContextAPI)
26+
if err != nil {
27+
return fmt.Errorf("log out failed: %w", err)
28+
}
29+
30+
p.Printer.Info("Successfully logged out from STACKIT Terraform Provider and SDK.\n")
31+
return nil
32+
},
33+
}
34+
return cmd
35+
}

0 commit comments

Comments
 (0)