Skip to content

Commit

Permalink
OCM-2373 | feat: Moved associated to GetRole function to common
Browse files Browse the repository at this point in the history
  • Loading branch information
den-rgb committed Oct 16, 2023
1 parent f1f61c8 commit 51d4b35
Show file tree
Hide file tree
Showing 8 changed files with 336 additions and 1 deletion.
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ require (
)

require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
)

require (
github.com/aws/aws-sdk-go v1.45.25
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/openshift/rosa v1.2.27
github.com/stretchr/testify v1.7.0 // indirect
github.com/zgalor/weberr v0.8.2
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/text v0.9.0 // indirect
Expand Down
43 changes: 42 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/aws/aws-sdk-go v1.45.25 h1:c4fLlh5sLdK2DCRTY1z0hyuJZU4ygxX8m1FswL6/nF4=
github.com/aws/aws-sdk-go v1.45.25/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand All @@ -16,29 +18,68 @@ github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLe
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/onsi/ginkgo/v2 v2.11.0 h1:WgqUCUt/lT6yXoQ8Wef0fsNn5cAuMK7+KT9UFRz2tcU=
github.com/onsi/ginkgo/v2 v2.11.0/go.mod h1:ZhrRA5XmEE3x3rhlzamx/JJvujdZoJ2uvgI7kR0iZvM=
github.com/onsi/gomega v1.27.8 h1:gegWiwZjBsf2DgiSbf5hpokZ98JVDMcWkUiigk6/KXc=
github.com/onsi/gomega v1.27.8/go.mod h1:2J8vzI/s+2shY9XHRApDkdgPo1TKT7P2u6fXeJKFnNQ=
github.com/openshift/rosa v1.2.27 h1:xKecvmwBIHJ3s5jO8yXyTYr0mJ+HlRDq+AT7MW0xAIk=
github.com/openshift/rosa v1.2.27/go.mod h1:syBOBLGLFEYClxnuvosPe+nuawI+QxbXyoNYGzwmtAw=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zgalor/weberr v0.8.2 h1:rzGP0jQVt8hGSNnzjDAQNHMxNNrf3gUrYhpSgY76+mk=
github.com/zgalor/weberr v0.8.2/go.mod h1:cqK89mj84q3PRgqQXQFWJDzCorOd8xOtov/ulOnqDwc=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.9.3 h1:Gn1I8+64MsuTb/HpH+LmQtNas23LhUVr3rYZ0eKuaMM=
golang.org/x/tools v0.9.3/go.mod h1:owI94Op576fPu3cIGQeHs3joujW/2Oc6MtlxbF5dfNc=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
60 changes: 60 additions & 0 deletions pkg/aws/validations/getRole.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package validations

import (
"fmt"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/aws"
"github.com/openshift/rosa/pkg/aws/tags"
semver "github.com/hashicorp/go-version"
)

func GetRoleName(prefix string, role string) string {
name := fmt.Sprintf("%s-%s-Role", prefix, role)
if len(name) > 64 {
name = name[0:64]
}
return name
}

func IsManagedRole(roleTags []*iam.Tag) bool {
for _, tag := range roleTags {
if aws.StringValue(tag.Key) == tags.ManagedPolicies && aws.StringValue(tag.Value) == "true" {
return true
}
}

return false
}

func HasCompatibleVersionTags(iamTags []*iam.Tag, version string) (bool, error) {
if len(iamTags) == 0 {
return false, nil
}
for _, tag := range iamTags {
if aws.StringValue(tag.Key) == tags.OpenShiftVersion {
if version == aws.StringValue(tag.Value) {
return true, nil
}
wantedVersion, err := semver.NewVersion(version)
if err != nil {
return false, err
}
currentVersion, err := semver.NewVersion(aws.StringValue(tag.Value))
if err != nil {
return false, err
}
return currentVersion.GreaterThanOrEqual(wantedVersion), nil
}
}
return false, nil
}

func IamResourceHasTag(iamTags []*iam.Tag, tagKey string, tagValue string) bool {
for _, tag := range iamTags {
if aws.StringValue(tag.Key) == tagKey && aws.StringValue(tag.Value) == tagValue {
return true
}
}

return false
}
145 changes: 145 additions & 0 deletions pkg/aws/validations/getRole_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package validations

import (
"fmt"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/aws"
"github.com/openshift/rosa/pkg/aws/tags"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("AWS IAM Functions", func() {
Describe("GetRoleName", func() {
It("should generate a role name with the given prefix and role name", func() {
prefix := "myPrefix"
roleName := "myRole"
expectedName := fmt.Sprintf("%s-%s-Role", prefix, roleName)

name := GetRoleName(prefix, roleName)

Expect(name).To(Equal(expectedName))
})

It("should truncate the generated name if it exceeds 64 characters", func() {
prefix := "myPrefix"
roleName := "myVeryLongRoleNameThatExceedsSixtyFourCharacters123456"
expectedName := "myPrefix-myVeryLongRoleNameThatExceedsSixtyFourCharacters123456-"

name := GetRoleName(prefix, roleName)

Expect(name).To(Equal(expectedName))
})
})

Describe("isManagedRole", func() {
It("should return true if the 'ManagedPolicies' tag has the value 'true'", func() {
roleTags := []*iam.Tag{
&iam.Tag{Key: aws.String(tags.ManagedPolicies), Value: aws.String("true")},
}

result := IsManagedRole(roleTags)

Expect(result).To(BeTrue())
})

It("should return false if the 'ManagedPolicies' tag does not have the value 'true'", func() {
roleTags := []*iam.Tag{
&iam.Tag{Key: aws.String(tags.ManagedPolicies), Value: aws.String("false")},
}

result := IsManagedRole(roleTags)

Expect(result).To(BeFalse())
})

It("should return false if the 'ManagedPolicies' tag is not present", func() {
roleTags := []*iam.Tag{
&iam.Tag{Key: aws.String("SomeOtherTag"), Value: aws.String("true")},
}

result := IsManagedRole(roleTags)

Expect(result).To(BeFalse())
})
})

var _ = Describe("HasCompatibleVersionTags", func() {
var iamTags []*iam.Tag

BeforeEach(func() {
iamTags = []*iam.Tag{
&iam.Tag{Key: aws.String(tags.OpenShiftVersion), Value: aws.String("1.2.3")},
&iam.Tag{Key: aws.String("SomeOtherTag"), Value: aws.String("value")},
}
})

It("should return true if the version tag matches the provided version", func() {
version := "1.2.3"

result, err := HasCompatibleVersionTags(iamTags, version)

Expect(result).To(BeTrue())
Expect(err).To(BeNil())
})

It("should return false if the version tag does not match the provided version", func() {
version := "2.0.0"

result, err := HasCompatibleVersionTags(iamTags, version)

Expect(result).To(BeFalse())
Expect(err).To(BeNil())
})

It("should return false if the version tag is not present", func() {
version := "1.2.3"
iamTags = []*iam.Tag{
&iam.Tag{Key: aws.String("SomeOtherTag"), Value: aws.String("value")},
}

result, err := HasCompatibleVersionTags(iamTags, version)

Expect(result).To(BeFalse())
Expect(err).To(BeNil())
})

It("should return an error if the provided version is not a valid semantic version", func() {
version := "invalid-version"

result, err := HasCompatibleVersionTags(iamTags, version)

Expect(result).To(BeFalse())
Expect(err).ToNot(BeNil())
})
})

var _ = Describe("IamResourceHasTag", func() {
It("should return true if the tag with the specified key and value exists", func() {
iamTags := []*iam.Tag{
&iam.Tag{Key: aws.String("Tag1"), Value: aws.String("Value1")},
&iam.Tag{Key: aws.String("Tag2"), Value: aws.String("Value2")},
}
tagKey := "Tag1"
tagValue := "Value1"

result := IamResourceHasTag(iamTags, tagKey, tagValue)

Expect(result).To(BeTrue())
})

It("should return false if the tag with the specified key and value does not exist", func() {
iamTags := []*iam.Tag{
&iam.Tag{Key: aws.String("Tag1"), Value: aws.String("Value1")},
&iam.Tag{Key: aws.String("Tag2"), Value: aws.String("Value2")},
}
tagKey := "Tag3"
tagValue := "Value3"

result := IamResourceHasTag(iamTags, tagKey, tagValue)

Expect(result).To(BeFalse())
})
})
})
13 changes: 13 additions & 0 deletions pkg/aws/validations/validation_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package validations

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestValidation(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Validations Suite")
}
25 changes: 25 additions & 0 deletions pkg/ocm/validations/getRole.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package validations

import (
"net/url"
"strings"

errors "github.com/zgalor/weberr"
)

func ValidateIssuerUrlMatchesAssumePolicyDocument(
roleArn string, parsedUrl *url.URL, assumePolicyDocument string) error {
issuerUrl := parsedUrl.Host
if parsedUrl.Path != "" {
issuerUrl += parsedUrl.Path
}
decodedAssumePolicyDocument, err := url.QueryUnescape(assumePolicyDocument)
if err != nil {
return err
}
if !strings.Contains(decodedAssumePolicyDocument, issuerUrl) {
return errors.Errorf("Operator role '%s' does not have trusted relationship to '%s' issuer URL",
roleArn, issuerUrl)
}
return nil
}
30 changes: 30 additions & 0 deletions pkg/ocm/validations/getRole_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package validations

import (
"net/url"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("validateIssuerUrlMatchesAssumePolicyDocument", func() {
It("should return nil if the issuer URL is present in the assume policy document", func() {
roleArn := "arn:aws:iam::123456789012:role/OperatorRole"
parsedUrl, _ := url.Parse("https://example.com/path")
assumePolicyDocument := "some document with https://example.com/path in it"

err := ValidateIssuerUrlMatchesAssumePolicyDocument(roleArn, parsedUrl, assumePolicyDocument)

Expect(err).To(BeNil())
})

It("should return an error if the issuer URL is not present in the assume policy document", func() {
roleArn := "arn:aws:iam::123456789012:role/OperatorRole"
parsedUrl, _ := url.Parse("https://example.com/path")
assumePolicyDocument := "some document without the issuer URL"

err := ValidateIssuerUrlMatchesAssumePolicyDocument(roleArn, parsedUrl, assumePolicyDocument)

Expect(err).To(MatchError("Operator role 'arn:aws:iam::123456789012:role/OperatorRole' does not have trusted relationship to 'example.com/path' issuer URL"))
})
})
13 changes: 13 additions & 0 deletions pkg/ocm/validations/validation_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package validations

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestValidations(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Validations Suite")
}

0 comments on commit 51d4b35

Please sign in to comment.