Skip to content

Commit a776263

Browse files
authored
Handle NotEntitledException in SSL file utils (#123491)
SSL file utils currently only handle security manager access control exceptions around file read checks. This PR extends these to support entitlement checks as well. There is no easy way to unit test this since we can't run unit tests with entitlements enabled (for now). The PR includes a REST test instead. Relates: #121960
1 parent c7e7dbe commit a776263

File tree

11 files changed

+109
-1
lines changed

11 files changed

+109
-1
lines changed

libs/ssl-config/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ apply plugin: "elasticsearch.publish"
1010

1111
dependencies {
1212
api project(':libs:core')
13+
api project(':libs:entitlement')
1314

1415
testImplementation(project(":test:framework")) {
1516
exclude group: 'org.elasticsearch', module: 'ssl-config'

libs/ssl-config/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
module org.elasticsearch.sslconfig {
1111
requires org.elasticsearch.base;
12+
requires org.elasticsearch.entitlement;
1213

1314
exports org.elasticsearch.common.ssl;
1415
}

libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/PemKeyConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package org.elasticsearch.common.ssl;
1111

1212
import org.elasticsearch.core.Tuple;
13+
import org.elasticsearch.entitlement.runtime.api.NotEntitledException;
1314

1415
import java.io.IOException;
1516
import java.nio.file.Path;
@@ -127,6 +128,8 @@ private PrivateKey getPrivateKey(Path path) {
127128
return privateKey;
128129
} catch (AccessControlException e) {
129130
throw SslFileUtil.accessControlFailure(KEY_FILE_TYPE, List.of(path), e, configBasePath);
131+
} catch (NotEntitledException e) {
132+
throw SslFileUtil.notEntitledFailure(KEY_FILE_TYPE, List.of(path), e, configBasePath);
130133
} catch (IOException e) {
131134
throw SslFileUtil.ioException(KEY_FILE_TYPE, List.of(path), e);
132135
} catch (GeneralSecurityException e) {

libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/PemTrustConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
package org.elasticsearch.common.ssl;
1111

12+
import org.elasticsearch.entitlement.runtime.api.NotEntitledException;
13+
1214
import java.io.IOException;
1315
import java.io.InputStream;
1416
import java.nio.file.Path;
@@ -99,6 +101,8 @@ private List<Certificate> readCertificates(List<Path> paths) {
99101
return PemUtils.readCertificates(paths);
100102
} catch (AccessControlException e) {
101103
throw SslFileUtil.accessControlFailure(CA_FILE_TYPE, paths, e, basePath);
104+
} catch (NotEntitledException e) {
105+
throw SslFileUtil.notEntitledFailure(CA_FILE_TYPE, paths, e, basePath);
102106
} catch (IOException e) {
103107
throw SslFileUtil.ioException(CA_FILE_TYPE, paths, e);
104108
} catch (GeneralSecurityException e) {

libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/PemUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package org.elasticsearch.common.ssl;
1111

1212
import org.elasticsearch.core.CharArrays;
13+
import org.elasticsearch.entitlement.runtime.api.NotEntitledException;
1314

1415
import java.io.BufferedReader;
1516
import java.io.IOException;
@@ -112,6 +113,8 @@ public static PrivateKey readPrivateKey(Path path, Supplier<char[]> passwordSupp
112113
return privateKey;
113114
} catch (AccessControlException e) {
114115
throw SslFileUtil.accessControlFailure("PEM private key", List.of(path), e, null);
116+
} catch (NotEntitledException e) {
117+
throw SslFileUtil.notEntitledFailure("PEM private key", List.of(path), e, null);
115118
} catch (IOException e) {
116119
throw SslFileUtil.ioException("PEM private key", List.of(path), e);
117120
} catch (GeneralSecurityException e) {

libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslFileUtil.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
package org.elasticsearch.common.ssl;
1111

12+
import org.elasticsearch.entitlement.runtime.api.NotEntitledException;
13+
1214
import java.io.FileNotFoundException;
1315
import java.io.IOException;
1416
import java.nio.file.AccessDeniedException;
@@ -78,7 +80,15 @@ static SslConfigException accessDenied(String fileType, List<Path> paths, Access
7880
return new SslConfigException(message, cause);
7981
}
8082

83+
static SslConfigException notEntitledFailure(String fileType, List<Path> paths, NotEntitledException cause, Path basePath) {
84+
return innerAccessControlFailure(fileType, paths, cause, basePath);
85+
}
86+
8187
static SslConfigException accessControlFailure(String fileType, List<Path> paths, AccessControlException cause, Path basePath) {
88+
return innerAccessControlFailure(fileType, paths, cause, basePath);
89+
}
90+
91+
private static SslConfigException innerAccessControlFailure(String fileType, List<Path> paths, Exception cause, Path basePath) {
8292
String message = "cannot read configured " + fileType + " [" + pathsToString(paths) + "] because ";
8393
if (paths.size() == 1) {
8494
message += "access to read the file is blocked";

libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/StoreKeyConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.elasticsearch.core.Nullable;
1313
import org.elasticsearch.core.Tuple;
14+
import org.elasticsearch.entitlement.runtime.api.NotEntitledException;
1415

1516
import java.io.IOException;
1617
import java.nio.file.Path;
@@ -168,6 +169,8 @@ private KeyStore readKeyStore(Path path) {
168169
return KeyStoreUtil.readKeyStore(path, type, storePassword);
169170
} catch (AccessControlException e) {
170171
throw SslFileUtil.accessControlFailure("[" + type + "] keystore", List.of(path), e, configBasePath);
172+
} catch (NotEntitledException e) {
173+
throw SslFileUtil.notEntitledFailure("[" + type + "] keystore", List.of(path), e, configBasePath);
171174
} catch (IOException e) {
172175
throw SslFileUtil.ioException("[" + type + "] keystore", List.of(path), e);
173176
} catch (GeneralSecurityException e) {

libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/StoreTrustConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
package org.elasticsearch.common.ssl;
1111

12+
import org.elasticsearch.entitlement.runtime.api.NotEntitledException;
13+
1214
import java.io.IOException;
1315
import java.nio.file.Path;
1416
import java.security.AccessControlException;
@@ -95,6 +97,8 @@ private KeyStore readKeyStore(Path path) {
9597
return KeyStoreUtil.readKeyStore(path, type, password);
9698
} catch (AccessControlException e) {
9799
throw SslFileUtil.accessControlFailure(fileTypeForException(), List.of(path), e, configBasePath);
100+
} catch (NotEntitledException e) {
101+
throw SslFileUtil.notEntitledFailure(fileTypeForException(), List.of(path), e, configBasePath);
98102
} catch (IOException e) {
99103
throw SslFileUtil.ioException(fileTypeForException(), List.of(path), e, getAdditionalErrorDetails());
100104
} catch (GeneralSecurityException e) {

x-pack/plugin/core/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
module org.elasticsearch.xcore {
99
requires org.elasticsearch.cli;
10+
requires org.elasticsearch.entitlement;
1011
requires org.elasticsearch.base;
1112
requires org.elasticsearch.grok;
1213
requires org.elasticsearch.server;

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/SSLConfigurationReloader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.action.support.PlainActionFuture;
1313
import org.elasticsearch.common.ssl.SslConfiguration;
1414
import org.elasticsearch.core.TimeValue;
15+
import org.elasticsearch.entitlement.runtime.api.NotEntitledException;
1516
import org.elasticsearch.watcher.FileChangesListener;
1617
import org.elasticsearch.watcher.FileWatcher;
1718
import org.elasticsearch.watcher.ResourceWatcherService;
@@ -109,7 +110,7 @@ private static void startWatching(
109110
fileWatcher.addListener(changeListener);
110111
try {
111112
resourceWatcherService.add(fileWatcher, Frequency.HIGH);
112-
} catch (IOException | AccessControlException e) {
113+
} catch (IOException | AccessControlException | NotEntitledException e) {
113114
logger.error("failed to start watching directory [{}] for ssl configurations [{}] - {}", path, configurations, e);
114115
}
115116
});

0 commit comments

Comments
 (0)