github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/worker/localstorage/config.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package localstorage 5 6 import ( 7 goyaml "gopkg.in/yaml.v1" 8 9 "github.com/juju/juju/agent" 10 ) 11 12 const ( 13 // TODO(axw) 2013-09-25 bug #1230131 14 // Move these variables out of agent when we can do upgrades in 15 // the right place. In this case, the local provider should do 16 // the envvar-to-agent.conf migration. 17 StorageDir = agent.StorageDir 18 StorageAddr = agent.StorageAddr 19 StorageCACert = "StorageCACert" 20 StorageCAKey = "StorageCAKey" 21 StorageHostnames = "StorageHostnames" 22 StorageAuthKey = "StorageAuthKey" 23 ) 24 25 // LocalStorageConfig is an interface that, if implemented, may be used 26 // to configure a machine agent for use with the localstorage worker in 27 // this package. 28 type LocalStorageConfig interface { 29 StorageDir() string 30 StorageAddr() string 31 } 32 33 // LocalTLSStorageConfig is an interface that extends LocalStorageConfig 34 // to support serving storage over TLS. 35 type LocalTLSStorageConfig interface { 36 LocalStorageConfig 37 38 // StorageCACert is the CA certificate in PEM format. 39 StorageCACert() string 40 41 // StorageCAKey is the CA private key in PEM format. 42 StorageCAKey() string 43 44 // StorageHostnames is the set of hostnames that will 45 // be assigned to the storage server's certificate. 46 StorageHostnames() []string 47 48 // StorageAuthKey is the key that clients must present 49 // to perform modifying operations. 50 StorageAuthKey() string 51 } 52 53 type config struct { 54 storageDir string 55 storageAddr string 56 caCertPEM string 57 caKeyPEM string 58 hostnames []string 59 authkey string 60 } 61 62 // StoreConfig takes a LocalStorageConfig (or derivative interface), 63 // and stores it in a map[string]string suitable for updating an 64 // agent.Config's key/value map. 65 func StoreConfig(storageConfig LocalStorageConfig) (map[string]string, error) { 66 kv := make(map[string]string) 67 kv[StorageDir] = storageConfig.StorageDir() 68 kv[StorageAddr] = storageConfig.StorageAddr() 69 if tlsConfig, ok := storageConfig.(LocalTLSStorageConfig); ok { 70 if authkey := tlsConfig.StorageAuthKey(); authkey != "" { 71 kv[StorageAuthKey] = authkey 72 } 73 if cert := tlsConfig.StorageCACert(); cert != "" { 74 kv[StorageCACert] = cert 75 } 76 if key := tlsConfig.StorageCAKey(); key != "" { 77 kv[StorageCAKey] = key 78 } 79 if hostnames := tlsConfig.StorageHostnames(); len(hostnames) > 0 { 80 data, err := goyaml.Marshal(hostnames) 81 if err != nil { 82 return nil, err 83 } 84 kv[StorageHostnames] = string(data) 85 } 86 } 87 return kv, nil 88 } 89 90 func loadConfig(agentConfig agent.Config) (*config, error) { 91 config := &config{ 92 storageDir: agentConfig.Value(StorageDir), 93 storageAddr: agentConfig.Value(StorageAddr), 94 authkey: agentConfig.Value(StorageAuthKey), 95 } 96 97 caCertPEM := agentConfig.Value(StorageCACert) 98 if len(caCertPEM) > 0 { 99 config.caCertPEM = caCertPEM 100 } 101 102 caKeyPEM := agentConfig.Value(StorageCAKey) 103 if len(caKeyPEM) > 0 { 104 config.caKeyPEM = caKeyPEM 105 } 106 107 hostnames := agentConfig.Value(StorageHostnames) 108 if len(hostnames) > 0 { 109 err := goyaml.Unmarshal([]byte(hostnames), &config.hostnames) 110 if err != nil { 111 return nil, err 112 } 113 } 114 115 return config, nil 116 }