Repository navigation
SOLR-13097: Make core-scoped authorization rules work in Solr standalone mode #5016
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
Open
nick-boss-tech
wants to merge
11
commits into
apache:main
Choose a base branch
from
nick-boss-tech:solr-13097-submit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
83305b4
SOLR-13097: Authorize standalone requests against the core name
nick-boss-tech a522cab
SOLR-13097: add testing handoff
nick-boss-tech 7546958
SOLR-13097: regression test for standalone core-scoped authorization
nick-boss-tech 9dd8702
SOLR-13097: add Apache license header to regression test
nick-boss-tech e8a7543
SOLR-13097: remove TESTING.md handoff file
nick-boss-tech 9adca1a
SOLR-13097: tidy formatting for CoreScopedAuthStandaloneTest
nick-boss-tech 65b2753
SOLR-13097: two-core and unauthenticated tests, framework configset p…
nick-boss-tech e869956
SOLR-13097: replace String.formatted with String.format(Locale.ROOT) …
nick-boss-tech 8aa4759
SOLR-13097: Scope the upgrade note to Solr 10.2 instead of 11
nick-boss-tech 4c08aa5
SOLR-13097: Changelog as a fix, and document core names for the colle…
nick-boss-tech f0e7395
SOLR-13097: Cover the v2 path and wildcard-plus-scoped rules in the s…
nick-boss-tech 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 hidden or 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,7 @@ | ||
| title: Core-scoped authorization permissions were not applied in standalone mode | ||
| type: fixed | ||
| authors: | ||
| - name: Nick Shanin | ||
| links: | ||
| - name: SOLR-13097 | ||
| url: https://issues.apache.org/jira/browse/SOLR-13097 |
This file contains hidden or 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
200 changes: 200 additions & 0 deletions
200
solr/core/src/test/org/apache/solr/security/CoreScopedAuthStandaloneTest.java
This file contains hidden or 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,200 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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.apache.solr.security; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import org.apache.solr.SolrTestCase; | ||
| import org.apache.solr.SolrTestCaseJ4; | ||
| import org.apache.solr.client.solrj.RemoteSolrException; | ||
| import org.apache.solr.client.solrj.SolrRequest; | ||
| import org.apache.solr.client.solrj.request.GenericV2SolrRequest; | ||
| import org.apache.solr.client.solrj.request.QueryRequest; | ||
| import org.apache.solr.client.solrj.request.SolrQuery; | ||
| import org.apache.solr.client.solrj.response.QueryResponse; | ||
| import org.apache.solr.common.util.NamedList; | ||
| import org.apache.solr.embedded.JettySolrRunner; | ||
| import org.junit.AfterClass; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
|
|
||
| /** | ||
| * Tests that authorization rules scoped to a core with the "collection" field apply in standalone | ||
| * mode, where the core name stands in for the collection. | ||
| */ | ||
| public class CoreScopedAuthStandaloneTest extends SolrTestCase { | ||
|
|
||
| private static final String CORE_1 = "collection1"; | ||
| private static final String CORE_2 = "collection2"; | ||
| private static final String CORE_3 = "collection3"; | ||
|
|
||
| private static final String READER_USER = "reader"; | ||
| private static final String READER_PASS = "ReaderPass123"; | ||
| private static final String OTHER_USER = "other"; | ||
| private static final String OTHER_PASS = "OtherPass123"; | ||
| private static final String STAR_USER = "star"; | ||
| private static final String STAR_PASS = "StarPass123"; | ||
|
|
||
| private static JettySolrRunner jetty; | ||
|
|
||
| @BeforeClass | ||
| public static void beforeClass() throws Exception { | ||
| Path homeDir = createTempDir("corescoped-auth").toAbsolutePath(); | ||
| Path confSrc = SolrTestCaseJ4.configset("_default"); | ||
| assertTrue("configset not found at " + confSrc, Files.isDirectory(confSrc)); | ||
| for (String core : List.of(CORE_1, CORE_2, CORE_3)) { | ||
| Path coreDir = homeDir.resolve(core); | ||
| copyDir(confSrc, coreDir.resolve("conf")); | ||
| Files.writeString(coreDir.resolve("core.properties"), "name=" + core + "\n"); | ||
| } | ||
|
|
||
| // each scoped role may read only the core named in its permission; the star role holds a | ||
| // wildcard permission alongside the scoped ones | ||
| String securityJsonTemplate = | ||
| """ | ||
| { | ||
| "authentication": { | ||
| "class": "solr.BasicAuthPlugin", | ||
| "credentials": {"%s": "%s", "%s": "%s", "%s": "%s"} | ||
| }, | ||
| "authorization": { | ||
| "class": "solr.RuleBasedAuthorizationPlugin", | ||
| "user-role": {"%s": "reader-role", "%s": "other-role", "%s": "star-role"}, | ||
| "permissions": [ | ||
| {"name": "read", "role": "reader-role", "collection": "%s"}, | ||
| {"name": "read", "role": "other-role", "collection": "%s"}, | ||
| {"name": "read", "role": "star-role", "collection": "*"} | ||
| ] | ||
| } | ||
| } | ||
| """; | ||
| String securityJson = | ||
| String.format( | ||
| Locale.ROOT, | ||
| securityJsonTemplate, | ||
| READER_USER, | ||
| Sha256AuthenticationProvider.getSaltedHashedValue(READER_PASS), | ||
| OTHER_USER, | ||
| Sha256AuthenticationProvider.getSaltedHashedValue(OTHER_PASS), | ||
| STAR_USER, | ||
| Sha256AuthenticationProvider.getSaltedHashedValue(STAR_PASS), | ||
| READER_USER, | ||
| OTHER_USER, | ||
| STAR_USER, | ||
| CORE_1, | ||
| CORE_2); | ||
| Files.writeString(homeDir.resolve("security.json"), securityJson, StandardCharsets.UTF_8); | ||
| jetty = new JettySolrRunner(homeDir.toString(), 0); | ||
| jetty.start(); | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void afterClass() throws Exception { | ||
| if (jetty != null) { | ||
| jetty.stop(); | ||
| jetty = null; | ||
| } | ||
| } | ||
|
|
||
| private static void copyDir(Path src, Path dest) throws Exception { | ||
| try (var stream = Files.walk(src)) { | ||
| for (Path p : (Iterable<Path>) stream::iterator) { | ||
| Path target = dest.resolve(src.relativize(p).toString()); | ||
| if (Files.isDirectory(p)) { | ||
| Files.createDirectories(target); | ||
| } else { | ||
| Files.copy(p, target); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static QueryResponse queryAs(String user, String pass, String core) throws Exception { | ||
| QueryRequest req = new QueryRequest(new SolrQuery("*:*")); | ||
| if (user != null) { | ||
| req.setBasicAuthCredentials(user, pass); | ||
| } | ||
| return req.process(jetty.getSolrClient(), core); | ||
| } | ||
|
|
||
| private static void assertDenied(String user, String pass, String core, int expectedCode) { | ||
| RemoteSolrException e = | ||
| expectThrows(RemoteSolrException.class, () -> queryAs(user, pass, core)); | ||
| assertEquals(user + " on " + core, expectedCode, e.code()); | ||
| } | ||
|
|
||
| private static NamedList<Object> queryV2As(String user, String pass, String core) | ||
| throws Exception { | ||
| GenericV2SolrRequest req = | ||
| new GenericV2SolrRequest( | ||
| SolrRequest.METHOD.GET, "/cores/" + core + "/select", new SolrQuery("*:*")); | ||
| if (user != null) { | ||
| req.setBasicAuthCredentials(user, pass); | ||
| } | ||
| return req.process(jetty.getSolrClient()).getResponse(); | ||
| } | ||
|
|
||
| private static void assertV2Allowed(String user, String pass, String core) throws Exception { | ||
| NamedList<Object> rsp = queryV2As(user, pass, core); | ||
| NamedList<?> header = (NamedList<?>) rsp.get("responseHeader"); | ||
| assertNotNull("v2 response for " + core + " has no responseHeader", header); | ||
| assertEquals(user + " on v2 " + core, 0, header.get("status")); | ||
| } | ||
|
|
||
| private static void assertV2Denied(String user, String pass, String core, int expectedCode) { | ||
| RemoteSolrException e = | ||
| expectThrows(RemoteSolrException.class, () -> queryV2As(user, pass, core)); | ||
| assertEquals(user + " on v2 " + core, expectedCode, e.code()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testScopedRuleAllowsItsRoleOnItsCore() throws Exception { | ||
| assertEquals(0, queryAs(READER_USER, READER_PASS, CORE_1).getStatus()); | ||
| assertEquals(0, queryAs(OTHER_USER, OTHER_PASS, CORE_2).getStatus()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testScopedRuleDeniesOtherRolesOnTheCore() { | ||
| assertDenied(OTHER_USER, OTHER_PASS, CORE_1, 403); | ||
| assertDenied(READER_USER, READER_PASS, CORE_2, 403); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRequestWithoutCredentialsIsRejected() { | ||
| assertDenied(null, null, CORE_1, 401); | ||
| } | ||
|
|
||
| @Test | ||
| public void testV2RequestUsesCoreScopedRules() throws Exception { | ||
| assertV2Allowed(READER_USER, READER_PASS, CORE_1); | ||
| assertV2Denied(OTHER_USER, OTHER_PASS, CORE_1, 403); | ||
| assertV2Denied(READER_USER, READER_PASS, CORE_2, 403); | ||
| } | ||
|
|
||
| @Test | ||
| public void testWildcardRuleAppliesOnlyWhereNoScopedRuleGoverns() throws Exception { | ||
| // a scoped rule governs its core on its own; the wildcard rule adds nothing there | ||
| assertDenied(STAR_USER, STAR_PASS, CORE_1, 403); | ||
| assertDenied(STAR_USER, STAR_PASS, CORE_2, 403); | ||
| // on a core no scoped rule governs, the wildcard rule is the one that decides | ||
| assertEquals(0, queryAs(STAR_USER, STAR_PASS, CORE_3).getStatus()); | ||
| assertDenied(READER_USER, READER_PASS, CORE_3, 403); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.