Skip to content

Commit

Permalink
Add per site ttl settings
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy5189 committed Jun 3, 2024
1 parent 86086a0 commit b237808
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type Config struct {
ExpiringDecisionTtlSeconds int `yaml:"expiring_decision_ttl_seconds"`
BlockIPTtlSeconds int `yaml:"block_ip_ttl_seconds"`
BlockSessionTtlSeconds int `yaml:"block_session_ttl_seconds"`
SitesToBlockIPTtlSeconds map[string]int `yaml:"sites_to_block_ip_ttl_seconds"`
SitesToBlockSessionTtlSeconds map[string]int `yaml:"sites_to_block_session_ttl_seconds"`
TooManyFailedChallengesIntervalSeconds int `yaml:"too_many_failed_challenges_interval_seconds"`
TooManyFailedChallengesThreshold int `yaml:"too_many_failed_challenges_threshold"`
PasswordCookieTtlSeconds int `yaml:"password_cookie_ttl_seconds"`
Expand Down
24 changes: 22 additions & 2 deletions internal/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,24 @@ func RunKafkaReader(
}
}

func getBlockIpTtl(config *Config, host string) (blockIpTtl int) {
blockIpTtl = config.BlockSessionTtlSeconds
if ttl, ok := config.SitesToBlockIPTtlSeconds[host]; ok {
log.Printf("KAFKA: found site-specific block_ip ttl %s %d\n", host, ttl)
blockIpTtl = ttl
}
return
}

func getBlockSessionTtl(config *Config, host string) (blockSessionTtl int) {
blockSessionTtl = config.BlockIPTtlSeconds
if ttl, ok := config.SitesToBlockSessionTtlSeconds[host]; ok {
log.Printf("KAFKA: found site-specific block_session ttl %s %d\n", host, ttl)
blockSessionTtl = ttl
}
return
}

func handleCommand(
config *Config,
command commandMessage,
Expand All @@ -155,13 +173,15 @@ func handleCommand(
handleIPCommand(config, command, decisionListsMutex, decisionLists, Challenge, config.ExpiringDecisionTtlSeconds)
break
case "block_ip":
handleIPCommand(config, command, decisionListsMutex, decisionLists, NginxBlock, config.BlockIPTtlSeconds)
ttl := getBlockIpTtl(config, command.Host)
handleIPCommand(config, command, decisionListsMutex, decisionLists, NginxBlock, ttl)
break
case "challenge_session":
handleSessionCommand(config, command, decisionListsMutex, decisionLists, Challenge, config.ExpiringDecisionTtlSeconds)
break
case "block_session":
handleSessionCommand(config, command, decisionListsMutex, decisionLists, NginxBlock, config.BlockSessionTtlSeconds)
ttl := getBlockSessionTtl(config, command.Host)
handleSessionCommand(config, command, decisionListsMutex, decisionLists, NginxBlock, ttl)
break
default:
log.Printf("KAFKA: unrecognized command name: %s\n", command.Name)
Expand Down

0 comments on commit b237808

Please sign in to comment.