-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3object.go
139 lines (115 loc) · 3.42 KB
/
s3object.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
package main
import (
"context"
"errors"
"fmt"
"math"
"time"
"github.com/gage-technologies/safelock"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/google/uuid"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
const (
// S3 Object Lock flags
flagS3ObjectLockBucket = "s3-bucket"
flagS3ObjectLockKey = "s3-key"
flagS3ObjectLockKMSKeyArn = "s3-kms-key-arn"
flagS3ObjectLockAction = "action"
flagS3ObjectLockID = "lock-id"
flagS3ObjectLockNode = "node"
actionLock = "lock"
actionUnlock = "unlock"
)
var (
validActions = []string{actionLock, actionUnlock}
)
func initS3ObjectLockFlags(flag *pflag.FlagSet) {
flag.String(flagAWSRegion, "us-west-2", "The AWS region")
flag.String(flagS3ObjectLockBucket, "", "The s3 bucket")
flag.String(flagS3ObjectLockKey, "", "The s3 key")
flag.String(flagS3ObjectLockKMSKeyArn, "", "The s3 kms key ARN")
flag.String(flagS3ObjectLockAction, actionLock, "The action to use")
flag.Uint64(flagS3ObjectLockID, uint64(time.Now().UnixNano()), "The id of the lock to act upon")
flag.Uint(flagS3ObjectLockNode, math.MaxInt, "The node of the lock to act upon")
}
func checkS3ObjectLockConfig(v *viper.Viper) error {
awsRegion := v.GetString(flagAWSRegion)
if len(awsRegion) == 0 {
return errors.New("An AWS region is required")
}
bucket := v.GetString(flagS3ObjectLockBucket)
if len(bucket) == 0 {
return errors.New("An s3 bucket is required")
}
key := v.GetString(flagS3ObjectLockKey)
if len(key) == 0 {
return errors.New("An s3 key is required")
}
kmsKeyArn := v.GetString(flagS3ObjectLockKMSKeyArn)
if len(kmsKeyArn) == 0 {
return errors.New("An s3 kms key ARN is required")
}
action := v.GetString(flagS3ObjectLockAction)
if !stringSliceContains(validActions, action) {
return fmt.Errorf("Action %q is not valid, must be one of %v", action, validActions)
}
if action == actionUnlock {
lockID := v.GetString(flagS3ObjectLockID)
if len(lockID) == 0 {
return errors.New("A lock ID is required when unlocking")
}
_, errParse := uuid.Parse(lockID)
if errParse != nil {
return fmt.Errorf("Lock ID %q is not a valid UUID", lockID)
}
}
return nil
}
func s3ObjectLockCmd(cmd *cobra.Command, args []string) error {
v, errViper := initViper(cmd)
if errViper != nil {
return fmt.Errorf("error initializing viper: %w", errViper)
}
if len(args) > 1 {
return cmd.Usage()
}
if errConfig := checkS3ObjectLockConfig(v); errConfig != nil {
return errConfig
}
awsRegion := v.GetString(flagAWSRegion)
bucket := v.GetString(flagS3ObjectLockBucket)
key := v.GetString(flagS3ObjectLockKey)
kmsKeyArn := v.GetString(flagS3ObjectLockKMSKeyArn)
action := v.GetString(flagS3ObjectLockAction)
awsCfg, errCfg := config.LoadDefaultConfig(context.TODO(), config.WithRegion(awsRegion))
if errCfg != nil {
return errCfg
}
svcS3 := s3.NewFromConfig(awsCfg)
node := v.GetUint(flagS3ObjectLockNode)
l := safelock.NewS3ObjectLock(uint16(node), bucket, key, kmsKeyArn, svcS3)
lockID := v.GetUint64(flagS3ObjectLockID)
l.SetID(lockID)
switch action {
case actionLock:
errWaitForLock := l.WaitForLock(safelock.DefaultTimeout)
if errWaitForLock != nil {
return errWaitForLock
}
errLock := l.Lock()
if errLock != nil {
return errLock
}
fmt.Println(l.GetID())
case actionUnlock:
errUnlock := l.Unlock()
if errUnlock != nil {
return errUnlock
}
}
return nil
}