Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #141 from danmx/#132
Browse files Browse the repository at this point in the history
aws: adding tracing, max retries for AWS api calls
  • Loading branch information
danmx authored Aug 16, 2020
2 parents 7508d48 + 246b1a6 commit 53f6ace
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<a name="unreleased"></a>
## [Unreleased]

### Aws
- customizing default retryer for AWS api calls

### Chore
- **deps:** bumping Go and tidying dependecies
- **deps:** update module aws/aws-sdk-go to v1.34.2
Expand All @@ -15,6 +18,7 @@

### Fix
- **bazel:** docker rules
- **pre-commit:** adding Gazelle run during fmt phase


<a name="0.5.3"></a>
Expand Down
3 changes: 3 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Filter format examples:
listType := cfg.GetString("list-type")
instanceIDs := cfg.GetStringSlice("filters.instance.ids")
mfaToken := cfg.GetString("mfa")
trace := log.IsLevelEnabled(log.TraceLevel)
// hack to get map[string]string from args
// https://github.com/spf13/viper/issues/608
if cmd.Flags().Changed("session-filters") {
Expand Down Expand Up @@ -113,6 +114,7 @@ Filter format examples:
"instanceIDs": instanceIDs,
"sessionFilters": sessionFilters,
"tags": tags,
"trace": trace,
}).Debug("List inputs")
input := &list.StartInput{
OutputFormat: &outputFormat,
Expand All @@ -122,6 +124,7 @@ Filter format examples:
Filters: &filters,
Interactive: &interactive,
Type: &listType,
Trace: &trace,
}
err := list.Start(input)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions cmd/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,22 @@ var sessionCmd = &cobra.Command{
profile := cfg.GetString("profile")
region := cfg.GetString("region")
mfaToken := cfg.GetString("mfa")
trace := log.IsLevelEnabled(log.TraceLevel)
log.WithFields(log.Fields{
"target": target,
"type": targetType,
"region": region,
"profile": profile,
"mfa": mfaToken,
"trace": trace,
}).Debug("Session inputs")
input := &session.StartInput{
Target: &target,
TargetType: &targetType,
Region: &region,
Profile: &profile,
MFAToken: &mfaToken,
Trace: &trace,
}
// returns err
return session.Start(input)
Expand Down
3 changes: 3 additions & 0 deletions cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var (
genKeyPair := cfg.GetBool("gen-key-pair")
genKeyDir := cfg.GetString("gen-key-dir")
mfaToken := cfg.GetString("mfa")
trace := log.IsLevelEnabled(log.TraceLevel)
if genKeyPair {
stat, err := os.Stat(genKeyDir)
if !(err == nil && stat.IsDir()) {
Expand Down Expand Up @@ -79,6 +80,7 @@ var (
"os-user": OSUser,
"gen-key-pair": genKeyPair,
"gen-key-dir": genKeyDir,
"trace": trace,
}).Debug("ssh inputs")
input := &ssh.StartInput{
Target: &target,
Expand All @@ -90,6 +92,7 @@ var (
Region: &region,
Profile: &profile,
MFAToken: &mfaToken,
Trace: &trace,
}
// returns err
return ssh.Start(input)
Expand Down
2 changes: 2 additions & 0 deletions pkg/aws/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ go_library(
importpath = "github.com/danmx/sigil/pkg/aws",
visibility = ["//visibility:public"],
deps = [
"//pkg/aws/log:go_default_library",
"//pkg/os:go_default_library",
"@com_github_aws_aws_sdk_go//aws:go_default_library",
"@com_github_aws_aws_sdk_go//aws/client:go_default_library",
"@com_github_aws_aws_sdk_go//aws/credentials/stscreds:go_default_library",
"@com_github_aws_aws_sdk_go//aws/session:go_default_library",
"@com_github_aws_aws_sdk_go//service/ec2:go_default_library",
Expand Down
28 changes: 24 additions & 4 deletions pkg/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import (
"os/exec"
"os/signal"
"syscall"
"time"

logger "github.com/danmx/sigil/pkg/aws/log"
sigilOS "github.com/danmx/sigil/pkg/os"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
log "github.com/sirupsen/logrus"
)

const execEnvVar = "AWS_EXECUTION_ENV"

// Provider contains necessary components like the session
type Provider struct {
filters Filters
Expand All @@ -32,6 +33,7 @@ type Config struct {
Region string
Profile string
MFAToken string
Trace bool
}

// Filters grouped per type
Expand Down Expand Up @@ -82,8 +84,15 @@ type SessionFilters struct {
}

const (
maxResults int64 = 50
pluginName string = "session-manager-plugin"
execEnvVar = "AWS_EXECUTION_ENV"
maxResults int64 = 50
pluginName = "session-manager-plugin"
// API calls retry configuration
numMaxRetries = 10
minRetryDelay = 1 * time.Second
minThrottleDelay = 2 * time.Second
maxRetryDelay = client.DefaultRetryerMaxRetryDelay
maxThrottleDelay = client.DefaultRetryerMaxThrottleDelay
// TargetTypeInstanceID points to an instance ID type
TargetTypeInstanceID = "instance-id"
// TargetTypePrivateDNS points to a private DNS type
Expand All @@ -102,6 +111,17 @@ func (p *Provider) NewWithConfig(c *Config) error {
Profile: c.Profile,
}
awsConfig := aws.NewConfig()
if c.Trace {
awsConfig.LogLevel = aws.LogLevel(aws.LogDebugWithRequestRetries)
awsConfig.Logger = logger.NewTraceLogger()
}
awsConfig.Retryer = client.DefaultRetryer{
NumMaxRetries: numMaxRetries,
MinRetryDelay: minRetryDelay,
MinThrottleDelay: minThrottleDelay,
MaxRetryDelay: maxRetryDelay,
MaxThrottleDelay: maxThrottleDelay,
}
awsConfig.Region = aws.String(c.Region)
options.Config = *awsConfig
sess, err := session.NewSessionWithOptions(options)
Expand Down
12 changes: 12 additions & 0 deletions pkg/aws/log/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["log.go"],
importpath = "github.com/danmx/sigil/pkg/aws/log",
visibility = ["//visibility:public"],
deps = [
"@com_github_aws_aws_sdk_go//aws:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
],
)
19 changes: 19 additions & 0 deletions pkg/aws/log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package aws

import (
"github.com/aws/aws-sdk-go/aws"
log "github.com/sirupsen/logrus"
)

// A traceLogger provides a minimalistic logger satisfying the aws.Logger interface.
type traceLogger struct{}

// newTraceLogger returns a Logger which will write log messages to current logger
func NewTraceLogger() aws.Logger {
return &traceLogger{}
}

// Log logs the parameters to the stdlib logger. See log.Println.
func (l traceLogger) Log(args ...interface{}) {
log.Trace(args) //nolint:govet // AWS Logger to Logrus
}
2 changes: 2 additions & 0 deletions pkg/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type StartInput struct {
Region *string
Profile *string
Filters *aws.Filters
Trace *bool
}

// Start will output a ist of all available EC2 instances
Expand All @@ -56,6 +57,7 @@ func Start(input *StartInput) error {
Region: *input.Region,
Profile: *input.Profile,
MFAToken: *input.MFAToken,
Trace: *input.Trace,
})
if err != nil {
log.Error(err)
Expand Down
2 changes: 2 additions & 0 deletions pkg/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type StartInput struct {
MFAToken *string
Region *string
Profile *string
Trace *bool
}

// Start will start a session in chosen instance
Expand All @@ -30,6 +31,7 @@ func (input *StartInput) start(provider aws.CloudInstances) error {
Region: *input.Region,
Profile: *input.Profile,
MFAToken: *input.MFAToken,
Trace: *input.Trace,
})
if err != nil {
log.Error("Failed to generate new provider")
Expand Down
6 changes: 6 additions & 0 deletions pkg/session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ func TestStart(t *testing.T) {
mfa := "123456"
region := "eu-west-1"
profile := "west"
trace := false
input := StartInput{
MFAToken: &mfa,
Region: &region,
Profile: &profile,
Trace: &trace,
}
// Instance ID
target := "i-xxxxxxxxxxxxxxxx1"
Expand All @@ -36,6 +38,7 @@ func TestStart(t *testing.T) {
Region: *input.Region,
Profile: *input.Profile,
MFAToken: *input.MFAToken,
Trace: *input.Trace,
})).Return(nil),
m.EXPECT().StartSession(gomock.Eq(*input.TargetType), gomock.Eq(*input.Target)).Return(nil),
)
Expand All @@ -48,6 +51,7 @@ func TestStart(t *testing.T) {
Region: *input.Region,
Profile: *input.Profile,
MFAToken: *input.MFAToken,
Trace: *input.Trace,
})).Return(nil),
m.EXPECT().StartSession(gomock.Eq(*input.TargetType), gomock.Eq(*input.Target)).Return(nil),
)
Expand All @@ -60,6 +64,7 @@ func TestStart(t *testing.T) {
Region: *input.Region,
Profile: *input.Profile,
MFAToken: *input.MFAToken,
Trace: *input.Trace,
})).Return(nil),
m.EXPECT().StartSession(gomock.Eq(*input.TargetType), gomock.Eq(*input.Target)).Return(nil),
)
Expand All @@ -71,6 +76,7 @@ func TestStart(t *testing.T) {
Region: *input.Region,
Profile: *input.Profile,
MFAToken: *input.MFAToken,
Trace: *input.Trace,
})).Return(nil),
m.EXPECT().StartSession(gomock.Eq(*input.TargetType), gomock.Eq(*input.Target)).Return(nil),
)
Expand Down
2 changes: 2 additions & 0 deletions pkg/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type StartInput struct {
MFAToken *string
Region *string
Profile *string
Trace *bool
}

