Skip to content

Tutorial: API Keys#3019

Open
predic8 wants to merge 8 commits into
masterfrom
tutorials-api-keys
Open

Tutorial: API Keys#3019
predic8 wants to merge 8 commits into
masterfrom
tutorials-api-keys

Conversation

@predic8

@predic8 predic8 commented Jun 27, 2026

Copy link
Copy Markdown
Member

Added comprehensive tutorials covering API Key Authentication, RBAC, OpenAPI integration, JDBC, and MongoDB key stores. Included corresponding test suites to validate behavior.

Summary by CodeRabbit

  • New Features
    • Added security tutorials for API key authentication, API key RBAC (scope-based), OpenAPI-enforced API key security, and database-backed API key stores (JDBC and MongoDB).
    • Included demo API key entries and an expanded OpenAPI 3.0 spec for the Fruit Shop API.
  • Tests
    • Added integration tests validating authentication, RBAC routing, and OpenAPI security response behavior.
  • Documentation
    • Added step-by-step tutorial configurations and example requests for all API key scenarios.
  • Bug Fixes
    • Improved MongoDB-backed API key store handling when a key document has no scopes field.

Added comprehensive tutorials covering API Key Authentication, RBAC, OpenAPI integration, JDBC, and MongoDB key stores. Included corresponding test suites to validate behavior.
@coderabbitai

coderabbitai Bot commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 67e334fb-4d48-4f03-aae0-60bb925ae20b

📥 Commits

Reviewing files that changed from the base of the PR and between bb09dea and 5fe8bd5.

📒 Files selected for processing (1)
  • core/src/main/java/com/predic8/membrane/core/interceptor/apikey/stores/MongoDBApiKeyStore.java

📝 Walkthrough

Walkthrough

Adds API key security tutorial YAML files, a fruitshop OpenAPI spec, demo key data, JUnit 5 integration tests, and a MongoDB API key store null-scope fix.

Changes

API Key Security Tutorials

Layer / File(s) Summary
Basic API key authentication tutorial and test
distribution/tutorials/security/100-API-Key-Authentication.yaml, distribution/src/test/java/com/predic8/membrane/tutorials/security/ApiKeyAuthenticationTutorialTest.java
Defines a port-2000 listener that validates X-Api-Key against stored secrets and returns 401 or 200 with Secured API!. The test asserts both response paths.
API key RBAC tutorial and test
distribution/tutorials/security/110-API-Key-RBAC.yaml, distribution/src/test/java/com/predic8/membrane/tutorials/security/ApiKeyRbacTutorialTest.java
Configures scope-based routing for finance, admin, and public responses using two API key secrets. The test asserts all three routing outcomes.
OpenAPI-enforced API key tutorial and test
distribution/tutorials/security/120-API-Key-OpenAPI.yaml, distribution/tutorials/security/fruitshop-api-v2-openapi-3-security.yml, distribution/tutorials/security/demo-keys.txt, distribution/src/test/java/com/predic8/membrane/tutorials/security/ApiKeyOpenApiTutorialTest.java
Loads a fruitshop OpenAPI spec with validateSecurity: true, uses X-Api-Key, and adds demo key data. The test asserts 401, 403, and 201 responses.
JDBC and MongoDB API key store tutorials
distribution/tutorials/security/130-JDBC-API-Key-Store.yaml, distribution/tutorials/security/140-MongoDB-API-Key-Store.yaml
Adds tutorial configs for PostgreSQL-backed and MongoDB-backed API key stores with setup instructions and header-based key extraction.
MongoDB API key store scope handling
core/src/main/java/com/predic8/membrane/core/interceptor/apikey/stores/MongoDBApiKeyStore.java
Replaces wildcard imports and returns an empty scope set when the MongoDB document does not contain scopes.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • christiangoerdes
  • rrayst

🐇 I hopped through keys and scopes today,
With tests that guard the gateway way.
OpenAPI, Mongo, JDBC bright,
401, 403, then 200 right—
Hop-hop, the security docs are in play!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title matches the main theme of the PR: adding API key tutorials and related validation.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch tutorials-api-keys

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
distribution/tutorials/security/fruitshop-api-v2-openapi-3-security.yml (1)

792-795: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick win

Use standard OpenAPI format names here.

url and datetime are not the standard OpenAPI 3.0.3 format names most tooling expects, so generators/validators may ignore them. Prefer uri and date-time in this tutorial spec.

