|
1 | 1 | # API Gateway Basicauth function using IDCS
|
2 |
| -This function provides verification of username and password against IDCS at runtime and allows only authorized users to access API gateway deployment. |
| 2 | + |
| 3 | +This function provides verification of username and password against IDCS at runtime and allows only authorized users to access API gateway deployment. |
3 | 4 |
|
4 | 5 | The implementation conforms to the guidelines in the OCI Documentation at https://docs.cloud.oracle.com/en-us/iaas/Content/APIGateway/Tasks/apigatewayusingauthorizerfunction.htm.
|
5 | 6 |
|
6 | 7 | As you make your way through this tutorial, look out for this icon .
|
7 | 8 | Whenever you see it, it's time for you to perform an action.
|
8 | 9 |
|
9 |
| - |
10 | 10 | ## Prerequisites
|
11 | 11 |
|
12 | 12 | [Create users in IDCS](https://docs.oracle.com/en/cloud/paas/identity-cloud/uaids/create-user-accounts.html)
|
13 | 13 |
|
14 | 14 | 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)
|
15 |
| -* A - Set up your tenancy |
16 |
| -* B - Create application |
17 |
| -* C - Set up your Cloud Shell dev environment |
| 15 | + |
| 16 | +- A - Set up your tenancy |
| 17 | +- B - Create application |
| 18 | +- C - Set up your Cloud Shell dev environment |
18 | 19 |
|
19 | 20 | ## List Applications
|
| 21 | + |
20 | 22 | Assuming your have successfully completed the prerequisites, you should see your
|
21 | 23 | application in the list of applications.
|
| 24 | + |
22 | 25 | ```
|
23 | 26 | fn ls apps
|
24 | 27 | ```
|
25 | 28 |
|
26 | 29 | ## Deploy a function that implements an API
|
| 30 | + |
27 | 31 | We need another function that will be a target for API Gateway. We suggest [oci-display-httprequest-info-python](../oci-display-httprequest-info-python).
|
28 |
| -In Cloud Shell, run the *fn deploy* command to build the function and its dependencies as a Docker image, |
| 32 | +In Cloud Shell, run the _fn deploy_ command to build the function and its dependencies as a Docker image, |
29 | 33 | push the image to OCIR, and deploy the function to Oracle Functions in your application.
|
30 | 34 |
|
31 | 35 | 
|
| 36 | + |
32 | 37 | ```
|
33 | 38 | cd ../oci-display-httprequest-info-python
|
34 | 39 | fn -v deploy --app <app-name>
|
35 | 40 | ```
|
36 | 41 |
|
37 | 42 | ## Create or Update your Dynamic Group for API Gateway
|
| 43 | + |
38 | 44 | In order to invoke functions, your API Gateway must be part of a dynamic group.
|
39 | 45 |
|
40 |
| -When specifying the *Matching Rules*, we suggest matching all functions in a compartment with: |
| 46 | +When specifying the _Matching Rules_, we suggest matching all functions in a compartment with: |
| 47 | + |
41 | 48 | ```
|
42 | 49 | ALL {resource.type = 'ApiGateway', resource.compartment.id = 'ocid1.compartment.oc1..aaaaaxxxxx'}
|
43 | 50 | ```
|
44 | 51 |
|
45 |
| - |
46 | 52 | ## Create or Update IAM Policies for API Gateway
|
| 53 | + |
47 | 54 | 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.
|
48 | 55 |
|
49 | 56 | 
|
50 | 57 |
|
51 | 58 | Your policy should look something like this:
|
| 59 | + |
52 | 60 | ```
|
53 | 61 | Allow dynamic-group <dynamic-group-name> to use functions-family in compartment <compartment-name>
|
54 | 62 | ```
|
55 | 63 |
|
56 | 64 | For more information on how to create policies, check the [documentation](https://docs.cloud.oracle.com/iaas/Content/Identity/Concepts/policysyntax.htm).
|
57 | 65 |
|
58 |
| - |
59 | 66 | ## Configure Identity Cloud Service (IDCS)
|
| 67 | + |
60 | 68 | Login to IDCS admin console and create, add an Application and select " Application".
|
61 | 69 | 
|
62 | 70 |
|
63 | 71 | Enter a name for your IDCS Application, for example "myAPI".
|
64 | 72 |
|
65 | 73 | 
|
66 | 74 |
|
67 |
| -For "Allowed Grant Types", select "Resource Owner". Click *Next*. |
| 75 | +For "Allowed Grant Types", select "Resource Owner". Click _Next_. |
68 | 76 |
|
69 | 77 | 
|
70 | 78 |
|
71 | 79 | For Primary Audience, enter anything "display-httprequest-info" for example.
|
72 |
| -For Scopes, click *Add*. In the dialog box, for field "Scope", enter anything "display-httprequest-info" for example, click *Add*. |
| 80 | +For Scopes, click _Add_. In the dialog box, for field "Scope", enter anything "display-httprequest-info" for example, click _Add_. |
73 | 81 |
|
74 | 82 | 
|
75 | 83 |
|
76 |
| -Click *Next*. |
| 84 | +Click _Next_. |
77 | 85 |
|
78 | 86 | 
|
79 | 87 |
|
80 |
| -Click *Finish*. |
| 88 | +Click _Finish_. |
81 | 89 |
|
82 | 90 | 
|
83 | 91 |
|
84 |
| -Now that the application is added, note the *Client ID* and *Client Secret*. |
| 92 | +Now that the application is added, note the _Client ID_ and _Client Secret_. |
85 | 93 |
|
86 | 94 | 
|
87 | 95 |
|
88 |
| -Click *Close*. |
| 96 | +Click _Close_. |
89 | 97 |
|
90 |
| -Click on Configurations tab under Client Information section click on add scope and select the *application name* from the dropdown. Note the scope value. |
| 98 | +Click on Configurations tab under Client Information section click on add scope and select the _application name_ from the dropdown. Note the scope value. |
91 | 99 |
|
92 | 100 | 
|
93 | 101 | 
|
94 | 102 |
|
95 |
| -Click *Activate* and click *Ok* in the dialog. |
96 |
| - |
97 |
| -Note the *IDCS URL*, this is the URL you see in your browser URL bar, copy the IDCS url ( For example: https://idcs-xxxxxxxxxxx.identity.oraclecloud.com/ ), client-id, client-secret and scope these values are provided to the Basicauth function. |
98 |
| - |
| 103 | +Click _Activate_ and click _Ok_ in the dialog. |
99 | 104 |
|
| 105 | +Note the _IDCS URL_, this is the URL you see in your browser URL bar, copy the IDCS url ( For example: https://idcs-xxxxxxxxxxx.identity.oraclecloud.com/ ), client-id, client-secret and scope these values are provided to the Basicauth function. |
100 | 106 |
|
101 | 107 | ## Review and customize the function
|
| 108 | + |
102 | 109 | Review the following files in the current folder:
|
| 110 | + |
103 | 111 | - [pom.xml](./pom.xml) specifies all the dependencies for your function
|
104 | 112 | - [func.yaml](./func.yaml) that contains metadata about your function and declares properties
|
105 | 113 | - [src/main/java/com/example/fn/BasicAuth.java](./src/main/java/com/example/fn/BasicAuth.java) which contains the Java code
|
106 | 114 |
|
107 |
| -The name of your function *basicauth* is specified in [func.yaml](./func.yaml). |
| 115 | +The name of your function _basicauth_ is specified in [func.yaml](./func.yaml). |
| 116 | + |
| 117 | +set the following config variables to the values noted while configuring IDCS. |
108 | 118 |
|
109 |
| -set the following variable in "src/main/java/com/example/utils/ResourceServerConfig.java" to the values noted while configuring IDCS. |
110 | 119 | ```
|
111 |
| -public static final String CLIENT_ID = "xxxxxxxxxxx"; |
112 |
| -public static final String CLIENT_SECRET = "xxxxxxxxx"; |
113 |
| -public static final String IDCS_URL = "https://idcs-xxxxxxxx.identity.oraclecloud.com"; |
| 120 | +CLIENT_ID = "xxxxxxxxxxx"; |
| 121 | +CLIENT_SECRET = "xxxxxxxxx"; |
| 122 | +IDCS_URL = "https://idcs-xxxxxxxx.identity.oraclecloud.com"; |
114 | 123 |
|
115 | 124 | //INFORMATION ABOUT THE TARGET APPLICATION
|
116 |
| -public static final String SCOPE_AUD = "display-httprequest-infodisplay-httprequest-info"; |
| 125 | +SCOPE_AUD = "display-httprequest-infodisplay-httprequest-info"; |
117 | 126 | ```
|
118 | 127 |
|
| 128 | +For the unit test to run, set the following variables in src/test/java/com/example/fn/BasicAuthTest.java |
| 129 | + |
| 130 | +``` |
| 131 | +private static final String TEST_IDCS_URL = "https://idcs-xxxxxxxx.identity.oraclecloud.com"; |
| 132 | +private static final String TEST_CLIENT_ID = "xxxxxxxxxxx"; |
| 133 | +private static final String TEST_CLIENT_SECRET = "xxxxxxxxxxx"; |
| 134 | +private static final String TEST_SCOPE_AUD = "display-httprequest-infodisplay-httprequest-info"; |
| 135 | +private static final String TEST_TOKEN = "xxxxxxxxxxx"; |
| 136 | +``` |
119 | 137 |
|
120 | 138 | ## Deploy the basicauth function
|
121 |
| -In Cloud Shell, run the *fn deploy* command to build the function and its dependencies as a Docker image, |
| 139 | + |
| 140 | +In Cloud Shell, run the _fn deploy_ command to build the function and its dependencies as a Docker image, |
122 | 141 | push the image to OCIR, and deploy the function to Oracle Functions in your application.
|
123 | 142 |
|
124 | 143 | 
|
| 144 | + |
125 | 145 | ```
|
126 | 146 | fn -v deploy --app <app-name>
|
127 | 147 | ```
|
| 148 | + |
128 | 149 | ## Invoke the basicauth function in cloud shell
|
129 |
| -In Cloud Shell, run *fn invoke* command to invoke the deployed function, returns active status as true if the token is valid or else returns false. |
| 150 | + |
| 151 | +In Cloud Shell, run _fn invoke_ command to invoke the deployed function, returns active status as true if the token is valid or else returns false. |
130 | 152 |
|
131 | 153 | 
|
| 154 | + |
132 | 155 | ```
|
133 | 156 | echo -n '{"type":"TOKEN", "token":"Basic aW5jaGFyYS5zaGFtYW5uYUBvcmFj....."}' | fn invoke <app-name> <func-name>
|
134 | 157 | ```
|
135 | 158 |
|
136 | 159 | ## Create the API Gateway
|
| 160 | + |
137 | 161 | The functions is meant to be invoked through API Gateway.
|
138 | 162 |
|
139 | 163 | 
|
140 | 164 |
|
141 |
| -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*. |
| 165 | +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_. |
142 | 166 |
|
143 | 167 | 
|
144 | 168 |
|
145 |
| -Once created, click on your gateway. Under *Resources*, select *Deployments* and click *Create Deployment*. |
| 169 | +Once created, click on your gateway. Under _Resources_, select _Deployments_ and click _Create Deployment_. |
146 | 170 |
|
147 |
| -* Provide a name, a path prefix ("/basicauth" for example). |
148 |
| -* Under *API Request Policies* Add Authentication |
149 |
| -* Authentication Type: *Custom* |
150 |
| -* Choose the application and the basicauth function |
151 |
| -* For "Authentication token", select *Header* |
152 |
| -* For the "Header Name", enter "Autorization" |
| 171 | +- Provide a name, a path prefix ("/basicauth" for example). |
| 172 | +- Under _API Request Policies_ Add Authentication |
| 173 | +- Authentication Type: _Custom_ |
| 174 | +- Choose the application and the basicauth function |
| 175 | +- For "Authentication token", select _Header_ |
| 176 | +- For the "Header Name", enter "Autorization" |
153 | 177 |
|
154 |
| -Click *Save Changes* when you are finished |
| 178 | +Click _Save Changes_ when you are finished |
155 | 179 | 
|
156 | 180 |
|
157 |
| -Click *Next*. Provide a name to the route ("/hello" for example), select methods eg: "GET", select *HTTP-URL* for your back-end. |
| 181 | +Click _Next_. Provide a name to the route ("/hello" for example), select methods eg: "GET", select _HTTP-URL_ for your back-end. |
158 | 182 |
|
159 | 183 | 
|
160 | 184 |
|
161 |
| -Click *Next* and finally, click *Save Changes*. |
| 185 | +Click _Next_ and finally, click _Save Changes_. |
162 | 186 |
|
163 | 187 | Note the endpoint of your API Gateway deployment.
|
164 | 188 |
|
165 | 189 | 
|
166 | 190 |
|
167 |
| - |
168 | 191 | ## Invoke the Deployment endpoint
|
| 192 | + |
169 | 193 | The function validates if the user information is valid.
|
170 | 194 |
|
171 | 195 | 
|
172 | 196 |
|
173 | 197 | Use the curl command to make the HTTP request
|
| 198 | + |
174 | 199 | ```
|
175 | 200 | curl -i -u "<username>:<password>" https://d6xxxxxxxxk64.apigateway.us-ashburn-1.oci.customer-oci.com/basicauth/hello
|
176 | 201 | ```
|
| 202 | + |
177 | 203 | If the user is valid gateway will make a call to backend with HTTP200 else
|
178 | 204 | The gateway will reject the request with an HTTP401.
|
179 |
| - |
180 |
| - |
181 |
| - |
|
0 commit comments