Skip to content

Commit 7e1eb87

Browse files
authored
Merge pull request #21 from jesserockz/ssh-optional-ec2instance-connect
Allow ignoring ec2-instance-connect for ssh access
2 parents 17b3380 + 27d32b9 commit 7e1eb87

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

cmd/ssh.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ var sshCmd = &cobra.Command{
1919
if containerName == "" {
2020
containerName = service
2121
}
22+
2223
exitCode, err := lib.ConnectSSH(
2324
viper.GetString("profile"),
2425
viper.GetString("cluster"),
25-
viper.GetString("task_definition"),
26+
viper.GetString("ssh.task_definition"),
2627
containerName,
2728
viper.GetString("ssh.shell"),
2829
service,
2930
viper.GetString("ssh.instance_user"),
31+
viper.GetBool("ssh.push_ssh_key"),
3032
)
3133
if err != nil {
3234
log.WithError(err).Error("Can't execute ssh")
@@ -38,5 +40,8 @@ var sshCmd = &cobra.Command{
3840
func init() {
3941
rootCmd.AddCommand(sshCmd)
4042
sshCmd.PersistentFlags().StringP("task_definition", "t", "", "name of task definition to use (required)")
41-
viper.BindPFlag("task_definition", runCmd.PersistentFlags().Lookup("task_definition"))
43+
viper.BindPFlag("ssh.task_definition", runCmd.PersistentFlags().Lookup("task_definition"))
44+
45+
viper.SetDefault("ssh.push_ssh_key", true)
46+
viper.SetDefault("ssh.task_definition", viper.GetString("task_definition"))
4247
}

lib/ssh.go

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
// ConnectSSH runs ssh with some magic parameters to connect to running containers on AWS ECS
19-
func ConnectSSH(profile, cluster, taskDefinitionName, containerName, shell, service, instanceUser string) (exitCode int, err error) {
19+
func ConnectSSH(profile, cluster, taskDefinitionName, containerName, shell, service, instanceUser string, pushSSHKey bool) (exitCode int, err error) {
2020
err = makeSession(profile)
2121
if err != nil {
2222
return 1, err
@@ -83,36 +83,39 @@ func ConnectSSH(profile, cluster, taskDefinitionName, containerName, shell, serv
8383
}
8484

8585
ec2Instance := ec2Result.Reservations[0].Instances[0]
86-
ec2ICSvc := ec2instanceconnect.New(localSession)
8786

88-
ctx.WithField("instance_id", aws.StringValue(ec2Instance.InstanceId)).Info("Pushing SSH key...")
87+
if pushSSHKey {
88+
ec2ICSvc := ec2instanceconnect.New(localSession)
8989

90-
sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
91-
if err != nil {
92-
ctx.WithError(err).Error("Can't connect to the ssh agent")
93-
return 1, err
94-
}
90+
ctx.WithField("instance_id", aws.StringValue(ec2Instance.InstanceId)).Info("Pushing SSH key...")
9591

96-
keys, err := agent.NewClient(sshAgent).List()
97-
if err != nil {
98-
ctx.WithError(err).Error("Can't get public keys from ssh agent. Please ensure you have the ssh-agent running")
99-
return 1, err
100-
}
101-
if len(keys) < 1 {
102-
ctx.Error("Can't get public keys from ssh agent. Please ensure you have at least one identity added (with ssh-add)")
103-
return 1, err
104-
}
105-
pubkey := keys[0].String()
92+
sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
93+
if err != nil {
94+
ctx.WithError(err).Error("Can't connect to the ssh agent")
95+
return 1, err
96+
}
10697

107-
_, err = ec2ICSvc.SendSSHPublicKey(&ec2instanceconnect.SendSSHPublicKeyInput{
108-
InstanceId: ec2Instance.InstanceId,
109-
InstanceOSUser: aws.String(instanceUser),
110-
AvailabilityZone: ec2Instance.Placement.AvailabilityZone,
111-
SSHPublicKey: aws.String(pubkey),
112-
})
113-
if err != nil {
114-
ctx.WithError(err).Error("Can't push SSH key")
115-
return 1, err
98+
keys, err := agent.NewClient(sshAgent).List()
99+
if err != nil {
100+
ctx.WithError(err).Error("Can't get public keys from ssh agent. Please ensure you have the ssh-agent running")
101+
return 1, err
102+
}
103+
if len(keys) < 1 {
104+
ctx.Error("Can't get public keys from ssh agent. Please ensure you have at least one identity added (with ssh-add)")
105+
return 1, err
106+
}
107+
pubkey := keys[0].String()
108+
109+
_, err = ec2ICSvc.SendSSHPublicKey(&ec2instanceconnect.SendSSHPublicKeyInput{
110+
InstanceId: ec2Instance.InstanceId,
111+
InstanceOSUser: aws.String(instanceUser),
112+
AvailabilityZone: ec2Instance.Placement.AvailabilityZone,
113+
SSHPublicKey: aws.String(pubkey),
114+
})
115+
if err != nil {
116+
ctx.WithError(err).Error("Can't push SSH key")
117+
return 1, err
118+
}
116119
}
117120

118121
ctx.WithField("instance_id", aws.StringValue(ec2Instance.InstanceId)).Info("Connecting to container...")

0 commit comments

Comments
 (0)