Skip to content

Commit

Permalink
Implement SetupSSH on Windows
Browse files Browse the repository at this point in the history
[#145070485]

Signed-off-by: Charlie Vieth <[email protected]>
  • Loading branch information
davidjahn authored and bot committed May 17, 2017
1 parent 3aa0324 commit 75333b5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
3 changes: 2 additions & 1 deletion bin/golint
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ errors=$(
| grep -v 'underscore in package name' \
| grep -v 'bootstrapper/spec/' \
| grep -v 'platform/cert/fakes/fake_manager.go' \
| grep -v 'jobsupervisor/pipe/syslog/syslog.go'
| grep -v 'jobsupervisor/pipe/syslog/syslog.go' \
| grep -v 'platform/syscall_windows.go'
)
set -e

Expand Down
2 changes: 1 addition & 1 deletion platform/syscall_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import "errors"

var ErrNotImplemented = errors.New("not implemented")

func CreateUserProfile(username string) error {
func createUserProfile(username string) error {
return ErrNotImplemented
}
2 changes: 1 addition & 1 deletion platform/syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func userExists(name string) bool {
return err == nil && t == syscall.SidTypeUser
}

func CreateUserProfile(name string) error {
func createUserProfile(name string) error {
if userExists(name) {
return fmt.Errorf("user account already exists: %s", name)
}
Expand Down
39 changes: 35 additions & 4 deletions platform/windows_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (p WindowsPlatform) SetupRuntimeConfiguration() (err error) {
}

func (p WindowsPlatform) CreateUser(username, _ string) error {
if err := CreateUserProfile(username); err != nil {
if err := createUserProfile(username); err != nil {
return bosherr.WrapError(err, "CreateUser: creating user")
}
return nil
Expand All @@ -128,8 +128,32 @@ func (p WindowsPlatform) SetupRootDisk(ephemeralDiskPath string) (err error) {
return
}

func (p WindowsPlatform) SetupSSH(publicKey []string, username string) (err error) {
return
func (p WindowsPlatform) SetupSSH(publicKey []string, username string) error {

homedir := filepath.Join("C:\\", "Users", username)
if _, err := p.fs.Stat(homedir); err != nil {
return bosherr.WrapErrorf(err, "missing home directory for user: %s", username)
}

sshdir := filepath.Join(homedir, ".ssh")
if err := p.fs.MkdirAll(sshdir, sshDirPermissions); err != nil {
return bosherr.WrapError(err, "creating .ssh directory")
}

authkeysPath := filepath.Join(sshdir, "authorized_keys")
publicKeyString := strings.Join(publicKey, "\n")
if err := p.fs.WriteFileString(authkeysPath, publicKeyString); err != nil {
return bosherr.WrapErrorf(err, "Creating authorized_keys file: %s", authkeysPath)
}

// Grant sshd service read access to the authorized_keys file.
_, stderr, _, err := p.cmdRunner.RunCommand("icacls.exe", authkeysPath, "/grant", "NT SERVICE\\SSHD:(R)")
if err != nil {
return bosherr.WrapErrorf(err, "Setting ACL on authorized_keys file (%s): %s",
authkeysPath, stderr)
}

return nil
}

func (p WindowsPlatform) SetUserPassword(user, encryptedPwd string) (err error) {
Expand All @@ -141,6 +165,7 @@ func (p WindowsPlatform) SaveDNSRecords(dnsRecords boshsettings.DNSRecords, host
if windir == "" {
return bosherr.Error("SaveDNSRecords: missing %WINDIR% env variable")
}

etcdir := filepath.Join(windir, "System32", "Drivers", "etc")
if err := p.fs.MkdirAll(etcdir, 0755); err != nil {
return bosherr.WrapError(err, "SaveDNSRecords: creating etc directory")
Expand Down Expand Up @@ -353,7 +378,13 @@ func (p WindowsPlatform) GetDefaultNetwork() (boshsettings.Network, error) {
}

func (p WindowsPlatform) GetHostPublicKey() (string, error) {
return "", nil
keypath := filepath.Join("C:\\", "Program Files", "OpenSSH", "ssh_host_rsa_key.pub")

key, err := p.fs.ReadFileString(keypath)
if err != nil {
return "", bosherr.WrapErrorf(err, "Unable to read host public key file: %s", keypath)
}
return key, nil
}

func (p WindowsPlatform) DeleteARPEntryWithIP(ip string) error {
Expand Down

0 comments on commit 75333b5

Please sign in to comment.