forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret.go
226 lines (193 loc) · 4.41 KB
/
secret.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
package secret
import (
"bytes"
"fmt"
"strings"
"sync"
"github.com/justwatchcom/gopass/store"
yaml "gopkg.in/yaml.v2"
)
// Secret is a decoded secret
type Secret struct {
sync.Mutex
password string
body string
data map[string]interface{}
}
// New creates a new secret
func New(password, body string) *Secret {
return &Secret{
password: password,
body: body,
}
}
// Parse decodes an secret. It will always return a valid secret. If decoding
// the body to YAML is may return an error which can be ignored.
func Parse(buf []byte) (*Secret, error) {
s := &Secret{}
lines := bytes.SplitN(buf, []byte("\n"), 2)
if len(lines) > 0 {
s.password = string(bytes.TrimSpace(lines[0]))
}
if len(lines) > 1 {
s.body = string(bytes.TrimSpace(lines[1]))
}
if _, err := s.decodeYAML(); err != nil {
return s, err
}
return s, nil
}
// decodeYAML attempts to decode an optional YAML part of a secret
func (s *Secret) decodeYAML() (bool, error) {
if !strings.HasPrefix(s.body, "---\n") && s.password != "---" {
return false, nil
}
d := make(map[string]interface{})
err := yaml.Unmarshal([]byte(s.body), &d)
if err != nil {
return true, err
}
s.data = d
s.body = ""
return true, nil
}
// Bytes encodes an secret
func (s *Secret) Bytes() ([]byte, error) {
buf := &bytes.Buffer{}
_, _ = buf.WriteString(s.password)
_, _ = buf.WriteString("\n")
if s.data != nil {
yb, err := yaml.Marshal(s.data)
if err != nil {
return nil, err
}
_, _ = buf.WriteString("---\n")
_, _ = buf.Write(yb)
return buf.Bytes(), nil
}
_, _ = buf.WriteString(s.body)
return buf.Bytes(), nil
}
// String encodes and returns a string representation of a secret
func (s *Secret) String() string {
buf := &bytes.Buffer{}
_, _ = buf.WriteString(s.password)
_, _ = buf.WriteString("\n")
if s.data != nil {
yb, err := yaml.Marshal(s.data)
if err != nil {
_, _ = buf.WriteString(fmt.Sprintf("YAML-Encoding Error: %s\n%+v\n", err, s.data))
return buf.String()
}
_, _ = buf.WriteString("---\n")
_, _ = buf.Write(yb)
return buf.String()
}
_, _ = buf.WriteString(s.body)
return buf.String()
}
// Password returns the first line from a secret
func (s *Secret) Password() string {
s.Lock()
defer s.Unlock()
return s.password
}
// SetPassword sets a new password (i.e. the first line)
func (s *Secret) SetPassword(pw string) {
s.Lock()
defer s.Unlock()
s.password = pw
}
// Body returns the body of a secret. If the body was valid YAML it returns an
// empty string
func (s *Secret) Body() string {
s.Lock()
defer s.Unlock()
return s.body
}
// Data returns the data of a secret. Unless the body was valid YAML, it returns
// an map
func (s *Secret) Data() map[string]interface{} {
s.Lock()
defer s.Unlock()
return s.data
}
// SetBody sets a new body possibly erasing an decoded YAML map
func (s *Secret) SetBody(b string) error {
s.Lock()
defer s.Unlock()
s.body = b
s.data = nil
_, err := s.decodeYAML()
return err
}
// Value returns the value of the given key if the body contained valid
// YAML
func (s *Secret) Value(key string) (string, error) {
s.Lock()
defer s.Unlock()
if s.data == nil {
if !strings.HasPrefix(s.body, "---\n") {
return "", store.ErrYAMLNoMark
}
if _, err := s.decodeYAML(); err != nil {
return "", err
}
}
if v, found := s.data[key]; found {
if sv, ok := v.(string); ok {
return sv, nil
}
return "", store.ErrYAMLValueUnsupported
}
return "", store.ErrYAMLNoKey
}
// SetValue sets a key to a given value. Will fail if an non-empty body exists
func (s *Secret) SetValue(key, value string) error {
s.Lock()
defer s.Unlock()
if s.body == "" && s.data == nil {
s.data = make(map[string]interface{}, 1)
}
if s.data == nil {
return store.ErrYAMLNoMark
}
s.data[key] = value
return nil
}
// DeleteKey key will delete a single key from an decoded map
func (s *Secret) DeleteKey(key string) error {
s.Lock()
defer s.Unlock()
if s.data == nil {
return store.ErrYAMLNoMark
}
delete(s.data, key)
return nil
}
// Equal returns true if two secrets are equal
func (s *Secret) Equal(other *Secret) bool {
s.Lock()
defer s.Unlock()
if s == nil && other == nil {
return true
}
if s.password != other.Password() {
return false
}
if s.body != other.Body() {
return false
}
buf, err := s.Bytes()
if err != nil {
return false
}
bufOther, err := other.Bytes()
if err != nil {
return false
}
if len(buf) != len(bufOther) {
return false
}
return true
}