forked from UKHomeOffice/vault-sidekick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault_resources.go
163 lines (150 loc) · 4.85 KB
/
vault_resources.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
/*
Copyright 2015 Home Office All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"
)
// VaultResources is a collection of type resource
type VaultResources struct {
// an array of resource to retrieve
items []*VaultResource
}
// Set is the implementation for the parser
// secret:test:file=filename.test,fmt=yaml
func (r *VaultResources) Set(value string) error {
rn := defaultVaultResource()
// step: split on the separator, default ':'
sep := getEnv("VAULT_SIDEKICK_SEPARATOR", ":")
items := strings.Split(os.ExpandEnv(value), sep)
if len(items) < 2 {
return fmt.Errorf("invalid resource, must have at least two sections TYPE:PATH")
}
if len(items) > 3 {
return fmt.Errorf("invalid resource, can only has three sections, TYPE:PATH[:OPTIONS]")
}
if items[0] == "" || items[1] == "" {
return fmt.Errorf("invalid resource, neither type or path can be empty")
}
// step: extract the elements
rn.resource = items[0]
rn.path = items[1]
rn.options = make(map[string]string, 0)
// step: extract any options
if len(items) > 2 {
for _, x := range strings.Split(items[2], ",") {
kp := strings.Split(x, "=")
if len(kp) != 2 {
return fmt.Errorf("invalid resource option: %s, must be KEY=VALUE", x)
}
if kp[1] == "" {
return fmt.Errorf("invalid resource option: %s, must have a value", x)
}
// step: set the name and value
name := strings.TrimSpace(kp[0])
value := strings.Replace(kp[1], "|", ",", -1)
// step: extract the control options from the path resource parameters
switch name {
case optionMode:
if !strings.HasPrefix(value, "0") {
value = "0" + value
}
if len(value) != 4 {
return errors.New("the file permission invalid, should be octal 0444 or alike")
}
v, err := strconv.ParseUint(value, 0, 32)
if err != nil {
return errors.New("invalid file permissions on resource")
}
rn.fileMode = os.FileMode(v)
case optionFormat:
if matched := resourceFormatRegex.MatchString(value); !matched {
return fmt.Errorf("unsupported output format: %s", value)
}
rn.format = value
case optionUpdate:
duration, err := time.ParseDuration(value)
if err != nil {
return fmt.Errorf("update option: %s is not value, should be a duration format", value)
}
rn.update = duration
case optionRevoke:
choice, err := strconv.ParseBool(value)
if err != nil {
return fmt.Errorf("the revoke option: %s is invalid, should be a boolean", value)
}
rn.revoked = choice
case optionsRevokeDelay:
duration, err := time.ParseDuration(value)
if err != nil {
return fmt.Errorf("the revoke delay option: %s is not value, should be a duration format", value)
}
rn.revokeDelay = duration
case optionRenewal:
choice, err := strconv.ParseBool(value)
if err != nil {
return fmt.Errorf("the renewal option: %s is invalid, should be a boolean", value)
}
rn.renewable = choice
case optionCreate:
choice, err := strconv.ParseBool(value)
if err != nil {
return fmt.Errorf("the create option: %s is invalid, should be a boolean", value)
}
if rn.resource != "secret" {
return fmt.Errorf("the create option is only supported for 'cn=secret' at this time")
}
rn.create = choice
case optionSize:
size, err := strconv.ParseInt(value, 10, 16)
if err != nil {
return fmt.Errorf("the size option: %s is invalid, should be an integer", value)
}
rn.size = size
case optionExec:
rn.execPath = value
case optionFilename:
rn.filename = value
case optionTemplatePath:
rn.templateFile = value
case optionMaxRetries:
maxRetries, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return fmt.Errorf("the retries option: %s is invalid, should be an integer", value)
}
rn.maxRetries = int(maxRetries)
case optionMaxJitter:
maxJitter, err := time.ParseDuration(value)
if err != nil {
return fmt.Errorf("the jitter option: %s is invalid, should be in duration format", value)
}
rn.maxJitter = maxJitter
case optionTtl:
rn.options["ttl"] = value
default:
rn.options[name] = value
}
}
}
// step: append to the list of resources
r.items = append(r.items, rn)
return nil
}
// String returns a string representation of the struct
func (r VaultResources) String() string {
return ""
}