Skip to content

Commit 233d2a1

Browse files
authored
CAMEL-22581 - OAuth may validate token audience incorrectly (#19715)
1 parent 21c49c9 commit 233d2a1

File tree

13 files changed

+51
-211
lines changed

13 files changed

+51
-211
lines changed

components/camel-oauth/helm/README.md

Lines changed: 21 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Local Kubernetes Cluster
22

3-
To keep the entry barrier for Camel OAuth low, we initially deploy Keycloak as our Identity Provider on Docker Desktop Kubernetes.
3+
To keep the entry barrier for Camel OAuth low, we initially deploy Keycloak as our Identity Provider on Rancher Desktop Kubernetes.
44
This is a single node Kubernetes cluster running on localhost.
55

66
## Ingress with Traefik
@@ -9,45 +9,32 @@ Keycloak should only be accessed with transport layer security (TLS) in place. T
99
of exchanging privacy/security sensitive data over any channel.
1010

1111
Here we place Keycloak behind a TLS terminating proxy (Traefik). It has the advantage that any traffic
12-
(i.e. not only for Keycloak) can be secured at ingress level.
12+
(i.e. not only for Keycloak) can be secured at ingress level. Traefik should already be installed with Rancher Desktop.
1313

1414
https://doc.traefik.io/traefik/
1515

16-
```
17-
helm repo add traefik https://traefik.github.io/charts
18-
helm repo update
19-
helm install traefik traefik/traefik
20-
```
21-
22-
Once Traefik is installed, we create a Kubernetes TLS 'secret'.
2316

24-
In case you'd like to regenerate the TLS certificate and key, do this ...
25-
Also, a Java app that wants to access Keycloak over TLS, must trust that certificate.
17+
Create and install TLS edge certificate
2618

2719
```
28-
# Generate TLS Certificate
29-
openssl req -x509 -newkey rsa:4096 -keyout ./helm/etc/cluster.key -out ./helm/etc/cluster.crt -days 365 -nodes -config ./helm/etc/san.cnf
20+
brew install mkcert nss
3021
31-
# Show Certificate
32-
cat ./helm/etc/cluster.crt | openssl x509 -noout -text
22+
# Make sure the mkcert root CA is trusted
23+
mkcert --install
3324
34-
# Import TLS Certificate to Java Keystore (i.e. trust the certificate)
35-
sudo keytool -import -alias camel-oauth -file ./helm/etc/cluster.crt -keystore $JAVA_HOME/lib/security/cacerts -storepass changeit
25+
mkcert "localtest.me" "*.localtest.me"
26+
mkdir -p helm/tls && mv localtest.* helm/tls
3627
37-
# Remove TLS Certificate from Java Keystore
38-
sudo keytool -delete -alias camel-oauth -keystore $JAVA_HOME/lib/security/cacerts -storepass changeit
28+
kubectl delete secret edge-tls --ignore-not-found=true
29+
kubectl create secret tls edge-tls \
30+
--cert=helm/tls/localtest.me+1.pem \
31+
--key=helm/tls/localtest.me+1-key.pem
3932
40-
# Trust this cert on macOS
41-
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ./helm/etc/cluster.crt
42-
43-
# Trust this cert on Rock9
44-
sudo cp ./helm/etc/cluster.crt /etc/pki/ca-trust/source/anchors/ && sudo update-ca-trust
45-
```
46-
47-
Once we have the TLS certificate, we can install the TLS secret like this ...
48-
49-
```
50-
helm upgrade --install traefik-tls ./helm -f ./helm/values-traefik-tls.yaml
33+
# The above shold also install the root cert with the Java system truststore. This is matter of mkcert finding the correct JRE home.
34+
# In case it does not, you may need to import the mkcert rootCA.pem manually to the Java truststore.
35+
keytool -delete -cacerts -alias mkcert-root -storepass changeit
36+
keytool -importcert -cacerts -alias mkcert-root -storepass changeit -noprompt \
37+
-file "$(mkcert -CAROOT)/rootCA.pem"
5138
```
5239

5340
... and verify that TLS access is working
@@ -56,13 +43,12 @@ helm upgrade --install traefik-tls ./helm -f ./helm/values-traefik-tls.yaml
5643
helm upgrade --install whoami ./helm -f ./helm/values-whoami.yaml
5744
```
5845

59-
https://example.local/who
46+
https://localtest.me/who
6047

61-
Note, the domains `example.local` and `keycloak.local` are mapped to an actual IP in `/etc/hosts`.
6248

6349
## Installing Keycloak
6450

65-
Using Helm, we can install a pre-configured instance of Keycloak behind Traefik like this ...
51+
Using Helm, we can also install a pre-configured instance of Keycloak behind Traefik like this ...
6652

6753
```
6854
helm upgrade --install keycloak ./helm -f ./helm/values-keycloak.yaml \
@@ -72,7 +58,7 @@ helm upgrade --install keycloak ./helm -f ./helm/values-keycloak.yaml \
7258
helm uninstall keycloak
7359
```
7460

75-
https://keycloak.local/kc
61+
https://oauth.localtest.me
7662

7763
Admin: admin/admin
7864
User: alice/alice
@@ -86,7 +72,7 @@ Note, in case you see `NoSuchAlgorithmException: RSA-OAEP`, we can disable that
8672
Create realm 'camel' if not already imported
8773

8874
```
89-
kcadm config credentials --server https://keycloak.local/kc --realm master --user admin --password admin
75+
kcadm config credentials --server https://oauth.localtest.me/kc --realm master --user admin --password admin
9076
9177
kcadm create realms -s realm=camel -s enabled=true
9278
@@ -137,59 +123,6 @@ helm upgrade --install kafka ./helm -f ./helm/values-kafka.yaml \
137123
helm uninstall kafka
138124
```
139125

140-
# Remote Kubernetes Cluster
141-
142-
Next level up, we run a single node cluster that we access remotely - [K3S](https://k3s.io/) is an excellent choice for that.
143-
144-
Once K3s is running, we can use [Lens](https://k8slens.dev/), [kubectx](https://github.com/ahmetb/kubectx) or plain `kubectl config` for context switching to k3s.
145-
146-
As above, we need to install the TLS secret
147-
148-
```
149-
helm upgrade --install traefik-tls ./helm -f ./helm/values-traefik-tls.yaml
150-
```
151-
152-
... and then Keycloak
153-
154-
```
155-
helm upgrade --install keycloak ./helm -f ./helm/values-keycloak.yaml \
156-
&& kubectl wait --for=condition=Ready pod -l app.kubernetes.io/name=keycloak --timeout=20s \
157-
&& kubectl logs --tail 400 -f -l app.kubernetes.io/name=keycloak
158-
159-
helm uninstall keycloak
160-
```
161-
162-
https://keycloak.k3s/kc
163-
164-
## Modifying CoreDNS
165-
166-
Unlike DockerDesktop Kubernetes, pods deployed on K3S do not see /etc/hosts from the host system. Instead, K3S uses
167-
CoreDNS to resolve host names, which we can use to add the required mapping.
168-
169-
```
170-
kubectl -n kube-system edit configmap coredns
171-
172-
Corefile: |
173-
.:53 {
174-
...
175-
hosts /etc/coredns/NodeHosts {
176-
<host-ip> keycloak.k3s
177-
ttl 60
178-
reload 15s
179-
fallthrough
180-
}
181-
```
182-
183-
Please let us know, when there is a better way to provide a host mapping such that traffic goes through the Keycloak
184-
IngressRoute, which references our custom TLS certificate.
185-
186-
## Private Registry
187-
188-
Most of our examples reference images that are deployed to the private registry of the given cluster (i.e. these images
189-
are not available in public registries). [camel-cloud-examples](https://github.com/tdiesler/camel-cloud-examples/tree/main)
190-
provides [Ansible playbooks](https://github.com/tdiesler/camel-cloud-examples/tree/main/ansible) that show how ton install
191-
a private registry in K3S. There is also some documentation in K3S [directly](https://docs.k3s.io/installation/private-registry).
192-
193126
# OpenShift
194127

195128
First, we create a new project on the OpenShift cluster

components/camel-oauth/helm/etc/camel-realm.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,12 @@
168168
"consentRequired" : false,
169169
"fullScopeAllowed" : false,
170170
"redirectUris": [
171+
"http://127.0.0.1:8080/auth",
171172
"https://example.local/auth",
172173
"https://example.k3s/auth"
173174
],
174175
"attributes": {
175-
"post.logout.redirect.uris": "https://example.local/##https://example.k3s/"
176+
"post.logout.redirect.uris": "http://127.0.0.1:8080/##https://example.local/##https://example.k3s/"
176177
}
177178
},
178179
{

components/camel-oauth/helm/etc/san.cnf

Lines changed: 0 additions & 25 deletions
This file was deleted.

components/camel-oauth/helm/templates/keycloak.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ spec:
111111
entryPoints:
112112
- websecure
113113
routes:
114-
- match: PathPrefix(`/kc`)
114+
- match: Host("{{ .apiHost }}")
115115
kind: Rule
116116
services:
117117
- name: keycloak
118118
port: 8080
119119
tls:
120-
secretName: traefik-tls
120+
secretName: edge-tls
121121
---
122122
{{- end }} # environment == 'k8s'
123123

components/camel-oauth/helm/templates/traefik-tls.yaml

Lines changed: 0 additions & 37 deletions
This file was deleted.

components/camel-oauth/helm/templates/whoami.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ spec:
7979
middlewares:
8080
- name: strip-prefix-who
8181
tls:
82-
secretName: traefik-tls
82+
secretName: edge-tls
8383

8484
---
8585
apiVersion: traefik.io/v1alpha1

components/camel-oauth/helm/values-keycloak.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ environment: k8s
1919

2020
deployments:
2121
keycloak:
22-
version: 26.1.2
22+
apiHost: "oauth.localtest.me"
23+
version: 26.4.2
2324

components/camel-oauth/helm/values-traefik-tls.yaml

Lines changed: 0 additions & 25 deletions
This file was deleted.

components/camel-oauth/src/main/docs/oauth-component.adoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ For details see the https://openid.net/specs/openid-connect-core-1_0.html[OIDC 1
6363
|===
6464
|Name |Description
6565

66-
|`camel.oauth.base-uri` |The base URL to the identity provider (e.g. https://keycloak.local/kc/realms/camel)
66+
|`camel.oauth.base-uri` |The base URL to the identity provider (e.g. https://oauth.localtest.me/kc/realms/camel)
6767

6868
|`camel.oauth.redirect-uri` |Valid URI pattern a browser can redirect to after a successful login (e.g. http://127.0.0.1:8080/auth). Must be registered with the identity provider.
6969

@@ -88,7 +88,7 @@ For details see the https://datatracker.ietf.org/doc/html/rfc6749#section-4.4[OA
8888
|===
8989
|Name |Description
9090

91-
|`camel.oauth.base-uri` |The base URL to the identity provider (e.g. https://keycloak.local/kc/realms/camel)
91+
|`camel.oauth.base-uri` |The base URL to the identity provider (e.g. https://oauth.localtest.me/kc/realms/camel)
9292

9393
|`camel.oauth.client-id` |The client identifier registered with the identity provider.
9494

@@ -102,7 +102,7 @@ Naturally, we want all communication between camel and the identity provider to
102102
[source,shell]
103103
----
104104
# Fetch the certificate from the IdP endpoint
105-
openssl s_client -connect keycloak.local:443 | openssl x509 > cluster.crt
105+
openssl s_client -connect oauth.localtest.me:443 | openssl x509 > cluster.crt
106106
107107
# Import certificate to Java Keystore (i.e. trust the certificate)
108108
sudo keytool -import -alias keycloak -file cluster.crt -keystore $JAVA_HOME/lib/security/cacerts -storepass changeit
@@ -150,13 +150,13 @@ For example ...
150150
----
151151
k8s-fetch-cert:
152152
@mkdir -p tls
153-
@echo -n | openssl s_client -connect keycloak.local:443 | openssl x509 > tls/cluster.crt
153+
@echo -n | openssl s_client -connect oauth.localtest.me:443 | openssl x509 > tls/cluster.crt
154154
155155
k8s-export: k8s-fetch-cert
156156
@$(CAMEL_CMD) kubernetes export platform-http-files/* tls/* \
157157
--dep=org.apache.camel:camel-oauth:4.16.0-SNAPSHOT \
158158
--gav=examples:platform-http-oauth:1.0.0 \
159-
--property=camel.oauth.base-uri=https://keycloak.local/kc/realms/camel \
159+
--property=camel.oauth.base-uri=https://oauth.localtest.me/kc/realms/camel \
160160
--property=camel.oauth.redirect-uri=http://127.0.0.1:8080/auth \
161161
--property=camel.oauth.logout.redirect-uri=http://127.0.0.1:8080/ \
162162
--property=camel.oauth.client-id=camel-client \

0 commit comments

Comments
 (0)