Skip to content

Commit

Permalink
Refactor bot protection secret key editing state management
Browse files Browse the repository at this point in the history
ref DEV-1771
  • Loading branch information
louischan-oursky committed Aug 19, 2024
2 parents 58e612d + eff5cd0 commit c13b789
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 139 deletions.
2 changes: 1 addition & 1 deletion pkg/lib/config/bot_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var _ = Schema.Add("BotProtectionProvider", `
"required": ["type"],
"properties": {
"type": { "type": "string", "enum": ["cloudflare", "recaptchav2"] },
"site_key": { "type": "string" }
"site_key": { "type": "string", "minLength": 1 }
},
"allOf": [
{
Expand Down
2 changes: 1 addition & 1 deletion pkg/lib/config/secret_bot_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var _ = SecretConfigSchema.Add("BotProtectionProviderCredentials", `
"additionalProperties": false,
"properties": {
"type": { "type": "string", "enum": ["cloudflare", "recaptchav2"] },
"secret_key": { "type": "string" }
"secret_key": { "type": "string", "minLength": 1 }
},
"allOf": [
{
Expand Down
13 changes: 13 additions & 0 deletions pkg/lib/config/secret_update_instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ func (i *BotProtectionProviderCredentialsUpdateInstruction) ApplyTo(ctx *SecretC
switch i.Action {
case SecretUpdateInstructionActionSet:
return i.set(currentConfig)
case SecretUpdateInstructionActionUnset:
return i.unset(currentConfig)
default:
return nil, fmt.Errorf("config: unexpected action for BotProtectionProviderCredentialsUpdateInstruction: %s", i.Action)
}
Expand Down Expand Up @@ -556,6 +558,17 @@ func (i *BotProtectionProviderCredentialsUpdateInstruction) set(currentConfig *S
return out, nil
}

func (i *BotProtectionProviderCredentialsUpdateInstruction) unset(currentConfig *SecretConfig) (*SecretConfig, error) {
out := &SecretConfig{}
for _, item := range currentConfig.Secrets {
if item.Key == BotProtectionProviderCredentialsKey {
continue
}
out.Secrets = append(out.Secrets, item)
}
return out, nil
}

var _ SecretConfigUpdateInstructionInterface = &SecretConfigUpdateInstruction{}
var _ SecretConfigUpdateInstructionInterface = &OAuthSSOProviderCredentialsUpdateInstruction{}
var _ SecretConfigUpdateInstructionInterface = &SMTPServerCredentialsUpdateInstruction{}
Expand Down
14 changes: 13 additions & 1 deletion pkg/lib/config/testdata/bot_protection_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,16 @@ value:
site_key: dummy
requirements:
signup_or_login:
mode: invalid_mode
mode: invalid_mode
---
part: BotProtectionConfig
name: bot-protection-site-key-cannot-be-empty-string
error: |-
invalid value:
/provider/site_key: minLength
map[actual:0 expected:1]
value:
enabled: true
provider:
type: cloudflare
site_key: ""
14 changes: 13 additions & 1 deletion pkg/lib/config/testdata/parse_secret_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,16 @@ config:
secrets:
- key: bot_protection.provider
data:
type: hcaptcha # not supported
type: hcaptcha # not supported
---
name: bot-protection/secret-key-must-be-non-empty-string
error: |-
invalid secrets:
/secrets/0/data/secret_key: minLength
map[actual:0 expected:1]
config:
secrets:
- key: bot_protection.provider
data:
type: cloudflare
secret_key: ""
2 changes: 1 addition & 1 deletion pkg/lib/config/testdata/secret_config_validate_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,4 @@ secret_config:
secrets:
- key: captcha.cloudflare
data:
secret: some secret
secret: some secret
25 changes: 24 additions & 1 deletion pkg/lib/config/testdata/secret_update_instruction.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -680,4 +680,27 @@ updateInstructionJSON: |-
"type": "recaptchav2"
}
}
}
}
---
name: unset-bot-protection-provider-secret
error: null
currentSecretConfigYAML: |-
secrets:
- key: redis
data:
redis_url: "redis://127.0.0.1"
- key: bot_protection.provider
data:
secret_key: old-key
type: recaptchav2
newSecretConfigYAML: |-
secrets:
- key: redis
data:
redis_url: "redis://127.0.0.1"
updateInstructionJSON: |-
{
"botProtectionProviderSecret": {
"action": "unset"
}
}
8 changes: 8 additions & 0 deletions portal/src/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,11 @@ export function useFormConflictErrors(): readonly APIResourceUpdateConflictError

return ctx.conflictErrors;
}

export function useFormLoading(): boolean {
const ctx = useContext(context);
if (!ctx) {
throw new Error("Attempted to use useFormLoading outside FormProvider");
}
return ctx.loading;
}
Loading

0 comments on commit c13b789

Please sign in to comment.