Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⭐️ Added resource validations #114

Merged
merged 3 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions internal/provider/integration_aws_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ package provider
import (
"context"
"fmt"
"regexp"

"github.com/hashicorp/terraform-plugin-framework-validators/objectvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down Expand Up @@ -102,6 +105,9 @@ func (r *integrationAwsResource) Schema(ctx context.Context, req resource.Schema
"name": schema.StringAttribute{
MarkdownDescription: "Name of the integration.",
Required: true,
Validators: []validator.String{
stringvalidator.LengthAtMost(250),
},
},
"credentials": schema.SingleNestedAttribute{
Required: true,
Expand Down Expand Up @@ -131,10 +137,22 @@ func (r *integrationAwsResource) Schema(ctx context.Context, req resource.Schema
"access_key": schema.StringAttribute{
Required: true,
Sensitive: true,
Validators: []validator.String{
stringvalidator.RegexMatches(
regexp.MustCompile(`^([A-Z0-9]{20})$`),
"must be a 20 character string with uppercase letters and numbers only",
),
},
},
"secret_key": schema.StringAttribute{
Required: true,
Sensitive: true,
Validators: []validator.String{
stringvalidator.RegexMatches(
regexp.MustCompile(`^([a-zA-Z0-9+/]{40})$`),
"must be a 40 character string with alphanumeric values and + and / only",
),
},
},
},
},
Expand Down
18 changes: 18 additions & 0 deletions internal/provider/integration_azure_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
mondoov1 "go.mondoo.com/mondoo-go"
)
Expand Down Expand Up @@ -66,6 +69,9 @@ func (r *integrationAzureResource) Schema(ctx context.Context, req resource.Sche
"name": schema.StringAttribute{
MarkdownDescription: "Name of the integration.",
Required: true,
Validators: []validator.String{
stringvalidator.LengthAtMost(250),
},
},
"client_id": schema.StringAttribute{
MarkdownDescription: "Azure Client ID.",
Expand All @@ -83,11 +89,23 @@ func (r *integrationAzureResource) Schema(ctx context.Context, req resource.Sche
MarkdownDescription: "List of Azure subscriptions to scan.",
Optional: true,
ElementType: types.StringType,
Validators: []validator.List{
// Validate only this attribute or other_attr is configured.
listvalidator.ConflictsWith(path.Expressions{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice addition

path.MatchRoot("subscription_deny_list"),
}...),
},
},
"subscription_deny_list": schema.ListAttribute{
MarkdownDescription: "List of Azure subscriptions to exclude from scanning.",
Optional: true,
ElementType: types.StringType,
Validators: []validator.List{
// Validate only this attribute or other_attr is configured.
listvalidator.ConflictsWith(path.Expressions{
path.MatchRoot("subscription_allow_list"),
}...),
},
},
"credentials": schema.SingleNestedAttribute{
Required: true,
Expand Down
9 changes: 9 additions & 0 deletions internal/provider/integration_domain_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package provider
import (
"context"
"fmt"
"regexp"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
mondoov1 "go.mondoo.com/mondoo-go"
)
Expand Down Expand Up @@ -56,6 +59,12 @@ func (r *integrationDomainResource) Schema(ctx context.Context, req resource.Sch
"host": schema.StringAttribute{
MarkdownDescription: "Domain name or IP address.",
Required: true,
Validators: []validator.String{
stringvalidator.RegexMatches(
regexp.MustCompile(`^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$|^([a-z0-9-]+\.)+[a-z]{2,}$`),
"must contain only lowercase letters and at least one dot or be an IPv4 address",
),
},
},
"https": schema.BoolAttribute{
MarkdownDescription: "Enable HTTPS port.",
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/integration_gcp_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
mondoov1 "go.mondoo.com/mondoo-go"
)
Expand Down Expand Up @@ -65,6 +67,9 @@ func (r *integrationGcpResource) Schema(ctx context.Context, req resource.Schema
"name": schema.StringAttribute{
MarkdownDescription: "Name of the integration.",
Required: true,
Validators: []validator.String{
stringvalidator.LengthAtMost(250),
},
},
"project_id": schema.StringAttribute{
MarkdownDescription: "GCP project id",
Expand Down
25 changes: 25 additions & 0 deletions internal/provider/integration_github_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package provider
import (
"context"
"fmt"
"regexp"

"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
mondoov1 "go.mondoo.com/mondoo-go"
)
Expand Down Expand Up @@ -101,6 +105,9 @@ func (r *integrationGithubResource) Schema(ctx context.Context, req resource.Sch
"name": schema.StringAttribute{
MarkdownDescription: "Name of the integration.",
Required: true,
Validators: []validator.String{
stringvalidator.LengthAtMost(250),
},
},
"owner": schema.StringAttribute{
MarkdownDescription: "GitHub Owner.",
Expand All @@ -114,11 +121,23 @@ func (r *integrationGithubResource) Schema(ctx context.Context, req resource.Sch
MarkdownDescription: "List of GitHub repositories to scan.",
Optional: true,
ElementType: types.StringType,
Validators: []validator.List{
// Validate only this attribute or other_attr is configured.
listvalidator.ConflictsWith(path.Expressions{
path.MatchRoot("repository_deny_list"),
}...),
},
},
"repository_deny_list": schema.ListAttribute{
MarkdownDescription: "List of GitHub repositories to exclude from scanning.",
Optional: true,
ElementType: types.StringType,
Validators: []validator.List{
// Validate only this attribute or other_attr is configured.
listvalidator.ConflictsWith(path.Expressions{
path.MatchRoot("repository_allow_list"),
}...),
},
},
"credentials": schema.SingleNestedAttribute{
Required: true,
Expand All @@ -127,6 +146,12 @@ func (r *integrationGithubResource) Schema(ctx context.Context, req resource.Sch
MarkdownDescription: "Token for GitHub integration.",
Required: true,
Sensitive: true,
Validators: []validator.String{
stringvalidator.RegexMatches(
regexp.MustCompile(`^(ghp_[a-zA-Z0-9]{36}|github_pat_[a-zA-Z0-9]{22}_[a-zA-Z0-9]{59})$`),
"must be a valid classic GitHub Token with 40 characters in length, with a prefix of ghp_ or a fine-grained GitHub token with 93 characters in length, with a prefix of github_pat_",
),
},
},
},
},
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/integration_ms365_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
mondoov1 "go.mondoo.com/mondoo-go"
)
Expand Down Expand Up @@ -63,6 +65,9 @@ func (r *integrationMs365Resource) Schema(ctx context.Context, req resource.Sche
"name": schema.StringAttribute{
MarkdownDescription: "Name of the integration.",
Required: true,
Validators: []validator.String{
stringvalidator.LengthAtMost(250),
},
},
"client_id": schema.StringAttribute{
MarkdownDescription: "Azure Client ID.",
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/integration_oci_tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
mondoov1 "go.mondoo.com/mondoo-go"
Expand Down Expand Up @@ -75,6 +77,9 @@ func (r *integrationOciTenantResource) Schema(ctx context.Context, req resource.
"name": schema.StringAttribute{
MarkdownDescription: "Name of the integration.",
Optional: true,
Validators: []validator.String{
stringvalidator.LengthAtMost(250),
},
},
"tenancy": schema.StringAttribute{
MarkdownDescription: "OCI tenancy",
Expand Down
12 changes: 12 additions & 0 deletions internal/provider/integration_slack_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package provider
import (
"context"
"fmt"
"regexp"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
mondoov1 "go.mondoo.com/mondoo-go"
)
Expand Down Expand Up @@ -57,11 +60,20 @@ func (r *integrationSlackResource) Schema(ctx context.Context, req resource.Sche
"name": schema.StringAttribute{
MarkdownDescription: "Name of the integration.",
Required: true,
Validators: []validator.String{
stringvalidator.LengthAtMost(250),
},
},
"slack_token": schema.StringAttribute{
Required: true,
Sensitive: true,
Description: "The Slack token to authenticate with the Slack API.",
Validators: []validator.String{
stringvalidator.RegexMatches(
regexp.MustCompile(`^xox[baprs](-[0-9a-zA-Z]{10,48})+$`),
"must start with xox and one of the following characters b, a, p, r, s, followed by one or more blocks consisting of a dash and 10-48 alphanumeric characters",
),
},
},
},
}
Expand Down
6 changes: 6 additions & 0 deletions internal/provider/policy_assignment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ package provider
import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
mondoov1 "go.mondoo.com/mondoo-go"

"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down Expand Up @@ -62,6 +65,9 @@ func (r *policyAssignmentResource) Schema(ctx context.Context, req resource.Sche
Default: stringdefault.StaticString("enabled"),
Computed: true,
Optional: true,
Validators: []validator.String{
stringvalidator.OneOf("enabled", "disabled", "preview"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice addition

},
},
},
}
Expand Down
8 changes: 7 additions & 1 deletion internal/provider/querypack_assignment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ package provider
import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
mondoov1 "go.mondoo.com/mondoo-go"
"strings"

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down Expand Up @@ -63,6 +66,9 @@ func (r *queryPackAssignmentResource) Schema(ctx context.Context, req resource.S
Default: stringdefault.StaticString("enabled"),
Computed: true,
Optional: true,
Validators: []validator.String{
stringvalidator.OneOf("enabled", "disabled"),
},
},
},
}
Expand Down
21 changes: 21 additions & 0 deletions internal/provider/space_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ package provider
import (
"context"
"fmt"
"regexp"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
mondoov1 "go.mondoo.com/mondoo-go"
Expand Down Expand Up @@ -56,6 +59,12 @@ func (r *SpaceResource) Schema(ctx context.Context, req resource.SchemaRequest,
"name": schema.StringAttribute{
MarkdownDescription: "Name of the space.",
Optional: true,
Validators: []validator.String{
stringvalidator.RegexMatches(
regexp.MustCompile(`^([a-zA-Z \-'_]|\d){2,30}$`),
"must contain 2 to 30 characters, where each character can be a letter (uppercase or lowercase), a space, a dash, an underscore, or a digit",
),
},
},
"id": schema.StringAttribute{
MarkdownDescription: "Id of the space. Must be globally unique.",
Expand All @@ -64,6 +73,12 @@ func (r *SpaceResource) Schema(ctx context.Context, req resource.SchemaRequest,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{
stringvalidator.RegexMatches(
regexp.MustCompile(`^[a-z]([\d-_]|[a-z]){6,35}[a-z\d]$`),
"must contain 6 to 35 digits, dashes, underscores, or lowercase letters, and ending with either a lowercase letter or a digit",
),
},
},
"mrn": schema.StringAttribute{
MarkdownDescription: "Mrn of the space.",
Expand All @@ -75,6 +90,12 @@ func (r *SpaceResource) Schema(ctx context.Context, req resource.SchemaRequest,
"org_id": schema.StringAttribute{
MarkdownDescription: "Id of the organization.",
Required: true,
Validators: []validator.String{
stringvalidator.RegexMatches(
regexp.MustCompile(`^[a-z]([\d-_]|[a-z]){6,35}[a-z\d]$`),
"must contain 6 to 35 digits, dashes, underscores, or lowercase letters, and ending with either a lowercase letter or a digit",
),
},
},
},
}
Expand Down
Loading