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

Generate host acl correctly for * host #786

Merged
merged 1 commit into from Dec 29, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions apis/voyager/v1beta1/annotations.go
Expand Up @@ -335,8 +335,11 @@ func (r Ingress) EnableCORS() bool {
}

func (r Ingress) ForceServicePort() bool {
v, _ := meta.GetBool(r.Annotations, ForceServicePort)
return v
if r.LBType() == LBTypeNodePort {
v, _ := meta.GetBool(r.Annotations, ForceServicePort)
return v
}
return true
}

func (r Ingress) EnableHSTS() bool {
Expand Down
7 changes: 4 additions & 3 deletions hack/docker/voyager/templates/http-frontend.cfg
Expand Up @@ -73,10 +73,11 @@ frontend {{ .FrontendName }}
acl is_proxy_https hdr(X-Forwarded-Proto) https

{{ range $host := .Hosts }}
{{ if and (or (eq $.Port 80) (eq $.Port 443)) ( or $.ForceMatchServicePort (not $.NodePort)) }}
{{ if $host.Host }}acl host_acl_{{ $host.Host | acl_name }} {{ $host.Host | host_name }}{{ end }}
{{ with $conditions := (host_acls $host.Host $.Port $.NodePort $.ForceMatchServicePort ) }}
{{ range $cond := $conditions }}
{{ if $cond }}acl host_acl_{{ $host.Host | acl_name }} {{ $cond }}{{ end }}
{{ end }}
{{ end }}
{{ if $host.Host }}acl host_acl_{{ $host.Host | acl_name }} {{ $host.Host | host_name }}{{ if and (not $.ForceMatchServicePort) $.NodePort }}:{{ $.NodePort }}{{ else }}:{{ $.Port }}{{ end }}{{ end }}
{{ range $path := $host.Paths }}
{{ if $path.Path }}acl url_acl_{{ $host.Host | acl_name }}_{{ $path.Path | acl_name }} path_beg {{ $path.Path }}{{ end }}
{{ if $path.SSLRedirect }}
Expand Down
20 changes: 16 additions & 4 deletions pkg/haproxy/renderer.go
Expand Up @@ -16,7 +16,9 @@ func RenderConfig(data TemplateData) (string, error) {
if err := data.isValid(); err != nil {
return "", err
}
data.convertWildcardHostToEmpty()
data.canonicalize()
data.moveAcmePathToTop()

var buf bytes.Buffer
err := haproxyTemplate.ExecuteTemplate(&buf, "haproxy.cfg", data)
Expand All @@ -39,6 +41,18 @@ func (td TemplateData) String() string {
return string(data)
}

func (td *TemplateData) convertWildcardHostToEmpty() {
for i, svc := range td.HTTPService {
for j, host := range svc.Hosts {
if host.Host == `*` {
host.Host = ""
}
svc.Hosts[j] = host // remove the acme path
}
td.HTTPService[i] = svc
}
}

func (td *TemplateData) canonicalize() {
if td.DefaultBackend != nil {
td.DefaultBackend.canonicalize()
Expand Down Expand Up @@ -96,8 +110,6 @@ func (td *TemplateData) canonicalize() {
td.UserLists[i].canonicalize()
}
sort.Slice(td.UserLists, func(i, j int) bool { return td.UserLists[i].Name < td.UserLists[j].Name })

td.moveAcmePathToTop()
}

func (td *TemplateData) moveAcmePathToTop() {
Expand Down Expand Up @@ -191,14 +203,14 @@ func (td *TemplateData) isValid() error {
}

func hostName(host string) string {
if host == "" || host == `*` {
if host == "" {
return ""
}
return strings.ToLower(strings.TrimPrefix(host, "*."))
}

func hostRank(host string) int {
if host == "" || host == `*` {
if host == "" {
return 0
}
if strings.HasPrefix(host, "*") {
Expand Down
25 changes: 20 additions & 5 deletions pkg/haproxy/template.go
Expand Up @@ -35,11 +35,26 @@ func HeaderName(v string) string {
return v[:index]
}

func HostName(v string) string {
v = strings.TrimSpace(v)
if v == "" || v == `*` {
return ""
func HostACLs(host string, port int, nodePort int32, forceSvcPort bool) []string {
fmt.Printf("host=%v, port=%v, nodePort=%v, forceSvcPort=%v", host, port, nodePort, forceSvcPort)
var conditions []string
host = strings.TrimSpace(host)

if !forceSvcPort && nodePort > 0 {
conditions = append(conditions, hostMatcher(fmt.Sprintf("%s:%d", host, nodePort)))
} else if forceSvcPort && port > 0 {
if port != 80 && port != 443 { // non standard http ports
conditions = append(conditions, hostMatcher(fmt.Sprintf("%s:%d", host, port)))
} else if host != "" { // http or https
conditions = append(conditions, hostMatcher(host))
conditions = append(conditions, hostMatcher(fmt.Sprintf("%s:%d", host, port)))
}
}
fmt.Println(">>>>>>>> ", strings.Join(conditions, "|"))
return conditions
}

func hostMatcher(v string) string {
if strings.HasPrefix(v, "*") {
return "hdr_end(host) -i " + v[1:]
}
Expand All @@ -63,7 +78,7 @@ var (
funcMap = template.FuncMap{
"acl_name": ACLName,
"header_name": HeaderName,
"host_name": HostName,
"host_acls": HostACLs,
"backend_hash": BackendHash,
}

Expand Down