/
template.go
211 lines (179 loc) · 5.9 KB
/
template.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
// Copyright © 2019 Banzai Cloud
//
// 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 configuration
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"net/url"
"os"
"strings"
"text/template"
cloudkms "cloud.google.com/go/kms/apiv1"
"cloud.google.com/go/kms/apiv1/kmspb"
"emperror.dev/errors"
"github.com/Masterminds/sprig/v3"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kms"
"gocloud.dev/blob"
_ "gocloud.dev/blob/azureblob" // Azure blob driver
_ "gocloud.dev/blob/fileblob" // File blob driver
_ "gocloud.dev/blob/gcsblob" // GCS blob driver
_ "gocloud.dev/blob/s3blob" // S3 blob driver
)
const (
// DefaultLeftDelimiter is the default left-hand delimeter that will be used for the templates
DefaultLeftDelimiter = "${"
// DefaultRightDelimiter is the default right-hand delimeter that will be used for the templates
DefaultRightDelimiter = "}"
)
const templateName = "config"
// Templater is used to hold the delimeters used to configure the template engine
type Templater struct {
leftDelimiter string
rightDelimiter string
}
// NewTemplater initializes a new templater object
func NewTemplater(leftDelimiter, rightDelimiter string) Templater {
return Templater{
leftDelimiter: leftDelimiter,
rightDelimiter: rightDelimiter,
}
}
// EnvTemplate interpolates environment variables in a configuration text
func (t Templater) EnvTemplate(templateText string) (*bytes.Buffer, error) {
var env struct {
Env map[string]string
}
env.Env = make(map[string]string, len(os.Environ()))
for _, v := range os.Environ() {
split := strings.SplitN(v, "=", 2)
env.Env[split[0]] = split[1]
}
return t.Template(templateText, env)
}
// Template interpolates a data structure in a template
func (t Templater) Template(templateText string, data interface{}) (*bytes.Buffer, error) {
configTemplate, err := template.New(templateName).
Funcs(sprig.TxtFuncMap()).
Funcs(customFuncs()).
Delims(t.leftDelimiter, t.rightDelimiter).
Parse(templateText)
if err != nil {
return nil, errors.WrapIf(err, "error parsing template")
}
buffer := bytes.NewBuffer(nil)
err = configTemplate.ExecuteTemplate(buffer, templateName, data)
if err != nil {
return nil, errors.WrapIf(err, "error executing template")
}
return buffer, nil
}
func customFuncs() template.FuncMap {
return funcMap()
}
func funcMap() map[string]interface{} {
return map[string]interface{}{
"awskms": awsKmsDecrypt,
"file": fileContent,
"gcpkms": gcpKmsDecrypt,
"blob": blobRead,
"accessor": accessorPlaceholder,
}
}
func accessorPlaceholder(path string) (string, error) {
return fmt.Sprintf("__accessor__%s", strings.TrimRight(path, "/")), nil
}
// blob reads a content from a blob url
// examples:
// - file:///path/to/dir/file
// - s3://my-bucket/object?region=us-west-1
// - gs://my-bucket/object
// - azblob://my-container/blob
func blobRead(urlstr string) (string, error) {
ctx := context.Background()
u, err := url.Parse(urlstr)
if err != nil {
return "", errors.Wrapf(err, "failed to parse blob url: %s", urlstr)
}
i := strings.LastIndex(u.Path, "/")
key := u.Path[i+1:]
u.Path = u.Path[:i]
bucket, err := blob.OpenBucket(ctx, u.String())
if err != nil {
return "", errors.Wrapf(err, "failed to open blob bucket: %s", u.String())
}
defer bucket.Close()
data, err := bucket.ReadAll(ctx, key)
if err != nil {
return "", errors.Wrapf(err, "failed to read blob bucket key: %s", key)
}
return string(data), nil
}
func awsKmsDecrypt(encodedString string, encryptionContext ...string) (string, error) {
awsSession, err := session.NewSession()
if err != nil {
return "", errors.Wrap(err, "failed to create AWS session")
}
svc := kms.New(awsSession)
decoded, err := base64.StdEncoding.DecodeString(encodedString)
if err != nil {
return "", errors.Wrap(err, "failed to decode base64 string")
}
result, err := svc.Decrypt(&kms.DecryptInput{CiphertextBlob: decoded, EncryptionContext: convertContextMap(encryptionContext)})
if err != nil {
return "", errors.Wrap(err, "failed to decrypt ciphertext")
}
return string(result.Plaintext), nil
}
func gcpKmsDecrypt(encodedString string, projectID string, location string, keyRing string, key string) (string, error) {
decoded, err := base64.StdEncoding.DecodeString(encodedString)
if err != nil {
return "", errors.Wrapf(err, "failed to base64 decode ciphertext")
}
ctx := context.Background()
client, err := cloudkms.NewKeyManagementClient(ctx)
if err != nil {
return "", errors.Wrapf(err, "failed to construct KMS client")
}
req := &kmspb.DecryptRequest{
Name: fmt.Sprintf("projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", projectID, location, keyRing, key),
Ciphertext: decoded,
}
resp, err := client.Decrypt(ctx, req)
if err != nil {
panic(fmt.Sprintf("Decrypt: %v", err))
}
return string(resp.Plaintext), nil
}
func fileContent(path string) (string, error) {
r, err := os.ReadFile(path)
if err != nil {
return "", err
}
return strings.ReplaceAll(string(r), "\n", "\\n"), nil
}
func convertContextMap(encryptionContext []string) map[string]*string {
m := make(map[string]*string)
for _, p := range encryptionContext {
v := strings.Split(p, "=")
m[v[0]] = &v[1]
}
return m
}
// IsGoTemplate returns true if s is probably a Go Template
func (t Templater) IsGoTemplate(templateText string) bool {
return strings.Contains(templateText, t.leftDelimiter) && strings.Contains(templateText, t.rightDelimiter)
}