Repository navigation
Update and delete OIDC trusts #1519
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,68 @@ | ||
| package oidctrust | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/planetscale/cli/internal/cmdutil" | ||
| ps "github.com/planetscale/cli/internal/planetscale" | ||
| "github.com/planetscale/cli/internal/printer" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func DeleteCmd(ch *cmdutil.Helper) *cobra.Command { | ||
| var force bool | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "delete <trust-id>", | ||
| Short: "Delete an OIDC trust", | ||
| Args: cmdutil.RequiredArgs("trust-id"), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| id := args[0] | ||
|
|
||
| client, err := ch.Client() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if !force { | ||
| if err := ch.Printer.ConfirmCommand(id, "delete OIDC trust", "OIDC trust deletion"); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| end := ch.Printer.PrintProgress(fmt.Sprintf("Deleting OIDC trust %s", printer.BoldBlue(id))) | ||
| defer end() | ||
|
|
||
| err = client.OIDCTrusts.Delete(cmd.Context(), &ps.DeleteOIDCTrustRequest{ | ||
| Organization: ch.Config.Organization, | ||
| ID: id, | ||
| }) | ||
| if err != nil { | ||
| return notFoundOrHandle(ch, id, err) | ||
| } | ||
|
|
||
| end() | ||
|
|
||
| if ch.Printer.Format() == printer.Human { | ||
| ch.Printer.Printf("OIDC trust %s was successfully deleted.\n", printer.BoldBlue(id)) | ||
| return nil | ||
| } | ||
|
|
||
| return ch.Printer.PrintResource(map[string]string{ | ||
| "result": "oidc trust deleted", | ||
| }) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().BoolVar(&force, "force", false, "Delete the OIDC trust without confirmation") | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func notFoundOrHandle(ch *cmdutil.Helper, id string, err error) error { | ||
| if cmdutil.ErrCode(err) == ps.ErrNotFound { | ||
| return fmt.Errorf("OIDC trust %s does not exist in organization %s", | ||
| printer.BoldBlue(id), printer.BoldBlue(ch.Config.Organization)) | ||
| } | ||
| return cmdutil.HandleError(err) | ||
| } | ||
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 oidctrust | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| qt "github.com/frankban/quicktest" | ||
| "github.com/planetscale/cli/internal/mock" | ||
| ps "github.com/planetscale/cli/internal/planetscale" | ||
| ) | ||
|
|
||
| func TestOIDCTrust_DeleteCmd_RequiresForceInJSON(t *testing.T) { | ||
| c := qt.New(t) | ||
|
|
||
| svc := &mock.OIDCTrustsService{} | ||
| cmd := DeleteCmd(newUpdateHelper(svc)) | ||
| cmd.SetArgs([]string{"trust-1"}) | ||
| c.Assert(cmd.Execute(), qt.Not(qt.IsNil)) | ||
| c.Assert(svc.DeleteFnInvoked, qt.IsFalse) | ||
| } | ||
|
|
||
| func TestOIDCTrust_DeleteCmd_Force(t *testing.T) { | ||
| c := qt.New(t) | ||
|
|
||
| svc := &mock.OIDCTrustsService{ | ||
| DeleteFn: func(ctx context.Context, req *ps.DeleteOIDCTrustRequest) error { | ||
| c.Assert(req.ID, qt.Equals, "trust-1") | ||
| return nil | ||
| }, | ||
| } | ||
| cmd := DeleteCmd(newUpdateHelper(svc)) | ||
| cmd.SetArgs([]string{"trust-1", "--force"}) | ||
| c.Assert(cmd.Execute(), qt.IsNil) | ||
| c.Assert(svc.DeleteFnInvoked, qt.IsTrue) | ||
| } |
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,94 @@ | ||
| package oidctrust | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/planetscale/cli/internal/cmdutil" | ||
| ps "github.com/planetscale/cli/internal/planetscale" | ||
| "github.com/planetscale/cli/internal/printer" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func UpdateCmd(ch *cmdutil.Helper) *cobra.Command { | ||
| var flags struct { | ||
| name string | ||
| maxTTL int | ||
| conditions []string | ||
| orgAccesses []string | ||
| databaseAccesses []string | ||
| allDatabasesAccesses []string | ||
| } | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "update <trust-id>", | ||
| Short: "Update an OIDC trust", | ||
| Args: cmdutil.RequiredArgs("trust-id"), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| id := args[0] | ||
| f := cmd.Flags() | ||
|
|
||
| req := &ps.UpdateOIDCTrustRequest{Organization: ch.Config.Organization, ID: id} | ||
| changed := false | ||
|
|
||
| if f.Changed("name") { | ||
| req.DisplayName = &flags.name | ||
| changed = true | ||
| } | ||
| if f.Changed("max-ttl") { | ||
| req.MaxTTLSeconds = &flags.maxTTL | ||
| changed = true | ||
| } | ||
| if f.Changed("condition") { | ||
| conditions, err := parseConditions(nonEmpty(flags.conditions)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| req.Conditions = &conditions | ||
| changed = true | ||
| } | ||
| if f.Changed("org-access") || f.Changed("database-access") { | ||
| accesses, err := parseAccesses(nonEmpty(flags.orgAccesses), nonEmpty(flags.databaseAccesses)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| req.Accesses = &accesses | ||
| changed = true | ||
| } | ||
| if f.Changed("all-databases-access") { | ||
| bulk := nonEmpty(flags.allDatabasesAccesses) | ||
| req.BulkDatabaseAccesses = &bulk | ||
| changed = true | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| if !changed { | ||
| return fmt.Errorf("at least one of --name, --max-ttl, --condition, --org-access, --database-access, or --all-databases-access must be provided") | ||
| } | ||
|
|
||
| client, err := ch.Client() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| end := ch.Printer.PrintProgress(fmt.Sprintf("Updating OIDC trust %s", printer.BoldBlue(id))) | ||
| defer end() | ||
|
|
||
| trust, err := client.OIDCTrusts.Update(cmd.Context(), req) | ||
| if err != nil { | ||
| return notFoundOrHandle(ch, id, err) | ||
| } | ||
|
|
||
| end() | ||
|
|
||
| return ch.Printer.PrintResource(toOIDCTrust(trust)) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar(&flags.name, "name", "", "The display name of the trust") | ||
| cmd.Flags().IntVar(&flags.maxTTL, "max-ttl", 0, "The maximum token lifetime in seconds") | ||
| cmd.Flags().StringArrayVar(&flags.conditions, "condition", nil, "A claim the token must match, as claim=value; repeatable; replaces all conditions") | ||
| cmd.Flags().StringArrayVar(&flags.orgAccesses, "org-access", nil, "An organization access to grant; repeatable; replaces all org and database accesses") | ||
| cmd.Flags().StringArrayVar(&flags.databaseAccesses, "database-access", nil, "A database access to grant, as database:access; repeatable; replaces all org and database accesses") | ||
| cmd.Flags().StringArrayVar(&flags.allDatabasesAccesses, "all-databases-access", nil, "An access to grant on all databases; repeatable; replaces all all-databases accesses") | ||
|
|
||
| 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,75 @@ | ||
| package oidctrust | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "testing" | ||
|
|
||
| qt "github.com/frankban/quicktest" | ||
| "github.com/planetscale/cli/internal/cmdutil" | ||
| "github.com/planetscale/cli/internal/config" | ||
| "github.com/planetscale/cli/internal/mock" | ||
| ps "github.com/planetscale/cli/internal/planetscale" | ||
| "github.com/planetscale/cli/internal/printer" | ||
| ) | ||
|
|
||
| func newUpdateHelper(svc *mock.OIDCTrustsService) *cmdutil.Helper { | ||
| var buf bytes.Buffer | ||
| format := printer.JSON | ||
| p := printer.NewPrinter(&format) | ||
| p.SetResourceOutput(&buf) | ||
| return &cmdutil.Helper{ | ||
| Printer: p, | ||
| Config: &config.Config{Organization: "planetscale"}, | ||
| Client: func() (*ps.Client, error) { return &ps.Client{OIDCTrusts: svc}, nil }, | ||
| } | ||
| } | ||
|
|
||
| func TestOIDCTrust_UpdateCmd_OnlySendsChangedFields(t *testing.T) { | ||
| c := qt.New(t) | ||
|
|
||
| svc := &mock.OIDCTrustsService{ | ||
| UpdateFn: func(ctx context.Context, req *ps.UpdateOIDCTrustRequest) (*ps.OIDCTrust, error) { | ||
| c.Assert(req.ID, qt.Equals, "trust-1") | ||
| c.Assert(req.Accesses, qt.DeepEquals, &[]ps.OIDCTrustAccessInput{{Access: "create_databases"}}) | ||
| c.Assert(req.Conditions, qt.IsNil) | ||
| c.Assert(req.BulkDatabaseAccesses, qt.IsNil) | ||
| c.Assert(req.DisplayName, qt.IsNil) | ||
| c.Assert(req.MaxTTLSeconds, qt.IsNil) | ||
| return &ps.OIDCTrust{ID: "trust-1"}, nil | ||
| }, | ||
| } | ||
|
|
||
| cmd := UpdateCmd(newUpdateHelper(svc)) | ||
| cmd.SetArgs([]string{"trust-1", "--org-access", "create_databases"}) | ||
| c.Assert(cmd.Execute(), qt.IsNil) | ||
| c.Assert(svc.UpdateFnInvoked, qt.IsTrue) | ||
| } | ||
|
|
||
| func TestOIDCTrust_UpdateCmd_NoFlags(t *testing.T) { | ||
| c := qt.New(t) | ||
|
|
||
| svc := &mock.OIDCTrustsService{} | ||
| cmd := UpdateCmd(newUpdateHelper(svc)) | ||
| cmd.SetArgs([]string{"trust-1"}) | ||
| c.Assert(cmd.Execute(), qt.ErrorMatches, `at least one of --name.*must be provided`) | ||
| c.Assert(svc.UpdateFnInvoked, qt.IsFalse) | ||
| } | ||
|
|
||
| func TestOIDCTrust_UpdateCmd_EmptyFlagsClearLists(t *testing.T) { | ||
| c := qt.New(t) | ||
|
|
||
| svc := &mock.OIDCTrustsService{ | ||
| UpdateFn: func(ctx context.Context, req *ps.UpdateOIDCTrustRequest) (*ps.OIDCTrust, error) { | ||
| c.Assert(req.Conditions, qt.DeepEquals, &[]ps.OIDCTrustConditionInput{}) | ||
| c.Assert(req.Accesses, qt.DeepEquals, &[]ps.OIDCTrustAccessInput{}) | ||
| c.Assert(req.BulkDatabaseAccesses, qt.DeepEquals, &[]string{}) | ||
| return &ps.OIDCTrust{ID: "trust-1"}, nil | ||
| }, | ||
| } | ||
|
|
||
| cmd := UpdateCmd(newUpdateHelper(svc)) | ||
| cmd.SetArgs([]string{"trust-1", "--condition=", "--org-access=", "--database-access=", "--all-databases-access="}) | ||
| c.Assert(cmd.Execute(), qt.IsNil) | ||
| c.Assert(svc.UpdateFnInvoked, qt.IsTrue) | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.