Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

feat: Support Xray groups and sampling rules #841

Merged
merged 10 commits into from
May 11, 2022
40 changes: 40 additions & 0 deletions client/mocks/xray.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions client/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,4 +670,6 @@ type WafRegionalClient interface {
//go:generate mockgen -package=mocks -destination=./mocks/xray.go . XrayClient
type XrayClient interface {
GetEncryptionConfig(ctx context.Context, params *xray.GetEncryptionConfigInput, optFns ...func(*xray.Options)) (*xray.GetEncryptionConfigOutput, error)
GetSamplingRules(ctx context.Context, params *xray.GetSamplingRulesInput, optFns ...func(*xray.Options)) (*xray.GetSamplingRulesOutput, error)
GetGroups(ctx context.Context, params *xray.GetGroupsInput, optFns ...func(*xray.Options)) (*xray.GetGroupsOutput, error)
}
13 changes: 13 additions & 0 deletions docs/tables/aws_xray_groups.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

# Table: aws_xray_groups
Details for a group.
## Columns
| Name | Type | Description |
| ------------- | ------------- | ----- |
|account_id|text|The AWS Account ID of the resource.|
|region|text|The AWS Region of the resource.|
|filter_expression|text|The filter expression defining the parameters to include traces.|
|arn|text|The ARN of the group generated based on the GroupName.|
|group_name|text|The unique case-sensitive name of the group.|
|insights_enabled|boolean|Set the InsightsEnabled value to true to enable insights or false to disable insights.|
|notifications_enabled|boolean|Set the NotificationsEnabled value to true to enable insights notifications. Notifications can only be enabled on a group with InsightsEnabled set to true.|
23 changes: 23 additions & 0 deletions docs/tables/aws_xray_sampling_rules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

# Table: aws_xray_sampling_rules
A SamplingRule.
## Columns
| Name | Type | Description |
| ------------- | ------------- | ----- |
|account_id|text|The AWS Account ID of the resource.|
|region|text|The AWS Region of the resource.|
|created_at|timestamp without time zone|When the rule was created.|
|modified_at|timestamp without time zone|When the rule was last modified.|
|fixed_rate|float|The percentage of matching requests to instrument, after the reservoir is exhausted.|
|http_method|text|Matches the HTTP method of a request.|
|host|text|Matches the hostname from a request URL.|
|priority|integer|The priority of the sampling rule.|
|reservoir_size|integer|A fixed number of matching requests to instrument per second, prior to applying the fixed rate|
|resource_arn|text|Matches the ARN of the Amazon Web Services resource on which the service runs.|
|service_name|text|Matches the name that the service uses to identify itself in segments.|
|service_type|text|Matches the origin that the service uses to identify its type in segments.|
|url_path|text|Matches the path from a request URL.|
|version|integer|The version of the sampling rule format (1).|
|attributes|jsonb|Matches attributes derived from the request.|
|arn|text|The ARN of the sampling rule|
|rule_name|text|The name of the sampling rule|
8 changes: 8 additions & 0 deletions resources/provider/migrations/postgres/30_v0.11.4.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Autogenerated by migration tool on 2022-05-09 08:55:35
-- CHANGEME: Verify or edit this file before proceeding

-- Resource: xray.groups
DROP TABLE IF EXISTS aws_xray_groups;

-- Resource: xray.sampling_rules
DROP TABLE IF EXISTS aws_xray_sampling_rules;
42 changes: 42 additions & 0 deletions resources/provider/migrations/postgres/30_v0.11.4.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
-- Autogenerated by migration tool on 2022-05-09 08:55:35
-- CHANGEME: Verify or edit this file before proceeding

-- Resource: xray.groups
CREATE TABLE IF NOT EXISTS "aws_xray_groups" (
"cq_id" uuid NOT NULL,
"cq_meta" jsonb,
"account_id" text,
"region" text,
"filter_expression" text,
"arn" text,
"group_name" text,
"insights_enabled" boolean,
"notifications_enabled" boolean,
CONSTRAINT aws_xray_groups_pk PRIMARY KEY(arn),
UNIQUE(cq_id)
);

-- Resource: xray.sampling_rules
CREATE TABLE IF NOT EXISTS "aws_xray_sampling_rules" (
"cq_id" uuid NOT NULL,
"cq_meta" jsonb,
"account_id" text,
"region" text,
"created_at" timestamp without time zone,
"modified_at" timestamp without time zone,
"fixed_rate" float,
"http_method" text,
"host" text,
"priority" integer,
"reservoir_size" integer,
"resource_arn" text,
"service_name" text,
"service_type" text,
"url_path" text,
"version" integer,
"attributes" jsonb,
"arn" text,
"rule_name" text,
CONSTRAINT aws_xray_sampling_rules_pk PRIMARY KEY(arn),
UNIQUE(cq_id)
);
8 changes: 8 additions & 0 deletions resources/provider/migrations/timescale/30_v0.11.4.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Autogenerated by migration tool on 2022-05-09 08:54:40
-- CHANGEME: Verify or edit this file before proceeding

-- Resource: xray.groups
DROP TABLE IF EXISTS aws_xray_groups;

-- Resource: xray.sampling_rules
DROP TABLE IF EXISTS aws_xray_sampling_rules;
46 changes: 46 additions & 0 deletions resources/provider/migrations/timescale/30_v0.11.4.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
-- Autogenerated by migration tool on 2022-05-09 08:54:40
-- CHANGEME: Verify or edit this file before proceeding

-- Resource: xray.groups
CREATE TABLE IF NOT EXISTS "aws_xray_groups" (
"cq_id" uuid NOT NULL,
"cq_meta" jsonb,
"cq_fetch_date" timestamp without time zone NOT NULL,
"account_id" text,
"region" text,
"filter_expression" text,
"arn" text,
"group_name" text,
"insights_enabled" boolean,
"notifications_enabled" boolean,
CONSTRAINT aws_xray_groups_pk PRIMARY KEY(cq_fetch_date,arn),
UNIQUE(cq_fetch_date,cq_id)
);
SELECT setup_tsdb_parent('aws_xray_groups');

-- Resource: xray.sampling_rules
CREATE TABLE IF NOT EXISTS "aws_xray_sampling_rules" (
"cq_id" uuid NOT NULL,
"cq_meta" jsonb,
"cq_fetch_date" timestamp without time zone NOT NULL,
"account_id" text,
"region" text,
"created_at" timestamp without time zone,
"modified_at" timestamp without time zone,
"fixed_rate" float,
"http_method" text,
"host" text,
"priority" integer,
"reservoir_size" integer,
"resource_arn" text,
"service_name" text,
"service_type" text,
"url_path" text,
"version" integer,
"attributes" jsonb,
"arn" text,
"rule_name" text,
CONSTRAINT aws_xray_sampling_rules_pk PRIMARY KEY(cq_fetch_date,arn),
UNIQUE(cq_fetch_date,cq_id)
);
SELECT setup_tsdb_parent('aws_xray_sampling_rules');
2 changes: 2 additions & 0 deletions resources/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ func Provider() *provider.Provider {
"workspaces.workspaces": workspaces.Workspaces(),
"workspaces.directories": workspaces.Directories(),
"xray.encryption_config": xray.EncryptionConfigs(),
"xray.groups": xray.Groups(),
"xray.sampling_rules": xray.SamplingRules(),
//"iot.security_profiles": iot.IotSecurityProfiles(), //TODO disabled because of api error NotFoundException: No method found matching route security-profiles for http method GET.
},
Config: func() provider.Config {
Expand Down
84 changes: 84 additions & 0 deletions resources/services/xray/gen.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,88 @@ resource "aws" "xray" "encryption_config" {
primary_keys = ["account_id", "region"]
}

}

resource "aws" "xray" "sampling_rules" {
path = "github.com/aws/aws-sdk-go-v2/service/xray/types.SamplingRuleRecord"
ignoreError "IgnoreAccessDenied" {
path = "github.com/cloudquery/cq-provider-aws/client.IgnoreAccessDeniedServiceDisabled"
}
deleteFilter "AccountRegionFilter" {
path = "github.com/cloudquery/cq-provider-aws/client.DeleteAccountRegionFilter"
}
multiplex "AwsAccountRegion" {
path = "github.com/cloudquery/cq-provider-aws/client.ServiceAccountRegionMultiplexer"
params = ["xray"]
}

userDefinedColumn "account_id" {
description = "The AWS Account ID of the resource."
type = "string"
resolver "resolveAWSAccount" {
path = "github.com/cloudquery/cq-provider-aws/client.ResolveAWSAccount"
}
}
userDefinedColumn "region" {
type = "string"
description = "The AWS Region of the resource."
resolver "resolveAWSRegion" {
path = "github.com/cloudquery/cq-provider-aws/client.ResolveAWSRegion"
}
}

column "sampling_rule" {
skip_prefix = true
}

column "rule_arn" {
rename = "arn"
}

options {
primary_keys = ["arn"]
}

}

resource "aws" "xray" "groups" {
path = "github.com/aws/aws-sdk-go-v2/service/xray/types.GroupSummary"
ignoreError "IgnoreAccessDenied" {
path = "github.com/cloudquery/cq-provider-aws/client.IgnoreAccessDeniedServiceDisabled"
}
deleteFilter "AccountRegionFilter" {
path = "github.com/cloudquery/cq-provider-aws/client.DeleteAccountRegionFilter"
}
multiplex "AwsAccountRegion" {
path = "github.com/cloudquery/cq-provider-aws/client.ServiceAccountRegionMultiplexer"
params = ["xray"]
}

userDefinedColumn "account_id" {
description = "The AWS Account ID of the resource."
type = "string"
resolver "resolveAWSAccount" {
path = "github.com/cloudquery/cq-provider-aws/client.ResolveAWSAccount"
}
}
userDefinedColumn "region" {
type = "string"
description = "The AWS Region of the resource."
resolver "resolveAWSRegion" {
path = "github.com/cloudquery/cq-provider-aws/client.ResolveAWSRegion"
}
}

column "insights_configuration" {
skip_prefix = true
}

column "group_arn" {
rename = "arn"
}

options {
primary_keys = ["arn"]
}

}
91 changes: 91 additions & 0 deletions resources/services/xray/groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package xray

import (
"context"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/xray"
"github.com/cloudquery/cq-provider-aws/client"
"github.com/cloudquery/cq-provider-sdk/provider/diag"
"github.com/cloudquery/cq-provider-sdk/provider/schema"
)

//go:generate cq-gen --resource groups --config gen.hcl --output .
func Groups() *schema.Table {
return &schema.Table{
Name: "aws_xray_groups",
Description: "Details for a group.",
Resolver: fetchXrayGroups,
Multiplex: client.ServiceAccountRegionMultiplexer("xray"),
IgnoreError: client.IgnoreAccessDeniedServiceDisabled,
DeleteFilter: client.DeleteAccountRegionFilter,
Options: schema.TableCreationOptions{PrimaryKeys: []string{"arn"}},
Columns: []schema.Column{
{
Name: "account_id",
Description: "The AWS Account ID of the resource.",
Type: schema.TypeString,
Resolver: client.ResolveAWSAccount,
},
{
Name: "region",
Description: "The AWS Region of the resource.",
Type: schema.TypeString,
Resolver: client.ResolveAWSRegion,
},
{
Name: "filter_expression",
Description: "The filter expression defining the parameters to include traces.",
Type: schema.TypeString,
},
{
Name: "arn",
Description: "The ARN of the group generated based on the GroupName.",
Type: schema.TypeString,
Resolver: schema.PathResolver("GroupARN"),
},
{
Name: "group_name",
Description: "The unique case-sensitive name of the group.",
Type: schema.TypeString,
},
{
Name: "insights_enabled",
Description: "Set the InsightsEnabled value to true to enable insights or false to disable insights.",
Type: schema.TypeBool,
Resolver: schema.PathResolver("InsightsConfiguration.InsightsEnabled"),
},
{
Name: "notifications_enabled",
Description: "Set the NotificationsEnabled value to true to enable insights notifications. Notifications can only be enabled on a group with InsightsEnabled set to true.",
Type: schema.TypeBool,
Resolver: schema.PathResolver("InsightsConfiguration.NotificationsEnabled"),
},
},
}
}

// ====================================================================================================================
// Table Resolver Functions
// ====================================================================================================================

func fetchXrayGroups(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
c := meta.(*client.Client)
svc := c.Services().Xray
input := xray.GetGroupsInput{}
for {
output, err := svc.GetGroups(ctx, &input, func(o *xray.Options) {
o.Region = c.Region
})
if err != nil {
return diag.WrapError(err)
}
res <- output.Groups

if aws.ToString(output.NextToken) == "" {
break
}
input.NextToken = output.NextToken
}
return nil
}
Loading