This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccount_azure.go
201 lines (170 loc) · 5.2 KB
/
account_azure.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
package autosignr
import (
"context"
"fmt"
"regexp"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
"github.com/Azure/go-autorest/autorest/azure/auth"
log "github.com/sirupsen/logrus"
)
var rgRegexp = regexp.MustCompile("resourceGroups/(.+?)/providers")
// AccountAzure encapsulates the account information for Azure
type AccountAzure struct {
Name string `yaml:"name"`
Domain string `yaml:"dns_zone"`
ClientID string `yaml:"client_id"`
ClientSecret string `yaml:"client_secret"`
SubscriptionID string `yaml:"subscription_id"`
TenantID string `yaml:"tenant_id"`
Attribute string `yaml:"attribute"`
vmClient compute.VirtualMachinesClient
vmssClient compute.VirtualMachineScaleSetsClient
vmssVMClient compute.VirtualMachineScaleSetVMsClient
}
// Init setup the account
func (a *AccountAzure) Init() error {
if a.Attribute == "" {
a.Attribute = "Name"
}
if a.Domain == "" {
a.Domain = "dns_zone"
}
conf := auth.NewClientCredentialsConfig(a.ClientID, a.ClientSecret, a.TenantID)
a.vmClient = compute.NewVirtualMachinesClient(a.SubscriptionID)
a.vmssClient = compute.NewVirtualMachineScaleSetsClient(a.SubscriptionID)
a.vmssVMClient = compute.NewVirtualMachineScaleSetVMsClient(a.SubscriptionID)
var err error
a.vmClient.Authorizer, err = conf.Authorizer()
if err != nil {
return err
}
a.vmssClient.Authorizer, err = conf.Authorizer()
if err != nil {
return err
}
a.vmssVMClient.Authorizer, err = conf.Authorizer()
if err != nil {
return err
}
return nil
}
// Type returns the type of account
func (a AccountAzure) Type() string {
return "azure"
}
// Check look for the instanceID in the account
func (a *AccountAzure) Check(instanceID string) bool {
log.WithFields(log.Fields{
"instance": instanceID,
"subscription": a.Name,
}).Debug("checking")
// Check the Virtual Machines endpoints
if vmCheck, err := a.checkVM(instanceID); err == nil && vmCheck {
log.WithFields(log.Fields{
"instance": instanceID,
"account": a.Name,
"found": true,
}).Debug("check-result")
return true
}
// Check the Virtual Machine Scale Sets
if vmssCheck, err := a.checkScaleSetVM(instanceID); err == nil && vmssCheck {
log.WithFields(log.Fields{
"instance": instanceID,
"account": a.Name,
"found": true,
}).Debug("check-result")
return true
}
log.WithFields(log.Fields{
"instance": instanceID,
"account": a.Name,
"found": false,
}).Debug("check-result")
return false
}
// String returns the account info
func (a AccountAzure) String() string {
return fmt.Sprintf("azure account: %s", a.Name)
}
// checkVM will look for the instance in Azure Virtual Machines
func (a AccountAzure) checkVM(instanceID string) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
// Check the Virtual Machines endpoints
for subList, err := a.vmClient.ListAll(ctx, ""); subList.NotDone(); err = subList.NextWithContext(ctx) {
if err != nil {
return false, err
}
for _, instance := range subList.Values() {
// Check for the name tag
if val, ok := instance.Tags[a.Attribute]; ok {
if *val == instanceID {
return true, nil
}
}
// Check for the dns_zone tag
if val, ok := instance.Tags[a.Domain]; ok {
if strings.ToLower(fmt.Sprintf("%s.%s", *instance.OsProfile.ComputerName, *val)) == instanceID {
return true, nil
}
}
}
}
return false, nil
}
// getAllScaleSets will pull all the Azure Virtual Machine Scale Set in the account
func (a AccountAzure) getAllScaleSets() (map[string]string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
scaleSets := make(map[string]string)
for subList, err := a.vmssClient.ListAll(ctx); subList.NotDone(); err = subList.NextWithContext(ctx) {
if err != nil {
return scaleSets, err
}
for _, scaleSet := range subList.Values() {
rg := resourceGroupFromAzureID(*scaleSet.ID)
if rg == "" {
return scaleSets, fmt.Errorf("Error parsing out resource group from %s", *scaleSet.ID)
}
scaleSets[rg] = *scaleSet.Name
}
}
return scaleSets, nil
}
// checkScaleSetVM will check all Azure Virtual Machine Scale Set instances
func (a AccountAzure) checkScaleSetVM(instanceID string) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
// Pull all the scale sets in subscription
scaleSets, err := a.getAllScaleSets()
if err != nil {
return false, err
}
for k, v := range scaleSets {
for subList, err := a.vmssVMClient.List(ctx, k, v, "", "", ""); subList.NotDone(); err = subList.NextWithContext(ctx) {
if err != nil {
return false, err
}
for _, instance := range subList.Values() {
// Check for the dns_zone tag
if val, ok := instance.Tags[a.Domain]; ok {
if strings.ToLower(fmt.Sprintf("%s.%s", *instance.OsProfile.ComputerName, *val)) == instanceID {
return true, nil
}
}
}
}
}
return false, nil
}
// resourceGroupFromAzureID will try to parse the resource group out of a url
func resourceGroupFromAzureID(str string) string {
match := rgRegexp.FindStringSubmatch(str)
if len(match) > 1 {
return match[1]
}
return ""
}