First Commit

This commit is contained in:
2024-01-23 21:11:06 -03:00
parent f04a1369d6
commit a2c8be6bad
5 changed files with 25 additions and 8 deletions

View File

@@ -158,7 +158,7 @@ You need to deploy the API (you can see the **deployment** file [applyValidation
![img_17.png](images/img_17.png)
![img.png](images/img_18d.png)
![img.png](images/img-18e.png)
![img_20.png](images/img_20.png)
@@ -168,6 +168,7 @@ There are 4 news HEADER parameters:
- **functionId**: It's your authorization **OCI function** **OCID** mentioned in **authApi** service.
- **host_name**: It's your **OCI API Gateway** endpoint. You can find this information in the console of your instance.
- **apiGatewayId**: It's your **OCI API Gateway** deployment **OCID** to deploy your specification
- **rateLimit**: It's the rate limit configuration for the **OCI API Gateway** deployment. Put in this format seconds,[CLIENT_IP/TOTAL]. Example: 2000,CLIENT_IP (optional)
And you can test with:

View File

@@ -41,7 +41,8 @@
"functionId": "request.headers[functionId]",
"host": "request.host",
"token": "request.headers[token]",
"apiGatewayId": "request.headers[apiGatewayId]"
"apiGatewayId": "request.headers[apiGatewayId]",
"rateLimit": "request.headers[rateLimit]"
},
"token_header": null,
"token_query_param": null,

View File

@@ -82,14 +82,14 @@ def creeateOrUpdateDeployment(compartmendId, displayName, validation_deployment_
deployment_id = gateway["items"][ind]["id"]
else:
gateway_id = api_gateway_id
deployment_id = 0
deployment_id = ""
if (gateway["items"] != [] and deployment_id > 0):
if (gateway["items"] != [] and deployment_id != ""):
apigateway_client.update_deployment(deployment_id=deployment_id, update_deployment_details=validation_deployment_details)
else:
apigateway_client.create_deployment(create_deployment_details=create_deployment_details)
def applyAuthApi(compartmentId, displayName, payload, functionId, host, api_gateway_id):
def applyAuthApi(compartmentId, displayName, payload, functionId, host, api_gateway_id, rate_limit):
config = oci.config.from_file("config")
logging = oci.loggingingestion.LoggingClient(config)
apigateway_client = oci.apigateway.DeploymentClient(config)
@@ -111,6 +111,16 @@ def applyAuthApi(compartmentId, displayName, payload, functionId, host, api_gate
gateway_id = api_gateway_id
deployment_id = 0
try:
rate_config = rate_limit.split(',')
rate_seconds = int(rate_config[0])
rate_key = rate_config[1]
rate_limiting = oci.apigateway.models.RateLimitingPolicy(
rate_in_requests_per_second=rate_seconds,
rate_key=rate_key)
except:
rate_limiting = None
path_prefix = "/"
routes = [ ]
new_routes = [ ]
@@ -184,6 +194,7 @@ def applyAuthApi(compartmentId, displayName, payload, functionId, host, api_gate
display_name=displayName + "-validation",
specification=oci.apigateway.models.ApiSpecification(
request_policies=oci.apigateway.models.ApiSpecificationRequestPolicies(
rate_limiting=rate_limiting,
authentication=oci.apigateway.models.CustomAuthenticationPolicy(
type="CUSTOM_AUTHENTICATION",
function_id=functionId,
@@ -207,6 +218,7 @@ def applyAuthApi(compartmentId, displayName, payload, functionId, host, api_gate
path_prefix= path_prefix + "validation-callback",
specification=oci.apigateway.models.ApiSpecification(
request_policies=oci.apigateway.models.ApiSpecificationRequestPolicies(
rate_limiting=rate_limiting,
authentication=oci.apigateway.models.CustomAuthenticationPolicy(
type="CUSTOM_AUTHENTICATION",
function_id=functionId,
@@ -244,6 +256,7 @@ def applyAuthApi(compartmentId, displayName, payload, functionId, host, api_gate
display_name=displayName,
specification=oci.apigateway.models.ApiSpecification(
request_policies=oci.apigateway.models.ApiSpecificationRequestPolicies(
rate_limiting=rate_limiting,
authentication=oci.apigateway.models.CustomAuthenticationPolicy(
type="CUSTOM_AUTHENTICATION",
function_id=functionId,
@@ -261,6 +274,7 @@ def applyAuthApi(compartmentId, displayName, payload, functionId, host, api_gate
path_prefix= path_prefix,
specification=oci.apigateway.models.ApiSpecification(
request_policies=oci.apigateway.models.ApiSpecificationRequestPolicies(
rate_limiting=rate_limiting,
authentication=oci.apigateway.models.CustomAuthenticationPolicy(
type="CUSTOM_AUTHENTICATION",
function_id=functionId,
@@ -322,7 +336,7 @@ def group_by(payload):
result_payload.append({"API_NAME": API_NAME, "TYPE": TYPE, "ENVIRONMENT": ENVIRONMENT, "PATH_PREFIX": PATH_PREFIX, "PATH": PATH, "ENDPOINT": ENDPOINT, "METHOD": method_list, "SCHEMA_BODY_VALIDATION": SCHEMA_BODY_VALIDATION})
return result_payload
def process_api_spec(api_id, compartmentId, environment, swagger, functionId, host, api_gateway_id):
def process_api_spec(api_id, compartmentId, environment, swagger, functionId, host, api_gateway_id, rate_limit):
type = "REST"
config = oci.config.from_file("config")
apigateway_client = oci.apigateway.ApiGatewayClient(config)
@@ -444,7 +458,7 @@ def process_api_spec(api_id, compartmentId, environment, swagger, functionId, ho
payload = json.loads(json.dumps(group_by(payload)))
json_data_list = { each['PATH'] : each for each in payload}.values()
print(payload)
applyAuthApi(compartmentId=compartmentId, displayName=API_NAME, payload=json_data_list, functionId=functionId, host=host, api_gateway_id=api_gateway_id)
applyAuthApi(compartmentId=compartmentId, displayName=API_NAME, payload=json_data_list, functionId=functionId, host=host, api_gateway_id=api_gateway_id, rate_limit=rate_limit)
except(Exception) as ex:
jsonData = 'error parsing json payload: ' + str(ex)
@@ -495,6 +509,7 @@ def handler(ctx, data: io.BytesIO = None):
compartmentId = header['apiCompartmentId']
functionId = header['functionId']
api_gateway_id = header['apiGatewayId']
rate_limit = header['rateLimit']
authorization = auth_idcs(access_token, url, options["ClientId"], options["ClientSecret"])
try:
@@ -526,7 +541,7 @@ def handler(ctx, data: io.BytesIO = None):
)
# Create API spec
process_api_spec(api_id=api_id, compartmentId=compartmentId, environment=environment, swagger=swagger, functionId=functionId, host=host, api_gateway_id=api_gateway_id)
process_api_spec(api_id=api_id, compartmentId=compartmentId, environment=environment, swagger=swagger, functionId=functionId, host=host, api_gateway_id=api_gateway_id, rate_limit=rate_limit)
rdata = json.dumps({
"active": True,

BIN
images/img-18e.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 233 KiB