-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add table azure_compute_ssh_key (#560)
- Loading branch information
Showing
3 changed files
with
196 additions
and
0 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,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 | ||
} |
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,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.'; | ||
``` | ||
|
||
### 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'; | ||
``` | ||
|