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_compute_ssh_key #560

Merged
merged 2 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions azure/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func Plugin(ctx context.Context) *plugin.Plugin {
"azure_compute_image": tableAzureComputeImage(ctx),
"azure_compute_resource_sku": tableAzureResourceSku(ctx),
"azure_compute_snapshot": tableAzureComputeSnapshot(ctx),
"azure_compute_ssh_key": tableAzureComputeSshKey(ctx),
"azure_compute_virtual_machine": tableAzureComputeVirtualMachine(ctx),
"azure_compute_virtual_machine_metric_cpu_utilization": tableAzureComputeVirtualMachineMetricCpuUtilization(ctx),
"azure_compute_virtual_machine_metric_cpu_utilization_daily": tableAzureComputeVirtualMachineMetricCpuUtilizationDaily(ctx),
Expand Down
165 changes: 165 additions & 0 deletions azure/table_azure_compute_ssh_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package azure

import (
"context"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute"
"github.com/turbot/steampipe-plugin-sdk/v4/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v4/plugin"
"github.com/turbot/steampipe-plugin-sdk/v4/plugin/transform"
)

//// TABLE DEFINITION ////

func tableAzureComputeSshKey(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "azure_compute_ssh_key",
Description: "Azure Compute SSH Key",
Get: &plugin.GetConfig{
KeyColumns: plugin.AllColumns([]string{"name", "resource_group"}),
Hydrate: getAzureComputeSshKey,
IgnoreConfig: &plugin.IgnoreConfig{
ShouldIgnoreErrorFunc: isNotFoundError([]string{"ResourceGroupNotFound", "ResourceNotFound", "404"}),
},
},
List: &plugin.ListConfig{
Hydrate: listAzureComputeSshKeys,
},
Columns: azureColumns([]*plugin.Column{
{
Name: "id",
Description: "The unique ID identifying the resource in subscription.",
Type: proto.ColumnType_STRING,
Transform: transform.FromGo(),
},
{
Name: "name",
Description: "Name of the SSH key.",
Type: proto.ColumnType_STRING,
},
{
Name: "type",
Description: "The type of the resource in Azure.",
Type: proto.ColumnType_STRING,
},
{
Name: "public_key",
Description: "SSH public key.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("SSHPublicKeyResourceProperties.PublicKey"),
},

// Azure standard columns
{
Name: "region",
Description: ColumnDescriptionRegion,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Location").Transform(toLower),
},
{
Name: "tags",
Description: ColumnDescriptionTags,
Type: proto.ColumnType_JSON,
},
{
Name: "resource_group",
Description: ColumnDescriptionResourceGroup,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ID").Transform(extractResourceGroupFromID),
},

// 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),
},
}),
}
}

//// LIST FUNCTION ////

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

subscriptionID := session.SubscriptionID
client := compute.NewSSHPublicKeysClientWithBaseURI(session.ResourceManagerEndpoint, subscriptionID)
client.Authorizer = session.Authorizer
result, err := client.ListBySubscription(ctx)
if err != nil {
plugin.Logger(ctx).Error("azure_compute_ssh_key.listAzureComputeSshKeys", "query_error", err)
return nil, err
}

for _, key := range result.Values() {
d.StreamListItem(ctx, key)
// Check if context has been cancelled or if the limit has been hit (if specified)
// if there is a limit, it will return the number of rows required to reach this limit
if d.QueryStatus.RowsRemaining(ctx) == 0 {
return nil, nil
}
}

for result.NotDone() {
err = result.NextWithContext(ctx)
if err != nil {
return nil, err
}

for _, key := range result.Values() {
d.StreamListItem(ctx, key)
// Check if context has been cancelled or if the limit has been hit (if specified)
// if there is a limit, it will return the number of rows required to reach this limit
if d.QueryStatus.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
}

return nil, nil
}

//// HYDRATE FUNCTION ////

func getAzureComputeSshKey(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("getAzureComputeSshKey")

name := d.KeyColumnQuals["name"].GetStringValue()
resourceGroup := d.KeyColumnQuals["resource_group"].GetStringValue()

session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
plugin.Logger(ctx).Error("azure_compute_ssh_key.getAzureComputeSshKey", "client_error", err)
return nil, err
}
subscriptionID := session.SubscriptionID
client := compute.NewSSHPublicKeysClientWithBaseURI(session.ResourceManagerEndpoint, subscriptionID)
client.Authorizer = session.Authorizer

op, err := client.Get(ctx, resourceGroup, name)
if err != nil {
plugin.Logger(ctx).Error("azure_compute_ssh_key.getAzureComputeSshKey", "query_error", err)
return nil, err
}

// In some cases resource does not give any notFound error
// instead of notFound error, it returns empty data
if op.ID != nil {
return op, nil
}

return nil, nil
}
30 changes: 30 additions & 0 deletions docs/tables/table_azure_compute_ssh_key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Table: table_azure_compute_ssh_key

Azure SSH public key used by VM.

## Examples

### Retrieve SSH public key by name

```sql
select
name,
publicKey
from
table_azure_compute_ssh_key
where
name = 'key-name.';
srgg marked this conversation as resolved.
Show resolved Hide resolved
```

### List compute virtual machines using SSH public key

```sql
select
m.name as machine_name,
k.name as ssh_key_name
from
azure_compute_virtual_machine as m,
jsonb_array_elements(linux_configuration_ssh_public_keys) as s
left join azure_compute_ssh_key as k on k.public_key = s ->> 'keyData';
```