Code Monkey home page Code Monkey logo

gcp-discover-orphaned-firewall-rules's Introduction

Google Cloud Discover Orphaned Firewall Rules

A tool to find all INGRESS firewall rules that are not applied to any VM instances in any project inside a shared VPC (orphaned rules).

Authentication to Google Cloud

This Project is using the Google Application Default Credentials (ADC).

You can either use the following command to login from the terminal using your default setup for gcloud:

gcloud auth application-default login

Or by a service account using the following environment variable:

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/[FILE_NAME].json"

How this tool works

This tool first gets all the INGRESS firewall rules for the given host project.

The next step is getting all of the child projects (projects associated with the host project)

A copy of the firewall rules is made intended to be as an elimination list for all active rules.

For each child project, all VM instances are fetched, and for each VM instance network tags are checked against the elimination list, if the instance network tags matches any of the rules target tags, the rule is considered active and removed from the elimination list.

Each child project is using the same elimination list until there are no more child projects, and the elimination (orphaned rules) list output is saved to a CSV file for further use.

Build from source

go get github.com/doitintl/gcp-discover-orphaned-firewall-rules
cd ${GOPATH}/src/github.com/doitintl/gcp-discover-orphaned-firewall-rules
go mod download
go build -o discover-orphaned-rules main.go
cp discover-orphaned-rules /usr/local/bin/

If compilation is failed, you should try to enable go modules:

GOPATH=/tmp/gopath-for-gcp-discover-orphaned-firewall-rules
GO111MODULE=on
go get github.com/doitintl/gcp-discover-orphaned-firewall-rules
go mod download
go build -o discover-orphaned-rules main.go
cp discover-orphaned-rules /usr/local/bin/

How to run

Available flags:

    --debug         Set log level
    --host string   Host Project ID <Required>
    --running       Filter only running VM instances
$ ./discover-orphaned-rules-darwin-amd64 --host=host-project-123ewquiyt

INFO[2019-06-12T12:57:54+03:00] creating a new Compute API client
INFO[2019-06-12T12:57:54+03:00] host project: host-project-123ewquiyt
INFO[2019-06-12T12:57:56+03:00] firewall Rules for host project: host-project-123ewquiyt
INFO[0001] listing only TargetTags rules...
INFO[0001] number of TargetTags Rules: 3
INFO[2019-06-12T12:57:56+03:00] child project: deleted-project-1268522
WARN[0001] error listing VM instances googleapi: Error 404: Failed to find project deleted-project-1268522, notFound
WARN[2019-06-12T12:57:56+03:00] Could not check project deleted-project-1268522 for orphaned rules: googleapi: Error 404: Failed to find project deleted-project-1268522, notFound
INFO[2019-06-12T12:57:56+03:00] child project: resource-project-12iuyt2854
INFO[0001] making a list of orphaned rules with all rules (active rules will be removed from it)
resource-project-12iuyt2854, ghostresource-project-12iuyt2854, instance-target-demo-ruleresource-project-12iuyt2854, real-ruleINFO[0001] looking for orphaned rules in project..
INFO[0001] remove active rule from orphans list: instance-target-demo-rule
INFO[0001] 2 potential orphaned firewall rules to evalute...
INFO[2019-06-12T12:57:56+03:00] child project: resource-2-23ouyrwe9
INFO[0002] making a list of orphaned rules with all rules (active rules will be removed from it)
resource-2-23ouyrwe9, ghostami-resource-2-23ouyrwe9, instance-target-demo-ruleami-resource-2-23ouyrwe9, real-ruleINFO[0002] looking for orphaned rules in project..
INFO[0002] remove active rule from orphans list: instance-target-demo-rule
INFO[0002] 2 potential orphaned firewall rules to evalute...
INFO[2019-06-12T12:57:57+03:00] generating CSV file for orphaned rules...
INFO[0002] creating a new CSV file: orphaned-rules.csv
INFO[2019-06-12T12:57:57+03:00] done!

This will also generate a CSV file:

rule-name,rule-tags
ghost,dead
real-rule,"real-1,real2,real-3"

gcp-discover-orphaned-firewall-rules's People

Contributors

eranchetz avatar innovia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

gcp-discover-orphaned-firewall-rules's Issues

Error Attempting Install

Here's what I get:

~$ go get github.com/doitintl/gcp-discover-orphaned-firewall-rules
go: finding github.com/doitintl/gcp-discover-orphaned-firewall-rules latest
go get: github.com/doitintl/gcp-discover-orphaned-firewall-rules@v0.0.0-20190624105935-0d655d4cb8e8: parsing go.mod:
        module declares its path as: doit.intl/orphaned-firewall-rules-gcp
                but was required as: github.com/doitintl/gcp-discover-orphaned-firewall-rules
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/nmckaskle/.cache/go-build"
GOENV="/home/nmckaskle/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/nmckaskle/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build630987888=/tmp/go-build -gno-record-gcc-switches"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.