/
main.go
176 lines (152 loc) · 5.42 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
package lambda
import (
"context"
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider"
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider/types"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/kvendingoldo/aws-cognito-restore-lambda/internal/cloud"
"github.com/kvendingoldo/aws-cognito-restore-lambda/internal/config"
log "github.com/sirupsen/logrus"
"io"
"time"
)
func getDataFromS3(ctx context.Context, client *cloud.Client, bucketName, keyName string) ([]byte, error) {
obj, err := client.S3Client.GetObject(ctx, &s3.GetObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(keyName),
})
if err != nil {
log.Errorf("Failed to get %s object data from %s bucket", keyName, bucketName)
return nil, err
}
data, err := io.ReadAll(obj.Body)
if err != nil {
log.Errorf("Failed to convert %s object data to bytes", keyName)
return nil, err
}
return data, err
}
func Execute(ctx context.Context, config config.Config) error {
client, err := cloud.New(ctx, config.CognitoRegion, config.S3BucketRegion)
if err != nil {
//nolint:stylecheck
return fmt.Errorf("Could not create AWS client. Error: %w", err)
}
if config.CleanUpBeforeRestore.Bool {
users, err := client.CognitoClient.ListUsers(ctx, &cognitoidentityprovider.ListUsersInput{
UserPoolId: aws.String(config.CognitoUserPoolID),
})
if err != nil {
return fmt.Errorf("[cleanup] Failed to get list of cognito users. Error: %w", err)
}
for _, user := range users.Users {
_, err := client.CognitoClient.AdminDeleteUser(
ctx, &cognitoidentityprovider.AdminDeleteUserInput{
UserPoolId: aws.String(config.CognitoUserPoolID),
Username: user.Username,
},
)
if err != nil {
log.Errorf("[cleanup] Failed to user %s. Error: %s", *user.Username, err)
} else {
log.Debugf("User %s has been successefully deleted from %s userpool", *user.Username, config.CognitoUserPoolID)
}
}
groups, err := client.CognitoClient.ListGroups(ctx, &cognitoidentityprovider.ListGroupsInput{
UserPoolId: aws.String(config.CognitoUserPoolID),
})
if err != nil {
return fmt.Errorf("[cleanup] Failed to get list of cognito groups. Error: %w", err)
}
for _, group := range groups.Groups {
_, err := client.CognitoClient.DeleteGroup(
ctx, &cognitoidentityprovider.DeleteGroupInput{
UserPoolId: aws.String(config.CognitoUserPoolID),
GroupName: group.GroupName,
},
)
if err != nil {
log.Errorf("[cleanup] Failed to delete group %s. Error: %s", *group.GroupName, err)
} else {
log.Debugf("Group %s has been successefully deleted from %s userpool", *group.GroupName, config.CognitoUserPoolID)
}
}
time.Sleep(3 * time.Second)
log.Infof("User pool %s has been successfully cleaned up", config.CognitoUserPoolID)
}
if config.RestoreUsers.Bool {
data, err := getDataFromS3(ctx, client, config.S3BucketName, fmt.Sprintf("%s/users.json", config.BackupDirPath))
if err != nil {
//nolint:stylecheck
return fmt.Errorf("Failed to get users backup data from S3. Error: %w", err)
} else { //nolint:revive
log.Debugf("%s/users.json data has been received successfully from S3", config.BackupDirPath)
}
var users cognitoidentityprovider.ListUsersOutput
err = json.Unmarshal(data, &users)
if err != nil {
//nolint:stylecheck
return fmt.Errorf("Failed to unmarshal users backup data. Error: %w", err)
} else { //nolint:revive
log.Debug("users data has been unmarshalled successfully")
}
for _, user := range users.Users {
var userAttributes []types.AttributeType
var userName *string
for _, attribute := range user.Attributes {
if *attribute.Name == "email" {
userName = attribute.Value
}
// NOTE: it's unique value for each Cognito UserPool and for that reason we do not need to
// include it to restore attributes
if *attribute.Name != "sub" {
userAttributes = append(userAttributes, attribute)
}
}
_, err := client.CognitoClient.AdminCreateUser(
ctx, &cognitoidentityprovider.AdminCreateUserInput{
UserPoolId: aws.String(config.CognitoUserPoolID),
Username: userName,
UserAttributes: userAttributes,
},
)
if err != nil {
//nolint:stylecheck
return fmt.Errorf("Failed to restore user %s. Error: %w", *user.Username, err)
}
}
}
if config.RestoreGroups.Bool {
data, err := getDataFromS3(ctx, client, config.S3BucketName, fmt.Sprintf("%s/groups.json", config.BackupDirPath))
if err != nil {
//nolint:stylecheck
return fmt.Errorf("Failed to get groups backup data from S3. Error: %w", err)
} else { //nolint:revive
log.Debugf("%s/groups.json data has been received successfully from S3", config.BackupDirPath)
}
var groups cognitoidentityprovider.ListGroupsOutput
err = json.Unmarshal(data, &groups)
if err != nil {
//nolint:stylecheck
return fmt.Errorf("Failed to unmarshal groups backup data. Error: %w", err)
} else { //nolint:revive
log.Debug("groups data has been unmarshalled successfully")
}
for _, group := range groups.Groups {
_, err := client.CognitoClient.CreateGroup(
ctx, &cognitoidentityprovider.CreateGroupInput{
UserPoolId: aws.String(config.CognitoUserPoolID),
GroupName: group.GroupName,
},
)
if err != nil {
//nolint:stylecheck
return fmt.Errorf("Failed to restore group %s. Error: %w", *group.GroupName, err)
}
}
}
return nil
}