Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server vulnerability assessment resource #10030

Merged
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
10d4fac
Initial work on new resource server_vulnerability_assessment_resource
martenbohlin Dec 30, 2020
9446b51
Fixed import of server_vulnerability_assessment_resource
martenbohlin Dec 30, 2020
3dc89c6
Fixed lint errors.
martenbohlin Jan 1, 2021
a852f12
Use azurerm_linux_virtual_machine in favor of azurerm_virtual_machine…
martenbohlin Jan 7, 2021
ec6c4b7
Merge remote-tracking branch 'origin/master' into server_vulnerabilit…
martenbohlin Jan 7, 2021
25d81d9
Fied lint errors.
martenbohlin Jan 7, 2021
8e59bda
Allow applying azurerm_server_vulnerability_assessment on Azure ARC (…
martenbohlin Jan 8, 2021
b41219e
Added note in documentation that Azure Defender has to be enabled for…
martenbohlin Jan 9, 2021
6767de6
Merge remote-tracking branch 'origin/master' into server_vulnerabilit…
martenbohlin Jan 9, 2021
a02734c
Added reference to bug in azure-sdk-for-go.
martenbohlin Jan 10, 2021
2ffd690
Improved the azurerm_server_vulnerability_assessment resource by usin…
martenbohlin Jan 10, 2021
a341ee1
Fixed feedback from neil-yechenwei on pull request #10030.
martenbohlin Jan 12, 2021
85288dd
Merge remote-tracking branch 'origin/master' into server_vulnerabilit…
martenbohlin Jan 12, 2021
29d2fc3
Fixed error in documentation meta data.
martenbohlin Jan 12, 2021
2bc6837
The refrenced bug was closed as duplicate, so update the reference to…
martenbohlin Jan 12, 2021
ed9f9c8
Added test asserting that server vulnerability assessment resource gi…
martenbohlin Jan 17, 2021
f81be24
Merge remote-tracking branch 'origin/master' into server_vulnerabilit…
martenbohlin Jan 17, 2021
828015d
Merge remote-tracking branch 'origin/master' into server_vulnerabilit…
martenbohlin Jan 20, 2021
eb51b02
Merge remote-tracking branch 'origin/master' into server_vulnerabilit…
martenbohlin Jan 21, 2021
9e233b7
Merge remote-tracking branch 'origin/master' into server_vulnerabilit…
martenbohlin Jan 30, 2021
b19e769
Revert changes to resourceid since it caused problems with other reso…
martenbohlin Jan 30, 2021
54b7f7d
Changed location in documentation.
martenbohlin Mar 3, 2021
3b7f573
Merge remote-tracking branch 'origin/master' into server_vulnerabilit…
martenbohlin Mar 3, 2021
412a716
Fixed formating after merge.
martenbohlin Mar 3, 2021
bd5cb3f
Improved documentation.
martenbohlin Mar 6, 2021
b493b59
Renamed azurerm_server_vulnerability_assessment to azurerm_security_c…
martenbohlin Mar 6, 2021
a4892eb
Merge remote-tracking branch 'origin/master' into server_vulnerabilit…
martenbohlin Mar 6, 2021
ac3cc59
Merge remote-tracking branch 'origin/master' into server_vulnerabilit…
martenbohlin Mar 9, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions azurerm/helpers/azure/resourceid.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func ParseAzureResourceID(id string) (*ResourceID, error) {
return nil, fmt.Errorf("The number of path segments is not divisible by 2 in %q", path)
}

var subscriptionID string
var subscriptionID, provider string

// Put the constituent key-value pairs into a map
componentMap := make(map[string]string, len(components)/2)
Expand All @@ -54,9 +54,12 @@ func ParseAzureResourceID(id string) (*ResourceID, error) {

// Catch the subscriptionID before it can be overwritten by another "subscriptions"
// value in the ID which is the case for the Service Bus subscription resource
if key == "subscriptions" && subscriptionID == "" {
switch {
case key == "subscriptions" && subscriptionID == "":
subscriptionID = value
} else {
case key == "providers" && provider == "": // The same for provider for serverVulnerabilityAssessment resource
provider = value
default:
componentMap[key] = value
}
}
Expand All @@ -83,9 +86,8 @@ func ParseAzureResourceID(id string) (*ResourceID, error) {
}

// It is OK not to have a provider in the case of a resource group
if provider, ok := componentMap["providers"]; ok {
if provider != "" {
idObj.Provider = provider
delete(componentMap, "providers")
}

return idObj, nil
Expand Down
69 changes: 69 additions & 0 deletions azurerm/internal/services/compute/parse/hybrid_machine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"fmt"
"strings"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type HybridMachineId struct {
SubscriptionId string
ResourceGroup string
MachineName string
}

func NewHybridMachineID(subscriptionId, resourceGroup, machineName string) HybridMachineId {
return HybridMachineId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
MachineName: machineName,
}
}

func (id HybridMachineId) String() string {
segments := []string{
fmt.Sprintf("Machine Name %q", id.MachineName),
fmt.Sprintf("Resource Group %q", id.ResourceGroup),
}
segmentsStr := strings.Join(segments, " / ")
return fmt.Sprintf("%s: (%s)", "Hybrid Machine", segmentsStr)
}

func (id HybridMachineId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.HybridCompute/machines/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.MachineName)
}

// HybridMachineID parses a HybridMachine ID into an HybridMachineId struct
func HybridMachineID(input string) (*HybridMachineId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

resourceId := HybridMachineId{
SubscriptionId: id.SubscriptionID,
ResourceGroup: id.ResourceGroup,
}

if resourceId.SubscriptionId == "" {
return nil, fmt.Errorf("ID was missing the 'subscriptions' element")
}

if resourceId.ResourceGroup == "" {
return nil, fmt.Errorf("ID was missing the 'resourceGroups' element")
}

if resourceId.MachineName, err = id.PopSegment("machines"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &resourceId, nil
}
112 changes: 112 additions & 0 deletions azurerm/internal/services/compute/parse/hybrid_machine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"testing"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/resourceid"
)

var _ resourceid.Formatter = HybridMachineId{}

func TestHybridMachineIDFormatter(t *testing.T) {
actual := NewHybridMachineID("12345678-1234-9876-4563-123456789012", "resGroup1", "machine1").ID()
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.HybridCompute/machines/machine1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

func TestHybridMachineID(t *testing.T) {
testData := []struct {
Input string
Error bool
Expected *HybridMachineId
}{

{
// empty
Input: "",
Error: true,
},

{
// missing SubscriptionId
Input: "/",
Error: true,
},

{
// missing value for SubscriptionId
Input: "/subscriptions/",
Error: true,
},

{
// missing ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/",
Error: true,
},

{
// missing value for ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/",
Error: true,
},

{
// missing MachineName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.HybridCompute/",
Error: true,
},

{
// missing value for MachineName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.HybridCompute/machines/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.HybridCompute/machines/machine1",
Expected: &HybridMachineId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
MachineName: "machine1",
},
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.HYBRIDCOMPUTE/MACHINES/MACHINE1",
Error: true,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Input)

actual, err := HybridMachineID(v.Input)
if err != nil {
if v.Error {
continue
}

t.Fatalf("Expect a value but got an error: %s", err)
}
if v.Error {
t.Fatal("Expect an error but didn't get one")
}

if actual.SubscriptionId != v.Expected.SubscriptionId {
t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId)
}
if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup)
}
if actual.MachineName != v.Expected.MachineName {
t.Fatalf("Expected %q but got %q for MachineName", v.Expected.MachineName, actual.MachineName)
}
}
}
1 change: 1 addition & 0 deletions azurerm/internal/services/compute/resourceids.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ package compute
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=VirtualMachineScaleSetExtension -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/virtualMachineScaleSets/scaleSet1/extensions/extension1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=SSHPublicKey -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/sshPublicKeys/sshpublickey1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=DiskAccess -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/diskAccesses/diskAccess1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=HybridMachine -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.HybridCompute/machines/machine1
23 changes: 23 additions & 0 deletions azurerm/internal/services/compute/validate/hybrid_machine_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package validate

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/compute/parse"
)

