Skip to content

Commit

Permalink
Support for using go sdk v2, Fix Network Firewall (#29)
Browse files Browse the repository at this point in the history
* Add option to ingore KMS errors due to permissions

* Add ignored option

* Working v2 and networkfirewall

* Update local workflows, mock commit

---------

Co-authored-by: bfisher8 <[email protected]>
  • Loading branch information
bjfish25 and bfisher8 authored Jul 22, 2024
1 parent 0c78aca commit de2d633
Show file tree
Hide file tree
Showing 14 changed files with 1,058 additions and 144 deletions.
93 changes: 0 additions & 93 deletions .github/workflows/ci.yaml

This file was deleted.

46 changes: 0 additions & 46 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,49 +56,3 @@ jobs:
tag: ${{ github.ref }}
overwrite: true
file_glob: true

docker_build:
runs-on: ubuntu-22.04
name: Docker Build

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Generate image tags
shell: bash
run: echo "tags=quay.io/rebuy/aws-nuke:${GITHUB_REF#refs/tags/},docker.io/rebuy/aws-nuke:${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
id: generate_tags

- name: Set up QEMU
id: qemu
uses: docker/setup-qemu-action@v3
with:
platforms: arm64

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
install: true

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Login to Quay.io
uses: docker/login-action@v3
with:
registry: quay.io
username: ${{ secrets.QUAY_USERNAME }}
password: ${{ secrets.QUAY_PASSWORD }}

- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.generate_tags.outputs.tags }}
platforms: linux/amd64,linux/arm64
6 changes: 5 additions & 1 deletion cmd/nuke.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ func (n *Nuke) Scan() error {
queue := make(Queue, 0)

for _, regionName := range n.Config.Regions {
region := NewRegion(regionName, n.Account.ResourceTypeToServiceType, n.Account.NewSession)
config, err := n.Account.Credentials.NewConfig(regionName)
if err != nil {
return err
}
region := NewRegion(regionName, config, n.Account.ResourceTypeToServiceType, n.Account.NewSession)

items := Scan(region, resourceTypes)
for item := range items {
Expand Down
6 changes: 6 additions & 0 deletions cmd/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ func (i *Item) Print() {
// List gets all resource items of the same resource type like the Item.
func (i *Item) List() ([]resources.Resource, error) {
lister := resources.GetLister(i.Type)
if lister == nil {
// Type has supported V2 implementation
lister := resources.GetListerV2(i.Type)
return lister(i.Region.Config)
}

sess, err := i.Region.Session(i.Type)
if err != nil {
return nil, err
Expand Down
17 changes: 16 additions & 1 deletion cmd/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"sync"

"github.com/aws/aws-sdk-go-v2/aws"

"github.com/aws/aws-sdk-go/aws/session"
"github.com/rebuy-de/aws-nuke/v2/pkg/awsutil"
)
Expand All @@ -19,14 +21,17 @@ type Region struct {
NewSession SessionFactory
ResTypeResolver ResourceTypeResolver

Config *aws.Config

cache map[string]*session.Session
lock *sync.RWMutex
}

func NewRegion(name string, typeResolver ResourceTypeResolver, sessionFactory SessionFactory) *Region {
func NewRegion(name string, config *aws.Config, typeResolver ResourceTypeResolver, sessionFactory SessionFactory) *Region {
return &Region{
Name: name,
NewSession: sessionFactory,
Config: config,
ResTypeResolver: typeResolver,
lock: &sync.RWMutex{},
cache: make(map[string]*session.Session),
Expand Down Expand Up @@ -60,3 +65,13 @@ func (region *Region) Session(resourceType string) (*session.Session, error) {
region.lock.Unlock()
return sess, nil
}

func (region *Region) NewConfig(resourceType string) (*aws.Config, error) {
if region.Name == "global" {
return nil, awsutil.ErrSkipRequest(fmt.Sprintf(
"No service available in region '%s' to handle '%s'",
region.Name, resourceType))
}

return region.Config, nil
}
28 changes: 27 additions & 1 deletion cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (s *scanner) list(region *Region, resourceType string) {
lister := resources.GetLister(resourceType)
var rs []resources.Resource
sess, err := region.Session(resourceType)
if err == nil {
if err == nil && lister != nil {
rs, err = lister(sess)
}
if err != nil {
Expand All @@ -77,6 +77,32 @@ func (s *scanner) list(region *Region, resourceType string) {
return
}

listerV2 := resources.GetListerV2(resourceType)
var rsV2 []resources.Resource
cfg, err := region.NewConfig(resourceType)
if err == nil && listerV2 != nil {
rsV2, err = listerV2(cfg)
}
if err != nil {
_, ok := err.(awsutil.ErrSkipRequest)
if ok {
log.Debugf("skipping request: %v", err)
return
}

_, ok = err.(awsutil.ErrUnknownEndpoint)
if ok {
log.Warnf("skipping request: %v", err)
return
}

dump := util.Indent(fmt.Sprintf("%v", err), " ")
log.Errorf("Listing %s failed:\n%s", resourceType, dump)
return
}

rs = append(rs, rsV2...)

for _, r := range rs {
s.items <- &Item{
Region: region,
Expand Down
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ go 1.21

require (
github.com/aws/aws-sdk-go v1.53.15
github.com/aws/aws-sdk-go-v2 v1.30.0
github.com/aws/aws-sdk-go-v2/config v1.27.22
github.com/aws/aws-sdk-go-v2/credentials v1.17.22
github.com/aws/aws-sdk-go-v2/service/networkfirewall v1.40.0
github.com/aws/aws-sdk-go-v2/service/sts v1.30.0
github.com/fatih/color v1.17.0
github.com/golang/mock v1.6.0
github.com/google/uuid v1.6.0
Expand All @@ -18,6 +23,15 @@ require (
)

require (
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.8 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.14 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.22.0 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.0 // indirect
github.com/aws/smithy-go v1.20.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gemnasium/logrus-graylog-hook/v3 v3.1.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
28 changes: 28 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
github.com/aws/aws-sdk-go v1.53.15 h1:FtZmkg7xM8RfP2oY6p7xdKBYrRgkITk9yve2QV7N938=
github.com/aws/aws-sdk-go v1.53.15/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/aws/aws-sdk-go-v2 v1.30.0 h1:6qAwtzlfcTtcL8NHtbDQAqgM5s6NDipQTkPxyH/6kAA=
github.com/aws/aws-sdk-go-v2 v1.30.0/go.mod h1:ffIFB97e2yNsv4aTSGkqtHnppsIJzw7G7BReUZ3jCXM=
github.com/aws/aws-sdk-go-v2/config v1.27.22 h1:TRkQVtpDINt+Na/ToU7iptyW6U0awAwJ24q4XN+59k8=
github.com/aws/aws-sdk-go-v2/config v1.27.22/go.mod h1:EYY3mVgFRUWkh6QNKH64MdyKs1YSUgatc0Zp3MDxi7c=
github.com/aws/aws-sdk-go-v2/credentials v1.17.22 h1:wu9kXQbbt64ul09v3ye4HYleAr4WiGV/uv69EXKDEr0=
github.com/aws/aws-sdk-go-v2/credentials v1.17.22/go.mod h1:pcvMtPcxJn3r2k6mZD9I0EcumLqPLA7V/0iCgOIlY+o=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.8 h1:FR+oWPFb/8qMVYMWN98bUZAGqPvLHiyqg1wqQGfUAXY=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.8/go.mod h1:EgSKcHiuuakEIxJcKGzVNWh5srVAQ3jKaSrBGRYvM48=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.12 h1:SJ04WXGTwnHlWIODtC5kJzKbeuHt+OUNOgKg7nfnUGw=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.12/go.mod h1:FkpvXhA92gb3GE9LD6Og0pHHycTxW7xGpnEh5E7Opwo=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.12 h1:hb5KgeYfObi5MHkSSZMEudnIvX30iB+E21evI4r6BnQ=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.12/go.mod h1:CroKe/eWJdyfy9Vx4rljP5wTUjNJfb+fPz1uMYUhEGM=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 h1:hT8rVHwugYE2lEfdFE0QWVo81lF7jMrYJVDWI+f+VxU=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0/go.mod h1:8tu/lYfQfFe6IGnaOdrpVgEL2IrrDOf6/m9RQum4NkY=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 h1:Ji0DY1xUsUr3I8cHps0G+XM3WWU16lP6yG8qu1GAZAs=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2/go.mod h1:5CsjAbs3NlGQyZNFACh+zztPDI7fU6eW9QsxjfnuBKg=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.14 h1:zSDPny/pVnkqABXYRicYuPf9z2bTqfH13HT3v6UheIk=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.14/go.mod h1:3TTcI5JSzda1nw/pkVC9dhgLre0SNBFj2lYS4GctXKI=
github.com/aws/aws-sdk-go-v2/service/networkfirewall v1.40.0 h1:ZKjJJWxZ4cGM6LWxXsnviGlBpqPvifSod4U8gOXik9U=
github.com/aws/aws-sdk-go-v2/service/networkfirewall v1.40.0/go.mod h1:23qyfghRkv9qOMRIL9KdUHiKyhARU/0FddRMtvMSVV0=
github.com/aws/aws-sdk-go-v2/service/sso v1.22.0 h1:lPIAPCRoJkmotLTU/9B6icUFlYDpEuWjKeL79XROv1M=
github.com/aws/aws-sdk-go-v2/service/sso v1.22.0/go.mod h1:lcQG/MmxydijbeTOp04hIuJwXGWPZGI3bwdFDGRTv14=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.0 h1:/4r71ghx+hX9spr884cqXHPEmPzqH/J3K7fkE1yfcmw=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.0/go.mod h1:z0P8K+cBIsFXUr5rzo/psUeJ20XjPN0+Nn8067Nd+E4=
github.com/aws/aws-sdk-go-v2/service/sts v1.30.0 h1:9ja34PaKybhCJjVKvxtDsUjbATUJGN+eF6QnO58u5cI=
github.com/aws/aws-sdk-go-v2/service/sts v1.30.0/go.mod h1:N2mQiucsO0VwK9CYuS4/c2n6Smeh1v47Rz3dWCPFLdE=
github.com/aws/smithy-go v1.20.2 h1:tbp628ireGtzcHDDmLT/6ADHidqnwgF57XOXZe6tp4Q=
github.com/aws/smithy-go v1.20.2/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
Loading

0 comments on commit de2d633

Please sign in to comment.