Skip to content

Commit

Permalink
Merge pull request #17 from jkueh/test-windows-input
Browse files Browse the repository at this point in the history
0.2.0 Release - Windows Fixes
  • Loading branch information
jkueh committed Dec 3, 2020
2 parents 380597a + d88f45e commit 5c3ae18
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 37 deletions.
4 changes: 2 additions & 2 deletions auto/build
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function buildCommand() {
go build -v \
-ldflags \
"-X 'main.cacheDir=${CACHE_DIR:-""}' -X 'main.version=${GITHUB_REF##*/}'" \
-o "${OUTPUT_PATH}" \
-o "${OUTPUT_PATH}${FILE_EXT:-""}" \
"${@}"
}

Expand All @@ -24,6 +24,6 @@ GOOS=linux GOARCH=amd64 CACHE_DIR="/tmp/roo" buildCommand "${@}"
GOOS=darwin GOARCH=amd64 CACHE_DIR="/tmp/roo" buildCommand "${@}"
GOOS=linux GOARCH=arm64 CACHE_DIR="/tmp/roo" buildCommand "${@}"
GOOS=linux GOARCH=mips64 CACHE_DIR="/tmp/roo" buildCommand "${@}"
GOOS=windows GOARCH=amd64 buildCommand "${@}"
GOOS=windows GOARCH=amd64 FILE_EXT=".exe" buildCommand "${@}"

chmod 755 build/roo_*
12 changes: 7 additions & 5 deletions cachedcredsprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/gob"
"log"
"os"
"runtime"
"time"

"github.com/aws/aws-sdk-go/aws/credentials"
Expand Down Expand Up @@ -122,11 +123,12 @@ func (p *CachedCredProvider) WriteNewCredentialsFromSTS(c *sts.Credentials, file
}
}

// Ensure the file permissions have been set19

err = cacheFile.Chmod(0600)
if err != nil {
log.Println("WARNING: Unable to set the file mode on the cache file", filePath, "-", err)
// Ensure the file permissions have been set - But skip this step for Windows.
if runtime.GOOS != "windows" {
err = cacheFile.Chmod(0600)
if err != nil {
log.Println("WARNING: Unable to set the file mode on the cache file", filePath, "-", err)
}
}

// Write to the cacheFile
Expand Down
2 changes: 1 addition & 1 deletion exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func executeCommand(commands ...string) error {
cmd := exec.Command("/usr/bin/env", commands...)
cmd := exec.Command(commands[0], commands[1:]...)
cmd.Stdout, cmd.Stderr, cmd.Stdin = os.Stdout, os.Stderr, os.Stdin
return cmd.Run()
}
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ module github.com/jkueh/roo
go 1.15

require (
github.com/aws/aws-sdk-go v1.35.33
github.com/go-sql-driver/mysql v1.5.0 // indirect
github.com/stretchr/testify v1.5.1 // indirect
gopkg.in/yaml.v2 v2.3.0
github.com/aws/aws-sdk-go v1.36.0
gopkg.in/yaml.v2 v2.4.0
)
24 changes: 10 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
github.com/aws/aws-sdk-go v1.34.18 h1:Mo/Clq3u1dQFzpg8YQqBii8m+Vl3fWIfHi6kXs5wpuM=
github.com/aws/aws-sdk-go v1.34.18/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
github.com/aws/aws-sdk-go v1.35.6 h1:yt7L4aU4lYSNGiIWAsaCFCh1fdVsdscVYOtKcpD3TpQ=
github.com/aws/aws-sdk-go v1.35.6/go.mod h1:tlPOdRjfxPBpNIwqDj61rmsnA85v9jc0Ps9+muhnW+k=
github.com/aws/aws-sdk-go v1.35.33 h1:8qPRZqCRok5i7VNN51k/Ky7CuyoXMdSs4mUfKyCqvPw=
github.com/aws/aws-sdk-go v1.35.33/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
github.com/aws/aws-sdk-go v1.36.0 h1:CscTrS+szX5iu34zk2bZrChnGO/GMtUYgMK1Xzs2hYo=
github.com/aws/aws-sdk-go v1.36.0/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc=
github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b h1:uwuIcX0g4Yl1NC5XAz37xsr2lTtcqevgzYNVt49waME=
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
26 changes: 23 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/aws/aws-sdk-go/service/sts"
)

const rooVersion = "0.1.1"
const rooVersion = "0.2.0"

