Skip to content

Commit

Permalink
gofmt and nicer errors
Browse files Browse the repository at this point in the history
  • Loading branch information
aidansteele committed Jun 28, 2019
1 parent 72c7745 commit d4f3b8b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 14 deletions.
32 changes: 28 additions & 4 deletions cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package cmd

import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"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"
Expand All @@ -15,6 +18,7 @@ import (
"io/ioutil"
"net"
"os"
"strings"
)

func init() {
Expand All @@ -29,7 +33,27 @@ func init() {

info, err := authorize(instanceId, region, user, sshKeyPath)
if err != nil {
panic(err)
if awsErr, ok := errors.Cause(err).(awserr.Error); ok {
if awsErr.Code() == credentials.ErrNoValidProvidersFoundInChain.Code() {
fmt.Fprintln(os.Stderr, `
No AWS credentials found.
* You can specify one of the profiles from ~/.aws/config by setting the
AWS_PROFILE environment variable.
* You can set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and optionally
AWS_SESSION_TOKEN.`)
} else if strings.HasPrefix(awsErr.Code(), "InvalidInstanceID.") {
fmt.Fprintf(os.Stderr, `
No instance found with ID %s. Try specifying an explicit region using the
AWS_REGION environment variable.
`, instanceId)
}
return
} else {
panic(err)
}
}

err = connect(info.Address + ":22")
Expand All @@ -38,7 +62,7 @@ func init() {
}
},
}

cmd.PersistentFlags().String("instance-id", "", "")
cmd.PersistentFlags().String("region", "", "")
cmd.PersistentFlags().String("user", "ec2-user", "")
Expand All @@ -59,9 +83,9 @@ func authorize(instanceId, region, user, sshKeyPath string) (*ec2connect.Connect
}

sess, err := session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
SharedConfigState: session.SharedConfigEnable,
AssumeRoleTokenProvider: stscreds.StdinTokenProvider,
Config: *aws.NewConfig().WithRegion(region),//.WithLogLevel(aws.LogDebugWithHTTPBody),
Config: *aws.NewConfig().WithRegion(region), //.WithLogLevel(aws.LogDebugWithHTTPBody),
})
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func init() {
cmd := &cobra.Command{
Use: "install",
Short: "Remotely install EC2 Instance Connect on EC2 instance",
Long: ``,
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
},
}
Expand All @@ -18,4 +18,4 @@ func init() {

func install() error {
return nil
}
}
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
var RootCmd = &cobra.Command{
Use: "ec2connect",
Short: "",
Long: ``,
Long: ``,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Expand All @@ -47,9 +47,9 @@ func init() {

// initConfig reads in config file and ENV variables if set.
func initConfig() {
viper.AddConfigPath(".") // adding home directory as first search path
viper.AddConfigPath("$HOME") // adding home directory as first search path
viper.AutomaticEnv() // read in environment variables that match
viper.AddConfigPath(".") // adding home directory as first search path
viper.AddConfigPath("$HOME") // adding home directory as first search path
viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ Match exec "ec2connect match --host %n --user %r"
Include %s
`, myConfPath))
return err
}
}
1 change: 0 additions & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ Date: %s

RootCmd.AddCommand(cmd)
}

4 changes: 2 additions & 2 deletions pkg/ec2connect/authorize.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (c *Authorizer) Authorize(ctx context.Context, instanceId, user, sshKey str
}

return &ConnectionInfo{
Address: ip,
Address: ip,
RequestId: *r2.RequestId,
}, nil
}
Expand All @@ -65,4 +65,4 @@ func NormalizeKey(input string) (string, error) {
}

return string(ssh.MarshalAuthorizedKey(s.PublicKey())), nil
}
}

0 comments on commit d4f3b8b

Please sign in to comment.