-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.go
199 lines (166 loc) · 4.05 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
package fleet
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/tls"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"time"
"golang.org/x/crypto/sha3"
"github.com/google/uuid"
)
type seedData struct {
seed []byte
Id uuid.UUID
ts time.Time
}
var (
// use a pointer for atomic seed details update
seed *seedData
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 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 := 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!
seed = makeSeed(d[:128], t)
log.Printf("[fleet] Initialized with saved cluster seed ID = %s", 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 := ioutil.ReadAll(f)
if err == nil {
t := time.Time{}
if t.UnmarshalBinary(tsBin) == nil {
// managed to read time too!
seed = makeSeed(s, t)
log.Printf("[fleet] Initialized with saved cluster seed ID = %s", SeedId())
if seed.WriteToDisk() == nil {
os.Remove("fleet_seed.bin")
}
return
}
}
}
}
_, err := rand.Read(s)
if err != nil {
panic(fmt.Sprintf("failed to initialize fleet seed: %s", err))
}
seed = makeSeed(s, time.Now())
seed.WriteToDisk()
log.Printf("[fleet] Initialized with cluster seed ID = %s", SeedId())
}
func SeedId() uuid.UUID {
return seed.Id
}
func (s *seedData) WriteToDisk() error {
ts, err := s.ts.MarshalBinary()
if err != nil {
return err
}
err = dbSimpleSet([]byte("fleet"), []byte("seed"), append(seed.seed, ts...))
if err != nil {
return err
}
return nil
}
func SeedTlsConfig(c *tls.Config) {
var k [32]byte
copy(k[:], seed.seed[32:64])
c.SetSessionTicketKeys([][32]byte{k})
}
func SeedSign(in []byte) []byte {
hmac := hmac.New(sha3.New256, seed.seed)
hmac.Write(in)
return hmac.Sum([]byte{})
}
func SeedCrypt(in []byte) ([]byte, error) {
block, err := aes.NewCipher(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 SeedDecrypt(in []byte) ([]byte, error) {
block, err := aes.NewCipher(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 seedPacket() *PacketSeed {
return &PacketSeed{
Seed: seed.seed,
Time: seed.ts,
}
}
func handleNewSeed(s []byte, t time.Time) error {
cur := seed
if t.After(cur.ts) {
// time is more recent, ignore seed
return nil
}
if bytes.Compare(s, cur.seed) == 0 {
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
}
}
seed = makeSeed(s, t)
seed.WriteToDisk()
log.Printf("[fleet] Updated seed from peer, new seed ID = %s", SeedId())
return nil
}