Skip to content

Commit

Permalink
Merge branch '8.x' into mergify/bp/8.x/pr-3008
Browse files Browse the repository at this point in the history
  • Loading branch information
kubasobon authored Feb 14, 2025
2 parents 5031940 + 4033cee commit e637710
Show file tree
Hide file tree
Showing 25 changed files with 97 additions and 95 deletions.
6 changes: 3 additions & 3 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ linters-settings:
exported-fields-are-used: false
# Mark all local variables as used.
local-variables-are-used: false
tenv:
all: true

usetesting:
os-setenv: true
os-temp-dir: true
prealloc:
simple: true
range-loops: true
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion bin/golangci-lint
5 changes: 3 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package cmd

import (
"errors"
"fmt"

"github.com/elastic/beats/v7/libbeat/cmd"
Expand Down Expand Up @@ -57,12 +58,12 @@ func cloudbeatCfg(rawIn *proto.UnitExpectedConfig, agentInfo *client.AgentInfo)
config := rawIn.Source.AsMap()
packagePolicyID, ok := config["package_policy_id"]
if !ok {
return nil, fmt.Errorf("'package_policy_id' element does not exist")
return nil, errors.New("'package_policy_id' element does not exist")
}

packagePolicyRevision, ok := config["revision"]
if !ok {
return nil, fmt.Errorf("'revision' element does not exist")
return nil, errors.New("'revision' element does not exist")
}

for i := range modules {
Expand Down
11 changes: 6 additions & 5 deletions deploy/asset-inventory-cloudformation/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package main

import (
"errors"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -95,19 +96,19 @@ func bindEnvs(iface any, parts ...string) error {

func validateConfig(cfg *config) error {
if cfg.StackName == "" {
return fmt.Errorf("missing required flag: STACK_NAME")
return errors.New("missing required flag: STACK_NAME")
}

if cfg.FleetURL == "" {
return fmt.Errorf("missing required flag: FLEET_URL")
return errors.New("missing required flag: FLEET_URL")
}

if cfg.EnrollmentToken == "" {
return fmt.Errorf("missing required flag: ENROLLMENT_TOKEN")
return errors.New("missing required flag: ENROLLMENT_TOKEN")
}

if cfg.ElasticAgentVersion == "" {
return fmt.Errorf("missing required flag: ELASTIC_AGENT_VERSION")
return errors.New("missing required flag: ELASTIC_AGENT_VERSION")
}

if cfg.Dev != nil {
Expand All @@ -119,7 +120,7 @@ func validateConfig(cfg *config) error {

func validateDevConfig(cfg *devConfig) error {
if cfg.AllowSSH && cfg.KeyName == "" {
return fmt.Errorf("missing required flag for SSH enablement mode: DEV.KEY_NAME")
return errors.New("missing required flag for SSH enablement mode: DEV.KEY_NAME")
}

return nil
Expand Down
11 changes: 6 additions & 5 deletions deploy/cloudformation/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package main

import (
"errors"
"fmt"
"reflect"
"slices"
Expand Down Expand Up @@ -104,19 +105,19 @@ func bindEnvs(iface any, parts ...string) error {

func validateConfig(cfg *config) error {
if cfg.StackName == "" {
return fmt.Errorf("missing required flag: STACK_NAME")
return errors.New("missing required flag: STACK_NAME")
}

if cfg.FleetURL == "" {
return fmt.Errorf("missing required flag: FLEET_URL")
return errors.New("missing required flag: FLEET_URL")
}

if cfg.EnrollmentToken == "" {
return fmt.Errorf("missing required flag: ENROLLMENT_TOKEN")
return errors.New("missing required flag: ENROLLMENT_TOKEN")
}

if cfg.ElasticAgentVersion == "" {
return fmt.Errorf("missing required flag: ELASTIC_AGENT_VERSION")
return errors.New("missing required flag: ELASTIC_AGENT_VERSION")
}

if cfg.Dev != nil {
Expand All @@ -133,7 +134,7 @@ func validateConfig(cfg *config) error {

func validateDevConfig(cfg *devConfig) error {
if cfg.AllowSSH && cfg.KeyName == "" {
return fmt.Errorf("missing required flag for SSH enablement mode: DEV.KEY_NAME")
return errors.New("missing required flag for SSH enablement mode: DEV.KEY_NAME")
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion internal/flavors/assetinventory/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package assetinventory

import (
"context"
"errors"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -63,7 +64,7 @@ func (s *strategy) NewAssetInventory(ctx context.Context, client beat.Client) (i
case config.ProviderGCP:
fetchers, err = s.initGcpFetchers(ctx)
case "":
err = fmt.Errorf("missing config.v1.asset_inventory_provider setting")
err = errors.New("missing config.v1.asset_inventory_provider setting")
default:
err = fmt.Errorf("unsupported Asset Inventory provider %q", s.cfg.AssetInventoryProvider)
}
Expand Down
9 changes: 5 additions & 4 deletions internal/flavors/benchmark/eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package benchmark

import (
"context"
"errors"
"fmt"

awssdk "github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -127,16 +128,16 @@ func (k *EKS) getEksAwsConfig(ctx context.Context, cfg *config.Config) (awssdk.C

func (k *EKS) checkDependencies() error {
if k.AWSIdentityProvider == nil {
return fmt.Errorf("aws identity provider is uninitialized")
return errors.New("aws identity provider is uninitialized")
}
if k.ClientProvider == nil {
return fmt.Errorf("kubernetes client provider is uninitialized")
return errors.New("kubernetes client provider is uninitialized")
}
if k.EKSClusterNameProvider == nil {
return fmt.Errorf("eks cluster name provider is uninitialized")
return errors.New("eks cluster name provider is uninitialized")
}
if k.AWSMetadataProvider == nil {
return fmt.Errorf("aws metadata provider is uninitialized")
return errors.New("aws metadata provider is uninitialized")
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package azurefetcher
import (
"bytes"
"context"
"fmt"
"errors"
"testing"
"time"

Expand Down Expand Up @@ -100,7 +100,7 @@ func TestActiveDirectoryFetcher_FetchError(t *testing.T) {

provider := newMockActivedirectoryProvider(t)
provider.EXPECT().ListServicePrincipals(mock.Anything).Return(
[]*models.ServicePrincipal{}, fmt.Errorf("! error listing service principals"),
[]*models.ServicePrincipal{}, errors.New("! error listing service principals"),
)

fetcher := newActiveDirectoryFetcher(log, provider)
Expand Down
6 changes: 3 additions & 3 deletions internal/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (l *launcher) waitForUpdates() (*config.C, error) {

case update, ok := <-l.reloader.Channel():
if !ok {
return nil, fmt.Errorf("reloader channel unexpectedly closed")
return nil, errors.New("reloader channel unexpectedly closed")
}

l.log.Infof("Launcher will restart %s to apply the configuration update", l.name)
Expand Down Expand Up @@ -285,14 +285,14 @@ func (l *launcher) reconfigureWait(timeout time.Duration) (*config.C, error) {
for {
select {
case <-l.beaterErr:
return nil, fmt.Errorf("error channel closed")
return nil, errors.New("error channel closed")

case <-timer:
return nil, fmt.Errorf("timed out waiting for reconfiguration after %s", time.Since(start))

case update, ok := <-l.reloader.Channel():
if !ok {
return nil, fmt.Errorf("reconfiguration channel is closed")
return nil, errors.New("reconfiguration channel is closed")
}

if l.validator != nil {
Expand Down
3 changes: 1 addition & 2 deletions internal/launcher/launcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package launcher

import (
"errors"
"fmt"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -120,7 +119,7 @@ type validatorMock struct {
func (v *validatorMock) Validate(cfg *config.C) error {
var err error
if !reflect.DeepEqual(cfg, v.expected) {
err = fmt.Errorf("mock validation failed")
err = errors.New("mock validation failed")
}

return err
Expand Down
3 changes: 2 additions & 1 deletion internal/resources/fetching/fetchers/aws/ecr_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package fetchers

import (
"context"
"errors"
"fmt"
"regexp"
"sort"
Expand Down Expand Up @@ -292,7 +293,7 @@ func (s *EcrFetcherTestSuite) TestCreateFetcherErrorCases() {
Name: "cloudbeat",
},
},
fmt.Errorf("ecr error"),
errors.New("ecr error"),
},
}
for _, test := range tests {
Expand Down
3 changes: 2 additions & 1 deletion internal/resources/fetching/fetchers/aws/elb_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package fetchers

import (
"context"
"errors"
"fmt"
"regexp"
"testing"
Expand Down Expand Up @@ -169,7 +170,7 @@ func (s *ElbFetcherTestSuite) TestCreateFetcherErrorCases() {
Hostname: "adda9cdc89b13452e92d48be46858d37-1423035038.us-east-2.elb.amazonaws.com",
},
},
fmt.Errorf("elb error")},
errors.New("elb error")},
}
for _, test := range tests {
kubeclient := k8sfake.NewSimpleClientset()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package fetchers

import (
"context"
"fmt"
"errors"
"testing"
"time"

Expand Down Expand Up @@ -77,13 +77,13 @@ func TestMonitoringFetcher_Fetch(t *testing.T) {
monitoring: clientMocks{
"AggregateResources": [2]mocks{
{mock.Anything},
{nil, fmt.Errorf("failed to run provider")},
{nil, errors.New("failed to run provider")},
},
},
securityhub: clientMocks{
"Describe": [2]mocks{
{mock.Anything},
{[]securityhub.SecurityHub{{}}, fmt.Errorf("failed to run provider")},
{[]securityhub.SecurityHub{{}}, errors.New("failed to run provider")},
},
},
},
Expand All @@ -92,7 +92,7 @@ func TestMonitoringFetcher_Fetch(t *testing.T) {
monitoring: clientMocks{
"AggregateResources": [2]mocks{
{mock.Anything},
{nil, fmt.Errorf("failed to run provider")},
{nil, errors.New("failed to run provider")},
},
},
securityhub: clientMocks{
Expand Down
38 changes: 15 additions & 23 deletions internal/resources/fetching/fetchers/azure/assets_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,23 @@ type typePair struct {
Type string
}

func newPair(subType string, tpe string) typePair {
return typePair{
SubType: subType,
Type: tpe,
}
}

var AzureAssetTypeToTypePair = map[string]typePair{
inventory.ClassicStorageAccountAssetType: newPair(fetching.AzureClassicStorageAccountType, fetching.CloudStorage),
inventory.DiskAssetType: newPair(fetching.AzureDiskType, fetching.CloudCompute),
inventory.DocumentDBDatabaseAccountAssetType: newPair(fetching.AzureDocumentDBDatabaseAccountType, fetching.CloudDatabase),
inventory.MySQLDBAssetType: newPair(fetching.AzureMySQLDBType, fetching.CloudDatabase),
inventory.FlexibleMySQLDBAssetType: newPair(fetching.AzureFlexibleMySQLDBType, fetching.CloudDatabase),
inventory.NetworkWatchersFlowLogAssetType: newPair(fetching.AzureNetworkWatchersFlowLogType, fetching.MonitoringIdentity),
inventory.FlexiblePostgreSQLDBAssetType: newPair(fetching.AzureFlexiblePostgreSQLDBType, fetching.CloudDatabase),
inventory.PostgreSQLDBAssetType: newPair(fetching.AzurePostgreSQLDBType, fetching.CloudDatabase),
inventory.SQLServersAssetType: newPair(fetching.AzureSQLServerType, fetching.CloudDatabase),
inventory.StorageAccountAssetType: newPair(fetching.AzureStorageAccountType, fetching.CloudStorage),
inventory.VirtualMachineAssetType: newPair(fetching.AzureVMType, fetching.CloudCompute),
inventory.WebsitesAssetType: newPair(fetching.AzureWebSiteType, fetching.CloudCompute),
inventory.VaultAssetType: newPair(fetching.AzureVaultType, fetching.KeyManagement),
inventory.RoleDefinitionsType: newPair(fetching.AzureRoleDefinitionType, fetching.CloudIdentity),

inventory.ClassicStorageAccountAssetType: {fetching.AzureClassicStorageAccountType, fetching.CloudStorage},
inventory.DiskAssetType: {fetching.AzureDiskType, fetching.CloudCompute},
inventory.DocumentDBDatabaseAccountAssetType: {fetching.AzureDocumentDBDatabaseAccountType, fetching.CloudDatabase},
inventory.MySQLDBAssetType: {fetching.AzureMySQLDBType, fetching.CloudDatabase},
inventory.FlexibleMySQLDBAssetType: {fetching.AzureFlexibleMySQLDBType, fetching.CloudDatabase},
inventory.NetworkWatchersFlowLogAssetType: {fetching.AzureNetworkWatchersFlowLogType, fetching.MonitoringIdentity},
inventory.FlexiblePostgreSQLDBAssetType: {fetching.AzureFlexiblePostgreSQLDBType, fetching.CloudDatabase},
inventory.PostgreSQLDBAssetType: {fetching.AzurePostgreSQLDBType, fetching.CloudDatabase},
inventory.SQLServersAssetType: {fetching.AzureSQLServerType, fetching.CloudDatabase},
inventory.StorageAccountAssetType: {fetching.AzureStorageAccountType, fetching.CloudStorage},
inventory.VirtualMachineAssetType: {fetching.AzureVMType, fetching.CloudCompute},
inventory.WebsitesAssetType: {fetching.AzureWebSiteType, fetching.CloudCompute},
inventory.VaultAssetType: {fetching.AzureVaultType, fetching.KeyManagement},
inventory.RoleDefinitionsType: {fetching.AzureRoleDefinitionType, fetching.CloudIdentity},
// This asset type is used only for enrichment purposes, but is sent to OPA layer, producing no findings.
inventory.NetworkSecurityGroupAssetType: newPair(fetching.AzureNetworkSecurityGroupType, fetching.MonitoringIdentity),
inventory.NetworkSecurityGroupAssetType: {fetching.AzureNetworkSecurityGroupType, fetching.MonitoringIdentity},
}

// In order to simplify the mappings, we are trying to query all AzureAssetTypeToTypePair on every asset group
Expand Down
6 changes: 3 additions & 3 deletions internal/resources/fetching/fetchers/azure/batch_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ type AzureBatchAssetFetcher struct {
}

var AzureBatchAssets = map[string]typePair{
inventory.ActivityLogAlertAssetType: newPair(fetching.AzureActivityLogAlertType, fetching.MonitoringIdentity),
inventory.ApplicationInsights: newPair(fetching.AzureInsightsComponentType, fetching.MonitoringIdentity),
inventory.BastionAssetType: newPair(fetching.AzureBastionType, fetching.CloudDns),
inventory.ActivityLogAlertAssetType: {fetching.AzureActivityLogAlertType, fetching.MonitoringIdentity},
inventory.ApplicationInsights: {fetching.AzureInsightsComponentType, fetching.MonitoringIdentity},
inventory.BastionAssetType: {fetching.AzureBastionType, fetching.CloudDns},
}

// In order to simplify the mappings, we are trying to query all AzureBatchAssets on every asset group
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ func (f *AzureLocationsNetworkWatcherAssetBatchFetcher) fetchNetworkWatchersPerL
case f.resourceCh <- fetching.ResourceInfo{
CycleMetadata: metadata,
Resource: &NetworkWatchersBatchedByLocationResource{
typePair: newPair(fetching.AzureNetworkWatchersType, fetching.MonitoringIdentity),
typePair: typePair{
SubType: fetching.AzureNetworkWatchersType,
Type: fetching.MonitoringIdentity,
},
Subscription: subscription,
Location: location,
NetworkWatchers: groupedWatchers[location.Name],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,10 @@ func batchedNetworkWatcherByLocationResourceInfo(cycle cycle.Metadata, subscript
return fetching.ResourceInfo{
CycleMetadata: cycle,
Resource: &NetworkWatchersBatchedByLocationResource{
typePair: newPair(fetching.AzureNetworkWatchersType, fetching.MonitoringIdentity),
typePair: typePair{
SubType: fetching.AzureNetworkWatchersType,
Type: fetching.MonitoringIdentity,
},
Subscription: subscription,
Location: azAssetLocation(subscription.ShortID, location),
NetworkWatchers: networkWatchers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func NewAzureSecurityAssetFetcher(log *clog.Logger, ch chan fetching.ResourceInf
}

var AzureSecurityAssetTypeToTypePair = map[string]typePair{
inventory.SecurityContactsAssetType: newPair(fetching.AzureSecurityContactsType, fetching.MonitoringIdentity),
inventory.SecurityAutoProvisioningSettingsType: newPair(fetching.AzureAutoProvisioningSettingsType, fetching.MonitoringIdentity),
inventory.SecurityContactsAssetType: {fetching.AzureSecurityContactsType, fetching.MonitoringIdentity},
inventory.SecurityAutoProvisioningSettingsType: {fetching.AzureAutoProvisioningSettingsType, fetching.MonitoringIdentity},
}

func (f *AzureSecurityAssetFetcher) Fetch(ctx context.Context, cycleMetadata cycle.Metadata) error {
Expand Down
Loading

0 comments on commit e637710

Please sign in to comment.