-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
287 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
package azqr | ||
|
||
import ( | ||
"github.com/cmendible/azqr/internal/scanners" | ||
"github.com/cmendible/azqr/internal/scanners/appi" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
scanCmd.AddCommand(appiCmd) | ||
} | ||
|
||
var appiCmd = &cobra.Command{ | ||
Use: "appi", | ||
Short: "Scan Azure Application Insights", | ||
Long: "Scan Azure Application Insights", | ||
Args: cobra.NoArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
serviceScanners := []scanners.IAzureScanner{ | ||
&appi.AppInsightsScanner{}, | ||
} | ||
|
||
scan(cmd, serviceScanners) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
package appi | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/applicationinsights/armapplicationinsights" | ||
"github.com/cmendible/azqr/internal/scanners" | ||
) | ||
|
||
// AppInsightsScanner - Scanner for Front Door | ||
type AppInsightsScanner struct { | ||
config *scanners.ScannerConfig | ||
diagnosticsSettings scanners.DiagnosticsSettings | ||
client *armapplicationinsights.ComponentsClient | ||
} | ||
|
||
// Init - Initializes the Application Insights Scanner | ||
func (a *AppInsightsScanner) Init(config *scanners.ScannerConfig) error { | ||
a.config = config | ||
var err error | ||
a.client, err = armapplicationinsights.NewComponentsClient(config.SubscriptionID, a.config.Cred, a.config.ClientOptions) | ||
if err != nil { | ||
return err | ||
} | ||
a.diagnosticsSettings = scanners.DiagnosticsSettings{} | ||
err = a.diagnosticsSettings.Init(config) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// Scan - Scans all Application Insights in a Resource Group | ||
func (a *AppInsightsScanner) Scan(resourceGroupName string, scanContext *scanners.ScanContext) ([]scanners.AzureServiceResult, error) { | ||
log.Printf("Scanning Application Insights in Resource Group %s", resourceGroupName) | ||
|
||
gateways, err := a.list(resourceGroupName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
engine := scanners.RuleEngine{} | ||
rules := a.GetRules() | ||
results := []scanners.AzureServiceResult{} | ||
|
||
for _, g := range gateways { | ||
rr := engine.EvaluateRules(rules, g, scanContext) | ||
|
||
results = append(results, scanners.AzureServiceResult{ | ||
SubscriptionID: a.config.SubscriptionID, | ||
ResourceGroup: resourceGroupName, | ||
Location: *g.Location, | ||
Type: *g.Type, | ||
ServiceName: *g.Name, | ||
Rules: rr, | ||
}) | ||
} | ||
return results, nil | ||
} | ||
|
||
func (a *AppInsightsScanner) list(resourceGroupName string) ([]*armapplicationinsights.Component, error) { | ||
pager := a.client.NewListByResourceGroupPager(resourceGroupName, nil) | ||
|
||
services := make([]*armapplicationinsights.Component, 0) | ||
for pager.More() { | ||
resp, err := pager.NextPage(a.config.Ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
services = append(services, resp.Value...) | ||
} | ||
return services, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
package appi | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/applicationinsights/armapplicationinsights" | ||
"github.com/cmendible/azqr/internal/scanners" | ||
) | ||
|
||
// GetRules - Returns the rules for the FrontDoorScanner | ||
func (a *AppInsightsScanner) GetRules() map[string]scanners.AzureRule { | ||
return map[string]scanners.AzureRule{ | ||
"SLA": { | ||
Id: "appi-001", | ||
Category: scanners.RulesCategoryReliability, | ||
Subcategory: scanners.RulesSubcategoryReliabilitySLA, | ||
Description: "Azure Application Insights SLA", | ||
Severity: scanners.SeverityHigh, | ||
Eval: func(target interface{}, scanContext *scanners.ScanContext) (bool, string) { | ||
return false, "99.9%" | ||
}, | ||
Url: "https://www.azure.cn/en-us/support/sla/application-insights/index.html", | ||
}, | ||
"CAF": { | ||
Id: "appi-002", | ||
Category: scanners.RulesCategoryOperationalExcellence, | ||
Subcategory: scanners.RulesSubcategoryOperationalExcellenceCAF, | ||
Description: "Azure Application Insights Name should comply with naming conventions", | ||
Severity: scanners.SeverityLow, | ||
Eval: func(target interface{}, scanContext *scanners.ScanContext) (bool, string) { | ||
c := target.(*armapplicationinsights.Component) | ||
caf := strings.HasPrefix(*c.Name, "appi") | ||
return !caf, "" | ||
}, | ||
Url: "https://learn.microsoft.com/en-us/azure/cloud-adoption-framework/ready/azure-best-practices/resource-abbreviations", | ||
}, | ||
"appi-003": { | ||
Id: "appi-003", | ||
Category: scanners.RulesCategoryOperationalExcellence, | ||
Subcategory: scanners.RulesSubcategoryOperationalExcellenceTags, | ||
Description: "Azure Application Insights should have tags", | ||
Severity: scanners.SeverityLow, | ||
Eval: func(target interface{}, scanContext *scanners.ScanContext) (bool, string) { | ||
c := target.(*armapplicationinsights.Component) | ||
return c.Tags == nil || len(c.Tags) == 0, "" | ||
}, | ||
Url: "https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/tag-resources?tabs=json", | ||
}, | ||
"appi-004": { | ||
Id: "appi-004", | ||
Category: scanners.RulesCategoryOperationalExcellence, | ||
Subcategory: scanners.RulesSubcategoryOperationalExcellenceTags, | ||
Description: "Azure Application Insights should store data in a Log Analytics Workspace", | ||
Severity: scanners.SeverityLow, | ||
Eval: func(target interface{}, scanContext *scanners.ScanContext) (bool, string) { | ||
c := target.(*armapplicationinsights.Component) | ||
|
||
return c.Properties.WorkspaceResourceID == nil, "" | ||
}, | ||
Url: "https://learn.microsoft.com/en-us/azure/azure-monitor/app/create-workspace-resource", | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
package appi | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/applicationinsights/armapplicationinsights" | ||
"github.com/Azure/go-autorest/autorest/to" | ||
"github.com/cmendible/azqr/internal/scanners" | ||
) | ||
|
||
func TestAppInsightsScanner_Rules(t *testing.T) { | ||
type fields struct { | ||
rule string | ||
target interface{} | ||
scanContext *scanners.ScanContext | ||
diagnosticsSettings scanners.DiagnosticsSettings | ||
} | ||
type want struct { | ||
broken bool | ||
result string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
want want | ||
}{ | ||
{ | ||
name: "AppInsightsScanner SLA", | ||
fields: fields{ | ||
rule: "SLA", | ||
target: &armapplicationinsights.Component{}, | ||
scanContext: &scanners.ScanContext{}, | ||
diagnosticsSettings: scanners.DiagnosticsSettings{}, | ||
}, | ||
want: want{ | ||
broken: false, | ||
result: "99.9%", | ||
}, | ||
}, | ||
{ | ||
name: "AppInsightsScanner CAF", | ||
fields: fields{ | ||
rule: "CAF", | ||
target: &armapplicationinsights.Component{ | ||
Name: to.StringPtr("appi-test"), | ||
}, | ||
scanContext: &scanners.ScanContext{}, | ||
diagnosticsSettings: scanners.DiagnosticsSettings{}, | ||
}, | ||
want: want{ | ||
broken: false, | ||
result: "", | ||
}, | ||
}, | ||
{ | ||
name: "AppInsightsScanner tags", | ||
fields: fields{ | ||
rule: "appi-003", | ||
target: &armapplicationinsights.Component{}, | ||
scanContext: &scanners.ScanContext{}, | ||
diagnosticsSettings: scanners.DiagnosticsSettings{}, | ||
}, | ||
want: want{ | ||
broken: true, | ||
result: "", | ||
}, | ||
}, | ||
{ | ||
name: "AppInsightsScanner WorkspaceId", | ||
fields: fields{ | ||
rule: "appi-004", | ||
target: &armapplicationinsights.Component{ | ||
Properties: &armapplicationinsights.ComponentProperties{}, | ||
}, | ||
scanContext: &scanners.ScanContext{}, | ||
diagnosticsSettings: scanners.DiagnosticsSettings{}, | ||
}, | ||
want: want{ | ||
broken: true, | ||
result: "", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
s := &AppInsightsScanner{ | ||
diagnosticsSettings: tt.fields.diagnosticsSettings, | ||
} | ||
rules := s.GetRules() | ||
b, w := rules[tt.fields.rule].Eval(tt.fields.target, tt.fields.scanContext) | ||
got := want{ | ||
broken: b, | ||
result: w, | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("AppInsightsScanner Rule.Eval() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |