Skip to content

Commit e9355f8

Browse files
authored
Release (#1)
* Add create registory func. * * Add policy file * change filename from aws.go to repository.go * Create puller params. * Add main command. * Fixed command. * Update readme.
1 parent 3267914 commit e9355f8

File tree

5 files changed

+252
-0
lines changed

5 files changed

+252
-0
lines changed

.editorconfig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
root = true
2+
charset = utf-8
3+
4+
[*]
5+
indent_style = tab
6+
indent_size = 4
7+
end_of_line = lf
8+
insert_final_newline = true
9+
10+
[*.go]
11+
indent_style = tab
12+
indent_size = 4
13+
14+
[*.yml]
15+
indent_style = space
16+
indent_size = 2
17+
18+
[Dockerfile*]
19+
indent_size = 4

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
11
# ecr-factory
22
AWS ECR factory
3+
4+
# Installation
5+
6+
```console
7+
$ go get github.com/ushios/ecr-factory
8+
```
9+
10+
# Usage
11+
12+
Create repository and policy that are pull and push.
13+
```console
14+
$ ecrf -id xxxxx -secret xxxx -name your_docker_repository_name
15+
```

cmd/ecrf/main.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
7+
"github.com/aws/aws-sdk-go/aws"
8+
"github.com/aws/aws-sdk-go/aws/credentials"
9+
"github.com/aws/aws-sdk-go/aws/session"
10+
"github.com/aws/aws-sdk-go/service/ecr"
11+
"github.com/aws/aws-sdk-go/service/iam"
12+
ecrf "github.com/ushios/ecr-factory"
13+
)
14+
15+
var (
16+
id = flag.String("id", "", "aws access key id")
17+
secret = flag.String("secret", "", "aws secret key")
18+
name = flag.String("name", "", "repository name")
19+
)
20+
21+
func main() {
22+
23+
flag.Parse()
24+
fmt.Printf("id: %s, secret: %s, name: %s\n", *id, *secret, *name)
25+
26+
cre := credentials.NewStaticCredentials(*id, *secret, "")
27+
sess, err := session.NewSession(&aws.Config{
28+
Region: aws.String("ap-northeast-1"),
29+
Credentials: cre,
30+
})
31+
if err != nil {
32+
panic(err)
33+
}
34+
35+
e := ecr.New(sess)
36+
repo, err := ecrf.CreateRepository(e, *name)
37+
if err != nil {
38+
panic(err)
39+
}
40+
41+
fmt.Printf("Created repository(name: %s)\n", *repo.RepositoryName)
42+
43+
i := iam.New(sess)
44+
pull, err := ecrf.CreatePullerPolicy(i, repo)
45+
if err != nil {
46+
panic(err)
47+
}
48+
49+
fmt.Printf("Created policy(id:%s, name: %s)\n", *pull.PolicyId, *pull.PolicyName)
50+
51+
push, err := ecrf.CreatePusherPolicy(i, repo)
52+
if err != nil {
53+
panic(err)
54+
}
55+
56+
fmt.Printf("Created policy(id:%s, name: %s)\n", *push.PolicyId, *push.PolicyName)
57+
}

