Skip to content

Commit

Permalink
✨ Add serverless v2 fields. (#135)
Browse files Browse the repository at this point in the history
Signed-off-by: Preslav <preslav@mondoo.com>
  • Loading branch information
preslavgerchev authored Oct 3, 2024
1 parent 3d6ada4 commit d5d9a70
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 9 deletions.
11 changes: 11 additions & 0 deletions docs/resources/integration_aws_serverless.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ Optional:
- `ecr_scan` (Boolean) Enable ECR scan.
- `ecs_scan` (Boolean) Enable ECS scan.
- `event_scan_triggers` (Attributes) (see [below for nested schema](#nestedatt--scan_configuration--event_scan_triggers))
- `vpc_configuration` (Attributes) (see [below for nested schema](#nestedatt--scan_configuration--vpc_configuration))

<a id="nestedatt--scan_configuration--ec2_scan_options"></a>
### Nested Schema for `scan_configuration.ec2_scan_options`
Expand Down Expand Up @@ -172,3 +173,13 @@ Optional:
- `event_detail_type` (String) Event detail type.
- `event_source` (String) Event source.
- `scan_type` (String) Scan type.


<a id="nestedatt--scan_configuration--vpc_configuration"></a>
### Nested Schema for `scan_configuration.vpc_configuration`

Optional:

- `cidr_block` (String) CIDR block for the Mondoo VPC.
- `use_default_vpc` (Boolean) Use default VPC.
- `use_mondoo_vpc` (Boolean) Use Mondoo VPC.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/hashicorp/terraform-plugin-go v0.23.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-testing v1.10.0
go.mondoo.com/mondoo-go v0.0.0-20240611114249-2c3b9b20e67a
go.mondoo.com/mondoo-go v0.0.0-20241001090148-7f5a1917d669
gopkg.in/yaml.v2 v2.4.0
)

Expand Down Expand Up @@ -113,7 +113,7 @@ require (
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.23.0 // indirect
golang.org/x/term v0.23.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,8 @@ go.abhg.dev/goldmark/frontmatter v0.2.0/go.mod h1:XqrEkZuM57djk7zrlRUB02x8I5J0px
go.etcd.io/etcd/api/v3 v3.5.4/go.mod h1:5GB2vv4A4AOn3yk7MftYGHkUfGtDHnEraIjym4dYz5A=
go.etcd.io/etcd/client/pkg/v3 v3.5.4/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=
go.etcd.io/etcd/client/v3 v3.5.4/go.mod h1:ZaRkVgBZC+L+dLCjTcF1hRXpgZXQPOvnA/Ak/gq3kiY=
go.mondoo.com/mondoo-go v0.0.0-20240611114249-2c3b9b20e67a h1:+EQW5uXRyUyeiyZnTy2Jc371PTynJm5OruUWt3SqiT4=
go.mondoo.com/mondoo-go v0.0.0-20240611114249-2c3b9b20e67a/go.mod h1:4032UBD0ph9LyhXq5OQmmxkJv37HdAGi34YLWbhnMDA=
go.mondoo.com/mondoo-go v0.0.0-20241001090148-7f5a1917d669 h1:9podmJ/O6dbJtzX13lwuUoQb5KYxeSQcXa65/rXOtVk=
go.mondoo.com/mondoo-go v0.0.0-20241001090148-7f5a1917d669/go.mod h1:dun0t/zVJCSB/u9dhSO4FXn13moiH7JSwS2m8Ucxx50=
go.mongodb.org/mongo-driver v1.15.1 h1:l+RvoUOoMXFmADTLfYDm7On9dRm7p4T80/lEQM+r7HU=
go.mongodb.org/mongo-driver v1.15.1/go.mod h1:Vzb0Mk/pa7e6cWw85R4F/endUC3u0U9jGcNU603k65c=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
Expand Down Expand Up @@ -553,8 +553,8 @@ golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs=
golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs=
golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
70 changes: 70 additions & 0 deletions internal/provider/integration_aws_serverless_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ type ScanConfigurationInput struct {
EventScanTriggers *[]*AWSEventPatternInput `tfsdk:"event_scan_triggers"`
// (Optional.)
Ec2ScanOptions *Ec2ScanOptionsInput `tfsdk:"ec2_scan_options"`
// (Optional.)
VpcConfiguration *VPCConfigurationInput `tfsdk:"vpc_configuration"`
}

type VPCConfigurationInput struct {
// (Optional.)
UseDefaultVPC types.Bool `tfsdk:"use_default_vpc"`
// (Optional.)
UseMondooVPC types.Bool `tfsdk:"use_mondoo_vpc"`
// (Optional.)
CIDR types.String `tfsdk:"cidr_block"`
}

type AWSEventPatternInput struct {
Expand Down Expand Up @@ -144,6 +155,11 @@ func (m integrationAwsServerlessResourceModel) GetConfigurationOptions() *mondoo
IsOrganization: mondoov1.NewBooleanPtr(mondoov1.Boolean(m.IsOrganization.ValueBool())),
AccountIDs: &accountIDs,
ScanConfiguration: mondoov1.ScanConfigurationInput{
VpcConfiguration: &mondoov1.VPCConfigurationInput{
UseDefaultVPC: mondoov1.NewBooleanPtr(mondoov1.Boolean(m.ScanConfiguration.VpcConfiguration.UseDefaultVPC.ValueBool())),
UseMondooVPC: mondoov1.NewBooleanPtr(mondoov1.Boolean(m.ScanConfiguration.VpcConfiguration.UseMondooVPC.ValueBool())),
CIDR: mondoov1.NewStringPtr(mondoov1.String(m.ScanConfiguration.VpcConfiguration.CIDR.ValueString())),
},
Ec2Scan: mondoov1.NewBooleanPtr(mondoov1.Boolean(m.ScanConfiguration.Ec2Scan.ValueBool())),
EcrScan: mondoov1.NewBooleanPtr(mondoov1.Boolean(m.ScanConfiguration.EcrScan.ValueBool())),
EcsScan: mondoov1.NewBooleanPtr(mondoov1.Boolean(m.ScanConfiguration.EcsScan.ValueBool())),
Expand Down Expand Up @@ -228,6 +244,23 @@ func (r *integrationAwsServerlessResource) Schema(ctx context.Context, req resou
MarkdownDescription: "Cron scan in hours.",
Optional: true,
},
"vpc_configuration": schema.SingleNestedAttribute{
Optional: true,
Attributes: map[string]schema.Attribute{
"use_default_vpc": schema.BoolAttribute{
MarkdownDescription: "Use default VPC.",
Optional: true,
},
"use_mondoo_vpc": schema.BoolAttribute{
MarkdownDescription: "Use Mondoo VPC.",
Optional: true,
},
"cidr_block": schema.StringAttribute{
MarkdownDescription: "CIDR block for the Mondoo VPC.",
Optional: true,
},
},
},
"ec2_scan_options": schema.SingleNestedAttribute{
Required: true,
Attributes: map[string]schema.Attribute{
Expand Down Expand Up @@ -305,6 +338,43 @@ func (r *integrationAwsServerlessResource) Schema(ctx context.Context, req resou
}
}

func (r integrationAwsServerlessResource) ValidateConfig(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) {
var data integrationAwsServerlessResourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

// user has provided both default or mondoo vpc
if !data.ScanConfiguration.VpcConfiguration.UseDefaultVPC.IsNull() && !data.ScanConfiguration.VpcConfiguration.UseMondooVPC.IsNull() {
defaultVpc := data.ScanConfiguration.VpcConfiguration.UseDefaultVPC.ValueBool()
mondooVpc := data.ScanConfiguration.VpcConfiguration.UseMondooVPC.ValueBool()
if defaultVpc && mondooVpc {
resp.Diagnostics.AddError(
"ConflictingAttributesError",
"Cannot set both use_default_vpc and use_mondoo_vpc to true at the same time.",
)
}

if !defaultVpc && !mondooVpc {
resp.Diagnostics.AddError(
"ConflictingAttributesError",
"Cannot set both use_default_vpc and use_mondoo_vpc to false at the same time.",
)
}
}
// user has provided mondoo vpc only
if mondooVpc := data.ScanConfiguration.VpcConfiguration.UseMondooVPC.ValueBool(); mondooVpc {
if cidr := data.ScanConfiguration.VpcConfiguration.CIDR.ValueString(); cidr == "" {
resp.Diagnostics.AddError(
"MissingAttributeError",
"Attribute cidr_block must not be empty when use_mondoo_vpc is set to true.",
)
}
}
}

func (r *integrationAwsServerlessResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/provider/space_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ func TestAccSpaceWithIDResource(t *testing.T) {
t.Fatal(err)
}

min := 1000
max := 3000
customSpaceID := "my-custom-space-id" + fmt.Sprint(rand.Intn(max-min)+min)
minV := 1000
maxV := 3000
customSpaceID := "my-custom-space-id" + fmt.Sprint(rand.Intn(maxV-minV)+minV)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down

0 comments on commit d5d9a70

Please sign in to comment.