Skip to content

Commit b9fc271

Browse files
committed
chore: unexport PassValue.Value
Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
1 parent afc67a6 commit b9fc271

File tree

5 files changed

+40
-19
lines changed

5 files changed

+40
-19
lines changed

plugins/pass/command_test.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ func Test_rootCommand(t *testing.T) {
5656
require.NoError(t, err)
5757
impl, ok := s.(*pass.PassValue)
5858
require.True(t, ok)
59-
assert.Equal(t, "bar=bar=bar", string(impl.Value))
59+
v, err := impl.Marshal()
60+
require.NoError(t, err)
61+
assert.Equal(t, "bar=bar=bar", string(v))
6062
})
6163
t.Run("from STDIN", func(t *testing.T) {
6264
mock := teststore.NewMockStore()
@@ -67,7 +69,9 @@ func Test_rootCommand(t *testing.T) {
6769
require.NoError(t, err)
6870
impl, ok := s.(*pass.PassValue)
6971
require.True(t, ok)
70-
assert.Equal(t, "my\nmultiline\nvalue", string(impl.Value))
72+
v, err := impl.Marshal()
73+
require.NoError(t, err)
74+
assert.Equal(t, "my\nmultiline\nvalue", string(v))
7175
})
7276
t.Run("with --metadata flag", func(t *testing.T) {
7377
mock := teststore.NewMockStore()
@@ -78,7 +82,9 @@ func Test_rootCommand(t *testing.T) {
7882
require.NoError(t, err)
7983
impl, ok := s.(*pass.PassValue)
8084
require.True(t, ok)
81-
assert.Equal(t, "bar", string(impl.Value))
85+
v, err := impl.Marshal()
86+
require.NoError(t, err)
87+
assert.Equal(t, "bar", string(v))
8288
assert.Equal(t, map[string]string{"name": "bob", "expiry": "2027-03-01"}, impl.Metadata())
8389
})
8490
t.Run("from STDIN JSON with value and metadata", func(t *testing.T) {
@@ -90,7 +96,9 @@ func Test_rootCommand(t *testing.T) {
9096
require.NoError(t, err)
9197
impl, ok := s.(*pass.PassValue)
9298
require.True(t, ok)
93-
assert.Equal(t, "bar", string(impl.Value))
99+
v, err := impl.Marshal()
100+
require.NoError(t, err)
101+
assert.Equal(t, "bar", string(v))
94102
assert.Equal(t, map[string]string{"name": "bob"}, impl.Metadata())
95103
})
96104
t.Run("from STDIN JSON merged with --metadata flag wins on collision", func(t *testing.T) {
@@ -102,7 +110,9 @@ func Test_rootCommand(t *testing.T) {
102110
require.NoError(t, err)
103111
impl, ok := s.(*pass.PassValue)
104112
require.True(t, ok)
105-
assert.Equal(t, "bar", string(impl.Value))
113+
v, err := impl.Marshal()
114+
require.NoError(t, err)
115+
assert.Equal(t, "bar", string(v))
106116
assert.Equal(t, map[string]string{"name": "alice", "extra": "thing"}, impl.Metadata())
107117
})
108118
t.Run("invalid --metadata flag (no =)", func(t *testing.T) {
@@ -129,8 +139,8 @@ func Test_rootCommand(t *testing.T) {
129139
t.Run("list", func(t *testing.T) {
130140
t.Run("ok", func(t *testing.T) {
131141
mock := teststore.NewMockStore(teststore.WithStore(map[store.ID]store.Secret{
132-
store.MustParseID("foo"): &pass.PassValue{Value: []byte("bar")},
133-
store.MustParseID("baz"): &pass.PassValue{Value: []byte("0")},
142+
store.MustParseID("foo"): pass.NewPassValue([]byte("bar")),
143+
store.MustParseID("baz"): pass.NewPassValue([]byte("0")),
134144
}))
135145
out, err := executeCommand(Root(t.Context(), mock, mockInfo), "list")
136146
assert.NoError(t, err)
@@ -147,8 +157,8 @@ func Test_rootCommand(t *testing.T) {
147157
t.Run("rm", func(t *testing.T) {
148158
t.Run("ok (two secrets)", func(t *testing.T) {
149159
mock := teststore.NewMockStore(teststore.WithStore(map[store.ID]store.Secret{
150-
store.MustParseID("foo"): &pass.PassValue{Value: []byte("bar")},
151-
store.MustParseID("baz"): &pass.PassValue{Value: []byte("0")},
160+
store.MustParseID("foo"): pass.NewPassValue([]byte("bar")),
161+
store.MustParseID("baz"): pass.NewPassValue([]byte("0")),
152162
}))
153163
out, err := executeCommand(Root(t.Context(), mock, mockInfo), "rm", "foo", "baz")
154164
assert.NoError(t, err)
@@ -159,8 +169,8 @@ func Test_rootCommand(t *testing.T) {
159169
})
160170
t.Run("--all", func(t *testing.T) {
161171
mock := teststore.NewMockStore(teststore.WithStore(map[store.ID]store.Secret{
162-
store.MustParseID("foo"): &pass.PassValue{Value: []byte("bar")},
163-
store.MustParseID("baz"): &pass.PassValue{Value: []byte("0")},
172+
store.MustParseID("foo"): pass.NewPassValue([]byte("bar")),
173+
store.MustParseID("baz"): pass.NewPassValue([]byte("0")),
164174
}))
165175
out, err := executeCommand(Root(t.Context(), mock, mockInfo), "rm", "--all")
166176
assert.NoError(t, err)
@@ -199,7 +209,7 @@ func Test_rootCommand(t *testing.T) {
199209
t.Run("get", func(t *testing.T) {
200210
t.Run("ok", func(t *testing.T) {
201211
mock := teststore.NewMockStore(teststore.WithStore(map[store.ID]store.Secret{
202-
store.MustParseID("foo"): &pass.PassValue{Value: []byte("bar")},
212+
store.MustParseID("foo"): pass.NewPassValue([]byte("bar")),
203213
}))
204214
out, err := executeCommand(Root(t.Context(), mock, mockInfo), "get", "foo")
205215
assert.NoError(t, err)
@@ -241,7 +251,7 @@ func Test_rootCommandTelemetry(t *testing.T) {
241251
t.Run(tc.name, func(t *testing.T) {
242252
spanRecorder, metricReader := testhelper.SetupTelemetry(t)
243253
mock := teststore.NewMockStore(teststore.WithStore(map[store.ID]store.Secret{
244-
store.MustParseID("baz"): &pass.PassValue{Value: []byte("bar")},
254+
store.MustParseID("baz"): pass.NewPassValue([]byte("bar")),
245255
}))
246256
_, err := executeCommand(Root(t.Context(), mock, mockInfo), tc.args...)
247257
assert.NoError(t, err)

plugins/pass/commands/set.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ func SetCommand(kc store.Store) *cobra.Command {
9595
merged[k] = v
9696
}
9797

98-
pv := &pass.PassValue{Value: []byte(s.val)}
98+
pv := &pass.PassValue{}
99+
if err := pv.Unmarshal([]byte(s.val)); err != nil {
100+
return err
101+
}
99102
if len(merged) > 0 {
100103
if err := pv.SetMetadata(merged); err != nil {
101104
return err

plugins/pass/plugin.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@ func unpackValue(id store.ID, secret store.Secret) (*plugin.Envelope, error) {
6060
if !ok {
6161
return nil, errUnknownSecretType
6262
}
63+
value, err := impl.Marshal()
64+
if err != nil {
65+
return nil, err
66+
}
6367
return &plugin.Envelope{
6468
ID: id,
65-
Value: impl.Value,
69+
Value: value,
6670
}, nil
6771
}
6872

plugins/pass/plugin_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func Test_passPlugin(t *testing.T) {
3434
t.Parallel()
3535
t.Run("ok", func(t *testing.T) {
3636
mock := teststore.NewMockStore(teststore.WithStore(map[store.ID]store.Secret{
37-
store.MustParseID("foo"): &pass.PassValue{Value: []byte("bar")},
37+
store.MustParseID("foo"): pass.NewPassValue([]byte("bar")),
3838
}))
3939
p := &passPlugin{kc: mock, logger: testhelper.TestLogger(t)}
4040
e, err := p.GetSecrets(t.Context(), secrets.MustParsePattern("foo"))

plugins/pass/store/store.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@ import (
2424
var _ store.Secret = &PassValue{}
2525

2626
type PassValue struct {
27-
Value []byte `json:"value"`
27+
value []byte
2828
metadata map[string]string // not exported; populated via SetMetadata
2929
}
3030

31+
func NewPassValue(value []byte) *PassValue {
32+
return &PassValue{value: value}
33+
}
34+
3135
func (m *PassValue) Marshal() ([]byte, error) {
32-
return m.Value, nil
36+
return m.value, nil
3337
}
3438

3539
func (m *PassValue) Unmarshal(data []byte) error {
36-
m.Value = data
40+
m.value = data
3741
return nil
3842
}
3943

0 commit comments

Comments
 (0)