Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace base64 encoding with random uuid #1215

Merged
merged 1 commit into from
Aug 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions controllers/nginx/pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,8 @@ var (

func buildWhitelistVariable(s string) string {
if _, ok := whitelistVarMap[s]; !ok {
str := base64.URLEncoding.EncodeToString([]byte(s))
whitelistVarMap[s] = strings.Replace(str, "=", "", -1)
whitelistVarMap[s] = buildRandomUUID()
}

return whitelistVarMap[s]
}

Expand All @@ -362,11 +360,11 @@ func buildRateLimitZones(input interface{}) []string {

for _, server := range servers {
for _, loc := range server.Locations {

whitelistVar := buildWhitelistVariable(loc.RateLimit.Name)
lrn := fmt.Sprintf("%v_%v", server.Hostname, loc.RateLimit.Name)
whitelistVar := buildWhitelistVariable(lrn)

if loc.RateLimit.Connections.Limit > 0 {
zone := fmt.Sprintf("limit_conn_zone $%s_limit zone=%v:%vm;",
zone := fmt.Sprintf("limit_conn_zone $limit_%s zone=%v:%vm;",
whitelistVar,
loc.RateLimit.Connections.Name,
loc.RateLimit.Connections.SharedSize)
Expand All @@ -376,7 +374,7 @@ func buildRateLimitZones(input interface{}) []string {
}

if loc.RateLimit.RPM.Limit > 0 {
zone := fmt.Sprintf("limit_req_zone $%s_limit zone=%v:%vm rate=%vr/m;",
zone := fmt.Sprintf("limit_req_zone $limit_%s zone=%v:%vm rate=%vr/m;",
whitelistVar,
loc.RateLimit.RPM.Name,
loc.RateLimit.RPM.SharedSize,
Expand All @@ -387,7 +385,7 @@ func buildRateLimitZones(input interface{}) []string {
}

if loc.RateLimit.RPS.Limit > 0 {
zone := fmt.Sprintf("limit_req_zone $%s_limit zone=%v:%vm rate=%vr/s;",
zone := fmt.Sprintf("limit_req_zone $limit_%s zone=%v:%vm rate=%vr/s;",
whitelistVar,
loc.RateLimit.RPS.Name,
loc.RateLimit.RPS.SharedSize,
Expand Down Expand Up @@ -468,7 +466,7 @@ func buildDenyVariable(a interface{}) string {
l := a.(string)

if _, ok := denyPathSlugMap[l]; !ok {
denyPathSlugMap[l] = uuid.New()
denyPathSlugMap[l] = buildRandomUUID()
}

return fmt.Sprintf("$deny_%v", denyPathSlugMap[l])
Expand Down Expand Up @@ -541,3 +539,9 @@ func buildAuthSignURL(input interface{}) string {

return fmt.Sprintf("%v&rd=$request_uri", s)
}

// buildRandomUUID return a random string to be used in the template
func buildRandomUUID() string {
s := uuid.New()
return strings.Replace(s, "-", "", -1)
}
10 changes: 8 additions & 2 deletions controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ http {

{{ if isLocationAllowed $location }}
{{ if gt (len $location.Whitelist.CIDR) 0 }}

# Deny for {{ print $server.Hostname $path }}
geo $the_real_ip {{ buildDenyVariable (print $server.Hostname "_" $path) }} {
default 1;

Expand All @@ -288,14 +290,18 @@ http {
}
{{ end }}
{{ end }}

{{ if ne $location.RateLimit.Name "" }}
geo ${{ buildWhitelistVariable $location.RateLimit.Name }}_whitelist {
# Ratelimit {{ $location.RateLimit.Name }}
{{ $rln := (print $server.Hostname "_" $location.RateLimit.Name) }}
geo $whitelist_{{ buildWhitelistVariable $rln }} {
default 0;
{{ range $ip := $location.RateLimit.Whitelist }}
{{ $ip }} 1;{{ end }}
}

map ${{ buildWhitelistVariable $location.RateLimit.Name }}_whitelist ${{ buildWhitelistVariable $location.RateLimit.Name }}_limit {
# Ratelimit {{ $location.RateLimit.Name }}
map $whitelist_{{ buildWhitelistVariable $rln }} $limit_{{ buildWhitelistVariable $rln }} {
0 {{ $cfg.LimitConnZoneVariable }};
1 "";
}
Expand Down