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

Commit

Permalink
Enhance config configuratiguration recorder with status (#301)
Browse files Browse the repository at this point in the history
* Enhance config configuratiguration recorder with status

Co-authored-by: roneli <38083777+roneli@users.noreply.github.com>
  • Loading branch information
spangenberg and roneli authored Nov 30, 2021
1 parent 270a724 commit 54288e1
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 17 deletions.
3 changes: 1 addition & 2 deletions client/mocks/builders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ import (
route53Types "github.com/aws/aws-sdk-go-v2/service/route53/types"
"github.com/aws/aws-sdk-go-v2/service/sns"
snsTypes "github.com/aws/aws-sdk-go-v2/service/sns/types"
"github.com/golang/mock/gomock"

"github.com/cloudquery/faker/v3"
"github.com/golang/mock/gomock"

"github.com/cloudquery/cq-provider-aws/client"
"github.com/cloudquery/cq-provider-aws/client/mocks"
Expand Down
20 changes: 20 additions & 0 deletions client/mocks/mock_configservice.go

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

7 changes: 4 additions & 3 deletions client/mocks/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"os"
"testing"

"github.com/cloudquery/cq-provider-sdk/logging"
"github.com/cloudquery/cq-provider-sdk/provider/schema"
providertest "github.com/cloudquery/cq-provider-sdk/provider/testing"
"github.com/cloudquery/faker/v3"
"github.com/golang/mock/gomock"
"github.com/hashicorp/go-hclog"
"github.com/jackc/pgx/v4"

"github.com/cloudquery/cq-provider-sdk/logging"
"github.com/cloudquery/cq-provider-sdk/provider/schema"
providertest "github.com/cloudquery/cq-provider-sdk/provider/testing"

"github.com/cloudquery/cq-provider-aws/client"
"github.com/cloudquery/cq-provider-aws/resources"
)
Expand Down
1 change: 1 addition & 0 deletions client/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ type CognitoUserPoolsClient interface {
//go:generate mockgen -package=mocks -destination=./mocks/mock_configservice.go . ConfigServiceClient
type ConfigServiceClient interface {
DescribeConfigurationRecorders(ctx context.Context, params *configservice.DescribeConfigurationRecordersInput, optFns ...func(*configservice.Options)) (*configservice.DescribeConfigurationRecordersOutput, error)
DescribeConfigurationRecorderStatus(ctx context.Context, params *configservice.DescribeConfigurationRecorderStatusInput, optFns ...func(*configservice.Options)) (*configservice.DescribeConfigurationRecorderStatusOutput, error)
configservice.DescribeConformancePacksAPIClient
}

Expand Down
7 changes: 7 additions & 0 deletions docs/tables/aws_config_configuration_recorders.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ An object that represents the recording of configuration changes of an AWS resou
|recording_group_include_global_resource_types|boolean|Specifies whether AWS Config includes all supported types of global resources (for example, IAM resources) with the resources that it records.|
|recording_group_resource_types|text[]|A comma-separated list that specifies the types of AWS resources for which AWS Config records configuration changes (for example, AWS::EC2::Instance or AWS::CloudTrail::Trail).|
|role_arn|text|Amazon Resource Name (ARN) of the IAM role used to describe the AWS resources associated with the account.|
|status_last_error_code|text|The error code indicating that the recording failed.|
|status_last_error_message|text|The message indicating that the recording failed due to an error.|
|status_last_start_time|timestamp without time zone|The time the recorder was last started.|
|status_last_status|text|The last (previous) status of the recorder.|
|status_last_status_change_time|timestamp without time zone|The time when the status was last changed.|
|status_last_stop_time|timestamp without time zone|The time the recorder was last stopped.|
|status_recording|boolean|Specifies whether or not the recorder is currently recording.|
86 changes: 82 additions & 4 deletions resources/config_configuration_recorders.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package resources
import (
"context"
"fmt"
"time"

"github.com/aws/aws-sdk-go-v2/service/configservice"
"github.com/aws/aws-sdk-go-v2/service/configservice/types"

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

"github.com/cloudquery/cq-provider-aws/client"
)

func ConfigConfigurationRecorders() *schema.Table {
Expand Down Expand Up @@ -68,6 +70,41 @@ func ConfigConfigurationRecorders() *schema.Table {
Type: schema.TypeString,
Resolver: schema.PathResolver("RoleARN"),
},
{
Name: "status_last_error_code",
Description: "The error code indicating that the recording failed.",
Type: schema.TypeString,
},
{
Name: "status_last_error_message",
Description: "The message indicating that the recording failed due to an error.",
Type: schema.TypeString,
},
{
Name: "status_last_start_time",
Description: "The time the recorder was last started.",
Type: schema.TypeTimestamp,
},
{
Name: "status_last_status",
Description: "The last (previous) status of the recorder.",
Type: schema.TypeString,
},
{
Name: "status_last_status_change_time",
Description: "The time when the status was last changed.",
Type: schema.TypeTimestamp,
},
{
Name: "status_last_stop_time",
Description: "The time the recorder was last stopped.",
Type: schema.TypeTimestamp,
},
{
Name: "status_recording",
Description: "Specifies whether or not the recorder is currently recording.",
Type: schema.TypeBool,
},
},
}
}
Expand All @@ -83,16 +120,57 @@ func fetchConfigConfigurationRecorders(ctx context.Context, meta schema.ClientMe
if err != nil {
return err
}
res <- resp.ConfigurationRecorders
if len(resp.ConfigurationRecorders) == 0 {
return nil
}
names := make([]string, len(resp.ConfigurationRecorders))
for i, configurationRecorder := range resp.ConfigurationRecorders {
names[i] = *configurationRecorder.Name
}
status, err := c.Services().ConfigService.DescribeConfigurationRecorderStatus(ctx, &configservice.DescribeConfigurationRecorderStatusInput{
ConfigurationRecorderNames: names,
})
if err != nil {
return err
}
for _, configurationRecorder := range resp.ConfigurationRecorders {
var configurationRecorderStatus types.ConfigurationRecorderStatus
for _, s := range status.ConfigurationRecordersStatus {
if s.Name == configurationRecorder.Name {
configurationRecorderStatus = s
break
}
}
res <- configurationRecorderWrapper{
ConfigurationRecorder: configurationRecorder,
StatusLastErrorCode: configurationRecorderStatus.LastErrorCode,
StatusLastErrorMessage: configurationRecorderStatus.LastErrorMessage,
StatusLastStartTime: configurationRecorderStatus.LastStartTime,
StatusLastStatus: configurationRecorderStatus.LastStatus,
StatusLastStatusChangeTime: configurationRecorderStatus.LastStatusChangeTime,
StatusLastStopTime: configurationRecorderStatus.LastStopTime,
StatusRecording: configurationRecorderStatus.Recording,
}
}
return nil
}

