From 821947ecfeae649921e656eb6e9ade68ee54d75e Mon Sep 17 00:00:00 2001 From: kruskall <99559985+kruskall@users.noreply.github.com> Date: Thu, 10 Oct 2024 08:56:57 +0200 Subject: [PATCH] feat: replace x/exp with stdlib functions (#2580) --- go.mod | 2 +- .../fetching/fetchers/azure/assets_fetcher.go | 5 +++-- .../fetching/fetchers/azure/assets_fetcher_test.go | 4 ++-- .../resources/fetching/fetchers/azure/batch_fetcher.go | 5 +++-- .../fetching/fetchers/azure/batch_fetcher_test.go | 10 +++++----- internal/vulnerability/events_creator.go | 5 +++-- scripts/update_assets_md/go.mod | 1 - scripts/update_assets_md/go.sum | 2 -- scripts/update_assets_md/main.go | 8 +++----- tools/tools.go | 1 - 10 files changed, 20 insertions(+), 23 deletions(-) diff --git a/go.mod b/go.mod index 670dc12abf..9a51457d0e 100644 --- a/go.mod +++ b/go.mod @@ -75,7 +75,6 @@ require ( go.uber.org/goleak v1.3.0 go.uber.org/zap v1.27.0 go.uber.org/zap/exp v0.1.1-0.20240530135403-273f9ea8c887 - golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 golang.org/x/oauth2 v0.23.0 google.golang.org/api v0.199.0 @@ -161,6 +160,7 @@ require ( go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.29.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.29.0 // indirect + golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect golang.org/x/tools v0.25.0 // indirect gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect gopkg.in/go-jose/go-jose.v2 v2.6.3 // indirect diff --git a/internal/resources/fetching/fetchers/azure/assets_fetcher.go b/internal/resources/fetching/fetchers/azure/assets_fetcher.go index f668a37d96..ee2074eaf6 100644 --- a/internal/resources/fetching/fetchers/azure/assets_fetcher.go +++ b/internal/resources/fetching/fetchers/azure/assets_fetcher.go @@ -21,9 +21,10 @@ import ( "context" "errors" "fmt" + "maps" + "slices" "github.com/elastic/elastic-agent-libs/logp" - "golang.org/x/exp/maps" "github.com/elastic/cloudbeat/internal/resources/fetching" "github.com/elastic/cloudbeat/internal/resources/fetching/cycle" @@ -98,7 +99,7 @@ func (f *AzureAssetsFetcher) Fetch(ctx context.Context, cycleMetadata cycle.Meta var assets []inventory.AzureAsset for _, assetGroup := range AzureAssetGroups { // Fetching all types even if non-existent in asset group for simplicity - r, err := f.provider.ListAllAssetTypesByName(ctx, assetGroup, maps.Keys(AzureAssetTypeToTypePair)) + r, err := f.provider.ListAllAssetTypesByName(ctx, assetGroup, slices.Collect(maps.Keys(AzureAssetTypeToTypePair))) if err != nil { f.log.Errorf("AzureAssetsFetcher.Fetch failed to fetch asset group %s: %s", assetGroup, err.Error()) errAgg = errors.Join(errAgg, err) diff --git a/internal/resources/fetching/fetchers/azure/assets_fetcher_test.go b/internal/resources/fetching/fetchers/azure/assets_fetcher_test.go index bee5f30beb..24fa2e62b5 100644 --- a/internal/resources/fetching/fetchers/azure/assets_fetcher_test.go +++ b/internal/resources/fetching/fetchers/azure/assets_fetcher_test.go @@ -20,12 +20,12 @@ package fetchers import ( "context" "errors" + "maps" "testing" "github.com/samber/lo" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/suite" - "golang.org/x/exp/maps" "github.com/elastic/cloudbeat/internal/resources/fetching" "github.com/elastic/cloudbeat/internal/resources/fetching/cycle" @@ -61,7 +61,7 @@ func (s *AzureAssetsFetcherTestSuite) TestFetcher_Fetch() { var flatMockAssets []inventory.AzureAsset for _, assetGroup := range AzureAssetGroups { var mockAssets []inventory.AzureAsset - for _, assetType := range maps.Keys(AzureAssetTypeToTypePair) { + for assetType := range maps.Keys(AzureAssetTypeToTypePair) { mockAssets = append(mockAssets, inventory.AzureAsset{ Id: "id", diff --git a/internal/resources/fetching/fetchers/azure/batch_fetcher.go b/internal/resources/fetching/fetchers/azure/batch_fetcher.go index 2aefc28af1..4247782c5f 100644 --- a/internal/resources/fetching/fetchers/azure/batch_fetcher.go +++ b/internal/resources/fetching/fetchers/azure/batch_fetcher.go @@ -21,10 +21,11 @@ import ( "context" "errors" "fmt" + "maps" + "slices" "github.com/elastic/elastic-agent-libs/logp" "github.com/samber/lo" - "golang.org/x/exp/maps" "github.com/elastic/cloudbeat/internal/resources/fetching" "github.com/elastic/cloudbeat/internal/resources/fetching/cycle" @@ -67,7 +68,7 @@ func (f *AzureBatchAssetFetcher) Fetch(ctx context.Context, cycleMetadata cycle. var errAgg error assets := []inventory.AzureAsset{} for _, assetGroup := range AzureBatchAssetGroups { - r, err := f.provider.ListAllAssetTypesByName(ctx, assetGroup, maps.Keys(AzureBatchAssets)) + r, err := f.provider.ListAllAssetTypesByName(ctx, assetGroup, slices.Collect(maps.Keys(AzureBatchAssets))) if err != nil { f.log.Errorf("AzureBatchAssetFetcher.Fetch failed to fetch asset group %s: %s", assetGroup, err.Error()) errAgg = errors.Join(errAgg, err) diff --git a/internal/resources/fetching/fetchers/azure/batch_fetcher_test.go b/internal/resources/fetching/fetchers/azure/batch_fetcher_test.go index 8c5621ae0e..67afdc9dc0 100644 --- a/internal/resources/fetching/fetchers/azure/batch_fetcher_test.go +++ b/internal/resources/fetching/fetchers/azure/batch_fetcher_test.go @@ -20,12 +20,12 @@ package fetchers import ( "context" "fmt" + "maps" "testing" "github.com/samber/lo" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/suite" - "golang.org/x/exp/maps" "github.com/elastic/cloudbeat/internal/resources/fetching" "github.com/elastic/cloudbeat/internal/resources/fetching/cycle" @@ -60,7 +60,7 @@ func (s *AzureBatchAssetFetcherTestSuite) TestFetcher_Fetch() { var flatMockAssets []inventory.AzureAsset for _, assetGroup := range AzureBatchAssetGroups { var mockAssets []inventory.AzureAsset - for _, assetType := range maps.Keys(AzureBatchAssets) { + for assetType := range maps.Keys(AzureBatchAssets) { mockId := fmt.Sprintf("%s-%s", AzureBatchAssets[assetType].SubType, "subId1") mockAssets = append(mockAssets, inventory.AzureAsset{ @@ -167,8 +167,8 @@ func (s *AzureBatchAssetFetcherTestSuite) TestFetcher_Fetch_Subscriptions() { for _, assetGroup := range AzureBatchAssetGroups { var mockAssets []inventory.AzureAsset - for _, assetType := range maps.Keys(AzureBatchAssets) { - for _, subKey := range maps.Keys(subMap) { + for assetType := range maps.Keys(AzureBatchAssets) { + for subKey := range maps.Keys(subMap) { mockId := fmt.Sprintf("%s-%s", AzureBatchAssets[assetType].SubType, subKey) mockAssets = append(mockAssets, inventory.AzureAsset{ @@ -213,7 +213,7 @@ func (s *AzureBatchAssetFetcherTestSuite) TestFetcher_Fetch_Subscriptions() { }) s.Len(expectedSubs, len(subMap)) - for _, subKey := range maps.Keys(expectedSubs) { + for subKey := range maps.Keys(expectedSubs) { typesPerSub := expectedSubs[subKey] s.Len(typesPerSub, len(AzureBatchAssets)) for _, subTypeRes := range typesPerSub { diff --git a/internal/vulnerability/events_creator.go b/internal/vulnerability/events_creator.go index b512f74cc1..cf70e406c4 100644 --- a/internal/vulnerability/events_creator.go +++ b/internal/vulnerability/events_creator.go @@ -21,6 +21,8 @@ import ( "context" "encoding/json" "fmt" + "maps" + "slices" "strings" "time" @@ -31,7 +33,6 @@ import ( libevents "github.com/elastic/beats/v7/libbeat/beat/events" "github.com/elastic/elastic-agent-libs/logp" "github.com/elastic/elastic-agent-libs/mapstr" - "golang.org/x/exp/maps" "github.com/elastic/cloudbeat/internal/config" "github.com/elastic/cloudbeat/internal/dataprovider" @@ -414,7 +415,7 @@ func getReference(vul trivyTypes.DetectedVulnerability) string { func getCVSSValue[T comparable](vul trivyTypes.DetectedVulnerability, value func(cvss dbTypes.CVSS) T, zeroVal T) T { // Get all the sources - sources := maps.Keys(vul.CVSS) + sources := slices.Collect(maps.Keys(vul.CVSS)) if len(sources) == 0 { return zeroVal } diff --git a/scripts/update_assets_md/go.mod b/scripts/update_assets_md/go.mod index 9c5de873a9..dbb90f9817 100644 --- a/scripts/update_assets_md/go.mod +++ b/scripts/update_assets_md/go.mod @@ -5,7 +5,6 @@ go 1.23.0 require ( github.com/ettle/strcase v0.2.0 github.com/xuri/excelize/v2 v2.8.1 - golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 ) require ( diff --git a/scripts/update_assets_md/go.sum b/scripts/update_assets_md/go.sum index 802c74eaeb..343280444d 100644 --- a/scripts/update_assets_md/go.sum +++ b/scripts/update_assets_md/go.sum @@ -21,8 +21,6 @@ github.com/xuri/nfp v0.0.0-20230919160717-d98342af3f05 h1:qhbILQo1K3mphbwKh1vNm4 github.com/xuri/nfp v0.0.0-20230919160717-d98342af3f05/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ= golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= -golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8= -golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY= golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4= golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE= golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= diff --git a/scripts/update_assets_md/main.go b/scripts/update_assets_md/main.go index 9878b3067d..1f23982f6d 100644 --- a/scripts/update_assets_md/main.go +++ b/scripts/update_assets_md/main.go @@ -22,13 +22,13 @@ import ( "go/ast" "go/parser" "go/token" + "maps" "os" "slices" "strings" "github.com/ettle/strcase" "github.com/xuri/excelize/v2" - "golang.org/x/exp/maps" ) const ( @@ -195,8 +195,7 @@ func writeSummary(plannedByProvider, implementedByProvider *ByProvider, filepath for providerNo, provider := range []string{AWS_PREFIX, AZURE_PREFIX, GCP_PREFIX} { planned := plannedByProvider.Get(provider) implemented := implementedByProvider.Get(provider) - sortedKeys := maps.Keys(planned) - slices.Sort(sortedKeys) + sortedKeys := slices.Sorted(maps.Keys(planned)) // stats totalImplemented := 0 @@ -241,8 +240,7 @@ func writeSummary(plannedByProvider, implementedByProvider *ByProvider, filepath fmt.Sprintf("**Progress: %d%% (%d/%d)**\n", percentage, totalImplemented, len(planned)), ) - sortedCategories := maps.Keys(plannedByCategory) - slices.Sort(sortedCategories) + sortedCategories := slices.Sorted(maps.Keys(plannedByCategory)) for _, category := range sortedCategories { plannedCount := plannedByCategory[category] diff --git a/tools/tools.go b/tools/tools.go index 14037652df..17dc46b6f2 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -29,7 +29,6 @@ import ( _ "github.com/pierrre/gotestcover" _ "github.com/tsg/go-daemon" _ "go.elastic.co/go-licence-detector" - _ "golang.org/x/exp/maps" _ "golang.org/x/lint/golint" _ "gotest.tools/gotestsum/cmd" )