Skip to content
This repository was archived by the owner on Mar 16, 2024. It is now read-only.

Commit 7195323

Browse files
authored
fix: cleanup acorn secrets reveal and general non-table output (#1812) (#1833)
1 parent 330e88e commit 7195323

File tree

9 files changed

+125
-14
lines changed

9 files changed

+125
-14
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@
5050
"description": "The image of Acorn to install."
5151
},
5252
]
53-
}
53+
}

pkg/cli/builder/table/funcs.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ func toKObject(obj any) (kclient.Object, bool) {
126126
}
127127

128128
func cleanFields(obj any) any {
129+
if ol, ok := obj.(objectList); ok {
130+
for i, o := range ol.Items {
131+
ol.Items[i] = cleanFields(o)
132+
}
133+
return ol
134+
}
135+
129136
ro, ok := toKObject(obj)
130137
if ok {
131138
ro.SetManagedFields(nil)
@@ -147,11 +154,38 @@ func cleanFields(obj any) any {
147154
}
148155
}
149156
ro.SetAnnotations(annotations)
157+
158+
// decode secret values
159+
if sec, ok := ro.(*apiv1.Secret); ok {
160+
return decodeSecret(sec)
161+
}
162+
150163
return ro
151164
}
152165
return obj
153166
}
154167