func HybridMachineID(input interface{}, key string) (warnings []string, errors []error) {
v, ok := input.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected %q to be a string", key))
return
}

if _, err := parse.HybridMachineID(v); err != nil {
errors = append(errors, err)
}

return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package validate

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import "testing"

func TestHybridMachineID(t *testing.T) {
cases := []struct {
Input string
Valid bool
}{

{
// empty
Input: "",
Valid: false,
},

{
// missing SubscriptionId
Input: "/",
Valid: false,
},

{
// missing value for SubscriptionId
Input: "/subscriptions/",
Valid: false,
},

{
// missing ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/",
Valid: false,
},

{
// missing value for ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/",
Valid: false,
},

{
// missing MachineName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.HybridCompute/",
Valid: false,
},

{
// missing value for MachineName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.HybridCompute/machines/",
Valid: false,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.HybridCompute/machines/machine1",
Valid: true,
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.HYBRIDCOMPUTE/MACHINES/MACHINE1",
Valid: false,
},
}
for _, tc := range cases {
t.Logf("[DEBUG] Testing Value %s", tc.Input)
_, errors := HybridMachineID(tc.Input, "test")
valid := len(errors) == 0

if tc.Valid != valid {
t.Fatalf("Expected %t but got %t", tc.Valid, valid)
}
}
}
33 changes: 19 additions & 14 deletions azurerm/internal/services/securitycenter/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
)

