@@ -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