-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ffdeb3
commit 4cc4402
Showing
23 changed files
with
1,153 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# JWT | ||
|
||
In this example, we deploy a web application, configure load balancing for it via a VirtualServer, and apply a JWT policy. | ||
|
||
## Prerequisites | ||
|
||
1. Follow the [installation](https://docs.nginx.com/nginx-ingress-controller/installation/installation-with-manifests/) instructions to deploy the Ingress Controller. | ||
1. Save the public IP address of the Ingress Controller into a shell variable: | ||
``` | ||
$ IC_IP=XXX.YYY.ZZZ.III | ||
``` | ||
1. Save the HTTP port of the Ingress Controller into a shell variable: | ||
``` | ||
$ IC_HTTP_PORT=<port number> | ||
``` | ||
## Step 1 - Deploy a Web Application | ||
Create the application deployment and service: | ||
``` | ||
$ kubectl apply -f webapp.yaml | ||
``` | ||
## Step 2 - Deploy the JWK Secret | ||
Create a secret with the name `jwk-secret` that will be used for JWT validation: | ||
``` | ||
$ kubectl apply -f jwk-secret.yaml | ||
``` | ||
## Step 3 - Deploy the JWT Policy | ||
Create a policy with the name `jwt-policy` that references the secret from the previous step and only permits requests to our web application that contain a valid JWT: | ||
``` | ||
$ kubectl apply -f jwt.yaml | ||
``` | ||
## Step 3 - Configure Load Balancing | ||
Create a VirtualServer resource for the web application: | ||
``` | ||
$ kubectl apply -f virtual-server.yaml | ||
``` | ||
Note that the VirtualServer references the policy `jwt-policy` created in Step 3. | ||
## Step 4 - Test the Configuration | ||
If you attempt to access the application without providing a valid JWT, NGINX will reject your requests for that VirtualServer: | ||
``` | ||
$ curl --resolve webapp.example.com:$IC_HTTP_PORT:$IC_IP http://webapp.example.com:$IC_HTTP_PORT/ | ||
<html> | ||
<head><title>401 Authorization Required</title></head> | ||
<body> | ||
<center><h1>401 Authorization Required</h1></center> | ||
<hr><center>nginx/1.19.1</center> | ||
</body> | ||
</html> | ||
``` | ||
If you provide a valid JWT, your request will succeed: | ||
``` | ||
$ curl --resolve webapp.example.com:$IC_HTTP_PORT:$IC_IP http://webapp.example.com:$IC_HTTP_PORT/ -H "token: `cat token.jwt`" | ||
Server address: 172.17.0.3:8080 | ||
Server name: webapp-7c6d448df9-lcrx6 | ||
Date: 10/Sep/2020:18:20:03 +0000 | ||
URI: / | ||
Request ID: db2c07ce640755ccbe9f666d16f85620 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Secret | ||
metadata: | ||
name: jwk-secret | ||
apiVersion: v1 | ||
data: | ||
jwk: eyJrZXlzIjoKICAgIFt7CiAgICAgICAgImsiOiJabUZ1ZEdGemRHbGphbmQwIiwKICAgICAgICAia3R5Ijoib2N0IiwKICAgICAgICAia2lkIjoiMDAwMSIKICAgIH1dCn0K |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
apiVersion: k8s.nginx.org/v1alpha1 | ||
kind: Policy | ||
metadata: | ||
name: jwt-policy | ||
spec: | ||
jwt: | ||
realm: MyProductAPI | ||
secret: jwk-secret | ||
token: $http_token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6IjAwMDEifQ.eyJuYW1lIjoiUXVvdGF0aW9uIFN5c3RlbSIsInN1YiI6InF1b3RlcyIsImlzcyI6Ik15IEFQSSBHYXRld2F5In0.ggVOHYnVFB8GVPE-VOIo3jD71gTkLffAY0hQOGXPL2I |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
apiVersion: k8s.nginx.org/v1 | ||
kind: VirtualServer | ||
metadata: | ||
name: webapp | ||
spec: | ||
host: webapp.example.com | ||
policies: | ||
- name: jwt-policy | ||
upstreams: | ||
- name: webapp | ||
service: webapp-svc | ||
port: 80 | ||
routes: | ||
- path: / | ||
action: | ||
pass: webapp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: webapp | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: webapp | ||
template: | ||
metadata: | ||
labels: | ||
app: webapp | ||
spec: | ||
containers: | ||
- name: webapp | ||
image: nginxdemos/nginx-hello:plain-text | ||
ports: | ||
- containerPort: 8080 | ||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: webapp-svc | ||
spec: | ||
ports: | ||
- port: 80 | ||
targetPort: 8080 | ||
protocol: TCP | ||
name: http | ||
selector: | ||
app: webapp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.