/
provider.go
179 lines (155 loc) · 5.26 KB
/
provider.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
// Copyright 2013 Joyent Inc.
// Licensed under the AGPLv3, see LICENCE file for details.
package joyent
import (
"fmt"
"github.com/joyent/gocommon/client"
joyenterrors "github.com/joyent/gocommon/errors"
"github.com/joyent/gosdc/cloudapi"
"github.com/joyent/gosign/auth"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/utils"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/environs/simplestreams"
)
var logger = loggo.GetLogger("juju.provider.joyent")
type joyentProvider struct{}
var providerInstance = joyentProvider{}
var _ environs.EnvironProvider = providerInstance
var _ simplestreams.HasRegion = (*joyentEnviron)(nil)
var errNotImplemented = errors.New("not implemented in Joyent provider")
// RestrictedConfigAttributes is specified in the EnvironProvider interface.
func (joyentProvider) RestrictedConfigAttributes() []string {
return nil
}
// PrepareForCreateEnvironment is specified in the EnvironProvider interface.
func (joyentProvider) PrepareForCreateEnvironment(cfg *config.Config) (*config.Config, error) {
// Turn an incomplete config into a valid one, if possible.
attrs := cfg.UnknownAttrs()
if _, ok := attrs["control-dir"]; !ok {
uuid, err := utils.NewUUID()
if err != nil {
return nil, errors.Trace(err)
}
attrs["control-dir"] = fmt.Sprintf("%x", uuid.Raw())
}
return cfg.Apply(attrs)
}
func (p joyentProvider) PrepareForBootstrap(ctx environs.BootstrapContext, cfg *config.Config) (environs.Environ, error) {
cfg, err := p.PrepareForCreateEnvironment(cfg)
if err != nil {
return nil, errors.Trace(err)
}
e, err := p.Open(cfg)
if err != nil {
return nil, errors.Trace(err)
}
if ctx.ShouldVerifyCredentials() {
if err := verifyCredentials(e.(*joyentEnviron)); err != nil {
return nil, errors.Trace(err)
}
}
return e, nil
}
const unauthorisedMessage = `
Please ensure the Manta username and SSH access key you have
specified are correct. You can create or import an SSH key via
the "Account Summary" page in the Joyent console.`
// verifyCredentials issues a cheap, non-modifying request to Joyent to
// verify the configured credentials. If verification fails, a user-friendly
// error will be returned, and the original error will be logged at debug
// level.
var verifyCredentials = func(e *joyentEnviron) error {
creds, err := credentials(e.Ecfg())
if err != nil {
return err
}
httpClient := client.NewClient(e.Ecfg().sdcUrl(), cloudapi.DefaultAPIVersion, creds, nil)
apiClient := cloudapi.New(httpClient)
_, err = apiClient.CountMachines()
if err != nil {
logger.Debugf("joyent request failed: %v", err)
if joyenterrors.IsInvalidCredentials(err) || joyenterrors.IsNotAuthorized(err) {
return errors.New("authentication failed.\n" + unauthorisedMessage)
}
return err
}
return nil
}
func credentials(cfg *environConfig) (*auth.Credentials, error) {
authentication, err := auth.NewAuth(cfg.mantaUser(), cfg.privateKey(), cfg.algorithm())
if err != nil {
return nil, errors.Errorf("cannot create credentials: %v", err)
}
return &auth.Credentials{
UserAuthentication: authentication,
MantaKeyId: cfg.mantaKeyId(),
MantaEndpoint: auth.Endpoint{URL: cfg.mantaUrl()},
SdcKeyId: cfg.sdcKeyId(),
SdcEndpoint: auth.Endpoint{URL: cfg.sdcUrl()},
}, nil
}
func (joyentProvider) Open(cfg *config.Config) (environs.Environ, error) {
env, err := newEnviron(cfg)
if err != nil {
return nil, err
}
return env, nil
}
func (joyentProvider) Validate(cfg, old *config.Config) (valid *config.Config, err error) {
newEcfg, err := validateConfig(cfg, old)
if err != nil {
return nil, errors.Errorf("invalid Joyent provider config: %v", err)
}
return cfg.Apply(newEcfg.attrs)
}
func (joyentProvider) SecretAttrs(cfg *config.Config) (map[string]string, error) {
// If you keep configSecretFields up to date, this method should Just Work.
ecfg, err := validateConfig(cfg, nil)
if err != nil {
return nil, err
}
secretAttrs := map[string]string{}
for _, field := range configSecretFields {
if value, ok := ecfg.attrs[field]; ok {
if stringValue, ok := value.(string); ok {
secretAttrs[field] = stringValue
} else {
// All your secret attributes must be strings at the moment. Sorry.
// It's an expedient and hopefully temporary measure that helps us
// plug a security hole in the API.
return nil, errors.Errorf(
"secret %q field must have a string value; got %v",
field, value,
)
}
}
}
return secretAttrs, nil
}
func (joyentProvider) BoilerplateConfig() string {
return boilerplateConfig
}
func GetProviderInstance() environs.EnvironProvider {
return providerInstance
}
// MetadataLookupParams returns parameters which are used to query image metadata to
// find matching image information.
func (p joyentProvider) MetadataLookupParams(region string) (*simplestreams.MetadataLookupParams, error) {
if region == "" {
return nil, errors.Errorf("region must be specified")
}
return &simplestreams.MetadataLookupParams{
Region: region,
Architectures: []string{"amd64", "armhf"},
}, nil
}
func (p joyentProvider) newConfig(cfg *config.Config) (*environConfig, error) {
valid, err := p.Validate(cfg, nil)
if err != nil {
return nil, err
}
return &environConfig{valid, valid.UnknownAttrs()}, nil
}