// Start will start ssh session
Expand All @@ -43,6 +44,7 @@ func (input *StartInput) start(provider aws.CloudSSH) error {
Region: *input.Region,
Profile: *input.Profile,
MFAToken: *input.MFAToken,
Trace: *input.Trace,
})
if err != nil {
log.Error(err)
Expand Down
9 changes: 9 additions & 0 deletions pkg/ssh/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestStart(t *testing.T) {
pubKey := path.Join(os.TempDir(), "sigil_test.pub")
osUser := "ec2-user"
genKey := true
trace := false
input := StartInput{
MFAToken: &mfa,
Region: &region,
Expand All @@ -40,13 +41,15 @@ func TestStart(t *testing.T) {
PublicKey: &pubKey,
OSUser: &osUser,
GenKeyPair: &genKey,
Trace: &trace,
}

gomock.InOrder(
m.EXPECT().NewWithConfig(gomock.Eq(&aws.Config{
Region: *input.Region,
Profile: *input.Profile,
MFAToken: *input.MFAToken,
Trace: *input.Trace,
})).Return(nil),
m.EXPECT().StartSSH(
gomock.Eq(*input.TargetType),
Expand All @@ -72,13 +75,15 @@ func TestStart(t *testing.T) {
PublicKey: &pubKey,
OSUser: &osUser,
GenKeyPair: &genKey,
Trace: &trace,
}

gomock.InOrder(
m.EXPECT().NewWithConfig(gomock.Eq(&aws.Config{
Region: *input.Region,
Profile: *input.Profile,
MFAToken: *input.MFAToken,
Trace: *input.Trace,
})).Return(nil),
m.EXPECT().StartSSH(
gomock.Eq(*input.TargetType),
Expand All @@ -104,13 +109,15 @@ func TestStart(t *testing.T) {
PublicKey: &pubKey,
OSUser: &osUser,
GenKeyPair: &genKey,
Trace: &trace,
}

gomock.InOrder(
m.EXPECT().NewWithConfig(gomock.Eq(&aws.Config{
Region: *input.Region,
Profile: *input.Profile,
MFAToken: *input.MFAToken,
Trace: *input.Trace,
})).Return(nil),
m.EXPECT().StartSSH(
gomock.Eq(*input.TargetType),
Expand All @@ -135,13 +142,15 @@ func TestStart(t *testing.T) {
PublicKey: &pubKey,
OSUser: &osUser,
GenKeyPair: &genKey,
Trace: &trace,
}

gomock.InOrder(
m.EXPECT().NewWithConfig(gomock.Eq(&aws.Config{
Region: *input.Region,
Profile: *input.Profile,
MFAToken: *input.MFAToken,
Trace: *input.Trace,
})).Return(nil),
m.EXPECT().StartSSH(
gomock.Eq(*input.TargetType),
Expand Down
2 changes: 1 addition & 1 deletion scripts/pre-commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test(){
}

fmt() {
bazelisk run :gofmt
bazelisk run :gazelle && bazelisk run :gofmt
}

lint() {
Expand Down

0 comments on commit 53f6ace

Please sign in to comment.