feat: add redirect to https when both an http and https port mapping match

This commit is contained in:
Jose Diaz-Gonzalez
2024-01-23 09:45:20 -05:00
parent 56ee889295
commit 6707e5f1eb
5 changed files with 52 additions and 19 deletions

View File

@@ -25,27 +25,27 @@ func (p PortMap) IsAllowedHttps() bool {
return p.Scheme == "https" || p.ContainerPort == 443
}
func getPortMaps(appName string) ([]PortMap, error) {
func getPortMaps(appName string) (map[string]PortMap, error) {
portMaps := []PortMap{}
allowedMappings := map[string]PortMap{}
output, err := common.PlugnTriggerOutputAsString("ports-get", []string{appName, "json"}...)
if err != nil {
return portMaps, err
return allowedMappings, err
}
err = json.Unmarshal([]byte(output), &portMaps)
if err != nil {
return portMaps, err
return allowedMappings, err
}
allowedMappings := []PortMap{}
for _, portMap := range portMaps {
if !portMap.IsAllowedHttp() && !portMap.IsAllowedHttps() {
// todo: log warning
continue
}
allowedMappings = append(allowedMappings, portMap)
allowedMappings[portMap.String()] = portMap
}
return allowedMappings, nil

View File

@@ -139,11 +139,12 @@ type ProcessCron struct {
}
type ProcessPortMap struct {
ContainerPort int32 `yaml:"container_port"`
HostPort int32 `yaml:"host_port"`
Scheme string `yaml:"scheme"`
Protocol PortmapProtocol `yaml:"protocol"`
Name string `yaml:"name"`
ContainerPort int32 `yaml:"container_port"`
HostPort int32 `yaml:"host_port"`
Scheme string `yaml:"scheme"`
Protocol PortmapProtocol `yaml:"protocol"`
Name string `yaml:"name"`
RedirectToHttps bool `yaml:"redirect_to_https"`
}
type PortmapProtocol string

View File

@@ -0,0 +1,15 @@
---
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
annotations:
dokku.com/managed: "true"
labels:
app.kubernetes.io/instance: {{ $.Values.global.app_name }}-redirect-to-https
app.kubernetes.io/part-of: {{ $.Values.global.app_name }}
name: {{ $.Values.global.app_name}}-redirect-to-https
namespace: {{ $.Values.global.namespace }}
spec:
redirectScheme:
scheme: https
permanent: true

View File

@@ -16,14 +16,20 @@ metadata:
namespace: {{ $.Values.global.namespace }}
spec:
entryPoints:
- web
{{- if and $config.web.tls (eq $port_map.scheme "https") }}
{{- if and $config.web.tls.enabled (eq $port_map.scheme "https") }}
- websecure
{{- else }}
- web
{{- end }}
routes:
{{- range $ddx, $domain := $config.web.domains }}
- kind: Rule
match: Host(`{{ $domain }}`)
{{- if and $config.web.tls.enabled (and (eq $port_map.scheme "http") $port_map.redirect_to_https) }}
middlewares:
- name: {{ $.Values.global.app_name}}-redirect-to-https
namespace: traefik
{{- end }}
services:
- name: {{ $.Values.global.app_name }}-{{ $processName }}
namespace: {{ $.Values.global.namespace }}
@@ -31,7 +37,7 @@ spec:
port: {{ $port_map.name }}
scheme: http
{{- end }}
{{- if and $config.web.tls (eq $port_map.scheme "https") }}
{{- if and $config.web.tls.enabled (eq $port_map.scheme "https") }}
tls:
secretName: tls-{{ $.Values.global.app_name }}-{{ $processName }}
{{- end }}

View File

@@ -372,18 +372,25 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e
IssuerName: issuerName,
},
}
processValues.ProcessType = ProcessType_Web
for _, portMap := range portMaps {
protocol := PortmapProtocol_TCP
if portMap.Scheme == "udp" {
protocol = PortmapProtocol_UDP
}
redirecToHttps := false
if _, ok := portMaps[fmt.Sprintf("https-443-%d", portMap.ContainerPort)]; ok && portMap.Scheme == "http" {
redirecToHttps = true
}
processValues.Web.PortMaps = append(processValues.Web.PortMaps, ProcessPortMap{
ContainerPort: portMap.ContainerPort,
HostPort: portMap.HostPort,
Name: portMap.String(),
Protocol: protocol,
Scheme: portMap.Scheme,
ContainerPort: portMap.ContainerPort,
HostPort: portMap.HostPort,
Name: portMap.String(),
Protocol: protocol,
RedirectToHttps: redirecToHttps,
Scheme: portMap.Scheme,
})
}
@@ -393,7 +400,11 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e
values.Processes[processType] = processValues
for _, templateName := range []string{"deployment", "service", "certificate", "ingress-route"} {
templateFiles := []string{"deployment"}
if processType == "web" {
templateFiles = append(templateFiles, "service", "certificate", "ingress-route", "https-redirect-middleware")
}
for _, templateName := range templateFiles {
b, err := templates.ReadFile(fmt.Sprintf("templates/chart/%s.yaml", templateName))
if err != nil {
return fmt.Errorf("Error reading %s template: %w", templateName, err)