-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from gdbranco/feat/ocm-5397
OCM-5397 | feat: add aws, account roles and oidc config common
- Loading branch information
Showing
20 changed files
with
565 additions
and
490 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package consts | ||
|
||
const ( | ||
MaxAwsRoleLength = 64 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package utils | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws/arn" | ||
) | ||
|
||
func GetPathFromArn(arnStr string) (string, error) { | ||
parse, err := arn.Parse(arnStr) | ||
if err != nil { | ||
return "", err | ||
} | ||
resource := parse.Resource | ||
firstIndex := strings.Index(resource, "/") | ||
lastIndex := strings.LastIndex(resource, "/") | ||
if firstIndex == lastIndex { | ||
return "", nil | ||
} | ||
path := resource[firstIndex : lastIndex+1] | ||
return path, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package utils_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestHelper(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "AWS Utils Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package utils_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
. "github.com/openshift-online/ocm-common/pkg/aws/utils" | ||
) | ||
|
||
var _ = Describe("AWS Utils", func() { | ||
var _ = Describe("Validates GetPathFromArn function", func() { | ||
It("Gets the path from arn", func() { | ||
path, err := GetPathFromArn( | ||
"arn:partition:service:region:account-id:resource-type/test-path/resource-id") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(path).To(Equal("/test-path/")) | ||
}) | ||
It("Retrieves empty when there's no path", func() { | ||
path, err := GetPathFromArn( | ||
"arn:partition:service:region:account-id:resource-type/resource-id") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(path).To(Equal("")) | ||
}) | ||
It("Errors if arn isn't valid", func() { | ||
_, err := GetPathFromArn("aaaa") | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package consts | ||
|
||
const ( | ||
rosa_prefix = "rosa_" | ||
CreatorArn = rosa_prefix + "creator_arn" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package consts | ||
|
||
const ( | ||
DefaultChannelGroup = "stable" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package utils_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestHelper(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "OCM Utils Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/openshift-online/ocm-common/pkg/ocm/consts" | ||
) | ||
|
||
func CreateVersionId(version string, channelGroup string) string { | ||
versionId := fmt.Sprintf("openshift-v%s", version) | ||
if channelGroup != consts.DefaultChannelGroup { | ||
versionId = fmt.Sprintf("%s-%s", versionId, channelGroup) | ||
} | ||
return versionId | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package utils_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/openshift-online/ocm-common/pkg/ocm/consts" | ||
. "github.com/openshift-online/ocm-common/pkg/ocm/utils" | ||
) | ||
|
||
var _ = Describe("OCM Utils", func() { | ||
var _ = Describe("Validates CreateVersionId function", func() { | ||
It("Generates the version ID on stable channel", func() { | ||
versionId := CreateVersionId("4.10.32", consts.DefaultChannelGroup) | ||
Expect(versionId).To(Equal("openshift-v4.10.32")) | ||
}) | ||
|
||
It("Generates the version ID on other channel", func() { | ||
versionId := CreateVersionId("4.10.32", "candidate") | ||
Expect(versionId).To(Equal("openshift-v4.10.32-candidate")) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package accountroles | ||
|
||
import ( | ||
awsUtils "github.com/openshift-online/ocm-common/pkg/aws/utils" | ||
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" | ||
) | ||
|
||
const ( | ||
InstallerAccountRole = "installer" | ||
ControlPlaneAccountRole = "instance_controlplane" | ||
WorkerAccountRole = "instance_worker" | ||
SupportAccountRole = "support" | ||
) | ||
|
||
type AccountRole struct { | ||
Name string | ||
Flag string | ||
} | ||
|
||
var AccountRoles = map[string]AccountRole{ | ||
InstallerAccountRole: {Name: "Installer", Flag: "role-arn"}, | ||
ControlPlaneAccountRole: {Name: "ControlPlane", Flag: "controlplane-iam-role"}, | ||
WorkerAccountRole: {Name: "Worker", Flag: "worker-iam-role"}, | ||
SupportAccountRole: {Name: "Support", Flag: "support-role-arn"}, | ||
} | ||
|
||
func GetPathFromAccountRole(cluster *cmv1.Cluster, roleNameSuffix string) (string, error) { | ||
accRoles := GetAccountRolesArnsMap(cluster) | ||
if accRoles[roleNameSuffix] == "" { | ||
return "", nil | ||
} | ||
return awsUtils.GetPathFromArn(accRoles[roleNameSuffix]) | ||
} | ||
|
||
func GetAccountRolesArnsMap(cluster *cmv1.Cluster) map[string]string { | ||
return map[string]string{ | ||
AccountRoles[InstallerAccountRole].Name: cluster.AWS().STS().RoleARN(), | ||
AccountRoles[SupportAccountRole].Name: cluster.AWS().STS().SupportRoleARN(), | ||
AccountRoles[ControlPlaneAccountRole].Name: cluster.AWS().STS().InstanceIAMRoles().MasterRoleARN(), | ||
AccountRoles[WorkerAccountRole].Name: cluster.AWS().STS().InstanceIAMRoles().WorkerRoleARN(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package accountroles_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestHelper(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Account Roles Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package accountroles_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
. "github.com/openshift-online/ocm-common/pkg/rosa/accountroles" | ||
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" | ||
) | ||
|
||
var _ = Describe("Account Role functions", func() { | ||
var _ = Describe("Validates GetPathFromAccountRole function", func() { | ||
It("Generates the version ID on stable channel", func() { | ||
fakeCluster, err := cmv1.NewCluster(). | ||
AWS( | ||
cmv1.NewAWS(). | ||
STS( | ||
cmv1.NewSTS(). | ||
RoleARN("arn:partition:service:region:account-id:resource-type/test-path/resource-id"). | ||
SupportRoleARN("support"). | ||
InstanceIAMRoles( | ||
cmv1.NewInstanceIAMRoles(). | ||
MasterRoleARN("controlplane"). | ||
WorkerRoleARN("worker"), | ||
), | ||
), | ||
).Build() | ||
Expect(err).ToNot(HaveOccurred()) | ||
path, err := GetPathFromAccountRole(fakeCluster, AccountRoles[InstallerAccountRole].Name) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(path).To(Equal("/test-path/")) | ||
}) | ||
}) | ||
|
||
var _ = Describe("Validates GetAccountRolesArnsMap function", func() { | ||
It("Checks the account roles are retrieved from a cluster", func() { | ||
fakeCluster, err := cmv1.NewCluster(). | ||
AWS( | ||
cmv1.NewAWS(). | ||
STS( | ||
cmv1.NewSTS(). | ||
RoleARN("installer"). | ||
SupportRoleARN("support"). | ||
InstanceIAMRoles( | ||
cmv1.NewInstanceIAMRoles(). | ||
MasterRoleARN("controlplane"). | ||
WorkerRoleARN("worker"), | ||
), | ||
), | ||
).Build() | ||
Expect(err).ToNot(HaveOccurred()) | ||
accRolesMap := GetAccountRolesArnsMap(fakeCluster) | ||
Expect(accRolesMap[AccountRoles[InstallerAccountRole].Name]).To(Equal("installer")) | ||
Expect(accRolesMap[AccountRoles[SupportAccountRole].Name]).To(Equal("support")) | ||
Expect(accRolesMap[AccountRoles[ControlPlaneAccountRole].Name]).To(Equal("controlplane")) | ||
Expect(accRolesMap[AccountRoles[WorkerAccountRole].Name]).To(Equal("worker")) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.