Skip to content

Commit 39f035c

Browse files
authored
simplify tls (#2977)
1 parent 758a579 commit 39f035c

3 files changed

Lines changed: 23 additions & 12 deletions

File tree

docs/modules/ROOT/pages/using-tika/grpc/index.adoc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,11 @@ java -jar tika-grpc-<version>.jar --secure \
126126
--trust-cert-collection ca.pem --client-auth-required
127127
----
128128

129-
Mutual TLS is opt-in: `--client-auth-required` is off by default, so it has no
130-
effect unless `--trust-cert-collection` is also given (a missing or non-existent
131-
trust-collection path is silently ignored). The default port is `50052`
132-
(`-p`/`--port`).
129+
`--client-auth-required` implies `--secure` -- it's included above for clarity,
130+
but TLS is enabled automatically whenever client authentication is required.
131+
`--trust-cert-collection` must point to a readable file when
132+
`--client-auth-required` is set; the server refuses to start otherwise. The
133+
default port is `50052` (`-p`/`--port`).
133134

134135
When running the Docker image, append these flags to the container command — they
135136
are forwarded to the server — and mount the certificate files into the container.

tika-grpc/src/main/java/org/apache/tika/pipes/grpc/TikaGrpcServer.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ public class TikaGrpcServer {
7070
@Parameter(names = {"--private-key-password"}, description = "Private key password, if needed")
7171
private String privateKeyPassword;
7272

73-
@Parameter(names = {"--trust-cert-collection"}, description = "The trust certificate collection (root certs). Example: ca.pem See: https://github.com/grpc/grpc-java/tree/b3ffb5078df361d7460786e134db7b5c00939246/examples/example-tls")
73+
@Parameter(names = {"--trust-cert-collection"}, description = "The trust certificate collection (root certs). Required, and must be a readable file, when --client-auth-required is set. Example: ca.pem See: https://github.com/grpc/grpc-java/tree/b3ffb5078df361d7460786e134db7b5c00939246/examples/example-tls")
7474
private File trustCertCollection;
7575

76-
@Parameter(names = {"--client-auth-required"}, description = "Is Mutual TLS required?")
76+
@Parameter(names = {"--client-auth-required"}, description = "Is Mutual TLS required? Implies --secure.")
7777
private boolean clientAuthRequired;
7878

7979
@Parameter(names = {"-h", "-H", "--help"}, description = "Display help menu")
@@ -82,6 +82,10 @@ public class TikaGrpcServer {
8282
public void start() throws Exception {
8383
HealthStatusManager healthStatusManager = new HealthStatusManager();
8484
ServerCredentials creds;
85+
if (clientAuthRequired && !secure) {
86+
LOGGER.info("--client-auth-required implies --secure; enabling TLS.");
87+
secure = true;
88+
}
8589
if (secure) {
8690
TlsServerCredentials.Builder channelCredBuilder = TlsServerCredentials.newBuilder();
8791
channelCredBuilder.keyManager(certChain, privateKey, privateKeyPassword);
@@ -97,9 +101,6 @@ public void start() throws Exception {
97101
}
98102
creds = channelCredBuilder.build();
99103
} else {
100-
if (clientAuthRequired) {
101-
throw new IllegalArgumentException("--client-auth-required requires --secure; refusing to start");
102-
}
103104
creds = InsecureServerCredentials.create();
104105
}
105106
if (tikaConfig == null) {

tika-grpc/src/test/java/org/apache/tika/pipes/grpc/TikaGrpcServerTlsTest.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,28 @@
3030
/**
3131
* Covers the trust-cert-collection states that {@link TikaGrpcServer#start()} should refuse
3232
* to start on when {@code --client-auth-required} is set: omitted, nonexistent, unreadable,
33-
* and invalid/corrupt content, plus {@code --client-auth-required} without {@code --secure}.
33+
* and invalid/corrupt content. Also covers {@code --client-auth-required} without
34+
* {@code --secure}, which implies {@code --secure} rather than being rejected.
3435
*/
3536
class TikaGrpcServerTlsTest {
3637
private static final File CERT_CHAIN = Paths.get("src", "test", "resources", "certs", "server1.pem").toFile();
3738
private static final File PRIVATE_KEY = Paths.get("src", "test", "resources", "certs", "server1.key").toFile();
3839
private static final File VALID_TRUST_COLLECTION = Paths.get("src", "test", "resources", "certs", "ca.pem").toFile();
3940

4041
@Test
41-
void clientAuthRequiredWithoutSecureRefusesToStart() {
42+
void clientAuthRequiredWithoutSecureImpliesSecureAndStarts() throws Exception {
4243
TikaGrpcServer server = new TikaGrpcServer()
44+
.setPort(0)
4345
.setSecure(false)
46+
.setCertChain(CERT_CHAIN)
47+
.setPrivateKey(PRIVATE_KEY)
48+
.setTrustCertCollection(VALID_TRUST_COLLECTION)
4449
.setClientAuthRequired(true);
45-
assertThrows(IllegalArgumentException.class, server::start);
50+
try {
51+
server.start();
52+
} finally {
53+
server.stop();
54+
}
4655
}
4756

4857
@Test

0 commit comments

Comments
 (0)