Skip to content

Commit 4f21d84

Browse files
jabubakelesv
authored andcommitted
Fixing IAP tests (#804)
* fixing IAP tests * updating README * enabling IAP
1 parent 1caa322 commit 4f21d84

File tree

4 files changed

+25
-31
lines changed

4 files changed

+25
-31
lines changed

appengine/iap/src/main/java/com/example/appengine/iap/JwtServlet.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
public class JwtServlet extends HttpServlet {
2727

2828
private static final String IAP_JWT_HEADER = "x-goog-iap-jwt-assertion";
29+
private static final String IAP_AUTHENTICATED_USER_HEADER = "x-goog-authenticated-user-jwt";
2930

3031
@Override
3132
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
32-
resp.getWriter().print(IAP_JWT_HEADER + ":" + req.getHeader(IAP_JWT_HEADER));
33+
resp.getWriter().print(IAP_AUTHENTICATED_USER_HEADER + ":" + req.getHeader(IAP_JWT_HEADER));
3334
}
3435
}

iap/README.md

+9-11
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,22 @@ It will be used to test both the authorization of an incoming request to an IAP
2828

2929
- Add the service account email to the Identity-Aware Proxy access list for the project.
3030

31-
- Set the following environment variables to test sending a request to an IAP protected resource:
31+
- Update the following variables in [BuildAndVerifyIapRequestIT.java](src/test/java/com/example/iap/BuildAndVerifyIapRequestIT.java):
3232
- `IAP_PROTECTED_URL` : URL of your IAP protected resource . eg. `https://your-project-id.appspot.com`
3333

3434
- `IAP_CLIENT_ID` to point to the [OAuth 2.0 Client ID](https://console.cloud.google.com/apis/credentials) of your IAP protected App Engine Application.
3535

36-
- Set the following environment variables to test verifying a JWT issued for an App Engine protected application:
37-
- `GOOGLE_CLOUD_PROJECT`: Google Cloud Project ID
36+
- `IAP_PROJECT_ID` : Google Cloud Project ID of the IAP protected application
3837

3938
- `IAP_PROJECT_NUMBER` : [Project number](https://console.cloud.google.com/home/dashboard) of the IAP protected resource.
40-
Also available via `gcloud` using:
41-
```
42-
gcloud projects describe PROJECT_ID
43-
```
44-
39+
Also available via `gcloud` using:
40+
```
41+
gcloud projects describe PROJECT_ID
42+
```
4543
- Run the integration test:
46-
```
47-
mvn -Dtest=com.example.iap.BuildAndVerifyIapRequestIT verify
48-
```
44+
```
45+
mvn -Dtest=com.example.iap.BuildAndVerifyIapRequestIT verify
46+
```
4947
5048
## References
5149
- [JWT library for Java (jjwt)](https://github.com/jwtk/jjwt)

iap/src/test/java/com/example/iap/BuildAndVerifyIapRequestIT.java

+14-17
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,29 @@
2626
import com.google.api.client.http.javanet.NetHttpTransport;
2727
import io.jsonwebtoken.Jwt;
2828
import org.apache.http.HttpStatus;
29-
import org.junit.Before;
3029
import org.junit.Test;
3130
import org.junit.runner.RunWith;
3231
import org.junit.runners.JUnit4;
3332

3433
@RunWith(JUnit4.class)
3534
public class BuildAndVerifyIapRequestIT {
3635

37-
private String iapProtectedUrl = System.getenv("IAP_PROTECTED_URL");
38-
private String iapClientId = System.getenv("IAP_CLIENT_ID");
39-
private Long projectNumber = Long.parseLong(System.getenv("IAP_PROJECT_NUMBER"));
40-
private String projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
36+
// Update these fields to reflect your IAP protected App Engine credentials
37+
private static Long IAP_PROJECT_NUMBER = 320431926067L;
38+
private static String IAP_PROJECT_ID = "gcp-devrel-iap-reflect";
39+
private static String IAP_PROTECTED_URL = "https://gcp-devrel-iap-reflect.appspot.com";
40+
private static String IAP_CLIENT_ID =
41+
"320431926067-ldm6839p8l2sei41nlsfc632l4d0v2u1.apps.googleusercontent.com";
42+
4143
private HttpTransport httpTransport = new NetHttpTransport();
4244
private VerifyIapRequestHeader verifyIapRequestHeader = new VerifyIapRequestHeader();
4345

44-
@Before
45-
public void setUp() {
46-
assertNotNull(iapProtectedUrl);
47-
assertNotNull(iapClientId);
48-
}
4946

5047
// Access an IAP protected url without signed jwt authorization header
5148
@Test
5249
public void accessIapProtectedResourceFailsWithoutJwtHeader() throws Exception {
5350
HttpRequest request =
54-
httpTransport.createRequestFactory().buildGetRequest(new GenericUrl(iapProtectedUrl));
51+
httpTransport.createRequestFactory().buildGetRequest(new GenericUrl(IAP_PROTECTED_URL));
5552
try {
5653
request.execute();
5754
} catch (HttpResponseException e) {
@@ -63,23 +60,23 @@ public void accessIapProtectedResourceFailsWithoutJwtHeader() throws Exception {
6360
@Test
6461
public void testGenerateAndVerifyIapRequestIsSuccessful() throws Exception {
6562
HttpRequest request =
66-
httpTransport.createRequestFactory().buildGetRequest(new GenericUrl(iapProtectedUrl));
67-
HttpRequest iapRequest = buildIAPRequest(request, iapClientId);
63+
httpTransport.createRequestFactory().buildGetRequest(new GenericUrl(IAP_PROTECTED_URL));
64+
HttpRequest iapRequest = buildIAPRequest(request, IAP_CLIENT_ID);
6865
HttpResponse response = iapRequest.execute();
6966
assertEquals(response.getStatusCode(), HttpStatus.SC_OK);
7067
String headerWithtoken = response.parseAsString();
7168
String[] split = headerWithtoken.split(":");
7269
assertNotNull(split);
73-
assertEquals(split.length, 2);
74-
assertEquals(split[0].trim(), "x-goog-iap-jwt-assertion");
70+
assertEquals(2, split.length);
71+
assertEquals("x-goog-authenticated-user-jwt", split[0].trim());
7572

7673
String jwtToken = split[1].trim();
7774
HttpRequest verifyJwtRequest = httpTransport
7875
.createRequestFactory()
79-
.buildGetRequest(new GenericUrl(iapProtectedUrl)).setHeaders(
76+
.buildGetRequest(new GenericUrl(IAP_PROTECTED_URL)).setHeaders(
8077
new HttpHeaders().set("x-goog-iap-jwt-assertion", jwtToken));
8178
Jwt decodedJWT = verifyIapRequestHeader.verifyJWTTokenForAppEngine(
82-
verifyJwtRequest, projectNumber, projectId);
79+
verifyJwtRequest, IAP_PROJECT_NUMBER, IAP_PROJECT_ID);
8380
assertNotNull(decodedJWT);
8481
}
8582
}

pom.xml

-2
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@
5555

5656
<module>dlp</module>
5757

58-
<!-- TODO() turn back on once we setup IAP envvar
5958
<module>iap</module>
60-
-->
6159

6260
<module>kms</module>
6361

0 commit comments

Comments
 (0)