Skip to content

Commit de7e382

Browse files
authored
✨ Add env for RemediationStatusCode (#250)
* ✨ Add env for defaultStatusCode * 📝 doc * ✨change name of the parameter * 🔧 Add config check * fix lint
1 parent abae855 commit de7e382

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,10 @@ _By careful when you upgrade to >1.4.x_
439439
- int64
440440
- default: 60
441441
- Used only in `live` mode, maximum decision duration
442+
- RemediationStatusCode
443+
- int
444+
- default: 403
445+
- HTTP status code for banned user (not captcha)
442446
- CrowdsecCapiMachineId
443447
- string
444448
- Used only in `alone` mode, login for Crowdsec CAPI
@@ -518,6 +522,7 @@ http:
518522
updateIntervalSeconds: 60
519523
updateMaxFailure: 0
520524
defaultDecisionSeconds: 60
525+
remediationStatusCode: 403
521526
httpTimeoutSeconds: 10
522527
crowdsecMode: live
523528
crowdsecAppsecEnabled: false
@@ -527,7 +532,6 @@ http:
527532
crowdsecAppsecUnreachableBlock: true
528533
crowdsecAppsecBodyLimit: 10485760
529534
crowdsecLapiKey: privateKey-foo
530-
crowdsecLapiKeyFile: /etc/traefik/cs-privateKey-foo
531535
crowdsecLapiScheme: http
532536
crowdsecLapiHost: crowdsec:8080
533537
crowdsecLapiPath: "/"
@@ -556,22 +560,19 @@ http:
556560
...
557561
Q0veeNzBQXg1f/JxfeA39IDIX1kiCf71tGlT
558562
-----END CERTIFICATE-----
559-
crowdsecLapiTLSCertificateAuthorityFile: /etc/traefik/crowdsec-certs/ca.pem
560563
crowdsecLapiTLSCertificateBouncer: |-
561564
-----BEGIN CERTIFICATE-----
562565
MIIEHjCCAwagAwIBAgIUOBTs1eqkaAUcPplztUr2xRapvNAwDQYJKoZIhvcNAQEL
563566
...
564567
RaXAnYYUVRblS1jmePemh388hFxbmrpG2pITx8B5FMULqHoj11o2Rl0gSV6tHIHz
565568
N2U=
566569
-----END CERTIFICATE-----
567-
crowdsecLapiTLSCertificateBouncerFile: /etc/traefik/crowdsec-certs/bouncer.pem
568570
crowdsecLapiTLSCertificateBouncerKey: |-
569571
-----BEGIN RSA PRIVATE KEY-----
570572
MIIEogIBAAKCAQEAtYQnbJqifH+ZymePylDxGGLIuxzcAUU4/ajNj+qRAdI/Ux3d
571573
...
572574
ic5cDRo6/VD3CS3MYzyBcibaGaV34nr0G/pI+KEqkYChzk/PZRA=
573575
-----END RSA PRIVATE KEY-----
574-
crowdsecLapiTLSCertificateBouncerKeyFile: /etc/traefik/crowdsec-certs/bouncer-key.pem
575576
captchaProvider: hcaptcha
576577
captchaSiteKey: FIXME
577578
captchaSecretKey: FIXME
@@ -582,7 +583,7 @@ http:
582583
583584
#### Fill variable with value of file
584585
585-
`CrowdsecLapiTlsCertificateBouncerKey`, `CrowdsecLapiTlsCertificateBouncer`, `CrowdsecLapiTlsCertificateAuthority`, `CrowdsecCapiMachineId`, `CrowdsecCapiPassword`, `CrowdsecLapiKey`, `CaptchaSiteKey` and `CaptchaSecretKey` can be provided with the content as raw or through a file path that Traefik can read.
586+
`CrowdsecLapiTlsCertificateBouncerKey`, `CrowdsecLapiTlsCertificateBouncer`, `CrowdsecLapiTlsCertificateAuthority`, `CrowdsecCapiMachineId`, `CrowdsecCapiPassword`, `CrowdsecLapiKey`, `CaptchaSiteKey`, `CaptchaSecretKey` and `RedisCachePassword` can be provided with the content as raw or through a file path that Traefik can read.
586587
The file variable will be used as preference if both content and file are provided for the same variable.
587588

588589
Format is:

bouncer.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ type Bouncer struct {
7777
updateInterval int64
7878
updateMaxFailure int
7979
defaultDecisionTimeout int64
80+
remediationStatusCode int
8081
remediationCustomHeader string
8182
forwardedCustomHeader string
8283
crowdsecStreamRoute string
@@ -170,6 +171,7 @@ func New(_ context.Context, next http.Handler, config *configuration.Config, nam
170171
remediationCustomHeader: config.RemediationHeadersCustomName,
171172
forwardedCustomHeader: config.ForwardedHeadersCustomName,
172173
defaultDecisionTimeout: config.DefaultDecisionSeconds,
174+
remediationStatusCode: config.RemediationStatusCode,
173175
redisUnreachableBlock: config.RedisCacheUnreachableBlock,
174176
banTemplateString: banTemplateString,
175177
crowdsecStreamRoute: crowdsecStreamRoute,
@@ -355,11 +357,11 @@ func handleBanServeHTTP(bouncer *Bouncer, rw http.ResponseWriter) {
355357
rw.Header().Set(bouncer.remediationCustomHeader, "ban")
356358
}
357359
if bouncer.banTemplateString == "" {
358-
rw.WriteHeader(http.StatusForbidden)
360+
rw.WriteHeader(bouncer.remediationStatusCode)
359361
return
360362
}
361363
rw.Header().Set("Content-Type", "text/html; charset=utf-8")
362-
rw.WriteHeader(http.StatusForbidden)
364+
rw.WriteHeader(bouncer.remediationStatusCode)
363365
_, err := fmt.Fprint(rw, bouncer.banTemplateString)
364366
if err != nil {
365367
bouncer.log.Error("handleBanServeHTTP could not write template to ResponseWriter")

pkg/configuration/configuration.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type Config struct {
6868
UpdateIntervalSeconds int64 `json:"updateIntervalSeconds,omitempty"`
6969
UpdateMaxFailure int `json:"updateMaxFailure,omitempty"`
7070
DefaultDecisionSeconds int64 `json:"defaultDecisionSeconds,omitempty"`
71+
RemediationStatusCode int `json:"remediationStatusCode,omitempty"`
7172
HTTPTimeoutSeconds int64 `json:"httpTimeoutSeconds,omitempty"`
7273
RemediationHeadersCustomName string `json:"remediationHeadersCustomName,omitempty"`
7374
ForwardedHeadersCustomName string `json:"forwardedHeadersCustomName,omitempty"`
@@ -119,6 +120,7 @@ func New() *Config {
119120
UpdateIntervalSeconds: 60,
120121
UpdateMaxFailure: 0,
121122
DefaultDecisionSeconds: 60,
123+
RemediationStatusCode: http.StatusForbidden,
122124
HTTPTimeoutSeconds: 10,
123125
CaptchaProvider: "",
124126
CaptchaSiteKey: "",
@@ -355,6 +357,9 @@ func validateParamsRequired(config *Config) error {
355357
if config.CrowdsecAppsecBodyLimit < 0 {
356358
return errors.New("CrowdsecAppsecBodyLimit: cannot be less than 0")
357359
}
360+
if config.RemediationStatusCode < 100 || config.RemediationStatusCode >= 600 {
361+
return errors.New("RemediationStatusCode: cannot be less than 100 and more than 600")
362+
}
358363

359364
if !contains([]string{NoneMode, LiveMode, StreamMode, AloneMode, AppsecMode}, config.CrowdsecMode) {
360365
return errors.New("CrowdsecMode: must be one of 'none', 'live', 'stream', 'alone' or 'appsec'")

0 commit comments

Comments
 (0)