Skip to content

Commit

Permalink
Add tables scaleway_iam_api_key and scaleway_iam_user (#25) (#30)
Browse files Browse the repository at this point in the history
Signed-off-by: jplanckeel <[email protected]>
Co-authored-by: Jeremy PLANCKEEL <[email protected]>
Co-authored-by: jplanckeel <[email protected]>
Co-authored-by: madhushreeray@30 <[email protected]>
  • Loading branch information
4 people authored Oct 23, 2023
1 parent 7be1d8b commit 3b09a61
Show file tree
Hide file tree
Showing 9 changed files with 499 additions and 5 deletions.
4 changes: 4 additions & 0 deletions config/scaleway.spc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ connection "scaleway" {
# variables using the `SCW_ACCESS_KEY` and `SCW_SECRET_KEY` arguments.
access_key = "YOUR_ACCESS_KEY"
secret_key = "YOUR_SECRET_ACCESS_KEY"

# Your organization ID is the identifier of your account inside Scaleway infrastructure.
# This is only required while querying the scaleway_iam_api_key and scaleway_iam_user tables.
# organization_id = "YOUR_ORGANIZATION_ID"

# You may connect to one or more regions. If `regions` is not specified,
# Steampipe will use a single default region using the `SCW_DEFAULT_REGION`
Expand Down
8 changes: 6 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ connection "scaleway" {
# Set the static credential with the `access_key` and `secret_key` arguments.
# Alternatively, if no creds passed in config, you may set the environment
# variables using the `SCW_ACCESS_KEY` and `SCW_SECRET_KEY` arguments.
# access_key = "SCWKMH185ZG5THRH7WVX"
# secret_key = "ee3b5cb8-2c81-887c-a772-17d46dd34vc7"
access_key = "SCWKMH185ZG5THRH7WVX"
secret_key = "ee3b5cb8-2c81-887c-a772-17d46dd34vc7"
# Your organization ID is the identifier of your account inside Scaleway infrastructure.
# This is only required while querying the scaleway_iam_api_key and scaleway_iam_user tables.
# organization_id = "14czbd62-29fe-46a6-967f-5433adcb2fc5"
# You may connect to one or more regions. If `regions` is not specified,
# Steampipe will use a single default region using the `SCW_DEFAULT_REGION`
Expand Down
36 changes: 36 additions & 0 deletions docs/tables/scaleway_iam_api_key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Table: scaleway_iam_api_key

API keys allow you to securely connect to scaleway console in your organization.

This table requires the `organization_id` config argument to be set.

## Examples

### Basic info

```sql
select
access_key,
created_at,
user_id,
expires_at,
default_project_id
from
scaleway_iam_api_key
```

### List API keys older than 90 days

```sql
select
access_key,
created_at,
user_id,
expires_at,
default_project_id,
extract(day from current_timestamp - created_at) as age
from
scaleway_iam_api_key
where
extract(day from current_timestamp - created_at) > 90;
```
59 changes: 59 additions & 0 deletions docs/tables/scaleway_iam_user.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Table: scaleway_iam_user

Users allow you to connect to scaleway console in your organization.

This table requires the `organization_id` config argument to be set.

## Examples

### Basic info

```sql
select
email,
created_at,
last_login_at,
id,
status,
two_factor_enabled
from
scaleway_iam_user
```

### List all the users for whom MFA is not enabled

```sql
select
email,
id,
two_factor_enabled
from
scaleway_iam_user
where
not two_factor_enabled;
```

### List all the users not actived

```sql
select
email,
id,
status
from
scaleway_iam_user
where
status = 'unknown_status';
```

### List all the users never connected

```sql
select
email,
id,
last_login_at
from
scaleway_iam_user
where
last_login_at is null;
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,8 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.12 h1:Aaz4T7dZp7cB2cv7D/tGtRdSMh48sRaDYr7Jh0HV4qQ=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.12/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.14 h1:yFl3jyaSVLNYXlnNYM5z2pagEk1dYQhfr1p20T1NyKY=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.14/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sethvargo/go-retry v0.2.4 h1:T+jHEQy/zKJf5s95UkguisicE0zuF9y7+/vgz08Ocec=
Expand Down
10 changes: 7 additions & 3 deletions scaleway/connection_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

type scalewayConfig struct {
AccessKey *string `cty:"access_key"`
SecretKey *string `cty:"secret_key"`
Regions []string `cty:"regions"`
AccessKey *string `cty:"access_key"`
SecretKey *string `cty:"secret_key"`
OrganizationID *string `cty:"organization_id"`
Regions []string `cty:"regions"`
}

var ConfigSchema = map[string]*schema.Attribute{
Expand All @@ -18,6 +19,9 @@ var ConfigSchema = map[string]*schema.Attribute{
"secret_key": {
Type: schema.TypeString,
},
"organization_id": {
Type: schema.TypeString,
},
"regions": {
Type: schema.TypeList,
Elem: &schema.Attribute{Type: schema.TypeString},
Expand Down
2 changes: 2 additions & 0 deletions scaleway/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func Plugin(ctx context.Context) *plugin.Plugin {
TableMap: map[string]*plugin.Table{
"scaleway_account_ssh_key": tableScalewayAccountSSHKey(ctx),
"scaleway_baremetal_server": tableScalewayBaremetalServer(ctx),
"scaleway_iam_api_key": tableScalewayIamAPIKey(ctx),
"scaleway_iam_user": tableScalewayIamUser(ctx),
"scaleway_instance_image": tableScalewayInstanceImage(ctx),
"scaleway_instance_ip": tableScalewayInstanceIP(ctx),
"scaleway_instance_security_group": tableScalewayInstanceSecurityGroup(ctx),
Expand Down
194 changes: 194 additions & 0 deletions scaleway/table_scaleway_iam_api_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package scaleway

import (
"context"

iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1"

"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)

//// TABLE DEFINITION

func tableScalewayIamAPIKey(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "scaleway_iam_api_key",
Description: "API keys allow you to securely connect to scaleway console in your organization.",
List: &plugin.ListConfig{
Hydrate: listIamAPIKeys,
KeyColumns: []*plugin.KeyColumn{
{
Name: "access_key",
Require: plugin.Optional,
},
},
},
Get: &plugin.GetConfig{
Hydrate: getIamAPIKey,
KeyColumns: plugin.SingleColumn("access_key"),
},
Columns: []*plugin.Column{
{
Name: "access_key",
Description: "The access key of API key.",
Type: proto.ColumnType_STRING,
},
{
Name: "secret_key",
Description: "The secret key of API Key.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("SecretKey").Transform(transform.ToString),
},
{
Name: "application_id",
Description: "ID of application bearer.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ApplicationID").Transform(transform.ToString),
},
{
Name: "created_at",
Description: "Creation date and time of API key.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "user_id",
Description: "ID of user bearer.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("UserID").Transform(transform.ToString),
},
{
Name: "updated_at",
Description: "Last update date and time of API key.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "expires_at",
Description: "The expiration date and time of API key.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "default_project_id",
Description: "The default project ID specified for this API key.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("DefaultProjectID").Transform(transform.ToString),
},
{
Name: "editable",
Description: "Whether or not the API key is editable.",
Type: proto.ColumnType_BOOL,
},
{
Name: "creation_ip",
Description: "The IP Address of the device which created the API key.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("CreationIP").Transform(transform.ToString),
},
{
Name: "description",
Description: "Description of API key.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Description").Transform(transform.ToString),
},
},
}
}

//// LIST FUNCTION

func listIamAPIKeys(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
// Create client
client, err := getSessionConfig(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("scaleway_iam_api_key.listIamAPIKeys", "connection_error", err)
return nil, err
}

// Create SDK objects for Scaleway IAM product
iamApi := iam.NewAPI(client)

// Get organisationID from config to request IAM API
organisationId := GetConfig(d.Connection).OrganizationID

req := &iam.ListAPIKeysRequest{
Page: scw.Int32Ptr(1),
OrganizationID: organisationId,
}

// Retrieve the list of servers
maxResult := int64(100)

// Reduce the basic request limit down if the user has only requested a small number of rows
limit := d.QueryContext.Limit
if d.QueryContext.Limit != nil {
if *limit < maxResult {
maxResult = *limit
}
}
req.PageSize = scw.Uint32Ptr(uint32(maxResult))

var count int

for {
resp, err := iamApi.ListAPIKeys(req)
if err != nil {
plugin.Logger(ctx).Error("scaleway_iam_api_key.listIamAPIKeys", "query_error", err)
}

for _, key := range resp.APIKeys {
d.StreamListItem(ctx, key)

// Increase the resource count by 1
count++

// Context can be cancelled due to manual cancellation or the limit has been hit
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}

if resp.TotalCount == uint32(count) {
break
}
req.Page = scw.Int32Ptr(*req.Page + 1)

}

return nil, nil
}

//// HYDRATE FUNCTIONS

func getIamAPIKey(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
// Create client
client, err := getSessionConfig(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("scaleway_iam_api_key.getIamAPIKey", "connection_error", err)
return nil, err
}

// Create SDK objects for Scaleway IAM product
iamApi := iam.NewAPI(client)

accessKey := d.EqualsQuals["access_key"].GetStringValue()

// No inputs
if accessKey == "" {
return nil, nil
}

data, err := iamApi.GetAPIKey(&iam.GetAPIKeyRequest{
AccessKey: accessKey,
})
if err != nil {
plugin.Logger(ctx).Error("scaleway_iam_api_key.getIamAPIKey", "query_error", err)
if is404Error(err) {
return nil, nil
}
return nil, err
}

return data, nil
}
Loading

0 comments on commit 3b09a61

Please sign in to comment.