Commit f1d2a25b authored by Jason's avatar Jason
Browse files

Merge branch 'internal_access_fix' into 'master'

Fix problem with internal access

See merge request !45
parents a237b85f 31eca3bf
Pipeline #32113 passed with stages
in 14 minutes and 11 seconds
......@@ -13,8 +13,6 @@ public abstract class AppProperties {
private String projectId;
@Value("${app.domain}")
private String domain;
@Value("${ACCEPT_HTTP:false}")
private boolean httpAccepted;
public String getProjectId() {
return projectId;
......@@ -24,10 +22,6 @@ public abstract class AppProperties {
return domain;
}
public boolean isHttpAccepted() {
return httpAccepted;
}
/**
* @return a list containing paths of configuration files
*/
......
......@@ -6,7 +6,6 @@ import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.http.Request;
import org.opengroup.osdu.core.common.model.http.RequestInfo;
import org.opengroup.osdu.entitlements.v2.AppProperties;
import org.springframework.stereotype.Component;
import javax.servlet.Filter;
......@@ -26,7 +25,6 @@ import java.util.Map;
@RequiredArgsConstructor
public class ResponseLogFilter implements Filter {
private final RequestInfo requestInfo;
private final AppProperties appProperties;
private final JaxRsDpsLog logger;
@Override
......@@ -56,14 +54,10 @@ public class ResponseLogFilter implements Filter {
}
try {
if (shouldRequestBeRedirectedToHttps(uri)) {
redirectRequestToHttps(uri, httpServletResponse);
if (isOptionsRequest(httpServletRequest)) {
httpServletResponse.setStatus(200);
} else {
if (isOptionsRequest(httpServletRequest)) {
httpServletResponse.setStatus(200);
} else {
filterChain.doFilter(servletRequest, servletResponse);
}
filterChain.doFilter(servletRequest, servletResponse);
}
} finally {
if (isNotHealthCheckRequest(uri)) {
......@@ -82,42 +76,10 @@ public class ResponseLogFilter implements Filter {
httpServletResponse.addHeader(DpsHeaders.CORRELATION_ID, requestInfo.getHeaders().getCorrelationId());
}
private boolean shouldRequestBeRedirectedToHttps(String uri) {
return (isNotLocalHost(uri)
&& isNotSwaggerRequest(uri)
&& isNotHealthCheckRequest(uri)
&& isNotHttps() && isNotHttpAccepted());
}
private void redirectRequestToHttps(String uri, HttpServletResponse httpServletResponse) {
httpServletResponse.setStatus(307);
String location = uri.replaceFirst("http", "https");
httpServletResponse.addHeader("location", location);
}
private boolean isNotHttps() {
return (!requestInfo.isHttps() && !requestInfo.isCronRequest());
}
private boolean isNotLocalHost(String uri) {
return (!uri.contains("//localhost") && !uri.contains("//127.0.0.1"));
}
private boolean isNotSwaggerRequest(String uri) {
return (!uri.contains("/swagger")
&& !uri.contains("/v2/api-docs")
&& !uri.contains("/configuration/ui")
&& !uri.contains("/webjars/"));
}
private boolean isNotHealthCheckRequest(String uri) {
return (!uri.endsWith("/liveness_check") && !uri.endsWith("/readiness_check"));
}
private boolean isNotHttpAccepted() {
return !appProperties.isHttpAccepted();
}
private boolean isOptionsRequest(HttpServletRequest request) {
return request.getMethod().equalsIgnoreCase("OPTIONS");
}
......
......@@ -33,8 +33,6 @@ public class ResponseLogFilterTests {
@Mock
private RequestInfo requestInfo;
@Mock
private AppProperties appProperties;
@Mock
private DpsHeaders headers;
@Mock
private ServletContext context;
......@@ -60,25 +58,6 @@ public class ResponseLogFilterTests {
when(context.getAttribute("starttime")).thenReturn(null);
}
@Test
public void shouldReturn307WithHttpsLocationWhenIsNotACronAndIsNotUsingHttps() throws Exception {
when(requestInfo.isCronRequest()).thenReturn(false);
responseLogFilter.doFilter(servletRequest, servletResponse, filterChain);
assertEquals(307, servletResponse.getStatus());
ArgumentCaptor<Request> argument = ArgumentCaptor.forClass(Request.class);
verify(logger).request(argument.capture());
Request result = argument.getValue();
assertEquals("127.0.0.1", result.getIp());
assertEquals(307, result.getStatus());
}
@Test
public void shouldNotTReturn307ExceptionWhenHttpAccepted() throws Exception {
when(appProperties.isHttpAccepted()).thenReturn(true);
responseLogFilter.doFilter(servletRequest, servletResponse, filterChain);
assertEquals(200, servletResponse.getStatus());
}
@Test
public void shouldNotThrowAppExceptionWhenIsACron() throws Exception {
when(requestInfo.isCronRequest()).thenReturn(true);
......
ACCEPT_HTTP=true
# Logging configuration
logging.level.org.springframework.web=DEBUG
......
......@@ -122,7 +122,6 @@ public class CreateMembershipsWorkflowSinglePartitionTest {
public void before() {
Mockito.when(config.getDomain()).thenReturn("contoso.com");
Mockito.when(config.getProjectId()).thenReturn("evd-ddl-us-services");
Mockito.when(config.isHttpAccepted()).thenReturn(true);
Mockito.when(config.getInitialGroups()).thenCallRealMethod();
Mockito.when(config.getGroupsOfServicePrincipal()).thenCallRealMethod();
TenantInfo tenantInfo = new TenantInfo();
......
......@@ -134,7 +134,6 @@ public class CreateMembershipsWorkflowSinglePartitionTest {
when(config.getPartitionRedisInstanceId()).thenReturn("instanceId");
when(config.getDomain()).thenReturn("contoso.com");
when(config.getProjectId()).thenReturn("evd-ddl-us-services");
when(config.isHttpAccepted()).thenReturn(true);
when(config.getInitialGroups()).thenCallRealMethod();
when(config.getGroupsOfServicePrincipal()).thenCallRealMethod();
when(partitionRedisInstanceService.getHostOfRedisInstanceForPartition("common")).thenReturn("localhost");
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment