github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/worker/uniter/runner/jujuc/secret-set.go (about) 1 // Copyright 2021 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package jujuc 5 6 import ( 7 "github.com/juju/cmd/v3" 8 "github.com/juju/errors" 9 10 jujucmd "github.com/juju/juju/cmd" 11 "github.com/juju/juju/core/secrets" 12 ) 13 14 type secretUpdateCommand struct { 15 secretUpsertCommand 16 17 secretURI *secrets.URI 18 } 19 20 // NewSecretSetCommand returns a command to create a secret. 21 func NewSecretSetCommand(ctx Context) (cmd.Command, error) { 22 return &secretUpdateCommand{ 23 secretUpsertCommand: secretUpsertCommand{ctx: ctx}, 24 }, nil 25 } 26 27 // Info implements cmd.Command. 28 func (c *secretUpdateCommand) Info() *cmd.Info { 29 doc := ` 30 Update a secret with a list of key values, or set new metadata. 31 If a value has the '#base64' suffix, it is already in base64 format and no 32 encoding will be performed, otherwise the value will be base64 encoded 33 prior to being stored. 34 To just update selected metadata like rotate policy, do not specify any secret value. 35 36 Examples: 37 secret-set secret:9m4e2mr0ui3e8a215n4g token=34ae35facd4 38 secret-set secret:9m4e2mr0ui3e8a215n4g key#base64 AA== 39 secret-set secret:9m4e2mr0ui3e8a215n4g --rotate monthly token=s3cret 40 secret-set secret:9m4e2mr0ui3e8a215n4g --expire 24h 41 secret-set secret:9m4e2mr0ui3e8a215n4g --expire 24h token=s3cret 42 secret-set secret:9m4e2mr0ui3e8a215n4g --expire 2025-01-01T06:06:06 token=s3cret 43 secret-set secret:9m4e2mr0ui3e8a215n4g --label db-password \ 44 --description "my database password" \ 45 data#base64 s3cret== 46 secret-set secret:9m4e2mr0ui3e8a215n4g --label db-password \ 47 --description "my database password" 48 secret-set secret:9m4e2mr0ui3e8a215n4g --label db-password \ 49 --description "my database password" \ 50 --file=/path/to/file 51 ` 52 return jujucmd.Info(&cmd.Info{ 53 Name: "secret-set", 54 Args: "<ID> [key[#base64]=value...]", 55 Purpose: "update an existing secret", 56 Doc: doc, 57 }) 58 } 59 60 // Init implements cmd.Command. 61 func (c *secretUpdateCommand) Init(args []string) error { 62 if len(args) < 1 { 63 return errors.New("missing secret URI") 64 } 65 var err error 66 if c.secretURI, err = secrets.ParseURI(args[0]); err != nil { 67 return errors.Trace(err) 68 } 69 return c.secretUpsertCommand.Init(args[1:]) 70 } 71 72 // Run implements cmd.Command. 73 func (c *secretUpdateCommand) Run(ctx *cmd.Context) error { 74 return c.ctx.UpdateSecret(c.secretURI, c.marshallArg()) 75 }