Skip to content

Commit 4035d77

Browse files
committed
Initial commit
0 parents  commit 4035d77

File tree

7 files changed

+223
-0
lines changed

7 files changed

+223
-0
lines changed

LICENSE

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Copyright (c) 2017, Mark Peek <mark@peek.org>
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# AWSAdminAccess
2+
3+
In a lot of organizations there is a master AWS account and then
4+
other accounts are added via consolidated billing. To better manage
5+
the assets it is usually good to add a AdministratorAccess role to
6+
the sub-account to allow the master account to monitor and control
7+
costs on the sub-accounts. AWSAdminAccess provides a quick and easy
8+
way to setup a trust policy for the AdministratorAccess.
9+
10+
## Running AWSAdminAccess
11+
12+
Download a binary for your system from the Releases page. You will
13+
need to know the role name you want to create and the account number
14+
of the master account.
15+
16+
```
17+
AWSAdminAccess -r MasterAccountAccess -a 123456789012
18+
```
19+
20+
## Building
21+
22+
To build the binaries it is preferable to use a docker build environment for
23+
consistency. First build the docker buildn environment:
24+
25+
```
26+
docker build -f build/Dockerfile-buildenv -t cloudtools:AWSAdminAccess-buildenv .
27+
```
28+
29+
Next install the vendor package using glide:
30+
```
31+
glide install
32+
```
33+
34+
And then build the binaries:
35+
```
36+
docker run -v `pwd`:/go/src/github.com/cloudtools/AWSAdminAccess -t cloudtools:AWSAdminAccess-buildenv bash -x build/build.sh
37+
```

build/Dockerfile-buildenv

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM vmware/photon:latest
2+
MAINTAINER Mark Peek <mark@peek.org>
3+
LABEL Description="Photon environment" Vendor="Cloudtools"
4+
5+
ENV GOLANG_VERSION 1.8.1
6+
ENV GOLANG_BIN_URL https://storage.googleapis.com/golang/go$GOLANG_VERSION.linux-amd64.tar.gz
7+
ENV GOLANG_BIN_SHA256 a579ab19d5237e263254f1eac5352efcf1d70b9dacadb6d6bb12b0911ede8994
8+
9+
RUN tdnf makecache
10+
RUN tdnf install -y git gzip tar && \
11+
tdnf clean all
12+
13+
RUN curl -s -o golang.tar.gz "$GOLANG_BIN_URL" && \
14+
echo "$GOLANG_BIN_SHA256 golang.tar.gz" | sha256sum -c - && \
15+
tar -C /usr/local -xzf golang.tar.gz && \
16+
rm -f golang.tar.gz
17+
18+
ENV GOPATH /go
19+
ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH
20+
RUN mkdir -p $GOPATH/src/github.com/cloudtools/AWSAdminAccess
21+
WORKDIR $GOPATH/src/github.com/cloudtools/AWSAdminAccess

build/build.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
ARCHITECTURES=amd64
4+
OPERATING_SYSTEMS="linux darwin"
5+
for GOARCH in $ARCHITECTURES; do
6+
for GOOS in $OPERATING_SYSTEMS; do
7+
GOOS=$GOOS GOARCH=$GOARCH go build -o AWSAdminAccess-$GOOS-$GOARCH
8+
gzip -f AWSAdminAccess-$GOOS-$GOARCH
9+
done
10+
done

glide.lock

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package: github.com/markpeek/AWSAdminAccess
2+
import:
3+
- package: github.com/aws/aws-sdk-go
4+
version: ^1.8.11
5+
subpackages:
6+
- aws
7+
- aws/session
8+
- service/iam

main.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"os"
7+
8+
"github.com/aws/aws-sdk-go/aws"
9+
"github.com/aws/aws-sdk-go/aws/session"
10+
"github.com/aws/aws-sdk-go/service/iam"
11+
)
12+
13+
var policyArn = "arn:aws:iam::aws:policy/AdministratorAccess"
14+
var assumeRoleDocument = `{
15+
"Version": "2012-10-17",
16+
"Statement": [
17+
{
18+
"Effect": "Allow",
19+
"Principal": {
20+
"AWS": "arn:aws:iam::%s:root"
21+
},
22+
"Action": "sts:AssumeRole"
23+
}
24+
]
25+
}`
26+
27+
func main() {
28+
var account, role string
29+
30+
flag.StringVar(&account, "a", "", "Account to trust")
31+
flag.StringVar(&role, "r", "", "Role name to create")
32+
flag.Parse()
33+
34+
if account == "" {
35+
fmt.Printf("Must specify account option\n")
36+
os.Exit(0)
37+
}
38+
39+
if role == "" {
40+
fmt.Printf("Must specify role name option\n")
41+
os.Exit(0)
42+
}
43+
44+
sess := session.Must(session.NewSession())
45+
svc := iam.New(sess)
46+
47+
// First see if the role already exists
48+
params := &iam.GetRoleInput{
49+
RoleName: aws.String(role),
50+
}
51+
_, err := svc.GetRole(params)
52+
if err == nil {
53+
fmt.Printf("Role %s already exists\n", role)
54+
os.Exit(0)
55+
}
56+
57+
// Make sure the policy exists before creating the role
58+
policyParams := &iam.GetPolicyInput{
59+
PolicyArn: aws.String(policyArn),
60+
}
61+
_, err = svc.GetPolicy(policyParams)
62+
if err != nil {
63+
fmt.Printf("Policy %s does not exist: %v\n", policyArn, err)
64+
os.Exit(0)
65+
}
66+
67+
// Create the role with a trust policy document
68+
assumeRoleString := fmt.Sprintf(assumeRoleDocument, account)
69+
roleParams := &iam.CreateRoleInput{
70+
AssumeRolePolicyDocument: aws.String(assumeRoleString),
71+
RoleName: aws.String(role),
72+
}
73+
roleOutput, err := svc.CreateRole(roleParams)
74+
if err != nil {
75+
fmt.Printf("Cannot create role %s\n", err)
76+
os.Exit(0)
77+
}
78+
79+
// Attach the role policy onto the role
80+
_, err = svc.AttachRolePolicy(&iam.AttachRolePolicyInput{
81+
PolicyArn: aws.String(policyArn),
82+
RoleName: aws.String(role),
83+
})
84+
if err != nil {
85+
fmt.Printf("AttachRolePolicy failed: %v\n", err)
86+
os.Exit(0)
87+
}
88+
89+
fmt.Printf("Role %s created - ARN: %s\n", role, *roleOutput.Role.Arn)
90+
}

0 commit comments

Comments
 (0)