Only load modsecurity_module when ModSec is active
This commit is contained in:
parent
28793092e7
commit
c11583dc5f
3 changed files with 99 additions and 8 deletions
|
|
@ -170,6 +170,7 @@ var (
|
|||
"buildCustomErrorDeps": buildCustomErrorDeps,
|
||||
"opentracingPropagateContext": opentracingPropagateContext,
|
||||
"buildCustomErrorLocationsPerServer": buildCustomErrorLocationsPerServer,
|
||||
"shouldLoadModSecurityModule": shouldLoadModSecurityModule,
|
||||
}
|
||||
)
|
||||
|
||||
|
|
@ -1043,3 +1044,37 @@ func opentracingPropagateContext(loc interface{}) string {
|
|||
|
||||
return "opentracing_propagate_context"
|
||||
}
|
||||
|
||||
// shouldLoadModSecurityModule determines whether or not the ModSecurity module needs to be loaded.
|
||||
// First, it checks if `enable-modsecurity` is set in the ConfigMap. If it is not, it iterates over all locations to
|
||||
// check if ModSecurity is enabled by the annotation `nginx.ingress.kubernetes.io/enable-modsecurity`.
|
||||
func shouldLoadModSecurityModule(c interface{}, s interface{}) bool {
|
||||
cfg, ok := c.(config.Configuration)
|
||||
if !ok {
|
||||
klog.Errorf("expected a 'config.Configuration' type but %T was returned", c)
|
||||
return false
|
||||
}
|
||||
|
||||
servers, ok := s.([]*ingress.Server)
|
||||
if !ok {
|
||||
klog.Errorf("expected an '[]*ingress.Server' type but %T was returned", s)
|
||||
return false
|
||||
}
|
||||
|
||||
// Determine if ModSecurity is enabled globally.
|
||||
if cfg.EnableModsecurity {
|
||||
return true
|
||||
}
|
||||
|
||||
// If ModSecurity is not enabled globally, check if any location has it enabled via annotation.
|
||||
for _, server := range servers {
|
||||
for _, location := range server.Locations {
|
||||
if location.ModSecurity.Enable {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Not enabled globally nor via annotation on a location, no need to load the module.
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue