Skip to content

Commit

Permalink
Normalise the AWS policy document to avoid conflicts when diffing
Browse files Browse the repository at this point in the history
  • Loading branch information
mtibben committed Sep 1, 2016
1 parent 6b05311 commit c019000
Show file tree
Hide file tree
Showing 9 changed files with 317 additions and 123 deletions.
57 changes: 30 additions & 27 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package: github.com/99designs/iamy
import:
- package: github.com/aws/aws-sdk-go
version: ~1.4.5
subpackages:
- aws
- aws/awserr
- aws/session
- service/ec2
- service/iam
- service/iam/iamiface
- package: github.com/mtibben/yamljsonmap
- package: gopkg.in/alecthomas/kingpin.v2
- package: gopkg.in/yaml.v2
- service/s3
- service/s3/s3iface
- package: github.com/ghodss/yaml
- package: github.com/pkg/errors
version: ~0.7.1
- package: gopkg.in/alecthomas/kingpin.v2
version: ~2.2.3
2 changes: 1 addition & 1 deletion iamy/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (a *AwsFetcher) Fetch() (*AccountData, error) {
wg.Wait()

if iamErr != nil {
return nil, errors.Wrap(iamErr, "Error fetching IAM error")
return nil, errors.Wrap(iamErr, "Error fetching IAM data")
}
if s3Err != nil {
return nil, errors.Wrap(s3Err, "Error fetching S3 data")
Expand Down
91 changes: 19 additions & 72 deletions iamy/models.go
Original file line number Diff line number Diff line change
@@ -1,64 +1,11 @@
package iamy

import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"regexp"
"strings"

"github.com/mtibben/yamljsonmap"
)

type PolicyDocument yamljsonmap.StringKeyMap

func (p *PolicyDocument) Encode() string {
return url.QueryEscape(string(p.json()))
}

func (p PolicyDocument) json() []byte {
jsonBytes, err := json.Marshal(yamljsonmap.StringKeyMap(p))
if err != nil {
panic(err.Error())
}
return jsonBytes
}

func (p *PolicyDocument) JsonString() string {
var out bytes.Buffer
json.Indent(&out, p.json(), "", " ")
return out.String()
}

func (m PolicyDocument) MarshalJSON() ([]byte, error) {
return json.Marshal(yamljsonmap.StringKeyMap(m))
}

func (m *PolicyDocument) UnmarshalYAML(unmarshal func(interface{}) error) error {
var n yamljsonmap.StringKeyMap
if err := unmarshal(&n); err != nil {
return err
}
*m = PolicyDocument(n)

return nil
}

func NewPolicyDocumentFromEncodedJson(encoded string) (PolicyDocument, error) {
jsonString, err := url.QueryUnescape(encoded)
if err != nil {
return nil, err
}

var doc PolicyDocument
if err = json.Unmarshal([]byte(jsonString), &doc); err != nil {
return nil, err
}

return doc, nil
}

type Account struct {
Id string
Alias string
Expand Down Expand Up @@ -103,8 +50,8 @@ func Arn(r AwsResource, a *Account) string {
}

type iamService struct {
Name string `yaml:"-"`
Path string `yaml:"-"`
Name string `json:"-"`
Path string `json:"-"`
}

func (s iamService) Service() string {
Expand All @@ -120,54 +67,54 @@ func (s iamService) ResourcePath() string {
}

type User struct {
iamService `yaml:"-"`
Groups []string `yaml:"Groups,omitempty"`
InlinePolicies []InlinePolicy `yaml:"InlinePolicies,omitempty"`
Policies []string `yaml:"Policies,omitempty"`
iamService `json:"-"`
Groups []string `json:"Groups,omitempty"`
InlinePolicies []InlinePolicy `json:"InlinePolicies,omitempty"`
Policies []string `json:"Policies,omitempty"`
}

func (u User) ResourceType() string {
return "user"
}

type Group struct {
iamService `yaml:"-"`
InlinePolicies []InlinePolicy `yaml:"InlinePolicies,omitempty"`
Policies []string `yaml:"Policies,omitempty"`
iamService `json:"-"`
InlinePolicies []InlinePolicy `json:"InlinePolicies,omitempty"`
Policies []string `json:"Policies,omitempty"`
}

func (g Group) ResourceType() string {
return "group"
}

type InlinePolicy struct {
Name string `yaml:"Name"`
Policy PolicyDocument `yaml:"Policy"`
Name string `json:"Name"`
Policy *PolicyDocument `json:"Policy"`
}

type Policy struct {
iamService `yaml:"-"`
Policy PolicyDocument `yaml:"Policy"`
iamService `json:"-"`
Policy *PolicyDocument `json:"Policy"`
}

func (p Policy) ResourceType() string {
return "policy"
}

type Role struct {
iamService `yaml:"-"`
AssumeRolePolicyDocument PolicyDocument `yaml:"AssumeRolePolicyDocument"`
InlinePolicies []InlinePolicy `yaml:"InlinePolicies,omitempty"`
Policies []string `yaml:"Policies,omitempty"`
iamService `json:"-"`
AssumeRolePolicyDocument *PolicyDocument `json:"AssumeRolePolicyDocument"`
InlinePolicies []InlinePolicy `json:"InlinePolicies,omitempty"`
Policies []string `json:"Policies,omitempty"`
}

func (r Role) ResourceType() string {
return "role"
}

type BucketPolicy struct {
BucketName string `yaml:"-"`
Policy PolicyDocument `yaml:"Policy"`
BucketName string `json:"-"`
Policy *PolicyDocument `json:"Policy"`
}

func (u BucketPolicy) Service() string {
Expand Down
19 changes: 1 addition & 18 deletions iamy/models_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
package iamy

import (
"fmt"
"testing"
)

func TestPolicyDocumentEncodingRoundTrip(t *testing.T) {
policy := PolicyDocument{
"foo": map[string]string{
"bar": "baz",
},
}
encodedPolicy := policy.Encode()
result, _ := NewPolicyDocumentFromEncodedJson(encodedPolicy)

if fmt.Sprintf("%v", result) != fmt.Sprintf("%v", policy) {
t.Errorf("PolicyDocument failed an Encode roundtrip, got %#v, expected %#v", result, policy)
}
}
import "testing"

func TestNewAccountFromString(t *testing.T) {

Expand Down
Loading

0 comments on commit c019000

Please sign in to comment.