168+
func decodeSecret(sec *apiv1.Secret) any {
169+
decodedSecret := struct {
170+
metav1.TypeMeta `json:",inline"`
171+
metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
172+
173+
Type string `json:"type,omitempty"`
174+
Data map[string]string `json:"data,omitempty"`
175+
Keys []string `json:"keys,omitempty"`
176+
}{
177+
TypeMeta: sec.TypeMeta,
178+
ObjectMeta: sec.ObjectMeta,
179+
Type: sec.Type,
180+
Data: map[string]string{},
181+
Keys: sec.Keys,
182+
}
183+
for k, v := range sec.Data {
184+
decodedSecret.Data[k] = string(v)
185+
}
186+
return decodedSecret
187+
}
188+
155189
func FormatYAML(data any) (string, error) {
156190
bytes, err := yaml.Marshal(cleanFields(data))
157191
return string(bytes) + "\n", err

pkg/cli/builder/table/writer.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ func (t *writer) writeDataObject(objs ...any) error {
230230
return errors.Join(t.errs...)
231231
}
232232

233+
type objectList struct {
234+
Items []any `json:"items"`
235+
}
236+
233237
func (t *writer) flush(closing bool) error {
234238
if len(t.errs) > 0 {
235239
return errors.Join(t.errs...)
@@ -262,9 +266,7 @@ func (t *writer) flush(closing bool) error {
262266
if closing {
263267
// if we are closing, then we want to print objects its in a proper list
264268
// data structure
265-
err := t.writeDataObject(map[string]any{
266-
"items": objs,
267-
})
269+
err := t.writeDataObject(objectList{Items: objs})
268270
if err != nil {
269271
return err
270272
}

pkg/cli/secret_expose.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ func (a *Reveal) Run(cmd *cobra.Command, args []string) error {
6666
Key: entry.Key,
6767
Value: string(entry.Value),
6868
}, &secret)
69+
if a.Output != "" {
70+
// in non-table output, we write the source object to buffer,
71+
// so we exit here to not write the same object multiple times (one per data key)
72+
break
73+
}
6974
}
7075
}
7176

pkg/cli/secret_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,63 @@ func TestSecret(t *testing.T) {
183183
wantErr: false,
184184
wantOut: "NAME TYPE KEY VALUE\n",
185185
},
186+
{
187+
name: "acorn secret reveal secret.withdata", fields: fields{
188+
All: false,
189+
Quiet: false,
190+
Output: "",
191+
},
192+
commandContext: CommandContext{
193+
ClientFactory: &testdata.MockClientFactory{},
194+
StdOut: w,
195+
StdErr: w,
196+
StdIn: strings.NewReader("y\n"),
197+
},
198+
args: args{
199+
args: []string{"reveal", "secret.withdata"},
200+
client: &testdata.MockClient{},
201+
},
202+
wantErr: false,
203+
wantOut: "NAME TYPE KEY VALUE\nsecret.withdata baz qux\nsecret.withdata foo bar\n",
204+
},
205+
{
206+
name: "acorn secret reveal secret.withdata -o jsoncompact", fields: fields{
207+
All: false,
208+
Quiet: false,
209+
Output: "",
210+
},
211+
commandContext: CommandContext{
212+
ClientFactory: &testdata.MockClientFactory{},
213+
StdOut: w,
214+
StdErr: w,
215+
StdIn: strings.NewReader("y\n"),
216+
},
217+
args: args{
218+
args: []string{"reveal", "secret.withdata", "-o=jsoncompact"},
219+
client: &testdata.MockClient{},
220+
},
221+
wantErr: false,
222+
wantOut: "{\"items\":[{\"metadata\":{\"name\":\"secret.withdata\",\"creationTimestamp\":null},\"data\":{\"baz\":\"qux\",\"foo\":\"bar\"}}]}\n",
223+
},
224+
{
225+
name: "acorn secret reveal secret.withdata secret.withdata2 -o jsoncompact", fields: fields{
226+
All: false,
227+
Quiet: false,
228+
Output: "",
229+
},
230+
commandContext: CommandContext{
231+
ClientFactory: &testdata.MockClientFactory{},
232+
StdOut: w,
233+
StdErr: w,
234+
StdIn: strings.NewReader("y\n"),
235+
},
236+
args: args{
237+
args: []string{"reveal", "secret.withdata", "secret.withdata2", "-o=jsoncompact"},
238+
client: &testdata.MockClient{},
239+
},
240+
wantErr: false,
241+
wantOut: "{\"items\":[{\"metadata\":{\"name\":\"secret.withdata\",\"creationTimestamp\":null},\"data\":{\"baz\":\"qux\",\"foo\":\"bar\"}},{\"metadata\":{\"name\":\"secret.withdata2\",\"creationTimestamp\":null},\"data\":{\"spam\":\"eggs\"}}]}\n",
242+
},
186243
{
187244
name: "acorn secret reveal dne", fields: fields{
188245
All: false,

pkg/cli/testdata/MockClient.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,26 @@ func (m *MockClient) SecretReveal(ctx context.Context, name string) (*apiv1.Secr
379379
Data: nil,
380380
Keys: nil,
381381
}, nil
382+
case "secret.withdata":
383+
return &apiv1.Secret{
384+
ObjectMeta: metav1.ObjectMeta{
385+
Name: "secret.withdata",
386+
},
387+
Data: map[string][]byte{
388+
"foo": []byte("bar"),
389+
"baz": []byte("qux"),
390+
},
391+
}, nil
392+
case "secret.withdata2":
393+
return &apiv1.Secret{
394+
ObjectMeta: metav1.ObjectMeta{
395+
Name: "secret.withdata2",
396+
},
397+
Data: map[string][]byte{
398+
"spam": []byte("eggs"),
399+
},
400+
}, nil
401+
382402
}
383403
return nil, nil
384404
}

pkg/cli/testdata/all/all_test_json.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,7 @@ VOLUMES:
6363
{
6464
"metadata": {
6565
"name": "found.vol",
66-
"creationTimestamp": null,
67-
"labels": {
68-
"acorn.io/app-name": "found",
69-
"acorn.io/volume-name": "vol"
70-
}
66+
"creationTimestamp": null
7167
},
7268
"spec": {},
7369
"status": {

pkg/cli/testdata/all/all_test_yaml.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ VOLUMES:
4444
items:
4545
- metadata:
4646
creationTimestamp: null
47-
labels:
48-
acorn.io/app-name: found
49-
acorn.io/volume-name: vol
5047
name: found.vol
5148
spec: {}
5249
status:

pkg/cli/volumes_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func TestVolume(t *testing.T) {
113113
client: &testdata.MockClient{},
114114
},
115115
wantErr: false,
116-
wantOut: "{\n \"items\": [\n {\n \"metadata\": {\n \"name\": \"found.vol\",\n \"creationTimestamp\": null,\n \"labels\": {\n \"acorn.io/app-name\": \"found\",\n \"acorn.io/volume-name\": \"vol\"\n }\n },\n \"spec\": {},\n \"status\": {\n \"appName\": \"found\",\n \"appPublicName\": \"found\",\n \"volumeName\": \"vol\",\n \"columns\": {}\n }\n }\n ]\n}\n\n",
116+
wantOut: "{\n \"items\": [\n {\n \"metadata\": {\n \"name\": \"found.vol\",\n \"creationTimestamp\": null\n },\n \"spec\": {},\n \"status\": {\n \"appName\": \"found\",\n \"appPublicName\": \"found\",\n \"volumeName\": \"vol\",\n \"columns\": {}\n }\n }\n ]\n}\n\n",
117117
},
118118
{
119119
name: "acorn volume -o yaml", fields: fields{
@@ -126,7 +126,7 @@ func TestVolume(t *testing.T) {
126126
client: &testdata.MockClient{},
127127
},
128128
wantErr: false,
129-
wantOut: "---\nitems:\n- metadata:\n creationTimestamp: null\n labels:\n acorn.io/app-name: found\n acorn.io/volume-name: vol\n name: found.vol\n spec: {}\n status:\n appName: found\n appPublicName: found\n columns: {}\n volumeName: vol\n\n",
129+
wantOut: "---\nitems:\n- metadata:\n creationTimestamp: null\n name: found.vol\n spec: {}\n status:\n appName: found\n appPublicName: found\n columns: {}\n volumeName: vol\n\n",
130130
},
131131
{
132132
name: "acorn volume found.vol", fields: fields{

0 commit comments

Comments
 (0)