|
| 1 | +# Function that validates an API key |
| 2 | +This is a sample function that can be used for a simple "API-Key" validation. An API-Key is a simple method for securing an API by requiring the client to pass a specific token. The gateway can use this as a custom authorizer to validate requests and only allow clients who pass the API-Key. |
| 3 | + |
| 4 | +As you make your way through this tutorial, look out for this icon . |
| 5 | +Whenever you see it, it's time for you to perform an action. |
| 6 | + |
| 7 | + |
| 8 | +## Prerequisites |
| 9 | +Before you deploy this sample function, make sure you have run step A, B and C of the [Oracle Functions Quick Start Guide for Cloud Shell](https://www.oracle.com/webfolder/technetwork/tutorials/infographics/oci_functions_cloudshell_quickview/functions_quickview_top/functions_quickview/index.html) |
| 10 | +* A - Set up your tenancy |
| 11 | +* B - Create application |
| 12 | +* C - Set up your Cloud Shell dev environment |
| 13 | +* D - Choose an back-end you would like to protect. This can be any back-end you already have access to, or you can use the [Display HTTP Request sample](../oci-apigw-display-httprequest-info-python) |
| 14 | + |
| 15 | + |
| 16 | +## List Applications |
| 17 | +Assuming your have successfully completed the prerequisites, you should see your |
| 18 | +application in the list of applications. |
| 19 | +``` |
| 20 | +fn ls apps |
| 21 | +``` |
| 22 | + |
| 23 | + |
| 24 | +## Review and customize the function |
| 25 | +Review the following files in the current folder: |
| 26 | +* the code of the function, [func.py](./func.py) |
| 27 | +* its dependencies, [requirements.txt](./requirements.txt) |
| 28 | +* the function metadata, [func.yaml](./func.yaml) |
| 29 | + |
| 30 | + |
| 31 | +## Deploy the function |
| 32 | +In Cloud Shell, run the *fn deploy* command to build the function and its dependencies as a Docker image, |
| 33 | +push the image to OCIR, and deploy the function to Oracle Functions in your application. |
| 34 | + |
| 35 | + |
| 36 | +``` |
| 37 | +fn -v deploy --app <app-name> |
| 38 | +``` |
| 39 | + |
| 40 | + |
| 41 | +## Set the function configuration values |
| 42 | +The function requires the following configuration values to be set: |
| 43 | +- FN_API_KEY |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +Use the *fn CLI* to set the config value: |
| 48 | +``` |
| 49 | +fn config function <app-name> <function-name> FN_API_KEY <API key value> |
| 50 | +``` |
| 51 | +e.g. |
| 52 | +``` |
| 53 | +fn config function myapp oci-apigw-apikey-validation-python FN_API_KEY XXXXXXXXXXXX |
| 54 | +``` |
| 55 | +Remember what you chose as you will need it later |
| 56 | + |
| 57 | +## Create the API Gateway |
| 58 | +The functions is meant to be invoked through API Gateway. |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +On the OCI console, navigate to *Developer Services* > *API Gateway*. Click on `Create Gateway`. Provide a name, set the type to "Public", select a compartment, a VCN, a public subnet, and click `Create`. |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +Once created, click on your gateway. Under *Resources*, select *Deployments* and click `Create Deployment`. |
| 67 | + |
| 68 | + * Provide a name, a path prefix ("/fn-samples/v1/python" for example). |
| 69 | + * Add Authentication |
| 70 | + * Authentication Type: *Custom* |
| 71 | + * Choose the application and function you created for your API-Key validation function |
| 72 | + * Choose your Authentication token, you can use either Header or Query Parameter |
| 73 | + * Give a name to the token |
| 74 | + |
| 75 | +Click *Save Changes* when you are finished |
| 76 | + |
| 77 | + |
| 78 | +click `Next`. Provide a name to the route ("/http-info" for example), select methods "GET" and "POST", select your back-end of choice such as if you have the . |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +Click `Next` and finally, click `Save Changes`. |
| 83 | + |
| 84 | +Note the endpoint of your API Gateway deployment. |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +## Create or Update your Dynamic Group for API Gateway |
| 90 | +In order to invoke functions, your API Gateway must be part of a dynamic group. |
| 91 | + |
| 92 | +When specifying the *Matching Rules*, we suggest matching all functions in a compartment with: |
| 93 | +``` |
| 94 | +ALL {resource.type = 'ApiGateway', resource.compartment.id = 'ocid1.compartment.oc1..aaaaaxxxxx'} |
| 95 | +``` |
| 96 | + |
| 97 | + |
| 98 | +## Create or Update IAM Policies for API Gateway |
| 99 | +Create a new policy that allows the API Gateway dynamic group to invoke functions. We will grant `use` access to `functions-family` in the compartment. |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +Your policy should look something like this: |
| 104 | +``` |
| 105 | +Allow dynamic-group <dynamic-group-name> to use functions-family in compartment <compartment-name> |
| 106 | +``` |
| 107 | + |
| 108 | +For more information on how to create policies, check the [documentation](https://docs.cloud.oracle.com/iaas/Content/Identity/Concepts/policysyntax.htm). |
| 109 | + |
| 110 | + |
| 111 | +## Invoke the function |
| 112 | +The function returns the information of the HTTP request through API Gateway. |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +Set the Environment variable "APIGW_ENDPOINT" to the value of the endpoint of your API Gateway deployment, e.g. |
| 117 | +``` |
| 118 | +export APIGW_ENDPOINT=https://xxxxx.apigateway.us-phoenix-1.oci.customer-oci.com/v1 |
| 119 | +``` |
| 120 | + |
| 121 | +Use the curl command to make the HTTP request and specify the API Key in your request: |
| 122 | +``` |
| 123 | +curl -v -H "x-api-key:<value from FN_API_KEY>" $APIGW_ENDPOINT/http-info |
| 124 | +``` |
| 125 | +You should receive the information from the HTTP request (or whatever back-end you chose) |
| 126 | + |
| 127 | +Try sending a request with a non-matching key, or no key at all. |
| 128 | + |
| 129 | +The gateway will reject the request with an HTTP401 |
0 commit comments