Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proper logger facility #75

Merged
merged 3 commits into from
Feb 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/rebuy-de/aws-nuke/pkg/types"

log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -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)
Expand Down
12 changes: 0 additions & 12 deletions cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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("")
}
17 changes: 15 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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

Expand Down Expand Up @@ -51,6 +60,10 @@ func NewRootCommand() *cobra.Command {
return n.Run()
}

command.PersistentFlags().BoolVarP(
&verbose, "verbose", "v", false,
"Enables debug output.")

command.PersistentFlags().StringVarP(
&params.ConfigPath, "config", "c", "",
"(required) Path to the nuke config file.")
Expand Down
10 changes: 4 additions & 6 deletions cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
17 changes: 13 additions & 4 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ import:
version: ^v1.10.14
- package: github.com/fatih/color
version: ^1.1.0
- package: github.com/sirupsen/logrus
version: ^1.0.4
- package: golang.org/x/crypto
subpackages:
- ssh/terminal
24 changes: 20 additions & 4 deletions pkg/awsutil/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.")
Expand All @@ -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
}
40 changes: 40 additions & 0 deletions pkg/awsutil/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package awsutil

import (
"bytes"
"net/http"
"net/http/httputil"
"regexp"

"github.com/rebuy-de/aws-nuke/pkg/util"
log "github.com/sirupsen/logrus"
)

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: <hidden>"))
dump = util.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 = util.IndentBytes(dump, []byte(" < "))
return string(dump)
}
18 changes: 18 additions & 0 deletions pkg/util/indent.go
Original file line number Diff line number Diff line change
@@ -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
}