From c7ca062a487563fc381c042b02ddb88991b31ef1 Mon Sep 17 00:00:00 2001 From: sgayangi Date: Tue, 16 Apr 2024 13:58:25 +0530 Subject: [PATCH] Add default values for API Key Authentication --- .../config-deployer-service/ballerina/APIClient.bal | 8 +++----- .../ballerina/resources/apk-conf-schema.yaml | 4 ++-- .../ballerina/tests/resources/apiKeyOnly.apk-conf | 1 - .../ballerina/tests/resources/apk-schema.json | 2 +- .../ballerina/tests/resources/jwtandAPIKey.apk-conf | 1 - runtime/config-deployer-service/ballerina/types.bal | 4 ++-- .../docker/config-deployer/conf/apk-schema.json | 13 +++++++++---- 7 files changed, 17 insertions(+), 16 deletions(-) diff --git a/runtime/config-deployer-service/ballerina/APIClient.bal b/runtime/config-deployer-service/ballerina/APIClient.bal index b5ea081e25..1975b4e118 100644 --- a/runtime/config-deployer-service/ballerina/APIClient.bal +++ b/runtime/config-deployer-service/ballerina/APIClient.bal @@ -361,16 +361,14 @@ public class APIClient { } else if authentication.authType == "JWT" { JWTAuthentication jwtAuthentication = check authentication.cloneWithType(JWTAuthentication); authTypes.jwt = {header: jwtAuthentication.headerName, sendTokenToUpstream: jwtAuthentication.sendTokenToUpstream, disabled: !jwtAuthentication.enabled, audience: jwtAuthentication.audience}; - } else if authentication.authType == "APIKey" { + } else if authentication.authType == "APIKey" && authentication is APIKeyAuthentication { APIKeyAuthentication apiKeyAuthentication = check authentication.cloneWithType(APIKeyAuthentication); model:APIKey[] apiKeys = []; - boolean|() headerEnabled = apiKeyAuthentication.headerEnable; - boolean|() queryEnabled = apiKeyAuthentication.queryParamEnable; - if headerEnabled is boolean && headerEnabled { + if apiKeyAuthentication.headerEnable { apiKeys.push({'in: "Header", name: apiKeyAuthentication.headerName, sendTokenToUpstream: apiKeyAuthentication.sendTokenToUpstream}); } - if queryEnabled is boolean && queryEnabled { + if apiKeyAuthentication.queryParamEnable { apiKeys.push({'in: "Query", name: apiKeyAuthentication.queryParamName, sendTokenToUpstream: apiKeyAuthentication.sendTokenToUpstream}); } authTypes.apiKey = apiKeys; diff --git a/runtime/config-deployer-service/ballerina/resources/apk-conf-schema.yaml b/runtime/config-deployer-service/ballerina/resources/apk-conf-schema.yaml index baf1c0ac89..9c9847c50f 100644 --- a/runtime/config-deployer-service/ballerina/resources/apk-conf-schema.yaml +++ b/runtime/config-deployer-service/ballerina/resources/apk-conf-schema.yaml @@ -150,7 +150,7 @@ components: headerEnable: type: boolean default: true - audience: + audience: type: array default: [] items: @@ -173,7 +173,7 @@ components: default: false headerName: type: string - example: Authorization + example: apikey default: apikey queryParamName: type: string diff --git a/runtime/config-deployer-service/ballerina/tests/resources/apiKeyOnly.apk-conf b/runtime/config-deployer-service/ballerina/tests/resources/apiKeyOnly.apk-conf index b1f0824119..52ef282892 100644 --- a/runtime/config-deployer-service/ballerina/tests/resources/apiKeyOnly.apk-conf +++ b/runtime/config-deployer-service/ballerina/tests/resources/apiKeyOnly.apk-conf @@ -18,7 +18,6 @@ authentication: - authType: "APIKey" enabled: true queryParamEnable: true - headerEnable: true corsConfiguration: corsConfigurationEnabled: false accessControlAllowOrigins: diff --git a/runtime/config-deployer-service/ballerina/tests/resources/apk-schema.json b/runtime/config-deployer-service/ballerina/tests/resources/apk-schema.json index ee39eacd35..9eb12e1292 100644 --- a/runtime/config-deployer-service/ballerina/tests/resources/apk-schema.json +++ b/runtime/config-deployer-service/ballerina/tests/resources/apk-schema.json @@ -213,7 +213,7 @@ }, "headerName": { "type": "string", - "example": "Authorization", + "example": "apikey", "default": "apikey" }, "queryParamName": { diff --git a/runtime/config-deployer-service/ballerina/tests/resources/jwtandAPIKey.apk-conf b/runtime/config-deployer-service/ballerina/tests/resources/jwtandAPIKey.apk-conf index 88b21c4aa3..62d5518b9a 100644 --- a/runtime/config-deployer-service/ballerina/tests/resources/jwtandAPIKey.apk-conf +++ b/runtime/config-deployer-service/ballerina/tests/resources/jwtandAPIKey.apk-conf @@ -20,7 +20,6 @@ authentication: - authType: "APIKey" enabled: true queryParamEnable: true - headerEnable: true corsConfiguration: corsConfigurationEnabled: false accessControlAllowOrigins: diff --git a/runtime/config-deployer-service/ballerina/types.bal b/runtime/config-deployer-service/ballerina/types.bal index 25bd738b71..41fa29730d 100644 --- a/runtime/config-deployer-service/ballerina/types.bal +++ b/runtime/config-deployer-service/ballerina/types.bal @@ -277,8 +277,8 @@ public type APIKeyAuthentication record {| boolean sendTokenToUpstream = false; string headerName = "apiKey"; string queryParamName = "apiKey"; - boolean headerEnable?; - boolean queryParamEnable?; + boolean headerEnable = true; + boolean queryParamEnable = false; |}; # Mutual SSL configuration of this API diff --git a/runtime/config-deployer-service/docker/config-deployer/conf/apk-schema.json b/runtime/config-deployer-service/docker/config-deployer/conf/apk-schema.json index c1f5c34026..870b4a3898 100644 --- a/runtime/config-deployer-service/docker/config-deployer/conf/apk-schema.json +++ b/runtime/config-deployer-service/docker/config-deployer/conf/apk-schema.json @@ -156,11 +156,16 @@ "required": { "type": "string", "default": "mandatory", - "enum": ["mandatory", "optional"] + "enum": [ + "mandatory", + "optional" + ] }, "authType": { "type": "string", - "enum": ["JWT"] + "enum": [ + "JWT" + ] }, "sendTokenToUpstream": { "type": "boolean", @@ -253,7 +258,7 @@ }, "headerName": { "type": "string", - "example": "Authorization", + "example": "apikey", "default": "apikey" }, "queryParamName": { @@ -608,7 +613,7 @@ } } }, - "required":[ + "required": [ "target", "verb" ],