forked from juju/juju
/
config.go
116 lines (99 loc) · 3.09 KB
/
config.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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package localstorage
import (
"launchpad.net/goyaml"
"github.com/juju/juju/agent"
)
const (
// TODO(axw) 2013-09-25 bug #1230131
// Move these variables out of agent when we can do upgrades in
// the right place. In this case, the local provider should do
// the envvar-to-agent.conf migration.
StorageDir = agent.StorageDir
StorageAddr = agent.StorageAddr
StorageCACert = "StorageCACert"
StorageCAKey = "StorageCAKey"
StorageHostnames = "StorageHostnames"
StorageAuthKey = "StorageAuthKey"
)
// LocalStorageConfig is an interface that, if implemented, may be used
// to configure a machine agent for use with the localstorage worker in
// this package.
type LocalStorageConfig interface {
StorageDir() string
StorageAddr() string
}
// LocalTLSStorageConfig is an interface that extends LocalStorageConfig
// to support serving storage over TLS.
type LocalTLSStorageConfig interface {
LocalStorageConfig
// StorageCACert is the CA certificate in PEM format.
StorageCACert() string
// StorageCAKey is the CA private key in PEM format.
StorageCAKey() string
// StorageHostnames is the set of hostnames that will
// be assigned to the storage server's certificate.
StorageHostnames() []string
// StorageAuthKey is the key that clients must present
// to perform modifying operations.
StorageAuthKey() string
}
type config struct {
storageDir string
storageAddr string
caCertPEM string
caKeyPEM string
hostnames []string
authkey string
}
// StoreConfig takes a LocalStorageConfig (or derivative interface),
// and stores it in a map[string]string suitable for updating an
// agent.Config's key/value map.
func StoreConfig(storageConfig LocalStorageConfig) (map[string]string, error) {
kv := make(map[string]string)
kv[StorageDir] = storageConfig.StorageDir()
kv[StorageAddr] = storageConfig.StorageAddr()
if tlsConfig, ok := storageConfig.(LocalTLSStorageConfig); ok {
if authkey := tlsConfig.StorageAuthKey(); authkey != "" {
kv[StorageAuthKey] = authkey
}
if cert := tlsConfig.StorageCACert(); cert != "" {
kv[StorageCACert] = cert
}
if key := tlsConfig.StorageCAKey(); key != "" {
kv[StorageCAKey] = key
}
if hostnames := tlsConfig.StorageHostnames(); len(hostnames) > 0 {
data, err := goyaml.Marshal(hostnames)
if err != nil {
return nil, err
}
kv[StorageHostnames] = string(data)
}
}
return kv, nil
}
func loadConfig(agentConfig agent.Config) (*config, error) {
config := &config{
storageDir: agentConfig.Value(StorageDir),
storageAddr: agentConfig.Value(StorageAddr),
authkey: agentConfig.Value(StorageAuthKey),
}
caCertPEM := agentConfig.Value(StorageCACert)
if len(caCertPEM) > 0 {
config.caCertPEM = caCertPEM
}
caKeyPEM := agentConfig.Value(StorageCAKey)
if len(caKeyPEM) > 0 {
config.caKeyPEM = caKeyPEM
}
hostnames := agentConfig.Value(StorageHostnames)
if len(hostnames) > 0 {
err := goyaml.Unmarshal([]byte(hostnames), &config.hostnames)
if err != nil {
return nil, err
}
}
return config, nil
}