|  | 
|  | 1 | +package waf | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"fmt" | 
|  | 5 | +	"text/template" | 
|  | 6 | + | 
|  | 7 | +	ngfAPI "github.com/nginx/nginx-gateway-fabric/apis/v1alpha1" | 
|  | 8 | +	"github.com/nginx/nginx-gateway-fabric/internal/controller/nginx/config/http" | 
|  | 9 | +	"github.com/nginx/nginx-gateway-fabric/internal/controller/nginx/config/policies" | 
|  | 10 | +	"github.com/nginx/nginx-gateway-fabric/internal/framework/helpers" | 
|  | 11 | +) | 
|  | 12 | + | 
|  | 13 | +var tmpl = template.Must(template.New("waf policy").Parse(wafTemplate)) | 
|  | 14 | + | 
|  | 15 | +const wafTemplate = ` | 
|  | 16 | +{{- if .BundlePath }} | 
|  | 17 | +app_protect_enable on; | 
|  | 18 | +app_protect_policy_file "{{ .BundlePath }}"; | 
|  | 19 | +{{- end }} | 
|  | 20 | +{{- if .SecurityLogs }} | 
|  | 21 | +app_protect_security_log_enable on; | 
|  | 22 | +{{- range .SecurityLogs }} | 
|  | 23 | +{{- if .LogProfile }} | 
|  | 24 | +app_protect_security_log "{{ .LogProfile }}" {{ .Destination }}; | 
|  | 25 | +{{- else if .LogProfileBundlePath }} | 
|  | 26 | +app_protect_security_log "{{ .LogProfileBundlePath }}" {{ .Destination }}; | 
|  | 27 | +{{- end }} | 
|  | 28 | +{{- end }} | 
|  | 29 | +{{- end }} | 
|  | 30 | +` | 
|  | 31 | + | 
|  | 32 | +// Generator generates nginx configuration based on a WAF policy. | 
|  | 33 | +type Generator struct { | 
|  | 34 | +	policies.UnimplementedGenerator | 
|  | 35 | +} | 
|  | 36 | + | 
|  | 37 | +// NewGenerator returns a new instance of Generator. | 
|  | 38 | +func NewGenerator() *Generator { | 
|  | 39 | +	return &Generator{} | 
|  | 40 | +} | 
|  | 41 | + | 
|  | 42 | +// GenerateForServer generates policy configuration for the server block. | 
|  | 43 | +func (g Generator) GenerateForServer(pols []policies.Policy, _ http.Server) policies.GenerateResultFiles { | 
|  | 44 | +	return generate(pols) | 
|  | 45 | +} | 
|  | 46 | + | 
|  | 47 | +// GenerateForLocation generates policy configuration for a normal location block. | 
|  | 48 | +func (g Generator) GenerateForLocation(pols []policies.Policy, _ http.Location) policies.GenerateResultFiles { | 
|  | 49 | +	return generate(pols) | 
|  | 50 | +} | 
|  | 51 | + | 
|  | 52 | +func generate(pols []policies.Policy) policies.GenerateResultFiles { | 
|  | 53 | +	files := make(policies.GenerateResultFiles, 0, len(pols)) | 
|  | 54 | + | 
|  | 55 | +	for _, pol := range pols { | 
|  | 56 | +		wp, ok := pol.(*ngfAPI.WAFPolicy) | 
|  | 57 | +		if !ok { | 
|  | 58 | +			continue | 
|  | 59 | +		} | 
|  | 60 | + | 
|  | 61 | +		fields := map[string]any{} | 
|  | 62 | + | 
|  | 63 | +		if wp.Spec.PolicySource != nil && wp.Spec.PolicySource.FileLocation != "" { | 
|  | 64 | +			fileLocation := wp.Spec.PolicySource.FileLocation | 
|  | 65 | +			bundleName := helpers.ToSafeFileName(fileLocation) | 
|  | 66 | +			bundlePath := fmt.Sprintf("%s/%s.tgz", "/etc/app_protect/bundles", bundleName) | 
|  | 67 | +			fields["BundlePath"] = bundlePath | 
|  | 68 | +		} | 
|  | 69 | + | 
|  | 70 | +		if len(wp.Spec.SecurityLogs) > 0 { | 
|  | 71 | +			securityLogs := make([]map[string]string, 0, len(wp.Spec.SecurityLogs)) | 
|  | 72 | + | 
|  | 73 | +			for _, secLog := range wp.Spec.SecurityLogs { | 
|  | 74 | +				logEntry := map[string]string{} | 
|  | 75 | + | 
|  | 76 | +				if secLog.LogProfile != nil { | 
|  | 77 | +					logEntry["LogProfile"] = string(*secLog.LogProfile) | 
|  | 78 | +				} | 
|  | 79 | + | 
|  | 80 | +				if secLog.LogProfileBundle != nil && secLog.LogProfileBundle.FileLocation != "" { | 
|  | 81 | +					bundleName := helpers.ToSafeFileName(secLog.LogProfileBundle.FileLocation) | 
|  | 82 | +					bundlePath := fmt.Sprintf("%s/%s.tgz", "/etc/app_protect/bundles", bundleName) | 
|  | 83 | +					logEntry["LogProfileBundlePath"] = bundlePath | 
|  | 84 | +				} | 
|  | 85 | + | 
|  | 86 | +				destination := formatSecurityLogDestination(secLog.Destination) | 
|  | 87 | +				logEntry["Destination"] = destination | 
|  | 88 | + | 
|  | 89 | +				securityLogs = append(securityLogs, logEntry) | 
|  | 90 | +			} | 
|  | 91 | + | 
|  | 92 | +			fields["SecurityLogs"] = securityLogs | 
|  | 93 | +		} | 
|  | 94 | + | 
|  | 95 | +		files = append(files, policies.File{ | 
|  | 96 | +			Name:    fmt.Sprintf("WafPolicy_%s_%s.conf", wp.Namespace, wp.Name), | 
|  | 97 | +			Content: helpers.MustExecuteTemplate(tmpl, fields), | 
|  | 98 | +		}) | 
|  | 99 | +	} | 
|  | 100 | + | 
|  | 101 | +	return files | 
|  | 102 | +} | 
|  | 103 | + | 
|  | 104 | +func formatSecurityLogDestination(dest ngfAPI.SecurityLogDestination) string { | 
|  | 105 | +	switch dest.Type { | 
|  | 106 | +	case ngfAPI.SecurityLogDestinationTypeStderr: | 
|  | 107 | +		return "stderr" | 
|  | 108 | +	case ngfAPI.SecurityLogDestinationTypeFile: | 
|  | 109 | +		if dest.File != nil { | 
|  | 110 | +			return dest.File.Path | 
|  | 111 | +		} | 
|  | 112 | +		return "stderr" | 
|  | 113 | +	case ngfAPI.SecurityLogDestinationTypeSyslog: | 
|  | 114 | +		if dest.Syslog != nil { | 
|  | 115 | +			return fmt.Sprintf("syslog:server=%s", dest.Syslog.Server) | 
|  | 116 | +		} | 
|  | 117 | +		return "stderr" | 
|  | 118 | +	default: | 
|  | 119 | +		return "stderr" | 
|  | 120 | +	} | 
|  | 121 | +} | 
0 commit comments