/
credentials.go
194 lines (165 loc) · 6.36 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
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package credentials
import (
"fmt"
"html/template"
"log"
"os"
"sigs.k8s.io/azuredisk-csi-driver/test/utils/testutil"
"github.com/pelletier/go-toml"
"k8s.io/apimachinery/pkg/util/uuid"
)
const (
AzurePublicCloud = "AzurePublicCloud"
ResourceGroupPrefix = "azuredisk-csi-driver-test-"
TempAzureCredentialFilePath = "/tmp/azure.json"
azureCredentialFileTemplate = `{
"cloud": "{{.Cloud}}",
"tenantId": "{{.TenantID}}",
"subscriptionId": "{{.SubscriptionID}}",
"aadClientId": "{{.AADClientID}}",
"aadClientSecret": "{{.AADClientSecret}}",
"resourceGroup": "{{.ResourceGroup}}",
"location": "{{.Location}}",
"vmType": "{{.VMType}}"
}`
defaultAzurePublicCloudLocation = "eastus2"
defaultAzurePublicCloudVMType = "vmss"
// Env vars
cloudNameEnvVar = "AZURE_CLOUD_NAME"
tenantIDEnvVar = "AZURE_TENANT_ID"
subscriptionIDEnvVar = "AZURE_SUBSCRIPTION_ID"
aadClientIDEnvVar = "AZURE_CLIENT_ID"
aadClientSecretEnvVar = "AZURE_CLIENT_SECRET"
resourceGroupEnvVar = "AZURE_RESOURCE_GROUP"
locationEnvVar = "AZURE_LOCATION"
vmTypeEnvVar = "AZURE_VM_TYPE"
)
// Config is used in Prow to store Azure credentials
// https://github.com/kubernetes/test-infra/blob/master/kubetest/azure.go#L116-L118
type Config struct {
Creds FromProw
}
// FromProw is used in Prow to store Azure credentials
// https://github.com/kubernetes/test-infra/blob/master/kubetest/azure.go#L107-L114
type FromProw struct {
ClientID string
ClientSecret string
TenantID string
SubscriptionID string
StorageAccountName string
StorageAccountKey string
}
// Credentials is used in Azure Disk CSI driver to store Azure credentials
type Credentials struct {
Cloud string
TenantID string
SubscriptionID string
AADClientID string
AADClientSecret string
ResourceGroup string
Location string
VMType string
}
// CreateAzureCredentialFile creates a temporary Azure credential file for
// Azure Disk CSI driver tests and returns the credentials
func CreateAzureCredentialFile() (*Credentials, error) {
// Search credentials through env vars first
var cloud, tenantID, subscriptionID, aadClientID, aadClientSecret, resourceGroup, location, vmType string
cloud = os.Getenv(cloudNameEnvVar)
if cloud == "" {
cloud = AzurePublicCloud
}
tenantID = os.Getenv(tenantIDEnvVar)
subscriptionID = os.Getenv(subscriptionIDEnvVar)
aadClientID = os.Getenv(aadClientIDEnvVar)
aadClientSecret = os.Getenv(aadClientSecretEnvVar)
resourceGroup = os.Getenv(resourceGroupEnvVar)
location = os.Getenv(locationEnvVar)
vmType = os.Getenv(vmTypeEnvVar)
if resourceGroup == "" {
resourceGroup = ResourceGroupPrefix + string(uuid.NewUUID())
}
if location == "" {
location = defaultAzurePublicCloudLocation
}
if vmType == "" {
vmType = defaultAzurePublicCloudVMType
}
// Running test locally
if tenantID != "" && subscriptionID != "" && aadClientID != "" && aadClientSecret != "" {
return parseAndExecuteTemplate(cloud, tenantID, subscriptionID, aadClientID, aadClientSecret, resourceGroup, location, vmType)
}
// If the tests are being run in Prow, credentials are not supplied through env vars. Instead, it is supplied
// through env var AZURE_CREDENTIALS. We need to convert it to AZURE_CREDENTIAL_FILE for sanity, integration and E2E tests
if testutil.IsRunningInProw() {
log.Println("Running in Prow, converting AZURE_CREDENTIALS to AZURE_CREDENTIAL_FILE")
c, err := getCredentialsFromAzureCredentials(os.Getenv("AZURE_CREDENTIALS"))
if err != nil {
return nil, err
}
return parseAndExecuteTemplate(cloud, c.TenantID, c.SubscriptionID, c.ClientID, c.ClientSecret, resourceGroup, location, vmType)
}
return nil, fmt.Errorf("If you are running tests locally, you will need to set the following env vars: $%s, $%s, $%s, $%s, $%s, $%s",
tenantIDEnvVar, subscriptionIDEnvVar, aadClientIDEnvVar, aadClientSecretEnvVar, resourceGroupEnvVar, locationEnvVar)
}
// DeleteAzureCredentialFile deletes the temporary Azure credential file
func DeleteAzureCredentialFile() error {
if err := os.Remove(TempAzureCredentialFilePath); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("error removing %s %v", TempAzureCredentialFilePath, err)
}
return nil
}
// getCredentialsFromAzureCredentials parses the azure credentials toml (AZURE_CREDENTIALS)
// in Prow and returns the credential information usable to Azure Disk CSI driver
func getCredentialsFromAzureCredentials(azureCredentialsPath string) (*FromProw, error) {
content, err := os.ReadFile(azureCredentialsPath)
log.Printf("Reading credentials file %v", azureCredentialsPath)
if err != nil {
return nil, fmt.Errorf("error reading credentials file %v %v", azureCredentialsPath, err)
}
c := Config{}
if err := toml.Unmarshal(content, &c); err != nil {
return nil, fmt.Errorf("error parsing credentials file %v %v", azureCredentialsPath, err)
}
return &c.Creds, nil
}
// parseAndExecuteTemplate replaces credential placeholders in azureCredentialFileTemplate with actual credentials
func parseAndExecuteTemplate(cloud, tenantID, subscriptionID, aadClientID, aadClientSecret, resourceGroup, location, vmType string) (*Credentials, error) {
t := template.New("AzureCredentialFileTemplate")
t, err := t.Parse(azureCredentialFileTemplate)
if err != nil {
return nil, fmt.Errorf("error parsing azureCredentialFileTemplate %v", err)
}
f, err := os.Create(TempAzureCredentialFilePath)
if err != nil {
return nil, fmt.Errorf("error creating %s %v", TempAzureCredentialFilePath, err)
}
defer f.Close()
c := Credentials{
cloud,
tenantID,
subscriptionID,
aadClientID,
aadClientSecret,
resourceGroup,
location,
vmType,
}
err = t.Execute(f, c)
if err != nil {
return nil, fmt.Errorf("error executing parsed azure credential file template %v", err)
}
return &c, nil
}