Skip to content

Commit 2527753

Browse files
authored
api: refactor, add nomad docs (#34)
1 parent 0d25667 commit 2527753

File tree

1 file changed

+64
-16
lines changed

1 file changed

+64
-16
lines changed

docs/services/api.md

+64-16
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,81 @@ title: API
33
tags:
44
- services
55
- api
6+
- ldap
67
---
78

8-
# API
9-
10-
## Redbrick Administrator Web API
9+
# Redbrick Administrative Web API
1110

1211
The source code for the API can be found [here](https://github.com/redbrick/api/).
1312

14-
The Redbrick web API serves as an easy interface to carry out administrator tasks (mainly LDAP related), and for use in automation. This saves time instead of accessing machines, and formulating and executing manual LDAP queries or scripts.
13+
The Redbrick web API serves as an easy interface to carry out administrator tasks *(mainly LDAP related)*, and for use in automation. This saves time instead of accessing machines, and formulating and executing manual LDAP queries or scripts.
14+
15+
The server code for the API is hosted on [`aperture`](../hardware/aperture/index.md) in a docker container deployed with [`nomad`](nomad.md), the job file for which is [here](https://github.com/redbrick/nomad/blob/master/jobs/services/api.hcl). It is written in Python with [FastAPI](https://fastapi.tiangolo.com/). This container is then served to the public using [`traefik`](traefik.md).
16+
17+
## Nomad Job File
18+
19+
The [nomad job for Redbrick's API](https://github.com/redbrick/nomad/blob/master/jobs/services/api.hcl) is similar to most other web servers for the most part. As always, all secrets are stored in [`consul`](consul.md). Some things to watch out for are:
20+
21+
- The docker image on ghcr.io is private and therefore requires credentials to access.
22+
23+
```hcl title="Nomad"
24+
auth {
25+
username = "${DOCKER_USER}"
26+
password = "${DOCKER_PASS}"
27+
}
28+
```
29+
30+
```hcl title="Nomad"
31+
template {
32+
data = <<EOH
33+
DOCKER_USER={{ key "api/ghcr/username" }}
34+
DOCKER_PASS={{ key "api/ghcr/password" }}
35+
...
36+
EOH
37+
```
38+
39+
- The docker container must access `/home` and `/storage` on [`icarus`](../hardware/nix/icarus.md) to configure users' home directory and webtree. This is mounted onto the [`aperture`](../hardware/aperture/index.md) boxes at `/oldstorage` and is mounted to the containers like this:
40+
41+
```hcl title="Nomad"
42+
volumes = [
43+
"/oldstorage:/storage",
44+
"/oldstorage/home:/home",
45+
]
46+
```
47+
48+
- The container requires the LDAP secret at `/etc/ldap.secret` to auth with LDAP. This is stored in [`consul`](consul.md), placed in a template and mounted to the container like this:
1549

16-
The server code for the API is hosted on Zeus in a docker container called 'api-redbrick', written in Python with [FastAPI](https://fastapi.tiangolo.com/). This container is then served to the public using traefik, you can view the dashboard [here](https://traefik.zeus.redbrick.dcu.ie/dashboard/#/).
50+
```hcl title="Nomad"
51+
template {
52+
destination = "local/ldap.secret"
53+
data = "{{ key \"api/ldap/secret\" }}" # this is necessary as the secret has no EOF
54+
}
55+
```
56+
57+
- The container is quite RAM intensive, regularly using `700-800MB`. The job has been configured to allocate `1GB` RAM to the container so it does not OOM. The default `cpu` allocation of `300` is fine.
58+
59+
```hcl title="Nomad"
60+
resources {
61+
cpu = 300
62+
memory = 1024
63+
}
64+
```
1765

18-
### Reference
66+
## Reference
1967

2068
For the most up to date, rich API reference please visit [https://api.redbrick.dcu.ie/docs](https://api.redbrick.dcu.ie/docs)
2169

2270
All requests are validated with Basic Auth for access. [See example.](https://docs.python-requests.org/en/master/user/authentication/#basic-authentication)
2371

24-
| Method | Route | URL Parameters | Body |
25-
| :--------: | :--------------------: | :----------------------------------: | :---------------: |
26-
| GET | **/users/**`username` | `username` - Redbrick username | N/A |
27-
| PUT | **/users/**`username` | `username` - Redbrick username | `ldap_key` |
28-
| POST | **/users/register** | N/A | `ldap_value` |
72+
| Method | Route | URL Parameters | Body |
73+
| :----: | :-------------------: | :----------------------------: | :----------: |
74+
| GET | **/users/**`username` | `username` - Redbrick username | N/A |
75+
| PUT | **/users/**`username` | `username` - Redbrick username | `ldap_key` |
76+
| POST | **/users/register** | N/A | `ldap_value` |
2977

30-
#### Examples
78+
## Examples
3179

32-
- GET a user's LDAP data
80+
- `GET` a user's LDAP data
3381

3482
```python
3583
import requests
@@ -45,7 +93,7 @@ response = requests.request("GET", url, headers=headers)
4593
print(response.text)
4694
```
4795

48-
- PUT a user's LDAP data to change their `loginShell` to `/usr/local/shells/zsh`
96+
- `PUT` a user's LDAP data to change their `loginShell` to `/usr/local/shells/zsh`
4997

5098
```python
5199
import requests
@@ -66,7 +114,7 @@ response = requests.request("GET", url, headers=headers, data=payload)
66114
print(response.text)
67115
```
68116

69-
### Important Notes and Caveats
117+
## Important Notes and Caveats
70118

71119
As the FastAPI server for the API is hosted inside of a Docker container, there are limitations to the commands we can execute that affect the "outside" world.
72120

@@ -90,4 +138,4 @@ chown 13371337:103 /storage/webtree/U/USERNAME
90138
# Where 13371337 is userid and 103 is the id for the 'member' group.
91139
```
92140

93-
Note that USERNAME can be used to refer to the user's web directory here since it is the name of the directory and doesn't refer to the user object.
141+
> Note that `USERNAME` can be used to refer to the user's web directory here since it is the name of the directory and doesn't refer to the user object.

0 commit comments

Comments
 (0)