Skip to content

Commit

Permalink
added debug logs for tunnel up/down
Browse files Browse the repository at this point in the history
  • Loading branch information
psihachina committed Oct 24, 2022
1 parent 8d3237d commit 9a7256f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions internal/commands/tunnel_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ func (o *TunnelDownOptions) Run() error {
c := exec.Command(
"ssh", "-S", "bastion.sock", "-O", "exit", "",
)

if o.Config.LogLevel == "debug" {
c.Args = append(c.Args, "-vvv")
}

out := &bytes.Buffer{}
c.Stdout = out
c.Stderr = out
Expand Down
15 changes: 14 additions & 1 deletion internal/commands/tunnel_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,15 @@ func (o *TunnelUpOptions) Validate() error {

func (o *TunnelUpOptions) Run() error {
logrus.Debugf("public key path: %s", o.PublicKeyFile)
logrus.Debugf("private key path: %s", o.PrivateKeyFile)

pk, err := getPublicKey(o.PublicKeyFile)
if err != nil {
return fmt.Errorf("can't get public key: %s", err)
}

logrus.Debugf("public key:\n%s", pk)

if o.Metadata {
err = sendSSHPublicKey(o.BastionHostID, pk, o.Config.Session)
if err != nil {
Expand All @@ -202,6 +205,7 @@ func (o *TunnelUpOptions) Run() error {

func (o *TunnelUpOptions) upTunnel() (string, error) {
sshConfigPath := fmt.Sprintf("%s/ssh.config", o.Config.EnvDir)
logrus.Debugf("ssh config path: %s", sshConfigPath)

if err := setAWSCredentials(o.Config.Session); err != nil {
return "", fmt.Errorf("can't run tunnel: %w", err)
Expand Down Expand Up @@ -250,6 +254,11 @@ func (o *TunnelUpOptions) getSSHCommandArgs(sshConfigPath string) []string {
if _, err := os.Stat(o.PrivateKeyFile); !os.IsNotExist(err) {
args = append(args, "-i", o.PrivateKeyFile)
}

if o.Config.LogLevel == "debug" {
args = append(args, "-vvv")
}

return args
}

Expand All @@ -273,6 +282,8 @@ func getTerraformOutput(wr *SSMWrapper, env string) (terraformOutput, error) {
return terraformOutput{}, fmt.Errorf("can't get terraform output: %w", err)
}

logrus.Debugf("decoded terrafrom output: \n%s", value)

var output terraformOutput

err = json.Unmarshal(value, &output)
Expand Down Expand Up @@ -311,9 +322,11 @@ func sendSSHPublicKeyLegacy(bastionID string, key string, sess *session.Session)
// This command is executed in the bastion host and it checks if our public key is present. If it's not it uploads it to _authorized_keys file.
command := fmt.Sprintf(
`grep -qR "%s" /home/ubuntu/.ssh/authorized_keys || echo "%s" >> /home/ubuntu/.ssh/authorized_keys`,
key, key,
strings.TrimSpace(key), strings.TrimSpace(key),
)

logrus.Debugf("send command: \n%s", command)

_, err := ssm.New(sess).SendCommand(&ssm.SendCommandInput{
InstanceIds: []*string{&bastionID},
DocumentName: aws.String("AWS-RunShellScript"),
Expand Down

0 comments on commit 9a7256f

Please sign in to comment.