Skip to content

Commit

Permalink
[Test] Increase test secret key length (#117675) (#117738)
Browse files Browse the repository at this point in the history
Running with FIPS approved mode requires secret keys to be at least 114
bits long.

Relates: #117324 Resolves: #117596 Resolves: #117709 Resolves: #117710
Resolves: #117711 Resolves: #117712
(cherry picked from commit 24bc505)

# Conflicts:
#	modules/repository-s3/src/javaRestTest/java/org/elasticsearch/repositories/s3/RepositoryS3RestReloadCredentialsIT.java
#	muted-tests.yml
#	test/fixtures/s3-fixture/src/main/java/fixture/s3/S3HttpFixtureWithSTS.java
  • Loading branch information
ywangd authored Nov 29, 2024
1 parent 5170cae commit f52b8c6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package org.elasticsearch.repositories.s3;

import fixture.s3.S3HttpFixture;
import io.netty.handler.codec.http.HttpMethod;

import org.elasticsearch.client.Request;
import org.elasticsearch.client.ResponseException;
Expand Down Expand Up @@ -54,8 +55,6 @@ protected String getTestRestCluster() {
}

public void testReloadCredentialsFromKeystore() throws IOException {
assumeFalse("doesn't work in a FIPS JVM, but that's ok", inFipsJvm());

// Register repository (?verify=false because we don't have access to the blob store yet)
final var repositoryName = randomIdentifier();
registerRepository(
Expand All @@ -70,15 +69,16 @@ public void testReloadCredentialsFromKeystore() throws IOException {
final var accessKey1 = randomIdentifier();
s3Fixture.setAccessKey(accessKey1);
keystoreSettings.put("s3.client.default.access_key", accessKey1);
keystoreSettings.put("s3.client.default.secret_key", randomIdentifier());
keystoreSettings.put("s3.client.default.secret_key", randomSecretKey());
cluster.updateStoredSecureSettings();
assertOK(client().performRequest(new Request("POST", "/_nodes/reload_secure_settings")));

assertOK(client().performRequest(createReloadSecureSettingsRequest()));

// Check access using initial credentials
assertOK(client().performRequest(verifyRequest));

// Rotate credentials in blob store
final var accessKey2 = randomValueOtherThan(accessKey1, ESTestCase::randomIdentifier);
final var accessKey2 = randomValueOtherThan(accessKey1, ESTestCase::randomSecretKey);
s3Fixture.setAccessKey(accessKey2);

// Ensure that initial credentials now invalid
Expand All @@ -92,10 +92,17 @@ public void testReloadCredentialsFromKeystore() throws IOException {
// Set up refreshed credentials
keystoreSettings.put("s3.client.default.access_key", accessKey2);
cluster.updateStoredSecureSettings();
assertOK(client().performRequest(new Request("POST", "/_nodes/reload_secure_settings")));
assertOK(client().performRequest(createReloadSecureSettingsRequest()));

// Check access using refreshed credentials
assertOK(client().performRequest(verifyRequest));
}

private Request createReloadSecureSettingsRequest() throws IOException {
return newXContentRequest(
HttpMethod.POST,
"/_nodes/reload_secure_settings",
(b, p) -> inFipsJvm() ? b.field("secure_settings_password", "keystore-password") : b
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Set;

import static org.elasticsearch.test.ESTestCase.randomIdentifier;
import static org.elasticsearch.test.ESTestCase.randomSecretKey;

/**
* Minimal HTTP handler that emulates the EC2 IMDS server
Expand Down Expand Up @@ -82,7 +83,7 @@ public void handle(final HttpExchange exchange) throws IOException {
accessKey,
ZonedDateTime.now(Clock.systemUTC()).plusDays(1L).format(DateTimeFormatter.ISO_DATE_TIME),
randomIdentifier(),
randomIdentifier(),
randomSecretKey(),
sessionToken
).getBytes(StandardCharsets.UTF_8);
exchange.getResponseHeaders().add("Content-Type", "application/json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,13 @@ public static String randomDateFormatterPattern() {
return randomFrom(FormatNames.values()).getName();
}

/**
* Generate a random string of at least 112 bits to satisfy minimum entropy requirement when running in FIPS mode.
*/
public static String randomSecretKey() {
return randomAlphaOfLengthBetween(14, 20);
}

/**
* Randomly choose between {@link EsExecutors#DIRECT_EXECUTOR_SERVICE} (which does not fork), {@link ThreadPool#generic}, and one of the
* other named threadpool executors.
Expand Down

0 comments on commit f52b8c6

Please sign in to comment.