-
Notifications
You must be signed in to change notification settings - Fork 5
/
root_cli.go
106 lines (84 loc) · 2.59 KB
/
root_cli.go
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
package main
import (
"fmt"
"os"
"runtime/debug"
"github.com/spf13/cobra"
"github.com/miquella/ssh-proxy-agent/lib/proxyagent"
)
// RootCLI is the root command for the `ssh-proxy-agent` entrypoint
var RootCLI = &cobra.Command{
Use: "ssh-proxy-agent",
Short: "SSH-Proxy-Agent creates an ssh-agent proxy",
PreRun: versionPreRun,
RunE: shellRunE,
SilenceUsage: true,
}
var doctor bool
var interactive bool
var version bool
var agentConfig = proxyagent.AgentConfig{}
var shell = proxyagent.Spawn{}
func init() {
RootCLI.Flags().BoolVarP(&interactive, "shell", "l", false, "spawn an interactive shell")
RootCLI.Flags().BoolVarP(&doctor, "doctor", "", false, "verify if a spawned session is running correctly")
RootCLI.Flags().BoolVar(&version, "version", false, "display version of ssh-proxy-agent")
RootCLI.Flags().BoolVar(&agentConfig.GenerateRSAKey, "generate-key", false, "generate RSA key pair (default: false)")
RootCLI.Flags().BoolVar(&agentConfig.DisableProxy, "no-proxy", false, "disable forwarding to an upstream agent (default: false)")
RootCLI.Flags().BoolVar(&agentConfig.ExposeUnsigned, "expose-unsigned", false, "expose both signed and unsigned versions of keys when signing is enabled (default: false)")
RootCLI.Flags().StringSliceVar(&agentConfig.ValidPrincipals, "valid-principals", []string{proxyagent.DefaultPrincipal()}, "valid principals for Vault key signing")
RootCLI.Flags().StringVar(&agentConfig.VaultSigningUrl, "vault-signing-url", "", "HashiCorp Vault url to sign SSH keys")
}
func shellRunE(cmd *cobra.Command, args []string) error {
if doctor {
proxyagent.Doctor()
return nil
}
if !interactive {
return cmd.Usage()
}
var err error
shell.Agent, err = proxyagent.SetupAgent(agentConfig)
if err != nil {
return err
}
shell.Command = loginShellCommand()
return shell.Run()
}
func loginShellCommand() []string {
shell := os.Getenv("SHELL")
if shell == "" {
shell = "/bin/sh"
}
return []string{shell, "--login"}
}
func versionPreRun(*cobra.Command, []string) {
if !version {
return
}
version := "(devel)"
vcsRevision := ""
vcsModified := false
buildInfo, ok := debug.ReadBuildInfo()
if ok {
version = buildInfo.Main.Version
for _, buildSetting := range buildInfo.Settings {
switch buildSetting.Key {
case "vcs.revision":
vcsRevision = buildSetting.Value
case "vcs.modified":
vcsModified = buildSetting.Value == "true"
}
}
}
fmt.Printf("ssh-proxy-agent %s", version)
if vcsRevision != "" {
fmt.Printf(" (%s", vcsRevision)
if vcsModified {
fmt.Print("-dirty")
}
fmt.Print(")")
}
fmt.Println()
os.Exit(0)
}