-
Notifications
You must be signed in to change notification settings - Fork 30
Refactor to support separate auth for the SDK (and thus terraform) + small bugfixes #1130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
franklouwers
wants to merge
8
commits into
stackitcloud:main
Choose a base branch
from
franklouwers:terraform-provider-login-squashed
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1703828
Add storage context system for credential isolation
franklouwers b2d6321
Refactor auth flows to support storage contexts
franklouwers 0a36ff6
Add API authentication commands for SDK/Terraform integration
franklouwers 1849c74
go fmt
franklouwers 55d3b42
Fix test failures by using correct StorageContext constant
franklouwers 6710db7
Fix command paths in auth api examples
franklouwers f011f08
"make generate-docs" -> docs
franklouwers 928f2be
Fix linter warnings in auth storage code
franklouwers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| ## stackit auth api | ||
|
|
||
| Manages authentication for the STACKIT Terraform Provider and SDK | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Manages authentication for the STACKIT Terraform Provider and SDK. | ||
|
|
||
| These commands allow you to authenticate with your personal STACKIT account | ||
| and share the credentials with the STACKIT Terraform Provider and SDK. | ||
| This provides an alternative to using service accounts for local development. | ||
|
|
||
| ``` | ||
| stackit auth api [flags] | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help Help for "stackit auth api" | ||
| ``` | ||
|
|
||
| ### Options inherited from parent commands | ||
|
|
||
| ``` | ||
| -y, --assume-yes If set, skips all confirmation prompts | ||
| --async If set, runs the command asynchronously | ||
| -o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"] | ||
| -p, --project-id string Project ID | ||
| --region string Target region for region-specific requests | ||
| --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [stackit auth](./stackit_auth.md) - Authenticates the STACKIT CLI | ||
| * [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 | ||
| * [stackit auth api login](./stackit_auth_api_login.md) - Logs in for the STACKIT Terraform Provider and SDK | ||
| * [stackit auth api logout](./stackit_auth_api_logout.md) - Logs out from the STACKIT Terraform Provider and SDK | ||
| * [stackit auth api status](./stackit_auth_api_status.md) - Shows authentication status for the STACKIT Terraform Provider and SDK | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| ## stackit auth api get-access-token | ||
|
|
||
| Prints a short-lived access token for the STACKIT Terraform Provider and SDK | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Prints a short-lived access token for the STACKIT Terraform Provider and SDK which can be used e.g. for API calls. | ||
|
|
||
| ``` | ||
| stackit auth api get-access-token [flags] | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| ``` | ||
| Print a short-lived access token for the STACKIT Terraform Provider and SDK | ||
| $ stackit auth api get-access-token | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help Help for "stackit auth api get-access-token" | ||
| ``` | ||
|
|
||
| ### Options inherited from parent commands | ||
|
|
||
| ``` | ||
| -y, --assume-yes If set, skips all confirmation prompts | ||
| --async If set, runs the command asynchronously | ||
| -o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"] | ||
| -p, --project-id string Project ID | ||
| --region string Target region for region-specific requests | ||
| --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [stackit auth api](./stackit_auth_api.md) - Manages authentication for the STACKIT Terraform Provider and SDK | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| ## stackit auth api login | ||
|
|
||
| Logs in for the STACKIT Terraform Provider and SDK | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Logs in for the STACKIT Terraform Provider and SDK using a user account. | ||
| 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. | ||
| The credentials are stored separately from the CLI authentication and will be used by the STACKIT Terraform Provider and SDK. | ||
|
|
||
| ``` | ||
| stackit auth api login [flags] | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| ``` | ||
| Login for the STACKIT Terraform Provider and SDK. This command will open a browser window where you can login to your STACKIT account | ||
| $ stackit auth api login | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help Help for "stackit auth api login" | ||
| ``` | ||
|
|
||
| ### Options inherited from parent commands | ||
|
|
||
| ``` | ||
| -y, --assume-yes If set, skips all confirmation prompts | ||
| --async If set, runs the command asynchronously | ||
| -o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"] | ||
| -p, --project-id string Project ID | ||
| --region string Target region for region-specific requests | ||
| --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [stackit auth api](./stackit_auth_api.md) - Manages authentication for the STACKIT Terraform Provider and SDK | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| ## stackit auth api logout | ||
|
|
||
| Logs out from the STACKIT Terraform Provider and SDK | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Logs out from the STACKIT Terraform Provider and SDK. This does not affect CLI authentication. | ||
|
|
||
| ``` | ||
| stackit auth api logout [flags] | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| ``` | ||
| Log out from the STACKIT Terraform Provider and SDK | ||
| $ stackit auth api logout | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help Help for "stackit auth api logout" | ||
| ``` | ||
|
|
||
| ### Options inherited from parent commands | ||
|
|
||
| ``` | ||
| -y, --assume-yes If set, skips all confirmation prompts | ||
| --async If set, runs the command asynchronously | ||
| -o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"] | ||
| -p, --project-id string Project ID | ||
| --region string Target region for region-specific requests | ||
| --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [stackit auth api](./stackit_auth_api.md) - Manages authentication for the STACKIT Terraform Provider and SDK | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| ## stackit auth api status | ||
|
|
||
| Shows authentication status for the STACKIT Terraform Provider and SDK | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Shows authentication status for the STACKIT Terraform Provider and SDK, including whether you are authenticated and with which account. | ||
|
|
||
| ``` | ||
| stackit auth api status [flags] | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| ``` | ||
| Show authentication status for the STACKIT Terraform Provider and SDK | ||
| $ stackit auth api status | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help Help for "stackit auth api status" | ||
| ``` | ||
|
|
||
| ### Options inherited from parent commands | ||
|
|
||
| ``` | ||
| -y, --assume-yes If set, skips all confirmation prompts | ||
| --async If set, runs the command asynchronously | ||
| -o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"] | ||
| -p, --project-id string Project ID | ||
| --region string Target region for region-specific requests | ||
| --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [stackit auth api](./stackit_auth_api.md) - Manages authentication for the STACKIT Terraform Provider and SDK | ||
|
|
89 changes: 89 additions & 0 deletions
89
internal/cmd/auth/api/get-access-token/get_access_token.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package getaccesstoken | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/stackitcloud/stackit-cli/internal/cmd/params" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/args" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/auth" | ||
| cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/examples" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/print" | ||
| ) | ||
|
|
||
| type inputModel struct { | ||
| *globalflags.GlobalFlagModel | ||
| } | ||
|
|
||
| func NewCmd(params *params.CmdParams) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "get-access-token", | ||
| Short: "Prints a short-lived access token for the STACKIT Terraform Provider and SDK", | ||
| Long: "Prints a short-lived access token for the STACKIT Terraform Provider and SDK which can be used e.g. for API calls.", | ||
| Args: args.NoArgs, | ||
| Example: examples.Build( | ||
| examples.NewExample( | ||
| `Print a short-lived access token for the STACKIT Terraform Provider and SDK`, | ||
| "$ stackit auth api get-access-token"), | ||
| ), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| model, err := parseInput(params.Printer, cmd, args) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| userSessionExpired, err := auth.UserSessionExpiredWithContext(auth.StorageContextAPI) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if userSessionExpired { | ||
| return &cliErr.SessionExpiredError{} | ||
| } | ||
|
|
||
| accessToken, err := auth.GetValidAccessTokenWithContext(params.Printer, auth.StorageContextAPI) | ||
| if err != nil { | ||
| params.Printer.Debug(print.ErrorLevel, "get valid access token: %v", err) | ||
| return &cliErr.SessionExpiredError{} | ||
| } | ||
|
|
||
| switch model.OutputFormat { | ||
| case print.JSONOutputFormat: | ||
| details, err := json.MarshalIndent(map[string]string{ | ||
| "access_token": accessToken, | ||
| }, "", " ") | ||
| if err != nil { | ||
| return fmt.Errorf("marshal access token: %w", err) | ||
| } | ||
| params.Printer.Outputln(string(details)) | ||
|
|
||
| return nil | ||
| default: | ||
| params.Printer.Outputln(accessToken) | ||
|
|
||
| return nil | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| // hide project id flag from help command because it could mislead users | ||
| cmd.SetHelpFunc(func(command *cobra.Command, strings []string) { | ||
| _ = command.Flags().MarkHidden(globalflags.ProjectIdFlag) // nolint:errcheck // there's no chance to handle the error here | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just a note, since this is copied verbatim from |
||
| command.Parent().HelpFunc()(command, strings) | ||
| }) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) { | ||
| globalFlags := globalflags.Parse(p, cmd) | ||
|
|
||
| model := inputModel{ | ||
| GlobalFlagModel: globalFlags, | ||
| } | ||
|
|
||
| p.DebugInputModel(model) | ||
| return &model, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package login | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/stackitcloud/stackit-cli/internal/cmd/params" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/args" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/auth" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/examples" | ||
| ) | ||
|
|
||
| func NewCmd(params *params.CmdParams) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "login", | ||
| Short: "Logs in for the STACKIT Terraform Provider and SDK", | ||
| Long: fmt.Sprintf("%s\n%s\n%s", | ||
| "Logs in for the STACKIT Terraform Provider and SDK using a user account.", | ||
| "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.", | ||
| "The credentials are stored separately from the CLI authentication and will be used by the STACKIT Terraform Provider and SDK."), | ||
| Args: args.NoArgs, | ||
| Example: examples.Build( | ||
| examples.NewExample( | ||
| `Login for the STACKIT Terraform Provider and SDK. This command will open a browser window where you can login to your STACKIT account`, | ||
| "$ stackit auth api login"), | ||
| ), | ||
| RunE: func(_ *cobra.Command, _ []string) error { | ||
| err := auth.AuthorizeUser(params.Printer, auth.StorageContextAPI, false) | ||
| if err != nil { | ||
| return fmt.Errorf("authorization failed: %w", err) | ||
| } | ||
|
|
||
| params.Printer.Outputln("Successfully logged in for STACKIT Terraform Provider and SDK.\n") | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
| return cmd | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package logout | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/stackitcloud/stackit-cli/internal/cmd/params" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/args" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/auth" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/examples" | ||
| ) | ||
|
|
||
| func NewCmd(params *params.CmdParams) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "logout", | ||
| Short: "Logs out from the STACKIT Terraform Provider and SDK", | ||
| Long: "Logs out from the STACKIT Terraform Provider and SDK. This does not affect CLI authentication.", | ||
| Args: args.NoArgs, | ||
| Example: examples.Build( | ||
| examples.NewExample( | ||
| `Log out from the STACKIT Terraform Provider and SDK`, | ||
| "$ stackit auth api logout"), | ||
| ), | ||
| RunE: func(_ *cobra.Command, _ []string) error { | ||
| err := auth.LogoutUserWithContext(auth.StorageContextAPI) | ||
| if err != nil { | ||
| return fmt.Errorf("log out failed: %w", err) | ||
| } | ||
|
|
||
| params.Printer.Info("Successfully logged out from STACKIT Terraform Provider and SDK.\n") | ||
| return nil | ||
| }, | ||
| } | ||
| return cmd | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would also handle
print.YAMLOutputFormat