Add header based remote access security
Update the remote endpoints to use 'shared secret' authentication. Secrets are provided as Environment properties and transfered using a custom HTTP header. See gh-3082pull/3077/merge
parent
fe4c0022d7
commit
207347e150
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* http://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 org.springframework.boot.developertools.remote.client;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link ClientHttpRequestInterceptor} to populate arbitrary HTTP headers with a value.
|
||||
* For example, it might be used to provide an X-AUTH-TOKEN and value for security
|
||||
* purposes.
|
||||
*
|
||||
* @author Rob Winch
|
||||
* @since 1.3.0
|
||||
*/
|
||||
public class HttpHeaderInterceptor implements ClientHttpRequestInterceptor {
|
||||
|
||||
private final String name;
|
||||
|
||||
private final String value;
|
||||
|
||||
/**
|
||||
* Creates a new {@link HttpHeaderInterceptor} instance.
|
||||
* @param name the header name to populate. Cannot be null or empty.
|
||||
* @param value the header value to populate. Cannot be null or empty.
|
||||
*/
|
||||
public HttpHeaderInterceptor(String name, String value) {
|
||||
Assert.hasLength(name, "Name must not be empty");
|
||||
Assert.hasLength(value, "Value" + " must not be empty");
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
|
||||
ClientHttpRequestExecution execution) throws IOException {
|
||||
request.getHeaders().add(this.name, this.value);
|
||||
return execution.execute(request, body);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* http://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 org.springframework.boot.developertools.remote.server;
|
||||
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link AccessManager} that checks for the presence of a HTTP header secret.
|
||||
*
|
||||
* @author Rob Winch
|
||||
* @author Phillip Webb
|
||||
* @since 1.3.0
|
||||
*/
|
||||
public class HttpHeaderAccessManager implements AccessManager {
|
||||
|
||||
private final String headerName;
|
||||
|
||||
private final String expectedSecret;
|
||||
|
||||
public HttpHeaderAccessManager(String headerName, String expectedSecret) {
|
||||
Assert.hasLength(headerName, "HeaderName must not be empty");
|
||||
Assert.hasLength(expectedSecret, "ExpectedSecret must not be empty");
|
||||
this.headerName = headerName;
|
||||
this.expectedSecret = expectedSecret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAllowed(ServerHttpRequest request) {
|
||||
String providedSecret = request.getHeaders().getFirst(this.headerName);
|
||||
return this.expectedSecret.equals(providedSecret);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* http://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 org.springframework.boot.developertools.remote.client;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.http.server.ServletServerHttpRequest;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Tests for {@link HttpHeaderInterceptor}.
|
||||
*
|
||||
* @author Rob Winch
|
||||
* @since 1.3.0
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class HttpHeaderInterceptorTests {
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private String name;
|
||||
|
||||
private String value;
|
||||
|
||||
private HttpHeaderInterceptor interceptor;
|
||||
|
||||
private HttpRequest request;
|
||||
|
||||
private byte[] body;
|
||||
|
||||
@Mock
|
||||
private ClientHttpRequestExecution execution;
|
||||
|
||||
@Mock
|
||||
private ClientHttpResponse response;
|
||||
|
||||
private MockHttpServletRequest httpRequest;
|
||||
|
||||
@Before
|
||||
public void setup() throws IOException {
|
||||
this.body = new byte[] {};
|
||||
this.httpRequest = new MockHttpServletRequest();
|
||||
this.request = new ServletServerHttpRequest(this.httpRequest);
|
||||
this.name = "X-AUTH-TOKEN";
|
||||
this.value = "secret";
|
||||
given(this.execution.execute(this.request, this.body)).willReturn(this.response);
|
||||
this.interceptor = new HttpHeaderInterceptor(this.name, this.value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructorNullHeaderName() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("Name must not be empty");
|
||||
new HttpHeaderInterceptor(null, this.value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructorEmptyHeaderName() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("Name must not be empty");
|
||||
new HttpHeaderInterceptor("", this.value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructorNullHeaderValue() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("Value must not be empty");
|
||||
new HttpHeaderInterceptor(this.name, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructorEmptyHeaderValue() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("Value must not be empty");
|
||||
new HttpHeaderInterceptor(this.name, "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void intercept() throws IOException {
|
||||
ClientHttpResponse result = this.interceptor.intercept(this.request, this.body,
|
||||
this.execution);
|
||||
assertThat(this.request.getHeaders().getFirst(this.name), equalTo(this.value));
|
||||
assertThat(result, equalTo(this.response));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* http://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 org.springframework.boot.developertools.remote.server;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
import org.springframework.http.server.ServletServerHttpRequest;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link HttpHeaderAccessManager}.
|
||||
*
|
||||
* @author Rob Winch
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class HttpHeaderAccessManagerTests {
|
||||
|
||||
private static final String HEADER = "X-AUTH_TOKEN";
|
||||
|
||||
private static final String SECRET = "password";
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private MockHttpServletRequest request;
|
||||
|
||||
private ServerHttpRequest serverRequest;
|
||||
|
||||
private HttpHeaderAccessManager manager;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.request = new MockHttpServletRequest("GET", "/");
|
||||
this.serverRequest = new ServletServerHttpRequest(this.request);
|
||||
this.manager = new HttpHeaderAccessManager(HEADER, SECRET);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headerNameMustNotBeNull() throws Exception {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("HeaderName must not be empty");
|
||||
new HttpHeaderAccessManager(null, SECRET);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headerNameMustNotBeEmpty() throws Exception {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("HeaderName must not be empty");
|
||||
new HttpHeaderAccessManager("", SECRET);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expectedSecretMustNotBeNull() throws Exception {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("ExpectedSecret must not be empty");
|
||||
new HttpHeaderAccessManager(HEADER, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expectedSecretMustNotBeEmpty() throws Exception {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("ExpectedSecret must not be empty");
|
||||
new HttpHeaderAccessManager(HEADER, "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allowsMatching() throws Exception {
|
||||
this.request.addHeader(HEADER, SECRET);
|
||||
assertThat(this.manager.isAllowed(this.serverRequest), equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void disallowsWrongSecret() throws Exception {
|
||||
this.request.addHeader(HEADER, "wrong");
|
||||
assertThat(this.manager.isAllowed(this.serverRequest), equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void disallowsNoSecret() throws Exception {
|
||||
assertThat(this.manager.isAllowed(this.serverRequest), equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void disallowsWrongHeader() throws Exception {
|
||||
this.request.addHeader("X-WRONG", SECRET);
|
||||
assertThat(this.manager.isAllowed(this.serverRequest), equalTo(false));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue