-
Notifications
You must be signed in to change notification settings - Fork 0
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
[TIC-958] Signup domain allowlist/blocklist #11
Merged
mrmauer
merged 8 commits into
main
from
TIC-958/Allow-and-block-list-for-domains-on-signup
Oct 2, 2024
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a81bb03
feat(basic_auth_configuration): add signup domain allowlist/blocklist
itailevi98 90b1bec
fix: lint
itailevi98 2a3e765
update .md from go generate
itailevi98 377d079
refactor(basic_auth_configuration): add validation to config for allo…
itailevi98 4292be1
fix(.md): go generate ./...
itailevi98 7fb8d33
chore: fix docstrings
itailevi98 888508a
fix: update .md docs
itailevi98 e333c0a
fix(client): change url back to prod
itailevi98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |
"terraform-provider-propelauth/internal/propelauth" | ||
|
||
"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/schema/validator" | ||
|
@@ -17,6 +18,7 @@ import ( | |
// Ensure provider defined types fully satisfy framework interfaces. | ||
var _ resource.Resource = &basicAuthConfigurationResource{} | ||
var _ resource.ResourceWithConfigure = &basicAuthConfigurationResource{} | ||
var _ resource.ResourceWithValidateConfig = &basicAuthConfigurationResource{} | ||
|
||
func NewBasicAuthConfigurationResource() resource.Resource { | ||
return &basicAuthConfigurationResource{} | ||
|
@@ -29,15 +31,17 @@ type basicAuthConfigurationResource struct { | |
|
||
// basicAuthConfigurationResourceModel describes the resource data model. | ||
type basicAuthConfigurationResourceModel struct { | ||
AllowUsersToSignupWithPersonalEmail types.Bool `tfsdk:"allow_users_to_signup_with_personal_email"` | ||
HasPasswordLogin types.Bool `tfsdk:"has_password_login"` | ||
HasPasswordlessLogin types.Bool `tfsdk:"has_passwordless_login"` | ||
WaitlistUsersEnabled types.Bool `tfsdk:"waitlist_users_enabled"` | ||
UserAutologoutSeconds types.Int64 `tfsdk:"user_autologout_seconds"` | ||
UserAutologoutType types.String `tfsdk:"user_autologout_type"` | ||
UsersCanDeleteOwnAccount types.Bool `tfsdk:"users_can_delete_own_account"` | ||
UsersCanChangeEmail types.Bool `tfsdk:"users_can_change_email"` | ||
IncludeLoginMethod types.Bool `tfsdk:"include_login_method"` | ||
AllowUsersToSignupWithPersonalEmail types.Bool `tfsdk:"allow_users_to_signup_with_personal_email"` | ||
SignupDomainAllowlist []types.String `tfsdk:"signup_domain_allowlist"` | ||
SignupDomainBlocklist []types.String `tfsdk:"signup_domain_blocklist"` | ||
HasPasswordLogin types.Bool `tfsdk:"has_password_login"` | ||
HasPasswordlessLogin types.Bool `tfsdk:"has_passwordless_login"` | ||
WaitlistUsersEnabled types.Bool `tfsdk:"waitlist_users_enabled"` | ||
UserAutologoutSeconds types.Int64 `tfsdk:"user_autologout_seconds"` | ||
UserAutologoutType types.String `tfsdk:"user_autologout_type"` | ||
UsersCanDeleteOwnAccount types.Bool `tfsdk:"users_can_delete_own_account"` | ||
UsersCanChangeEmail types.Bool `tfsdk:"users_can_change_email"` | ||
IncludeLoginMethod types.Bool `tfsdk:"include_login_method"` | ||
} | ||
|
||
func (r *basicAuthConfigurationResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
|
@@ -52,8 +56,26 @@ func (r *basicAuthConfigurationResource) Schema(ctx context.Context, req resourc | |
"allow_users_to_signup_with_personal_email": schema.BoolAttribute{ | ||
Optional: true, | ||
Description: "If true, your users will be able to sign up using personal email domains (@gmail.com, @yahoo.com, etc.)." + | ||
"The default setting is true.", | ||
"The default setting is true. This is only enabled if `signup_domain_allowlist` is empty.", | ||
}, | ||
// "signup_domain_allowlist_enabled": schema.BoolAttribute{ | ||
// Optional: true, | ||
// Description: "If true, only users with email domains in the allowlist will be able to sign up. The default setting is false.", | ||
// }, | ||
"signup_domain_allowlist": schema.ListAttribute{ | ||
ElementType: types.StringType, | ||
Optional: true, | ||
Description: "A list of email domains that are allowed to sign up. This is only used if `signup_domain_allowlist_enabled` is true.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we remove any references in these field docstrings to the now deleted fields such as |
||
}, | ||
"signup_domain_blocklist": schema.ListAttribute{ | ||
ElementType: types.StringType, | ||
Optional: true, | ||
Description: "A list of email domains that are blocked from signing up. This is only used if `signup_domain_blocklist_enabled` is true and `signup_domain_allowlist` is empty.", | ||
}, | ||
// "signup_domain_blocklist_enabled": schema.BoolAttribute{ | ||
// Optional: true, | ||
// Description: "If true, users with email domains in the blocklist will not be able to sign up. The default setting is false. This is only used if `signup_domain_allowlist_enabled` is false.", | ||
// }, | ||
"has_password_login": schema.BoolAttribute{ | ||
Optional: true, | ||
Description: "If true, your users will be able to log in using their email and password. The default setting is true.", | ||
|
@@ -116,6 +138,27 @@ func (r *basicAuthConfigurationResource) Configure(ctx context.Context, req reso | |
r.client = client | ||
} | ||
|
||
func (r *basicAuthConfigurationResource) ValidateConfig(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) { | ||
var plan basicAuthConfigurationResourceModel | ||
|
||
// Read Terraform plan data into the model | ||
diags := req.Config.Get(ctx, &plan) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// Validate the plan data | ||
if plan.SignupDomainAllowlist != nil && plan.SignupDomainBlocklist != nil { | ||
resp.Diagnostics.AddAttributeError( | ||
path.Root("signup_domain_allowlist"), | ||
"Invalid `signup_domain_allowlist`", | ||
"`signup_domain_allowlist` and `signup_domain_blocklist` cannot both be set", | ||
) | ||
return | ||
} | ||
} | ||
|
||
func (r *basicAuthConfigurationResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
var plan basicAuthConfigurationResourceModel | ||
|
||
|
@@ -139,6 +182,30 @@ func (r *basicAuthConfigurationResource) Create(ctx context.Context, req resourc | |
IncludeLoginMethod: plan.IncludeLoginMethod.ValueBoolPointer(), | ||
} | ||
|
||
var signupDomainAllowlistEnabled bool | ||
if plan.SignupDomainAllowlist != nil { | ||
signupDomainAllowlistEnabled = true | ||
environmentConfigUpdate.SignupDomainAllowlist = make([]string, len(plan.SignupDomainAllowlist)) | ||
for i, domain := range plan.SignupDomainAllowlist { | ||
environmentConfigUpdate.SignupDomainAllowlist[i] = domain.ValueString() | ||
} | ||
} else { | ||
signupDomainAllowlistEnabled = false | ||
} | ||
environmentConfigUpdate.SignupDomainAllowlistEnabled = &signupDomainAllowlistEnabled | ||
|
||
var signupDomainBlocklistEnabled bool | ||
if plan.SignupDomainBlocklist != nil { | ||
signupDomainBlocklistEnabled = true | ||
environmentConfigUpdate.SignupDomainBlocklist = make([]string, len(plan.SignupDomainBlocklist)) | ||
for i, domain := range plan.SignupDomainBlocklist { | ||
environmentConfigUpdate.SignupDomainBlocklist[i] = domain.ValueString() | ||
} | ||
} else { | ||
signupDomainBlocklistEnabled = false | ||
} | ||
environmentConfigUpdate.SignupDomainBlocklistEnabled = &signupDomainBlocklistEnabled | ||
|
||
environmentConfigResponse, err := r.client.UpdateEnvironmentConfig(&environmentConfigUpdate) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
|
@@ -221,6 +288,42 @@ func (r *basicAuthConfigurationResource) Create(ctx context.Context, req resourc | |
) | ||
return | ||
} | ||
if plan.SignupDomainAllowlist != nil { | ||
if len(plan.SignupDomainAllowlist) != len(environmentConfigResponse.SignupDomainAllowlist) { | ||
resp.Diagnostics.AddError( | ||
"Error updating basic auth configuration", | ||
"SignupDomainAllowlist failed to update. The `signup_domain_allowlist` is instead "+fmt.Sprintf("%v", environmentConfigResponse.SignupDomainAllowlist), | ||
) | ||
return | ||
} | ||
for i, domain := range plan.SignupDomainAllowlist { | ||
if domain.ValueString() != environmentConfigResponse.SignupDomainAllowlist[i] { | ||
resp.Diagnostics.AddError( | ||
"Error updating basic auth configuration", | ||
"SignupDomainAllowlist failed to update. The `signup_domain_allowlist` is instead "+fmt.Sprintf("%v", environmentConfigResponse.SignupDomainAllowlist), | ||
) | ||
return | ||
} | ||
} | ||
} | ||
if plan.SignupDomainBlocklist != nil { | ||
if len(plan.SignupDomainBlocklist) != len(environmentConfigResponse.SignupDomainBlocklist) { | ||
resp.Diagnostics.AddError( | ||
"Error updating basic auth configuration", | ||
"SignupDomainBlocklist failed to update. The `signup_domain_blocklist` is instead "+fmt.Sprintf("%v", environmentConfigResponse.SignupDomainBlocklist), | ||
) | ||
return | ||
} | ||
for i, domain := range plan.SignupDomainBlocklist { | ||
if domain.ValueString() != environmentConfigResponse.SignupDomainBlocklist[i] { | ||
resp.Diagnostics.AddError( | ||
"Error updating basic auth configuration", | ||
"SignupDomainBlocklist failed to update. The `signup_domain_blocklist` is instead "+fmt.Sprintf("%v", environmentConfigResponse.SignupDomainBlocklist), | ||
) | ||
return | ||
} | ||
} | ||
} | ||
|
||
// Write logs using the tflog package | ||
// Documentation: https://terraform.io/plugin/log | ||
|
@@ -305,6 +408,30 @@ func (r *basicAuthConfigurationResource) Update(ctx context.Context, req resourc | |
IncludeLoginMethod: plan.IncludeLoginMethod.ValueBoolPointer(), | ||
} | ||
|
||
var signupDomainAllowlistEnabled bool | ||
if plan.SignupDomainAllowlist != nil { | ||
signupDomainAllowlistEnabled = true | ||
environmentConfigUpdate.SignupDomainAllowlist = make([]string, len(plan.SignupDomainAllowlist)) | ||
for i, domain := range plan.SignupDomainAllowlist { | ||
environmentConfigUpdate.SignupDomainAllowlist[i] = domain.ValueString() | ||
} | ||
} else { | ||
signupDomainAllowlistEnabled = false | ||
} | ||
environmentConfigUpdate.SignupDomainAllowlistEnabled = &signupDomainAllowlistEnabled | ||
|
||
var signupDomainBlocklistEnabled bool | ||
if plan.SignupDomainBlocklist != nil { | ||
signupDomainBlocklistEnabled = true | ||
environmentConfigUpdate.SignupDomainBlocklist = make([]string, len(plan.SignupDomainBlocklist)) | ||
for i, domain := range plan.SignupDomainBlocklist { | ||
environmentConfigUpdate.SignupDomainBlocklist[i] = domain.ValueString() | ||
} | ||
} else { | ||
signupDomainBlocklistEnabled = false | ||
} | ||
environmentConfigUpdate.SignupDomainBlocklistEnabled = &signupDomainBlocklistEnabled | ||
|
||
environmentConfigResponse, err := r.client.UpdateEnvironmentConfig(&environmentConfigUpdate) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
|
@@ -387,6 +514,42 @@ func (r *basicAuthConfigurationResource) Update(ctx context.Context, req resourc | |
) | ||
return | ||
} | ||
if plan.SignupDomainAllowlist != nil { | ||
if len(plan.SignupDomainAllowlist) != len(environmentConfigResponse.SignupDomainAllowlist) { | ||
resp.Diagnostics.AddError( | ||
"Error updating basic auth configuration", | ||
"SignupDomainAllowlist failed to update. The `signup_domain_allowlist` is instead "+fmt.Sprintf("%v", environmentConfigResponse.SignupDomainAllowlist), | ||
) | ||
return | ||
} | ||
for i, domain := range plan.SignupDomainAllowlist { | ||
if domain.ValueString() != environmentConfigResponse.SignupDomainAllowlist[i] { | ||
resp.Diagnostics.AddError( | ||
"Error updating basic auth configuration", | ||
"SignupDomainAllowlist failed to update. The `signup_domain_allowlist` is instead "+fmt.Sprintf("%v", environmentConfigResponse.SignupDomainAllowlist), | ||
) | ||
return | ||
} | ||
} | ||
} | ||
if plan.SignupDomainBlocklist != nil { | ||
if len(plan.SignupDomainBlocklist) != len(environmentConfigResponse.SignupDomainBlocklist) { | ||
resp.Diagnostics.AddError( | ||
"Error updating basic auth configuration", | ||
"SignupDomainBlocklist failed to update. The `signup_domain_blocklist` is instead "+fmt.Sprintf("%v", environmentConfigResponse.SignupDomainBlocklist), | ||
) | ||
return | ||
} | ||
for i, domain := range plan.SignupDomainBlocklist { | ||
if domain.ValueString() != environmentConfigResponse.SignupDomainBlocklist[i] { | ||
resp.Diagnostics.AddError( | ||
"Error updating basic auth configuration", | ||
"SignupDomainBlocklist failed to update. The `signup_domain_blocklist` is instead "+fmt.Sprintf("%v", environmentConfigResponse.SignupDomainBlocklist), | ||
) | ||
return | ||
} | ||
} | ||
} | ||
|
||
// Write logs using the tflog package | ||
// Documentation: https://terraform.io/plugin/log | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit] Can we deleted the commented out code here and below?