func generateConfigRecorderArn(_ context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
cl := meta.(*client.Client)
cfg, ok := resource.Item.(types.ConfigurationRecorder)
cfg, ok := resource.Item.(configurationRecorderWrapper)
if !ok {
return fmt.Errorf("not config config recorder")
}
return resource.Set(c.Name, client.GenerateResourceARN("config", "config-recorder", *cfg.Name, cl.Region, cl.AccountID))
}

type configurationRecorderWrapper struct {
types.ConfigurationRecorder
StatusLastErrorCode *string
StatusLastErrorMessage *string
StatusLastStartTime *time.Time
StatusLastStatus types.RecorderStatus
StatusLastStatusChangeTime *time.Time
StatusLastStopTime *time.Time
StatusRecording bool
}
23 changes: 15 additions & 8 deletions resources/config_configuration_recorders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,32 @@ import (

"github.com/aws/aws-sdk-go-v2/service/configservice"
"github.com/aws/aws-sdk-go-v2/service/configservice/types"
"github.com/cloudquery/cq-provider-aws/client"
"github.com/cloudquery/cq-provider-aws/client/mocks"
"github.com/cloudquery/faker/v3"
"github.com/golang/mock/gomock"

"github.com/cloudquery/cq-provider-aws/client"
"github.com/cloudquery/cq-provider-aws/client/mocks"
)

func buildConfigConfigurationRecorders(t *testing.T, ctrl *gomock.Controller) client.Services {
m := mocks.NewMockConfigServiceClient(ctrl)

cr := types.ConfigurationRecorder{}
err := faker.FakeData(&cr)
if err != nil {
l := types.ConfigurationRecorder{}
if err := faker.FakeData(&l); err != nil {
t.Fatal(err)
}
sl := types.ConfigurationRecorderStatus{}
if err := faker.FakeData(&sl); err != nil {
t.Fatal(err)
}
sl.Name = l.Name
m.EXPECT().DescribeConfigurationRecorderStatus(gomock.Any(), gomock.Any(), gomock.Any()).Return(
&configservice.DescribeConfigurationRecorderStatusOutput{
ConfigurationRecordersStatus: []types.ConfigurationRecorderStatus{sl},
}, nil)
m.EXPECT().DescribeConfigurationRecorders(gomock.Any(), gomock.Any(), gomock.Any()).Return(
&configservice.DescribeConfigurationRecordersOutput{
ConfigurationRecorders: []types.ConfigurationRecorder{cr},
ConfigurationRecorders: []types.ConfigurationRecorder{l},
}, nil)

return client.Services{
ConfigService: m,
}
Expand Down
7 changes: 7 additions & 0 deletions resources/migrations/8_v0.7.1.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE IF EXISTS "aws_config_configuration_recorders" DROP COLUMN status_last_error_code,
DROP COLUMN status_last_error_message,
DROP COLUMN status_last_start_time,
DROP COLUMN status_last_status,
DROP COLUMN status_last_status_change_time,
DROP COLUMN status_last_stop_time,
DROP COLUMN status_recording;
7 changes: 7 additions & 0 deletions resources/migrations/9_v0.7.1.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE IF EXISTS "aws_config_configuration_recorders" ADD COLUMN status_last_error_code text,
ADD COLUMN status_last_error_message text,
ADD COLUMN status_last_start_time timestamp without time zone,
ADD COLUMN status_last_status text,
ADD COLUMN status_last_status_change_time timestamp without time zone,
ADD COLUMN status_last_stop_time timestamp without time zone,
ADD COLUMN status_recording boolean;

0 comments on commit 54288e1

Please sign in to comment.