This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
/
secret.go
185 lines (148 loc) · 4.69 KB
/
secret.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
Copyright (c) 2019 the Octant contributors. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package printer
import (
"context"
"encoding/base64"
"fmt"
"unicode/utf8"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"github.com/vmware-tanzu/octant/pkg/view/component"
)
var (
secretTableCols = component.NewTableCols("Name", "Labels", "Type", "Data", "Age")
secretDataCols = component.NewTableCols("Key")
)
// SecretListHandler is a printFunc that lists secrets.
func SecretListHandler(ctx context.Context, list *corev1.SecretList, options Options) (component.Component, error) {
if list == nil {
return nil, errors.New("list of secrets is nil")
}
ot := NewObjectTable("Secrets", "We couldn't find any secrets!", secretTableCols, options.DashConfig.ObjectStore())
ot.EnablePluginStatus(options.DashConfig.PluginManager())
for _, secret := range list.Items {
row := component.TableRow{}
nameLink, err := options.Link.ForObject(&secret, secret.Name)
if err != nil {
return nil, err
}
row["Name"] = nameLink
row["Labels"] = component.NewLabels(secret.ObjectMeta.Labels)
row["Type"] = component.NewText(string(secret.Type))
row["Data"] = component.NewText(fmt.Sprintf("%d", len(secret.Data)))
row["Age"] = component.NewTimestamp(secret.ObjectMeta.CreationTimestamp.Time)
if err := ot.AddRowForObject(ctx, &secret, row); err != nil {
return nil, fmt.Errorf("add row for object: %w", err)
}
}
return ot.ToComponent()
}
// SecretHandler is a printFunc for printing a secret summary.
func SecretHandler(ctx context.Context, secret *corev1.Secret, options Options) (component.Component, error) {
o := NewObject(secret)
sh, err := newSecretHandler(secret, o)
if err != nil {
return nil, err
}
if err := sh.Config(options); err != nil {
return nil, errors.Wrap(err, "print secret configuration")
}
if err := sh.Data(options); err != nil {
return nil, errors.Wrap(err, "print secret data")
}
return o.ToComponent(ctx, options)
}
// SecretConfiguration generates a secret configuration
type SecretConfiguration struct {
secret *corev1.Secret
}
// NewSecretConfiguration creates an instance of SecretConfiguration
func NewSecretConfiguration(secret *corev1.Secret) *SecretConfiguration {
return &SecretConfiguration{
secret: secret,
}
}
// Create creates a secret configuration summary
func (s *SecretConfiguration) Create(options Options) (*component.Summary, error) {
if s.secret == nil {
return nil, errors.New("secret is nil")
}
secret := s.secret
var sections []component.SummarySection
sections = append(sections, component.SummarySection{
Header: "Type",
Content: component.NewText(string(secret.Type)),
})
summary := component.NewSummary("Configuration", sections...)
return summary, nil
}
func describeSecretData(secret corev1.Secret) (*component.Table, error) {
table := component.NewTable("Data", "This secret has no data!", secretDataCols)
for key, value := range secret.Data {
row := component.TableRow{}
keyText := component.NewText(key)
if utf8.Valid(value) {
keyText.AddClipboard(string(value))
} else {
keyText.AddClipboard(base64.StdEncoding.EncodeToString(value))
}
row["Key"] = keyText
table.Add(row)
}
table.Sort("Key")
return table, nil
}
type secretObject interface {
Config(options Options) error
Data(options Options) error
}
type secretHandler struct {
secret *corev1.Secret
configFunc func(*corev1.Secret, Options) (*component.Summary, error)
dataFunc func(*corev1.Secret, Options) (*component.Table, error)
object *Object
}
func newSecretHandler(secret *corev1.Secret, object *Object) (*secretHandler, error) {
if secret == nil {
return nil, errors.New("can't print a nil secret")
}
if object == nil {
return nil, errors.New("can't print secret using a nil object printer")
}
sh := &secretHandler{
secret: secret,
configFunc: defaultSecretConfig,
dataFunc: defaultSecretData,
object: object,
}
return sh, nil
}
func (s *secretHandler) Config(options Options) error {
out, err := s.configFunc(s.secret, options)
if err != nil {
return err
}
s.object.RegisterConfig(out)
return nil
}
func defaultSecretConfig(secret *corev1.Secret, options Options) (*component.Summary, error) {
return NewSecretConfiguration(secret).Create(options)
}
func (s *secretHandler) Data(options Options) error {
if s.secret == nil {
return errors.New("can't display data for nil secret")
}
s.object.RegisterItems(ItemDescriptor{
Width: component.WidthFull,
Func: func() (component.Component, error) {
return s.dataFunc(s.secret, options)
},
})
return nil
}
func defaultSecretData(secret *corev1.Secret, options Options) (*component.Table, error) {
return describeSecretData(*secret)
}