Skip to content

Commit

Permalink
Add region to all CIS AWS resources (elastic#883)
Browse files Browse the repository at this point in the history
  • Loading branch information
ari-aviran authored Apr 24, 2023
1 parent e18f1ec commit 57143d4
Show file tree
Hide file tree
Showing 30 changed files with 165 additions and 15 deletions.
1 change: 1 addition & 0 deletions resources/fetchers/iam_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (r IAMResource) GetMetadata() (fetching.ResourceMetadata, error) {
Type: fetching.CloudIdentity,
SubType: r.GetResourceType(),
Name: r.GetResourceName(),
Region: r.GetRegion(),
}, nil
}
func (r IAMResource) GetElasticCommonData() any { return nil }
1 change: 1 addition & 0 deletions resources/fetchers/kms_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (r KmsResource) GetMetadata() (fetching.ResourceMetadata, error) {
Type: fetching.KeyManagement,
SubType: r.key.GetResourceType(),
Name: r.key.GetResourceName(),
Region: r.key.GetRegion(),
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions resources/fetchers/logging_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (r LoggingResource) GetMetadata() (fetching.ResourceMetadata, error) {
Type: fetching.CloudAudit,
SubType: r.GetResourceType(),
Name: r.GetResourceName(),
Region: r.GetRegion(),
}, nil
}
func (r LoggingResource) GetElasticCommonData() any { return nil }
Expand Down
2 changes: 2 additions & 0 deletions resources/fetchers/monitoring_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func (r MonitoringResource) GetMetadata() (fetching.ResourceMetadata, error) {
Type: fetching.MonitoringIdentity,
SubType: fetching.MultiTrailsType,
Name: id,
Region: awslib.GlobalRegion,
}, nil
}
func (r MonitoringResource) GetElasticCommonData() any { return nil }
Expand All @@ -108,6 +109,7 @@ func (s SecurityHubResource) GetMetadata() (fetching.ResourceMetadata, error) {
Name: s.GetResourceName(),
Type: fetching.MonitoringIdentity,
SubType: fetching.SecurityHubType,
Region: s.GetRegion(),
}, nil
}

