generated from steadybit/extension-scaffold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
157 lines (142 loc) · 5.22 KB
/
client.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
package common
import (
"crypto"
"crypto/x509"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v4"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resourcegraph/armresourcegraph"
"github.com/rs/zerolog/log"
"github.com/steadybit/extension-azure/config"
"os"
)
func GetClientByCredentials() (*armresourcegraph.Client, error) {
cred, err := connectionAzure()
if err != nil {
log.Error().Err(err).Msgf("Failed to create Azure connection.")
return nil, err
}
// Create and authorize a ResourceGraph client
client, err := armresourcegraph.NewClient(cred, nil)
if err != nil {
log.Error().Err(err).Msgf("Failed to create Azure resource graph client.")
return nil, err
}
return client, nil
}
func GetVirtualMachinesClient(subscriptionId string) (*armcompute.VirtualMachinesClient, error) {
conn, err := connectionAzure()
if err != nil {
log.Error().Err(err).Msgf("Failed to create Azure connection.")
return nil, err
}
computeClientFactory, err := armcompute.NewClientFactory(subscriptionId, conn, nil)
if err != nil {
log.Error().Err(err).Msgf("Failed to create Azure compute client.")
return nil, err
}
virtualMachinesClient := computeClientFactory.NewVirtualMachinesClient()
return virtualMachinesClient, nil
}
func GetVirtualMachineScaleSetVMsClient(subscriptionId string) (*armcompute.VirtualMachineScaleSetVMsClient, error) {
conn, err := connectionAzure()
if err != nil {
log.Error().Err(err).Msgf("Failed to create Azure connection.")
return nil, err
}
computeClientFactory, err := armcompute.NewClientFactory(subscriptionId, conn, nil)
if err != nil {
log.Error().Err(err).Msgf("Failed to create Azure compute client.")
return nil, err
}
virtualMachinesClient := computeClientFactory.NewVirtualMachineScaleSetVMsClient()
return virtualMachinesClient, nil
}
func connectionAzure() (azcore.TokenCredential, error) {
tenantID := os.Getenv("AZURE_TENANT_ID")
clientID := os.Getenv("AZURE_CLIENT_ID")
clientSecret := os.Getenv("AZURE_CLIENT_SECRET")
userAssertionString := config.Config.AzureUserAssertionString
certificateLocation := config.Config.AzureCertificatePath
certificatePassphrase := config.Config.AzureCertificatePassword
if certificateLocation != "" {
if userAssertionString != "" {
return credsByCertificateOnUserBehalf(certificateLocation, certificatePassphrase, userAssertionString, tenantID, clientID)
}
return credsByCertificate(certificateLocation, certificatePassphrase, tenantID, clientID)
}
if userAssertionString != "" {
return credsByUserAssertion(userAssertionString, tenantID, clientID, clientSecret)
}
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Warn().Err(err).Msgf("Failed to create default Azure credential.")
// Constructs a ClientSecretCredential. Pass nil for options to accept defaults.
cred, err := azidentity.NewClientSecretCredential(tenantID, clientID, clientSecret, nil)
if err != nil {
log.Error().Msgf("failed to create credential: %v", err)
}
return cred, err
}
return cred, nil
}
func credsByUserAssertion(userAssertionString string, tenantID string, clientID string, clientSecret string) (azcore.TokenCredential, error) {
cred, err := azidentity.NewOnBehalfOfCredentialWithSecret(
tenantID,
clientID,
userAssertionString,
clientSecret,
nil,
)
if err != nil {
log.Error().Err(err).Msgf("Failed to create Azure credential with user assertion.")
return nil, err
}
return cred, nil
}
func credsByCertificate(certificateLocation string, passphrase string, tenantID string, clientID string) (azcore.TokenCredential, error) {
certs, key, err := getCertsAndKey(certificateLocation, passphrase)
if err != nil {
log.Error().Err(err).Msgf("Failed to get certificate and key.")
return nil, err
}
cred, err := azidentity.NewClientCertificateCredential(
tenantID,
clientID,
certs,
key,
nil,
)
if err != nil {
log.Error().Err(err).Msgf("Failed to create credential by certificate.")
return nil, err
}
return cred, nil
}
func getCertsAndKey(certificateLocation string, passphrase string) ([]*x509.Certificate, crypto.PrivateKey, error) {
log.Debug().Msgf("Using certificate authentication.")
certData, err := os.ReadFile(certificateLocation)
if err != nil {
log.Error().Err(err).Msgf("Failed to read certificate file.")
return nil, nil, err
}
certs, key, err := azidentity.ParseCertificates(certData, []byte(passphrase))
if err != nil {
log.Error().Err(err).Msgf("Failed to parse certificate.")
return nil, nil, err
}
return certs, key, nil
}
func credsByCertificateOnUserBehalf(certificateLocation string, passphrase string, userAssertionString string, tenantID string, clientID string) (azcore.TokenCredential, error) {
certs, key, err := getCertsAndKey(certificateLocation, passphrase)
if err != nil {
log.Error().Err(err).Msgf("Failed to get certificate and key.")
return nil, err
}
creds, err := azidentity.NewOnBehalfOfCredentialWithCertificate(tenantID, clientID, userAssertionString, certs, key, nil)
if err != nil {
log.Fatal().Err(err).Msgf("Failed to create Azure credential by NewOnBehalfOfCredentialWithCertificate.")
return nil, err
}
return creds, nil
}