-
Notifications
You must be signed in to change notification settings - Fork 45
/
accountkey.go
189 lines (174 loc) · 7.22 KB
/
accountkey.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
/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package openstack
import (
"fmt"
"net/url"
"regexp"
)
// AccountKey is a OpenStack account key.
type AccountKey struct {
AuthURL string
Username string
Password string
ProjectID string
ProjectName string
UserDomainName string
ProjectDomainName string
RegionName string
}
// AccountKeyFromURI parses ServiceAccountKey from URI.
func AccountKeyFromURI(serviceAccountURI string) (AccountKey, error) {
uri, err := url.Parse(serviceAccountURI)
if err != nil {
return AccountKey{}, err
}
if uri.Scheme != "serviceaccount" {
return AccountKey{}, fmt.Errorf("invalid service account URI: invalid scheme: %s", uri.Scheme)
}
if uri.Host != "openstack" {
return AccountKey{}, fmt.Errorf("invalid service account URI: invalid host: %s", uri.Host)
}
query := uri.Query()
if query.Get("auth_url") == "" {
return AccountKey{}, fmt.Errorf("invalid service account URI: missing parameter \"auth_url\": %s", uri)
}
if query.Get("username") == "" {
return AccountKey{}, fmt.Errorf("invalid service account URI: missing parameter \"username\": %s", uri)
}
if query.Get("password") == "" {
return AccountKey{}, fmt.Errorf("invalid service account URI: missing parameter \"password\": %s", uri)
}
if query.Get("project_id") == "" {
return AccountKey{}, fmt.Errorf("invalid service account URI: missing parameter \"project_id\": %s", uri)
}
if query.Get("project_name") == "" {
return AccountKey{}, fmt.Errorf("invalid service account URI: missing parameter \"project_name\": %s", uri)
}
if query.Get("user_domain_name") == "" {
return AccountKey{}, fmt.Errorf("invalid service account URI: missing parameter \"user_domain_name\": %s", uri)
}
if query.Get("project_domain_name") == "" {
return AccountKey{}, fmt.Errorf("invalid service account URI: missing parameter \"project_domain_name\": %s", uri)
}
if query.Get("region_name") == "" {
return AccountKey{}, fmt.Errorf("invalid service account URI: missing parameter \"region_name\": %s", uri)
}
return AccountKey{
AuthURL: query.Get("auth_url"),
Username: query.Get("username"),
Password: query.Get("password"),
ProjectID: query.Get("project_id"),
ProjectName: query.Get("project_name"),
UserDomainName: query.Get("user_domain_name"),
ProjectDomainName: query.Get("project_domain_name"),
RegionName: query.Get("region_name"),
}, nil
}
// ToCloudServiceAccountURI converts the AccountKey into a cloud service account URI.
func (k AccountKey) ToCloudServiceAccountURI() string {
query := url.Values{}
query.Add("auth_url", k.AuthURL)
query.Add("username", k.Username)
query.Add("password", k.Password)
query.Add("project_id", k.ProjectID)
query.Add("project_name", k.ProjectName)
query.Add("user_domain_name", k.UserDomainName)
query.Add("project_domain_name", k.ProjectDomainName)
query.Add("region_name", k.RegionName)
uri := url.URL{
Scheme: "serviceaccount",
Host: "openstack",
RawQuery: query.Encode(),
}
return uri.String()
}
// CloudINI converts the AccountKey into a CloudINI.
func (k AccountKey) CloudINI() CloudINI {
return CloudINI{
AuthURL: k.AuthURL,
Username: k.Username,
Password: k.Password,
ProjectID: k.ProjectID,
TenantName: k.ProjectName,
UserDomainName: k.UserDomainName,
TenantDomainName: k.ProjectDomainName,
Region: k.RegionName,
}
}
// CloudINI is a struct that represents the cloud.ini file used by OpenStack k8s deployments.
type CloudINI struct {
AuthURL string `gcfg:"auth-url" mapstructure:"auth-url" name:"os-authURL" dependsOn:"os-password|os-trustID|os-applicationCredentialSecret|os-clientCertPath"`
Username string `name:"os-userName" value:"optional" dependsOn:"os-password"`
Password string `name:"os-password" value:"optional" dependsOn:"os-domainID|os-domainName,os-projectID|os-projectName,os-userID|os-userName"`
ProjectID string `gcfg:"project-id" mapstructure:"project-id" name:"os-projectID" value:"optional" dependsOn:"os-password|os-clientCertPath"`
TenantName string `gcfg:"tenant-name" mapstructure:"project-name" name:"os-projectName" value:"optional" dependsOn:"os-password|os-clientCertPath"`
UserDomainName string `gcfg:"user-domain-name" mapstructure:"user-domain-name" name:"os-userDomainName" value:"optional"`
TenantDomainName string `gcfg:"tenant-domain-name" mapstructure:"project-domain-name" name:"os-projectDomainName" value:"optional"`
Region string `name:"os-region"`
}
// FullConfiguration returns the string representation of the full CloudINI.
func (i CloudINI) FullConfiguration() string {
// sanitize parameters to not include newlines
authURL := newlineRegexp.ReplaceAllString(i.AuthURL, "")
username := newlineRegexp.ReplaceAllString(i.Username, "")
password := newlineRegexp.ReplaceAllString(i.Password, "")
tenantID := newlineRegexp.ReplaceAllString(i.ProjectID, "")
tenantName := newlineRegexp.ReplaceAllString(i.TenantName, "")
userDomainName := newlineRegexp.ReplaceAllString(i.UserDomainName, "")
tenantDomainName := newlineRegexp.ReplaceAllString(i.TenantDomainName, "")
region := newlineRegexp.ReplaceAllString(i.Region, "")
return fmt.Sprintf(`[Global]
auth-url = %s
username = %s
password = %s
tenant-id = %s
tenant-name = %s
user-domain-name = %s
tenant-domain-name = %s
region = %s
`, authURL, username, password, tenantID, tenantName, userDomainName, tenantDomainName, region)
}
// YawolConfiguration returns the string representation of the CloudINI subset yawol expects.
func (i CloudINI) YawolConfiguration() string {
// sanitize parameters to not include newlines
authURL := newlineRegexp.ReplaceAllString(i.AuthURL, "")
username := newlineRegexp.ReplaceAllString(i.Username, "")
password := newlineRegexp.ReplaceAllString(i.Password, "")
projectID := newlineRegexp.ReplaceAllString(i.ProjectID, "")
userDomainName := newlineRegexp.ReplaceAllString(i.UserDomainName, "")
region := newlineRegexp.ReplaceAllString(i.Region, "")
return fmt.Sprintf(`[Global]
auth-url = %s
username = %s
password = %s
project-id = %s
domain-name = %s
region = %s
`, authURL, username, password, projectID, userDomainName, region)
}
// CinderCSIConfiguration returns the string representation of the CloudINI subset cinder expects.
func (i CloudINI) CinderCSIConfiguration() string {
// sanitize parameters to not include newlines
authURL := newlineRegexp.ReplaceAllString(i.AuthURL, "")
username := newlineRegexp.ReplaceAllString(i.Username, "")
password := newlineRegexp.ReplaceAllString(i.Password, "")
projectID := newlineRegexp.ReplaceAllString(i.ProjectID, "")
projectName := newlineRegexp.ReplaceAllString(i.TenantName, "")
userDomainName := newlineRegexp.ReplaceAllString(i.UserDomainName, "")
tenantDomainName := newlineRegexp.ReplaceAllString(i.TenantDomainName, "")
region := newlineRegexp.ReplaceAllString(i.Region, "")
return fmt.Sprintf(`[Global]
auth-url = %s
username = %s
password = %s
project-id = %s
project-name = %s
user-domain-name = %s
project-domain-name = %s
region = %s
`, authURL, username, password, projectID, projectName, userDomainName, tenantDomainName, region)
}
var newlineRegexp = regexp.MustCompile(`[\r\n]+`)