Skip to content

Commit 8d34347

Browse files
author
Vladimir Kotal
committed
add indexer option to specify bearer token for API calls
fixes #3398
1 parent 4c08bcc commit 8d34347

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/Configuration.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ public final class Configuration {
304304
private Set<String> disabledRepositories;
305305

306306
private Set<String> authenticationTokens; // for non-localhost API access
307+
private String indexerAuthenticationToken;
307308

308309
/*
309310
* types of handling history for remote SCM repositories:
@@ -1347,6 +1348,14 @@ public void setAuthenticationTokens(Set<String> tokens) {
13471348
this.authenticationTokens = tokens;
13481349
}
13491350

1351+
public String getIndexerAuthenticationToken() {
1352+
return indexerAuthenticationToken;
1353+
}
1354+
1355+
public void setIndexerAuthenticationToken(String token) {
1356+
this.indexerAuthenticationToken = token;
1357+
}
1358+
13501359
/**
13511360
* Write the current configuration to a file.
13521361
*

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/RuntimeEnvironment.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package org.opengrok.indexer.configuration;
2525

2626
import static org.opengrok.indexer.configuration.Configuration.makeXMLStringAsConfiguration;
27+
import static org.opengrok.indexer.index.IndexerUtil.getHeaders;
2728

2829
import java.io.File;
2930
import java.io.FileNotFoundException;
@@ -1418,6 +1419,7 @@ public void writeConfiguration(String host) throws IOException {
14181419
.path("configuration")
14191420
.queryParam("reindex", true)
14201421
.request()
1422+
.headers(getHeaders())
14211423
.put(Entity.xml(configXML));
14221424

14231425
if (r.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
@@ -1444,6 +1446,7 @@ public void signalTorefreshSearcherManagers(List<String> subFiles, String host)
14441446
.path("system")
14451447
.path("refresh")
14461448
.request()
1449+
.headers(getHeaders())
14471450
.put(Entity.text(project));
14481451

14491452
if (r.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
@@ -1948,6 +1951,14 @@ private int getMessageLimit() {
19481951
return syncReadConfiguration(Configuration::getMessageLimit);
19491952
}
19501953

1954+
public String getIndexerAuthenticationToken() {
1955+
return syncReadConfiguration(Configuration::getIndexerAuthenticationToken);
1956+
}
1957+
1958+
public void setIndexerAuthenticationToken(String token) {
1959+
syncWriteConfiguration(token, Configuration::setIndexerAuthenticationToken);
1960+
}
1961+
19511962
public Set<String> getAuthenticationTokens() {
19521963
return Collections.unmodifiableSet(syncReadConfiguration(Configuration::getAuthenticationTokens));
19531964
}

opengrok-indexer/src/main/java/org/opengrok/indexer/index/IndexDatabase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@
104104
import org.opengrok.indexer.util.TandemPath;
105105
import org.opengrok.indexer.web.Util;
106106

107+
import static org.opengrok.indexer.index.IndexerUtil.getHeaders;
108+
107109
/**
108110
* This class is used to create / update the index databases. Currently we use
109111
* one index database per project.
@@ -376,6 +378,7 @@ private void markProjectIndexed(Project project) {
376378
.path(Util.URIEncode(project.getName()))
377379
.path("indexed")
378380
.request()
381+
.headers(getHeaders())
379382
.put(Entity.text(""));
380383
} catch (RuntimeException e) {
381384
LOGGER.log(Level.WARNING, String.format("Couldn''t notify the webapp that project %s was indexed",

opengrok-indexer/src/main/java/org/opengrok/indexer/index/Indexer.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,23 @@ public static String[] parseOptions(String[] argv) throws ParseException {
750750
"stylesheet. The factory-setting is: \"default\".").execute(stylePath ->
751751
cfg.setWebappLAF((String) stylePath));
752752

753+
parser.on("--token", "=string|@file_with_string",
754+
"Authorization bearer API token to use when making API calls",
755+
"to the web application").
756+
execute(optarg -> {
757+
String value = ((String) optarg).trim();
758+
if (value.startsWith("@")) {
759+
try {
760+
String token = new String(Files.readAllBytes(Path.of(value))).trim();
761+
cfg.setIndexerAuthenticationToken(token);
762+
} catch (IOException e) {
763+
die("Failed to read from " + value);
764+
}
765+
} else {
766+
cfg.setIndexerAuthenticationToken(value);
767+
}
768+
});
769+
753770
parser.on("-T", "--threads", "=number", Integer.class,
754771
"The number of threads to use for index generation, repository scan",
755772
"and repository invalidation.",

opengrok-indexer/src/main/java/org/opengrok/indexer/index/IndexerUtil.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,37 @@
2222
*/
2323
package org.opengrok.indexer.index;
2424

25+
import org.opengrok.indexer.configuration.RuntimeEnvironment;
26+
2527
import javax.ws.rs.ProcessingException;
2628
import javax.ws.rs.WebApplicationException;
2729
import javax.ws.rs.client.ClientBuilder;
2830
import javax.ws.rs.client.Entity;
2931
import javax.ws.rs.client.Invocation;
3032
import javax.ws.rs.client.ResponseProcessingException;
33+
import javax.ws.rs.core.HttpHeaders;
34+
import javax.ws.rs.core.MultivaluedHashMap;
35+
import javax.ws.rs.core.MultivaluedMap;
3136
import javax.ws.rs.core.Response;
3237

33-
class IndexerUtil {
38+
public class IndexerUtil {
3439

3540
private IndexerUtil() {
3641
}
3742

43+
/**
44+
* @return map of HTTP headers to use when making API requests to the web application
45+
*/
46+
public static MultivaluedMap<String, Object> getHeaders() {
47+
MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
48+
String token = null;
49+
if ((token = RuntimeEnvironment.getInstance().getIndexerAuthenticationToken()) != null) {
50+
headers.add(HttpHeaders.AUTHORIZATION, "Bearer " + token);
51+
}
52+
53+
return headers;
54+
}
55+
3856
/**
3957
* Enable projects in the remote host application.
4058
* <p>
@@ -56,7 +74,8 @@ public static void enableProjects(final String host) throws
5674
.path("v1")
5775
.path("configuration")
5876
.path("projectsEnabled")
59-
.request();
77+
.request()
78+
.headers(getHeaders());
6079
final String enabled = request.get(String.class);
6180
if (enabled == null || !Boolean.valueOf(enabled)) {
6281
final Response r = request.put(Entity.text(Boolean.TRUE.toString()));

0 commit comments

Comments
 (0)