forked from hartfordfive/cloudflarebeat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statefile.go
334 lines (275 loc) · 7 KB
/
statefile.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package cloudflare
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/elastic/beats/libbeat/logp"
)
/*
const (
FETCH_PERIOD_SECONDS = 1800
)
*/
type StateFile struct {
FileName string
FilePath string
ZoneName string
StorageType string
properties Properties
lastUpdated time.Time
s3settings *awsS3Settings
lock *sync.Mutex
}
type Properties struct {
LastStartTS int `json:"last_start_ts"`
LastEndTS int `json:"last_end_ts"`
LastCount int `json:"last_count"`
LastRequestTS int `json:"last_request_ts"`
LastUpdateTS int `json:"last_update_ts"`
}
type awsS3Settings struct {
awsAccesKey string
awsSecretAccessKey string
s3BucketName string
}
func (p *Properties) ToJsonBytes() []byte {
b, _ := json.Marshal(p)
return b
}
func NewStateFile(config map[string]string) (*StateFile, error) {
sf := &StateFile{
StorageType: config["storage_type"],
}
if sf.StorageType == "s3" {
if _, ok := config["aws_access_key"]; !ok {
return nil, errors.New("Must specify aws_access_key when using S3 storage.")
}
if _, ok := config["aws_secret_access_key"]; !ok {
return nil, errors.New("Must specify aws_secret_access_key when using S3 storage.")
}
if _, ok := config["aws_s3_bucket_name"]; !ok {
return nil, errors.New("Must specify aws_secret_access_key when using S3 storage.")
}
sf.s3settings = &awsS3Settings{config["aws_access_key"], config["aws_secret_access_key"], config["aws_s3_bucket_name"]}
} else if _, ok := config["filepath"]; ok {
sf.FilePath = config["filepath"]
}
if _, ok := config["zone_tag"]; !ok {
return nil, errors.New("Must specify zone_tag.")
}
sf.FileName = config["filename"] + "-" + config["zone_tag"] + ".state"
sf.lock = &sync.Mutex{}
sf.initialize()
return sf, nil
}
func (s *StateFile) initialize() error {
logp.Info("Initializing state file '%s' with storage type '%s'", s.FileName, s.StorageType)
var err error
if s.StorageType == "disk" {
err = s.loadFromDisk()
} else if s.StorageType == "s3" {
err = s.loadFromS3()
} else {
return errors.New("Unsupported storage type")
}
if err != nil {
return err
}
return nil
}
func (s *StateFile) initializeStateFileValues() {
s.properties.LastUpdateTS = int(time.Now().UTC().Unix())
}
func (s *StateFile) loadFromDisk() error {
sfName := filepath.Join(s.FilePath, s.FileName)
// Create it if it doesn't exist
if _, err := os.Stat(sfName); os.IsNotExist(err) {
var file, err = os.Create(sfName)
defer file.Close()
if err != nil {
return err
}
s.initializeStateFileValues()
logp.Info("Saving newly initialized state file.")
if err := s.Save(); err != nil {
logp.Info("[ERROR] Could not save new state file: %v", err)
}
return nil
}
// Now load the file in memory
sfData, err := ioutil.ReadFile(sfName)
if err != nil {
return err
}
var dat Properties
if err := json.Unmarshal(sfData, &dat); err != nil {
// If the state file isn't valid json, then re-create it
if err != nil {
logp.Info("[ERROR] Could not unmarshal: %s", err)
logp.Info("State file contents: %s", string(sfData))
err = os.Remove(sfName)
var file, err = os.Create(sfName)
defer file.Close()
if err != nil {
return err
}
return nil
}
}
s.properties = dat
return nil
}
func (s *StateFile) loadFromS3() error {
s.lock.Lock()
svc, err := s.getAwsSession()
if err != nil {
s.lock.Unlock()
return err
}
// 1. Check if the file exists and if not, create it
// 2. Otherwise, fetch the object's contents, and store it in the local state instance
params := &s3.GetObjectInput{
Bucket: aws.String(s.s3settings.s3BucketName),
Key: aws.String(s.FileName),
}
resp, err := svc.GetObject(params)
if err != nil && err.Error() == "NoSuchKey: The specified key does not exist." {
// Create the file here as it doesn't exist
s.initializeStateFileValues()
_ = s.Save()
s.lock.Unlock()
return err
} else if err != nil {
s.lock.Unlock()
return err
}
// File was successfully loaded. Unmarshall into state attribute
var p Properties
if err := json.Unmarshal([]byte(fmt.Sprint(resp)), &p); err != nil {
s.lock.Unlock()
return err
}
s.properties = p
s.lock.Unlock()
return nil
}
func (s *StateFile) GetLastStartTS() int {
return s.properties.LastStartTS
}
func (s *StateFile) GetLastEndTS() int {
return s.properties.LastEndTS
}
func (s *StateFile) GetLastCount() int {
return s.properties.LastCount
}
func (s *StateFile) GetLastRequestTS() int {
return s.properties.LastRequestTS
}
func (s *StateFile) UpdateLastStartTS(ts int) {
s.lock.Lock()
s.properties.LastStartTS = ts
s.lock.Unlock()
}
func (s *StateFile) UpdateLastEndTS(ts int) {
s.lock.Lock()
s.properties.LastEndTS = ts
s.lock.Unlock()
}
func (s *StateFile) UpdateLastCount(count int) {
s.lock.Lock()
s.properties.LastCount = count
s.lock.Unlock()
}
func (s *StateFile) UpdateLastRequestTS(ts int) {
s.lock.Lock()
s.properties.LastRequestTS = ts
s.lock.Unlock()
}
func (s *StateFile) Save() error {
var err error
s.lock.Lock()
if s.StorageType == "disk" {
err = s.saveToDisk()
} else if s.StorageType == "s3" {
err = s.saveToDisk()
}
s.lock.Unlock()
if err != nil {
return err
}
logp.Info("Done saving state file...")
return nil
}
func (s *StateFile) saveToDisk() error {
s.lastUpdated = time.Now()
s.properties.LastUpdateTS = int(time.Now().Unix())
// open file using READ & WRITE permission
var file, err = os.OpenFile(s.FileName, os.O_RDWR, 0644)
defer file.Close()
if err != nil {
return err
}
data, _ := json.Marshal(s.properties)
_, err = file.WriteString(string(data))
if err != nil {
return err
}
// save changes
err = file.Sync()
if err != nil {
return err
}
return nil
}
func (s *StateFile) saveToS3() error {
svc, err := s.getAwsSession()
if err != nil {
return err
}
_, err = s.writeToS3(svc)
if err != nil {
return err
}
return nil
}
func (s *StateFile) getAwsSession() (*s3.S3, error) {
sess := session.New(&aws.Config{
Region: aws.String("us-east-1"),
})
/*
Or with debugging on:
sess := session.New((&aws.Config{
Region: aws.String("us-east-1"),
}).WithLogLevel(aws.LogDebugWithRequestRetries | aws.LogDebugWithRequestErrors))
*/
token := ""
creds := credentials.NewStaticCredentials(s.s3settings.awsAccesKey, s.s3settings.awsSecretAccessKey, token)
_, err := creds.Get()
if err != nil {
logp.Info("[ERROR] AWS Credentials: %v", err)
return nil, err
}
svc := s3.New(sess, &aws.Config{
Region: aws.String("us-east-1"),
Credentials: creds,
})
return svc, nil
}
func (s *StateFile) writeToS3(svc *s3.S3) (*s3.PutObjectOutput, error) {
params := &s3.PutObjectInput{
Bucket: aws.String(s.s3settings.s3BucketName), // Required
Key: aws.String(s.FileName), // Required
Body: bytes.NewReader(s.properties.ToJsonBytes()),
}
return svc.PutObject(params)
}