-
Notifications
You must be signed in to change notification settings - Fork 43
/
secret.go
182 lines (168 loc) · 4.6 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
package models
import (
"fmt"
"reflect"
"time"
specV1 "github.com/baetyl/baetyl-go/v2/spec/v1"
"github.com/jinzhu/copier"
)
type SecretList struct {
Total int `json:"total"`
*ListOptions `json:",inline"`
Items []specV1.Secret `json:"items"`
}
func FromSecretToRegistry(s *specV1.Secret, needToFilter bool) *Registry {
if v, ok := s.Labels[specV1.SecretLabel]; needToFilter && (!ok || v != specV1.SecretRegistry) {
return nil
}
res := &Registry{}
err := copier.Copy(res, s)
if err != nil {
panic(fmt.Sprintf("copier exception: %s", err.Error()))
}
if v, ok := s.Data["address"]; ok {
res.Address = string(v)
}
if v, ok := s.Data["password"]; ok {
res.Password = string(v)
}
if v, ok := s.Data["username"]; ok {
res.Username = string(v)
}
return res
}
func FromSecretListToRegistryList(s *SecretList, needToFilter bool) *RegistryList {
res := &RegistryList{
Total: s.Total,
ListOptions: s.ListOptions,
Items: []Registry{},
}
for _, sd := range s.Items {
r := FromSecretToRegistry(&sd, needToFilter)
if r == nil {
res.Total--
} else {
res.Items = append(res.Items, *r)
}
}
return res
}
func FromSecretToCertificate(s *specV1.Secret, needToFilter bool) *Certificate {
// SecretCustomCertificate is deprecated, using "baetyl-cloud-system: true" to distinguish whether the secret is system's secret
// For compatibility, still keep SecretCustomCertificate here
if v, ok := s.Labels[specV1.SecretLabel]; needToFilter && (!ok || (v != specV1.SecretCertificate && v != specV1.SecretCustomCertificate)) {
return nil
}
res := &Certificate{}
err := copier.Copy(res, s)
if err != nil {
panic(fmt.Sprintf("copier exception: %s", err.Error()))
}
if v, ok := s.Data["key"]; ok {
res.Data.Key = string(v)
}
if v, ok := s.Data["certificate"]; ok {
res.Data.Certificate = string(v)
}
if v, ok := s.Data["signatureAlgorithm"]; ok {
res.SignatureAlgorithm = string(v)
}
if v, ok := s.Data["effectiveTime"]; ok {
res.EffectiveTime = string(v)
}
if v, ok := s.Data["expiredTime"]; ok {
res.ExpiredTime = string(v)
}
if v, ok := s.Data["serialNumber"]; ok {
res.SerialNumber = string(v)
}
if v, ok := s.Data["issuer"]; ok {
res.Issuer = string(v)
}
if v, ok := s.Data["fingerPrint"]; ok {
res.FingerPrint = string(v)
}
return res
}
func FromSecretListToCertificateList(s *SecretList, needToFilter bool) *CertificateList {
res := &CertificateList{
Total: s.Total,
ListOptions: s.ListOptions,
Items: []Certificate{},
}
for _, sd := range s.Items {
r := FromSecretToCertificate(&sd, needToFilter)
if r == nil {
res.Total--
} else {
res.Items = append(res.Items, *r)
}
}
return res
}
type SecretView struct {
Name string `json:"name,omitempty" validate:"omitempty,resourceName"`
Namespace string `json:"namespace,omitempty"`
Data map[string]string `json:"data,omitempty" binding:"required"`
CreationTimestamp time.Time `json:"createTime,omitempty"`
UpdateTimestamp time.Time `json:"updateTime,omitempty"`
Description string `json:"description"`
Version string `json:"version,omitempty"`
}
func (s *SecretView) Equal(target *SecretView) bool {
return reflect.DeepEqual(s.Data, target.Data) &&
reflect.DeepEqual(s.Description, target.Description)
}
type SecretViewList struct {
Total int `json:"total"`
*ListOptions `json:",inline"`
Items []SecretView `json:"items"`
}
func (s *SecretView) ToSecret() *specV1.Secret {
res := &specV1.Secret{
Labels: map[string]string{
specV1.SecretLabel: specV1.SecretConfig,
},
}
err := copier.Copy(res, s)
if err != nil {
panic(fmt.Sprintf("copier exception: %s", err.Error()))
}
res.Data = map[string][]byte{}
for k, v := range s.Data {
res.Data[k] = []byte(v)
}
return res
}
func FromSecretToView(s *specV1.Secret, needToFilter bool) *SecretView {
if v, ok := s.Labels[specV1.SecretLabel]; needToFilter && (!ok || v != specV1.SecretConfig) {
return nil
}
res := &SecretView{}
err := copier.Copy(res, s)
if err != nil {
panic(fmt.Sprintf("copier exception: %s", err.Error()))
}
res.Data = map[string]string{}
for k, v := range s.Data {
res.Data[k] = string(v)
}
return res
}
func FromSecretListToView(s *SecretList, needToFilter bool) *SecretViewList {
res := &SecretViewList{
Total: s.Total,
ListOptions: s.ListOptions,
Items: []SecretView{},
}
for _, sd := range s.Items {
r := FromSecretToView(&sd, needToFilter)
if r == nil {
res.Total--
} else {
r.Data = nil
res.Items = append(res.Items, *r)
}
}
return res
}