-
Notifications
You must be signed in to change notification settings - Fork 115
/
windows_platform.go
488 lines (406 loc) · 13.7 KB
/
windows_platform.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package platform
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
boshdpresolv "github.com/cloudfoundry/bosh-agent/infrastructure/devicepathresolver"
boshcert "github.com/cloudfoundry/bosh-agent/platform/cert"
boshnet "github.com/cloudfoundry/bosh-agent/platform/net"
boshstats "github.com/cloudfoundry/bosh-agent/platform/stats"
boshvitals "github.com/cloudfoundry/bosh-agent/platform/vitals"
boshsettings "github.com/cloudfoundry/bosh-agent/settings"
boshdir "github.com/cloudfoundry/bosh-agent/settings/directories"
boshdirs "github.com/cloudfoundry/bosh-agent/settings/directories"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshcmd "github.com/cloudfoundry/bosh-utils/fileutil"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
)
type WindowsPlatform struct {
collector boshstats.Collector
fs boshsys.FileSystem
cmdRunner boshsys.CmdRunner
compressor boshcmd.Compressor
copier boshcmd.Copier
dirProvider boshdirs.Provider
vitalsService boshvitals.Service
netManager boshnet.Manager
devicePathResolver boshdpresolv.DevicePathResolver
certManager boshcert.Manager
defaultNetworkResolver boshsettings.DefaultNetworkResolver
auditLogger AuditLogger
uuidGenerator boshuuid.Generator
}
func NewWindowsPlatform(
collector boshstats.Collector,
fs boshsys.FileSystem,
cmdRunner boshsys.CmdRunner,
dirProvider boshdirs.Provider,
netManager boshnet.Manager,
certManager boshcert.Manager,
devicePathResolver boshdpresolv.DevicePathResolver,
logger boshlog.Logger,
defaultNetworkResolver boshsettings.DefaultNetworkResolver,
auditLogger AuditLogger,
uuidGenerator boshuuid.Generator,
) Platform {
return &WindowsPlatform{
fs: fs,
cmdRunner: cmdRunner,
collector: collector,
compressor: boshcmd.NewTarballCompressor(cmdRunner, fs),
copier: boshcmd.NewGenericCpCopier(fs, logger),
dirProvider: dirProvider,
netManager: netManager,
devicePathResolver: devicePathResolver,
vitalsService: boshvitals.NewService(collector, dirProvider),
certManager: certManager,
defaultNetworkResolver: defaultNetworkResolver,
auditLogger: auditLogger,
uuidGenerator: uuidGenerator,
}
}
func (p WindowsPlatform) AssociateDisk(name string, settings boshsettings.DiskSettings) error {
return errors.New("unimplemented")
}
func (p WindowsPlatform) GetFs() (fs boshsys.FileSystem) {
return p.fs
}
func (p WindowsPlatform) GetRunner() (runner boshsys.CmdRunner) {
return p.cmdRunner
}
func (p WindowsPlatform) GetCompressor() (compressor boshcmd.Compressor) {
return p.compressor
}
func (p WindowsPlatform) GetCopier() (copier boshcmd.Copier) {
return p.copier
}
func (p WindowsPlatform) GetDirProvider() (dirProvider boshdir.Provider) {
return p.dirProvider
}
func (p WindowsPlatform) GetVitalsService() (service boshvitals.Service) {
return p.vitalsService
}
func (p WindowsPlatform) GetDevicePathResolver() (devicePathResolver boshdpresolv.DevicePathResolver) {
return p.devicePathResolver
}
func (p WindowsPlatform) GetAuditLogger() AuditLogger {
return p.auditLogger
}
func (p WindowsPlatform) SetupRuntimeConfiguration() (err error) {
return
}
func (p WindowsPlatform) CreateUser(username, _ string) error {
if err := createUserProfile(username); err != nil {
return bosherr.WrapError(err, "CreateUser: creating user")
}
return nil
}
func (p WindowsPlatform) AddUserToGroups(username string, groups []string) (err error) {
return
}
func (p WindowsPlatform) findEphemeralUsersMatching(reg *regexp.Regexp) ([]string, error) {
users, err := localAccountNames()
if err != nil {
return nil, bosherr.WrapError(err, "Getting list of users")
}
var matchingUsers []string
for _, user := range users {
if !strings.HasPrefix(user, boshsettings.EphemeralUserPrefix) {
continue
}
if reg.MatchString(user) {
matchingUsers = append(matchingUsers, user)
}
}
return matchingUsers, nil
}
func (p WindowsPlatform) DeleteEphemeralUsersMatching(pattern string) error {
reg, err := regexp.Compile(pattern)
if err != nil {
return bosherr.WrapError(err, "Compiling regexp")
}
users, err := p.findEphemeralUsersMatching(reg)
if err != nil {
return bosherr.WrapError(err, "Finding ephemeral users")
}
for _, user := range users {
if err := deleteUserProfile(user); err != nil {
return err
}
}
return nil
}
func (p WindowsPlatform) SetupRootDisk(ephemeralDiskPath string) (err error) {
return
}
func (p WindowsPlatform) SetupSSH(publicKey []string, username string) error {
homedir, err := userHomeDirectory(username)
if err != nil {
return bosherr.WrapErrorf(err, "Finding 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.
//
// Do not use the WindowsPlatform.cmdRunner for this - it passes
// every command through PowerShell, which breaks this command.
//
cmd := exec.Command("icacls.exe", authkeysPath, "/grant", "NT SERVICE\\SSHD:(R)")
out, err := cmd.CombinedOutput()
if err != nil {
// Remove authorized_keys file - don't check the error
p.fs.RemoveAll(authkeysPath)
return bosherr.WrapErrorf(err, "Setting ACL on authorized_keys file (%s): %s",
authkeysPath, string(out))
}
return nil
}
func (p WindowsPlatform) SetUserPassword(user, encryptedPwd string) (err error) {
return
}
func (p WindowsPlatform) SaveDNSRecords(dnsRecords boshsettings.DNSRecords, hostname string) (err error) {
windir := os.Getenv("windir")
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")
}
uuid, err := p.uuidGenerator.Generate()
if err != nil {
return bosherr.WrapError(err, "SaveDNSRecords: generating UUID")
}
tmpfile := filepath.Join(etcdir, "hosts-"+uuid)
f, err := p.fs.OpenFile(tmpfile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return bosherr.WrapError(err, "SaveDNSRecords: opening hosts file")
}
var buf bytes.Buffer
for _, rec := range dnsRecords.Records {
fmt.Fprintf(&buf, "%s %s\n", rec[0], rec[1])
}
if _, err := buf.WriteTo(f); err != nil {
f.Close()
return bosherr.WrapErrorf(err, "SaveDNSRecords: writing DNS records to: %s", tmpfile)
}
f.Close() // Explicitly close before renaming - required to release handle
hostfile := filepath.Join(etcdir, "hosts")
if err := p.fs.Rename(tmpfile, hostfile); err != nil {
return bosherr.WrapErrorf(err, "SaveDNSRecords: renaming %s to %s", tmpfile, hostfile)
}
return
}
func (p WindowsPlatform) SetupIPv6(config boshsettings.IPv6) error {
return nil
}
func (p WindowsPlatform) SetupHostname(hostname string) (err error) {
return
}
func (p WindowsPlatform) SetupNetworking(networks boshsettings.Networks) (err error) {
return p.netManager.SetupNetworking(networks, nil)
}
func (p WindowsPlatform) GetConfiguredNetworkInterfaces() (interfaces []string, err error) {
return p.netManager.GetConfiguredNetworkInterfaces()
}
func (p WindowsPlatform) GetCertManager() (certManager boshcert.Manager) {
return p.certManager
}
func (p WindowsPlatform) SetupLogrotate(groupName, basePath, size string) (err error) {
return
}
func (p WindowsPlatform) SetTimeWithNtpServers(servers []string) (err error) {
if len(servers) == 0 {
return
}
var (
stderr string
)
ntpServers := strings.Join(servers, " ")
_, stderr, _, err = p.cmdRunner.RunCommand("powershell.exe",
"new-netfirewallrule",
"-displayname", "NTP",
"-direction", "outbound",
"-action", "allow",
"-protocol", "udp",
"-RemotePort", "123")
if err != nil {
err = bosherr.WrapErrorf(err, "SetTimeWithNtpServers %s", stderr)
return
}
_, _, _, _ = p.cmdRunner.RunCommand("net", "stop", "w32time")
manualPeerList := fmt.Sprintf("/manualpeerlist:\"%s\"", ntpServers)
_, stderr, _, err = p.cmdRunner.RunCommand("w32tm", "/config", "/syncfromflags:manual", manualPeerList)
if err != nil {
err = bosherr.WrapErrorf(err, "SetTimeWithNtpServers %s", stderr)
return
}
_, _, _, _ = p.cmdRunner.RunCommand("net", "start", "w32time")
_, stderr, _, err = p.cmdRunner.RunCommand("w32tm", "/config", "/update")
if err != nil {
err = bosherr.WrapErrorf(err, "SetTimeWithNtpServers %s", stderr)
return
}
_, stderr, _, err = p.cmdRunner.RunCommand("w32tm", "/resync", "/rediscover")
if err != nil {
err = bosherr.WrapErrorf(err, "SetTimeWithNtpServers %s", stderr)
return
}
return
}
func (p WindowsPlatform) SetupEphemeralDiskWithPath(devicePath string, desiredSwapSizeInBytes *uint64) (err error) {
return
}
func (p WindowsPlatform) SetupRawEphemeralDisks(devices []boshsettings.DiskSettings) (err error) {
return
}
func (p WindowsPlatform) SetupDataDir() error {
dataDir := p.dirProvider.DataDir()
sysDataDir := filepath.Join(dataDir, "sys")
logDir := filepath.Join(sysDataDir, "log")
if err := p.fs.MkdirAll(logDir, logDirPermissions); err != nil {
return bosherr.WrapErrorf(err, "Making %s dir", logDir)
}
sysDir := filepath.Join(p.dirProvider.BaseDir(), "sys")
if !p.fs.FileExists(sysDir) {
if err := p.fs.Symlink(sysDataDir, sysDir); err != nil {
return bosherr.WrapErrorf(err, "Symlinking '%s' to '%s'", sysDir, sysDataDir)
}
}
return nil
}
func (p WindowsPlatform) SetupHomeDir() error {
return nil
}
func (p WindowsPlatform) SetupTmpDir() error {
boshTmpDir := p.dirProvider.TmpDir()
err := p.fs.MkdirAll(boshTmpDir, tmpDirPermissions)
if err != nil {
return bosherr.WrapError(err, "Creating temp dir")
}
err = os.Setenv("TMP", boshTmpDir)
if err != nil {
return bosherr.WrapError(err, "Setting TMP")
}
err = os.Setenv("TEMP", boshTmpDir)
if err != nil {
return bosherr.WrapError(err, "Setting TEMP")
}
return nil
}
func (p WindowsPlatform) SetupLogDir() error {
return nil
}
func (p WindowsPlatform) SetupBlobsDir() error {
blobsDirPath := p.dirProvider.BlobsDir()
err := p.fs.MkdirAll(blobsDirPath, blobsDirPermissions)
if err != nil {
return bosherr.WrapError(err, "Creating blobs dir")
}
return nil
}
func (p WindowsPlatform) SetupLoggingAndAuditing() error {
return nil
}
func (p WindowsPlatform) MountPersistentDisk(diskSettings boshsettings.DiskSettings, mountPoint string) (err error) {
return
}
func (p WindowsPlatform) UnmountPersistentDisk(diskSettings boshsettings.DiskSettings) (didUnmount bool, err error) {
return
}
func (p WindowsPlatform) GetEphemeralDiskPath(diskSettings boshsettings.DiskSettings) string {
return ""
}
func (p WindowsPlatform) GetFileContentsFromCDROM(filePath string) (contents []byte, err error) {
return p.fs.ReadFile("D:/" + filePath)
}
func (p WindowsPlatform) GetFilesContentsFromDisk(diskPath string, fileNames []string) (contents [][]byte, err error) {
return
}
func (p WindowsPlatform) MigratePersistentDisk(fromMountPoint, toMountPoint string) (err error) {
return
}
func (p WindowsPlatform) IsMountPoint(path string) (string, bool, error) {
return "", true, nil
}
func (p WindowsPlatform) IsPersistentDiskMounted(diskSettings boshsettings.DiskSettings) (bool, error) {
return true, nil
}
func (p WindowsPlatform) IsPersistentDiskMountable(diskSettings boshsettings.DiskSettings) (bool, error) {
return true, nil
}
func (p WindowsPlatform) StartMonit() (err error) {
return
}
func (p WindowsPlatform) SetupMonitUser() (err error) {
return
}
func (p WindowsPlatform) GetMonitCredentials() (username, password string, err error) {
return
}
func (p WindowsPlatform) PrepareForNetworkingChange() error {
return nil
}
func (p WindowsPlatform) CleanIPMacAddressCache(ip string) error {
return nil
}
func (p WindowsPlatform) RemoveDevTools(packageFileListPath string) error {
return nil
}
func (p WindowsPlatform) RemoveStaticLibraries(packageFileListPath string) error {
return nil
}
func (p WindowsPlatform) GetDefaultNetwork() (boshsettings.Network, error) {
return p.defaultNetworkResolver.GetDefaultNetwork()
}
func (p WindowsPlatform) GetHostPublicKey() (string, error) {
if err := sshEnabled(); err != nil {
return "", bosherr.WrapError(err, "OpenSSH is not running")
}
drive := os.Getenv("SYSTEMDRIVE")
if drive == "" {
drive = "C:"
}
drive += "\\"
sshdir := filepath.Join(drive, "Program Files", "OpenSSH")
keypath := filepath.Join(sshdir, "ssh_host_rsa_key.pub")
key, err := p.fs.ReadFileString(keypath)
if err != nil {
// Provide a useful error message.
//
// Do this here otherwise the FakeFileSystem we use for tests
// incorrectly complains that the directories we created don't
// exist.
//
if _, err := p.fs.Stat(sshdir); os.IsNotExist(err) {
return "", bosherr.WrapErrorf(err, "Reading host public key: "+
"expected OpenSSH to be installed at: %s", sshdir)
}
return "", bosherr.WrapErrorf(err, "Missing host public RSA key: %s", keypath)
}
return key, nil
}
func (p WindowsPlatform) DeleteARPEntryWithIP(ip string) error {
_, _, _, err := p.cmdRunner.RunCommand("arp", "-d", ip)
if err != nil {
return bosherr.WrapError(err, "Deleting arp entry")
}
return nil
}
func (p WindowsPlatform) SetupRecordsJSONPermission(path string) error {
return nil
}