-
Notifications
You must be signed in to change notification settings - Fork 157
/
file_provisioner.go
181 lines (155 loc) · 5.2 KB
/
file_provisioner.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
package provision
import (
"bytes"
"context"
"crypto/rand"
"fmt"
"path/filepath"
"text/template"
"github.com/1Password/shell-plugins/sdk"
)
// FileProvisioner provisions one or more secrets as a temporary file.
type FileProvisioner struct {
sdk.Provisioner
fileContents ItemToFileContents
outfileName string
outpathFixed string
outpathEnvVar string
outdirEnvVar string
setOutpathAsArg bool
outpathArgTemplates []string
}
type ItemToFileContents func(in sdk.ProvisionInput) ([]byte, error)
// FieldAsFile can be used to store the value of a single field as a file.
func FieldAsFile(fieldName sdk.FieldName) ItemToFileContents {
return ItemToFileContents(func(in sdk.ProvisionInput) ([]byte, error) {
if value, ok := in.ItemFields[fieldName]; ok {
return []byte(value), nil
} else {
return nil, fmt.Errorf("no value present in the item for field '%s'", fieldName)
}
})
}
// TempFile returns a file provisioner and takes a function that maps a 1Password item to the contents of
// a single file.
func TempFile(fileContents ItemToFileContents, opts ...FileOption) sdk.Provisioner {
p := FileProvisioner{
fileContents: fileContents,
}
for _, opt := range opts {
opt(&p)
}
return p
}
// FileOption can be used to influence the behavior of the file provisioner.
type FileOption func(*FileProvisioner)
// AtFixedPath can be used to tell the file provisioner to store the credential at a specific location, instead of
// an autogenerated temp dir. This is useful for executables that can only load credentials from a specific path.
func AtFixedPath(path string) FileOption {
return func(p *FileProvisioner) {
p.outpathFixed = path
}
}
// Filename can be used to tell the file provisioner to store the credential with a specific name, instead of
// an autogenerated name. The specified filename will be appended to the path of the autogenerated temp dir.
// Gets ignored if the provision.AtFixedPath option is also set.
func Filename(name string) FileOption {
return func(p *FileProvisioner) {
p.outfileName = name
}
}
// SetPathAsEnvVar can be used to provision the temporary file path as an environment variable.
func SetPathAsEnvVar(envVarName string) FileOption {
return func(p *FileProvisioner) {
p.outpathEnvVar = envVarName
}
}
// SetOutputDirAsEnvVar can be used to provision the directory of the output file as an environment variable.
func SetOutputDirAsEnvVar(envVarName string) FileOption {
return func(p *FileProvisioner) {
p.outdirEnvVar = envVarName
}
}
// AddArgs can be used to add args to the command line. This is useful when the output file path
// should be passed as an arg. The output path is available as "{{ .Path }}" in each arg.
// For example:
// * `AddArgs("--config-file", "{{ .Path }}")` will result in `--config-file /path/to/tempfile`.
// * `AddArgs("--config-file={{ .Path }}")` will result in `--config-file=/path/to/tempfile`.
func AddArgs(argTemplates ...string) FileOption {
return func(p *FileProvisioner) {
p.setOutpathAsArg = true
p.outpathArgTemplates = argTemplates
}
}
func (p FileProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, out *sdk.ProvisionOutput) {
contents, err := p.fileContents(in)
if err != nil {
out.AddError(err)
return
}
outpath := ""
if p.outpathFixed != "" {
// Default to the provision.AtFixedPath option
outpath = p.outpathFixed
} else if p.outfileName != "" {
// Fall back to the provision.Filename option
outpath = in.FromTempDir(p.outfileName)
} else {
// If both are undefined, resort to generating a random filename
fileName, err := randomFilename()
if err != nil {
// This should only fail in rare circumstances
out.AddError(fmt.Errorf("generating random file name: %s", err))
return
}
outpath = in.FromTempDir(fileName)
}
out.AddSecretFile(outpath, contents)
if p.outpathEnvVar != "" {
// Populate the specified environment variable with the output path.
out.AddEnvVar(p.outpathEnvVar, outpath)
}
if p.outdirEnvVar != "" {
// Populate the specified environment variable with the output dir.
dir := filepath.Dir(outpath)
out.AddEnvVar(p.outpathEnvVar, dir)
}
// Add args to specify the output path.
if p.setOutpathAsArg {
tmplData := struct{ Path string }{
Path: outpath,
}
// Resolve arg templates with the resulting output path injected.
// Example: "--config-file={{ .Path }}" => "--config-file=/tmp/file"
argsResolved := make([]string, len(p.outpathArgTemplates))
for i, tmplStr := range p.outpathArgTemplates {
tmpl, err := template.New("arg").Parse(tmplStr)
if err != nil {
out.AddError(err)
return
}
var result bytes.Buffer
err = tmpl.Execute(&result, tmplData)
if err != nil {
out.AddError(err)
return
}
argsResolved[i] = result.String()
}
out.AddArgs(argsResolved...)
}
}
func (p FileProvisioner) Deprovision(ctx context.Context, in sdk.DeprovisionInput, out *sdk.DeprovisionOutput) {
// Nothing to do here: deleting the files gets taken care of.
}
func (p FileProvisioner) Description() string {
return "Provision secret file"
}
func randomFilename() (string, error) {
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", b), nil
}