Skip to content

Commit 7b01f34

Browse files
authored
API Keys: update JDBC API Key Store tutorial for cons… (#2753)
* docs(security, examples): update JDBC API Key Store tutorial for consistency and clarity - Improved Docker and PostgreSQL setup steps. - Simplified JDBC configuration properties and corrected password values. - Enhanced API key usage demonstration with updated testing instructions. - Made YAML configurations more concise and aligned with global standards. - Refined scope logic and logging messages for better readability. * Update APIKeyRBACExampleTest assertions for improved scope clarity - Adjusted test assertions to use more descriptive text for scopes (`"Only finance or accounting!"` and `"Only admins!"`). - Updated expected output values to refine validation logic, ensuring alignment with clarified roles.
1 parent 8d2947d commit 7b01f34

4 files changed

Lines changed: 67 additions & 68 deletions

File tree

distribution/examples/security/api-key/jdbc-api-key-store/README.md

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,49 @@ A quick guide to setting up a JDBC-based API key store using PostgreSQL.
55
### Prerequisite
66

77
- **Docker installed:**
8-
98
- If Docker is already installed, skip to the next step.
109
- Otherwise, install Docker from [https://docs.docker.com/get-started/get-docker/](https://docs.docker.com/get-started/get-docker/).
1110

1211
---
1312

1413
1. **Run Database Container:**
14+
- Start a database container (e.g., PostgreSQL) with:
1515

16-
- Start a database container (e.g., PostgreSQL) with:
17-
18-
```shell
19-
docker run --name postgres -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password -p 5432:5432 -d postgres
20-
```
16+
```shell
17+
docker run --rm --name postgres -e POSTGRES_PASSWORD=secret -p 5432:5432 postgres
18+
```
2119

2220
2. **Download JDBC Driver:**
23-
24-
- Download the PostgreSQL JDBC driver from the official
25-
site: [https://jdbc.postgresql.org/download/](https://jdbc.postgresql.org/download/).
26-
- Place it in the `lib` directory of your Membrane installation.
21+
- Download the PostgreSQL JDBC driver from the official
22+
site: [https://jdbc.postgresql.org/download/](https://jdbc.postgresql.org/download/).
23+
- Place it in the `lib` directory of your Membrane installation.
2724

2825
3. **Take a look at the configuration in the [`apis.yaml`](apis.yaml)**
2926

30-
4. **run the membrane.sh script:**
27+
4. **Run the membrane.sh script:**
3128

32-
```shell
33-
./membrane.sh
34-
```
29+
```shell
30+
./membrane.sh
31+
```
3532

3633
5. **Run SQL File in your Docker Container:**
34+
- Make sure that Membrane was running before to create the database tables.
35+
- Run this command:
3736

38-
- Run this command:
39-
40-
```shell
41-
docker exec -i postgres psql -U user -d postgres < ./insert_apikeys.sql
42-
```
37+
```shell
38+
docker exec -i postgres psql -U postgres -d postgres < ./insert_apikeys.sql
39+
```
4340

44-
- test
41+
- test
4542

46-
```shell
47-
docker exec -i postgres psql -U user -d postgres -c "SELECT * FROM key;"
48-
```
43+
```shell
44+
docker exec -i postgres psql -U postgres -d postgres -c "SELECT * FROM key;"
45+
```
4946

50-
6. **You can test it using curl:**
47+
6. **Test it using curl:**
5148

52-
```shell
53-
curl localhost:2000 -H "x-api-key:unsecure2000"
54-
```
49+
```shell
50+
curl localhost:2000 -H "x-api-key:unsecure2000"
51+
```
5552

56-
- if the API key is valid, you will be redirected to ```https://api.predic8.de```.
53+
- if the API key is valid, you will be forwarded to ```https://api.predic8.de```.

distribution/examples/security/api-key/jdbc-api-key-store/apis.yaml

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,30 @@ components:
55
bean:
66
class: org.apache.commons.dbcp2.BasicDataSource
77
properties:
8-
- property:
9-
name: driverClassName
10-
value: org.postgresql.Driver
11-
- property:
12-
name: url
13-
value: jdbc:postgresql://localhost:5432/postgres
14-
- property:
15-
name: username
16-
value: user
17-
- property:
18-
name: password
19-
value: password
8+
- name: driverClassName
9+
value: org.postgresql.Driver
10+
- name: url
11+
value: jdbc:postgresql://localhost:5432/postgres
12+
- name: username
13+
value: postgres
14+
- name: password
15+
value: secret
2016

2117
---
18+
global:
19+
- apiKey:
20+
stores:
21+
- databaseApiKeyStore:
22+
datasource: '#/components/dataSource'
23+
keyTable:
24+
name: key
25+
scopeTable:
26+
name: scope
27+
extractors:
28+
- headerExtractor: {}
2229

30+
---
2331
api:
2432
port: 2000
25-
flow:
26-
- apiKey:
27-
stores:
28-
- databaseApiKeyStore:
29-
datasource: '#/components/dataSource'
30-
keyTable:
31-
name: key
32-
scopeTable:
33-
name: scope
34-
extractors:
35-
- headerExtractor: {}
3633
target:
3734
url: https://api.predic8.de

distribution/examples/security/api-key/rbac/apis.yaml

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,49 @@
44
# In this instance, we use an `ApiKeyFileStore`, which fetches API keys from a specified file.
55
# For global access across all API key interceptors, declare the stores as shown here.
66
# If an interceptor-specific store is needed, embed the store directly within the interceptor.
7+
78
components:
89
keys:
910
apiKeyFileStore:
1011
location: ./demo-keys.txt
1112

1213
---
13-
# API key authorization providing key is optional. Certain scopes allow for additional access rights.
14+
global:
15+
- apiKey:
16+
# API key authorization is optional.
17+
# But certain scopes allow for additional access rights.
18+
required: false
19+
extractors:
20+
- headerExtractor:
21+
name: X-Key
22+
23+
---
1424
api:
1525
port: 2000
1626
flow:
17-
- apiKey:
18-
required: false
19-
extractors:
20-
- headerExtractor:
21-
name: X-Key
2227
- request:
28+
- log:
29+
message: "Scopes: ${scopes()}"
2330
- if:
2431
test: hasScope('admin')
25-
language: spel
2632
flow:
2733
- template:
2834
src: |
29-
Only for admins!
30-
Caller scopes: ${fn.scopes()}
35+
Only admins!
36+
Scopes: ${fn.scopes()}
3137
- return:
3238
status: 200
3339
- if:
34-
test: hasScope({'finance','accounting'})
35-
language: spel
40+
test: hasScope('finance') or hasScope('accounting')
3641
flow:
3742
- template:
3843
src: |
39-
Only for finance or accounting!
40-
Caller scopes: ${fn.scopes()}
44+
Only finance or accounting!
45+
Scopes: ${fn.scopes()}
4146
- return:
4247
status: 200
4348
- template:
4449
src: Normal API
4550
- return:
4651
status: 200
52+

distribution/src/test/java/com/predic8/membrane/examples/withoutinternet/test/APIKeyRBACExampleTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void normalScope() {
3434
.get("http://localhost:2000")
3535
.then().assertThat()
3636
.statusCode(200)
37-
.body(containsString("Caller scopes"))
37+
.body(containsString("Only finance or accounting!"))
3838
.body(containsString("accounting"))
3939
.body(containsString("finance"));
4040
}
@@ -47,8 +47,7 @@ void conditionalScope() {
4747
.get("http://localhost:2000")
4848
.then().assertThat()
4949
.statusCode(200)
50-
.body(containsString("Caller scopes"))
51-
.body(not(containsString("accounting")))
52-
.body(containsString("admin"));
50+
.body(containsString("Only admins!"))
51+
.body(containsString("[admin]"));
5352
}
5453
}

0 commit comments

Comments
 (0)