-
Notifications
You must be signed in to change notification settings - Fork 217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow for OPTIONS requests to be passed through auth filters #1244
Merged
adejanovski
merged 11 commits into
thelastpickle:master
from
jdonenine:jdinoto-allow-options-requests
Dec 8, 2022
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a68640e
Allow for OPTION requests to be passed through auth filters
jdonenine d37c1af
Fix codestyle issues.
jdonenine c85d6c6
Change RequestUtils code pattern to be more testable
jdonenine ba38c35
Add additional testing
jdonenine 6e61d02
no message
jdonenine 3151caa
no message
jdonenine 2b34934
Switch to leveraging the already in use REAPER_ENABLE_CROSS_ORIGIN en…
jdonenine c7aebca
Update shiro configuration to properly secure cookies.
jdonenine 6015800
Update src/server/src/main/java/io/cassandrareaper/resources/RequestU…
jdonenine 5cf1a23
Switch away from the direct usage of the env variable
jdonenine cc5e3e7
Fix codestyle problem
jdonenine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/server/src/main/java/io/cassandrareaper/resources/RequestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* | ||
* Copyright 2022-2022 The Last Pickle Ltd | ||
* | ||
* 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 io.cassandrareaper.resources; | ||
|
||
import javax.servlet.ServletRequest; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.ws.rs.HttpMethod; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
|
||
public final class RequestUtils { | ||
public static final String ENABLE_CORS_ENV_VAR_NAME = "REAPER_ENABLE_CROSS_ORIGIN"; | ||
|
||
private RequestUtils() {} | ||
|
||
public static boolean isOptionsRequest(ServletRequest request) { | ||
if (request != null && request instanceof HttpServletRequest) { | ||
if (((HttpServletRequest) request).getMethod().equalsIgnoreCase(HttpMethod.OPTIONS)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@VisibleForTesting | ||
static boolean isCorsEnabled(String corsEnabledEnvVarValue) { | ||
if (corsEnabledEnvVarValue != null) { | ||
return Boolean.parseBoolean(corsEnabledEnvVarValue.trim().toLowerCase()); | ||
} | ||
return false; | ||
} | ||
|
||
public static boolean isCorsEnabled() { | ||
String corsEnabledEnvVarValue = System.getenv(ENABLE_CORS_ENV_VAR_NAME); | ||
return isCorsEnabled(corsEnabledEnvVarValue); | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/server/src/test/java/io/cassandrareaper/resources/RequestUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* | ||
* Copyright 2019-2019 The Last Pickle Ltd | ||
* | ||
* 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 io.cassandrareaper.resources; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.ws.rs.HttpMethod; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.Test; | ||
|
||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class RequestUtilsTest { | ||
@Test | ||
public void testIsCorsEnabledFromEnvironmentWithEmptyEnvironmentReturnsFalse() { | ||
boolean allowAll = RequestUtils.isCorsEnabled(); | ||
Assertions.assertThat(allowAll).isFalse(); | ||
} | ||
|
||
@Test | ||
public void testIsCorsEnabledFromEnvironmentWithTrueEnvironmentReturnsTrue() { | ||
boolean allowAll = RequestUtils.isCorsEnabled("true"); | ||
Assertions.assertThat(allowAll).isTrue(); | ||
} | ||
|
||
@Test | ||
public void testIsCorsEnabledFromEnvironmentWithFalseEnvironmentReturnsFalse() { | ||
boolean allowAll = RequestUtils.isCorsEnabled("false"); | ||
Assertions.assertThat(allowAll).isFalse(); | ||
} | ||
|
||
@Test | ||
public void testIsCorsEnabledFromInvalidEnvironmentWithEnvironmentReturnsFalse() { | ||
boolean allowAll = RequestUtils.isCorsEnabled("bad"); | ||
Assertions.assertThat(allowAll).isFalse(); | ||
} | ||
|
||
@Test | ||
public void testIsOptionsRequestInvalidInputReturnsFalse() { | ||
boolean isOptionsRequest = RequestUtils.isOptionsRequest(null); | ||
Assertions.assertThat(isOptionsRequest).isFalse(); | ||
} | ||
|
||
@Test | ||
public void testIsOptionsRequestOptionsServletInputReturnsTrue() { | ||
HttpServletRequest mockServletRequest = spy(HttpServletRequest.class); | ||
when(mockServletRequest.getMethod()).thenReturn(HttpMethod.OPTIONS); | ||
boolean isOptionsRequest = RequestUtils.isOptionsRequest(mockServletRequest); | ||
Assertions.assertThat(isOptionsRequest).isTrue(); | ||
} | ||
|
||
@Test | ||
public void testIsOptionsRequestGetServletInputReturnsTrue() { | ||
HttpServletRequest mockServletRequest = spy(HttpServletRequest.class); | ||
when(mockServletRequest.getMethod()).thenReturn(HttpMethod.GET); | ||
boolean isOptionsRequest = RequestUtils.isOptionsRequest(mockServletRequest); | ||
Assertions.assertThat(isOptionsRequest).isFalse(); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/server/src/test/java/io/cassandrareaper/resources/auth/RestPermissionsFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* | ||
* Copyright 2022-2022 The Last Pickle Ltd | ||
* | ||
* 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 io.cassandrareaper.resources.auth; | ||
|
||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.ws.rs.HttpMethod; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
public class RestPermissionsFilterTest { | ||
|
||
@Test | ||
public void testOptionsRequestWithoutAuthorizationIsAllowed() throws Exception { | ||
RestPermissionsFilter filter = Mockito.spy(RestPermissionsFilter.class); | ||
HttpServletRequest mockHttpServletRequest = Mockito.spy(HttpServletRequest.class); | ||
Mockito.when(mockHttpServletRequest.getMethod()).thenReturn(HttpMethod.OPTIONS); | ||
Mockito.when(filter.isCorsEnabled()).thenReturn(true); | ||
|
||
boolean allowed = filter.isAccessAllowed( | ||
mockHttpServletRequest, | ||
Mockito.mock(ServletResponse.class), | ||
Mockito.mock(Object.class) | ||
); | ||
Assertions.assertThat(allowed).isTrue(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After reviewing this and thinking about another requirement we have (which is to shut down the sessions despite being active after the configured timeout), I'm of the opinion that we shouldn't rely on env variable.
We can statically set a
isCorsEnabled
variable in RequestUtils which will be initialized in the ReaperApplication.java initialization phase, and feed on the corresponding config setting:config.isEnableCrossOrigin()
Initialization can be done as follows in ReaperApplication.java:
wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, so the latest commit switches to initializing with config property instead of directly using the env variable... duh, I should have seen that to start 😔 so thanks for that suggestion!
But, I wasn't sure why you suggested tangling up the enabling of CORS within the isAccessControlEnabled() block as you mention here?
I initialized things in place with the other CORS related configuration that already happened within ReaperApplication.java's startup code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was under the impression that we needed it to allow OPTIONS calls when auth is enabled. Isn't it what you were trying to do here?
Either way, I'm fine with initializing it outside of that block 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nah, the two can be unrelated.