github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/worker/uniter/runner/jujuc/secret-get_test.go (about) 1 // Copyright 2021 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package jujuc_test 5 6 import ( 7 "encoding/base64" 8 9 "github.com/juju/cmd/v3" 10 "github.com/juju/cmd/v3/cmdtesting" 11 "github.com/juju/testing" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/core/secrets" 16 "github.com/juju/juju/worker/uniter/runner/jujuc" 17 ) 18 19 type SecretGetSuite struct { 20 ContextSuite 21 } 22 23 var _ = gc.Suite(&SecretGetSuite{}) 24 25 func (s *SecretGetSuite) TestSecretGetInit(c *gc.C) { 26 27 for _, t := range []struct { 28 args []string 29 err string 30 }{{ 31 args: []string{"secret:9m4e2mr0ui3e8a215n4g", "--peek", "--refresh"}, 32 err: "ERROR specify one of --peek or --refresh but not both", 33 }, { 34 args: []string{}, 35 err: "ERROR require either a secret URI or label", 36 }} { 37 hctx, _ := s.ContextSuite.NewHookContext() 38 com, err := jujuc.NewCommand(hctx, "secret-get") 39 c.Assert(err, jc.ErrorIsNil) 40 ctx := cmdtesting.Context(c) 41 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, t.args) 42 c.Check(code, gc.Equals, 2) 43 c.Check(bufferString(ctx.Stderr), gc.Equals, t.err+"\n") 44 } 45 } 46 47 func (s *SecretGetSuite) TestSecretGetJson(c *gc.C) { 48 hctx, _ := s.ContextSuite.NewHookContext() 49 hctx.ContextSecrets.SecretValue = secrets.NewSecretValue(map[string]string{ 50 "key": base64.StdEncoding.EncodeToString([]byte("s3cret!")), 51 }) 52 53 com, err := jujuc.NewCommand(hctx, "secret-get") 54 c.Assert(err, jc.ErrorIsNil) 55 ctx := cmdtesting.Context(c) 56 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"secret:9m4e2mr0ui3e8a215n4g", "--format", "json"}) 57 c.Assert(code, gc.Equals, 0) 58 59 s.Stub.CheckCalls(c, []testing.StubCall{{FuncName: "GetSecret", Args: []interface{}{"secret:9m4e2mr0ui3e8a215n4g", "", false, false}}}) 60 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 61 c.Assert(bufferString(ctx.Stdout), gc.Equals, `{"key":"s3cret!"}`+"\n") 62 } 63 64 func (s *SecretGetSuite) TestSecretGetViaURI(c *gc.C) { 65 s.assertSecretGet(c, func() ([]string, testing.StubCall) { 66 return []string{"secret:9m4e2mr0ui3e8a215n4g"}, 67 testing.StubCall{ 68 FuncName: "GetSecret", 69 Args: []interface{}{"secret:9m4e2mr0ui3e8a215n4g", "", false, false}, 70 } 71 }) 72 } 73 74 func (s *SecretGetSuite) TestSecretGetViaLabel(c *gc.C) { 75 s.assertSecretGet(c, func() ([]string, testing.StubCall) { 76 return []string{"--label", "label"}, 77 testing.StubCall{ 78 FuncName: "GetSecret", 79 Args: []interface{}{"", "label", false, false}, 80 } 81 }) 82 } 83 84 func (s *SecretGetSuite) TestSecretGetPeekViaURI(c *gc.C) { 85 s.assertSecretGet(c, func() ([]string, testing.StubCall) { 86 return []string{"secret:9m4e2mr0ui3e8a215n4g", "--peek"}, 87 testing.StubCall{ 88 FuncName: "GetSecret", 89 Args: []interface{}{"secret:9m4e2mr0ui3e8a215n4g", "", false, true}, 90 } 91 }) 92 } 93 94 func (s *SecretGetSuite) TestSecretGetPeekViaLabel(c *gc.C) { 95 s.assertSecretGet(c, func() ([]string, testing.StubCall) { 96 return []string{"--label", "label", "--peek"}, 97 testing.StubCall{ 98 FuncName: "GetSecret", 99 Args: []interface{}{"", "label", false, true}, 100 } 101 }) 102 } 103 104 func (s *SecretGetSuite) TestSecretGetUpdateWithURI(c *gc.C) { 105 s.assertSecretGet(c, func() ([]string, testing.StubCall) { 106 return []string{"secret:9m4e2mr0ui3e8a215n4g", "--refresh"}, 107 testing.StubCall{ 108 FuncName: "GetSecret", 109 Args: []interface{}{"secret:9m4e2mr0ui3e8a215n4g", "", true, false}, 110 } 111 }) 112 } 113 114 func (s *SecretGetSuite) TestSecretGetUpdateWithLabel(c *gc.C) { 115 s.assertSecretGet(c, func() ([]string, testing.StubCall) { 116 return []string{"--label", "label", "--refresh"}, 117 testing.StubCall{ 118 FuncName: "GetSecret", 119 Args: []interface{}{"", "label", true, false}, 120 } 121 }) 122 } 123 124 func (s *SecretGetSuite) TestSecretGetUpdateWithBothURIAndLabel(c *gc.C) { 125 s.assertSecretGet(c, func() ([]string, testing.StubCall) { 126 return []string{"secret:9m4e2mr0ui3e8a215n4g", "--label", "label", "--refresh"}, 127 testing.StubCall{ 128 FuncName: "GetSecret", 129 Args: []interface{}{"secret:9m4e2mr0ui3e8a215n4g", "label", true, false}, 130 } 131 }) 132 } 133 134 func (s *SecretGetSuite) assertSecretGet(c *gc.C, f func() ([]string, testing.StubCall)) { 135 hctx, _ := s.ContextSuite.NewHookContext() 136 hctx.ContextSecrets.SecretValue = secrets.NewSecretValue(map[string]string{ 137 "cert": base64.StdEncoding.EncodeToString([]byte("cert")), 138 "key": base64.StdEncoding.EncodeToString([]byte("key")), 139 }) 140 141 com, err := jujuc.NewCommand(hctx, "secret-get") 142 c.Assert(err, jc.ErrorIsNil) 143 ctx := cmdtesting.Context(c) 144 args, checkCall := f() 145 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, args) 146 c.Assert(code, gc.Equals, 0) 147 148 s.Stub.CheckCalls(c, []testing.StubCall{checkCall}) 149 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 150 c.Assert(bufferString(ctx.Stdout), gc.Equals, ` 151 cert: cert 152 key: key 153 `[1:]) 154 } 155 156 func (s *SecretGetSuite) TestSecretGetBinary(c *gc.C) { 157 encodedValue := `R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLCAgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=` 158 hctx, _ := s.ContextSuite.NewHookContext() 159 hctx.ContextSecrets.SecretValue = secrets.NewSecretValue(map[string]string{ 160 "key": encodedValue, 161 }) 162 163 com, err := jujuc.NewCommand(hctx, "secret-get") 164 c.Assert(err, jc.ErrorIsNil) 165 ctx := cmdtesting.Context(c) 166 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"secret:9m4e2mr0ui3e8a215n4g"}) 167 c.Assert(code, gc.Equals, 0) 168 169 s.Stub.CheckCalls(c, []testing.StubCall{{FuncName: "GetSecret", Args: []interface{}{"secret:9m4e2mr0ui3e8a215n4g", "", false, false}}}) 170 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 171 c.Assert(bufferString(ctx.Stdout), gc.Equals, ` 172 key: !!binary | 173 R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5OTk6enp5 174 6enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/+ 175 +f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLCAgjoEwnuNAFOhpEMTRiggcz4 176 BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs= 177 `[1:]) 178 } 179 180 func (s *SecretGetSuite) TestSecretGetKey(c *gc.C) { 181 hctx, _ := s.ContextSuite.NewHookContext() 182 hctx.ContextSecrets.SecretValue = secrets.NewSecretValue(map[string]string{ 183 "cert": base64.StdEncoding.EncodeToString([]byte("cert")), 184 "key": base64.StdEncoding.EncodeToString([]byte("key")), 185 }) 186 187 com, err := jujuc.NewCommand(hctx, "secret-get") 188 c.Assert(err, jc.ErrorIsNil) 189 ctx := cmdtesting.Context(c) 190 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"secret:9m4e2mr0ui3e8a215n4g", "cert"}) 191 c.Assert(code, gc.Equals, 0) 192 193 s.Stub.CheckCalls(c, []testing.StubCall{{FuncName: "GetSecret", Args: []interface{}{"secret:9m4e2mr0ui3e8a215n4g", "", false, false}}}) 194 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 195 c.Assert(bufferString(ctx.Stdout), gc.Equals, ` 196 cert 197 `[1:]) 198 } 199 200 func (s *SecretGetSuite) TestSecretGetKeyBase64(c *gc.C) { 201 hctx, _ := s.ContextSuite.NewHookContext() 202 hctx.ContextSecrets.SecretValue = secrets.NewSecretValue(map[string]string{ 203 "cert": base64.StdEncoding.EncodeToString([]byte("cert")), 204 "key": base64.StdEncoding.EncodeToString([]byte("key")), 205 }) 206 207 com, err := jujuc.NewCommand(hctx, "secret-get") 208 c.Assert(err, jc.ErrorIsNil) 209 ctx := cmdtesting.Context(c) 210 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"secret:9m4e2mr0ui3e8a215n4g", "cert#base64"}) 211 c.Assert(code, gc.Equals, 0) 212 213 s.Stub.CheckCalls(c, []testing.StubCall{{FuncName: "GetSecret", Args: []interface{}{"secret:9m4e2mr0ui3e8a215n4g", "", false, false}}}) 214 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 215 c.Assert(bufferString(ctx.Stdout), gc.Equals, "Y2VydA==\n") 216 }