Repository navigation
Expand file tree
/
Copy pathssh.go
More file actions
114 lines (92 loc) · 3.12 KB
/
Copy pathssh.go
File metadata and controls
114 lines (92 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package cmd
import (
sim "github.com/kubernetes-simulator/simulator/pkg/simulator"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func newSSHConfigCommand(logger *logrus.Logger) *cobra.Command {
cmd := &cobra.Command{
Use: `config`,
Short: "Prints the stanzas to add to ssh config to connect to your cluster",
RunE: func(cmd *cobra.Command, args []string) error {
bucketName := viper.GetString("state-bucket")
scenariosDir := viper.GetString("scenarios-dir")
attackTag := viper.GetString("attack-container-tag")
tfDir := viper.GetString("tf-dir")
tfVarsDir := viper.GetString("tf-vars-dir")
disableIPDetection := true
simulator := sim.NewSimulator(
sim.WithLogger(logger),
sim.WithTfDir(tfDir),
sim.WithScenariosDir(scenariosDir),
sim.WithAttackTag(attackTag),
sim.WithBucketName(bucketName),
sim.WithoutIPDetection(disableIPDetection),
sim.WithTfVarsDir(tfVarsDir))
cfg, err := simulator.SSHConfig()
if err != nil {
return errors.Wrap(err, "Error getting SSH config")
}
shouldwrite, err := cmd.Flags().GetBool("write")
if err != nil {
return errors.Wrapf(err, "Error getting --write cli flag")
}
if !shouldwrite {
logger.Info(cfg)
}
err = simulator.SSHStateProvider.SaveSSHConfig(*cfg)
if err != nil {
return errors.Wrapf(err, "Error writing SSH config")
}
return nil
},
}
cmd.PersistentFlags().Bool("write", true, "Write the ssh config - this is isolated from your default SSH config")
return cmd
}
func newSSHAttackCommand(logger *logrus.Logger) *cobra.Command {
cmd := &cobra.Command{
Use: `attack`,
Short: "Connect to an attack container to complete the scenario",
RunE: func(cmd *cobra.Command, args []string) error {
bucketName := viper.GetString("state-bucket")
attackTag := viper.GetString("attack-container-tag")
tfDir := viper.GetString("tf-dir")
tfVarsDir := viper.GetString("tf-vars-dir")
disableIPDetection := true
simulator := sim.NewSimulator(
sim.WithLogger(logger),
sim.WithTfDir(tfDir),
sim.WithAttackTag(attackTag),
sim.WithBucketName(bucketName),
sim.WithoutIPDetection(disableIPDetection),
sim.WithTfVarsDir(tfVarsDir))
return simulator.Attack()
},
}
return cmd
}
func newSSHCommand() *cobra.Command {
cmd := &cobra.Command{
Use: `ssh <command>`,
Short: "Interact with the cluster",
SilenceUsage: true,
SilenceErrors: false,
}
logger := newLogger(viper.GetString("loglevel"))
cmd.AddCommand(newSSHConfigCommand(logger))
cmd.AddCommand(newSSHAttackCommand(logger))
cmd.PersistentFlags().StringP("tf-vars-dir", "v", "/home/launch/.kubesim",
"Path to a directory containing the terraform variables file")
if err := viper.BindPFlag("tf-vars-dir", cmd.PersistentFlags().Lookup("tf-vars-dir")); err != nil {
panic(err)
}
cmd.PersistentFlags().StringP("tf-dir", "t", "./terraform/deployments/AWS",
"Path to a directory containing the infrastructure scripts")
if err := viper.BindPFlag("tf-dir", cmd.PersistentFlags().Lookup("tf-dir")); err != nil {
panic(err)
}
return cmd
}