generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 11
/
types.go
239 lines (204 loc) · 5.87 KB
/
types.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
** OCI Secrets Store CSI Driver Provider
**
** Copyright (c) 2022 Oracle America, Inc. and its affiliates.
** Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
*/
package types
import (
"encoding/base64"
"fmt"
"strconv"
"strings"
"gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/util/validation/field"
)
// SecretBundleRequest represents request for a single secret bundle.
// Bundle is identified by Name and either Stage or VersionNumber.
type SecretBundleRequest struct {
Name string `yaml:"name"`
Stage Stage `yaml:"stage,omitempty"`
VersionNumber VersionNumber `yaml:"versionNumber,omitempty"`
FileName string `yaml:"fileName,omitempty"`
}
// String returns string representation of SecretBundleRequest.
// Method is useful for secret bundle requests logging.
func (request *SecretBundleRequest) String() string {
return fmt.Sprintf("{name=%v, version=%v, stage=%v}",
request.Name, request.VersionNumber, request.Stage.String())
}
func (request *SecretBundleRequest) GetFilePath() string {
return determineFileName(request.Name, request.FileName)
}
func (request *SecretBundle) GetFilePath() string {
return determineFileName(request.Name, request.FileName)
}
func determineFileName(name string, alias string) string {
var fileName = strings.TrimSpace(name)
if fileAlias := strings.TrimSpace(alias); len(fileAlias) > 0 {
fileName = fileAlias
}
return fileName
}
type VersionNumber int64
// UnmarshalYAML customizes unmarshaling of YAML document into VersionNumber
func (versionNumber *VersionNumber) UnmarshalYAML(node *yaml.Node) error {
if node.Value == "" {
// zero should be treated like empty value
*versionNumber = 0
return nil
}
intValue, err := strconv.ParseInt(node.Value, 10, 64)
if err != nil {
return err
}
if intValue <= 0 {
return fmt.Errorf("version number should be positive")
}
*versionNumber = VersionNumber(intValue)
return nil
}
// Stage represents secret's stage.
type Stage int
const (
None Stage = iota // None means that stage is not defined
Current
Pending
Latest
Previous
Deprecated
)
var stageMapping = map[Stage]string{
Current: "CURRENT",
Pending: "PENDING",
Latest: "LATEST",
Previous: "PREVIOUS",
Deprecated: "DEPRECATED",
}
// String returns string representation of ContentType
func (stage *Stage) String() string {
if *stage == None {
return ""
}
return stageMapping[*stage]
}
func (stage *Stage) FromString(value string) error {
if value == "" {
*stage = None
return nil
}
for stageValue, stageString := range stageMapping {
if stageString == value {
*stage = stageValue
return nil
}
}
return fmt.Errorf("unknown stage: %v", value)
}
// MarshalYAML customizes marshaling of Stage into a YAML document
func (stage *Stage) MarshalYAML() (interface{}, error) {
return stage.String(), nil
}
// UnmarshalYAML customizes unmarshaling of YAML document into Stage
func (stage *Stage) UnmarshalYAML(node *yaml.Node) error {
return stage.FromString(node.Value)
}
// SecretBundle stores secrets itself and it's details
type SecretBundle struct {
ID string
Name string
VersionNumber int64
FileName string
Stages []Stage
BundleContent *SecretBundleContent
}
// SecretBundleContent stores secrets content
type SecretBundleContent struct {
ContentType ContentType
Content string
}
// Decode decodes secret bundle content to plain text
func (content *SecretBundleContent) Decode() (string, error) {
if content.Content == "" {
return "", fmt.Errorf("missed secret content")
}
if content.ContentType != Base64 {
return "", fmt.Errorf("unknown content type")
}
decodedContent, err := base64.StdEncoding.DecodeString(content.Content)
return string(decodedContent), err
}
// ContentType is encoding type of secret content
type ContentType int
const (
Base64 ContentType = iota
)
// String returns string representation of ContentType
func (contentType *ContentType) String() string {
// OCI Vault supports single content type: Base64
return []string{"BASE64"}[*contentType]
}
type OCIPrincipalType string
const (
Instance OCIPrincipalType = "instance"
User OCIPrincipalType = "user"
)
type VaultID string
func MapToPrincipalType(authType string) (OCIPrincipalType, error) {
switch authType {
case string(Instance):
return Instance, nil
case string(User):
return User, nil
default:
return "", fmt.Errorf("unknown OCI principal type: %v", authType)
}
}
type SecretServiceRequest struct {
VaultID string
Region string
Auth Auth
Secrets []SecretBundleRequest
}
type Auth struct {
Type OCIPrincipalType
Config AuthConfig
}
type AuthConfig struct {
Region string `yaml:"region"`
TenancyID string `yaml:"tenancy"`
UserID string `yaml:"user"`
PrivateKey string `yaml:"privateKey"`
Fingerprint string `yaml:"fingerprint"`
Passphrase string `yaml:"passphrase"`
}
type AuthConfigYaml struct {
Auth map[string]string `yaml:"auth,omitempty"`
}
func (config *AuthConfig) Validate() error {
return validateConfig(config).ToAggregate()
}
func validateConfig(c *AuthConfig) field.ErrorList {
errs := field.ErrorList{}
if len(c.TenancyID) == 0 {
errs = append(errs, field.Required(field.NewPath("Auth", "Tenancy"),
"Tenancy is required for user principal"))
}
if len(c.Region) == 0 {
errs = append(errs, field.Required(field.NewPath("Auth", "Region"),
"Region is required for user principal"))
}
if len(c.Fingerprint) == 0 {
errs = append(errs, field.Required(field.NewPath("Auth", "Fingerprint"),
"Fingerprint is required for user principal"))
}
if len(c.UserID) == 0 {
errs = append(errs, field.Required(field.NewPath("Auth", "UserID"),
"UserID is required for user principal"))
}
if len(c.PrivateKey) == 0 {
errs = append(errs, field.Required(field.NewPath("Auth", "PrivateKey"),
"PrivateKey is required for user principal"))
}
return errs
}