var debug bool
var verbose bool
Expand Down Expand Up @@ -157,9 +157,20 @@ func main() {
if tokenNeedsRefresh && oneTimePasscode == "" {
oneTimePasscodePrompts := 0
oneTimePasscodeValid := false
var oneTimePasscodeValidationError error
for oneTimePasscodePrompts < 3 && oneTimePasscodeValid == false {
oneTimePasscode = getStringInputFromUser("MFA Code")
oneTimePasscodeValid = oneTimePasscodeIsValid(oneTimePasscode)
oneTimePasscodeInput := getStringInputFromUser("MFA Code")

// Ensure that trailing newline characters are removed (e.g. Windows will add \r at the end)
oneTimePasscode = strings.TrimRight(oneTimePasscodeInput, "\r\n")

if debug {
log.Println("MFA Code Provided:", oneTimePasscode)
}
oneTimePasscodeValid, oneTimePasscodeValidationError = oneTimePasscodeIsValid(oneTimePasscode)
if oneTimePasscodeValidationError != nil {
log.Println("Invalid MFA Code:", oneTimePasscodeValidationError)
}
oneTimePasscodePrompts++
}
if !oneTimePasscodeValid {
Expand Down Expand Up @@ -317,6 +328,15 @@ func main() {

fmt.Println("Profile written:", targetProfileName)
} else {
if debug {
log.Println("flag.Args() length:", len(flag.Args()))
}
if len(flag.Args()) == 0 { // Let's make sure we have something to run here...
// println() for STDERR output
println("Please provide a command to execute, e.g.:")
println("roo -role my_role_name aws sts get-caller-identity")
os.Exit(100)
}
if debug {
log.Println("We're going to want to run the following command:", flag.Args())
}
Expand Down
8 changes: 4 additions & 4 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ func getStringInputFromUser(prompt string) string {
return strings.TrimSuffix(text, "\n")
}

func oneTimePasscodeIsValid(code string) bool {
func oneTimePasscodeIsValid(code string) (bool, error) {
// Step 0 - It must be at least 6 characters
if len(code) < 6 {
return false
return false, fmt.Errorf("Code provided was less than 6 characters long")
}
_, err := strconv.Atoi(code)
if err != nil {
return false
return false, err
}
return true
return true, nil
}
6 changes: 6 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ package util

import (
"os"
"runtime"
)

// EnsureDirExists will create a directory if it doesn't exist.
func EnsureDirExists(dirPath string, fileMode os.FileMode) error {
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
return os.MkdirAll(dirPath, fileMode)
}
if runtime.GOOS != "windows" { // We skip the chmod step for Windows... Because we can't chmod.
return nil
}
// This won't return an error if we're setting it to 0700 - which we do in init.go, but for consistency with the other
// chmod operations we do, we remove it from the code path.
return os.Chmod(dirPath, fileMode)
}

Expand Down
16 changes: 12 additions & 4 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,33 @@ package main
import "testing"

func TestEmptyOTP(t *testing.T) {
if oneTimePasscodeIsValid("") {
if valid, err := oneTimePasscodeIsValid(""); err == nil {
t.Errorf("Empty OTP triggered did not trigger error as expected: %s", err)
} else if valid {
t.Errorf("Empty OTP passed validation")
}
}

func TestAlphabetOTP(t *testing.T) {
if oneTimePasscodeIsValid("hunter2") {
if valid, err := oneTimePasscodeIsValid("hunter2"); err == nil {
t.Errorf("Invalid (alphabetical) OTP did not trigger error as expected: %s", err)
} else if valid {
t.Errorf("Invalid (alphabetical) OTP passed validation")
}
}

func TestShortOTP(t *testing.T) {
if oneTimePasscodeIsValid("42069") {
if valid, err := oneTimePasscodeIsValid("42069"); err == nil {
t.Errorf("Invalid (short) OTP did not trigger error as expected: %s", err)
} else if valid {
t.Errorf("Invalid (short) OTP passed validation")
}
}

func TestOTP(t *testing.T) {
if !oneTimePasscodeIsValid("054389") {
if valid, err := oneTimePasscodeIsValid("054389"); err != nil {
t.Errorf("A valid OTP threw an error: %s", err)
} else if !valid {
t.Errorf("A valid OTP did not pass validation")
}
}

0 comments on commit 5c3ae18

Please sign in to comment.