Skip to content

Commit

Permalink
Fix config with long fqdn #112
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmoraisjr committed Mar 16, 2018
1 parent f31e25f commit 2d4dceb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
7 changes: 7 additions & 0 deletions pkg/controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package controller

import (
"bufio"
"crypto/md5"
"fmt"
"github.com/golang/glog"
"github.com/jcmoraisjr/haproxy-ingress/pkg/common/file"
"github.com/jcmoraisjr/haproxy-ingress/pkg/common/ingress"
Expand Down Expand Up @@ -170,6 +172,7 @@ func (cfg *haConfig) createHAProxyServers() {
UseHTTPS: server.SSLCertificate != "" || isDefaultServer,
Hostname: server.Hostname,
HostnameLabel: labelizeHostname(server.Hostname),
HostnameHash: hashHostname(server.Hostname),
SSLCertificate: server.SSLCertificate,
SSLPemChecksum: server.SSLPemChecksum,
RootLocation: haRootLocation,
Expand Down Expand Up @@ -244,6 +247,10 @@ func labelizeHostname(hostname string) string {
return re.ReplaceAllLiteralString(hostname, "_")
}

func hashHostname(hostname string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(hostname)))
}

// This could be improved creating a list of auth secrets (or even configMaps)
// on Ingress and saving usr(s)/pwd in auth.BasicDigest struct
func (cfg *haConfig) createUserlists() {
Expand Down
6 changes: 6 additions & 0 deletions pkg/controller/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ type template struct {
}

var funcMap = gotemplate.FuncMap{
"iif": func(q bool, o1, o2 string) string {
if q {
return o1
}
return o2
},
"backendHash": func(endpoint string) string {
return utils.BackendHash(endpoint)
},
Expand Down
1 change: 1 addition & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ type (
UseHTTPS bool `json:"useHTTPS"`
Hostname string `json:"hostname"`
HostnameLabel string `json:"hostnameLabel"`
HostnameHash string `json:"hostnameHash"`
SSLCertificate string `json:"sslCertificate"`
SSLPemChecksum string `json:"sslPemChecksum"`
RootLocation *HAProxyLocation `json:"defaultLocation"`
Expand Down
9 changes: 5 additions & 4 deletions rootfs/etc/haproxy/template/haproxy.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -235,34 +235,35 @@ frontend httpsfront
######
{{- range $server := $ing.HAProxies }}
{{- $host := $server.HostnameLabel }}
{{- $sock := iif (lt (len $host) 65) $host $server.HostnameHash }}
##
## {{ if $server.IsDefaultServer }}Default backend{{ else }}{{ $server.Hostname }}{{ end }}

{{- if or $server.UseHTTP $hasHTTPStoHTTP }}
backend httpback-{{ $host }}
mode http
server {{ $host }} unix@/var/run/haproxy-http-{{ $host }}.sock send-proxy-v2
server {{ $host }} unix@/var/run/haproxy-http-{{ $sock }}.sock send-proxy-v2
{{- end }}

{{- if $server.UseHTTPS }}
backend httpsback-{{ $host }}
mode tcp
server {{ $host }} unix@/var/run/haproxy-https-{{ $host }}.sock send-proxy-v2
server {{ $host }} unix@/var/run/haproxy-https-{{ $sock }}.sock send-proxy-v2
{{- end }}

{{- $sslconn := or $server.UseHTTPS $hasHTTPStoHTTP }}
{{- $authSSLCert := $server.CertificateAuth.AuthSSLCert }}
frontend httpfront-{{ $host }}
{{- if or $server.UseHTTP $hasHTTPStoHTTP }}
bind unix@/var/run/haproxy-http-{{ $host }}.sock accept-proxy
bind unix@/var/run/haproxy-http-{{ $sock }}.sock accept-proxy
{{- end }}

{{- if $server.UseHTTPS }}
# CRT PEM checksum: {{ $server.SSLPemChecksum }}
{{- if ne $authSSLCert.PemSHA "" }}
# CA PEM checksum: {{ $authSSLCert.PemSHA }}
{{- end }}
bind unix@/var/run/haproxy-https-{{ $host }}.sock ssl crt {{ $server.SSLCertificate }}{{ if ne $authSSLCert.CAFileName "" }} ca-file {{ $authSSLCert.CAFileName }} verify optional ca-ignore-err all crt-ignore-err all{{ end }} accept-proxy
bind unix@/var/run/haproxy-https-{{ $sock }}.sock ssl crt {{ $server.SSLCertificate }}{{ if ne $authSSLCert.CAFileName "" }} ca-file {{ $authSSLCert.CAFileName }} verify optional ca-ignore-err all crt-ignore-err all{{ end }} accept-proxy
{{- end }}
mode http

Expand Down

0 comments on commit 2d4dceb

Please sign in to comment.