-
Notifications
You must be signed in to change notification settings - Fork 124
/
secret.go
209 lines (169 loc) · 4.35 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//
// Last.Backend LLC CONFIDENTIAL
// __________________
//
// [2014] - [2018] Last.Backend LLC
// All Rights Reserved.
//
// NOTICE: All information contained herein is, and remains
// the property of Last.Backend LLC and its suppliers,
// if any. The intellectual and technical concepts contained
// herein are proprietary to Last.Backend LLC
// and its suppliers and may be covered by Russian Federation and Foreign Patents,
// patents in process, and are protected by trade secret or copyright law.
// Dissemination of this information or reproduction of this material
// is strictly forbidden unless prior written permission is obtained
// from Last.Backend LLC.
//
package types
import (
"crypto/sha1"
"encoding/base64"
"errors"
"fmt"
"time"
)
const (
KindSecretOpaque = "opaque"
KindSecretAuth = "auth"
SecretUsernameKey = "username"
SecretPasswordKey = "password"
)
// swagger:ignore
// swagger:model types_secret
type Secret struct {
Runtime
Meta SecretMeta `json:"meta" yaml:"meta"`
Spec SecretSpec `json:"spec" yaml:"yaml"`
}
// swagger:ignore
type SecretList struct {
Runtime
Items []*Secret
}
// swagger:ignore
type SecretMap struct {
Runtime
Items map[string]*Secret
}
// swagger:ignore
// swagger:model types_secret_meta
type SecretMeta struct {
Meta `yaml:",inline"`
Namespace string `json:"namespace" yaml:"namespace"`
}
type SecretSpec struct {
Type string `json:"type"`
Data map[string][]byte `json:"data" yaml:"data"`
}
type SecretManifest struct {
Runtime
State string `json:"state"`
Type string `json:"type"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
}
type SecretManifestList struct {
Runtime
Items []*SecretManifest
}
type SecretManifestMap struct {
Runtime
Items map[string]*SecretManifest
}
func NewSecretManifestList() *SecretManifestList {
dm := new(SecretManifestList)
dm.Items = make([]*SecretManifest, 0)
return dm
}
func NewSecretManifestMap() *SecretManifestMap {
dm := new(SecretManifestMap)
dm.Items = make(map[string]*SecretManifest)
return dm
}
func (s *Secret) EncodeSecretAuthData(d SecretAuthData) {
s.Spec.Data = make(map[string][]byte)
s.Spec.Data["username"] = []byte(base64.StdEncoding.EncodeToString([]byte(d.Username)))
s.Spec.Data["password"] = []byte(base64.StdEncoding.EncodeToString([]byte(d.Password)))
}
func (s *Secret) DecodeSecretAuthData() (*SecretAuthData, error) {
if s.Spec.Type != KindSecretAuth {
return nil, errors.New("invalid secret type")
}
data := new(SecretAuthData)
u, err := base64.StdEncoding.DecodeString(string(s.Spec.Data["username"]))
if err != nil {
return nil, err
}
data.Username = string(u)
p, err := base64.StdEncoding.DecodeString(string(s.Spec.Data["password"]))
if err != nil {
return nil, err
}
data.Password = string(p)
return data, nil
}
func (s *Secret) DecodeSecretTextData(key string) (string, error) {
if s.Spec.Type != KindSecretOpaque {
return EmptyString, errors.New("invalid secret type")
}
if _, ok := s.Spec.Data[key]; !ok {
return EmptyString, errors.New("secret key not found")
}
d, err := base64.StdEncoding.DecodeString(string(s.Spec.Data[key]))
if err != nil {
return EmptyString, err
}
return string(d), nil
}
type SecretAuthData struct {
Username string `json:"username"`
Password string `json:"password"`
}
type SecretText struct {
Text string `json:"text"`
}
type SecretFile struct {
Files map[string][]byte `json:"text"`
}
func (s *Secret) GetHash() string {
h := sha1.New()
h.Write([]byte(fmt.Sprintf("%s", s.Meta.Name)))
return base64.URLEncoding.EncodeToString(h.Sum(nil))
}
func (s *Secret) SelfLink() string {
if s.Meta.SelfLink == "" {
s.Meta.SelfLink = s.CreateSelfLink(s.Meta.Namespace, s.Meta.Name)
}
return s.Meta.SelfLink
}
func (s *Secret) CreateSelfLink(namespace, name string) string {
return fmt.Sprintf("%s:%s", namespace, name)
}
func (s *Secret) DecodeRegistry() {
}
// swagger:ignore
type SecretCreateOptions struct {
Name string
Kind string
Data map[string][]byte
}
// swagger:ignore
type SecretUpdateOptions struct {
Kind string
Data map[string][]byte
}
// swagger:ignore
type SecretRemoveOptions struct {
Force bool `json:"force"`
}
func NewSecretList() *SecretList {
dm := new(SecretList)
dm.Items = make([]*Secret, 0)
return dm
}
func NewSecretMap() *SecretMap {
dm := new(SecretMap)
dm.Items = make(map[string]*Secret)
return dm
}