Skip to content

Commit

Permalink
feat: add commands to get and update root encryption key (#1541)
Browse files Browse the repository at this point in the history
* feat: add commands to get and update root encryption key

* chore: rename command to align with vault product
  • Loading branch information
sweatybridge authored Oct 3, 2023
1 parent 064d7eb commit aa52e0c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
41 changes: 41 additions & 0 deletions cmd/vault.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cmd

import (
"os"

"github.com/spf13/cobra"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/internal/vault/get"
"github.com/supabase/cli/internal/vault/update"
)

var (
vaultCmd = &cobra.Command{
GroupID: groupManagementAPI,
Use: "vault",
Short: "Manage column encryption of Supabase projects",
}

rootKeyGetCmd = &cobra.Command{
Use: "get-root-key",
Short: "Get the root encryption key of a Supabase project",
RunE: func(cmd *cobra.Command, args []string) error {
return get.Run(cmd.Context(), flags.ProjectRef)
},
}

rootKeyUpdateCmd = &cobra.Command{
Use: "update-root-key",
Short: "Update root encryption key of a Supabase project",
RunE: func(cmd *cobra.Command, args []string) error {
return update.Run(cmd.Context(), flags.ProjectRef, os.Stdin)
},
}
)

func init() {
vaultCmd.PersistentFlags().StringVar(&flags.ProjectRef, "project-ref", "", "Project ref of the Supabase project.")
vaultCmd.AddCommand(rootKeyUpdateCmd)
vaultCmd.AddCommand(rootKeyGetCmd)
rootCmd.AddCommand(vaultCmd)
}
23 changes: 23 additions & 0 deletions internal/vault/get/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package get

import (
"context"
"errors"
"fmt"

"github.com/supabase/cli/internal/utils"
)

func Run(ctx context.Context, projectRef string) error {
resp, err := utils.GetSupabase().GetPgsodiumConfigWithResponse(ctx, projectRef)
if err != nil {
return err
}

if resp.JSON200 == nil {
return errors.New("Unexpected error retrieving project root key: " + string(resp.Body))
}

fmt.Println(resp.JSON200.RootKey)
return nil
}
31 changes: 31 additions & 0 deletions internal/vault/update/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package update

import (
"context"
"errors"
"fmt"
"os"
"strings"

"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/credentials"
"github.com/supabase/cli/pkg/api"
)

func Run(ctx context.Context, projectRef string, stdin *os.File) error {
fmt.Fprintf(os.Stderr, "Enter a new root key: ")
input := credentials.PromptMasked(stdin)
resp, err := utils.GetSupabase().UpdatePgsodiumConfigWithResponse(ctx, projectRef, api.UpdatePgsodiumConfigBody{
RootKey: strings.TrimSpace(input),
})
if err != nil {
return err
}

if resp.JSON200 == nil {
return errors.New("Unexpected error updating project root key: " + string(resp.Body))
}

fmt.Println("Finished " + utils.Aqua("supabase root-key update") + ".")
return nil
}

0 comments on commit aa52e0c

Please sign in to comment.