Skip to content

Commit

Permalink
Merge pull request #252 from GoogleCloudPlatform:secureRequest-HttpCo…
Browse files Browse the repository at this point in the history
…nnectorMode

PiperOrigin-RevId: 653875419
Change-Id: Ie953edcf1ec99944300a301259b87050e079f1d5
  • Loading branch information
gae-java-bot committed Jul 19, 2024
2 parents 9da58a4 + a34e8dc commit ec7bd83
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ public boolean isSecure() {
};

this.baseRequest = request;
this.baseRequest.setSecure(isSecure);
this.baseRequest.setHttpURI(httpUri);
}

public Request getBaseRequest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@

package com.google.apphosting.runtime.jetty9;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;

import com.google.common.flogger.GoogleLogger;
import java.util.Arrays;
import java.util.Collection;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.http.HttpStatus;
Expand All @@ -26,51 +34,72 @@
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.runners.Parameterized;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
@RunWith(Parameterized.class)
public class TransportGuaranteeTest extends JavaRuntimeViaHttpBase {

@RunWith(JUnit4.class)
public class TransportGuarenteeTest extends JavaRuntimeViaHttpBase {
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(
new Object[][] {
{"jetty94", false},
{"jetty94", true},
{"ee8", false},
{"ee8", true},
{"ee10", false},
{"ee10", true},
});
}

private static final GoogleLogger logger = GoogleLogger.forEnclosingClass();
@Rule public TemporaryFolder temp = new TemporaryFolder();
private HttpClient httpClient;
private RuntimeContext<?> runtime;
private final boolean httpMode;
private final String environment;

public TransportGuaranteeTest(String environment, boolean httpMode) {
this.environment = environment;
this.httpMode = httpMode;
System.setProperty("appengine.use.HttpConnector", Boolean.toString(httpMode));
}

private RuntimeContext<?> runtimeContext() throws Exception {
RuntimeContext.Config<?> config =
RuntimeContext.Config.builder().setApplicationPath(temp.getRoot().toString()).build();
RuntimeContext.Config.builder().setApplicationPath(temp.getRoot().toString()).build();
return RuntimeContext.create(config);
}

@Before
public void before() throws Exception {
copyAppToDir("transportguaranteeapp", temp.getRoot().toPath());
String app = "transportguaranteeapp-" + environment;
copyAppToDir(app, temp.getRoot().toPath());

SslContextFactory ssl = new SslContextFactory.Client(true);
httpClient = new HttpClient(ssl);
httpClient.start();
runtime = runtimeContext();
logger.atInfo().log(
"%s: env=%s, httpMode=%s", this.getClass().getSimpleName(), environment, httpMode);
}

@After
public void after() throws Exception
{
httpClient.stop();
runtime.close();
public void after() throws Exception {
if (httpClient != null) {
httpClient.stop();
}
if (runtime != null) {
runtime.close();
}
}

@Test
public void testSecureRequest() throws Exception {
String url = runtime.jettyUrl("/");
assertThat(url, startsWith("http://"));
ContentResponse response = httpClient.newRequest(url)
.header("x-appengine-https", "on")
.send();

ContentResponse response = httpClient.newRequest(url).header("x-appengine-https", "on").send();
assertThat(response.getStatus(), equalTo(HttpStatus.OK_200));
String expectedUrl = url.replace("http://", "https://");
assertThat(response.getContentAsString(), containsString("requestURL=" + expectedUrl));
Expand All @@ -82,10 +111,10 @@ public void testInsecureRequest() throws Exception {
String url = runtime.jettyUrl("/");
assertThat(url, startsWith("http://"));

ContentResponse response = httpClient.newRequest(url)
.send();

ContentResponse response = httpClient.newRequest(url).send();
assertThat(response.getStatus(), equalTo(HttpStatus.FORBIDDEN_403));
assertThat(response.getContentAsString(), containsString("!Secure"));
if (!"ee10".equals(environment)) {
assertThat(response.getContentAsString(), containsString("!Secure"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.apphosting.runtime.jetty9.transportguaranteeapp;

import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class RequestUrlServletEE10 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/plain");
response.setStatus(HttpServletResponse.SC_OK);
PrintWriter out = response.getWriter();
out.println("requestURL=" + request.getRequestURL().toString());
out.println("isSecure=" + request.isSecure());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@
import java.io.PrintWriter;

@SuppressWarnings("serial")
public class RequestUrlServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException
{
response.setContentType("text/plain");
response.setStatus(HttpServletResponse.SC_OK);
PrintWriter out = response.getWriter();
out.println("requestURL=" + request.getRequestURL().toString());
out.println("isSecure=" + request.isSecure());
}
public class RequestUrlServletEE8 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/plain");
response.setStatus(HttpServletResponse.SC_OK);
PrintWriter out = response.getWriter();
out.println("requestURL=" + request.getRequestURL().toString());
out.println("isSecure=" + request.isSecure());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2021 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<runtime>java21</runtime>
<application>transportguarantee</application>
<version>1</version>
<threadsafe>true</threadsafe>

<system-properties>
<property name="appengine.use.EE10" value="true"/>
</system-properties>
</appengine-web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2021 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="false"
version="3.0">
<servlet>
<servlet-name>RequestUrlServlet</servlet-name>
<servlet-class>com.google.apphosting.runtime.jetty9.transportguaranteeapp.RequestUrlServletEE10</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RequestUrlServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

<security-constraint>
<web-resource-collection>
<web-resource-name>Confidential</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
</web-app>
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
limitations under the License.
-->

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.1"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd">
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="false"
version="3.0">
<servlet>
<servlet-name>RequestUrlServlet</servlet-name>
<servlet-class>com.google.apphosting.runtime.jetty9.transportguaranteeapp.RequestUrlServlet</servlet-class>
<servlet-class>com.google.apphosting.runtime.jetty9.transportguaranteeapp.RequestUrlServletEE8</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RequestUrlServlet</servlet-name>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2021 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<runtime>java21</runtime>
<application>transportguarantee</application>
<version>1</version>
<threadsafe>true</threadsafe>

<system-properties>
<property name="appengine.use.EE8" value="false"/>
<property name="appengine.use.EE10" value="false"/>
</system-properties>
</appengine-web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2021 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="false"
version="3.0">
<servlet>
<servlet-name>RequestUrlServlet</servlet-name>
<servlet-class>com.google.apphosting.runtime.jetty9.transportguaranteeapp.RequestUrlServletEE8</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RequestUrlServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

<security-constraint>
<web-resource-collection>
<web-resource-name>Confidential</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
</web-app>

0 comments on commit ec7bd83

Please sign in to comment.