|
| 1 | +# RBAC on Nginx with Open Policy Agent |
| 2 | + |
| 3 | +This repository demonstrates how to implement role based access control (RBAC) on Nginx with Open Policy Agent. |
| 4 | + |
| 5 | +## How to use |
| 6 | + |
| 7 | +First, start the container of Nginx and Open Policy Agent. |
| 8 | + |
| 9 | +``` |
| 10 | +$ docker-compose up -d |
| 11 | +``` |
| 12 | + |
| 13 | +When you send a request to `/compute/` using Alice's client certificate, nginx will grant access based on role definition. |
| 14 | + |
| 15 | +``` |
| 16 | +$ curl -k --cert ./nginx/tls/alice.pem --key ./nginx/tls/alice-key.pem https://127.0.0.1:8080/compute/ |
| 17 | +``` |
| 18 | + |
| 19 | +For requests to `/storage/` as well, nginx allows too. |
| 20 | + |
| 21 | +``` |
| 22 | +$ curl -k --cert ./nginx/tls/alice.pem --key ./nginx/tls/alice-key.pem https://127.0.0.1:8080/storage/ |
| 23 | +``` |
| 24 | + |
| 25 | +Access is allowed even if you send a request to `/compute/` using Bob's client certificate. |
| 26 | + |
| 27 | +``` |
| 28 | +$ curl -k --cert ./nginx/tls/bob.pem --key ./nginx/tls/bob-key.pem https://127.0.0.1:8080/compute/ |
| 29 | +``` |
| 30 | + |
| 31 | +However, Bob can not access to `/storage/` based on role definition. |
| 32 | + |
| 33 | +``` |
| 34 | +$ curl -k --cert ./nginx/tls/bob.pem --key ./nginx/tls/bob-key.pem https://127.0.0.1:8080/storage/ |
| 35 | +``` |
| 36 | + |
| 37 | +## Generating a new client certificate |
| 38 | + |
| 39 | +This repository uses a client certificate for user authentication. We use [CFSSL](https://github.com/cloudflare/cfssl) to issue client certificates. How to issue a new client certificate as follows. |
| 40 | + |
| 41 | +Generate a new CSR definition of CFSSL. |
| 42 | + |
| 43 | +``` |
| 44 | +$ cd nginx/tls |
| 45 | +$ cfssl print-defaults csr > client-csr.json |
| 46 | +``` |
| 47 | + |
| 48 | +Rewrite the CSR definition as you need. |
| 49 | + |
| 50 | +``` |
| 51 | +$ vim client-csr.json |
| 52 | +``` |
| 53 | + |
| 54 | +Generate client certificate files by using CSR definition. |
| 55 | + |
| 56 | +``` |
| 57 | +$ cfssl gencert -ca=ca.pem -ca-key=ca-key.pem client-csr.json | cfssljson -bare client |
| 58 | +``` |
| 59 | + |
| 60 | +You can use the new client certificate. |
| 61 | + |
| 62 | +``` |
| 63 | +$ curl -k --cert ./nginx/tls/client.pem --key ./nginx/tls/client-key.pem https://127.0.0.1:8080/compute |
| 64 | +``` |
| 65 | + |
| 66 | +## Defining roles and role bindings |
| 67 | + |
| 68 | +Authorization by RBAC is implemented by the combination of Nginx and Open Policy Agent. |
| 69 | + |
| 70 | +The Role definition is defined in the JSON file as follows. The role has a combination of a path and an list of HTTP methods allowing access, and the Open Policy Agent performs authorization based on the role. |
| 71 | + |
| 72 | +``` |
| 73 | +{ |
| 74 | + "roles": { |
| 75 | + "compute.admin": { |
| 76 | + "/compute/": ["GET", "POST", "PUT", "DELETE"] |
| 77 | + }, |
| 78 | + "compute.viewer": { |
| 79 | + "/compute/": ["GET"] |
| 80 | + }, |
| 81 | + "storage.admin": { |
| 82 | + "/storage/": ["GET", "POST", "PUT", "DELETE"] |
| 83 | + }, |
| 84 | + "storage.viewer": { |
| 85 | + "/storage/": ["GET"] |
| 86 | + } |
| 87 | + }, |
| 88 | +
|
| 89 | + "role_bindings": { |
| 90 | + "alice": ["compute.admin", "storage.admin"], |
| 91 | + "bob": ["compute.viewer"] |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +You can add a new role or role binding to the role definition. |
| 97 | + |
| 98 | +``` |
| 99 | +$ vim opa/rbac.json |
| 100 | +``` |
0 commit comments