Skip to content

Commit e11ff23

Browse files
authored
Merge pull request #17 from 99designs/policy-normalisation
Normalise the AWS policy document
2 parents 6b05311 + c019000 commit e11ff23

File tree

9 files changed

+317
-123
lines changed

9 files changed

+317
-123
lines changed

glide.lock

+30-27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package: github.com/99designs/iamy
22
import:
33
- package: github.com/aws/aws-sdk-go
4+
version: ~1.4.5
45
subpackages:
56
- aws
7+
- aws/awserr
68
- aws/session
79
- service/ec2
810
- service/iam
911
- service/iam/iamiface
10-
- package: github.com/mtibben/yamljsonmap
11-
- package: gopkg.in/alecthomas/kingpin.v2
12-
- package: gopkg.in/yaml.v2
12+
- service/s3
13+
- service/s3/s3iface
14+
- package: github.com/ghodss/yaml
1315
- package: github.com/pkg/errors
16+
version: ~0.7.1
17+
- package: gopkg.in/alecthomas/kingpin.v2
18+
version: ~2.2.3

iamy/aws.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (a *AwsFetcher) Fetch() (*AccountData, error) {
6262
wg.Wait()
6363

6464
if iamErr != nil {
65-
return nil, errors.Wrap(iamErr, "Error fetching IAM error")
65+
return nil, errors.Wrap(iamErr, "Error fetching IAM data")
6666
}
6767
if s3Err != nil {
6868
return nil, errors.Wrap(s3Err, "Error fetching S3 data")

iamy/models.go

+19-72
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,11 @@
11
package iamy
22

33
import (
4-
"bytes"
5-
"encoding/json"
64
"fmt"
7-
"net/url"
85
"regexp"
96
"strings"
10-
11-
"github.com/mtibben/yamljsonmap"
127
)
138

14-
type PolicyDocument yamljsonmap.StringKeyMap
15-
16-
func (p *PolicyDocument) Encode() string {
17-
return url.QueryEscape(string(p.json()))
18-
}
19-
20-
func (p PolicyDocument) json() []byte {
21-
jsonBytes, err := json.Marshal(yamljsonmap.StringKeyMap(p))
22-
if err != nil {
23-
panic(err.Error())
24-
}
25-
return jsonBytes
26-
}
27-
28-
func (p *PolicyDocument) JsonString() string {
29-
var out bytes.Buffer
30-
json.Indent(&out, p.json(), "", " ")
31-
return out.String()
32-
}
33-
34-
func (m PolicyDocument) MarshalJSON() ([]byte, error) {
35-
return json.Marshal(yamljsonmap.StringKeyMap(m))
36-
}
37-
38-
func (m *PolicyDocument) UnmarshalYAML(unmarshal func(interface{}) error) error {
39-
var n yamljsonmap.StringKeyMap
40-
if err := unmarshal(&n); err != nil {
41-
return err
42-
}
43-
*m = PolicyDocument(n)
44-
45-
return nil
46-
}
47-
48-
func NewPolicyDocumentFromEncodedJson(encoded string) (PolicyDocument, error) {
49-
jsonString, err := url.QueryUnescape(encoded)
50-
if err != nil {
51-
return nil, err
52-
}
53-
54-
var doc PolicyDocument
55-
if err = json.Unmarshal([]byte(jsonString), &doc); err != nil {
56-
return nil, err
57-
}
58-
59-
return doc, nil
60-
}
61-
629
type Account struct {
6310
Id string
6411
Alias string
@@ -103,8 +50,8 @@ func Arn(r AwsResource, a *Account) string {
10350
}
10451

10552
type iamService struct {
106-
Name string `yaml:"-"`
107-
Path string `yaml:"-"`
53+
Name string `json:"-"`
54+
Path string `json:"-"`
10855
}
10956

11057
func (s iamService) Service() string {
@@ -120,54 +67,54 @@ func (s iamService) ResourcePath() string {
12067
}
12168

12269
type User struct {
123-
iamService `yaml:"-"`
124-
Groups []string `yaml:"Groups,omitempty"`
125-
InlinePolicies []InlinePolicy `yaml:"InlinePolicies,omitempty"`
126-
Policies []string `yaml:"Policies,omitempty"`
70+
iamService `json:"-"`
71+
Groups []string `json:"Groups,omitempty"`
72+
InlinePolicies []InlinePolicy `json:"InlinePolicies,omitempty"`
73+
Policies []string `json:"Policies,omitempty"`
12774
}
12875

12976
func (u User) ResourceType() string {
13077
return "user"
13178
}
13279

13380
type Group struct {
134-
iamService `yaml:"-"`
135-
InlinePolicies []InlinePolicy `yaml:"InlinePolicies,omitempty"`
136-
Policies []string `yaml:"Policies,omitempty"`
81+
iamService `json:"-"`
82+
InlinePolicies []InlinePolicy `json:"InlinePolicies,omitempty"`
83+
Policies []string `json:"Policies,omitempty"`
13784
}
13885

13986
func (g Group) ResourceType() string {
14087
return "group"
14188
}
14289

14390
type InlinePolicy struct {
144-
Name string `yaml:"Name"`
145-
Policy PolicyDocument `yaml:"Policy"`
91+
Name string `json:"Name"`
92+
Policy *PolicyDocument `json:"Policy"`
14693
}
14794

14895
type Policy struct {
149-
iamService `yaml:"-"`
150-
Policy PolicyDocument `yaml:"Policy"`
96+
iamService `json:"-"`
97+
Policy *PolicyDocument `json:"Policy"`
15198
}
15299

153100
func (p Policy) ResourceType() string {
154101
return "policy"
155102
}
156103

157104
type Role struct {
158-
iamService `yaml:"-"`
159-
AssumeRolePolicyDocument PolicyDocument `yaml:"AssumeRolePolicyDocument"`
160-
InlinePolicies []InlinePolicy `yaml:"InlinePolicies,omitempty"`
161-
Policies []string `yaml:"Policies,omitempty"`
105+
iamService `json:"-"`
106+
AssumeRolePolicyDocument *PolicyDocument `json:"AssumeRolePolicyDocument"`
107+
InlinePolicies []InlinePolicy `json:"InlinePolicies,omitempty"`
108+
Policies []string `json:"Policies,omitempty"`
162109
}
163110

164111
func (r Role) ResourceType() string {
165112
return "role"
166113
}
167114

168115
type BucketPolicy struct {
169-
BucketName string `yaml:"-"`
170-
Policy PolicyDocument `yaml:"Policy"`
116+
BucketName string `json:"-"`
117+
Policy *PolicyDocument `json:"Policy"`
171118
}
172119

173120
func (u BucketPolicy) Service() string {

iamy/models_test.go

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
package iamy
22

3-
import (
4-
"fmt"
5-
"testing"
6-
)
7-
8-
func TestPolicyDocumentEncodingRoundTrip(t *testing.T) {
9-
policy := PolicyDocument{
10-
"foo": map[string]string{
11-
"bar": "baz",
12-
},
13-
}
14-
encodedPolicy := policy.Encode()
15-
result, _ := NewPolicyDocumentFromEncodedJson(encodedPolicy)
16-
17-
if fmt.Sprintf("%v", result) != fmt.Sprintf("%v", policy) {
18-
t.Errorf("PolicyDocument failed an Encode roundtrip, got %#v, expected %#v", result, policy)
19-
}
20-
}
3+
import "testing"
214

225
func TestNewAccountFromString(t *testing.T) {
236

0 commit comments

Comments
 (0)