forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_drive_settings_source.go
101 lines (80 loc) · 2.63 KB
/
config_drive_settings_source.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
package infrastructure
import (
"encoding/json"
boshplatform "github.com/cloudfoundry/bosh-agent/platform"
boshsettings "github.com/cloudfoundry/bosh-agent/settings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
type MetadataContentsType struct {
PublicKeys map[string]PublicKeyType `json:"public-keys"`
InstanceID string `json:"instance-id"` // todo remove
}
type PublicKeyType map[string]string
type ConfigDriveSettingsSource struct {
diskPaths []string
metadataPath string
settingsPath string
platform boshplatform.Platform
logTag string
logger boshlog.Logger
}
func NewConfigDriveSettingsSource(
diskPaths []string,
metadataPath string,
settingsPath string,
platform boshplatform.Platform,
logger boshlog.Logger,
) *ConfigDriveSettingsSource {
return &ConfigDriveSettingsSource{
diskPaths: diskPaths,
metadataPath: metadataPath,
settingsPath: settingsPath,
platform: platform,
logTag: "ConfigDriveSettingsSource",
logger: logger,
}
}
func (s *ConfigDriveSettingsSource) PublicSSHKeyForUsername(string) (string, error) {
metadataContent, err := s.loadFileFromConfigDrive(s.metadataPath)
if err != nil {
return "", err
}
var metadata MetadataContentsType
err = json.Unmarshal(metadataContent, &metadata)
if err != nil {
return "", bosherr.WrapErrorf(err, "Parsing config drive metadata from '%s'", s.metadataPath)
}
if firstPublicKey, ok := metadata.PublicKeys["0"]; ok {
if openSSHKey, ok := firstPublicKey["openssh-key"]; ok {
return openSSHKey, nil
}
}
return "", nil
}
func (s *ConfigDriveSettingsSource) Settings() (boshsettings.Settings, error) {
settingsContent, err := s.loadFileFromConfigDrive(s.settingsPath)
if err != nil {
return boshsettings.Settings{}, err
}
var settings boshsettings.Settings
err = json.Unmarshal(settingsContent, &settings)
if err != nil {
return boshsettings.Settings{}, bosherr.WrapErrorf(
err, "Parsing config drive settings from '%s'", s.settingsPath)
}
return settings, err
}
func (s *ConfigDriveSettingsSource) loadFileFromConfigDrive(contentPath string) ([]byte, error) {
var err error
var contents [][]byte
for _, diskPath := range s.diskPaths {
contents, err = s.platform.GetFilesContentsFromDisk(diskPath, []string{contentPath})
if err == nil {
s.logger.Debug(s.logTag, "Successfully loaded file '%s' from config drive: '%s'", contentPath, diskPath)
return contents[0], nil
}
s.logger.Warn(s.logTag, "Failed to load config from %s - %s", diskPath, err.Error())
}
return []byte{}, bosherr.WrapErrorf(err, "Loading file '%s' from config drive", contentPath)
}