-
Notifications
You must be signed in to change notification settings - Fork 89
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
1 parent
6f1e35b
commit 0aad5bd
Showing
9 changed files
with
526 additions
and
271 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
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/Azure/azqr/internal/scanners" | ||
"github.com/Azure/azqr/internal/scanners/vpng" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
scanCmd.AddCommand(vpngCmd) | ||
} | ||
|
||
var vpngCmd = &cobra.Command{ | ||
Use: "vpng", | ||
Short: "Scan Azure VPN Gateway", | ||
Long: "Scan Azure VPN Gateway", | ||
Args: cobra.NoArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
serviceScanners := []scanners.IAzureScanner{ | ||
&vpng.VPNGatewayScanner{}, | ||
} | ||
|
||
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,63 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
package vpng | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/Azure/azqr/internal/scanners" | ||
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork" | ||
) | ||
|
||
// GetRules - Returns the rules for the VPNGatewayScanner | ||
func (a *VPNGatewayScanner) GetRules() map[string]scanners.AzureRule { | ||
return map[string]scanners.AzureRule{ | ||
"vpng-001": { | ||
Id: "vpng-001", | ||
Category: scanners.RulesCategoryMonitoringAndAlerting, | ||
Recommendation: "VPN Gateway should have diagnostic settings enabled", | ||
Impact: scanners.ImpactLow, | ||
Eval: func(target interface{}, scanContext *scanners.ScanContext) (bool, string) { | ||
service := target.(*armnetwork.VPNGateway) | ||
_, ok := scanContext.DiagnosticsSettings[strings.ToLower(*service.ID)] | ||
return !ok, "" | ||
}, | ||
Url: "https://learn.microsoft.com/en-us/azure/vpn-gateway/monitor-vpn-gateway", | ||
}, | ||
"vpng-002": { | ||
Id: "vpng-002", | ||
Category: scanners.RulesCategoryGovernance, | ||
Recommendation: "VPN Gateway Name should comply with naming conventions", | ||
Impact: scanners.ImpactLow, | ||
Eval: func(target interface{}, scanContext *scanners.ScanContext) (bool, string) { | ||
c := target.(*armnetwork.VPNGateway) | ||
caf := strings.HasPrefix(*c.Name, "vpng") | ||
return !caf, "" | ||
}, | ||
Url: "https://learn.microsoft.com/en-us/azure/cloud-adoption-framework/ready/azure-best-practices/resource-abbreviations", | ||
}, | ||
"vpng-003": { | ||
Id: "vpng-003", | ||
Category: scanners.RulesCategoryGovernance, | ||
Recommendation: "VPN Gateway should have tags", | ||
Impact: scanners.ImpactLow, | ||
Eval: func(target interface{}, scanContext *scanners.ScanContext) (bool, string) { | ||
c := target.(*armnetwork.VPNGateway) | ||
return len(c.Tags) == 0, "" | ||
}, | ||
Url: "https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/tag-resources?tabs=json", | ||
}, | ||
"vpng-004": { | ||
Id: "vpng-004", | ||
Category: scanners.RulesCategoryHighAvailability, | ||
Recommendation: "VPN Gateway should have a SLA", | ||
Impact: scanners.ImpactHigh, | ||
Eval: func(target interface{}, scanContext *scanners.ScanContext) (bool, string) { | ||
return false, "99.9%" | ||
//TODO: Filter SKU based on tier (BASIC / Or others) | ||
}, | ||
Url: "https://www.microsoft.com/licensing/docs/view/Service-Level-Agreements-SLA-for-Online-Services", | ||
}, | ||
} | ||
} |
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,89 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
package vpng | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/Azure/azqr/internal/scanners" | ||
"github.com/Azure/azqr/internal/to" | ||
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork" | ||
) | ||
|
||
func TestVPNGatewayScanner_Rules(t *testing.T) { | ||
type fields struct { | ||
rule string | ||
target interface{} | ||
scanContext *scanners.ScanContext | ||
} | ||
type want struct { | ||
broken bool | ||
result string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
want want | ||
}{ | ||
{ | ||
name: "VPNGatewayScanner DiagnosticSettings", | ||
fields: fields{ | ||
rule: "vpng-001", | ||
target: &armnetwork.VPNGateway{ | ||
ID: to.Ptr("test"), | ||
}, | ||
scanContext: &scanners.ScanContext{ | ||
DiagnosticsSettings: map[string]bool{ | ||
"test": true, | ||
}, | ||
}, | ||
}, | ||
want: want{ | ||
broken: false, | ||
result: "", | ||
}, | ||
}, | ||
{ | ||
name: "VPNGatewayScanner CAF", | ||
fields: fields{ | ||
rule: "vpng-002", | ||
target: &armnetwork.VPNGateway{ | ||
Name: to.Ptr("vpng-test"), | ||
}, | ||
scanContext: &scanners.ScanContext{}, | ||
}, | ||
want: want{ | ||
broken: false, | ||
result: "", | ||
}, | ||
}, | ||
{ | ||
name: "VPNGatewayScanner SLA 99.9%", | ||
fields: fields{ | ||
rule: "vpng-004", | ||
target: &armnetwork.VPNGateway{}, | ||
scanContext: &scanners.ScanContext{}, | ||
}, | ||
want: want{ | ||
broken: false, | ||
result: "99.9%", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
s := &VPNGatewayScanner{} | ||
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("VPNGatewayScanner Rule.Eval() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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,64 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
package vpng | ||
|
||
import ( | ||
"github.com/Azure/azqr/internal/scanners" | ||
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork" | ||
) | ||
|
||
// VPNGatewayScanner - Scanner for VPN Gateway | ||
type VPNGatewayScanner struct { | ||
config *scanners.ScannerConfig | ||
client *armnetwork.VPNGatewaysClient | ||
} | ||
|
||
// Init - Initializes the VPN Gateway | ||
func (c *VPNGatewayScanner) Init(config *scanners.ScannerConfig) error { | ||
c.config = config | ||
var err error | ||
c.client, err = armnetwork.NewVPNGatewaysClient(config.SubscriptionID, config.Cred, config.ClientOptions) | ||
return err | ||
} | ||
|
||
// Scan - Scans all VirtualNetwork in a Resource Group | ||
func (c *VPNGatewayScanner) Scan(resourceGroupName string, scanContext *scanners.ScanContext) ([]scanners.AzureServiceResult, error) { | ||
scanners.LogResourceGroupScan(c.config.SubscriptionID, resourceGroupName, "VPN Gateway") | ||
|
||
vpns, err := c.list(resourceGroupName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
engine := scanners.RuleEngine{} | ||
rules := c.GetRules() | ||
results := []scanners.AzureServiceResult{} | ||
|
||
for _, w := range vpns { | ||
rr := engine.EvaluateRules(rules, w, scanContext) | ||
|
||
results = append(results, scanners.AzureServiceResult{ | ||
SubscriptionID: c.config.SubscriptionID, | ||
ResourceGroup: resourceGroupName, | ||
ServiceName: *w.Name, | ||
Type: *w.Type, | ||
Location: *w.Location, | ||
Rules: rr, | ||
}) | ||
} | ||
return results, nil | ||
} | ||
|
||
func (c *VPNGatewayScanner) list(resourceGroupName string) ([]*armnetwork.VPNGateway, error) { | ||
pager := c.client.NewListByResourceGroupPager(resourceGroupName, nil) | ||
|
||
vpns := make([]*armnetwork.VPNGateway, 0) | ||
for pager.More() { | ||
resp, err := pager.NextPage(c.config.Ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
vpns = append(vpns, resp.Value...) | ||
} | ||
return vpns, nil | ||
} |