Skip to content

Commit cd2f4ab

Browse files
ramkcursoragent
andcommitted
RANGER-5643: address review comments for Solr URL host rewrite
Use URI API for SPNEGO hostname rewrite, clearer variable names, public test class, and fix site XML description for HTTP/<host>@realm. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 0dd13b3 commit cd2f4ab

4 files changed

Lines changed: 46 additions & 32 deletions

File tree

agents-audit/dest-solr/src/main/java/org/apache/ranger/audit/destination/SolrAuditDestination.java

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import java.security.UnrecoverableKeyException;
6161
import java.security.cert.CertificateException;
6262
import java.net.URI;
63+
import java.net.URISyntaxException;
6364
import java.util.ArrayList;
6465
import java.util.Arrays;
6566
import java.util.Collection;
@@ -377,48 +378,58 @@ static String resolveSolrUrls(String urls, Properties props, String propPrefix)
377378
return urls;
378379
}
379380

380-
String solrHost = MiscUtil.getStringProperty(props, propPrefix + "." + PROP_SOLR_HOST);
381+
String configuredSolrHost = MiscUtil.getStringProperty(props, propPrefix + "." + PROP_SOLR_HOST);
381382

382-
if (StringUtils.isBlank(solrHost)) {
383+
if (StringUtils.isBlank(configuredSolrHost)) {
383384
return urls;
384385
}
385386

386-
solrHost = solrHost.trim();
387-
List<String> resolved = new ArrayList<>();
388-
boolean changed = false;
387+
configuredSolrHost = configuredSolrHost.trim();
388+
List<String> urlEntries = MiscUtil.toArray(urls, ",");
389+
List<String> resolvedUrls = new ArrayList<>(urlEntries.size());
390+
boolean hostRewritten = false;
389391

390-
for (String entry : MiscUtil.toArray(urls, ",")) {
391-
if (StringUtils.isBlank(entry)) {
392+
for (String urlEntry : urlEntries) {
393+
if (StringUtils.isBlank(urlEntry)) {
392394
continue;
393395
}
394396

395-
entry = entry.trim();
397+
String trimmedUrl = urlEntry.trim();
398+
String resolvedUrl = rewriteSolrUrlHostForSpnego(trimmedUrl, configuredSolrHost);
396399

397-
try {
398-
URI uri = URI.create(entry);
400+
if (!trimmedUrl.equals(resolvedUrl)) {
401+
hostRewritten = true;
402+
}
399403

400-
if (uri.getHost() != null && !uri.getHost().equalsIgnoreCase(solrHost)) {
401-
int port = uri.getPort() > 0 ? uri.getPort() : 8983;
402-
String scheme = StringUtils.isNotBlank(uri.getScheme()) ? uri.getScheme() : "http";
403-
String path = uri.getRawPath() != null ? uri.getRawPath() : "";
404-
String query = uri.getRawQuery() != null ? "?" + uri.getRawQuery() : "";
405-
String fragment = uri.getRawFragment() != null ? "#" + uri.getRawFragment() : "";
406-
String updated = scheme + "://" + solrHost + ":" + port + path + query + fragment;
404+
resolvedUrls.add(resolvedUrl);
405+
}
407406

408-
LOG.info("Solr audit URL host rewritten for Kerberos SPNEGO: {} -> {} (configured host={})",
409-
entry, updated, solrHost);
407+
return hostRewritten ? String.join(",", resolvedUrls) : urls;
408+
}
410409

411-
entry = updated;
412-
changed = true;
413-
}
414-
} catch (IllegalArgumentException e) {
415-
LOG.warn("Unable to parse Solr audit URL [{}]; leaving unchanged", entry, e);
410+
private static String rewriteSolrUrlHostForSpnego(String solrUrl, String configuredSolrHost) {
411+
try {
412+
URI parsedUri = URI.create(solrUrl);
413+
String currentHost = parsedUri.getHost();
414+
415+
if (currentHost == null || currentHost.equalsIgnoreCase(configuredSolrHost)) {
416+
return solrUrl;
416417
}
417418

418-
resolved.add(entry);
419-
}
419+
URI rewrittenUri = new URI(parsedUri.getScheme(), parsedUri.getUserInfo(), configuredSolrHost,
420+
parsedUri.getPort(), parsedUri.getPath(), parsedUri.getQuery(), parsedUri.getFragment());
421+
422+
String rewrittenUrl = rewrittenUri.toASCIIString();
420423

421-
return changed ? String.join(",", resolved) : urls;
424+
LOG.info("Solr audit URL host rewritten for Kerberos SPNEGO: {} -> {} (configured host={})",
425+
solrUrl, rewrittenUrl, configuredSolrHost);
426+
427+
return rewrittenUrl;
428+
} catch (IllegalArgumentException | URISyntaxException ex) {
429+
LOG.warn("Unable to parse Solr audit URL [{}]; leaving unchanged", solrUrl, ex);
430+
431+
return solrUrl;
432+
}
422433
}
423434

424435
private String resolveSolrUrls(String urls) {

agents-audit/dest-solr/src/test/java/org/apache/ranger/audit/destination/SolrAuditDestinationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import static org.junit.jupiter.api.Assertions.assertEquals;
2727

28-
class SolrAuditDestinationTest {
28+
public class SolrAuditDestinationTest {
2929

3030
private static final String PROP_PREFIX = AuditProviderFactory.AUDIT_DEST_BASE + ".solr";
3131

audit-server/audit-dispatcher/dispatcher-solr/src/main/resources/conf/ranger-audit-dispatcher-solr-site.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@
125125
<property>
126126
<name>xasecure.audit.destination.solr.host</name>
127127
<value>ranger-solr.rangernw</value>
128-
<description>
129-
FQDN for Kerberos SPNEGO to Solr (must match HTTP/&lt;host&gt;@REALM).
128+
<description><![CDATA[
129+
FQDN for Kerberos SPNEGO to Solr (must match HTTP/<host>@REALM).
130130
Rewrites xasecure.audit.destination.solr.urls hostname when they differ.
131-
</description>
131+
]]></description>
132132
</property>
133133

134134
<property>

dev-support/ranger-docker/scripts/audit-dispatcher/ranger-audit-dispatcher-solr-site.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@
107107
<property>
108108
<name>xasecure.audit.destination.solr.host</name>
109109
<value>ranger-solr.rangernw</value>
110-
<description>FQDN for Kerberos SPNEGO to Solr (rewrites urls hostname when needed)</description>
110+
<description><![CDATA[
111+
FQDN for Kerberos SPNEGO to Solr (must match HTTP/<host>@REALM).
112+
Rewrites xasecure.audit.destination.solr.urls hostname when they differ.
113+
]]></description>
111114
</property>
112115

113116
<property>

0 commit comments

Comments
 (0)