-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
650 lines (618 loc) · 18.2 KB
/
main.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
// -*- tab-width: 2 -*-
package main
import (
"errors"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/organizations"
"github.com/aws/aws-sdk-go/service/s3"
count "github.com/jayalane/go-counter"
set "github.com/jayalane/go-persist-set"
"github.com/jayalane/go-tinyconfig"
"log"
"math/rand"
"net"
"net/http"
_ "net/http/pprof"
"os"
"reflect"
"runtime"
"strings"
"sync"
"sync/atomic"
"time"
)
var theConfig config.Config
var defaultConfig = `#
logAllObjects = false
oneBucket = false
oneBucketName = bucket_name
oneBucketReencrypt = false
checkReplica = false
checkEtag = false
reCopyFile = false
setToDangerToReCopy = asdfasd
checkAcl = false
aclOwnerAcct = true
checkOrgAccounts = true
listFilesMatchingSuffix = %%%/
listFilesMatchingPrefix = %%%
listFilesMatchingExclude = %%%
useDeleteAnywayFile =
justListFiles = false
setToDangerToReencrypt = no
reencryptToTargetBucket =
setToDangerToDeleteMatching = no
setToDangerToForceACL = no
numAccountHandlers = 1
numBucketHandlers = 10
numObjectHandlers = 1000
profListen = localhost:6060
# comments
`
// info about an object to check
type objectChanItem struct {
acctID string
bucket string
object string
wg *sync.WaitGroup
}
// info about a bucket to check
type bucketChanItem struct {
acctID string
bucket string
}
// global state
type context struct {
doneObjects *set.SetDb
lastObj uint64
filter *[]string
bucketChan chan bucketChanItem
objectChan chan objectChanItem
accountChan chan string
credsRW sync.RWMutex
creds map[string]*credentials.Credentials
wg *sync.WaitGroup
canonIDMap map[string]string
canonRW sync.RWMutex
keyIDMap map[string]string
keyRW sync.RWMutex
}
var theCtx context
// first a few utilities
// given a bucket, replace us-east-1 with us-west-2
func getReplicaBucket(b string) string {
// todo
return strings.Replace(b, "us-east-1", "us-west-2", -1)
}
// given an account, gets a session
func getSessForAcct(a string) *session.Session {
if a == "0" {
initSess, err := session.NewSession(&aws.Config{Region: aws.String("us-east-1")})
if err != nil {
panic(fmt.Sprintf("Can't get session for master %s", err.Error()))
}
return initSess
}
theCtx.credsRW.RLock()
defer theCtx.credsRW.RUnlock()
var creds *credentials.Credentials
var ok bool
if creds, ok = theCtx.creds[a]; !ok {
initSess, err := session.NewSession(&aws.Config{Region: aws.String("us-east-1")})
count.Incr("aws-newsession-root")
if err != nil {
panic(fmt.Sprintf("Can't get session for master %s", err.Error()))
}
creds = getCredentials(*initSess, a)
}
count.Incr("aws-newsession-acct")
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-east-1"),
Credentials: creds})
if err != nil {
fmt.Println("Can't get session for master", err.Error())
count.Incr("aws-newsession-error")
return nil
}
return sess
}
// given an account, gets a Canonical ID
func lookupCanonicalIDForAcct(a string, sess *session.Session) {
theCtx.canonRW.Lock()
defer theCtx.canonRW.Unlock()
if _, ok := theCtx.canonIDMap[a]; !ok {
cID, err := lookupCanonID(a, sess)
if err != nil {
log.Println("Error getting canonical ID for acct id", a, err)
}
theCtx.canonIDMap[a] = cID
}
}
func getDefaultKey(b *string, svc *s3.S3) (string, error) {
if b == nil || svc == nil {
return "", errors.New("No bucket or svc")
}
Input := s3.GetBucketEncryptionInput{
Bucket: b,
}
count.Incr("aws-get-bucket-enc")
out, err := svc.GetBucketEncryption(&Input)
if err != nil {
if reqerr, ok := err.(awserr.RequestFailure); ok {
if reqerr.StatusCode() == 404 {
count.Incr("404 error")
count.Incr("404 error" + *b)
} else if reqerr.StatusCode() == 403 {
count.Incr("403 error")
count.Incr("403 error" + *b)
} else {
log.Println("Got request error on object", b, err, reqerr, reqerr.OrigErr())
}
} else {
if netErr, ok := err.(net.Error); ok {
log.Println("Error is type", reflect.TypeOf(netErr.Timeout()))
log.Println("Got net error on bucket", b, netErr)
} else {
log.Println("Got error on object", b, err)
}
}
return "", err
}
keyID := ""
if out != nil {
// this is complex API for some reason
if out.ServerSideEncryptionConfiguration == nil {
return "", errors.New("No encryption setting")
}
for _, v := range out.ServerSideEncryptionConfiguration.Rules {
if v.ApplyServerSideEncryptionByDefault != nil {
if "AES256" == aws.StringValue(v.ApplyServerSideEncryptionByDefault.SSEAlgorithm) {
return "AES256", nil
}
keyID = aws.StringValue(v.ApplyServerSideEncryptionByDefault.KMSMasterKeyID)
}
}
}
fmt.Println("Got kms key", keyID, " for bucket", *b)
setBucketKey(b, keyID)
return keyID, nil
}
// write id to map with sync
func setBucketKey(b *string, id string) {
if b != nil && id != "" {
theCtx.keyRW.Lock()
theCtx.keyIDMap[*b] = id
theCtx.keyRW.Unlock()
}
}
// given a master session and an account ID, generate an assumed role credentials
func getCredentials(session session.Session, acctID string) *credentials.Credentials {
a := "arn:aws:iam::" + acctID + ":role/OrganizationAccountAccessRole"
count.Incr("aws-sts-new-creds")
creds := stscreds.NewCredentials(&session, a)
return creds
}
// then a few go routines
// go routine to get objects and see if they are encrypted
func handleObject() {
count.Incr("aws-new-session-init")
initSess, err := session.NewSession(&aws.Config{Region: aws.String("us-east-1")})
var sess *session.Session
if err != nil {
panic(fmt.Sprintf("Can't get session for master %s", err.Error()))
}
for {
select {
case kb := <-theCtx.objectChan:
count.Incr("object-chan-remove")
if kb.acctID == "0" {
sess = initSess
} else {
sess = getSessForAcct(kb.acctID)
if sess == nil {
log.Println("Can't log into AWS!")
kb.wg.Done()
theCtx.wg.Done()
continue
}
}
svc := s3.New(sess)
k := kb.object
b := kb.bucket
count.Incr("total-object")
count.Incr("total-object-" + b)
if theConfig["justListFiles"].BoolVal {
if filterObjectPasses(b, k, theCtx.filter) {
if theConfig["logAllObjects"].BoolVal {
fmt.Printf(
"Found match s3://%s/%s\n",
b,
k)
}
count.IncrDelta("list-found", 1)
count.IncrDelta("list-found-"+b, 1)
if theConfig["setToDangerToDeleteMatching"].StrVal == "danger" {
// fmt.Println("Going to delete", k)
_, err = deleteObject(k, b, sess)
if err != nil {
fmt.Println("Error deleting", k, b, err.Error())
}
count.IncrDelta("list-deleted", 1)
count.IncrDelta("list-deleted-"+b, 1)
}
} else {
// fmt.Println("Skipping object", k, b)
count.IncrDelta("list-skipping", 1)
count.IncrDelta("list-skipping-"+b, 1)
}
kb.wg.Done()
theCtx.wg.Done()
continue
}
// not just list files
if theConfig["checkAcl"].BoolVal {
count.Incr("handle-acl")
count.Incr("handle-acl-" + b)
handleACL(b, k, kb.acctID, sess)
kb.wg.Done()
theCtx.wg.Done()
continue
}
if theConfig["threeAcl"].BoolVal {
count.Incr("handle-3acl")
count.Incr("handle-3acl-" + b)
handleThreeACL(b, k, kb.acctID, sess)
kb.wg.Done()
theCtx.wg.Done()
continue
}
sb := keyName(*aws.String(b), *aws.String(k))
if theCtx.doneObjects.InSet(sb) {
count.Incr("skip-done-pre-head")
continue
}
req := &s3.HeadObjectInput{Key: aws.String(k),
Bucket: aws.String(b)}
count.Incr("aws-head-object")
head, err := svc.HeadObject(req)
if err != nil {
logCountErrTag(err, "bucket/object"+k+"/"+b, b)
} else { // head succeeded
// needed for recopy, re-encrypt, and checkEtag
tooBig := false
etag := head.ETag
// while we are here, check repl status
if head.ReplicationStatus == nil {
count.Incr("object-replication-empty")
fmt.Println("Replication empty " + k + "/" + b)
} else if *head.ReplicationStatus == "COMPLETED" {
count.Incr("object-replication-completed")
} else {
count.Incr("object-replication-not-completed")
count.Incr("object-replication-status-" + *head.ReplicationStatus)
fmt.Println("Replication status " + k + "/" + b + *head.ReplicationStatus)
}
if head.ContentLength != nil {
count.IncrDelta("object-length", *head.ContentLength)
count.IncrDelta("object-length-"+b, *head.ContentLength)
if *head.ContentLength > 5368709000 {
fmt.Println("Big object", *aws.String(b), *aws.String(k))
count.Incr("big-object")
count.Incr("big-object-" + b)
tooBig = true
}
}
if theConfig["checkReplica"].BoolVal {
continue
}
if theConfig["checkEtag"].BoolVal {
// fmt.Println("Checking etag", b, k, etag)
// another head to another bucket
b2 := getReplicaBucket(b)
req := &s3.HeadObjectInput{Key: aws.String(k),
Bucket: aws.String(b2)}
fmt.Println("About to call head", req)
count.Incr("aws-head-object-etag-repl")
head, err := svc.HeadObject(req)
if err != nil {
logCountErrTag(err, "bucket/object"+k+"/"+b2, b2)
} else { // head succeeded
replEtag := head.ETag
if replEtag == etag {
continue
}
fmt.Print("Object out of sync", k+"/"+b+"/"+b2)
}
continue
}
if theConfig["reCopyFiles"].BoolVal {
count.Incr("copy-start")
count.Incr("copy-start-" + b)
if !(theConfig["setToDangerToReCopy"].StrVal == "danger") {
continue
}
if !tooBig {
retry := reencryptObject(b, k, false, sess) // false is don't care about key
if retry {
count.Incr("retry-copy-object")
count.Incr("retry-copy-object-" + b)
theCtx.objectChan <- kb
kb.wg.Add(1) // Done in handleObject
theCtx.wg.Add(1) // Done in handleObject
}
}
} else if theConfig["oneBucketReencrypt"].BoolVal {
// we will be reencrypting
if !isObjectEncOk(b, *head) {
count.Incr("encrypt-bad")
count.Incr("encrypt-bad-" + b)
if !(theConfig["setToDangerToReencrypt"].StrVal == "danger") {
continue
}
if !tooBig {
retry := reencryptObject(b, k, true, sess) // true is must have KMS ID
if retry {
count.Incr("retry-object")
count.Incr("retry-object-" + b)
theCtx.objectChan <- kb
}
}
} else {
count.Incr("encrypt-good")
count.Incr("encrypt-good-" + b)
}
} else {
if !isObjectEncOk(b, *head) {
count.Incr("unencrypted")
count.Incr("unencrypted-" + b)
if rand.Float64() < (1.0 / (float64(count.ReadSync("unencrypted")))) {
if nil == head.ServerSideEncryption {
fmt.Println("ERROR: no encryption", b, k)
} else if *head.ServerSideEncryption == "aws:kms" {
fmt.Println("ERROR: ", b, k, *head.ServerSideEncryption, *head.SSEKMSKeyId)
} else {
fmt.Println("ERROR: ", b, k, *head.ServerSideEncryption)
}
}
}
}
}
kb.wg.Done()
theCtx.wg.Done()
case <-time.After(60 * time.Second * 60):
log.Println("Exiting object handler after 1 hour with no traffic")
return
}
}
}
func makeTimestamp() uint64 { // from stackoverflow
return uint64(time.Now().UnixNano()) / uint64(time.Millisecond)
}
// go routine to get buckets and list their objects
func handleBucket() {
count.Incr("aws-new-session-bare-2")
initSess, err := session.NewSession(&aws.Config{Region: aws.String("us-east-1")})
var sess *session.Session
if err != nil {
panic(fmt.Sprintf("Can't get session for master %s", err.Error()))
}
for {
select {
case b := <-theCtx.bucketChan:
atomic.StoreUint64(&theCtx.lastObj, makeTimestamp())
log.Println("Got a bucket", b.bucket)
if b.acctID == "0" {
sess = initSess
} else {
sess = getSessForAcct(b.acctID)
if sess == nil {
log.Println("Can't log into AWS!")
theCtx.wg.Done()
continue
}
}
count.Incr("total-bucket")
svc := s3.New(sess)
// get default key
key, err := getDefaultKey(aws.String(b.bucket), svc)
if err != nil && key != "" {
setBucketKey(aws.String(b.bucket), key)
}
// start list objects
req := &s3.ListObjectsV2Input{Bucket: aws.String(b.bucket)}
count.Incr("aws-list-objects-v2")
wg := new(sync.WaitGroup) // different WG to make bucket wait for objects
_ = svc.ListObjectsV2Pages(req, func(resp *s3.ListObjectsV2Output, lastPage bool) bool {
count.Incr("object-page")
for _, content := range resp.Contents {
key := *content.Key
wg.Add(1) // Done in handleObject
theCtx.wg.Add(1) // Done in handleObject
count.Incr("object-chan-add")
runtime.Gosched()
theCtx.objectChan <- objectChanItem{b.acctID, b.bucket, key, wg}
}
count.Incr("object-page-exit")
return true
})
theCtx.wg.Done()
case <-time.After(60 * time.Second):
log.Println("Giving up on bucket channel after 1 minute with no traffic")
return
}
}
}
// go routine to get accounts and list their buckets
func handleAccount() {
count.Incr("aws-new-session-bare-2")
initSess, err := session.NewSession(&aws.Config{Region: aws.String("us-east-1")})
var sess *session.Session
if err != nil {
panic(fmt.Sprintf("Can't get session for master %s", err.Error()))
}
gotOne := false
for {
select {
case a := <-theCtx.accountChan:
if a == "0" {
sess = initSess
log.Println("Got default account")
} else {
log.Println("Got an account", a)
creds := getCredentials(*initSess, a)
theCtx.credsRW.Lock()
theCtx.creds[a] = creds
theCtx.credsRW.Unlock()
count.Incr("aws-new-session-creds-2")
sess, err = session.NewSession(&aws.Config{Region: aws.String("us-east-1"),
Credentials: creds})
if err != nil {
log.Println("Couldn't use credentials for acct", a, err)
continue
}
}
log.Println("About to call get canonical id", a)
lookupCanonicalIDForAcct(a, sess)
svc := s3.New(sess)
count.Incr("aws-list-buckets")
result, err := svc.ListBuckets(nil)
if err != nil {
log.Println("Can't list buckets!", err)
}
for _, b := range result.Buckets {
log.Println("Got a bucket", aws.StringValue(b.Name))
if !theConfig["oneBucket"].BoolVal || theConfig["oneBucketName"].StrVal == aws.StringValue(b.Name) {
theCtx.wg.Add(1) // done in handleBucket
log.Println("Got a bucket", aws.StringValue(b.Name))
theCtx.bucketChan <- bucketChanItem{a, *b.Name}
gotOne = true
}
}
if !gotOne {
log.Println("Processing for buckets done with no buckets seen for", a)
}
theCtx.wg.Done() // Add(1) in main
case <-time.After(60 * time.Second):
log.Println("Giving up on buckets after 1 minute with no traffic")
return
}
}
}
func main() {
// stats
count.InitCounters()
// config
if len(os.Args) > 1 && os.Args[1] == "--dumpConfig" {
fmt.Println(defaultConfig)
return
}
// still config
var err error
theConfig, err = config.ReadConfig("config.txt", defaultConfig)
log.Println("Config", theConfig)
if err != nil {
log.Println("Error opening config.txt", err.Error())
if theConfig == nil {
os.Exit(11)
}
}
// save objects we have copied to disk
if theConfig["reCopyFiles"].BoolVal || theConfig["checkEtag"].BoolVal || theConfig["checkReplica"].BoolVal {
theCtx.doneObjects = set.New("doneObjects_2")
}
// filters for delete only these things under these things
if len(theConfig["useDeleteAnywayFile"].StrVal) > 0 {
theCtx.filter, err = readWillDeleteFile(theConfig["useDeleteAnywayFile"].StrVal)
}
if theCtx.filter != nil {
log.Println("Filter", theCtx.filter)
}
if err != nil {
log.Println("Error opening config.txt", err.Error())
if theConfig == nil {
os.Exit(11)
}
}
// init the globals
atomic.StoreUint64(&theCtx.lastObj, makeTimestamp())
theCtx.wg = new(sync.WaitGroup)
theCtx.accountChan = make(chan string, 100)
theCtx.bucketChan = make(chan bucketChanItem, 100)
theCtx.objectChan = make(chan objectChanItem, 1000000)
theCtx.creds = make(map[string]*credentials.Credentials)
theCtx.credsRW = sync.RWMutex{}
theCtx.keyIDMap = make(map[string]string)
theCtx.keyRW = sync.RWMutex{}
theCtx.canonIDMap = make(map[string]string)
theCtx.canonRW = sync.RWMutex{}
// start go routines
go handleAccount()
for i := 0; i < theConfig["numBucketHandlers"].IntVal; i++ {
go handleBucket()
}
for i := 0; i < theConfig["numObjectHandlers"].IntVal; i++ {
go handleObject()
}
// now the work
// log into master account
sess, err := session.NewSession(&aws.Config{Region: aws.String("us-east-1")})
if err != nil {
panic(fmt.Sprintf("Can't log into AWS! %s", err))
}
if theConfig["checkOrgAccounts"].BoolVal {
svc := organizations.New(sess)
// to get all the accounts
input := &organizations.ListAccountsInput{}
count.Incr("aws-list-accounts-org")
la, err := svc.ListAccounts(input)
if err != nil {
log.Println("Got an Organization error: ", err, err.Error())
return
}
for { // to handle paginatin - break is down in the "no next token"
for _, r := range la.Accounts {
fmt.Println("Account", r.Status, r)
if *r.Status == "ACTIVE" {
theCtx.accountChan <- *r.Id
theCtx.wg.Add(1) // done in handleBucket
}
}
if la.NextToken != nil {
in := &organizations.ListAccountsInput{NextToken: la.NextToken}
la, err = svc.ListAccounts(in)
if err != nil {
log.Println("Got an Organization error: ", err, err.Error())
break
}
} else { // no more data
break
}
}
} else {
log.Println("Just doing one account")
theCtx.accountChan <- "0"
theCtx.wg.Add(1) // done in handleBucket
}
// start the profiler
go func() {
if len(theConfig["profListen"].StrVal) > 0 {
log.Println(http.ListenAndServe(theConfig["profListen"].StrVal, nil))
}
}()
// waiting till done -
// this needs to wait a bit because the WG can empty when an object list page is fully processed
// and the work per object is very small
for makeTimestamp()-atomic.LoadUint64(&theCtx.lastObj) < 10*1000 {
log.Println("Last activity sleeping 60 seconds", makeTimestamp()-atomic.LoadUint64(&theCtx.lastObj))
time.Sleep(60 * time.Second)
log.Println("Now waiting")
theCtx.wg.Wait()
}
count.LogCounters()
log.Println("Exiting", makeTimestamp()-atomic.LoadUint64(&theCtx.lastObj))
}