Skip to content

Commit f0f16b7

Browse files
committed
[TEST] Make SSL restrictions update atomic (#31050)
SSLTrustRestrictionsTests updates the restrictions YML file during the test run to change the set of restrictions. This update was small, but it wasn't atomic. If the yml file is reloaded while empty or invalid, then it causes all SSL certificates to be considered invalid (until it is reloaded again), which could break the sniffing/administrative client that runs underneath the tests.
1 parent 652193f commit f0f16b7

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/ssl/SSLTrustRestrictionsTests.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@
2828
import javax.net.ssl.SSLSocketFactory;
2929
import java.io.IOException;
3030
import java.net.SocketException;
31+
import java.nio.file.AtomicMoveNotSupportedException;
3132
import java.nio.file.Files;
3233
import java.nio.file.Path;
3334
import java.security.PrivateKey;
3435
import java.security.cert.X509Certificate;
3536
import java.util.Collections;
3637
import java.util.concurrent.TimeUnit;
3738

39+
import static java.nio.file.StandardCopyOption.ATOMIC_MOVE;
40+
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
3841
import static org.hamcrest.Matchers.is;
3942

4043
/**
@@ -46,11 +49,6 @@
4649
@TestLogging("org.elasticsearch.xpack.ssl.RestrictedTrustManager:DEBUG")
4750
public class SSLTrustRestrictionsTests extends SecurityIntegTestCase {
4851

49-
/**
50-
* Use a small keysize for performance, since the keys are only used in this test, but a large enough keysize
51-
* to get past the SSL algorithm checker
52-
*/
53-
5452
private static final int RESOURCE_RELOAD_MILLIS = 3;
5553
private static final TimeValue MAX_WAIT_RELOAD = TimeValue.timeValueSeconds(1);
5654

@@ -61,6 +59,7 @@ public class SSLTrustRestrictionsTests extends SecurityIntegTestCase {
6159
private static CertificateInfo trustedCert;
6260
private static CertificateInfo untrustedCert;
6361
private static Path restrictionsPath;
62+
private static Path restrictionsTmpPath;
6463

6564
@Override
6665
protected int maxNumberOfNodes() {
@@ -124,6 +123,8 @@ public Settings nodeSettings(int nodeOrdinal) {
124123
.put(nodeSSL);
125124

126125
restrictionsPath = configPath.resolve("trust_restrictions.yml");
126+
restrictionsTmpPath = configPath.resolve("trust_restrictions.tmp");
127+
127128
writeRestrictions("*.trusted");
128129
builder.put("xpack.ssl.trust_restrictions.path", restrictionsPath);
129130
builder.put("resource.reload.interval.high", RESOURCE_RELOAD_MILLIS + "ms");
@@ -133,7 +134,12 @@ public Settings nodeSettings(int nodeOrdinal) {
133134

134135
private void writeRestrictions(String trustedPattern) {
135136
try {
136-
Files.write(restrictionsPath, Collections.singleton("trust.subject_name: \"" + trustedPattern + "\""));
137+
Files.write(restrictionsTmpPath, Collections.singleton("trust.subject_name: \"" + trustedPattern + "\""));
138+
try {
139+
Files.move(restrictionsTmpPath, restrictionsPath, REPLACE_EXISTING, ATOMIC_MOVE);
140+
} catch (final AtomicMoveNotSupportedException e) {
141+
Files.move(restrictionsTmpPath, restrictionsPath, REPLACE_EXISTING);
142+
}
137143
} catch (IOException e) {
138144
throw new ElasticsearchException("failed to write restrictions", e);
139145
}

0 commit comments

Comments
 (0)