Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions internal/cmd/password/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (

func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
var flags struct {
role string
ttl cmdutil.TTLFlag
replica bool
readOnlyRegion string
role string
ttl cmdutil.TTLFlag
replica bool
readOnlyRegion string
clientTabletTargetingPolicy string
}

cmd := &cobra.Command{
Expand Down Expand Up @@ -83,14 +84,15 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
defer end()

pass, err := client.Passwords.Create(cmd.Context(), &ps.DatabaseBranchPasswordRequest{
Database: database,
Branch: branch,
Organization: ch.Config.Organization,
Name: name,
Role: flags.role,
TTL: int(flags.ttl.Value.Seconds()),
Replica: flags.replica,
ReadOnlyRegionID: readOnlyRegionID,
Database: database,
Branch: branch,
Organization: ch.Config.Organization,
Name: name,
Role: flags.role,
TTL: int(flags.ttl.Value.Seconds()),
Replica: flags.replica,
ReadOnlyRegionID: readOnlyRegionID,
ClientTabletTargetingPolicy: flags.clientTabletTargetingPolicy,
})
if err != nil {
switch cmdutil.ErrCode(err) {
Expand Down Expand Up @@ -121,6 +123,8 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
cmd.Flags().BoolVar(&flags.replica, "replica", false, "When enabled, the password will route all reads to the branch's primary replicas and all read-only regions.")
cmd.Flags().StringVar(&flags.readOnlyRegion, "read-only-region", "",
"Create a password scoped to a Vitess read-only region (region slug, display name, or id). List regions with: pscale database read-only-regions list <database>.")
cmd.Flags().StringVar(&flags.clientTabletTargetingPolicy, "client-tablet-targeting-policy", "",
"How a replica password handles a tablet type set by the client, such as USE @replica or mydb@replica. Allowed values: apply, ignore.")

return cmd
}
34 changes: 34 additions & 0 deletions internal/cmd/password/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,40 @@ func TestPassword_CreateCmd_Replica(t *testing.T) {
c.Assert(svc.CreateFnInvoked, qt.IsTrue)
}

func TestPassword_CreateCmd_ClientTabletTargetingPolicy(t *testing.T) {
for _, policy := range []string{"apply", "ignore"} {
t.Run(policy, func(t *testing.T) {
c := qt.New(t)

format := printer.JSON
p := printer.NewPrinter(&format)

svc := &mock.PasswordsService{
CreateFn: func(ctx context.Context, req *ps.DatabaseBranchPasswordRequest) (*ps.DatabaseBranchPassword, error) {
c.Assert(req.Replica, qt.IsTrue)
c.Assert(req.ClientTabletTargetingPolicy, qt.Equals, policy)
return &ps.DatabaseBranchPassword{Name: "foo"}, nil
},
}

ch := &cmdutil.Helper{
Printer: p,
Config: &config.Config{Organization: "planetscale"},
Client: func() (*ps.Client, error) {
return &ps.Client{Passwords: svc}, nil
},
}

cmd := CreateCmd(ch)
cmd.SetArgs([]string{"planetscale", "main", "production-replica-password", "--replica", "--client-tablet-targeting-policy", policy})
err := cmd.Execute()

c.Assert(err, qt.IsNil)
c.Assert(svc.CreateFnInvoked, qt.IsTrue)
})
}
}

func TestPassword_CreateCmd_ReplicaWithoutRole(t *testing.T) {
c := qt.New(t)

Expand Down
88 changes: 46 additions & 42 deletions internal/cmd/password/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,30 +94,32 @@ func passwordSelector(args []string, name string) error {
type Passwords []*Password

type Password struct {
PublicID string `header:"id" json:"id"`
Name string `header:"name" json:"name"`
Branch string `header:"branch" json:"branch"`
Username string `header:"username" json:"username"`
Role string `header:"role" json:"role"`
RoleDesc string `header:"role description" json:"-"`
ConnectionType string `header:"connection type" json:"connection_type"`
TTL int `header:"ttl" json:"ttl"`
Remaining int `header:"ttl_remaining" json:"-"`
CreatedAt int64 `json:"created_at"`
Expired bool `header:"expired" json:"expired"`
orig *ps.DatabaseBranchPassword
PublicID string `header:"id" json:"id"`
Name string `header:"name" json:"name"`
Branch string `header:"branch" json:"branch"`
Username string `header:"username" json:"username"`
Role string `header:"role" json:"role"`
RoleDesc string `header:"role description" json:"-"`
ConnectionType string `header:"connection type" json:"connection_type"`
ClientTabletTargetingPolicy string `header:"client tablet targeting" json:"client_tablet_targeting_policy"`
TTL int `header:"ttl" json:"ttl"`
Remaining int `header:"ttl_remaining" json:"-"`
CreatedAt int64 `json:"created_at"`
Expired bool `header:"expired" json:"expired"`
orig *ps.DatabaseBranchPassword
}

type passwordWithoutTTL struct {
PublicID string `header:"id" json:"id"`
Name string `header:"name" json:"name"`
Branch string `header:"branch" json:"branch"`
Username string `header:"username" json:"username"`
Role string `header:"role" json:"role"`
RoleDesc string `header:"role description" json:"-"`
ConnectionType string `header:"connection type" json:"connection_type"`
CreatedAt int64 `json:"created_at"`
orig *ps.DatabaseBranchPassword
PublicID string `header:"id" json:"id"`
Name string `header:"name" json:"name"`
Branch string `header:"branch" json:"branch"`
Username string `header:"username" json:"username"`
Role string `header:"role" json:"role"`
RoleDesc string `header:"role description" json:"-"`
ConnectionType string `header:"connection type" json:"connection_type"`
ClientTabletTargetingPolicy string `header:"client tablet targeting" json:"client_tablet_targeting_policy"`
CreatedAt int64 `json:"created_at"`
orig *ps.DatabaseBranchPassword

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create output omits targeting policy

Low Severity

PasswordWithPlainText and toPasswordWithPlainText never copy ClientTabletTargetingPolicy, so pscale password create human and CSV tables omit the policy just set with --client-tablet-targeting-policy. JSON still includes it via orig, and show/list display it because Password and passwordWithoutTTL were updated.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 5a3e995. Configure here.

}

type PasswordWithPlainText struct {
Expand Down Expand Up @@ -163,32 +165,34 @@ func toPassword(password *ps.DatabaseBranchPassword) *Password {
ttlRemaining = max(int(time.Until(password.ExpiresAt).Seconds()), 0)
}
return &Password{
Name: password.Name,
Branch: password.Branch.Name,
PublicID: password.PublicID,
Username: password.Username,
Role: password.Role,
RoleDesc: toRoleDesc(password.Role),
ConnectionType: toConnectionTypeDesc(password),
TTL: password.TTL,
Remaining: ttlRemaining,
CreatedAt: toTimestamp(password.CreatedAt),
Expired: password.TTL > 0 && ttlRemaining == 0,
orig: password,
Name: password.Name,
Branch: password.Branch.Name,
PublicID: password.PublicID,
Username: password.Username,
Role: password.Role,
RoleDesc: toRoleDesc(password.Role),
ConnectionType: toConnectionTypeDesc(password),
ClientTabletTargetingPolicy: password.ClientTabletTargetingPolicy,
TTL: password.TTL,
Remaining: ttlRemaining,
CreatedAt: toTimestamp(password.CreatedAt),
Expired: password.TTL > 0 && ttlRemaining == 0,
orig: password,
}
}

func toPasswordWithoutTTL(password *ps.DatabaseBranchPassword) *passwordWithoutTTL {
return &passwordWithoutTTL{
Name: password.Name,
Branch: password.Branch.Name,
PublicID: password.PublicID,
Username: password.Username,
Role: password.Role,
RoleDesc: toRoleDesc(password.Role),
ConnectionType: toConnectionTypeDesc(password),
CreatedAt: toTimestamp(password.CreatedAt),
orig: password,
Name: password.Name,
Branch: password.Branch.Name,
PublicID: password.PublicID,
Username: password.Username,
Role: password.Role,
RoleDesc: toRoleDesc(password.Role),
ConnectionType: toConnectionTypeDesc(password),
ClientTabletTargetingPolicy: password.ClientTabletTargetingPolicy,
CreatedAt: toTimestamp(password.CreatedAt),
orig: password,
}
}

Expand Down
9 changes: 9 additions & 0 deletions internal/cmd/password/password_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,12 @@ func TestToConnectionTypeDesc(t *testing.T) {
Region: ps.Region{Slug: "eu-west"},
}), qt.Equals, "Read-only region (eu-west)")
}

func TestToPasswordClientTabletTargetingPolicy(t *testing.T) {
c := qt.New(t)

password := &ps.DatabaseBranchPassword{Replica: true, ClientTabletTargetingPolicy: "ignore"}

c.Assert(toPassword(password).ClientTabletTargetingPolicy, qt.Equals, "ignore")
c.Assert(toPasswordWithoutTTL(password).ClientTabletTargetingPolicy, qt.Equals, "ignore")
}
32 changes: 22 additions & 10 deletions internal/cmd/password/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,28 @@ import (
"github.com/spf13/cobra"
)

// UpdateCmd updates the name or IP restrictions of a branch password. It never
// changes the password secret; use create and delete, or renew, for that.
// UpdateCmd updates the name, IP restrictions or client tablet targeting policy
// of a branch password. It never changes the password secret; use create and
// delete, or renew, for that.
func UpdateCmd(ch *cmdutil.Helper) *cobra.Command {
var flags struct {
name string
newName string
cidrs []string
name string
newName string
cidrs []string
clientTabletTargetingPolicy string
}

cmd := &cobra.Command{
Use: "update <database> <branch> [<password-id>]",
Short: "Update a branch password's name or IP restrictions",
Long: `Update a branch password's name or IP restrictions.
Short: "Update a branch password's name, IP restrictions or client tablet targeting policy",
Long: `Update a branch password's name, IP restrictions or client tablet targeting policy.

This does not change the password itself. To rotate a password, create a new
one and delete the old one.`,
Example: ` pscale password update mydb main pscale_pw_xxx --new-name reporting
pscale password update mydb main --name reporting --cidrs 10.0.0.0/8,192.168.1.1/32
pscale password update mydb main --name reporting --cidrs ""`,
pscale password update mydb main --name reporting --cidrs ""
pscale password update mydb main --name reporting --client-tablet-targeting-policy ignore`,
Args: cobra.RangeArgs(2, 3),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
Expand All @@ -44,8 +47,9 @@ one and delete the old one.`,

newNameSet := cmd.Flags().Changed("new-name")
cidrsSet := cmd.Flags().Changed("cidrs")
if !newNameSet && !cidrsSet {
return errors.New("at least one of --new-name or --cidrs must be provided")
clientTabletTargetingPolicySet := cmd.Flags().Changed("client-tablet-targeting-policy")
if !newNameSet && !cidrsSet && !clientTabletTargetingPolicySet {
return errors.New("at least one of --new-name, --cidrs or --client-tablet-targeting-policy must be provided")
}
if newNameSet && flags.newName == "" {
return errors.New("--new-name cannot be empty")
Expand Down Expand Up @@ -79,6 +83,9 @@ one and delete the old one.`,
cidrs := normalizeCIDRs(flags.cidrs)
updateReq.CIDRs = &cidrs
}
if clientTabletTargetingPolicySet {
updateReq.ClientTabletTargetingPolicy = flags.clientTabletTargetingPolicy
}

end := ch.Printer.PrintProgress(fmt.Sprintf("Updating password %s in %s/%s",
printer.BoldBlue(passwordId), printer.BoldBlue(database), printer.BoldBlue(branch)))
Expand Down Expand Up @@ -107,6 +114,9 @@ one and delete the old one.`,
ch.Printer.Printf("IP restrictions: %s\n", strings.Join(password.CIDRs, ", "))
}
}
if clientTabletTargetingPolicySet {
ch.Printer.Printf("Client tablet targeting: %s\n", password.ClientTabletTargetingPolicy)
}
return nil
}

Expand All @@ -118,6 +128,8 @@ one and delete the old one.`,
cmd.Flags().StringVar(&flags.newName, "new-name", "", "New name for the password")
cmd.Flags().StringSliceVar(&flags.cidrs, "cidrs", nil,
"Replace the IP addresses and CIDR ranges allowed to use this password. Pass an empty value to remove all restrictions")
cmd.Flags().StringVar(&flags.clientTabletTargetingPolicy, "client-tablet-targeting-policy", "",
"How a replica password handles a tablet type set by the client, such as USE @replica or mydb@replica. Allowed values: apply, ignore.")

return cmd
}
Expand Down
33 changes: 32 additions & 1 deletion internal/cmd/password/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,37 @@ func TestPassword_UpdateCmdClearsCIDRs(t *testing.T) {
c.Assert(svc.UpdateFnInvoked, qt.IsTrue)
}

func TestPassword_UpdateCmdSetsClientTabletTargetingPolicy(t *testing.T) {
c := qt.New(t)

format := printer.JSON
p := printer.NewPrinter(&format)

svc := &mock.PasswordsService{
UpdateFn: func(ctx context.Context, req *ps.UpdateDatabaseBranchPasswordRequest) (*ps.DatabaseBranchPassword, error) {
c.Assert(req.Name, qt.Equals, "")
c.Assert(req.CIDRs, qt.IsNil)
c.Assert(req.ClientTabletTargetingPolicy, qt.Equals, "apply")
return &ps.DatabaseBranchPassword{PublicID: "pscale_pw_xxx", ClientTabletTargetingPolicy: "apply"}, nil
},
}

ch := &cmdutil.Helper{
Printer: p,
Config: &config.Config{Organization: "planetscale"},
Client: func() (*ps.Client, error) {
return &ps.Client{Passwords: svc}, nil
},
}

cmd := UpdateCmd(ch)
cmd.SetArgs([]string{"planetscale", "development", "pscale_pw_xxx", "--client-tablet-targeting-policy", "apply"})
err := cmd.Execute()

c.Assert(err, qt.IsNil)
c.Assert(svc.UpdateFnInvoked, qt.IsTrue)
}

func TestPassword_UpdateCmdByName(t *testing.T) {
c := qt.New(t)

Expand Down Expand Up @@ -180,6 +211,6 @@ func TestPassword_UpdateCmdRequiresAFlag(t *testing.T) {
err := cmd.Execute()

c.Assert(err, qt.IsNotNil)
c.Assert(err.Error(), qt.Contains, "at least one of --new-name or --cidrs")
c.Assert(err.Error(), qt.Contains, "at least one of --new-name, --cidrs or --client-tablet-targeting-policy")
c.Assert(svc.UpdateFnInvoked, qt.IsFalse)
}
Loading