policy.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package ecrf
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/aws/aws-sdk-go/aws"
7+
"github.com/aws/aws-sdk-go/service/ecr"
8+
"github.com/aws/aws-sdk-go/service/iam"
9+
"github.com/ushios/iamgo"
10+
)
11+
12+
const (
13+
PullerPolicyName = "ecr-pull-%s"
14+
PullerDescription = "Pull %s. Generated by ecr-factory"
15+
PusherPolicyName = "ecr-push-%s"
16+
PusherDescription = "Push to %s. Generated by ecr-factory"
17+
)
18+
19+
// CreatePullerPolicy create policy
20+
func CreatePullerPolicy(i *iam.IAM, repo *ecr.Repository) (*iam.Policy, error) {
21+
param, err := pulleerParam(repo)
22+
if err != nil {
23+
return nil, err
24+
}
25+
26+
resp, err := i.CreatePolicy(param)
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
return resp.Policy, nil
32+
}
33+
34+
// CreatePusherPolicy create policy
35+
func CreatePusherPolicy(i *iam.IAM, repo *ecr.Repository) (*iam.Policy, error) {
36+
param, err := pusherParam(repo)
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
resp, err := i.CreatePolicy(param)
42+
if err != nil {
43+
return nil, err
44+
}
45+
46+
return resp.Policy, nil
47+
}
48+
49+
func pulleerParam(repo *ecr.Repository) (*iam.CreatePolicyInput, error) {
50+
p := pullerPolicy(repo)
51+
b, err := p.PolicyScheme()
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
param := &iam.CreatePolicyInput{
57+
PolicyName: aws.String(fmt.Sprintf(PullerPolicyName, *(repo.RepositoryName))),
58+
Description: aws.String(fmt.Sprintf(PullerDescription, *(repo.RepositoryName))),
59+
PolicyDocument: aws.String(string(b)),
60+
}
61+
62+
return param, nil
63+
}
64+
65+
func pullerPolicy(repo *ecr.Repository) iamgo.Policy {
66+
p := iamgo.NewLatestPolicy(
67+
[]iamgo.StatementEntry{
68+
iamgo.StatementEntry{
69+
Effect: iamgo.Allow,
70+
Action: []string{
71+
"ecr:GetAuthorizationToken",
72+
},
73+
Resource: "*",
74+
},
75+
iamgo.StatementEntry{
76+
Effect: iamgo.Allow,
77+
Action: []string{
78+
"ecr:GetAuthorizationToken",
79+
"ecr:BatchCheckLayerAvailability",
80+
"ecr:GetDownloadUrlForLayer",
81+
"ecr:GetRepositoryPolicy",
82+
"ecr:DescribeRepositories",
83+
"ecr:ListImages",
84+
"ecr:DescribeImages",
85+
"ecr:BatchGetImage",
86+
},
87+
Resource: *(repo.RepositoryArn),
88+
},
89+
},
90+
)
91+
92+
return p
93+
}
94+
95+
func pusherParam(repo *ecr.Repository) (*iam.CreatePolicyInput, error) {
96+
p := pusherPolicy(repo)
97+
b, err := p.PolicyScheme()
98+
if err != nil {
99+
return nil, err
100+
}
101+
102+
param := &iam.CreatePolicyInput{
103+
PolicyName: aws.String(fmt.Sprintf(PusherPolicyName, *(repo.RepositoryName))),
104+
Description: aws.String(fmt.Sprintf(PusherDescription, *(repo.RepositoryName))),
105+
PolicyDocument: aws.String(string(b)),
106+
}
107+
108+
return param, nil
109+
}
110+
111+
func pusherPolicy(repo *ecr.Repository) iamgo.Policy {
112+
p := iamgo.NewLatestPolicy(
113+
[]iamgo.StatementEntry{
114+
iamgo.StatementEntry{
115+
Effect: iamgo.Allow,
116+
Action: []string{
117+
"ecr:GetAuthorizationToken",
118+
},
119+
Resource: "*",
120+
},
121+
iamgo.StatementEntry{
122+
Effect: iamgo.Allow,
123+
Action: []string{
124+
"ecr:GetAuthorizationToken",
125+
"ecr:BatchCheckLayerAvailability",
126+
"ecr:GetDownloadUrlForLayer",
127+
"ecr:GetRepositoryPolicy",
128+
"ecr:DescribeRepositories",
129+
"ecr:ListImages",
130+
"ecr:DescribeImages",
131+
"ecr:BatchGetImage",
132+
"ecr:InitiateLayerUpload",
133+
"ecr:UploadLayerPart",
134+
"ecr:CompleteLayerUpload",
135+
"ecr:PutImage",
136+
},
137+
Resource: *(repo.RepositoryArn),
138+
},
139+
},
140+
)
141+
142+
return p
143+
}

repository.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ecrf
2+
3+
import (
4+
"github.com/aws/aws-sdk-go/aws"
5+
"github.com/aws/aws-sdk-go/service/ecr"
6+
)
7+
8+
// CreateRepository create name repository
9+
func CreateRepository(c *ecr.ECR, name string) (*ecr.Repository, error) {
10+
param := &ecr.CreateRepositoryInput{
11+
RepositoryName: aws.String(name),
12+
}
13+
14+
resp, err := c.CreateRepository(param)
15+
if err != nil {
16+
return nil, err
17+
}
18+
19+
return resp.Repository, nil
20+
}

0 commit comments

Comments
 (0)