|
1 | | -# Access Control |
| 1 | +# Deploy a Policy for access control |
2 | 2 |
|
3 | | -In this example, we deploy a web application; configure load balancing for it via a VirtualServer; and apply access |
4 | | -control policies to deny and allow traffic from a specific subnet. |
5 | | - |
6 | | -## Prerequisites |
7 | | - |
8 | | -1. Follow the [installation](https://docs.nginx.com/nginx-ingress-controller/installation/installation-with-manifests/) |
9 | | - instructions to deploy the Ingress Controller. |
10 | | -1. Save the public IP address of the Ingress Controller into a shell variable: |
11 | | - |
12 | | - ```console |
13 | | - IC_IP=XXX.YYY.ZZZ.III |
14 | | - ``` |
15 | | - |
16 | | -1. Save the HTTP port of the Ingress Controller into a shell variable: |
17 | | - |
18 | | - ```console |
19 | | - IC_HTTP_PORT=<port number> |
20 | | - ``` |
21 | | - |
22 | | -## Step 1 - Deploy a Web Application |
23 | | - |
24 | | -Create the application deployment and service: |
25 | | - |
26 | | -```console |
27 | | -kubectl apply -f webapp.yaml |
28 | | -``` |
29 | | - |
30 | | -## Step 2 - Deploy an Access Control Policy |
31 | | - |
32 | | -In this step, we create a policy with the name `webapp-policy` that denies requests from clients with an IP that belongs |
33 | | -to the subnet `10.0.0.0/8`. This is the subnet that our test client in Steps 4 and 6 will belong to. Make sure to change |
34 | | -the `deny` field of the `access-control-policy-deny.yaml` according to your environment (use the subnet of your |
35 | | -machine). |
36 | | - |
37 | | -Create the policy: |
38 | | - |
39 | | -```console |
40 | | -kubectl apply -f access-control-policy-deny.yaml |
41 | | -``` |
42 | | - |
43 | | -## Step 3 - Configure Load Balancing |
44 | | - |
45 | | -Create a VirtualServer resource for the web application: |
46 | | - |
47 | | -```console |
48 | | -kubectl apply -f virtual-server.yaml |
49 | | -``` |
50 | | - |
51 | | -Note that the VirtualServer references the policy `webapp-policy` created in Step 2. |
52 | | - |
53 | | -## Step 4 - Test the Configuration |
54 | | - |
55 | | -Let's access the application: |
56 | | - |
57 | | -```console |
58 | | -curl --resolve webapp.example.com:$IC_HTTP_PORT:$IC_IP http://webapp.example.com:$IC_HTTP_PORT |
59 | | -``` |
60 | | - |
61 | | -```text |
62 | | -<html> |
63 | | -<head><title>403 Forbidden</title></head> |
64 | | -<body> |
65 | | -<center><h1>403 Forbidden</h1></center> |
66 | | -</body> |
67 | | -</html> |
68 | | -``` |
69 | | - |
70 | | -We got a 403 response from NGINX, which means that our policy successfully blocked our request. |
71 | | - |
72 | | -## Step 5 - Update the Policy |
73 | | - |
74 | | -In this step, we update the policy to allow requests from clients from the subnet `10.0.0.0/8`. Make sure to change the |
75 | | -`allow` field of the `access-control-policy-allow.yaml` according to your environment. |
76 | | - |
77 | | -Update the policy: |
78 | | - |
79 | | -```console |
80 | | -kubectl apply -f access-control-policy-allow.yaml |
81 | | -``` |
82 | | - |
83 | | -## Step 6 - Test the Configuration |
84 | | - |
85 | | -Let's access the application again: |
86 | | - |
87 | | -```console |
88 | | -curl --resolve webapp.example.com:$IC_HTTP_PORT:$IC_IP http://webapp.example.com:$IC_HTTP_PORT |
89 | | -``` |
90 | | - |
91 | | -```text |
92 | | -Server address: 10.64.0.13:8080 |
93 | | -Server name: webapp-5cbbc7bd78-wf85w |
94 | | -``` |
95 | | - |
96 | | -In contrast with Step 4, we got a 200 response, which means that our updated policy successfully allowed our request. |
| 3 | +This is the example code used in the [Deploy a Policy for access control](https://docs.nginx.com/nginx-ingress-controller/configuration/access-control/) documentation. |
0 commit comments