diff --git a/go.mod b/go.mod index eb24246..f0e4fb7 100644 --- a/go.mod +++ b/go.mod @@ -10,13 +10,13 @@ require ( github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/recoveryservices/armrecoveryservicesbackup v1.0.0 github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/security/armsecurity v0.11.0 github.com/Azure/go-autorest/autorest/to v0.4.0 - github.com/ekristen/libnuke v0.6.0 + github.com/ekristen/libnuke v0.12.0 github.com/gotidy/ptr v1.4.0 github.com/hashicorp/go-azure-helpers v0.66.1 github.com/hashicorp/go-azure-sdk v0.20240125.1100331 github.com/manicminer/hamilton v0.61.0 github.com/sirupsen/logrus v1.9.3 - github.com/stretchr/testify v1.8.4 + github.com/stretchr/testify v1.9.0 github.com/urfave/cli/v2 v2.3.0 ) diff --git a/go.sum b/go.sum index 40c590c..3d5fa9f 100644 --- a/go.sum +++ b/go.sum @@ -78,6 +78,8 @@ github.com/dnaeon/go-vcr v1.2.0 h1:zHCHvJYTMh1N7xnV7zf1m1GPBF9Ad0Jk/whtQ1663qI= github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ= github.com/ekristen/libnuke v0.6.0 h1:VsNkdrprFfbFSsJnwQEynzww7JWR/icKox/sso7ucT0= github.com/ekristen/libnuke v0.6.0/go.mod h1:WhYx7LDAkvkXwwfhWCASRn7fbifF8kfyhNsUj5zCCVs= +github.com/ekristen/libnuke v0.12.0 h1:Dsk+ckT9sh9QZTLq5m8kOA1KFJGJxSv0TLnfe3YeL1o= +github.com/ekristen/libnuke v0.12.0/go.mod h1:sBdA04l9IMMejQw5gO9k6o/a0GffSYhgZYaUSdRjIac= github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= @@ -282,6 +284,7 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M= github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI= github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= diff --git a/pkg/commands/list/list.go b/pkg/commands/list/list.go index a20841b..111b1b5 100644 --- a/pkg/commands/list/list.go +++ b/pkg/commands/list/list.go @@ -2,26 +2,27 @@ package list import ( "fmt" - "github.com/ekristen/azure-nuke/pkg/nuke" + "sort" + "github.com/fatih/color" "github.com/urfave/cli/v2" - "sort" + + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/azure-nuke/pkg/commands/global" "github.com/ekristen/azure-nuke/pkg/common" - - "github.com/ekristen/libnuke/pkg/resource" + "github.com/ekristen/azure-nuke/pkg/nuke" _ "github.com/ekristen/azure-nuke/resources" ) func execute(c *cli.Context) error { - ls := resource.GetNames() + ls := registry.GetNames() sort.Strings(ls) for _, name := range ls { - reg := resource.GetRegistration(name) + reg := registry.GetRegistration(name) if reg.AlternativeResource != "" { color.New(color.Bold).Printf("%-55s\n", name) @@ -34,7 +35,7 @@ func execute(c *cli.Context) error { c = color.FgHiGreen } else if reg.Scope == nuke.Subscription { c = color.FgHiBlue - } else if reg.Scope == nuke.ResourceGroup { + } else if reg.Scope == nuke.Subscription { c = color.FgHiMagenta } else { diff --git a/pkg/commands/nuke/command.go b/pkg/commands/nuke/command.go index ef7488b..dbdf9b4 100644 --- a/pkg/commands/nuke/command.go +++ b/pkg/commands/nuke/command.go @@ -3,20 +3,25 @@ package nuke import ( "context" "fmt" + libscanner "github.com/ekristen/libnuke/pkg/scanner" + "log" + "slices" + "strings" + "time" + + "github.com/sirupsen/logrus" + "github.com/urfave/cli/v2" + + libconfig "github.com/ekristen/libnuke/pkg/config" + libnuke "github.com/ekristen/libnuke/pkg/nuke" + "github.com/ekristen/libnuke/pkg/registry" + "github.com/ekristen/libnuke/pkg/types" + "github.com/ekristen/azure-nuke/pkg/azure" "github.com/ekristen/azure-nuke/pkg/commands/global" "github.com/ekristen/azure-nuke/pkg/common" "github.com/ekristen/azure-nuke/pkg/config" "github.com/ekristen/azure-nuke/pkg/nuke" - libconfig "github.com/ekristen/libnuke/pkg/config" - libnuke "github.com/ekristen/libnuke/pkg/nuke" - "github.com/ekristen/libnuke/pkg/resource" - "github.com/ekristen/libnuke/pkg/types" - "github.com/sirupsen/logrus" - "github.com/urfave/cli/v2" - "log" - "slices" - "time" ) type log2LogrusWriter struct { @@ -65,7 +70,7 @@ func execute(c *cli.Context) error { parsedConfig, err := config.New(libconfig.Options{ Path: c.Path("config"), - Deprecations: resource.GetDeprecatedResourceTypeMapping(), + Deprecations: registry.GetDeprecatedResourceTypeMapping(), }) if err != nil { return err @@ -85,10 +90,11 @@ func execute(c *cli.Context) error { n := libnuke.New(params, filters, parsedConfig.Settings) n.SetRunSleep(5 * time.Second) + n.SetLogger(logrus.WithField("component", "nuke")) tenantConfig := parsedConfig.Accounts[c.String("tenant-id")] tenantResourceTypes := types.ResolveResourceTypes( - resource.GetNamesForScope(nuke.Tenant), + registry.GetNamesForScope(nuke.Tenant), []types.Collection{ n.Parameters.Includes, parsedConfig.ResourceTypes.GetIncludes(), @@ -104,23 +110,7 @@ func execute(c *cli.Context) error { ) subResourceTypes := types.ResolveResourceTypes( - resource.GetNamesForScope(nuke.Subscription), - []types.Collection{ - n.Parameters.Includes, - parsedConfig.ResourceTypes.GetIncludes(), - tenantConfig.ResourceTypes.GetIncludes(), - }, - []types.Collection{ - n.Parameters.Excludes, - parsedConfig.ResourceTypes.Excludes, - tenantConfig.ResourceTypes.Excludes, - }, - nil, - nil, - ) - - rgResourceTypes := types.ResolveResourceTypes( - resource.GetNamesForScope(nuke.ResourceGroup), + registry.GetNamesForScope(nuke.Subscription), []types.Collection{ n.Parameters.Includes, parsedConfig.ResourceTypes.GetIncludes(), @@ -136,7 +126,7 @@ func execute(c *cli.Context) error { ) if slices.Contains(parsedConfig.Regions, "global") { - if err := n.RegisterScanner(nuke.Tenant, libnuke.NewScanner("tenant/all", tenantResourceTypes, &nuke.ListerOpts{ + if err := n.RegisterScanner(nuke.Tenant, libscanner.New("tenant/all", tenantResourceTypes, &nuke.ListerOpts{ Authorizers: authorizers, TenantId: tenant.ID, })); err != nil { @@ -147,39 +137,19 @@ func execute(c *cli.Context) error { logrus.Debug("registering scanner for tenant subscription resources") for _, subscriptionId := range tenant.SubscriptionIds { logrus.Debug("registering scanner for subscription resources") - if err := n.RegisterScanner(nuke.Subscription, libnuke.NewScanner("tenant/sub", subResourceTypes, &nuke.ListerOpts{ + parts := strings.Split(subscriptionId, "-") + if err := n.RegisterScanner(nuke.Subscription, libscanner.New(fmt.Sprintf("sub/%s", parts[:1][0]), subResourceTypes, &nuke.ListerOpts{ Authorizers: tenant.Authorizers, TenantId: tenant.ID, SubscriptionId: subscriptionId, + Locations: parsedConfig.Regions, })); err != nil { return err } } - for _, region := range parsedConfig.Regions { - if region == "global" { - continue - } - - for _, subscriptionId := range tenant.SubscriptionIds { - logrus.Debug("registering scanner for subscription resources") - - for i, resourceGroup := range tenant.ResourceGroups[subscriptionId] { - logrus.Debugf("registering scanner for resource group resources: rg/%s", resourceGroup) - if err := n.RegisterScanner(nuke.ResourceGroup, libnuke.NewScanner(fmt.Sprintf("%s/rg%d", region, i), rgResourceTypes, &nuke.ListerOpts{ - Authorizers: tenant.Authorizers, - TenantId: tenant.ID, - SubscriptionId: subscriptionId, - ResourceGroup: resourceGroup, - Location: region, - })); err != nil { - return err - } - } - } - } - logrus.Debug("running ...") + return n.Run(c.Context) } diff --git a/pkg/nuke/resource.go b/pkg/nuke/resource.go index bf864b4..494fe58 100644 --- a/pkg/nuke/resource.go +++ b/pkg/nuke/resource.go @@ -2,13 +2,13 @@ package nuke import ( "github.com/ekristen/azure-nuke/pkg/azure" - "github.com/ekristen/libnuke/pkg/resource" + "github.com/ekristen/libnuke/pkg/registry" ) const ( - Tenant resource.Scope = "tenant" - Subscription resource.Scope = "subscription" - ResourceGroup resource.Scope = "resource-group" + Tenant registry.Scope = "tenant" + Subscription registry.Scope = "subscription" + ResourceGroup registry.Scope = "resource-group" ) type ListerOpts struct { @@ -16,5 +16,5 @@ type ListerOpts struct { TenantId string SubscriptionId string ResourceGroup string - Location string + Locations []string } diff --git a/resources/aad-group.go b/resources/aad-group.go index feb40d1..5116c93 100644 --- a/resources/aad-group.go +++ b/resources/aad-group.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/go-azure-sdk/sdk/odata" "github.com/manicminer/hamilton/msgraph" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -17,7 +18,7 @@ import ( const AzureAdGroupResource = "AzureADGroup" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: AzureAdGroupResource, Scope: nuke.Tenant, Lister: &AzureAdGroupLister{}, diff --git a/resources/aad-user.go b/resources/aad-user.go index dd0789c..156b624 100644 --- a/resources/aad-user.go +++ b/resources/aad-user.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/go-azure-sdk/sdk/odata" "github.com/manicminer/hamilton/msgraph" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -17,7 +18,7 @@ import ( const AzureADUserResource = "AzureADUser" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: AzureADUserResource, Scope: nuke.Tenant, Lister: &AzureADUserLister{}, diff --git a/resources/app-service-plan.go b/resources/app-service-plan.go index b4ca992..919fdec 100644 --- a/resources/app-service-plan.go +++ b/resources/app-service-plan.go @@ -8,6 +8,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/web/mgmt/2021-03-01/web" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -17,9 +18,9 @@ import ( const AppServicePlanResource = "AppServicePlan" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: AppServicePlanResource, - Scope: nuke.ResourceGroup, + Scope: nuke.Subscription, Lister: &AppServicePlanLister{}, }) } diff --git a/resources/application-certificate.go b/resources/application-certificate.go index d9971ee..3c2ac9f 100644 --- a/resources/application-certificate.go +++ b/resources/application-certificate.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/go-azure-sdk/sdk/odata" "github.com/manicminer/hamilton/msgraph" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -17,7 +18,7 @@ import ( const ApplicationCertificateResource = "ApplicationCertificate" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: ApplicationCertificateResource, Scope: nuke.Tenant, Lister: &ApplicationCertificateLister{}, diff --git a/resources/application-federated-credential.go b/resources/application-federated-credential.go index 8c72268..7cba02d 100644 --- a/resources/application-federated-credential.go +++ b/resources/application-federated-credential.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/go-azure-sdk/sdk/odata" "github.com/manicminer/hamilton/msgraph" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -17,7 +18,7 @@ import ( const ApplicationFederatedCredentialResource = "ApplicationFederatedCredential" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: ApplicationFederatedCredentialResource, Scope: nuke.Tenant, Lister: &ApplicationFederatedCredentialLister{}, diff --git a/resources/application-secret.go b/resources/application-secret.go index c7c74c3..a64c1ad 100644 --- a/resources/application-secret.go +++ b/resources/application-secret.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/go-azure-sdk/sdk/odata" "github.com/manicminer/hamilton/msgraph" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -17,7 +18,7 @@ import ( const ApplicationSecretResource = "ApplicationSecret" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: ApplicationSecretResource, Scope: nuke.Tenant, Lister: &ApplicationSecretLister{}, diff --git a/resources/application.go b/resources/application.go index 2471d0f..d31993f 100644 --- a/resources/application.go +++ b/resources/application.go @@ -2,13 +2,14 @@ package resources import ( "context" - "github.com/gotidy/ptr" + "github.com/gotidy/ptr" "github.com/sirupsen/logrus" "github.com/hashicorp/go-azure-sdk/sdk/odata" "github.com/manicminer/hamilton/msgraph" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -18,7 +19,7 @@ import ( const ApplicationResource = "Application" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: ApplicationResource, Scope: nuke.Tenant, Lister: &ApplicationLister{}, diff --git a/resources/budget.go b/resources/budget.go index 8a042e2..f66e511 100644 --- a/resources/budget.go +++ b/resources/budget.go @@ -12,6 +12,7 @@ import ( "github.com/hashicorp/go-azure-sdk/resource-manager/consumption/2021-10-01/budgets" "github.com/hashicorp/go-azure-sdk/sdk/environments" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -21,7 +22,7 @@ import ( const BudgetResource = "Budget" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: BudgetResource, Scope: nuke.Subscription, Lister: &BudgetLister{}, diff --git a/resources/container-registry.go b/resources/container-registry.go index d50f329..15132ae 100644 --- a/resources/container-registry.go +++ b/resources/container-registry.go @@ -8,6 +8,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/containerregistry/mgmt/2019-05-01/containerregistry" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -17,9 +18,9 @@ import ( const ContainerRegistryResource = "ContainerRegistry" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: ContainerRegistryResource, - Scope: nuke.ResourceGroup, + Scope: nuke.Subscription, Lister: &ContainerRegistryLister{}, }) } diff --git a/resources/disk.go b/resources/disk.go index 2abe280..5aa8d0f 100644 --- a/resources/disk.go +++ b/resources/disk.go @@ -8,6 +8,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-04-01/compute" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -17,9 +18,9 @@ import ( const DiskResource = "Disk" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: DiskResource, - Scope: nuke.ResourceGroup, + Scope: nuke.Subscription, Lister: &DiskLister{}, DependsOn: []string{ VirtualMachineResource, diff --git a/resources/dns-zone.go b/resources/dns-zone.go index 4a16096..cc06084 100644 --- a/resources/dns-zone.go +++ b/resources/dns-zone.go @@ -8,6 +8,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2018-05-01/dns" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -17,9 +18,9 @@ import ( const DNSZoneResource = "DNSZone" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: DNSZoneResource, - Scope: nuke.ResourceGroup, + Scope: nuke.Subscription, Lister: &DNSZoneLister{}, }) } diff --git a/resources/ip-allocation.go b/resources/ip-allocation.go index 7cdc1d5..e8a64ec 100644 --- a/resources/ip-allocation.go +++ b/resources/ip-allocation.go @@ -8,6 +8,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-05-01/network" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -17,9 +18,9 @@ import ( const IPAllocationResource = "IPAllocation" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: IPAllocationResource, - Scope: nuke.ResourceGroup, + Scope: nuke.Subscription, Lister: &IPAllocationLister{}, }) } diff --git a/resources/key-vault.go b/resources/key-vault.go index ba3c071..66cbc13 100644 --- a/resources/key-vault.go +++ b/resources/key-vault.go @@ -8,6 +8,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/keyvault/mgmt/2019-09-01/keyvault" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -17,9 +18,9 @@ import ( const KeyVaultResource = "KeyVault" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: KeyVaultResource, - Scope: nuke.ResourceGroup, + Scope: nuke.Subscription, Lister: &KeyVaultLister{}, }) } diff --git a/resources/network-interface.go b/resources/network-interface.go index 5986993..ca0278d 100644 --- a/resources/network-interface.go +++ b/resources/network-interface.go @@ -8,6 +8,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-05-01/network" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -17,9 +18,9 @@ import ( const NetworkInterfaceResource = "NetworkInterface" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: NetworkInterfaceResource, - Scope: nuke.ResourceGroup, + Scope: nuke.Subscription, Lister: &NetworkInterfaceLister{}, }) } diff --git a/resources/network-security-group.go b/resources/network-security-group.go index 78941ad..7f5dcd7 100644 --- a/resources/network-security-group.go +++ b/resources/network-security-group.go @@ -8,6 +8,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-05-01/network" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -17,9 +18,9 @@ import ( const NetworkSecurityGroupResource = "NetworkSecurityGroup" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: NetworkSecurityGroupResource, - Scope: nuke.ResourceGroup, + Scope: nuke.Subscription, Lister: &NetworkSecurityGroupLister{}, }) } diff --git a/resources/policy-assignment.go b/resources/policy-assignment.go index bc2b58e..9df0a23 100644 --- a/resources/policy-assignment.go +++ b/resources/policy-assignment.go @@ -8,6 +8,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/preview/resources/mgmt/2021-06-01-preview/policy" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -17,7 +18,7 @@ import ( const PolicyAssignmentResource = "PolicyAssignment" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: PolicyAssignmentResource, Scope: nuke.Subscription, Lister: &PolicyAssignmentLister{}, diff --git a/resources/policy-definition.go b/resources/policy-definition.go index f539a7c..bd4f7eb 100644 --- a/resources/policy-definition.go +++ b/resources/policy-definition.go @@ -10,6 +10,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/preview/resources/mgmt/2021-06-01-preview/policy" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -19,7 +20,7 @@ import ( const PolicyDefinitionResource = "PolicyDefinition" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: PolicyDefinitionResource, Scope: nuke.Subscription, Lister: &PolicyDefinitionLister{}, diff --git a/resources/private-dns-zone.go b/resources/private-dns-zone.go index 8b7f608..dba5569 100644 --- a/resources/private-dns-zone.go +++ b/resources/private-dns-zone.go @@ -8,6 +8,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/privatedns/mgmt/2018-09-01/privatedns" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -17,7 +18,7 @@ import ( const PrivateDNSZoneResource = "PrivateDNSZone" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: PrivateDNSZoneResource, Scope: nuke.Subscription, Lister: &PrivateDNSZoneLister{}, diff --git a/resources/public-ip-address.go b/resources/public-ip-address.go index e78d6af..94a4203 100644 --- a/resources/public-ip-address.go +++ b/resources/public-ip-address.go @@ -8,6 +8,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-05-01/network" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -17,9 +18,9 @@ import ( const PublicIPAddressesResource = "PublicIPAddresses" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: PublicIPAddressesResource, - Scope: nuke.ResourceGroup, + Scope: nuke.Subscription, Lister: &PublicIPAddressesLister{}, }) } diff --git a/resources/recoveryservices-backup-policy.go b/resources/recoveryservices-backup-policy.go index ed9214b..39a94fd 100644 --- a/resources/recoveryservices-backup-policy.go +++ b/resources/recoveryservices-backup-policy.go @@ -2,7 +2,6 @@ package resources import ( "context" - "github.com/hashicorp/go-azure-sdk/sdk/environments" "time" "github.com/gotidy/ptr" @@ -12,7 +11,9 @@ import ( "github.com/hashicorp/go-azure-sdk/resource-manager/recoveryservices/2023-02-01/vaults" "github.com/hashicorp/go-azure-sdk/resource-manager/recoveryservicesbackup/2023-02-01/backuppolicies" "github.com/hashicorp/go-azure-sdk/resource-manager/recoveryservicesbackup/2023-02-01/protectionpolicies" + "github.com/hashicorp/go-azure-sdk/sdk/environments" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -22,9 +23,9 @@ import ( const RecoveryServicesBackupPolicyResource = "RecoveryServicesBackupPolicy" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: RecoveryServicesBackupPolicyResource, - Scope: nuke.ResourceGroup, + Scope: nuke.Subscription, Lister: &RecoveryServicesBackupPolicyLister{}, DependsOn: []string{ RecoveryServicesBackupProtectedItemResource, diff --git a/resources/recoveryservices-backup-protecteditem.go b/resources/recoveryservices-backup-protecteditem.go index cdd214b..86928a6 100644 --- a/resources/recoveryservices-backup-protecteditem.go +++ b/resources/recoveryservices-backup-protecteditem.go @@ -13,6 +13,7 @@ import ( "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/recoveryservices/armrecoveryservicesbackup" "github.com/Azure/go-autorest/autorest/to" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -22,9 +23,9 @@ import ( const RecoveryServicesBackupProtectedItemResource = "RecoveryServicesBackupProtectedItem" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: RecoveryServicesBackupProtectedItemResource, - Scope: nuke.ResourceGroup, + Scope: nuke.Subscription, Lister: &RecoveryServicesBackupProtectedItemLister{}, }) } diff --git a/resources/recoveryservices-backup-protectioncontainer.go b/resources/recoveryservices-backup-protectioncontainer.go index 7936afb..2d5d8c6 100644 --- a/resources/recoveryservices-backup-protectioncontainer.go +++ b/resources/recoveryservices-backup-protectioncontainer.go @@ -11,6 +11,7 @@ import ( "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/recoveryservices/armrecoveryservicesbackup" "github.com/Azure/go-autorest/autorest/to" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -20,9 +21,9 @@ import ( const RecoveryServicesBackupProtectionContainerResource = "RecoveryServicesBackupProtectionContainer" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: RecoveryServicesBackupProtectionContainerResource, - Scope: nuke.ResourceGroup, + Scope: nuke.Subscription, Lister: &RecoveryServicesBackupProtectionContainersLister{}, }) } diff --git a/resources/recoveryservices-backup-protectionintent.go b/resources/recoveryservices-backup-protectionintent.go index ab9a514..2044284 100644 --- a/resources/recoveryservices-backup-protectionintent.go +++ b/resources/recoveryservices-backup-protectionintent.go @@ -11,6 +11,7 @@ import ( "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/recoveryservices/armrecoveryservicesbackup" "github.com/Azure/go-autorest/autorest/to" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -20,9 +21,9 @@ import ( const RecoveryServicesBackupProtectionIntentResource = "RecoveryServicesBackupProtectionIntent" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: RecoveryServicesBackupProtectionIntentResource, - Scope: nuke.ResourceGroup, + Scope: nuke.Subscription, Lister: &RecoveryServicesBackupProtectionIntentLister{}, }) } diff --git a/resources/recoveryservices-vault.go b/resources/recoveryservices-vault.go index 3a6c39b..0511561 100644 --- a/resources/recoveryservices-vault.go +++ b/resources/recoveryservices-vault.go @@ -2,14 +2,16 @@ package resources import ( "context" + "time" + "github.com/gotidy/ptr" - "github.com/hashicorp/go-azure-sdk/sdk/environments" "github.com/sirupsen/logrus" - "time" "github.com/hashicorp/go-azure-helpers/resourcemanager/commonids" "github.com/hashicorp/go-azure-sdk/resource-manager/recoveryservices/2023-02-01/vaults" + "github.com/hashicorp/go-azure-sdk/sdk/environments" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -19,9 +21,9 @@ import ( const RecoveryServicesVaultResource = "RecoveryServicesVault" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: RecoveryServicesVaultResource, - Scope: nuke.ResourceGroup, + Scope: nuke.Subscription, Lister: &RecoveryServicesVaultLister{}, DependsOn: []string{ RecoveryServicesBackupProtectedItemResource, diff --git a/resources/resource-group.go b/resources/resource-group.go index 5d0f22b..fbf8362 100644 --- a/resources/resource-group.go +++ b/resources/resource-group.go @@ -2,13 +2,17 @@ package resources import ( "context" - "github.com/hashicorp/go-azure-sdk/sdk/environments" - "github.com/sirupsen/logrus" + "fmt" + "slices" "time" + "github.com/sirupsen/logrus" + "github.com/hashicorp/go-azure-helpers/resourcemanager/commonids" "github.com/hashicorp/go-azure-sdk/resource-manager/resources/2022-09-01/resourcegroups" + "github.com/hashicorp/go-azure-sdk/sdk/environments" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -18,12 +22,12 @@ import ( const ResourceGroupResource = "ResourceGroup" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: ResourceGroupResource, Lister: &ResourceGroupLister{}, // Scope is set to ResourceGroup because we want to be able to query based on location and resource group // which is not possible if we treat this as part of subscription because that's considered global. - Scope: nuke.ResourceGroup, + Scope: nuke.Subscription, }) } @@ -32,9 +36,21 @@ type ResourceGroup struct { name *string location string subscriptionId string + listerOpts *nuke.ListerOpts +} + +func (r *ResourceGroup) Filter() error { + if !slices.Contains(r.listerOpts.Locations, r.location) { + return fmt.Errorf("resource not in enabled region/location") + } + + return nil } func (r *ResourceGroup) Remove(ctx context.Context) error { + ctx, cancel := context.WithDeadline(ctx, time.Now().Add(30*time.Second)) + defer cancel() + _, err := r.client.Delete(ctx, commonids.NewResourceGroupID(r.subscriptionId, *r.name), resourcegroups.DefaultDeleteOperationOptions()) return err } @@ -89,6 +105,7 @@ func (l ResourceGroupLister) List(ctx context.Context, o interface{}) ([]resourc name: entity.Name, location: entity.Location, subscriptionId: opts.SubscriptionId, + listerOpts: opts, }) } diff --git a/resources/security-alert.go b/resources/security-alert.go index a1cd529..1febb39 100644 --- a/resources/security-alert.go +++ b/resources/security-alert.go @@ -11,6 +11,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/preview/security/mgmt/v3.0/security" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -22,7 +23,7 @@ const SecurityAlertResource = "SecurityAlert" const SecurityAlertLocation = "/Microsoft.Security/locations/(?P.*)/alerts/" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: SecurityAlertResource, Scope: nuke.Subscription, Lister: &SecurityAlertsLister{}, diff --git a/resources/security-assessment.go b/resources/security-assessment.go index 1ccbd04..3654fc1 100644 --- a/resources/security-assessment.go +++ b/resources/security-assessment.go @@ -10,6 +10,7 @@ import ( "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/security/armsecurity" "github.com/Azure/go-autorest/autorest/to" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -19,7 +20,7 @@ import ( const SecurityAssessmentResource = "SecurityAssessment" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: SecurityAssessmentResource, Scope: nuke.Subscription, Lister: &SecurityAssessmentLister{}, diff --git a/resources/security-pricing.go b/resources/security-pricing.go index b88a655..c2b850e 100644 --- a/resources/security-pricing.go +++ b/resources/security-pricing.go @@ -9,6 +9,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/preview/security/mgmt/v3.0/security" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -18,7 +19,7 @@ import ( const SecurityPricingResource = "SecurityPricing" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: SecurityPricingResource, Scope: nuke.Subscription, Lister: &SecurityPricingLister{}, diff --git a/resources/security-workspace.go b/resources/security-workspace.go index a5a3578..9e35ab1 100644 --- a/resources/security-workspace.go +++ b/resources/security-workspace.go @@ -8,6 +8,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/preview/security/mgmt/v3.0/security" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -17,7 +18,7 @@ import ( const SecurityWorkspaceResource = "SecurityWorkspace" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: SecurityWorkspaceResource, Scope: nuke.Subscription, Lister: &SecurityWorkspaceLister{}, diff --git a/resources/service-principal.go b/resources/service-principal.go index 1c5fcda..e2cb4ad 100644 --- a/resources/service-principal.go +++ b/resources/service-principal.go @@ -12,6 +12,7 @@ import ( "github.com/hashicorp/go-azure-sdk/sdk/odata" "github.com/manicminer/hamilton/msgraph" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -21,7 +22,7 @@ import ( const ServicePrincipalResource = "ServicePrincipal" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: ServicePrincipalResource, Scope: nuke.Tenant, Lister: &ServicePrincipalsLister{}, diff --git a/resources/ssh-key.go b/resources/ssh-key.go index 6ea6e42..006b275 100644 --- a/resources/ssh-key.go +++ b/resources/ssh-key.go @@ -8,6 +8,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-04-01/compute" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -17,18 +18,19 @@ import ( const SSHPublicKeyResource = "SSHPublicKey" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: SSHPublicKeyResource, Lister: &SSHPublicKeyLister{}, - Scope: nuke.ResourceGroup, + Scope: nuke.Subscription, }) } type SSHPublicKey struct { - client compute.SSHPublicKeysClient - name *string - rg *string - tags map[string]*string + client compute.SSHPublicKeysClient + name *string + rg *string + location *string + tags map[string]*string } func (r *SSHPublicKey) Remove(ctx context.Context) error { @@ -39,8 +41,9 @@ func (r *SSHPublicKey) Remove(ctx context.Context) error { func (r *SSHPublicKey) Properties() types.Properties { properties := types.NewProperties() - properties.Set("Name", *r.name) - properties.Set("ResourceGroup", *r.rg) + properties.Set("Name", r.name) + properties.Set("Location", r.location) + properties.Set("ResourceGroup", r.rg) for tag, value := range r.tags { properties.SetTag(&tag, value) @@ -72,7 +75,7 @@ func (l SSHPublicKeyLister) List(ctx context.Context, o interface{}) ([]resource log.Trace("attempting to list ssh key") - list, err := client.ListByResourceGroup(ctx, opts.ResourceGroup) + list, err := client.ListBySubscription(ctx) if err != nil { return nil, err } @@ -83,10 +86,11 @@ func (l SSHPublicKeyLister) List(ctx context.Context, o interface{}) ([]resource log.Trace("list not done") for _, g := range list.Values() { resources = append(resources, &SSHPublicKey{ - client: client, - name: g.Name, - rg: &opts.ResourceGroup, - tags: g.Tags, + client: client, + name: g.Name, + rg: &opts.ResourceGroup, + location: g.Location, + tags: g.Tags, }) } diff --git a/resources/storage-account.go b/resources/storage-account.go index 5e437b3..5f59f5f 100644 --- a/resources/storage-account.go +++ b/resources/storage-account.go @@ -8,6 +8,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2021-09-01/storage" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -17,9 +18,9 @@ import ( const StorageAccountResource = "StorageAccount" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: StorageAccountResource, - Scope: nuke.ResourceGroup, + Scope: nuke.Subscription, Lister: &StorageAccountLister{}, DependsOn: []string{ VirtualMachineResource, diff --git a/resources/virtual-machine.go b/resources/virtual-machine.go index 7088caf..b329e4e 100644 --- a/resources/virtual-machine.go +++ b/resources/virtual-machine.go @@ -8,6 +8,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-04-01/compute" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -17,10 +18,10 @@ import ( const VirtualMachineResource = "VirtualMachine" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: VirtualMachineResource, Lister: &VirtualMachineLister{}, - Scope: nuke.ResourceGroup, + Scope: nuke.Subscription, }) } diff --git a/resources/virtual-network.go b/resources/virtual-network.go index 4d31d86..15c7f51 100644 --- a/resources/virtual-network.go +++ b/resources/virtual-network.go @@ -8,6 +8,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-05-01/network" + "github.com/ekristen/libnuke/pkg/registry" "github.com/ekristen/libnuke/pkg/resource" "github.com/ekristen/libnuke/pkg/types" @@ -17,9 +18,9 @@ import ( const VirtualNetworkResource = "VirtualNetwork" func init() { - resource.Register(&resource.Registration{ + registry.Register(®istry.Registration{ Name: VirtualNetworkResource, - Scope: nuke.ResourceGroup, + Scope: nuke.Subscription, Lister: &VirtualNetworkLister{}, }) }