Skip to content

Commit a021aa7

Browse files
authored
Merge pull request oracle-samples#51 from karankap/feature/MPDO-1015
Feature/mpdo 1015
2 parents 37853d0 + 5547861 commit a021aa7

File tree

8 files changed

+357
-201
lines changed

8 files changed

+357
-201
lines changed

samples/oci-apigw-idcs-auth-basic/README.md

Lines changed: 78 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,181 +1,204 @@
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.
1+
# API Gateway Basicauth function using Identity Cloud Service (IDCS)
32

4-
The implementation conforms to the guidelines in the OCI Documentation at https://docs.cloud.oracle.com/en-us/iaas/Content/APIGateway/Tasks/apigatewayusingauthorizerfunction.htm.
3+
This function provides verification of username and password against IDCS at runtime and allows only authorized users to access API gateway deployment.
4+
5+
The implementation conforms to the [documented guidlines for using authorizer functions to add Authentication and Authorization to API deployments](https://docs.cloud.oracle.com/en-us/iaas/Content/APIGateway/Tasks/apigatewayusingauthorizerfunction.htm).
56

67
As you make your way through this tutorial, look out for this icon ![user input icon](./images/userinput.png).
78
Whenever you see it, it's time for you to perform an action.
89

9-
1010
## Prerequisites
1111

1212
[Create users in IDCS](https://docs.oracle.com/en/cloud/paas/identity-cloud/uaids/create-user-accounts.html)
1313

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
14+
Before you deploy this sample function, you need to complete steps 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+
16+
- A - Set up your tenancy
17+
- B - Create application
18+
- C - Set up your Cloud Shell dev environment
1819

1920
## List Applications
21+
2022
Assuming your have successfully completed the prerequisites, you should see your
2123
application in the list of applications.
24+
2225
```
2326
fn ls apps
2427
```
2528

2629
## Deploy a function that implements an API
30+
2731
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,
29-
push the image to OCIR, and deploy the function to Oracle Functions in your application.
32+
In Cloud Shell, run `fn deploy` to build the function and its dependencies as a container,
33+
push the image to Oracle Cloud Infrastructure Registry (OCIR), and deploy the function to Oracle Functions in your application.
3034

3135
![user input icon](./images/userinput.png)
36+
3237
```
3338
cd ../oci-display-httprequest-info-python
3439
fn -v deploy --app <app-name>
3540
```
3641

3742
## Create or Update your Dynamic Group for API Gateway
43+
3844
In order to invoke functions, your API Gateway must be part of a dynamic group.
3945

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+
4148
```
4249
ALL {resource.type = 'ApiGateway', resource.compartment.id = 'ocid1.compartment.oc1..aaaaaxxxxx'}
4350
```
4451

45-
4652
## Create or Update IAM Policies for API Gateway
53+
4754
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.
4855

4956
![user input icon](./images/userinput.png)
5057

5158
Your policy should look something like this:
59+
5260
```
5361
Allow dynamic-group <dynamic-group-name> to use functions-family in compartment <compartment-name>
5462
```
5563

5664
For more information on how to create policies, check the [documentation](https://docs.cloud.oracle.com/iaas/Content/Identity/Concepts/policysyntax.htm).
5765

58-
5966
## Configure Identity Cloud Service (IDCS)
67+
6068
Login to IDCS admin console and create, add an Application and select "Confidential Application".
6169
![IDCS-appcreate0](./images/IDCS-appcreate0.png)
6270

6371
Enter a name for your IDCS Application, for example "myAPI".
6472

6573
![IDCS-appcreate1](./images/IDCS-appcreate1.png)
6674

67-
For "Allowed Grant Types", select "Resource Owner". Click *Next*.
75+
For "Allowed Grant Types", select "Resource Owner". Click _Next_.
6876

6977
![IDCS-appcreate2](./images/IDCS-appcreate2.png)
7078

7179
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_.
7381

7482
![IDCS-appcreate3](./images/IDCS-appcreate3.png)
7583

76-
Click *Next*.
84+
Click _Next_.
7785

7886
![IDCS-appcreate4](./images/IDCS-appcreate4.png)
7987

80-
Click *Finish*.
88+
Click _Finish_.
8189

8290
![IDCS-appcreate5](./images/IDCS-appcreate5.png)
8391

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_.
8593

8694
![IDCS-appcreate6](./images/IDCS-appcreate6.png)
8795

88-
Click *Close*.
96+
Click _Close_.
8997

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.
9199

92100
![IDCS-appcreate7](./images/IDCS-appcreate7.png)
93101
![IDCS-appcreate8](./images/IDCS-appcreate8.png)
94102

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.
99104

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.
100106

101107
## Review and customize the function
108+
102109
Review the following files in the current folder:
103-
- [pom.xml](./pom.xml) specifies all the dependencies for your function
104-
- [func.yaml](./func.yaml) that contains metadata about your function and declares properties
105-
- [src/main/java/com/example/fn/BasicAuth.java](./src/main/java/com/example/fn/BasicAuth.java) which contains the Java code
106110

107-
The name of your function *basicauth* is specified in [func.yaml](./func.yaml).
111+
- [`pom.xml`](./pom.xml) specifies all the dependencies for your function
112+
- [`func.yaml`](./func.yaml) that contains metadata about your function and declares properties
113+
- [`src/main/java/com/example/fn/BasicAuth.java`](./src/main/java/com/example/fn/BasicAuth.java) which contains the Java code
108114

109-
set the following variable in "src/main/java/com/example/utils/ResourceServerConfig.java" to the values noted while configuring IDCS.
110-
```
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";
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. The IDCS URL is the token endpoint that returns the access token after validating credentials
118+
119+
```java
120+
CLIENT_ID = "xxxxxxxxxxx";
121+
CLIENT_SECRET = "xxxxxxxxx";
122+
IDCS_URL = "https://idcs-xxxxxxxx.identity.oraclecloud.com/oauth2/v1/token";
114123

115124
//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";
117126
```
118127

128+
For the unit test to run, set the following variables in src/test/java/com/example/fn/BasicAuthTest.java
129+
130+
```java
131+
private static final String TEST_IDCS_URL = "https://idcs-xxxxxxxx.identity.oraclecloud.com/oauth2/v1/token";
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+
```
119137

120138
## 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 `fn deploy` to build the function and its dependencies as a container,
122141
push the image to OCIR, and deploy the function to Oracle Functions in your application.
123142

124143
![user input icon](./images/userinput.png)
125-
```
144+
145+
```shell
126146
fn -v deploy --app <app-name>
127147
```
148+
128149
## 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` to invoke the deployed function. It should return an active status of true if the token is valid or otherwise returns false.
130152

131153
![user input icon](./images/userinput.png)
132-
```
154+
155+
```shell
133156
echo -n '{"type":"TOKEN", "token":"Basic aW5jaGFyYS5zaGFtYW5uYUBvcmFj....."}' | fn invoke <app-name> <func-name>
134157
```
135158

136159
## Create the API Gateway
160+
137161
The functions is meant to be invoked through API Gateway.
138162

139163
![user input icon](./images/userinput.png)
140164

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_.
142166

143167
![APIGW create](./images/apigw-create.png)
144168

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_.
146170

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"
153177

154-
Click *Save Changes* when you are finished
178+
Click _Save Changes_ when you are finished
155179
![APIGW deployment create](./images/apigw-deployment-create.png)
156180

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.
158182

159183
![APIGW deployment create](./images/apigw-deployment-create-route.png)
160184

161-
Click *Next* and finally, click *Save Changes*.
185+
Click _Next_ and finally, click _Save Changes_.
162186

163187
Note the endpoint of your API Gateway deployment.
164188

165189
![APIGW deployment endpoint](./images/apigw-deployment-endpoint.png)
166190

167-
168191
## Invoke the Deployment endpoint
192+
169193
The function validates if the user information is valid.
170194

171195
![user input icon](./images/userinput.png)
172196

173-
Use the curl command to make the HTTP request
174-
```
197+
Use `curl` to make the HTTP request
198+
199+
```shell
175200
curl -i -u "<username>:<password>" https://d6xxxxxxxxk64.apigateway.us-ashburn-1.oci.customer-oci.com/basicauth/hello
176201
```
202+
177203
If the user is valid gateway will make a call to backend with HTTP200 else
178204
The gateway will reject the request with an HTTP401.
179-
180-
181-
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
schema_version: 20180708
22
name: basicauth
3-
version: 0.0.6
3+
version: 0.0.21
44
runtime: java
55
build_image: fnproject/fn-java-fdk-build:jdk11-1.0.146
66
run_image: fnproject/fn-java-fdk:jre11-1.0.146
77
cmd: com.example.fn.BasicAuth::handleRequest
8+
memory: 512
9+
timeout: 120

samples/oci-apigw-idcs-auth-basic/pom.xml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
53
<modelVersion>4.0.0</modelVersion>
64
<properties>
75
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
86
<fdk.version>1.0.146</fdk.version>
7+
<jdk.version>11</jdk.version>
98
</properties>
9+
1010
<groupId>com.example.fn</groupId>
1111
<artifactId>basicAuth</artifactId>
12-
<version>1.0.0</version>
12+
<version>1.0.1</version>
1313

1414
<dependencies>
1515
<dependency>
@@ -49,18 +49,18 @@
4949
<artifactId>maven-compiler-plugin</artifactId>
5050
<version>3.3</version>
5151
<configuration>
52-
<source>11</source>
53-
<target>11</target>
52+
<source>${jdk.version}</source>
53+
<target>${jdk.version}</target>
5454
</configuration>
5555
</plugin>
5656
<plugin>
57-
<groupId>org.apache.maven.plugins</groupId>
58-
<artifactId>maven-surefire-plugin</artifactId>
59-
<version>2.22.1</version>
60-
<configuration>
61-
<useSystemClassLoader>false</useSystemClassLoader>
62-
</configuration>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-surefire-plugin</artifactId>
59+
<version>2.22.1</version>
60+
<configuration>
61+
<useSystemClassLoader>false</useSystemClassLoader>
62+
</configuration>
6363
</plugin>
6464
</plugins>
6565
</build>
66-
</project>
66+
</project>

0 commit comments

Comments
 (0)