Skip to content

Commit f13a11b

Browse files
Add support for global token based auth for k6 script generation (#19348)
* Add global token based auth support (basic, bearer) for k6 * config files and generated code files after the changes * Handle empty or null auth at path level * files changed after update with master and run build
1 parent 2b40a2c commit f13a11b

16 files changed

Lines changed: 375 additions & 1 deletion

File tree

bin/configs/k6-basic-auth.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
generatorName: k6
2+
outputDir: samples/client/others/k6/basicAuth
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/k6/basic_auth.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/k6

bin/configs/k6-bearer-auth.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
generatorName: k6
2+
outputDir: samples/client/others/k6/bearerAuth
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/k6/bearer_auth.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/k6

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/K6ClientCodegen.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ private List<HTTPRequest> sortRequests(Map<Integer, HTTPRequest> requestsMap) {
362362
public static final String PROJECT_DESCRIPTION = "projectDescription";
363363
public static final String PROJECT_VERSION = "projectVersion";
364364
public static final String BASE_URL = "baseURL";
365+
public static final String TOKEN = "authToken";
365366
public static final String PRESERVE_LEADING_PARAM_CHAR = "preserveLeadingParamChar";
366367
static final Collection<String> INVOKER_PKG_SUPPORTING_FILES = Arrays.asList("script.mustache", "README.mustache");
367368
static final String[][] JAVASCRIPT_SUPPORTING_FILES = {
@@ -666,7 +667,7 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
666667

667668
pathVariables.put(groupName, variables);
668669

669-
// put auth medthods in header or cookie
670+
// put auth methods in header or cookie
670671
for (CodegenSecurity globalAuthMethod : globalAuthMethods) {
671672
if (globalAuthMethod.isKeyInHeader) {
672673
httpParams.add(new Parameter(globalAuthMethod.keyParamName, getTemplateString(toVarName(globalAuthMethod.keyParamName))));
@@ -676,6 +677,25 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
676677
cookieParams.add(new Parameter(globalAuthMethod.keyParamName, getTemplateString(toVarName(globalAuthMethod.keyParamName))));
677678
extraParameters.add(new Parameter(toVarName(globalAuthMethod.keyParamName), globalAuthMethod.keyParamName.toUpperCase(Locale.ROOT)));
678679
}
680+
681+
// when security is specifically given empty for an end point, it should not be included
682+
if (operation.getSecurity() != null && operation.getSecurity().isEmpty()) {
683+
continue;
684+
}
685+
if ("bearerAuth".equalsIgnoreCase(globalAuthMethod.name)) {
686+
if (operation.getSecurity() == null ||
687+
operation.getSecurity().stream().anyMatch(securityRequirement -> securityRequirement.containsKey("bearerAuth"))) {
688+
httpParams.add(new Parameter("Authorization", "`Bearer ${TOKEN}`"));
689+
additionalProperties.put(TOKEN, true);
690+
}
691+
}
692+
if ("basicAuth".equalsIgnoreCase(globalAuthMethod.name)) {
693+
if (operation.getSecurity() == null ||
694+
operation.getSecurity().stream().anyMatch(securityRequirement -> securityRequirement.containsKey("basicAuth"))) {
695+
httpParams.add(new Parameter("Authorization", "`Basic ${TOKEN}`"));
696+
additionalProperties.put(TOKEN, true);
697+
}
698+
}
679699
}
680700

681701
final HTTPParameters params = new HTTPParameters(null, cookieParams, httpParams, null, null, null, null, null,

modules/openapi-generator/src/main/resources/k6/script.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import http from "k6/http";
44
import { group, check, sleep } from "k6";
55

66
const BASE_URL = "{{baseURL}}";
7+
{{#authToken}}
8+
const TOKEN = "TOKEN_VALUE_HERE";
9+
{{/authToken}}
710
// Sleep duration between successive requests.
811
// You might want to edit the value of this variable or remove calls to the sleep function on the script.
912
const SLEEP_DURATION = 0.1;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Sample API
4+
description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.
5+
version: 0.1.9
6+
servers:
7+
- url: http://api.example.com/v1
8+
description: Optional server description, e.g. Main (production) server
9+
- url: http://staging-api.example.com
10+
description: Optional server description, e.g. Internal staging server for testing
11+
security:
12+
- basicAuth: []
13+
paths:
14+
/users:
15+
get:
16+
summary: Returns a list of users.
17+
description: Optional extended description in CommonMark or HTML.
18+
responses:
19+
"200": # status code
20+
description: A JSON array of user names
21+
content:
22+
application/json:
23+
schema:
24+
type: array
25+
items:
26+
type: string
27+
security:
28+
- basicAuth: []
29+
/public:
30+
get:
31+
summary: Returns public information.
32+
description: This endpoint does not require authentication.
33+
responses:
34+
"200":
35+
description: A JSON object with public information
36+
content:
37+
application/json:
38+
schema:
39+
type: object
40+
security: []
41+
/private:
42+
get:
43+
summary: Returns private information.
44+
description: This endpoint requires global security settings.
45+
responses:
46+
"200":
47+
description: A JSON object with private information
48+
content:
49+
application/json:
50+
schema:
51+
type: object
52+
components:
53+
securitySchemes:
54+
basicAuth:
55+
type: http
56+
scheme: basic
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Sample API
4+
description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.
5+
version: 0.1.9
6+
servers:
7+
- url: http://api.example.com/v1
8+
description: Optional server description, e.g. Main (production) server
9+
- url: http://staging-api.example.com
10+
description: Optional server description, e.g. Internal staging server for testing
11+
security:
12+
- bearerAuth: []
13+
paths:
14+
/users:
15+
get:
16+
summary: Returns a list of users.
17+
description: Optional extended description in CommonMark or HTML.
18+
responses:
19+
"200": # status code
20+
description: A JSON array of user names
21+
content:
22+
application/json:
23+
schema:
24+
type: array
25+
items:
26+
type: string
27+
security:
28+
- bearerAuth: []
29+
/public:
30+
get:
31+
summary: Returns public information.
32+
description: This endpoint does not require authentication.
33+
responses:
34+
"200":
35+
description: A JSON object with public information
36+
content:
37+
application/json:
38+
schema:
39+
type: object
40+
security: []
41+
/private:
42+
get:
43+
summary: Returns private information.
44+
description: This endpoint requires global security settings.
45+
responses:
46+
"200":
47+
description: A JSON object with private information
48+
content:
49+
application/json:
50+
schema:
51+
type: object
52+
components:
53+
securitySchemes:
54+
bearerAuth:
55+
type: http
56+
scheme: bearer
57+
bearerFormat: JWT # Optional: specify the format (e.g., JWT) if applicable
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
README.md
2+
script.js
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.9.0-SNAPSHOT
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Generated k6 script
2+
3+
The `script.js` file contains most of the Swagger/OpenAPI specification and you can customize it to your needs.
4+
5+
Global header variables are defined at the top of the file, like `api_key`. Each path in the specification is converted into a [group](https://docs.k6.io/docs/tags-and-groups) in k6 and each group contains all the request methods related to that path. Path and query parameters are extracted from the specification and put at the start of the group. The URL is constructed from the base URL plus path and query.
6+
7+
If the Swagger/OpenAPI specification used as the input spec contains examples at parameter level, those will be extracted and utilized as parameter values. The `handleParamValue` custom Mustache lambda registered for use in the K6 `script.mustache` template handles the conditional checks, formatting, and outputting of parameter values. If a given parameter has value specified – either in `example` or `examples` field, defined at the parameter level – that value will be used. For list (`examples`), entire list will be output in the generated script and the first element from that list will be assigned as parameter value. If a given parameter does not have an example defined, a placeholder value with `TODO_EDIT_THE_` prefix will be generated for that parameter, and you will have to assign a value before you can run the script. In other words, you can now generate K6 test scripts which are ready to run, provided the Swagger/OpenAPI specification used as the input spec contains examples for all of the path/query parameters; see `modules/openapi-generator/src/test/resources/3_0/examples.yaml` for an example of such specification, and https://swagger.io/docs/specification/adding-examples/ for more information about adding examples.
8+
9+
k6 specific parameters are in the [`params`](https://docs.k6.io/docs/params-k6http) object, and `body` contains the [request](https://docs.k6.io/docs/http-requests) body which is in the form of `identifier: type`, which the `type` should be substituted by a proper value. Then goes the request and the check.
10+
11+
[Check](https://docs.k6.io/docs/checks) are like asserts but differ in that they don't halt execution, instead they just store the result of the check, pass or fail, and let the script execution continue.
12+
13+
Each request is always followed by a 0.1 second [sleep](https://docs.k6.io/docs/sleep-t-1) to prevent the script execution from flooding the system with too many requests simultaneously.
14+
15+
Note that the default iteration count and VU count is 1. So each request in each group will be executed once. For more information, see the [k6 options](https://docs.k6.io/docs/options).

0 commit comments

Comments
 (0)