-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.go
206 lines (170 loc) · 4.34 KB
/
seed.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
package fleet
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"crypto/tls"
"errors"
"fmt"
"io"
"log"
"os"
"time"
"golang.org/x/crypto/sha3"
"github.com/google/uuid"
)
type seedData struct {
seed []byte
Id uuid.UUID
ts time.Time
}
var (
uuidSeedidSpace = uuid.Must(uuid.Parse(UUID_SEEDID_SPACE))
)
const UUID_SEEDID_SPACE = "da736663-83ec-46ef-9c29-3f9102c5c519"
func makeSeed(s []byte, t time.Time) *seedData {
seedId := uuid.NewHash(sha3.New256(), uuidSeedidSpace, s, 6) // uuid v6 - this is not in uuid specifications
return &seedData{
seed: s,
Id: seedId,
ts: t,
}
}
func (a *Agent) initSeed() {
// check for seed in db (seed is actually shared, but update rule is different from regular record so we use fleet)
if d, err := a.dbSimpleGet([]byte("fleet"), []byte("seed")); d != nil && err == nil && len(d) > 128 {
// found seed data in db
t := time.Time{}
if t.UnmarshalBinary(d[128:]) == nil {
// managed to read time too!
a.seed = makeSeed(d[:128], t)
log.Printf("[fleet] Initialized with saved cluster seed ID = %s", a.SeedId())
return
}
}
s := make([]byte, 128)
// try to load from file (legacy) and remove file (makeSeed will store it on disk)
if f, err := os.Open("fleet_seed.bin"); err == nil {
defer f.Close()
// let's try to read the seed from there?
n, err := f.Read(s)
if n == 128 && err == nil {
// read the timestamp
tsBin, err := io.ReadAll(f)
if err == nil {
t := time.Time{}
if t.UnmarshalBinary(tsBin) == nil {
// managed to read time too!
a.seed = makeSeed(s, t)
log.Printf("[fleet] Initialized with saved cluster seed ID = %s", a.SeedId())
if a.seed.WriteToDisk(a) == nil {
os.Remove("fleet_seed.bin")
}
return
}
}
}
}
_, err := rand.Read(s)
if err != nil {
panic(fmt.Sprintf("failed to initialize fleet seed: %s", err))
}
a.seed = makeSeed(s, time.Now())
a.seed.WriteToDisk(a)
log.Printf("[fleet] Initialized with cluster seed ID = %s", a.SeedId())
}
func (a *Agent) SeedId() uuid.UUID {
return a.seed.Id
}
func (s *seedData) WriteToDisk(a *Agent) error {
ts, err := s.ts.MarshalBinary()
if err != nil {
return err
}
err = a.dbSimpleSet([]byte("fleet"), []byte("seed"), append(a.seed.seed, ts...))
if err != nil {
return err
}
return nil
}
func (a *Agent) SeedTlsConfig(c *tls.Config) {
k := sha256.Sum256(a.seed.seed[32:64])
// TODO use hmac
c.SetSessionTicketKeys([][32]byte{k})
}
func (a *Agent) SeedShake128(N []byte) sha3.ShakeHash {
v := sha3.NewCShake128(N, a.seed.seed)
return v
}
func (a *Agent) SeedShake256(N []byte) sha3.ShakeHash {
v := sha3.NewCShake256(N, a.seed.seed)
return v
}
func (a *Agent) SeedSign(in []byte) []byte {
hmac := hmac.New(sha3.New256, a.seed.seed)
hmac.Write(in)
return hmac.Sum([]byte{})
}
func (a *Agent) SeedCrypt(in []byte) ([]byte, error) {
block, err := aes.NewCipher(a.seed.seed[:32])
if err != nil {
return nil, err
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
nonce := make([]byte, aesgcm.NonceSize())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return nil, err
}
ciphertext := aesgcm.Seal(nil, nonce, in, nil)
return append(nonce, ciphertext...), nil
}
func (a *Agent) SeedDecrypt(in []byte) ([]byte, error) {
block, err := aes.NewCipher(a.seed.seed[:32])
if err != nil {
return nil, err
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
if len(in) <= aesgcm.NonceSize() {
// not enough data
return nil, errors.New("decrypt: not enough data to decrypt input")
}
plaintext, err := aesgcm.Open(nil, in[:aesgcm.NonceSize()], in[aesgcm.NonceSize():], nil)
if err != nil {
return nil, err
}
return plaintext, nil
}
func (a *Agent) seedData() []byte {
ts := DbStamp(a.seed.ts).Bytes()
return append(ts, a.seed.seed...)
}
func (a *Agent) handleNewSeed(s []byte, t time.Time) error {
cur := a.seed
if t.After(cur.ts) {
// time is more recent, ignore seed
return nil
}
if bytes.Equal(s, cur.seed) {
return nil // same seed already
}
if t == cur.ts {
// not same seed, but same time, compare seeds
if bytes.Compare(s, cur.seed) > 0 {
// s is larger, keep seed
return nil
}
}
a.seed = makeSeed(s, t)
a.seed.WriteToDisk(a)
log.Printf("[fleet] Updated seed from peer, new seed ID = %s", a.SeedId())
return nil
}