forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
credentials.go
215 lines (201 loc) · 6.25 KB
/
credentials.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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package openstack
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strconv"
"github.com/juju/errors"
"github.com/juju/utils"
"gopkg.in/goose.v2/identity"
"gopkg.in/ini.v1"
"github.com/juju/juju/cloud"
"github.com/juju/juju/environs"
)
const (
CredAttrTenantName = "tenant-name"
CredAttrTenantID = "tenant-id"
CredAttrUserName = "username"
CredAttrPassword = "password"
CredAttrDomainName = "domain-name"
CredAttrProjectDomainName = "project-domain-name"
CredAttrUserDomainName = "user-domain-name"
CredAttrAccessKey = "access-key"
CredAttrSecretKey = "secret-key"
CredAttrVersion = "version"
)
type OpenstackCredentials struct{}
// CredentialSchemas is part of the environs.ProviderCredentials interface.
func (OpenstackCredentials) CredentialSchemas() map[cloud.AuthType]cloud.CredentialSchema {
return map[cloud.AuthType]cloud.CredentialSchema{
cloud.UserPassAuthType: {
{
CredAttrUserName, cloud.CredentialAttr{Description: "The username to authenticate with."},
}, {
CredAttrPassword, cloud.CredentialAttr{
Description: "The password for the specified username.",
Hidden: true,
},
}, {
CredAttrTenantName, cloud.CredentialAttr{
Description: "The OpenStack tenant name.",
Optional: true,
},
}, {
CredAttrTenantID, cloud.CredentialAttr{
Description: "The Openstack tenant ID",
Optional: true,
},
}, {
CredAttrVersion, cloud.CredentialAttr{
Description: "The Openstack identity version",
Optional: true,
},
}, {
CredAttrDomainName, cloud.CredentialAttr{
Description: "The OpenStack domain name.",
Optional: true,
},
}, {
CredAttrProjectDomainName, cloud.CredentialAttr{
Description: "The OpenStack project domain name.",
Optional: true,
},
}, {
CredAttrUserDomainName, cloud.CredentialAttr{
Description: "The OpenStack user domain name.",
Optional: true,
},
},
},
cloud.AccessKeyAuthType: {
{
CredAttrAccessKey, cloud.CredentialAttr{Description: "The access key to authenticate with."},
}, {
CredAttrSecretKey, cloud.CredentialAttr{
Description: "The secret key to authenticate with.",
Hidden: true,
},
}, {
CredAttrTenantName, cloud.CredentialAttr{
Description: "The OpenStack tenant name.",
Optional: true,
},
}, {
CredAttrTenantID, cloud.CredentialAttr{
Description: "The Openstack tenant ID",
Optional: true,
},
}, {
CredAttrVersion, cloud.CredentialAttr{
Description: "The Openstack identity version",
Optional: true,
},
},
},
}
}
// DetectCredentials is part of the environs.ProviderCredentials interface.
func (c OpenstackCredentials) DetectCredentials() (*cloud.CloudCredential, error) {
result := cloud.CloudCredential{
AuthCredentials: make(map[string]cloud.Credential),
}
// Try just using environment variables
creds, user, region, err := c.detectCredential()
if err == nil {
result.DefaultRegion = region
result.AuthCredentials[user] = *creds
}
// Now look for .novarc file in home dir.
novarc := filepath.Join(utils.Home(), ".novarc")
novaInfo, err := ini.LooseLoad(novarc)
if err != nil {
return nil, errors.Annotate(err, "loading novarc file")
}
stripExport := regexp.MustCompile(`(?i)^\s*export\s*`)
keyValues := novaInfo.Section(ini.DEFAULT_SECTION).KeysHash()
if len(keyValues) > 0 {
for k, v := range keyValues {
k = stripExport.ReplaceAllString(k, "")
os.Setenv(k, v)
}
creds, user, region, err := c.detectCredential()
if err == nil {
result.DefaultRegion = region
result.AuthCredentials[user] = *creds
}
}
if len(result.AuthCredentials) == 0 {
return nil, errors.NotFoundf("openstack credentials")
}
return &result, nil
}
func (c OpenstackCredentials) detectCredential() (*cloud.Credential, string, string, error) {
creds, err := identity.CredentialsFromEnv()
if err != nil {
return nil, "", "", errors.Errorf("failed to retrive cred from env : %v", err)
}
if creds.TenantName == "" {
logger.Debugf("neither OS_TENANT_NAME nor OS_PROJECT_NAME environment variable not set")
}
if creds.TenantID == "" {
logger.Debugf("neither OS_TENANT_ID nor OS_PROJECT_ID environment variable not set")
}
if creds.User == "" {
return nil, "", "", errors.NewNotFound(nil, "neither OS_USERNAME nor OS_ACCESS_KEY environment variable not set")
}
if creds.Secrets == "" {
return nil, "", "", errors.NewNotFound(nil, "neither OS_PASSWORD nor OS_SECRET_KEY environment variable not set")
}
user, err := utils.LocalUsername()
if err != nil {
return nil, "", "", errors.Trace(err)
}
var version string
if creds.Version != 0 {
version = strconv.Itoa(creds.Version)
} else {
version = ""
}
// If OS_USERNAME or NOVA_USERNAME is set, assume userpass.
var credential cloud.Credential
if os.Getenv("OS_USERNAME") != "" || os.Getenv("NOVA_USERNAME") != "" {
user = creds.User
credential = cloud.NewCredential(
cloud.UserPassAuthType,
map[string]string{
CredAttrUserName: creds.User,
CredAttrPassword: creds.Secrets,
CredAttrTenantName: creds.TenantName,
CredAttrTenantID: creds.TenantID,
CredAttrUserDomainName: creds.UserDomain,
CredAttrProjectDomainName: creds.ProjectDomain,
CredAttrDomainName: creds.Domain,
CredAttrVersion: version,
},
)
} else {
credential = cloud.NewCredential(
cloud.AccessKeyAuthType,
map[string]string{
CredAttrAccessKey: creds.User,
CredAttrSecretKey: creds.Secrets,
CredAttrTenantName: creds.TenantName,
CredAttrTenantID: creds.TenantID,
CredAttrVersion: version,
},
)
}
region := creds.Region
if region == "" {
region = "<unspecified>"
}
credential.Label = fmt.Sprintf("openstack region %q project %q user %q", region, creds.TenantName, user)
return &credential, user, creds.Region, nil
}
// FinalizeCredential is part of the environs.ProviderCredentials interface.
func (OpenstackCredentials) FinalizeCredential(_ environs.FinalizeCredentialContext, args environs.FinalizeCredentialParams) (*cloud.Credential, error) {
return &args.Credential, nil
}