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

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

No files matched your search

+5 -5
View File
@@ -25,27 +25,27 @@ func (p PortMap) IsAllowedHttps() bool {
return p.Scheme == "https" || p.ContainerPort == 443 return p.Scheme == "https" || p.ContainerPort == 443
} }
func getPortMaps(appName string) ([]PortMap, error) { func getPortMaps(appName string) (map[string]PortMap, error) {
portMaps := []PortMap{} portMaps := []PortMap{}
allowedMappings := map[string]PortMap{}
output, err := common.PlugnTriggerOutputAsString("ports-get", []string{appName, "json"}...) output, err := common.PlugnTriggerOutputAsString("ports-get", []string{appName, "json"}...)
if err != nil { if err != nil {
return portMaps, err return allowedMappings, err
} }
err = json.Unmarshal([]byte(output), &portMaps) err = json.Unmarshal([]byte(output), &portMaps)
if err != nil { if err != nil {
return portMaps, err return allowedMappings, err
} }
allowedMappings := []PortMap{}
for _, portMap := range portMaps { for _, portMap := range portMaps {
if !portMap.IsAllowedHttp() && !portMap.IsAllowedHttps() { if !portMap.IsAllowedHttp() && !portMap.IsAllowedHttps() {
// todo: log warning // todo: log warning
continue continue
} }
allowedMappings = append(allowedMappings, portMap) allowedMappings[portMap.String()] = portMap
} }
return allowedMappings, nil return allowedMappings, nil
+6 -5
View File
@@ -139,11 +139,12 @@ type ProcessCron struct {
} }
type ProcessPortMap struct { type ProcessPortMap struct {
ContainerPort int32 `yaml:"container_port"` ContainerPort int32 `yaml:"container_port"`
HostPort int32 `yaml:"host_port"` HostPort int32 `yaml:"host_port"`
Scheme string `yaml:"scheme"` Scheme string `yaml:"scheme"`
Protocol PortmapProtocol `yaml:"protocol"` Protocol PortmapProtocol `yaml:"protocol"`
Name string `yaml:"name"` Name string `yaml:"name"`
RedirectToHttps bool `yaml:"redirect_to_https"`
} }
type PortmapProtocol string type PortmapProtocol string
@@ -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
@@ -16,14 +16,20 @@ metadata:
namespace: {{ $.Values.global.namespace }} namespace: {{ $.Values.global.namespace }}
spec: spec:
entryPoints: entryPoints:
- web {{- if and $config.web.tls.enabled (eq $port_map.scheme "https") }}
{{- if and $config.web.tls (eq $port_map.scheme "https") }}
- websecure - websecure
{{- else }}
- web
{{- end }} {{- end }}
routes: routes:
{{- range $ddx, $domain := $config.web.domains }} {{- range $ddx, $domain := $config.web.domains }}
- kind: Rule - kind: Rule
match: Host(`{{ $domain }}`) 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: services:
- name: {{ $.Values.global.app_name }}-{{ $processName }} - name: {{ $.Values.global.app_name }}-{{ $processName }}
namespace: {{ $.Values.global.namespace }} namespace: {{ $.Values.global.namespace }}
@@ -31,7 +37,7 @@ spec:
port: {{ $port_map.name }} port: {{ $port_map.name }}
scheme: http scheme: http
{{- end }} {{- end }}
{{- if and $config.web.tls (eq $port_map.scheme "https") }} {{- if and $config.web.tls.enabled (eq $port_map.scheme "https") }}
tls: tls:
secretName: tls-{{ $.Values.global.app_name }}-{{ $processName }} secretName: tls-{{ $.Values.global.app_name }}-{{ $processName }}
{{- end }} {{- end }}
+17 -6
View File
@@ -372,18 +372,25 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e
IssuerName: issuerName, IssuerName: issuerName,
}, },
} }
processValues.ProcessType = ProcessType_Web processValues.ProcessType = ProcessType_Web
for _, portMap := range portMaps { for _, portMap := range portMaps {
protocol := PortmapProtocol_TCP protocol := PortmapProtocol_TCP
if portMap.Scheme == "udp" { if portMap.Scheme == "udp" {
protocol = PortmapProtocol_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{ processValues.Web.PortMaps = append(processValues.Web.PortMaps, ProcessPortMap{
ContainerPort: portMap.ContainerPort, ContainerPort: portMap.ContainerPort,
HostPort: portMap.HostPort, HostPort: portMap.HostPort,
Name: portMap.String(), Name: portMap.String(),
Protocol: protocol, Protocol: protocol,
Scheme: portMap.Scheme, RedirectToHttps: redirecToHttps,
Scheme: portMap.Scheme,
}) })
} }
@@ -393,7 +400,11 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e
values.Processes[processType] = processValues 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)) b, err := templates.ReadFile(fmt.Sprintf("templates/chart/%s.yaml", templateName))
if err != nil { if err != nil {
return fmt.Errorf("Error reading %s template: %w", templateName, err) return fmt.Errorf("Error reading %s template: %w", templateName, err)