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

Fix dynamic variable name #809

Merged
merged 1 commit into from
Jun 2, 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
23 changes: 23 additions & 0 deletions controllers/nginx/pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ import (
"net"
"os"
"os/exec"
"regexp"
"strings"
text_template "text/template"

"k8s.io/apimachinery/pkg/util/sets"

"github.com/golang/glog"

"github.com/pborman/uuid"
"k8s.io/ingress/controllers/nginx/pkg/config"
"k8s.io/ingress/core/pkg/ingress"
ing_net "k8s.io/ingress/core/pkg/net"
Expand Down Expand Up @@ -136,6 +138,7 @@ var (
"buildResolvers": buildResolvers,
"isLocationAllowed": isLocationAllowed,
"buildLogFormatUpstream": buildLogFormatUpstream,
"buildDenyVariable": buildDenyVariable,
"getenv": os.Getenv,
"contains": strings.Contains,
"hasPrefix": strings.HasPrefix,
Expand Down Expand Up @@ -372,3 +375,23 @@ func isLocationAllowed(input interface{}) bool {

return loc.Denied == nil
}

var (
nonAlpha = regexp.MustCompile("[^a-zA-Z0-9]+")
denyPathSlugMap = map[string]string{}
)

// buildDenyVariable returns a nginx variable for a location in a
// server to be used in the whitelist check
// This method uses a unique id generator library to reduce the
// size of the string to be used as a variable in nginx to avoid
// issue with the size of the variable bucket size directive
func buildDenyVariable(a interface{}) string {
l := a.(string)

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

return fmt.Sprintf("$deny_%v", denyPathSlugMap[l])
}
8 changes: 8 additions & 0 deletions controllers/nginx/pkg/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,11 @@ func BenchmarkTemplateWithData(b *testing.B) {
ngxTpl.Write(dat)
}
}

func TestBuildDenyVariable(t *testing.T) {
a := buildDenyVariable("host1.example.com_/.well-known/acme-challenge")
b := buildDenyVariable("host1.example.com_/.well-known/acme-challenge")
if !reflect.DeepEqual(a, b) {
t.Errorf("Expected '%v' but returned '%v'", a, b)
}
}
4 changes: 2 additions & 2 deletions controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ http {

{{ if isLocationAllowed $location }}
{{ if gt (len $location.Whitelist.CIDR) 0 }}
geo $the_real_ip $deny_{{ $server.Hostname }}_{{ $path }} {
geo $the_real_ip {{ buildDenyVariable (print $server.Hostname "_" $path) }} {
default 1;

{{ range $ip := $location.Whitelist.CIDR }}
Expand Down Expand Up @@ -337,7 +337,7 @@ http {

{{ if isLocationAllowed $location }}
{{ if gt (len $location.Whitelist.CIDR) 0 }}
if ($deny_{{ $server.Hostname }}_{{ $path }}) {
if ({{ buildDenyVariable (print $server.Hostname "_" $path) }}) {
return 403;
}
{{ end }}
Expand Down