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

Add table azure_security_center_contact. Closes #119 #121

Merged
merged 11 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
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
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"id": "{{ output.resource_id.value }}",
"name": "{{ output.resource_name.value }}",
"type": "Microsoft.Security/securityContacts"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select name, id, type
from azure.azure_security_center_contact
where name = '{{ output.resource_name.value }}';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"id": "{{ output.resource_id.value }}",
"name": "{{ output.resource_name.value }}"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select id, name
from azure.azure_security_center_contact
where id = '{{ output.resource_id.value }}'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select name, akas, title
from azure.azure_security_center_contact
where name = 'dummy-{{ output.resource_name.value }}';
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"akas": [
"{{ output.resource_aka.value }}",
"{{ output.resource_aka_lower.value }}"
],
"name": "{{ output.resource_name.value }}",
"title": "{{ output.resource_name.value }}"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select name, akas, title
from azure.azure_security_center_contact
where name = '{{ output.resource_name.value }}';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
61 changes: 61 additions & 0 deletions azure-test/tests/azure_security_center_contact/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

variable "resource_name" {
type = string
default = "turbot-test-20200125-create-update"
description = "Name of the resource used throughout the test."
}

variable "azure_environment" {
type = string
default = "public"
description = "Azure environment used for the test."
}

variable "azure_subscription" {
type = string
default = "3510ae4d-530b-497d-8f30-53b9616fc6c1"
bigdatasourav marked this conversation as resolved.
Show resolved Hide resolved
description = "Azure subscription used for the test."
}

provider "azurerm" {
# Cannot be passed as a variable
version = "=2.43.0"
features {}
environment = var.azure_environment
subscription_id = var.azure_subscription
}

data "azurerm_client_config" "current" {}

data "null_data_source" "resource" {
inputs = {
scope = "azure:///subscriptions/${data.azurerm_client_config.current.subscription_id}"
}
}

resource "azurerm_security_center_contact" "named_test_resource" {
email = "[email protected]"
phone = "+1-555-555-5555"
alert_notifications = true
alerts_to_admins = true
}
bigdatasourav marked this conversation as resolved.
Show resolved Hide resolved

output "resource_aka" {
value = "azure://${azurerm_security_center_contact.named_test_resource.id}"
}

output "resource_aka_lower" {
value = "azure://${lower(azurerm_security_center_contact.named_test_resource.id)}"
}

output "resource_id" {
value = azurerm_security_center_contact.named_test_resource.id
}

output "resource_name" {
value = element(split("/", azurerm_security_center_contact.named_test_resource.id), 6)
}

output "subscription_id" {
value = var.azure_subscription
}
1 change: 1 addition & 0 deletions azure/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func Plugin(ctx context.Context) *plugin.Plugin {
"azure_role_assignment": tableAzureIamRoleAssignment(ctx),
"azure_role_definition": tableAzureIamRoleDefinition(ctx),
"azure_route_table": tableAzureRouteTable(ctx),
"azure_security_center_contact": tableAzureSecurityCenterContact(ctx),
"azure_sql_database": tableAzureSqlDatabase(ctx),
"azure_sql_server": tableAzureSQLServer(ctx),
"azure_storage_account": tableAzureStorageAccount(ctx),
Expand Down
135 changes: 135 additions & 0 deletions azure/table_azure_security_center_contact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package azure

import (
"context"

"github.com/Azure/azure-sdk-for-go/services/preview/security/mgmt/v1.0/security"
"github.com/turbot/steampipe-plugin-sdk/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/plugin/transform"

"github.com/turbot/steampipe-plugin-sdk/plugin"
)

//// TABLE DEFINITION

func tableAzureSecurityCenterContact(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "azure_security_center_contact",
Description: "Azure Security Center Contact",
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("name"),
Hydrate: getSecurityCenterContact,
},
List: &plugin.ListConfig{
Hydrate: listSecurityCenterContacts,
},
Columns: []*plugin.Column{
{
Name: "id",
Type: proto.ColumnType_STRING,
Description: "The resource id.",
Transform: transform.FromGo(),
},
{
Name: "name",
Description: "The resource name.",
Type: proto.ColumnType_STRING,
},
{
Name: "type",
Description: "The resource type.",
Type: proto.ColumnType_STRING,
},
{
Name: "email",
Description: "The email of this security contact.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ContactProperties.Email"),
bigdatasourav marked this conversation as resolved.
Show resolved Hide resolved
},
{
Name: "phone",
Description: "The phone number of this security contact.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ContactProperties.Phone"),
},
{
Name: "alert_notifications",
Description: "Whether to send security alerts notifications to the security contact.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ContactProperties.AlertNotifications"),
},
{
Name: "alerts_to_admins",
Description: "Whether to send security alerts notifications to subscription admins.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ContactProperties.AlertsToAdmins"),
},

// Steampipe standard columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
{
Name: "akas",
Description: ColumnDescriptionAkas,
Type: proto.ColumnType_JSON,
Transform: transform.FromField("ID").Transform(idToAkas),
},

// Azure standard columns
{
Name: "subscription_id",
Description: ColumnDescriptionSubscription,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ID").Transform(idToSubscriptionID),
},
},
}
}

//// LIST FUNCTION

func listSecurityCenterContacts(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
return nil, err
}

subscriptionID := session.SubscriptionID
contactClient := security.NewContactsClient(subscriptionID, "")
contactClient.Authorizer = session.Authorizer

contactList, err := contactClient.List(ctx)
if err != nil {
return err, nil
}

for _, contact := range contactList.Values() {
d.StreamListItem(ctx, contact)
}
return nil, nil
}

//// HYDRATE FUNCTIONS

func getSecurityCenterContact(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
return nil, err
}
name := d.KeyColumnQuals["name"].GetStringValue()

subscriptionID := session.SubscriptionID
contactClient := security.NewContactsClient(subscriptionID, "")
contactClient.Authorizer = session.Authorizer

contact, err := contactClient.Get(ctx, name)
bigdatasourav marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err, nil
}

return contact, nil
}
5 changes: 4 additions & 1 deletion azure/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import (
"github.com/turbot/steampipe-plugin-sdk/plugin/transform"
)

//// TRANSFORM FUNCTION ////
//// TRANSFORM FUNCTIONS

func idToSubscriptionID(ctx context.Context, d *transform.TransformData) (interface{}, error) {
id := types.SafeString(d.Value)
if len(id) == 0 {
return nil, nil
}
subscriptionid := strings.Split(id, "/")[2]
return subscriptionid, nil
}
Expand Down
31 changes: 31 additions & 0 deletions docs/tables/azure_security_center_contact.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Table: azure_security_center_contact

Azure security center contact configurations for the subscription.

## Examples

### Basic info

```sql
select
id,
email,
alert_notifications,
alerts_to_admins
from
azure_security_center_contact;
```

### Ensure security contact email configured for the subscription

```sql
select
id,
email,
alert_notifications,
alerts_to_admins
from
azure_security_center_contact
where
email != '';
```