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 2FA support #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion cloudwatch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"fmt"
"log"
"os"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
)

//CW provides the APIo peration methods for making requests to AWS cloudwatch logs.
Expand All @@ -18,7 +20,7 @@ type CW struct {
}

// New creates a new instance of the CW client
func New(awsProfile *string, awsRegion *string, log *log.Logger) *CW {
func New(awsProfile *string, awsRegion *string, mfa *bool, log *log.Logger) *CW {
//workaround to figure out the user actual home dir within a SNAP (rather than the sandboxed one)
//and access the .aws folder in its default location
if os.Getenv("SNAP_INSTANCE_NAME") != "" {
Expand Down Expand Up @@ -48,6 +50,11 @@ func New(awsProfile *string, awsRegion *string, log *log.Logger) *CW {
if awsRegion != nil {
opts.Config = aws.Config{Region: awsRegion}
}
if *mfa {
duration, _ := time.ParseDuration("1h")
opts.AssumeRoleTokenProvider = stscreds.StdinTokenProvider
opts.AssumeRoleDuration = duration
}

sess := session.Must(session.NewSessionWithOptions(opts))
return &CW{awsClwClient: cloudwatchlogs.New(sess),
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ var (
grep = tailCommand.Flag("grep", "Pattern to filter logs by. See http://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html for syntax.").
Short('g').Default("").String()
grepv = tailCommand.Flag("grepv", "Equivalent of grep --invert-match. Invert match pattern to filter logs by.").Short('v').Default("").String()
mfa = kp.Flag("mfa", "When target AWS profile has MFA enabled.").Short('m').Default("false").Bool()
)

func timestampToTime(timeStamp *string) (time.Time, error) {
Expand Down Expand Up @@ -173,7 +174,7 @@ func main() {
color.NoColor = true
}

c := cloudwatch.New(awsProfile, awsRegion, log)
c := cloudwatch.New(awsProfile, awsRegion, mfa, log)

switch cmd {
case "ls groups":
Expand Down