Expand Down
4 changes: 4 additions & 0 deletions resources/fetchers/monitoring_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func TestMonitoringResource_GetMetadata(t *testing.T) {
Name: "cloudtrail-aws-account-id",
Type: fetching.MonitoringIdentity,
SubType: fetching.MultiTrailsType,
Region: awslib.GlobalRegion,
},
},
{
Expand All @@ -178,6 +179,7 @@ func TestMonitoringResource_GetMetadata(t *testing.T) {
Name: "cloudtrail-aws-account-id",
Type: fetching.MonitoringIdentity,
SubType: fetching.MultiTrailsType,
Region: awslib.GlobalRegion,
},
},
}
Expand Down Expand Up @@ -227,6 +229,7 @@ func TestSecurityHubResource_GetMetadata(t *testing.T) {
Name: "securityhub-us-east-1-" + accountId,
Type: fetching.MonitoringIdentity,
SubType: fetching.SecurityHubType,
Region: "us-east-1",
},
},
{
Expand All @@ -243,6 +246,7 @@ func TestSecurityHubResource_GetMetadata(t *testing.T) {
Name: "securityhub-us-east-2-" + accountId,
Type: fetching.MonitoringIdentity,
SubType: fetching.SecurityHubType,
Region: "us-east-2",
},
},
}
Expand Down
1 change: 1 addition & 0 deletions resources/fetchers/network_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (r NetworkResource) GetMetadata() (fetching.ResourceMetadata, error) {
Type: fetching.EC2Identity,
SubType: r.GetResourceType(),
Name: r.GetResourceName(),
Region: r.GetRegion(),
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions resources/fetchers/rds_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (r RdsResource) GetMetadata() (fetching.ResourceMetadata, error) {
Type: fetching.CloudDatabase,
SubType: r.dbInstance.GetResourceType(),
Name: r.dbInstance.GetResourceName(),
Region: r.dbInstance.GetRegion(),
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions resources/fetchers/s3_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (r S3Resource) GetMetadata() (fetching.ResourceMetadata, error) {
Type: fetching.CloudStorage,
SubType: r.bucket.GetResourceType(),
Name: r.bucket.GetResourceName(),
Region: r.bucket.GetRegion(),
}, nil
}

Expand Down
7 changes: 7 additions & 0 deletions resources/providers/aws_cis/logging/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,10 @@ func (e EnrichedTrail) GetResourceName() string {
func (e EnrichedTrail) GetResourceType() string {
return fetching.TrailType
}

func (e EnrichedTrail) GetRegion() string {
if e.Trail.HomeRegion == nil {
return ""
}
return *e.Trail.HomeRegion
}
6 changes: 5 additions & 1 deletion resources/providers/awslib/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import (
awssdk "github.com/aws/aws-sdk-go-v2/aws"
)

const DefaultRegion = "us-east-1"
const (
DefaultRegion = "us-east-1"
GlobalRegion = "global"
)

var ErrClientNotFound = errors.New("aws client not found")

Expand All @@ -35,6 +38,7 @@ type AwsResource interface {
GetResourceArn() string
GetResourceName() string
GetResourceType() string
GetRegion() string
}

func GetClient[T any](region *string, list map[string]T) (T, error) {
Expand Down
4 changes: 4 additions & 0 deletions resources/providers/awslib/configservice/configservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,7 @@ func (c Config) GetResourceName() string {
func (c Config) GetResourceType() string {
return fetching.ConfigServiceResourceType
}

func (c Config) GetRegion() string {
return c.region
}
4 changes: 4 additions & 0 deletions resources/providers/awslib/ec2/ebs_encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ func (e EBSEncryption) GetResourceName() string {
func (e EBSEncryption) GetResourceType() string {
return fetching.EBSType
}

func (e EBSEncryption) GetRegion() string {
return e.region
}
4 changes: 4 additions & 0 deletions resources/providers/awslib/ec2/nacl.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ func (r NACLInfo) GetResourceName() string {
func (r NACLInfo) GetResourceType() string {
return fetching.NetworkNACLType
}

func (r NACLInfo) GetRegion() string {
return r.region
}
4 changes: 4 additions & 0 deletions resources/providers/awslib/ec2/security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ func (s SecurityGroup) GetResourceName() string {
func (s SecurityGroup) GetResourceType() string {
return fetching.SecurityGroupType
}

func (s SecurityGroup) GetRegion() string {
return s.region
}
4 changes: 4 additions & 0 deletions resources/providers/awslib/ec2/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ func (v VpcInfo) GetResourceName() string {
func (v VpcInfo) GetResourceType() string {
return fetching.VpcType
}

func (v VpcInfo) GetRegion() string {
return v.region
}
4 changes: 4 additions & 0 deletions resources/providers/awslib/iam/password_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ func (p PasswordPolicy) GetResourceName() string {
func (p PasswordPolicy) GetResourceType() string {
return fetching.PwdPolicyType
}

func (p PasswordPolicy) GetRegion() string {
return awslib.GlobalRegion
}
4 changes: 4 additions & 0 deletions resources/providers/awslib/iam/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ func (p Policy) GetResourceType() string {
return fetching.PolicyType
}

func (p Policy) GetRegion() string {
return awslib.GlobalRegion
}

func stringOrEmpty(s *string) string {
if s == nil {
return ""
Expand Down
4 changes: 4 additions & 0 deletions resources/providers/awslib/iam/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ func (u User) GetResourceType() string {
return fetching.IAMUserType
}

func (u User) GetRegion() string {
return awslib.GlobalRegion
}

func (p Provider) listUsers(ctx context.Context) ([]types.User, error) {
p.log.Debug("IAMProvider.getUsers")
var nativeUsers []types.User
Expand Down
1 change: 1 addition & 0 deletions resources/providers/awslib/kms/kms.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
type KmsInfo struct {
KeyMetadata types.KeyMetadata `json:"key_metadata"`
KeyRotationEnabled bool `json:"key_rotation_enabled"`
region string
}

type KMS interface {
Expand Down
5 changes: 5 additions & 0 deletions resources/providers/awslib/kms/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func (p *Provider) DescribeSymmetricKeys(ctx context.Context) ([]awslib.AwsResou
result = append(result, KmsInfo{
KeyMetadata: *keyInfo.KeyMetadata,
KeyRotationEnabled: rotationStatus.KeyRotationEnabled,
region: region,
})
}
return result, nil
Expand All @@ -107,3 +108,7 @@ func (k KmsInfo) GetResourceName() string {
func (k KmsInfo) GetResourceType() string {
return fetching.KmsType
}

func (k KmsInfo) GetRegion() string {
return k.region
}
19 changes: 10 additions & 9 deletions resources/providers/awslib/kms/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
kmsClient "github.com/aws/aws-sdk-go-v2/service/kms"
"github.com/aws/aws-sdk-go-v2/service/kms/types"
"github.com/elastic/cloudbeat/resources/providers/awslib"
"github.com/elastic/cloudbeat/resources/utils/testhelper"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -58,7 +57,7 @@ func (s *ProviderTestSuite) TearDownTest() {}
var keyId1 = "21c0ba99-3a6c-4f72-8ef8-8118d4804710"
var keyId2 = "21c0ba99-3a6c-4f72-8ef8-8118d4804711"

func (s *ProviderTestSuite) TestProvider_DescribeBuckets() {
func (s *ProviderTestSuite) TestProvider_DescribeSymmetricKeys() {
var tests = []struct {
name string
regions []string
Expand Down Expand Up @@ -142,25 +141,27 @@ func (s *ProviderTestSuite) TestProvider_DescribeBuckets() {
},
},
expected: []awslib.AwsResource{
KmsInfo{KeyMetadata: types.KeyMetadata{KeyId: &keyId1, KeySpec: types.KeySpecSymmetricDefault}, KeyRotationEnabled: true},
KmsInfo{KeyMetadata: types.KeyMetadata{KeyId: &keyId2, KeySpec: types.KeySpecSymmetricDefault}, KeyRotationEnabled: true},
KmsInfo{KeyMetadata: types.KeyMetadata{KeyId: &keyId1, KeySpec: types.KeySpecSymmetricDefault}, KeyRotationEnabled: true, region: "us-east-1"},
KmsInfo{KeyMetadata: types.KeyMetadata{KeyId: &keyId2, KeySpec: types.KeySpecSymmetricDefault}, KeyRotationEnabled: true, region: "us-east-2"},
},
expectError: false,
regions: []string{"us-east-1", "us-east-2"},
},
}

for _, test := range tests {
kmsClientMock := &MockClient{}
for funcName, returnVals := range test.kmsClientMockReturnVals {
for _, vals := range returnVals {
kmsClientMock.On(funcName, vals[0]...).Return(vals[1]...).Once()
mockClients := make(map[string]Client, len(test.regions))
for i, region := range test.regions {
kmsClientMock := &MockClient{}
for funcName, returnVals := range test.kmsClientMockReturnVals {
kmsClientMock.On(funcName, returnVals[i][0]...).Return(returnVals[i][1]...)
}
mockClients[region] = kmsClientMock
}

kmsProvider := Provider{
log: s.log,
clients: testhelper.CreateMockClients[Client](kmsClientMock, test.regions),
clients: mockClients,
}

ctx := context.Background()
Expand Down
41 changes: 41 additions & 0 deletions resources/providers/awslib/mock_aws_resource.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions resources/providers/awslib/multi_region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ func (t testAwsResource) GetResourceName() string { return "" }

func (t testAwsResource) GetResourceType() string { return "" }

func (t testAwsResource) GetRegion() string { return "" }

func (d dummyTester) DummyFunc() ([]AwsResource, error) {
awsRes := []AwsResource{testAwsResource{resRegion: d.region}}
switch d.region {
Expand Down
5 changes: 5 additions & 0 deletions resources/providers/awslib/rds/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (p Provider) DescribeDBInstances(ctx context.Context) ([]awslib.AwsResource
AutoMinorVersionUpgrade: dbInstance.AutoMinorVersionUpgrade,
PubliclyAccessible: dbInstance.PubliclyAccessible,
Subnets: subnets,
region: region,
})
}

Expand Down Expand Up @@ -105,3 +106,7 @@ func (d DBInstance) GetResourceName() string {
func (d DBInstance) GetResourceType() string {
return fetching.RdsType
}

func (d DBInstance) GetRegion() string {
return d.region
}
23 changes: 21 additions & 2 deletions resources/providers/awslib/rds/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,27 @@ func (s *ProviderTestSuite) TestProvider_DescribeDBInstances() {
{ec2types.RouteTable{RouteTableId: &identifier, Routes: []ec2types.Route{{DestinationCidrBlock: &destinationCidrBlock, GatewayId: &gatewayId}}}, nil},
},
expected: []awslib.AwsResource{
DBInstance{Identifier: identifier, Arn: arn, StorageEncrypted: false, AutoMinorVersionUpgrade: false, PubliclyAccessible: false, Subnets: []Subnet(nil)},
DBInstance{Identifier: identifier2, Arn: arn2, StorageEncrypted: true, AutoMinorVersionUpgrade: true, PubliclyAccessible: true, Subnets: []Subnet{{ID: identifier, RouteTable: nil}, {ID: identifier2, RouteTable: &RouteTable{ID: identifier, Routes: []Route{{DestinationCidrBlock: &destinationCidrBlock, GatewayId: &gatewayId}}}}}},
DBInstance{
Identifier: identifier,
Arn: arn,
StorageEncrypted: false,
AutoMinorVersionUpgrade: false,
PubliclyAccessible: false,
Subnets: []Subnet(nil),
region: awslib.DefaultRegion,
},
DBInstance{
Identifier: identifier2,
Arn: arn2,
StorageEncrypted: true,
AutoMinorVersionUpgrade: true,
PubliclyAccessible: true, Subnets: []Subnet{
{ID: identifier, RouteTable: nil},
{ID: identifier2, RouteTable: &RouteTable{
ID: identifier,
Routes: []Route{{DestinationCidrBlock: &destinationCidrBlock, GatewayId: &gatewayId}},
}}},
region: awslib.DefaultRegion},
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions resources/providers/awslib/rds/rds.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ package rds

import (
"context"
"github.com/elastic/cloudbeat/resources/providers/awslib/ec2"

"github.com/aws/aws-sdk-go-v2/service/rds"
"github.com/elastic/cloudbeat/resources/providers/awslib"
"github.com/elastic/cloudbeat/resources/providers/awslib/ec2"
"github.com/elastic/elastic-agent-libs/logp"
)

Expand All @@ -33,6 +32,7 @@ type DBInstance struct {
AutoMinorVersionUpgrade bool `json:"auto_minor_version_upgrade"`
PubliclyAccessible bool `json:"publicly_accessible"`
Subnets []Subnet `json:"subnets"`
region string
}

type Subnet struct {
Expand Down
Loading

0 comments on commit 57143d4

Please sign in to comment.