forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh.go
134 lines (108 loc) · 3.27 KB
/
ssh.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package action
import (
"errors"
"path"
boshplatform "github.com/cloudfoundry/bosh-agent/platform"
boshsettings "github.com/cloudfoundry/bosh-agent/settings"
boshdirs "github.com/cloudfoundry/bosh-agent/settings/directories"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
type SSHAction struct {
settingsService boshsettings.Service
platform boshplatform.Platform
dirProvider boshdirs.Provider
logger boshlog.Logger
}
func NewSSH(
settingsService boshsettings.Service,
platform boshplatform.Platform,
dirProvider boshdirs.Provider,
logger boshlog.Logger,
) (action SSHAction) {
action.settingsService = settingsService
action.platform = platform
action.dirProvider = dirProvider
action.logger = logger
return
}
func (a SSHAction) IsAsynchronous(_ ProtocolVersion) bool {
return false
}
func (a SSHAction) IsPersistent() bool {
return false
}
func (a SSHAction) IsLoggable() bool {
return true
}
type SSHParams struct {
UserRegex string `json:"user_regex"`
User string
PublicKey string `json:"public_key"`
}
type SSHResult struct {
Command string `json:"command"`
Status string `json:"status"`
IP string `json:"ip,omitempty"`
HostPublicKey string `json:"host_public_key,omitempty"`
}
func (a SSHAction) Run(cmd string, params SSHParams) (SSHResult, error) {
switch cmd {
case "setup":
return a.setupSSH(params)
case "cleanup":
return a.cleanupSSH(params)
}
return SSHResult{}, errors.New("Unknown command for SSH method")
}
func (a SSHAction) setupSSH(params SSHParams) (SSHResult, error) {
var result SSHResult
boshSSHPath := path.Join(a.dirProvider.BaseDir(), "bosh_ssh")
// this must happen first so unfulfilled prerequistes on windows
// can stop the creation of new users
publicKey, err := a.platform.GetHostPublicKey()
if err != nil {
return result, bosherr.WrapError(err, "Getting host public key")
}
err = a.platform.CreateUser(params.User, boshSSHPath)
if err != nil {
return result, bosherr.WrapError(err, "Creating user")
}
err = a.platform.AddUserToGroups(params.User, []string{boshsettings.VCAPUsername, boshsettings.AdminGroup, boshsettings.SudoersGroup, boshsettings.SshersGroup})
if err != nil {
return result, bosherr.WrapError(err, "Adding user to groups")
}
err = a.platform.SetupSSH([]string{params.PublicKey}, params.User)
if err != nil {
return result, bosherr.WrapError(err, "Setting ssh public key")
}
settings := a.settingsService.GetSettings()
defaultIP, found := settings.Networks.DefaultIP()
if !found {
return result, errors.New("No default ip could be found")
}
result = SSHResult{
Command: "setup",
Status: "success",
IP: defaultIP,
HostPublicKey: publicKey,
}
return result, nil
}
func (a SSHAction) cleanupSSH(params SSHParams) (SSHResult, error) {
err := a.platform.DeleteEphemeralUsersMatching(params.UserRegex)
if err != nil {
return SSHResult{}, bosherr.WrapError(err, "SSH Cleanup: Deleting Ephemeral Users")
}
result := SSHResult{
Command: "cleanup",
Status: "success",
}
return result, nil
}
func (a SSHAction) Resume() (interface{}, error) {
return nil, errors.New("not supported")
}
func (a SSHAction) Cancel() error {
return errors.New("not supported")
}