Also applies to: 921-927, 1064-1066

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@distribution/tutorials/security/fruitshop-api-v2-openapi-3-security.yml`
around lines 792 - 795, Update the OpenAPI tutorial spec to use standard format
names so tooling recognizes them: in the SelfLink schema and the other
referenced examples, replace nonstandard format values in the relevant schema
definitions with the OpenAPI 3.0.3 equivalents. Focus on the affected properties
in the tutorial YAML and keep the changes consistent wherever the same pattern
appears.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@distribution/src/test/java/com/predic8/membrane/tutorials/security/ApiKeyOpenApiTutorialTest.java`:
- Around line 28-45: The test in enforcesOpenApiSecurityRequirements only covers
the unauthorized and forbidden branches, so extend it with the authorized-path
case as well. Add an assertion for POST /shop/v2/products using the documented
working X-Api-Key value (222) and verify the expected success response from the
OpenAPI spec (201), keeping the existing 401 and 403 checks intact.

In `@distribution/tutorials/security/140-MongoDB-API-Key-Store.yaml`:
- Around line 5-6: The MongoDB API key tutorial seeds a scope-less key without a
scopes field, but MongoDBApiKeyStore.getScopes() expects a list and will fail on
that document. Update the seeded demo key in this tutorial to include scopes: []
for the no-scope case, or adjust MongoDBApiKeyStore.getScopes() to treat a
missing scopes field as an empty set; use the demo-key / Valid key (no scopes)
steps as the main place to fix.

---

Nitpick comments:
In `@distribution/tutorials/security/fruitshop-api-v2-openapi-3-security.yml`:
- Around line 792-795: Update the OpenAPI tutorial spec to use standard format
names so tooling recognizes them: in the SelfLink schema and the other
referenced examples, replace nonstandard format values in the relevant schema
definitions with the OpenAPI 3.0.3 equivalents. Focus on the affected properties
in the tutorial YAML and keep the changes consistent wherever the same pattern
appears.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 62102bd4-c0af-4da8-8814-2dbbdbeacff7

📥 Commits

Reviewing files that changed from the base of the PR and between d7e3dfa and ad665ae.

📒 Files selected for processing (10)
  • distribution/src/test/java/com/predic8/membrane/tutorials/security/ApiKeyAuthenticationTutorialTest.java
  • distribution/src/test/java/com/predic8/membrane/tutorials/security/ApiKeyOpenApiTutorialTest.java
  • distribution/src/test/java/com/predic8/membrane/tutorials/security/ApiKeyRbacTutorialTest.java
  • distribution/tutorials/security/100-API-Key-Authentication.yaml
  • distribution/tutorials/security/110-API-Key-RBAC.yaml
  • distribution/tutorials/security/120-API-Key-OpenAPI.yaml
  • distribution/tutorials/security/130-JDBC-API-Key-Store.yaml
  • distribution/tutorials/security/140-MongoDB-API-Key-Store.yaml
  • distribution/tutorials/security/demo-keys.txt
  • distribution/tutorials/security/fruitshop-api-v2-openapi-3-security.yml

Comment thread distribution/tutorials/security/140-MongoDB-API-Key-Store.yaml
@membrane-ci-server

Copy link
Copy Markdown

This pull request needs "/ok-to-test" from an authorized committer.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@core/src/main/java/com/predic8/membrane/core/interceptor/apikey/stores/MongoDBApiKeyStore.java`:
- Around line 84-85: `MongoDBApiKeyStore#getScopes` is returning a present empty
set when no scopes exist, which breaks the `ApiKeyStore#getScopes` contract
followed by `SimpleKeyStore`. Update the `getScopes` logic so that a missing or
null `scopes` field yields `Optional.empty()` instead of `Optional.of(new
HashSet<>())`, while still returning a populated set when scopes are present.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: a5677bdf-9a00-4eb5-a915-c08e4fe4de75

📥 Commits

Reviewing files that changed from the base of the PR and between 0fd00ab and bb09dea.

📒 Files selected for processing (1)
  • core/src/main/java/com/predic8/membrane/core/interceptor/apikey/stores/MongoDBApiKeyStore.java

@predic8 predic8 added this to the 7.3.0 milestone Jun 28, 2026
@rrayst

rrayst commented Jun 28, 2026

Copy link
Copy Markdown
Member

/ok-to-test

@predic8 predic8 requested a review from rrayst June 28, 2026 19:13
@@ -0,0 +1,67 @@
# yaml-language-server: $schema=https://www.membrane-api.io/v7.2.4.json
#
# Tutorial: JDBC API Key Store

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be tested using a H2 database. See e.g. LoggingJDBCExampleTest.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

H2 only needs the driver no docker.
But I think the postgres is closer to the application of the users. And it is easier to create the users in the database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants