Skip to content

Commit

Permalink
Merge pull request #106 from ekristen/fix-multiple-subs
Browse files Browse the repository at this point in the history
fix(command/run): bug with scanner registration with multiple subscriptions
  • Loading branch information
ekristen authored Jan 6, 2025
2 parents 6e1914a + 0eddd08 commit 1e32ccc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
11 changes: 6 additions & 5 deletions pkg/azure/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,20 @@ func NewTenant( //nolint:gocyclo
return nil, err
}
for _, s := range list.Values() {
slog := log.WithField("subscription_id", *s.SubscriptionID)
if len(subscriptionIDs) > 0 && !slices.Contains(subscriptionIDs, *s.SubscriptionID) {
log.Warnf("skipping subscription id: %s (reason: not requested)", *s.SubscriptionID)
slog.Warnf("skipping subscription id: %s (reason: not requested)", *s.SubscriptionID)
continue
}

log.Tracef("adding subscriptions id: %s", *s.SubscriptionID)
slog.Trace("adding subscription")
tenant.SubscriptionIds = append(tenant.SubscriptionIds, *s.SubscriptionID)

log.Trace("listing resource groups")
slog.Trace("listing resource groups")
groupsClient := resources.NewGroupsClient(*s.SubscriptionID)
groupsClient.Authorizer = authorizers.Management

log.Debugf("configured regions: %v", regions)
slog.Debugf("configured regions: %v", regions)
for list, err := groupsClient.List(ctx, "", nil); list.NotDone(); err = list.NextWithContext(ctx) {
if err != nil {
return nil, err
Expand All @@ -90,7 +91,7 @@ func NewTenant( //nolint:gocyclo
continue
}

log.Debugf("resource group name: %s", *g.Name)
slog.Debugf("resource group name: %s", *g.Name)
tenant.ResourceGroups[*s.SubscriptionID] = append(tenant.ResourceGroups[*s.SubscriptionID], *g.Name)
}
}
Expand Down
32 changes: 25 additions & 7 deletions pkg/commands/run/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"os"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -47,7 +48,10 @@ func execute(c *cli.Context) error { //nolint:funlen,gocyclo
entry: logrus.WithField("source", "standard-logger"),
})

logrus.Tracef("tenant id: %s", c.String("tenant-id"))
logger := logrus.StandardLogger()
logger.SetOutput(os.Stdout)

logger.Tracef("tenant id: %s", c.String("tenant-id"))

authorizers, err := azure.ConfigureAuth(ctx,
c.String("environment"), c.String("tenant-id"), c.String("client-id"),
Expand All @@ -57,7 +61,7 @@ func execute(c *cli.Context) error { //nolint:funlen,gocyclo
return err
}

logrus.Trace("preparing to run nuke")
logger.Trace("preparing to run nuke")

params := &libnuke.Parameters{
Force: c.Bool("force"),
Expand Down Expand Up @@ -113,7 +117,7 @@ func execute(c *cli.Context) error { //nolint:funlen,gocyclo
n := libnuke.New(params, filters, parsedConfig.Settings)

n.SetRunSleep(5 * time.Second)
n.SetLogger(logrus.WithField("component", "nuke"))
n.SetLogger(logger.WithField("component", "nuke"))

n.RegisterVersion(fmt.Sprintf("> %s", common.AppVersion.String()))

Expand Down Expand Up @@ -178,9 +182,17 @@ func execute(c *cli.Context) error { //nolint:funlen,gocyclo
return err
}

logrus.Debug("registering scanner for tenant subscription resources")
logger.
WithField("component", "run").
WithField("scope", "tenant").
Debug("registering scanner")
for _, subscriptionID := range tenant.SubscriptionIds {
logrus.Debug("registering scanner for subscription resources")
logger.
WithField("component", "run").
WithField("scope", "subscription").
WithField("subscription_id", subscriptionID).
Debug("registering scanner")

parts := strings.Split(subscriptionID, "-")
if err := n.RegisterScanner(azure.SubscriptionScope,
libscanner.New(fmt.Sprintf("sub/%s", parts[:1][0]), subResourceTypes, &azure.ListerOpts{
Expand All @@ -196,9 +208,15 @@ func execute(c *cli.Context) error { //nolint:funlen,gocyclo

for subscriptionID, resourceGroups := range tenant.ResourceGroups {
for _, rg := range resourceGroups {
logrus.Debug("registering scanner for resource group")
logger.
WithField("component", "run").
WithField("scope", "resource-group").
WithField("subscription_id", subscriptionID).
WithField("resource_group", rg).
Debug("registering scanner")

if err := n.RegisterScanner(azure.ResourceGroupScope,
libscanner.New(fmt.Sprintf("rg/%s", rg), rgResourceTypes, &azure.ListerOpts{
libscanner.New(fmt.Sprintf("sub/%s/rg/%s", subscriptionID, rg), rgResourceTypes, &azure.ListerOpts{
Authorizers: tenant.Authorizers,
TenantID: tenant.ID,
SubscriptionID: subscriptionID,
Expand Down

0 comments on commit 1e32ccc

Please sign in to comment.