-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathreports_azure.go
183 lines (157 loc) · 4.63 KB
/
reports_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
//
// Author:: Darren Murray (<[email protected]>)
// Copyright:: Copyright 2022, Lacework Inc.
// License:: Apache License, Version 2.0
//
// 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 api
import (
"fmt"
"io"
"os"
"sort"
"time"
"github.com/pkg/errors"
)
// v2AzureReportsService is a service that interacts with the APIv2
type azureReportsService struct {
client *Client
}
const ComplianceReportDefaultAzure = "CIS Microsoft Azure Foundations Benchmark v1.5.0"
type AzureReportConfig struct {
TenantID string
SubscriptionID string
Value string
Parameter reportFilter
}
type AzureReportType int
func (report AzureReportType) String() string {
return azureReportTypes[report]
}
func NewAzureReportType(report string) (AzureReportType, error) {
for k, v := range azureReportTypes {
if v == report {
return k, nil
}
}
return NONE_AZURE_REPORT, errors.Errorf("no report type found for %s", report)
}
func AzureReportTypes() []string {
reportTypes := make([]string, 0, len(azureReportTypes))
for _, report := range azureReportTypes {
reportTypes = append(reportTypes, report)
}
sort.Strings(reportTypes)
return reportTypes
}
var azureReportTypes = map[AzureReportType]string{
AZURE_CIS: "AZURE_CIS",
AZURE_CIS_131: "AZURE_CIS_131",
AZURE_SOC: "AZURE_SOC",
AZURE_SOC_Rev2: "AZURE_SOC_Rev2",
AZURE_PCI: "AZURE_PCI",
AZURE_PCI_Rev2: "AZURE_PCI_Rev2",
AZURE_ISO_27001: "AZURE_ISO_27001",
AZURE_NIST_CSF: "AZURE_NIST_CSF",
AZURE_NIST_800_53_REV5: "AZURE_NIST_800_53_REV5",
AZURE_NIST_800_171_REV2: "AZURE_NIST_800_171_REV2",
AZURE_HIPAA: "AZURE_HIPAA",
}
const (
NONE_AZURE_REPORT AzureReportType = iota
AZURE_CIS
AZURE_CIS_131
AZURE_SOC
AZURE_SOC_Rev2
AZURE_PCI
AZURE_PCI_Rev2
AZURE_ISO_27001
AZURE_NIST_CSF
AZURE_NIST_800_53_REV5
AZURE_NIST_800_171_REV2
AZURE_HIPAA
)
// Get returns an AzureReportResponse
func (svc *azureReportsService) Get(reportCfg AzureReportConfig) (response AzureReportResponse, err error) {
if reportCfg.SubscriptionID == "" {
return AzureReportResponse{}, errors.New("specify an account id")
}
apiPath := fmt.Sprintf(apiV2ReportsSecondaryQuery,
reportCfg.TenantID,
reportCfg.SubscriptionID,
"json",
reportCfg.Parameter.String(),
reportCfg.Value,
)
err = svc.client.RequestDecoder("GET", apiPath, nil, &response)
return
}
func (svc *azureReportsService) DownloadPDF(filepath string, config AzureReportConfig) error {
if config.TenantID == "" || config.SubscriptionID == "" {
return errors.New("tenant_id and subscription_id are required")
}
apiPath := fmt.Sprintf(apiV2ReportsSecondaryQuery,
config.TenantID,
config.SubscriptionID,
"pdf",
config.Parameter.String(),
config.Value,
)
request, err := svc.client.NewRequest("GET", apiPath, nil)
if err != nil {
return err
}
response, err := svc.client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()
err = checkErrorInResponse(response)
if err != nil {
return err
}
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, response.Body)
return err
}
func (azure AzureReport) GetComplianceRecommendation(recommendationID string) (*RecommendationV2, bool) {
for _, r := range azure.Recommendations {
if r.RecID == recommendationID {
return &r, true
}
}
return nil, false
}
type AzureReportResponse struct {
Data []AzureReport `json:"data"`
Ok bool `json:"ok"`
Message string `json:"message"`
}
type AzureReport struct {
ReportType string `json:"reportType"`
ReportTitle string `json:"reportTitle"`
Recommendations []RecommendationV2 `json:"recommendations"`
Summary []ReportSummary `json:"summary"`
ReportTime time.Time `json:"reportTime"`
SubscriptionName string `json:"subscriptionName"`
SubscriptionID string `json:"SubscriptionID"`
TenantName string `json:"tenantName"`
TenantID string `json:"tenantId"`
}