-
Notifications
You must be signed in to change notification settings - Fork 19
/
compute.go
229 lines (192 loc) · 7.22 KB
/
compute.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright 2021 Fraunhofer AISEC
//
// 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.
//
// $$\ $$\ $$\ $$\
// $$ | $$ |\__| $$ |
// $$$$$$$\ $$ | $$$$$$\ $$\ $$\ $$$$$$$ |$$\ $$$$$$\ $$$$$$\ $$$$$$\
// $$ _____|$$ |$$ __$$\ $$ | $$ |$$ __$$ |$$ |\_$$ _| $$ __$$\ $$ __$$\
// $$ / $$ |$$ / $$ |$$ | $$ |$$ / $$ |$$ | $$ | $$ / $$ |$$ | \__|
// $$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ |$$\ $$ | $$ |$$ |
// \$$$$$$\ $$ |\$$$$$ |\$$$$$ |\$$$$$$ |$$ | \$$$ |\$$$$$ |$$ |
// \_______|\__| \______/ \______/ \_______|\__| \____/ \______/ \__|
//
// This file is part of Clouditor Community Edition.
package azure
import (
"context"
"fmt"
"clouditor.io/clouditor/api/discovery"
"clouditor.io/clouditor/voc"
"github.com/Azure/azure-sdk-for-go/profiles/2020-09-01/compute/mgmt/compute"
"github.com/Azure/azure-sdk-for-go/profiles/latest/web/mgmt/web"
"github.com/Azure/go-autorest/autorest/to"
)
type azureComputeDiscovery struct {
azureDiscovery
}
func NewAzureComputeDiscovery(opts ...DiscoveryOption) discovery.Discoverer {
d := &azureComputeDiscovery{}
for _, opt := range opts {
if auth, ok := opt.(*authorizerOption); ok {
d.authOption = auth
} else {
d.options = append(d.options, opt)
}
}
return d
}
func (*azureComputeDiscovery) Name() string {
return "Azure Compute"
}
func (*azureComputeDiscovery) Description() string {
return "Discovery Azure compute."
}
// List compute resources
func (d *azureComputeDiscovery) List() (list []voc.IsCloudResource, err error) {
if err = d.authorize(); err != nil {
return nil, fmt.Errorf("could not authorize Azure account: %w", err)
}
// Discover virtual machines
virtualMachines, err := d.discoverVirtualMachines()
if err != nil {
return nil, fmt.Errorf("could not discover virtual machines: %w", err)
}
list = append(list, virtualMachines...)
// Discover functions
function, err := d.discoverFunction()
if err != nil {
return nil, fmt.Errorf("could not discover functions: %w", err)
}
list = append(list, function...)
return
}
// Discover function
func (d *azureComputeDiscovery) discoverFunction() ([]voc.IsCloudResource, error) {
var list []voc.IsCloudResource
client := web.NewAppsClient(to.String(d.sub.SubscriptionID))
d.apply(&client.Client)
result, err := client.ListComplete(context.Background())
if err != nil {
return nil, fmt.Errorf("could not list functions: %w", err)
}
functionApp := *result.Response().Value
for i := range functionApp {
functionResource := d.handleFunction(&functionApp[i])
list = append(list, functionResource)
}
return list, err
}
func (*azureComputeDiscovery) handleFunction(function *web.Site) voc.IsCompute {
return &voc.Function{
Compute: &voc.Compute{
Resource: &voc.Resource{
ID: voc.ResourceID(to.String(function.ID)),
Name: to.String(function.Name),
CreationTime: 0, // No creation time available
Type: []string{"Function", "Compute", "Resource"},
GeoLocation: voc.GeoLocation{
Region: to.String(function.Location),
},
},
},
}
}
// Discover virtual machines
func (d *azureComputeDiscovery) discoverVirtualMachines() ([]voc.IsCloudResource, error) {
var list []voc.IsCloudResource
client := compute.NewVirtualMachinesClient(to.String(d.sub.SubscriptionID))
d.apply(&client.Client)
result, err := client.ListAllComplete(context.Background(), "true")
if err != nil {
return nil, fmt.Errorf("could not list virtual machines: %w", err)
}
vms := *result.Response().Value
for i := range vms {
s, err := d.handleVirtualMachines(&vms[i])
if err != nil {
return nil, fmt.Errorf("could not handle virtual machine: %w", err)
}
log.Infof("Adding virtual machine %+v", s)
list = append(list, s)
}
return list, err
}
func (d *azureComputeDiscovery) handleVirtualMachines(vm *compute.VirtualMachine) (voc.IsCompute, error) {
vmExtended, err := d.extendedVirtualMachine(vm)
if err != nil {
return nil, fmt.Errorf("could not get virtual machine with extended information: %w", err)
}
r := &voc.VirtualMachine{
Compute: &voc.Compute{
Resource: &voc.Resource{
ID: voc.ResourceID(to.String(vm.ID)),
Name: to.String(vm.Name),
CreationTime: 0, // No creation time available
Type: []string{"VirtualMachine", "Compute", "Resource"},
GeoLocation: voc.GeoLocation{
Region: to.String(vm.Location),
},
}},
BootLogging: &voc.BootLogging{Logging: &voc.Logging{
Enabled: isBootDiagnosticEnabled(vmExtended),
LoggingService: []voc.ResourceID{voc.ResourceID(bootLogOutput(vmExtended))},
RetentionPeriod: 0, // Currently, configuring the retention period for Managed Boot Diagnostics is not available. The logs will be overwritten after 1gb of space according to https://github.com/MicrosoftDocs/azure-docs/issues/69953
}},
OSLogging: &voc.OSLogging{
Logging: &voc.Logging{
Enabled: false,
RetentionPeriod: 0,
LoggingService: []voc.ResourceID{},
},
},
BlockStorage: []voc.ResourceID{},
}
// Reference to networkInterfaces
for _, networkInterfaces := range *vmExtended.VirtualMachineProperties.NetworkProfile.NetworkInterfaces {
r.NetworkInterface = append(r.NetworkInterface, voc.ResourceID(to.String(networkInterfaces.ID)))
}
// Reference to blockstorage
r.BlockStorage = append(r.BlockStorage, voc.ResourceID(to.String(vmExtended.StorageProfile.OsDisk.ManagedDisk.ID)))
for _, blockstorage := range *vmExtended.StorageProfile.DataDisks {
r.BlockStorage = append(r.BlockStorage, voc.ResourceID(to.String(blockstorage.ManagedDisk.ID)))
}
return r, nil
}
// extendedVirtualMachine gets virtual machine with extended information, e.g., managed disk ID, network interface ID
func (d *azureComputeDiscovery) extendedVirtualMachine(vm *compute.VirtualMachine) (*compute.VirtualMachine, error) {
client := compute.NewVirtualMachinesClient(to.String(d.sub.SubscriptionID))
d.apply(&client.Client)
vmExtended, err := client.Get(context.Background(), getResourceGroupName(to.String(vm.ID)), to.String(vm.Name), "")
if err != nil {
return nil, fmt.Errorf("could not get virtual machine: %w", err)
}
return &vmExtended, nil
}
func isBootDiagnosticEnabled(vm *compute.VirtualMachine) bool {
if vm.DiagnosticsProfile == nil {
return false
} else {
return to.Bool(vm.DiagnosticsProfile.BootDiagnostics.Enabled)
}
}
func bootLogOutput(vm *compute.VirtualMachine) string {
if isBootDiagnosticEnabled(vm) {
// If storageUri is not specified while enabling boot diagnostics, managed storage will be used.
if vm.DiagnosticsProfile.BootDiagnostics.StorageURI != nil {
return to.String(vm.DiagnosticsProfile.BootDiagnostics.StorageURI)
}
return ""
}
return ""
}