Skip to content

Commit 4d64bd3

Browse files
committed
fix(oauth): add "derive" command
1 parent 8774b08 commit 4d64bd3

File tree

1 file changed

+43
-0
lines changed
  • src/extension/host-binary/cmd

1 file changed

+43
-0
lines changed

src/extension/host-binary/cmd/main.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/spf13/cobra"
1111
"os"
1212
"os/signal"
13+
"slices"
1314
"syscall"
1415
)
1516

@@ -23,6 +24,7 @@ func main() {
2324
cmd.AddCommand(AuthorizeApp(ctx))
2425
cmd.AddCommand(UnauthorizeApp(ctx))
2526
cmd.AddCommand(ListOAuthApps(ctx))
27+
cmd.AddCommand(DeriveSecret(ctx))
2628
if err := cmd.Execute(); err != nil {
2729
fmt.Println(err)
2830
os.Exit(1)
@@ -238,3 +240,44 @@ func runDeleteSecret(ctx context.Context, opts deleteOptions) error {
238240
func assertMcpPolicyExists(ctx context.Context, apiClient client.ApiClient) error {
239241
return apiClient.SetPolicy(ctx, secretsapi.Policy{Name: mcpPolicyName, Images: []string{"*"}})
240242
}
243+
244+
type deriveOptions struct {
245+
Src string
246+
Dst string
247+
}
248+
249+
func DeriveSecret(ctx context.Context) *cobra.Command {
250+
opts := &deriveOptions{}
251+
cmd := &cobra.Command{
252+
Use: "derive",
253+
Short: "Derive a secret from another secret",
254+
Args: cobra.NoArgs,
255+
RunE: func(*cobra.Command, []string) error {
256+
return runDeriveSecret(ctx, *opts)
257+
},
258+
}
259+
flags := cmd.Flags()
260+
flags.StringVarP(&opts.Src, "src", "s", "", "Name of the source secret")
261+
_ = cmd.MarkFlagRequired("src")
262+
flags.StringVarP(&opts.Dst, "dst", "d", "", "Name of the destination secret")
263+
_ = cmd.MarkFlagRequired("dst")
264+
return cmd
265+
}
266+
267+
func runDeriveSecret(ctx context.Context, opts deriveOptions) error {
268+
c, err := newApiClient()
269+
if err != nil {
270+
return err
271+
}
272+
if err := assertMcpPolicyExists(ctx, c); err != nil {
273+
return err
274+
}
275+
s, err := c.GetSecret(ctx, opts.Src)
276+
if err != nil {
277+
return err
278+
}
279+
if !slices.Contains(s.Policies, mcpPolicyName) {
280+
s.Policies = append(s.Policies, mcpPolicyName)
281+
}
282+
return c.SetSecret(ctx, secretsapi.Secret{Name: opts.Dst, Value: s.Value, Policies: []string{mcpPolicyName}})
283+
}

0 commit comments

Comments
 (0)