Skip to content

Commit

Permalink
add cloud connectors config
Browse files Browse the repository at this point in the history
  • Loading branch information
moukoublen committed Jan 30, 2025
1 parent 6eb24ec commit 34d5f72
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 12 deletions.
34 changes: 31 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ type CloudConfig struct {
}

type AwsConfig struct {
Cred aws.ConfigAWS `config:"credentials"`
AccountType string `config:"account_type"`
CloudConnectors bool `config:"supports_cloud_connectors"`
Cred aws.ConfigAWS `config:"credentials"`
AccountType string `config:"account_type"`
CloudConnectors bool `config:"supports_cloud_connectors"`
CloudConnectorsConfig CloudConnectorsConfig
}

type GcpConfig struct {
Expand Down Expand Up @@ -170,6 +171,10 @@ func New(cfg *config.C) (*Config, error) {
))
}

if c.CloudConfig.Aws.CloudConnectors {
c.CloudConfig.Aws.CloudConnectorsConfig = newCloudConnectorsConfig()
}

return c, nil
}

Expand Down Expand Up @@ -204,3 +209,26 @@ func isSupportedBenchmark(benchmark string) bool {
}
return false
}

// Cloud Connectors roles and resource id must be provided by the system (controller)
// and not user input (package policy) for security reasons.

const (
CloudConnectorsLocalRoleEnvVar = "CLOUD_CONNECTORS_LOCAL_ROLE"
CloudConnectorsGlobalRoleEnvVar = "CLOUD_CONNECTORS_GLOBAL_ROLE"
ResourceIDEnvVar = "RESOURCE_ID"
)

type CloudConnectorsConfig struct {
LocalRoleARN string
GlobalRoleARN string
ResourceID string
}

func newCloudConnectorsConfig() CloudConnectorsConfig {
return CloudConnectorsConfig{
LocalRoleARN: os.Getenv(CloudConnectorsLocalRoleEnvVar),
GlobalRoleARN: os.Getenv(CloudConnectorsGlobalRoleEnvVar),
ResourceID: os.Getenv(ResourceIDEnvVar),
}
}
124 changes: 115 additions & 9 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,25 @@ func (s *ConfigTestSuite) TestNew() {
expectedCloudConfig CloudConfig
}{
{
`
config: `
config:
v1:
benchmark: cis_k8s
`,
"cis_k8s",
CloudConfig{},
expectedType: "cis_k8s",
expectedCloudConfig: CloudConfig{},
},
{
`
config: `
config:
v1:
benchmark: cis_azure
`,
"cis_azure",
CloudConfig{},
expectedType: "cis_azure",
expectedCloudConfig: CloudConfig{},
},
{
`
config: `
config:
v1:
benchmark: cis_eks
Expand All @@ -79,8 +79,8 @@ config:
credential_profile_name: credential_profile_name
role_arn: role_arn
`,
"cis_eks",
CloudConfig{
expectedType: "cis_eks",
expectedCloudConfig: CloudConfig{
Aws: AwsConfig{
Cred: aws.ConfigAWS{
AccessKeyID: "key",
Expand Down Expand Up @@ -229,3 +229,109 @@ revision: 1`,
})
}
}

func (s *ConfigTestSuite) TestCloudConnectorsConfig() {
tests := map[string]struct {
config string
overwriteEnv func(t *testing.T)
expectedType string
expectedCloudConfig CloudConfig
}{
"happy path cloud connectors enabled": {
config: `
config:
v1:
benchmark: cis_aws
aws:
supports_cloud_connectors: true
credentials:
external_id: abc123
`,
expectedType: "cis_aws",
expectedCloudConfig: CloudConfig{
Aws: AwsConfig{
CloudConnectors: true,
Cred: aws.ConfigAWS{
ExternalID: "abc123",
},
CloudConnectorsConfig: CloudConnectorsConfig{},
},
},
},
"happy path cloud connectors enabled - attempt overwrite roles": {
config: `
config:
v1:
benchmark: cis_aws
aws:
account_type: single-account
supports_cloud_connectors: true
credentials:
external_id: abc123
CloudConnectorsConfig:
LocalRoleARN: "abc123"
LocalRoleARN: "abc123"
`,
expectedType: "cis_aws",
expectedCloudConfig: CloudConfig{
Aws: AwsConfig{
AccountType: SingleAccount,
CloudConnectors: true,
Cred: aws.ConfigAWS{
ExternalID: "abc123",
},
CloudConnectorsConfig: CloudConnectorsConfig{},
},
},
},
"happy path cloud connectors enabled - env vars set": {
config: `
config:
v1:
benchmark: cis_aws
aws:
account_type: single-account
supports_cloud_connectors: true
credentials:
external_id: abc123
`,
overwriteEnv: func(t *testing.T) {
t.Helper()
t.Setenv(CloudConnectorsLocalRoleEnvVar, "abc123")
t.Setenv(CloudConnectorsGlobalRoleEnvVar, "abc456")
t.Setenv(ResourceIDEnvVar, "abc789")
},
expectedType: "cis_aws",
expectedCloudConfig: CloudConfig{
Aws: AwsConfig{
AccountType: SingleAccount,
CloudConnectors: true,
Cred: aws.ConfigAWS{
ExternalID: "abc123",
},
CloudConnectorsConfig: CloudConnectorsConfig{
LocalRoleARN: "abc123",
GlobalRoleARN: "abc456",
ResourceID: "abc789",
},
},
},
},
}

for i, test := range tests {
s.Run(fmt.Sprint(i), func() {
if test.overwriteEnv != nil {
test.overwriteEnv(s.T())
}
cfg, err := config.NewConfigFrom(test.config)
s.Require().NoError(err)

c, err := New(cfg)
s.Require().NoError(err)

s.Equal(test.expectedType, c.Benchmark)
s.Equal(test.expectedCloudConfig, c.CloudConfig)
})
}
}

0 comments on commit 34d5f72

Please sign in to comment.