-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Add support for fetching k8s Ingress TLS data from secrets #2439
Changes from 21 commits
3892fb7
228362c
765fba4
1f96b88
bf648b8
4c0ba9e
18c5f9e
cc0b448
1c60f29
b0a7899
741163e
c13b21a
bce97e9
383b9e8
74616a4
e082818
3ecf770
d0a5c2f
4d156ec
d00111a
6ab8f21
1d2e121
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -333,6 +333,51 @@ echo "$(minikube ip) traefik-ui.minikube" | sudo tee -a /etc/hosts | |
|
||
We should now be able to visit [traefik-ui.minikube](http://traefik-ui.minikube) in the browser and view the Træfik Web UI. | ||
|
||
### Add a TLS Certificate to the Ingress | ||
|
||
!!! note | ||
For this example to work you need a TLS entrypoint. You don't have to provide a TLS certificate at this point. For more details see [here](/configuration/entrypoints/). | ||
|
||
To setup an HTTPS-protected ingress, you can leverage the TLS feature of the ingress resource. | ||
|
||
```yaml | ||
apiVersion: extensions/v1beta1 | ||
kind: Ingress | ||
metadata: | ||
name: traefik-web-ui | ||
namespace: kube-system | ||
annotations: | ||
kubernetes.io/ingress.class: traefik | ||
spec: | ||
rules: | ||
- host: traefik-ui.minikube | ||
http: | ||
paths: | ||
- backend: | ||
serviceName: traefik-web-ui | ||
servicePort: 80 | ||
tls: | ||
secretName: traefik-ui-tls-cert | ||
``` | ||
|
||
In addition to the modified ingress you need to provide the TLS certificate via a kubernetes secret in the same namespace as the ingress. The following two commands will generate a new certificate and create a secret containing the key and cert files. | ||
|
||
```shell | ||
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=traefik-ui.minikube" | ||
kubectl -n kube-system create secret tls traefik-ui-tls-cert --key=tls.key --cert=tls.crt | ||
``` | ||
|
||
If there are any errors while loading the TLS section of an ingress, the whole ingress will be skipped. | ||
|
||
!!! note | ||
The secret must have two entries named `tls.key`and `tls.crt`. See the [kubernetes documentation](https://kubernetes.io/docs/concepts/services-networking/ingress/#tls) for more details. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please add a note to explain that the certificates contained into the K8s secrets will be added to all the TLS entryPoints linked to the services which use them (if the label There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would the following ok with you? The TLS certificates will be added to all entrypoints defined by the ingress annotation Did you mean with 'note' the note formatting or just an addition to the documentation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes the sentence LGTM 👍 |
||
!!! note | ||
The TLS certificates will be added to all entrypoints defined by the ingress annotation `traefik.frontend.entryPoints`. If no such annotation is provided, the TLS certificates will be added to all TLS-enabled `defaultEntryPoints`. | ||
|
||
!!! note | ||
The field `hosts` in the TLS configuration is ignored. Instead, the domains provided by the certificate are used for this purpose. It is recommended to not use wildcard certificates as they will match globally. | ||
|
||
## Basic Authentication | ||
|
||
It's possible to add additional authentication annotations in the Ingress rule. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import ( | |
"github.com/containous/traefik/provider" | ||
"github.com/containous/traefik/provider/label" | ||
"github.com/containous/traefik/safe" | ||
"github.com/containous/traefik/tls" | ||
"github.com/containous/traefik/types" | ||
"k8s.io/client-go/pkg/api/v1" | ||
"k8s.io/client-go/pkg/apis/extensions/v1beta1" | ||
|
@@ -174,6 +175,13 @@ func (p *Provider) loadIngresses(k8sClient Client) (*types.Configuration, error) | |
continue | ||
} | ||
|
||
tlsConfigs, err := getTLSConfigurations(i, k8sClient) | ||
if err != nil { | ||
log.Errorf("Error configuring TLS for ingress %s/%s: %v", i.Namespace, i.Name, err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, my IDE is telling me that this line is supposedly missing code coverage. Are we possibly missing a test to validate that we actually skip the Ingress if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, you are right. This case isn't covered. |
||
continue | ||
} | ||
templateObjects.TLSConfiguration = append(templateObjects.TLSConfiguration, tlsConfigs...) | ||
|
||
for _, r := range i.Spec.Rules { | ||
if r.HTTP == nil { | ||
log.Warn("Error in ingress: HTTP is nil") | ||
|
@@ -441,6 +449,48 @@ func loadAuthCredentials(namespace, secretName string, k8sClient Client) ([]stri | |
return creds, nil | ||
} | ||
|
||
func getTLSConfigurations(ingress *v1beta1.Ingress, k8sClient Client) ([]*tls.Configuration, error) { | ||
var tlsConfigs []*tls.Configuration | ||
|
||
for _, t := range ingress.Spec.TLS { | ||
tlsSecret, exists, err := k8sClient.GetSecret(ingress.Namespace, t.SecretName) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to fetch secret %s/%s: %v", ingress.Namespace, t.SecretName, err) | ||
} | ||
if !exists { | ||
return nil, fmt.Errorf("secret %s/%s does not exist", ingress.Namespace, t.SecretName) | ||
} | ||
|
||
tlsCrtData, tlsCrtExists := tlsSecret.Data["tls.crt"] | ||
tlsKeyData, tlsKeyExists := tlsSecret.Data["tls.key"] | ||
|
||
var missingEntries []string | ||
if !tlsCrtExists { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WDYT about testing if both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That sounds reasonable. Would be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My suggestion would be |
||
missingEntries = append(missingEntries, "tls.crt") | ||
} | ||
if !tlsKeyExists { | ||
missingEntries = append(missingEntries, "tls.key") | ||
} | ||
if len(missingEntries) > 0 { | ||
return nil, fmt.Errorf("secret %s/%s is missing the following TLS data entries: %s", ingress.Namespace, t.SecretName, strings.Join(missingEntries, ", ")) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this a lot 👍 |
||
|
||
entryPoints := label.GetSliceStringValue(ingress.Annotations, label.TraefikFrontendEntryPoints) | ||
|
||
tlsConfig := &tls.Configuration{ | ||
EntryPoints: entryPoints, | ||
Certificate: &tls.Certificate{ | ||
CertFile: tls.FileOrContent(tlsCrtData), | ||
KeyFile: tls.FileOrContent(tlsKeyData), | ||
}, | ||
} | ||
|
||
tlsConfigs = append(tlsConfigs, tlsConfig) | ||
} | ||
|
||
return tlsConfigs, nil | ||
} | ||
|
||
func endpointPortNumber(servicePort v1.ServicePort, endpointPorts []v1.EndpointPort) int { | ||
if len(endpointPorts) > 0 { | ||
//name is optional if there is only one port | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent extension to the guide 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had no idea how to do something like this but I'm glad it is OK