Skip to content

Commit

Permalink
Set credentials path if value AWS_SHARED_CREDENTIALS_FILE is set
Browse files Browse the repository at this point in the history
  • Loading branch information
kenzo0107 committed Dec 5, 2019
1 parent 57cf782 commit e493481
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions omssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"runtime"
"time"

"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2instanceconnect"
"github.com/patrickmn/go-cache"
"github.com/urfave/cli"
"golang.org/x/crypto/ssh"
Expand All @@ -23,7 +25,7 @@ const (
var defCredentialsPath string

func init() {
credentialsPath := os.Getenv("CREDENTIALS_PATH")
credentialsPath := os.Getenv("AWS_SHARED_CREDENTIALS_FILE")
if credentialsPath != "" {
defCredentialsPath = credentialsPath
} else {
Expand All @@ -48,12 +50,16 @@ func Pre(c *cli.Context) {
return
}

// get ec2 list
ec2Client := awsapi.NewEC2(sess)
ec2List, _ := ec2Client.GetEC2List()
// get list of ec2 instances
ec2Svc := ec2.New(sess)
ec2Instances, err := awsapi.DescribeRunningEC2Instances(ec2Svc)
if err != nil {
log.Fatal(err)
return
}

// select an ec2 instance
ec2, err := awsapi.FinderEC2Info(ec2List)
ec2, err := awsapi.FinderEC2Instance(ec2Instances)
if err != nil {
log.Fatal(err)
return
Expand All @@ -72,11 +78,15 @@ func Pre(c *cli.Context) {
publicKey, privateKey := sshKeyGen()

// use ec2 instance connect to send public key
e := awsapi.NewEC2InstanceConnect(sess)
e.SendSSHPubKey(user, ec2.InstanceID, publicKey, ec2.AvailabilityZone)
ec2instanceconnectSvc := ec2instanceconnect.New(sess)
r, err := awsapi.SendSSHPubKey(ec2instanceconnectSvc, user, ec2.InstanceID, publicKey, ec2.AvailabilityZone)
if err != nil || !r {
log.Fatal(err)
return
}

// ssh -i <temporary ssh private key> <user>@<public ip address>
fmt.Printf("ssh %s@%s -p %s [%s]\n", user, ec2.PublicIPAddress, c.String("port"), ec2.InstanceID)
log.Printf("ssh %s@%s -p %s [%s]\n", user, ec2.PublicIPAddress, c.String("port"), ec2.InstanceID)
doSSH(user, ec2.PublicIPAddress, c.String("port"), privateKey)
}

Expand Down

0 comments on commit e493481

Please sign in to comment.