From fe1de570125773055fd7e3648ebaf80291bf620a Mon Sep 17 00:00:00 2001 From: Sven Walter Date: Sat, 10 Feb 2018 15:42:15 +0100 Subject: [PATCH 1/3] add debug logs --- cmd/root.go | 17 +++++++++++-- glide.lock | 12 ++++++--- glide.yaml | 2 ++ pkg/awsutil/session.go | 24 +++++++++++++++--- pkg/awsutil/util.go | 56 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 pkg/awsutil/util.go diff --git a/cmd/root.go b/cmd/root.go index 9dad48db..8fce1833 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -6,13 +6,15 @@ import ( "github.com/rebuy-de/aws-nuke/pkg/awsutil" "github.com/rebuy-de/aws-nuke/resources" + log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) func NewRootCommand() *cobra.Command { var ( - params NukeParameters - creds awsutil.Credentials + params NukeParameters + creds awsutil.Credentials + verbose bool ) command := &cobra.Command{ @@ -21,6 +23,13 @@ func NewRootCommand() *cobra.Command { Long: `A tool which removes every resource from an AWS account. Use it with caution, since it cannot distinguish between production and non-production.`, } + command.PreRun = func(cmd *cobra.Command, args []string) { + log.SetLevel(log.InfoLevel) + if verbose { + log.SetLevel(log.DebugLevel) + } + } + command.RunE = func(cmd *cobra.Command, args []string) error { var err error @@ -51,6 +60,10 @@ func NewRootCommand() *cobra.Command { return n.Run() } + command.PersistentFlags().BoolVarP( + &verbose, "verbose", "v", false, + "Enables debug output.") + command.PersistentFlags().StringVarP( ¶ms.ConfigPath, "config", "c", "", "(required) Path to the nuke config file.") diff --git a/glide.lock b/glide.lock index a836f884..304ddf54 100644 --- a/glide.lock +++ b/glide.lock @@ -1,8 +1,8 @@ -hash: 6fa91765a8814057be46a070a4ebcbe888bedeb5e38fe3dbae2de14ef1ef1ca6 -updated: 2017-07-24T12:04:43.624073528+02:00 +hash: 1c2230f3409ee41ca50875383087f519ee3183c4045a2c4a2ce26116414579ce +updated: 2018-02-09T19:10:54.675812835+01:00 imports: - name: github.com/aws/aws-sdk-go - version: afd601335e2a72d43caa3af6bd2abe512fcc3bfd + version: de28909e9837364f0368d94ddcba085c57af3c12 subpackages: - aws - aws/awserr @@ -35,6 +35,7 @@ imports: - service/cloudformation - service/cloudtrail - service/cloudwatchevents + - service/dynamodb - service/ec2 - service/ecr - service/efs @@ -42,6 +43,7 @@ imports: - service/elb - service/elbv2 - service/iam + - service/kms - service/lambda - service/rds - service/route53 @@ -51,7 +53,7 @@ imports: - name: github.com/fatih/color version: 570b54cabe6b8eb0bc2dfce68d964677d63b5260 - name: github.com/go-ini/ini - version: 6e4869b434bd001f6983749881c7ead3545887d8 + version: 5b3e00af70a9484542169a976dcab8d03e601a17 - name: github.com/inconshreveable/mousetrap version: 76626ae9c91c4f2a10f34cad8ce83ea42c93bb75 - name: github.com/jmespath/go-jmespath @@ -62,6 +64,8 @@ imports: - name: github.com/mattn/go-isatty version: 57fdcb988a5c543893cc61bce354a6e24ab70022 repo: https://github.com/mattn/go-isatty +- name: github.com/sirupsen/logrus + version: d682213848ed68c0a260ca37d6dd5ace8423f5ba - name: github.com/spf13/cobra version: 35136c09d8da66b901337c6e86fd8e88a1a255bd - name: github.com/spf13/pflag diff --git a/glide.yaml b/glide.yaml index 1219538d..1d9b8d7c 100644 --- a/glide.yaml +++ b/glide.yaml @@ -4,3 +4,5 @@ import: version: ^v1.10.14 - package: github.com/fatih/color version: ^1.1.0 +- package: github.com/sirupsen/logrus + version: ^1.0.4 diff --git a/pkg/awsutil/session.go b/pkg/awsutil/session.go index 4d5ec87e..c0c73aae 100644 --- a/pkg/awsutil/session.go +++ b/pkg/awsutil/session.go @@ -6,7 +6,9 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" + "github.com/aws/aws-sdk-go/aws/request" "github.com/aws/aws-sdk-go/aws/session" + log "github.com/sirupsen/logrus" ) type Credentials struct { @@ -38,24 +40,24 @@ func (c *Credentials) Validate() error { func (c *Credentials) NewSession(region string) (*session.Session, error) { if c.HasProfile() { - return session.NewSessionWithOptions(session.Options{ + return Log(session.NewSessionWithOptions(session.Options{ Config: aws.Config{ Region: aws.String(region), }, SharedConfigState: session.SharedConfigEnable, Profile: c.Profile, - }) + })) } if c.HasKeys() { - return session.NewSessionWithOptions(session.Options{ + return Log(session.NewSessionWithOptions(session.Options{ Config: aws.Config{ Region: aws.String(region), Credentials: credentials.NewStaticCredentials( strings.TrimSpace(c.AccessKeyID), strings.TrimSpace(c.SecretAccessKey), "", - )}}) + )}})) } return nil, fmt.Errorf("You have to specify a profile or credentials for at least one region.") @@ -74,3 +76,17 @@ func (c *Credentials) Session(region string) (*session.Session, error) { return sess, err } + +func Log(s *session.Session, err error) (*session.Session, error) { + if err == nil { + s.Handlers.Send.PushFront(func(r *request.Request) { + log.Debugf("sending AWS request:\n%s", DumpRequest(r.HTTPRequest)) + }) + + s.Handlers.ValidateResponse.PushFront(func(r *request.Request) { + log.Debugf("received AWS response:\n%s", DumpResponse(r.HTTPResponse)) + }) + } + + return s, err +} diff --git a/pkg/awsutil/util.go b/pkg/awsutil/util.go new file mode 100644 index 00000000..8fde593b --- /dev/null +++ b/pkg/awsutil/util.go @@ -0,0 +1,56 @@ +package awsutil + +import ( + "bytes" + "net/http" + "net/http/httputil" + "regexp" + + log "github.com/sirupsen/logrus" +) + +func Indent(s, prefix string) string { + return string(IndentBytes([]byte(s), []byte(prefix))) +} + +func IndentBytes(b, prefix []byte) []byte { + var res []byte + bol := true + for _, c := range b { + if bol && c != '\n' { + res = append(res, prefix...) + } + res = append(res, c) + bol = c == '\n' + } + return res +} + +var REAuthHeader = regexp.MustCompile(`(?m:^(Auth[^:]*):.*$)`) + +func DumpRequest(r *http.Request) string { + dump, err := httputil.DumpRequest(r, true) + if err != nil { + log.WithField("Error", err). + Warnf("failed to dump HTTP request") + return "" + } + + dump = bytes.TrimSpace(dump) + dump = REAuthHeader.ReplaceAll(dump, []byte("$1: ")) + dump = IndentBytes(dump, []byte(" > ")) + return string(dump) +} + +func DumpResponse(r *http.Response) string { + dump, err := httputil.DumpResponse(r, true) + if err != nil { + log.WithField("Error", err). + Warnf("failed to dump HTTP response") + return "" + } + + dump = bytes.TrimSpace(dump) + dump = IndentBytes(dump, []byte(" < ")) + return string(dump) +} From 9ce88d8f8356067c0a92508eb8db6e6ac6a98c0c Mon Sep 17 00:00:00 2001 From: Sven Walter Date: Sat, 10 Feb 2018 15:56:11 +0100 Subject: [PATCH 2/3] replace warn and error loggers --- cmd/config.go | 3 ++- cmd/log.go | 12 ------------ cmd/scan.go | 10 ++++------ pkg/awsutil/util.go | 22 +++------------------- pkg/util/indent.go | 18 ++++++++++++++++++ 5 files changed, 27 insertions(+), 38 deletions(-) create mode 100644 pkg/util/indent.go diff --git a/cmd/config.go b/cmd/config.go index 12621e54..b740f404 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -7,6 +7,7 @@ import ( "github.com/rebuy-de/aws-nuke/pkg/types" + log "github.com/sirupsen/logrus" "gopkg.in/yaml.v2" ) @@ -127,7 +128,7 @@ func (c *NukeConfig) resolveDeprecations() error { if !ok { continue } - LogWarn("deprecated resource type '%s' - converting to '%s'\n", resourceType, replacement) + log.Warnf("deprecated resource type '%s' - converting to '%s'\n", resourceType, replacement) if _, ok := a.Filters[replacement]; ok { return fmt.Errorf("using deprecated resource type and replacement: '%s','%s'", resourceType, replacement) diff --git a/cmd/log.go b/cmd/log.go index 59236ede..8607b1fe 100644 --- a/cmd/log.go +++ b/cmd/log.go @@ -19,7 +19,6 @@ var ( ColorRegion = *color.New(color.Bold) ColorResourceType = *color.New() ColorResourceID = *color.New(color.Bold) - ColorWarning = *color.New(color.FgYellow) ) func Log(region Region, resourceType string, r resources.Resource, c color.Color, msg string) { @@ -31,14 +30,3 @@ func Log(region Region, resourceType string, r resources.Resource, c color.Color fmt.Printf(" - ") c.Printf("%s\n", msg) } - -func LogWarn(s string, i ...interface{}) { - ColorWarning.Printf("WARNING: "+s, i...) -} - -func LogErrorf(err error) { - out := color.New(color.FgRed) - trace := fmt.Sprintf("%+v", err) - out.Println(trace) - out.Println("") -} diff --git a/cmd/scan.go b/cmd/scan.go index 02c1e426..e6401048 100644 --- a/cmd/scan.go +++ b/cmd/scan.go @@ -5,7 +5,9 @@ import ( "runtime/debug" "github.com/aws/aws-sdk-go/aws/session" + "github.com/rebuy-de/aws-nuke/pkg/util" "github.com/rebuy-de/aws-nuke/resources" + log "github.com/sirupsen/logrus" ) func Scan(region Region, resourceTypes []string) <-chan *Item { @@ -16,12 +18,8 @@ func Scan(region Region, resourceTypes []string) <-chan *Item { lister := resources.GetLister(resourceType) rs, err := safeLister(region.Session, lister) if err != nil { - LogErrorf(fmt.Errorf("\n=============\n\n"+ - "Listing with %T failed:\n\n"+ - "%v\n\n"+ - "Please report this to https://github.com/rebuy-de/aws-nuke/issues/new.\n\n"+ - "=============", - lister, err)) + dump := util.Indent(fmt.Sprintf("%v", err), " !!! ") + log.Errorf("Listing with %T failed. Please report this to https://github.com/rebuy-de/aws-nuke/issues/new.\n%s", lister, dump) continue } diff --git a/pkg/awsutil/util.go b/pkg/awsutil/util.go index 8fde593b..5681ac94 100644 --- a/pkg/awsutil/util.go +++ b/pkg/awsutil/util.go @@ -6,26 +6,10 @@ import ( "net/http/httputil" "regexp" + "github.com/rebuy-de/aws-nuke/pkg/util" log "github.com/sirupsen/logrus" ) -func Indent(s, prefix string) string { - return string(IndentBytes([]byte(s), []byte(prefix))) -} - -func IndentBytes(b, prefix []byte) []byte { - var res []byte - bol := true - for _, c := range b { - if bol && c != '\n' { - res = append(res, prefix...) - } - res = append(res, c) - bol = c == '\n' - } - return res -} - var REAuthHeader = regexp.MustCompile(`(?m:^(Auth[^:]*):.*$)`) func DumpRequest(r *http.Request) string { @@ -38,7 +22,7 @@ func DumpRequest(r *http.Request) string { dump = bytes.TrimSpace(dump) dump = REAuthHeader.ReplaceAll(dump, []byte("$1: ")) - dump = IndentBytes(dump, []byte(" > ")) + dump = util.IndentBytes(dump, []byte(" > ")) return string(dump) } @@ -51,6 +35,6 @@ func DumpResponse(r *http.Response) string { } dump = bytes.TrimSpace(dump) - dump = IndentBytes(dump, []byte(" < ")) + dump = util.IndentBytes(dump, []byte(" < ")) return string(dump) } diff --git a/pkg/util/indent.go b/pkg/util/indent.go new file mode 100644 index 00000000..2a09e535 --- /dev/null +++ b/pkg/util/indent.go @@ -0,0 +1,18 @@ +package util + +func Indent(s, prefix string) string { + return string(IndentBytes([]byte(s), []byte(prefix))) +} + +func IndentBytes(b, prefix []byte) []byte { + var res []byte + bol := true + for _, c := range b { + if bol && c != '\n' { + res = append(res, prefix...) + } + res = append(res, c) + bol = c == '\n' + } + return res +} From aef3280ed0c337dcbe7683c1c1f02455976c36ad Mon Sep 17 00:00:00 2001 From: Sven Walter Date: Sat, 10 Feb 2018 16:02:41 +0100 Subject: [PATCH 3/3] add dependency for logrus --- glide.lock | 11 ++++++++--- glide.yaml | 3 +++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/glide.lock b/glide.lock index 304ddf54..d51a38fa 100644 --- a/glide.lock +++ b/glide.lock @@ -1,8 +1,8 @@ -hash: 1c2230f3409ee41ca50875383087f519ee3183c4045a2c4a2ce26116414579ce -updated: 2018-02-09T19:10:54.675812835+01:00 +hash: ce6bd4f43a8f766e187d3d9223ca51ab4b306c5ada7ad2c367e5be864552786f +updated: 2018-02-10T16:02:26.35322554+01:00 imports: - name: github.com/aws/aws-sdk-go - version: de28909e9837364f0368d94ddcba085c57af3c12 + version: 82eadef012f77590875babb177c60878727821c0 subpackages: - aws - aws/awserr @@ -70,11 +70,16 @@ imports: version: 35136c09d8da66b901337c6e86fd8e88a1a255bd - name: github.com/spf13/pflag version: 5ccb023bc27df288a957c5e994cd44fd19619465 +- name: golang.org/x/crypto + version: 81e90905daefcd6fd217b62423c0908922eadb30 + subpackages: + - ssh/terminal - name: golang.org/x/sys version: e24f485414aeafb646f6fca458b0bf869c0880a1 repo: https://go.googlesource.com/sys subpackages: - unix + - windows - name: gopkg.in/yaml.v2 version: 53feefa2559fb8dfa8d81baad31be332c97d6c77 testImports: [] diff --git a/glide.yaml b/glide.yaml index 1d9b8d7c..72909998 100644 --- a/glide.yaml +++ b/glide.yaml @@ -6,3 +6,6 @@ import: version: ^1.1.0 - package: github.com/sirupsen/logrus version: ^1.0.4 +- package: golang.org/x/crypto + subpackages: + - ssh/terminal