type Client struct {
ContactsClient *security.ContactsClient
PricingClient *security.PricingsClient
WorkspaceClient *security.WorkspaceSettingsClient
AdvancedThreatProtectionClient *security.AdvancedThreatProtectionClient
AutoProvisioningClient *security.AutoProvisioningSettingsClient
SettingClient *security.SettingsClient
AutomationsClient *security.AutomationsClient
ContactsClient *security.ContactsClient
PricingClient *security.PricingsClient
WorkspaceClient *security.WorkspaceSettingsClient
AdvancedThreatProtectionClient *security.AdvancedThreatProtectionClient
AutoProvisioningClient *security.AutoProvisioningSettingsClient
SettingClient *security.SettingsClient
AutomationsClient *security.AutomationsClient
ServerVulnerabilityAssessmentClient *security.ServerVulnerabilityAssessmentClient
}

func NewClient(o *common.ClientOptions) *Client {
Expand All @@ -39,13 +40,17 @@ func NewClient(o *common.ClientOptions) *Client {
AutomationsClient := security.NewAutomationsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId, ascLocation)
o.ConfigureClient(&AutomationsClient.Client, o.ResourceManagerAuthorizer)

ServerVulnerabilityAssessmentClient := security.NewServerVulnerabilityAssessmentClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId, ascLocation)
o.ConfigureClient(&ServerVulnerabilityAssessmentClient.Client, o.ResourceManagerAuthorizer)

return &Client{
ContactsClient: &ContactsClient,
PricingClient: &PricingClient,
WorkspaceClient: &WorkspaceClient,
AdvancedThreatProtectionClient: &AdvancedThreatProtectionClient,
AutoProvisioningClient: &AutoProvisioningClient,
SettingClient: &SettingClient,
AutomationsClient: &AutomationsClient,
ContactsClient: &ContactsClient,
PricingClient: &PricingClient,
WorkspaceClient: &WorkspaceClient,
AdvancedThreatProtectionClient: &AdvancedThreatProtectionClient,
AutoProvisioningClient: &AutoProvisioningClient,
SettingClient: &SettingClient,
AutomationsClient: &AutomationsClient,
ServerVulnerabilityAssessmentClient: &ServerVulnerabilityAssessmentClient,
}
}
1 change: 1 addition & 0 deletions azurerm/internal/services/securitycenter/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ func (r Registration) SupportedResources() map[string]*schema.Resource {
"azurerm_security_center_workspace": resourceSecurityCenterWorkspace(),
"azurerm_security_center_automation": resourceSecurityCenterAutomation(),
"azurerm_security_center_auto_provisioning": resourceSecurityCenterAutoProvisioning(),
"azurerm_server_vulnerability_assessment": resourceServerVulnerabilityAssessment(),
}
}
Loading