Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moukoublen committed Jan 30, 2025
1 parent ae9c259 commit 4cd6061
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
4 changes: 2 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func isSupportedBenchmark(benchmark string) bool {
const (
CloudConnectorsLocalRoleEnvVar = "CLOUD_CONNECTORS_LOCAL_ROLE"
CloudConnectorsGlobalRoleEnvVar = "CLOUD_CONNECTORS_GLOBAL_ROLE"
ResourceIDEnvVar = "RESOURCE_ID"
CloudResourceIDEnvVar = "CLOUD_RESOURCE_ID"
)

type CloudConnectorsConfig struct {
Expand All @@ -229,6 +229,6 @@ func newCloudConnectorsConfig() CloudConnectorsConfig {
return CloudConnectorsConfig{
LocalRoleARN: os.Getenv(CloudConnectorsLocalRoleEnvVar),
GlobalRoleARN: os.Getenv(CloudConnectorsGlobalRoleEnvVar),
ResourceID: os.Getenv(ResourceIDEnvVar),
ResourceID: os.Getenv(CloudResourceIDEnvVar),
}
}
2 changes: 1 addition & 1 deletion internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ config:
t.Helper()
t.Setenv(CloudConnectorsLocalRoleEnvVar, "abc123")
t.Setenv(CloudConnectorsGlobalRoleEnvVar, "abc456")
t.Setenv(ResourceIDEnvVar, "abc789")
t.Setenv(CloudResourceIDEnvVar, "abc789")
},
expectedType: "cis_aws",
expectedCloudConfig: CloudConfig{
Expand Down
51 changes: 51 additions & 0 deletions internal/flavors/benchmark/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ import (
"errors"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
libbeataws "github.com/elastic/beats/v7/x-pack/libbeat/common/aws"
"github.com/stretchr/testify/mock"

"github.com/elastic/cloudbeat/internal/config"
"github.com/elastic/cloudbeat/internal/dataprovider/providers/cloud"
"github.com/elastic/cloudbeat/internal/resources/fetching"
"github.com/elastic/cloudbeat/internal/resources/providers/awslib"
"github.com/elastic/cloudbeat/internal/resources/utils/testhelper"
Expand Down Expand Up @@ -60,6 +66,51 @@ func TestAWS_Initialize(t *testing.T) {
fetching.S3Type,
},
},
{
name: "cloud connectors",
cfg: config.Config{
Benchmark: "cis_aws",
CloudConfig: config.CloudConfig{
Aws: config.AwsConfig{
AccountType: config.SingleAccount,
Cred: libbeataws.ConfigAWS{},
CloudConnectors: true,
CloudConnectorsConfig: config.CloudConnectorsConfig{
LocalRoleARN: "abc123",
GlobalRoleARN: "abc456",
ResourceID: "abc789",
},
},
},
},
identityProvider: func() awslib.IdentityProviderGetter {
cfgMatcher := mock.MatchedBy(func(cfg aws.Config) bool {
c, is := cfg.Credentials.(*aws.CredentialsCache)
if !is {
return false
}
return c.IsCredentialsProvider(&stscreds.AssumeRoleProvider{})
})
identityProvider := &awslib.MockIdentityProviderGetter{}
identityProvider.EXPECT().GetIdentity(mock.Anything, cfgMatcher).Return(
&cloud.Identity{
Account: "test-account",
},
nil,
)

return identityProvider
}(),
want: []string{
fetching.IAMType,
fetching.KmsType,
fetching.TrailType,
fetching.AwsMonitoringType,
fetching.EC2NetworkingType,
fetching.RdsType,
fetching.S3Type,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 4cd6061

Please sign in to comment.