From 6cde1bd08fc26c42e568917225aa35f5d5f0803d Mon Sep 17 00:00:00 2001 From: Kostas Stamatakis Date: Thu, 30 Jan 2025 14:29:53 +0200 Subject: [PATCH] add tests --- internal/config/config.go | 4 +- internal/config/config_test.go | 2 +- internal/flavors/benchmark/aws_test.go | 51 ++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index bddddec5c9..0c24ee235d 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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 { @@ -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), } } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index f6333efed0..b980357a42 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -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{ diff --git a/internal/flavors/benchmark/aws_test.go b/internal/flavors/benchmark/aws_test.go index dd581fca71..9ca7869612 100644 --- a/internal/flavors/benchmark/aws_test.go +++ b/internal/flavors/benchmark/aws_test.go @@ -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" @@ -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) {