Skip to content

Commit 54ce45d

Browse files
Simplify API Key YAML configuration and JavaDoc; add collapsed attr… (#2808)
* Simplify API Key YAML configuration and JavaDoc; add `collapsed` attribute to MCElement annotations. * docs(api-key): update YAML configuration and examples, refine migration guide - Replaced `headerExtractor` and `queryParamExtractor` with simplified `header` and `query` formats in YAML. - Updated Migration Guide and examples to reflect the new configuration styles for ApiKey extractors. - Added new configuration formats to the list of supported elements. * Simplified CORS interceptor Javadoc examples by removing `<pre><code>` tags. * fixes * fixes --------- Co-authored-by: Christian Gördes <118011644+christiangoerdes@users.noreply.github.com> Co-authored-by: Christian Gördes <christian.goerdes@outlook.de>
1 parent f9d448e commit 54ce45d

8 files changed

Lines changed: 20 additions & 32 deletions

File tree

core/src/main/java/com/predic8/membrane/core/interceptor/apikey/extractors/ApiKeyHeaderExtractor.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,11 @@
3030
* @yaml <pre><code>
3131
* apiKey:
3232
* extractors:
33-
* - header: {} # default: X-Api-Key
34-
* - header:
35-
* name: Authorization # custom header name
33+
* - header: x-key # custom header name
3634
* </code></pre>
3735
* @topic 3. Security and Validation
3836
*/
39-
@MCElement(name="header", id = "headerExtractor", component = false)
37+
@MCElement(name="header", id = "headerExtractor", component = false, collapsed = true)
4038
public class ApiKeyHeaderExtractor implements ApiKeyExtractor{
4139

4240
private HeaderName headerName = new HeaderName("X-Api-Key");
@@ -54,9 +52,9 @@ public Optional<LocationNameValue> extract(Exchange exc) {
5452

5553
/**
5654
* @description The HTTP header name to check for an API key.
57-
* @default X-Api-Key
5855
* @example Authorization
5956
*/
57+
@Required
6058
@MCAttribute(attributeName = "name")
6159
public void setHeaderName(String headerName) {
6260
if(!headerName.isBlank()) this.headerName = new HeaderName(headerName);

core/src/main/java/com/predic8/membrane/core/interceptor/apikey/extractors/ApiKeyQueryParamExtractor.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,11 @@
3333
* @yaml <pre><code>
3434
* - apiKey:
3535
* extractors:
36-
* - query: {} # default: api-key
37-
* - query:
38-
* name: auth # custom query param name
36+
* - query: apikey # custom query param name
3937
* </code></pre>
4038
* @topic 3. Security and Validation
4139
*/
42-
@MCElement(name = "query", id="queryParamExtractor", component = false)
40+
@MCElement(name = "query", id="queryParamExtractor", component = false, collapsed = true)
4341
public class ApiKeyQueryParamExtractor implements ApiKeyExtractor{
4442

4543
private static final Logger log = LoggerFactory.getLogger(ApiKeyQueryParamExtractor.class);
@@ -66,9 +64,9 @@ public Optional<LocationNameValue> extract(Exchange exc) {
6664

6765
/**
6866
* @description The query parameter name to check for an API key.
69-
* @default api-key
7067
* @example api_key
7168
*/
69+
@Required
7270
@MCAttribute(attributeName = "name")
7371
public void setParamName(String paramName) {
7472
this.paramName = paramName;

core/src/main/java/com/predic8/membrane/core/interceptor/cors/CorsInterceptor.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public Set<String> getMethods() {
243243
* @default "" (no additional headers beyond safe headers)
244244
*
245245
* @example
246-
* <pre><code><cors headers="Authorization,X-Custom-Header,X-Requested-With" /></code></pre>
246+
* Authorization,X-Custom-Header,X-Requested-With
247247
*/
248248
@MCAttribute
249249
public void setHeaders(String headers) {
@@ -266,7 +266,7 @@ public String getHeaders() {
266266
* @default "" (expose no additional headers)
267267
*
268268
* @example
269-
* <pre><code><cors exposeHeaders="X-Total-Count,X-Custom-Info" /></code></pre>
269+
* X-Total-Count,X-Custom-Info
270270
*/
271271
@MCAttribute
272272
public void setExposeHeaders(String headers) {
@@ -289,10 +289,7 @@ public String getExposeHeaders() {
289289
* @param credentials true to allow credentials, false to disallow
290290
* @default false
291291
*
292-
* @example
293-
* <pre><code><cors
294-
* origins="https://trusted-app.com"
295-
* credentials="true" /></code></pre>
292+
* @example true
296293
*/
297294
@MCAttribute
298295
public void setCredentials(boolean credentials) {
@@ -315,8 +312,7 @@ public boolean getCredentials() {
315312
* @param maxAge maximum cache age in seconds (0 = no caching)
316313
* @default 5 seconds
317314
*
318-
* @example
319-
* <pre><code><cors maxAge="3600" /></code></pre>
315+
* @example 3600
320316
*/
321317
@MCAttribute
322318
public void setMaxAge(int maxAge) {

distribution/examples/security/api-key/apikey-openapi/apis.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ api:
1616
# API keys are validated in the OpenAPI validator with validateSecurity: true. See the OpenAPI document for details.
1717
required: false
1818
extractors:
19-
- header:
20-
name: "X-Api-Key"
19+
- header: X-Api-Key
2120
# The API key must be extracted before the OpenAPI validator is called.
2221
# Normally, the OpenAPI is validated before the flow is executed. By explicitly
2322
# setting the openapiValidator to this position, the OpenAPI is validated after the apiKey plugin.

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ global:
1717
# But certain scopes allow for additional access rights.
1818
required: false
1919
extractors:
20-
- header:
21-
name: X-Key
20+
- header: X-Key
2221

2322
---
2423
api:

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ global:
1414
extractors:
1515
# Fetch the API key from header
1616
# X-Api-Key: demokey
17-
- header: {}
17+
- header: X-Api-Key
1818

1919
# Fetch key from query
2020
# GET /foo?api-key=abc123
21-
- query: {}
21+
- query: api-key
2222

2323
---
2424

distribution/examples/security/jwt/apikey-to-jwt-conversion/apis.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ api:
1010
- apiKeyFileStore:
1111
location: demo-keys.txt
1212
extractors:
13-
- header: {}
13+
- header: X-Api-Key
1414
- request:
1515
- template:
1616
contentType: application/json

docs/MIGRATION-GUIDE.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ Remove these elements from your configuration and replace them with modern equiv
2727
- Return string from script does not set a content type of `text/html` anymore. User has to set the content type manually.
2828

2929
## ApiKey extractors:
30-
- Rename `headerExtractor` to `header`
31-
- Rename `queryParamExtractor` to `query`
30+
- Replace `headerExtractor: \n name: <value>` with `header: <value>`
31+
- Replace `queryParamExtractor: \n name: <value>` with `query: <value>`
3232
```yaml
3333
# before
3434
apiKey:
@@ -41,10 +41,8 @@ apiKey:
4141
# now
4242
apiKey:
4343
extractors:
44-
- header:
45-
name: X-API-KEY
46-
- query:
47-
name: api-key
44+
- header: X-API-KEY
45+
- query: api-key
4846
```
4947
5048
## fileUserDataProvider
@@ -209,7 +207,7 @@ api:
209207
### YAML configuration for Elements with exactly one configurable Attribute
210208
Some Elements with **exactly one** configurable Attribute can now be configured inline. This **does not** apply to all elements with exactly one configurable Attribute.
211209

212-
The supported elements are: `api.description`, `publicURL`, `headerFilter.include`, `headerFilter.exclude`, `keyTable`, `scopeTable`, `accessControl.allow`, `accessControl.deny`, `counter`, `mutation`
210+
The supported elements are: `api.description`, `publicURL`, `headerFilter.include`, `headerFilter.exclude`, `keyTable`, `scopeTable`, `accessControl.allow`, `accessControl.deny`, `counter`, `mutation`, `header` (ApiKey extractor), `query` (ApiKey extractor)
213211

214212
#### headerFilter
215213
```yaml

0 commit comments

Comments
 (0)