-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
366 additions
and
46 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
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,83 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package aws | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/elastic/elastic-agent-libs/logp" | ||
"github.com/samber/lo" | ||
|
||
"github.com/elastic/cloudbeat/internal/dataprovider/providers/cloud" | ||
"github.com/elastic/cloudbeat/internal/inventory" | ||
"github.com/elastic/cloudbeat/internal/resources/providers/awslib" | ||
"github.com/elastic/cloudbeat/internal/resources/providers/awslib/s3" | ||
) | ||
|
||
type S3BucketFetcher struct { | ||
logger *logp.Logger | ||
provider s3BucketProvider | ||
} | ||
|
||
var s3BucketClassification = inventory.AssetClassification{ | ||
Category: inventory.CategoryInfrastructure, | ||
SubCategory: inventory.SubCategoryStorage, | ||
Type: inventory.TypeObjectStorage, | ||
SubStype: inventory.SubTypeS3, | ||
} | ||
|
||
type s3BucketProvider interface { | ||
DescribeBuckets(ctx context.Context) ([]awslib.AwsResource, error) | ||
} | ||
|
||
func NewS3BucketFetcher(logger *logp.Logger, identity *cloud.Identity, cfg aws.Config) inventory.AssetFetcher { | ||
provider := s3.NewProvider(logger, cfg, &awslib.MultiRegionClientFactory[s3.Client]{}, identity.Account) | ||
return &S3BucketFetcher{ | ||
logger: logger, | ||
provider: provider, | ||
} | ||
} | ||
|
||
func (s S3BucketFetcher) Fetch(ctx context.Context, assetChannel chan<- inventory.AssetEvent) { | ||
awsBuckets, err := s.provider.DescribeBuckets(ctx) | ||
if err != nil { | ||
s.logger.Errorf("Could not list s3 buckets: %v", err) | ||
if len(awsBuckets) == 0 { | ||
return | ||
} | ||
} | ||
|
||
buckets := lo.Map(awsBuckets, func(item awslib.AwsResource, _ int) s3.BucketDescription { | ||
return item.(s3.BucketDescription) | ||
}) | ||
|
||
for _, bucket := range buckets { | ||
assetChannel <- inventory.NewAssetEvent( | ||
s3BucketClassification, | ||
bucket.GetResourceArn(), | ||
bucket.GetResourceName(), | ||
|
||
inventory.WithRawAsset(bucket), | ||
inventory.WithCloud(inventory.AssetCloud{ | ||
Provider: inventory.AwsCloudProvider, | ||
Region: bucket.GetRegion(), | ||
}), | ||
) | ||
} | ||
} |
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,124 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package aws | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
s3types "github.com/aws/aws-sdk-go-v2/service/s3/types" | ||
s3ctrltypes "github.com/aws/aws-sdk-go-v2/service/s3control/types" | ||
"github.com/elastic/elastic-agent-libs/logp" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
|
||
"github.com/elastic/cloudbeat/internal/inventory" | ||
"github.com/elastic/cloudbeat/internal/resources/providers/awslib" | ||
"github.com/elastic/cloudbeat/internal/resources/providers/awslib/s3" | ||
"github.com/elastic/cloudbeat/internal/resources/utils/pointers" | ||
) | ||
|
||
func TestS3BucketFetcher_Fetch(t *testing.T) { | ||
bucket1 := s3.BucketDescription{ | ||
Name: "bucket-1", | ||
SSEAlgorithm: nil, | ||
BucketPolicy: nil, | ||
BucketVersioning: &s3.BucketVersioning{ | ||
Enabled: true, | ||
MfaDelete: true, | ||
}, | ||
PublicAccessBlockConfiguration: &s3types.PublicAccessBlockConfiguration{ | ||
BlockPublicAcls: pointers.Ref(true), | ||
}, | ||
AccountPublicAccessBlockConfiguration: &s3ctrltypes.PublicAccessBlockConfiguration{ | ||
BlockPublicAcls: pointers.Ref(true), | ||
}, | ||
Region: "europe-west-1", | ||
} | ||
|
||
bucket2 := s3.BucketDescription{ | ||
Name: "bucket-2", | ||
SSEAlgorithm: nil, | ||
BucketPolicy: nil, | ||
BucketVersioning: &s3.BucketVersioning{ | ||
Enabled: false, | ||
MfaDelete: false, | ||
}, | ||
PublicAccessBlockConfiguration: &s3types.PublicAccessBlockConfiguration{ | ||
BlockPublicAcls: pointers.Ref(false), | ||
}, | ||
AccountPublicAccessBlockConfiguration: &s3ctrltypes.PublicAccessBlockConfiguration{ | ||
BlockPublicAcls: pointers.Ref(false), | ||
}, | ||
Region: "europe-west-1", | ||
} | ||
in := []awslib.AwsResource{bucket1, bucket2} | ||
|
||
expected := []inventory.AssetEvent{ | ||
inventory.NewAssetEvent( | ||
s3BucketClassification, | ||
"arn:aws:s3:::bucket-1", | ||
"bucket-1", | ||
inventory.WithRawAsset(bucket1), | ||
inventory.WithCloud(inventory.AssetCloud{ | ||
Provider: inventory.AwsCloudProvider, | ||
Region: "europe-west-1", | ||
}), | ||
), | ||
inventory.NewAssetEvent( | ||
s3BucketClassification, | ||
"arn:aws:s3:::bucket-2", | ||
"bucket-2", | ||
inventory.WithRawAsset(bucket2), | ||
inventory.WithCloud(inventory.AssetCloud{ | ||
Provider: inventory.AwsCloudProvider, | ||
Region: "europe-west-1", | ||
}), | ||
), | ||
} | ||
|
||
logger := logp.NewLogger("test_fetcher_s3_bucket") | ||
provider := newMockS3BucketProvider(t) | ||
provider.EXPECT().DescribeBuckets(mock.Anything).Return(in, nil) | ||
|
||
fetcher := S3BucketFetcher{ | ||
logger: logger, | ||
provider: provider, | ||
} | ||
|
||
ch := make(chan inventory.AssetEvent) | ||
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) | ||
defer cancel() | ||
go func() { | ||
fetcher.Fetch(ctx, ch) | ||
}() | ||
|
||
received := make([]inventory.AssetEvent, 0, len(expected)) | ||
for len(expected) != len(received) { | ||
select { | ||
case <-ctx.Done(): | ||
assert.ElementsMatch(t, expected, received) | ||
return | ||
case event := <-ch: | ||
received = append(received, event) | ||
} | ||
} | ||
|
||
assert.ElementsMatch(t, expected, received) | ||
} |
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
Oops, something went wrong.