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

Commit

Permalink
fix: cleanup acorn secrets reveal and general non-table output (#1812
Browse files Browse the repository at this point in the history
…) (#1833)
  • Loading branch information
iwilltry42 authored Jun 24, 2023
1 parent 330e88e commit 7195323
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@
"description": "The image of Acorn to install."
},
]
}
}
34 changes: 34 additions & 0 deletions pkg/cli/builder/table/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ func toKObject(obj any) (kclient.Object, bool) {
}

func cleanFields(obj any) any {
if ol, ok := obj.(objectList); ok {
for i, o := range ol.Items {
ol.Items[i] = cleanFields(o)
}
return ol
}

ro, ok := toKObject(obj)
if ok {
ro.SetManagedFields(nil)
Expand All @@ -147,11 +154,38 @@ func cleanFields(obj any) any {
}
}
ro.SetAnnotations(annotations)

// decode secret values
if sec, ok := ro.(*apiv1.Secret); ok {
return decodeSecret(sec)
}

return ro
}
return obj
}

func decodeSecret(sec *apiv1.Secret) any {
decodedSecret := struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`

Type string `json:"type,omitempty"`
Data map[string]string `json:"data,omitempty"`
Keys []string `json:"keys,omitempty"`
}{
TypeMeta: sec.TypeMeta,
ObjectMeta: sec.ObjectMeta,
Type: sec.Type,
Data: map[string]string{},
Keys: sec.Keys,
}
for k, v := range sec.Data {
decodedSecret.Data[k] = string(v)
}
return decodedSecret
}

func FormatYAML(data any) (string, error) {
bytes, err := yaml.Marshal(cleanFields(data))
return string(bytes) + "\n", err
Expand Down
8 changes: 5 additions & 3 deletions pkg/cli/builder/table/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ func (t *writer) writeDataObject(objs ...any) error {
return errors.Join(t.errs...)
}

type objectList struct {
Items []any `json:"items"`
}

func (t *writer) flush(closing bool) error {
if len(t.errs) > 0 {
return errors.Join(t.errs...)
Expand Down Expand Up @@ -262,9 +266,7 @@ func (t *writer) flush(closing bool) error {
if closing {
// if we are closing, then we want to print objects its in a proper list
// data structure
err := t.writeDataObject(map[string]any{
"items": objs,
})
err := t.writeDataObject(objectList{Items: objs})
if err != nil {
return err
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/cli/secret_expose.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func (a *Reveal) Run(cmd *cobra.Command, args []string) error {
Key: entry.Key,
Value: string(entry.Value),
}, &secret)
if a.Output != "" {
// in non-table output, we write the source object to buffer,
// so we exit here to not write the same object multiple times (one per data key)
break
}
}
}

Expand Down
57 changes: 57 additions & 0 deletions pkg/cli/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,63 @@ func TestSecret(t *testing.T) {
wantErr: false,
wantOut: "NAME TYPE KEY VALUE\n",
},
{
name: "acorn secret reveal secret.withdata", fields: fields{
All: false,
Quiet: false,
Output: "",
},
commandContext: CommandContext{
ClientFactory: &testdata.MockClientFactory{},
StdOut: w,
StdErr: w,
StdIn: strings.NewReader("y\n"),
},
args: args{
args: []string{"reveal", "secret.withdata"},
client: &testdata.MockClient{},
},
wantErr: false,
wantOut: "NAME TYPE KEY VALUE\nsecret.withdata baz qux\nsecret.withdata foo bar\n",
},
{
name: "acorn secret reveal secret.withdata -o jsoncompact", fields: fields{
All: false,
Quiet: false,
Output: "",
},
commandContext: CommandContext{
ClientFactory: &testdata.MockClientFactory{},
StdOut: w,
StdErr: w,
StdIn: strings.NewReader("y\n"),
},
args: args{
args: []string{"reveal", "secret.withdata", "-o=jsoncompact"},
client: &testdata.MockClient{},
},
wantErr: false,
wantOut: "{\"items\":[{\"metadata\":{\"name\":\"secret.withdata\",\"creationTimestamp\":null},\"data\":{\"baz\":\"qux\",\"foo\":\"bar\"}}]}\n",
},
{
name: "acorn secret reveal secret.withdata secret.withdata2 -o jsoncompact", fields: fields{
All: false,
Quiet: false,
Output: "",
},
commandContext: CommandContext{
ClientFactory: &testdata.MockClientFactory{},
StdOut: w,
StdErr: w,
StdIn: strings.NewReader("y\n"),
},
args: args{
args: []string{"reveal", "secret.withdata", "secret.withdata2", "-o=jsoncompact"},
client: &testdata.MockClient{},
},
wantErr: false,
wantOut: "{\"items\":[{\"metadata\":{\"name\":\"secret.withdata\",\"creationTimestamp\":null},\"data\":{\"baz\":\"qux\",\"foo\":\"bar\"}},{\"metadata\":{\"name\":\"secret.withdata2\",\"creationTimestamp\":null},\"data\":{\"spam\":\"eggs\"}}]}\n",
},
{
name: "acorn secret reveal dne", fields: fields{
All: false,
Expand Down
20 changes: 20 additions & 0 deletions pkg/cli/testdata/MockClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,26 @@ func (m *MockClient) SecretReveal(ctx context.Context, name string) (*apiv1.Secr
Data: nil,
Keys: nil,
}, nil
case "secret.withdata":
return &apiv1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "secret.withdata",
},
Data: map[string][]byte{
"foo": []byte("bar"),
"baz": []byte("qux"),
},
}, nil
case "secret.withdata2":
return &apiv1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "secret.withdata2",
},
Data: map[string][]byte{
"spam": []byte("eggs"),
},
}, nil

}
return nil, nil
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/cli/testdata/all/all_test_json.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ VOLUMES:
{
"metadata": {
"name": "found.vol",
"creationTimestamp": null,
"labels": {
"acorn.io/app-name": "found",
"acorn.io/volume-name": "vol"
}
"creationTimestamp": null
},
"spec": {},
"status": {
Expand Down
3 changes: 0 additions & 3 deletions pkg/cli/testdata/all/all_test_yaml.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ VOLUMES:
items:
- metadata:
creationTimestamp: null
labels:
acorn.io/app-name: found
acorn.io/volume-name: vol
name: found.vol
spec: {}
status:
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/volumes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestVolume(t *testing.T) {
client: &testdata.MockClient{},
},
wantErr: false,
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",
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",
},
{
name: "acorn volume -o yaml", fields: fields{
Expand All @@ -126,7 +126,7 @@ func TestVolume(t *testing.T) {
client: &testdata.MockClient{},
},
wantErr: false,
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",
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",
},
{
name: "acorn volume found.vol", fields: fields{
Expand Down

0 comments on commit 7195323

Please sign in to comment.