-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
CreatePolicy.go
111 lines (98 loc) · 3.23 KB
/
CreatePolicy.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
// snippet-start:[iam.go.create_policy]
package main
// snippet-start:[iam.go.create_policy.imports]
import (
"encoding/json"
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/iam/iamiface"
)
// snippet-end:[iam.go.create_policy.imports]
// StatementEntry will dictate what this policy allows or doesn't allow.
// snippet-start:[iam.go.create_policy.structs]
type StatementEntry struct {
Effect string
Action []string
Resource string
}
// PolicyDocument is our definition of our policies to be uploaded to IAM.
type PolicyDocument struct {
Version string
Statement []StatementEntry
}
// snippet-end:[iam.go.create_policy.structs]
// MakePolicy creates an IAM policy
// Inputs:
// svc is an IAM service client
// Output:
// If success, nil
// Otherwise, an error from the call to json.Marshall or CreatePolicy
func MakePolicy(svc iamiface.IAMAPI, policyName *string) error {
// snippet-start:[iam.go.create_policy.doc]
// Builds our policy document for IAM.
policy := PolicyDocument{
Version: "2012-10-17",
Statement: []StatementEntry{
StatementEntry{
Effect: "Allow",
Action: []string{
"logs:CreateLogGroup", // Allow for creating log groups
},
Resource: "RESOURCE ARN FOR logs:*",
},
StatementEntry{
Effect: "Allow",
// Allows for DeleteItem, GetItem, PutItem, Scan, and UpdateItem
Action: []string{
"dynamodb:DeleteItem",
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:Scan",
"dynamodb:UpdateItem",
},
Resource: "RESOURCE ARN FOR dynamodb:*",
},
},
}
b, err := json.Marshal(&policy)
// snippet-end:[iam.go.create_policy.doc]
if err != nil {
return err
}
// snippet-start:[iam.go.create_policy.call]
_, err = svc.CreatePolicy(&iam.CreatePolicyInput{
PolicyDocument: aws.String(string(b)),
PolicyName: policyName,
})
// snippet-end:[iam.go.create_policy.call]
return err
}
func main() {
// snippet-start:[iam.go.create_policy.args]
policyName := flag.String("n", "", "The name of the policy")
flag.Parse()
if *policyName == "" {
fmt.Println("You must supply the name of the policy (-n POLICY)")
return
}
// snippet-end:[iam.go.create_policy.args]
// snippet-start:[iam.go.create_policy.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := iam.New(sess)
// snippet-end:[iam.go.create_policy.session]
err := MakePolicy(svc, policyName)
if err != nil {
fmt.Println("Got an error creating the policy:")
fmt.Println(err)
return
}
fmt.Println("Created policy " + *policyName)
}
// snippet-end:[iam.go.create_policy]