forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new.go
296 lines (246 loc) · 8.1 KB
/
new.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package secrets
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strings"
kapi "k8s.io/kubernetes/pkg/api"
kvalidation "k8s.io/kubernetes/pkg/api/validation"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
"github.com/spf13/cobra"
)
const (
NewSecretRecommendedCommandName = "new"
newLong = `
Create a new secret based on a file or directory
Key files can be specified using their file path, in which case a default name will be given to them, or optionally
with a name and file path, in which case the given name will be used. Specifying a directory will create a secret
using with all valid keys in that directory.
`
newExample = ` # Create a new secret named my-secret with a key named ssh-privatekey
$ %[1]s my-secret ~/.ssh/ssh-privatekey
# Create a new secret named my-secret with keys named ssh-privatekey and ssh-publickey instead of the names of the keys on disk
$ %[1]s my-secret ssh-privatekey=~/.ssh/id_rsa ssh-publickey=~/.ssh/id_rsa.pub
# Create a new secret named my-secret with keys for each file in the folder "bar"
$ %[1]s my-secret path/to/bar
# Create a new .dockercfg secret named my-secret
$ %[1]s my-secret path/to/.dockercfg
# Create a new .docker/config.json secret named my-secret
$ %[1]s my-secret .dockerconfigjson=path/to/.docker/config.json`
)
type CreateSecretOptions struct {
// Name of the resulting secret
Name string
// SecretTypeName is the type to use when creating the secret. It is checked against known types.
SecretTypeName string
// Files/Directories to read from.
// Directory sources are listed and any direct file children included (but subfolders are not traversed)
Sources []string
SecretsInterface kclient.SecretsInterface
// Writer to write warnings to
Stderr io.Writer
Out io.Writer
// Controls whether to output warnings
Quiet bool
AllowUnknownTypes bool
}
func NewCmdCreateSecret(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := NewCreateSecretOptions()
options.Out = out
cmd := &cobra.Command{
Use: fmt.Sprintf("%s NAME [KEY=]SOURCE ...", name),
Short: "Create a new secret based on a key file or on files within a directory",
Long: newLong,
Example: fmt.Sprintf(newExample, fullName),
Run: func(c *cobra.Command, args []string) {
if err := options.Complete(args, f); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(c, err.Error()))
}
if err := options.Validate(); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(c, err.Error()))
}
if len(kcmdutil.GetFlagString(c, "output")) != 0 {
secret, err := options.BundleSecret()
kcmdutil.CheckErr(err)
kcmdutil.CheckErr(f.Factory.PrintObject(c, secret, out))
return
}
_, err := options.CreateSecret()
kcmdutil.CheckErr(err)
},
}
cmd.Flags().BoolVarP(&options.Quiet, "quiet", "q", options.Quiet, "Suppress warnings")
cmd.Flags().BoolVar(&options.AllowUnknownTypes, "confirm", options.AllowUnknownTypes, "Allow unknown secret types.")
cmd.Flags().StringVar(&options.SecretTypeName, "type", "", "The type of secret")
kcmdutil.AddPrinterFlags(cmd)
return cmd
}
func NewCreateSecretOptions() *CreateSecretOptions {
return &CreateSecretOptions{
Stderr: os.Stderr,
Sources: []string{},
}
}
func (o *CreateSecretOptions) Complete(args []string, f *clientcmd.Factory) error {
// Fill name from args[0]
if len(args) > 0 {
o.Name = args[0]
}
// Add sources from args[1:...] in addition to -f
if len(args) > 1 {
o.Sources = append(o.Sources, args[1:]...)
}
if f != nil {
_, kubeClient, err := f.Clients()
if err != nil {
return err
}
namespace, _, err := f.Factory.DefaultNamespace()
if err != nil {
return err
}
o.SecretsInterface = kubeClient.Secrets(namespace)
}
return nil
}
func (o *CreateSecretOptions) Validate() error {
if len(o.Name) == 0 {
return errors.New("secret name is required")
}
if len(o.Sources) == 0 {
return errors.New("at least one source file or directory must be specified")
}
if !o.AllowUnknownTypes {
switch o.SecretTypeName {
case string(kapi.SecretTypeOpaque), "":
// this is ok
default:
found := false
for _, secretType := range KnownSecretTypes {
if o.SecretTypeName == string(secretType.Type) {
found = true
break
}
}
if !found {
return fmt.Errorf("unknown secret type %q; use --confirm to use it anyway", o.SecretTypeName)
}
}
}
return nil
}
func (o *CreateSecretOptions) CreateSecret() (*kapi.Secret, error) {
secret, err := o.BundleSecret()
if err != nil {
return nil, err
}
persistedSecret, err := o.SecretsInterface.Create(secret)
if err == nil {
fmt.Fprintf(o.Out, "secret/%s\n", persistedSecret.Name)
}
return persistedSecret, err
}
func (o *CreateSecretOptions) BundleSecret() (*kapi.Secret, error) {
secretData := make(map[string][]byte)
for _, source := range o.Sources {
keyName, filePath, err := parseSource(source)
if err != nil {
return nil, err
}
info, err := os.Stat(filePath)
if err != nil {
switch err := err.(type) {
case *os.PathError:
return nil, fmt.Errorf("error reading %s: %v", filePath, err.Err)
default:
return nil, fmt.Errorf("error reading %s: %v", filePath, err)
}
}
if info.IsDir() {
if strings.Contains(source, "=") {
return nil, errors.New("Cannot give a key name for a directory path.")
}
fileList, err := ioutil.ReadDir(filePath)
if err != nil {
return nil, fmt.Errorf("error listing files in %s: %v", filePath, err)
}
for _, item := range fileList {
itemPath := path.Join(filePath, item.Name())
if !item.Mode().IsRegular() {
if o.Stderr != nil && o.Quiet != true {
fmt.Fprintf(o.Stderr, "Skipping resource %s\n", itemPath)
}
} else {
keyName = item.Name()
err = addKeyToSecret(keyName, itemPath, secretData)
if err != nil {
return nil, err
}
}
}
} else {
err = addKeyToSecret(keyName, filePath, secretData)
if err != nil {
return nil, err
}
}
}
if len(secretData) == 0 {
return nil, errors.New("No files selected")
}
// if the secret type isn't specified, attempt to auto-detect likely hit
secretType := kapi.SecretType(o.SecretTypeName)
if len(o.SecretTypeName) == 0 {
secretType = kapi.SecretTypeOpaque
for _, knownSecretType := range KnownSecretTypes {
if knownSecretType.Matches(secretData) {
secretType = knownSecretType.Type
}
}
}
secret := &kapi.Secret{
ObjectMeta: kapi.ObjectMeta{Name: o.Name},
Type: secretType,
Data: secretData,
}
return secret, nil
}
func addKeyToSecret(keyName, filePath string, secretData map[string][]byte) error {
if !kvalidation.IsSecretKey(keyName) {
return fmt.Errorf("%v is not a valid key name for a secret", keyName)
}
if _, entryExists := secretData[keyName]; entryExists {
return fmt.Errorf("cannot add key %s from path %s, another key by that name already exists: %v.", keyName, filePath, secretData)
}
data, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
secretData[keyName] = data
return nil
}
// parseSource parses the source given. Acceptable formats include:
// source-name=source-path, where source-name will become the key name and source-path is the path to the key file
// source-path, where source-path is a path to a file or directory, and key names will default to file names
// Key names cannot include '='.
func parseSource(source string) (keyName, filePath string, err error) {
numSeparators := strings.Count(source, "=")
switch {
case numSeparators == 0:
return path.Base(source), source, nil
case numSeparators == 1 && strings.HasPrefix(source, "="):
return "", "", fmt.Errorf("key name for file path %v missing.", strings.TrimPrefix(source, "="))
case numSeparators == 1 && strings.HasSuffix(source, "="):
return "", "", fmt.Errorf("file path for key name %v missing.", strings.TrimSuffix(source, "="))
case numSeparators > 1:
return "", "", errors.New("Key names or file paths cannot contain '='.")
default:
components := strings.Split(source, "=")
return components[0], components[1], nil
}
}