Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IZE-682 added explain mode for ize tunnel commands #522

Merged
merged 1 commit into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion internal/commands/tunnel_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ import (
"github.com/spf13/cobra"
)

var explainTunnelDownTmpl = `
# Change to the dir and send an exit request
(cd {{.EnvDir}} && $(aws ssm get-parameter --name "/{{.Env}}/terraform-output" --with-decryption | jq -r '.Parameter.Value' | base64 -d | jq -r '.cmd.value.tunnel.down'))
`

type TunnelDownOptions struct {
Config *config.Project
Config *config.Project
Explain bool
}

func NewTunnelDownOptions(project *config.Project) *TunnelDownOptions {
Expand All @@ -33,6 +39,15 @@ func NewCmdTunnelDown(project *config.Project) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

if o.Explain {
err := o.Config.Generate(explainTunnelDownTmpl, nil)
if err != nil {
return err
}

return nil
}

err := o.Complete()
if err != nil {
return err
Expand All @@ -52,6 +67,8 @@ func NewCmdTunnelDown(project *config.Project) *cobra.Command {
},
}

cmd.Flags().BoolVar(&o.Explain, "explain", false, "bash alternative shown")

return cmd
}

Expand Down
22 changes: 20 additions & 2 deletions internal/commands/tunnel_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ import (
"github.com/spf13/cobra"
)

var explainTunnelStatusTmpl = `
# Change to the dir and get status
(cd {{.EnvDir}} && $(aws ssm get-parameter --name "/{{.Env}}/terraform-output" --with-decryption | jq -r '.Parameter.Value' | base64 -d | jq -r '.cmd.value.tunnel.status'))
`

type TunnelStatusOptions struct {
Config *config.Project
UI terminal.UI
Config *config.Project
UI terminal.UI
Explain bool
}

func NewTunnelStatusOptions(project *config.Project) *TunnelStatusOptions {
Expand All @@ -29,6 +35,16 @@ func NewCmdTunnelStatus(project *config.Project) *cobra.Command {
Long: "Tunnel running status",
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

if o.Explain {
err := o.Config.Generate(explainTunnelStatusTmpl, nil)
if err != nil {
return err
}

return nil
}

err := o.Complete()
if err != nil {
return err
Expand All @@ -48,6 +64,8 @@ func NewCmdTunnelStatus(project *config.Project) *cobra.Command {
},
}

cmd.Flags().BoolVar(&o.Explain, "explain", false, "bash alternative shown")

return cmd
}

Expand Down
30 changes: 30 additions & 0 deletions internal/commands/tunnel_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartS
{{end}}
`

var explainTunnelUpTmpl = `
# Set variables
SSH_CONFIG={{.EnvDir}}/ssh.config
SSH_PUBLIC_KEY=$(cat ~/.ssh/id_rsa.pub)

# Get bastion instance id
BASTION_INSTANCE_ID=$(aws ssm get-parameter --name "/{{.Env}}/terraform-output" --with-decryption | jq -r '.Parameter.Value' | base64 -d | jq -r '.bastion_instance_id.value'

# Get ssh config
aws ssm get-parameter --name "/{{.Env}}/terraform-output" --with-decryption | jq -r '.Parameter.Value' | base64 -d | jq -r '.ssh_forward_config.value[]' > $SSH_CONFIG

# Send ssh public key to instance
aws ssm send-command --instance-ids $BASTION_INSTANCE_ID --document-name AWS-RunShellScript --comment 'Add an SSH public key to authorized_keys' --parameters '{"commands": ["grep -qR \"$(SSH_PUBLIC_KEY)\" /home/ubuntu/.ssh/authorized_keys || echo \"$(SSH_PUBLIC_KEY)\" >> /home/ubuntu/.ssh/authorized_keys"]}' 1> /dev/null)

# Change to the dir and up tunnel
(cd {{.EnvDir}} && $(aws ssm get-parameter --name "/{{.Env}}/terraform-output" --with-decryption | jq -r '.Parameter.Value' | base64 -d | jq -r '.cmd.value.tunnel.up') -F $SSH_CONFIG)
`

type TunnelUpOptions struct {
Config *config.Project
PrivateKeyFile string
Expand All @@ -48,6 +66,7 @@ type TunnelUpOptions struct {
ForwardHost []string
StrictHostKeyChecking bool
Metadata bool
Explain bool
}

func NewTunnelUpFlags(project *config.Project) *TunnelUpOptions {
Expand All @@ -65,6 +84,16 @@ func NewCmdTunnelUp(project *config.Project) *cobra.Command {
Long: "Open tunnel with sending ssh key to remote server",
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

if o.Explain {
err := o.Config.Generate(explainTunnelUpTmpl, nil)
if err != nil {
return err
}

return nil
}

err := o.Complete()
if err != nil {
return err
Expand All @@ -90,6 +119,7 @@ func NewCmdTunnelUp(project *config.Project) *cobra.Command {
cmd.Flags().StringVar(&o.PrivateKeyFile, "ssh-private-key", "", "set ssh key private path")
cmd.PersistentFlags().BoolVar(&o.StrictHostKeyChecking, "strict-host-key-checking", true, "set strict host key checking")
cmd.PersistentFlags().BoolVar(&o.Metadata, "use-ec2-metadata", false, "send ssh key to EC2 metadata (work only for Ubuntu versions > 20.0)")
cmd.Flags().BoolVar(&o.Explain, "explain", false, "bash alternative shown")

return cmd
}
Expand Down