Skip to content

Commit 1435445

Browse files
committed
initial commit iap samples
1 parent c90a83d commit 1435445

File tree

9 files changed

+558
-0
lines changed

9 files changed

+558
-0
lines changed

appengine/iap/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Identity-Aware Proxy sample for Google App Engine
2+
3+
This sample demonstrates how to use the [Identity-Aware Proxy][iap-docs] on [Google App
4+
Engine][ae-docs].
5+
6+
[iap-docs]: https://cloud.google.com/iap/docs/
7+
[ae-docs]: https://cloud.google.com/appengine/docs/java/
8+
9+
## Running locally
10+
11+
This application depends on being enabled behind an IAP, so this program should not be run locally.
12+
13+
## Deploying
14+
15+
- Update [appengine-web.xml](src/main/test/app/src/main/webapp/WEB-INF/appengine-web.xml) with your project-id
16+
- Deploy the application to the project
17+
```
18+
mvn clean appengine:update
19+
```
20+
- [Enable](https://cloud.google.com/iap/docs/app-engine-quickstart) Identity-Aware Proxy on the App Engine app.
21+
- Add the email account you'll be running the test as to the Identity-Aware Proxy access list for the project.
22+
23+
## Test
24+
25+
Once deployed, access `https://your-project-id.appspot.com` . This should now prompt you to sign in for access.
26+
Sign in with the email account that was added to the Identity-Aware proxy access list.
27+
You should now see the jwt token that was received from the IAP server.

appengine/iap/pom.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!--
2+
Copyright 2017 Google Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<packaging>war</packaging>
19+
<version>1.0-SNAPSHOT</version>
20+
<groupId>com.example.appengine</groupId>
21+
<artifactId>iap-demo</artifactId>
22+
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
23+
<parent>
24+
<groupId>com.google.cloud</groupId>
25+
<artifactId>appengine-doc-samples</artifactId>
26+
<version>1.0.0</version>
27+
<relativePath>..</relativePath>
28+
</parent>
29+
<dependencies>
30+
<dependency>
31+
<groupId>javax.servlet</groupId>
32+
<artifactId>servlet-api</artifactId>
33+
<version>2.5</version>
34+
<scope>provided</scope>
35+
</dependency>
36+
</dependencies>
37+
<build>
38+
<!-- for hot reload of the web application -->
39+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.apache.maven.plugins</groupId>
43+
<version>3.3</version>
44+
<artifactId>maven-compiler-plugin</artifactId>
45+
<configuration>
46+
<source>1.7</source>
47+
<target>1.7</target>
48+
</configuration>
49+
</plugin>
50+
<plugin>
51+
<groupId>com.google.appengine</groupId>
52+
<artifactId>appengine-maven-plugin</artifactId>
53+
<version>${appengine.sdk.version}</version>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright 2017 Google Inc.
3+
*
4+
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* <p>http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
* express or implied. See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package com.example.appengine.iap;
15+
16+
import java.io.IOException;
17+
import javax.servlet.http.HttpServlet;
18+
import javax.servlet.http.HttpServletRequest;
19+
import javax.servlet.http.HttpServletResponse;
20+
21+
/**
22+
* Identity Aware Proxy (IAP) Test application to reflect jwt token issued by IAP. IAP must be
23+
* enabled on application. {@see https://cloud.google.com/iap/docs/app-engine-quickstart}
24+
*/
25+
@SuppressWarnings("serial")
26+
public class JwtServlet extends HttpServlet {
27+
28+
private static final String IAP_JWT_HEADER = "x-goog-authenticated-user-jwt";
29+
30+
@Override
31+
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
32+
resp.getWriter().print(IAP_JWT_HEADER + ":" + req.getHeader(IAP_JWT_HEADER));
33+
}
34+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2016 Google Inc.
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
-->
14+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
15+
<application>ja-test-iap-2</application>
16+
<version>alpha-001</version>
17+
<threadsafe>true</threadsafe>
18+
</appengine-web-app>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
3+
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
5+
version="2.5">
6+
<servlet>
7+
<servlet-name>hello</servlet-name>
8+
<servlet-class>com.example.appengine.iap.JwtServlet</servlet-class>
9+
</servlet>
10+
<servlet-mapping>
11+
<servlet-name>hello</servlet-name>
12+
<url-pattern>/</url-pattern>
13+
</servlet-mapping>
14+
</web-app>

iap/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Cloud Identity-Aware Proxy Java Samples
2+
Cloud Identity-Aware Proxy (Cloud IAP) lets you manage access to applications running in Compute Engine, App Engine standard environment, and Container Engine. Cloud IAP establishes a central authorization layer for applications accessed by HTTPS, enabling you to adopt an application-level access control model instead of relying on network-level firewalls. When you enable Cloud IAP, you must also use signed headers or the App Engine standard environment Users API to secure your app.
3+
## Setup
4+
- A Google Cloud project with billing enabled
5+
- [Create an App engine service account](https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow) and download the credentials file as JSON.
6+
- Install the [Google Cloud SDK](https://cloud.google.com/sdk/) and run:
7+
```
8+
gcloud init
9+
gcloud app create
10+
```
11+
12+
## Description
13+
14+
- [BuildIapRequest.java](src/main/java/com/example/iap/BuildIapRequest.java) demonstrates how to set the
15+
`Authorization : Bearer` header to authorize access to an IAP protected URL.
16+
- [VerifyIapRequestHeader.java](src/main/java/com/example/iap/VerifyIapRequestHeader.java) demonstrates how to
17+
verify the JWT token in an incoming request to an IAP protected resource.
18+
19+
## Testing
20+
- Deploy the [demo app engine application](../appengine/iap/README.md). This application will return the JWT token to an authorized incoming request.
21+
It will be used to test both the authorization of an incoming request to an IAP protected resource and the JWT token returned from IAP.
22+
- Update [appengine-web.xml](../appengine/src/main/webapp/WEB-INF/appengine-web.xml)
23+
with your project-id
24+
- Deploy the application to the project
25+
```
26+
mvn clean appengine:update
27+
```
28+
- [Enable](https://cloud.google.com/iap/docs/app-engine-quickstart) Identity-Aware Proxy on the App Engine app.
29+
- Set the environment variable `GOOGLE_APPLICATION_CREDENTIALS` to point to the service account credentials file
30+
- Add the service account email you'll be running the test as to the Identity-Aware Proxy access list for the project.
31+
- Set the environment variable `IAP_PROTECTED_URL` to point to `https://your-project-id.appspot.com`
32+
- Run the integration test:
33+
```
34+
mvn -Dtest=com.example.iap.BuildAndVerifyIapRequestIT verify
35+
```
36+
37+
## References
38+
[JWT library for Java](https://github.com/auth0/java-jwt)
39+
[Cloud IAP docs](https://cloud.google.com/iap/docs/)

iap/pom.xml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2016 Google Inc. All Rights Reserved.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!-- [START pom] -->
18+
<project> <!-- REQUIRED -->
19+
20+
<modelVersion>4.0.0</modelVersion> <!-- REQUIRED -->
21+
<packaging>jar</packaging> <!-- REQUIRED -->
22+
23+
<groupId>com.example</groupId>
24+
<artifactId>iap-samples</artifactId> <!-- Name of your project -->
25+
<version>1.0-SNAPSHOT</version> <!-- xx.xx.xx -SNAPSHOT means development -->
26+
27+
<properties>
28+
<maven.compiler.source>1.8</maven.compiler.source> <!-- REQUIRED -->
29+
<maven.compiler.target>1.8</maven.compiler.target> <!-- REQUIRED -->
30+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
31+
<jetty.maven.plugin>9.4.3.v20170317</jetty.maven.plugin>
32+
</properties>
33+
34+
<dependencyManagement>
35+
<dependencies>
36+
<dependency>
37+
<groupId>com.fasterxml.jackson.core</groupId>
38+
<artifactId>jackson-core</artifactId>
39+
<version>2.8.6</version>
40+
</dependency>
41+
</dependencies>
42+
</dependencyManagement>
43+
44+
<dependencies>
45+
<dependency> <!-- REQUIRED -->
46+
<groupId>javax.servlet</groupId> <!-- Java Servlet API -->
47+
<artifactId>javax.servlet-api</artifactId>
48+
<version>3.1.0</version>
49+
</dependency>
50+
51+
<!-- [START dependencies] -->
52+
<dependency>
53+
<groupId>com.google.auth</groupId>
54+
<artifactId>google-auth-library-oauth2-http</artifactId>
55+
<version>0.6.0</version>
56+
</dependency>
57+
<dependency>
58+
<groupId>com.auth0</groupId>
59+
<artifactId>java-jwt</artifactId>
60+
<version>3.2.0</version>
61+
</dependency>
62+
<!-- [END dependencies] -->
63+
64+
<!-- Test dependencies -->
65+
<dependency>
66+
<groupId>junit</groupId>
67+
<artifactId>junit</artifactId>
68+
<version>4.12</version>
69+
</dependency>
70+
</dependencies>
71+
</project>
72+
<!-- [END pom] -->

0 commit comments

Comments
 (0)