-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathsecret.go
More file actions
127 lines (100 loc) · 3.09 KB
/
secret.go
File metadata and controls
127 lines (100 loc) · 3.09 KB
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
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// 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 extraconfig
import (
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
log "github.com/Sirupsen/logrus"
"golang.org/x/crypto/nacl/secretbox"
)
// The value of this key is hidden from API requests, but visible within the guest
// #nosec: Potential hardcoded credentials
const GuestInfoSecretKey = "guestinfo.ovfEnv"
// SecretKey provides helpers to encrypt/decrypt extraconfig values
type SecretKey struct {
key [32]byte
}
// NewSecretKey generates a new secret key
func NewSecretKey() (*SecretKey, error) {
s := new(SecretKey)
if _, err := rand.Read(s.key[:]); err != nil {
return nil, err
}
return s, nil
}
// FromString base64 decodes an existing SecretKey
func (s *SecretKey) FromString(key string) error {
b, err := base64.StdEncoding.DecodeString(key)
if err != nil {
return err
}
if len(b) != 32 {
return errors.New("invalid secret key")
}
copy(s.key[:], b)
return nil
}
// String base64 encodes a SecretKey
func (s *SecretKey) String() string {
return base64.StdEncoding.EncodeToString(s.key[:])
}
// Source wraps the given DataSource, decrypting any secret values
func (s *SecretKey) Source(ds DataSource) DataSource {
// If GuestInfoSecretKey has a value, it should be our secret key.
// #nosec: Errors unhandled.
if val, _ := ds(GuestInfoSecretKey); val != "" {
if err := s.FromString(val); err != nil {
log.Errorf("failed to decode %s: %s", GuestInfoSecretKey, err)
} else {
log.Debugf("secret key decoded from %s", GuestInfoSecretKey)
}
}
return func(key string) (string, error) {
val, err := ds(key)
if err == nil && isSecret(key) {
b, err := base64.StdEncoding.DecodeString(val)
if err != nil {
return "", err
}
var nonce [24]byte
copy(nonce[:], b[:24])
plaintext, ok := secretbox.Open([]byte{}, b[24:], &nonce, &s.key)
if !ok {
return "", fmt.Errorf("failed to decrypt value for %s", key)
}
val = string(plaintext)
}
return val, err
}
}
// Sink wraps the given DataSink, encrypting any secret values
func (s *SecretKey) Sink(ds DataSink) DataSink {
// Store our secret key.
if err := ds(GuestInfoSecretKey, s.String()); err != nil {
log.Errorf("failed to store %s: %s", GuestInfoSecretKey, err)
}
return func(key, value string) error {
if isSecret(key) {
var nonce [24]byte
if _, err := rand.Read(nonce[:]); err != nil {
return err
}
ciphertext := secretbox.Seal(nonce[:], []byte(value), &nonce, &s.key)
value = base64.StdEncoding.EncodeToString(ciphertext)
}
return ds(key, value)
}
}