-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrapper.go
225 lines (185 loc) · 5.21 KB
/
wrapper.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
package age
import (
"bytes"
"fmt"
"strings"
"filippo.io/age"
"filippo.io/age/armor"
"sylr.dev/yaml/v3"
)
const (
// YAMLTag tag that is used to identify data to encrypt/decrypt
YAMLTag = "!crypto/age"
YAMLTagPrefix = "!crypto/age:"
)
var _ yaml.Unmarshaler = (*Wrapper)(nil)
var _ yaml.Marshaler = (*Wrapper)(nil)
// Wrapper is a struct used as a wrapper for yaml.Marshal and yaml.Unmarshal.
type Wrapper struct {
// Value holds the struct that will either be decrypted with the given
// Identities or encrypted with the given Recipients.
Value interface{}
// Identities that will be used to try decrypting encrypted Value.
Identities []age.Identity
// Recipients that will be used for encrypting un-encrypted Value.
Recipients []age.Recipient
// DiscardNoTag instructs the Unmarshaler to not honour the NoTag
// `!crypto/age` tag attribute. This is useful when re-keying data.
DiscardNoTag bool
// ForceNoTag strip the `!crypto/age` tags from the Marshaler output.
ForceNoTag bool
// NoDecrypt inscruts the Unmarshaler to leave encrypted data encrypted.
// This is useful when you want to Marshal new un-encrytped data in a
// document already containing encrypted data.
NoDecrypt bool
}
// UnmarshalYAML takes a yaml.Node and recursively decrypt nodes marked with the
// `!crypto/age` YAML tag.
func (w Wrapper) UnmarshalYAML(value *yaml.Node) error {
decoded, err := w.decode(value)
if err != nil {
return err
}
return decoded.Decode(w.Value)
}
func (w Wrapper) decode(node *yaml.Node) (*yaml.Node, error) {
if node == nil {
return nil, nil
}
// Recurse into sequence types
if node.Kind == yaml.DocumentNode || node.Kind == yaml.SequenceNode || node.Kind == yaml.MappingNode {
var err error
if len(node.Content) > 0 {
for i := range node.Content {
node.Content[i], err = w.decode(node.Content[i])
if err != nil {
return nil, err
}
}
}
}
var notag bool
var style yaml.Style
switch {
case node.Tag == YAMLTag:
case strings.HasPrefix(node.Tag, YAMLTagPrefix):
attrStr := node.Tag[len(YAMLTagPrefix):]
attrs := strings.Split(attrStr, ",")
for _, attr := range attrs {
lower := strings.ToLower(attr)
switch lower {
case "doublequoted", "singlequoted", "literal", "folded", "flow":
if style != 0 {
return nil, fmt.Errorf("%w: %s", ErrMoreThanOneStyleAttribute, attrStr)
}
switch lower {
case "doublequoted":
style = yaml.DoubleQuotedStyle
case "singlequoted":
style = yaml.SingleQuotedStyle
case "literal":
style = yaml.LiteralStyle
case "folded":
style = yaml.FoldedStyle
case "flow":
style = yaml.FlowStyle
}
case "notag":
notag = true
default:
return nil, fmt.Errorf("%w: %s", ErrUnknownAttribute, attrStr)
}
}
default:
return node, nil
}
if w.ForceNoTag {
node.Tag = ""
}
// Check the absence of armored age header and footer
if w.NoDecrypt || !isArmoredAgeFile(node.Value) {
return node, nil
}
var str string
err := node.Decode(&str)
if err != nil {
return nil, err
}
stringReader := strings.NewReader(str)
armoredReader := armor.NewReader(stringReader)
decryptedReader, err := age.Decrypt(armoredReader, w.Identities...)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrUpstreamAgeError, err)
}
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(decryptedReader)
if err != nil {
return nil, err
}
tempTag := node.Tag
node.SetString(buf.String())
if !w.ForceNoTag {
if w.DiscardNoTag || !notag {
node.Tag = tempTag
}
}
if style == 0 {
if strings.Contains(node.Value, "\n") {
node.Style = yaml.LiteralStyle
} else {
node.Style = yaml.FlowStyle
}
} else {
node.Style = style
}
return node, nil
}
// MarshalYAML recursively encrypts Value.
func (w Wrapper) MarshalYAML() (interface{}, error) {
switch v := w.Value.(type) {
case *yaml.Node:
return w.encode(v)
default:
return nil, fmt.Errorf("%w: %#v", ErrUnsupportedValueType, v)
}
}
// marshalYAML is the internal implementation of MarshalYAML. We need the internal
// implementation to be able to return *yaml.Node instead of interface{} because
// the global MarshalYAML function needs to return an interface{} to comply with
// the yaml.Marshaler interface.
func (w Wrapper) encode(node *yaml.Node) (*yaml.Node, error) {
if node == nil {
return nil, nil
}
// Recurse into sequence types
if node.Kind == yaml.DocumentNode || node.Kind == yaml.SequenceNode || node.Kind == yaml.MappingNode {
var err error
if len(node.Content) > 0 {
for i := range node.Content {
node.Content[i], err = w.encode(node.Content[i])
if err != nil {
return nil, err
}
}
}
return node, nil
}
switch {
case node.Tag == YAMLTag:
case strings.HasPrefix(node.Tag, YAMLTagPrefix):
default:
return node, nil
}
if isArmoredAgeFile(node.Value) {
return node, nil
}
str := NewStringFromNode(node, w.Recipients)
nodeInterface, err := str.MarshalYAML()
return nodeInterface.(*yaml.Node), err
}
// isArmoredAgeFile checks whether the value starts with the AGE armor.Header
// and ends with the AGE armor Footer.
func isArmoredAgeFile(data string) bool {
trimmed := strings.TrimSpace(data)
return strings.HasPrefix(trimmed, armor.Header) && strings.HasSuffix(trimmed, armor.Footer)
}