Skip to content

Commit 986dce7

Browse files
committed
fix modules/plugins to run inside FIPS env.
1 parent 9b5da5c commit 986dce7

File tree

78 files changed

+1074
-195
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+1074
-195
lines changed

buildSrc/src/main/groovy/org/opensearch/gradle/test/StandaloneRestTestPlugin.groovy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
package org.opensearch.gradle.test
3232

3333
import groovy.transform.CompileStatic
34+
import org.gradle.api.artifacts.VersionCatalog
35+
import org.gradle.api.artifacts.VersionCatalogsExtension
3436
import org.opensearch.gradle.OpenSearchJavaPlugin
3537
import org.opensearch.gradle.ExportOpenSearchBuildResourcesTask
3638
import org.opensearch.gradle.RepositoriesSetupPlugin
@@ -92,6 +94,10 @@ class StandaloneRestTestPlugin implements Plugin<Project> {
9294
// create a compileOnly configuration as others might expect it
9395
project.configurations.create("compileOnly")
9496
project.dependencies.add('testImplementation', project.project(':test:framework'))
97+
if (BuildParams.inFipsJvm) {
98+
VersionCatalog libs = project.extensions.getByType(VersionCatalogsExtension).named("libs")
99+
project.dependencies.add('testImplementation', libs.findBundle("bouncycastle").get())
100+
}
95101

96102
EclipseModel eclipse = project.extensions.getByType(EclipseModel)
97103
eclipse.classpath.sourceSets = [testSourceSet]

client/rest-high-level/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ apply plugin: 'opensearch.build'
3636
apply plugin: 'opensearch.rest-test'
3737
apply plugin: 'opensearch.publish'
3838
apply plugin: 'opensearch.rest-resources'
39+
apply from: "$rootDir/gradle/fips.gradle"
3940

4041
base {
4142
group = 'org.opensearch.client'
@@ -66,6 +67,7 @@ dependencies {
6667
testImplementation "junit:junit:${versions.junit}"
6768
//this is needed to make RestHighLevelClientTests#testApiNamingConventions work from IDEs
6869
testImplementation project(":rest-api-spec")
70+
testFipsRuntimeOnly libs.bundles.bouncycastle
6971
}
7072

7173
tasks.named('forbiddenApisMain').configure {

client/rest/build.gradle

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ dependencies {
7272
testImplementation "org.apache.logging.log4j:log4j-core:${versions.log4j}"
7373
testImplementation "org.apache.logging.log4j:log4j-jul:${versions.log4j}"
7474
testImplementation "org.apache.logging.log4j:log4j-slf4j-impl:${versions.log4j}"
75-
76-
testFipsOnly "org.bouncycastle:bc-fips:${versions.bouncycastle_jce}"
77-
testFipsOnly "org.bouncycastle:bctls-fips:${versions.bouncycastle_tls}"
7875
}
7976

8077
tasks.named("dependencyLicenses").configure {

client/rest/src/main/java/org/opensearch/client/RestClientBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ private CloseableHttpAsyncClient createHttpClient() {
315315

316316
try {
317317
final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create()
318-
.setSslContext(SSLContext.getDefault())
318+
.setSslContext(SSLContext.getInstance("TLS"))
319319
// See https://issues.apache.org/jira/browse/HTTPCLIENT-2219
320320
.setTlsDetailsFactory(new Factory<SSLEngine, TlsDetails>() {
321321
@Override

client/rest/src/test/java/org/opensearch/client/RestClientBuilderIntegTests.java

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939

4040
import org.apache.hc.core5.http.HttpHost;
4141
import org.apache.hc.core5.ssl.SSLContextBuilder;
42-
import org.bouncycastle.crypto.CryptoServicesRegistrar;
43-
import org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider;
44-
import org.bouncycastle.jsse.provider.BouncyCastleJsseProvider;
4542
import org.junit.AfterClass;
4643
import org.junit.BeforeClass;
4744

@@ -58,7 +55,6 @@
5855
import java.security.KeyStore;
5956
import java.security.PrivilegedAction;
6057
import java.security.SecureRandom;
61-
import java.security.Security;
6258

6359
import static org.hamcrest.MatcherAssert.assertThat;
6460
import static org.hamcrest.Matchers.instanceOf;
@@ -70,25 +66,12 @@
7066
*/
7167
public class RestClientBuilderIntegTests extends RestClientTestCase implements RestClientFipsAwareTestCase {
7268

73-
static {
74-
if (inFipsJvm()) {
75-
int highestPriority = 1;
76-
if (Security.getProvider(BouncyCastleFipsProvider.PROVIDER_NAME) == null) {
77-
Security.insertProviderAt(new BouncyCastleFipsProvider(), highestPriority++);
78-
}
79-
if (Security.getProvider(BouncyCastleJsseProvider.PROVIDER_NAME) == null) {
80-
Security.insertProviderAt(new BouncyCastleJsseProvider(), highestPriority);
81-
}
82-
}
83-
}
84-
8569
private static HttpsServer httpsServer;
8670

8771
@BeforeClass
8872
public static void startHttpServer() throws Exception {
8973
httpsServer = HttpsServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
90-
String keyStoreType = CryptoServicesRegistrar.isInApprovedOnlyMode() ? "BCFKS" : "JKS";
91-
httpsServer.setHttpsConfigurator(new HttpsConfigurator(getSslContext(true, keyStoreType)));
74+
httpsServer.setHttpsConfigurator(new HttpsConfigurator(new RestClientBuilderIntegTests().getSslContext(true)));
9275
httpsServer.createContext("/", new ResponseHandler());
9376
httpsServer.start();
9477
}
@@ -108,11 +91,6 @@ public static void stopHttpServers() throws IOException {
10891
}
10992

11093
public void testBuilderUsesDefaultSSLContext() throws Exception {
111-
makeRequest();
112-
}
113-
114-
@Override
115-
public void makeRequest(String keyStoreType) throws Exception {
11694
final SSLContext defaultSSLContext = SSLContext.getDefault();
11795
try {
11896
try (RestClient client = buildRestClient()) {
@@ -124,7 +102,7 @@ public void makeRequest(String keyStoreType) throws Exception {
124102
}
125103
}
126104

127-
SSLContext.setDefault(getSslContext(false, keyStoreType));
105+
SSLContext.setDefault(getSslContext(false));
128106
try (RestClient client = buildRestClient()) {
129107
Response response = client.performRequest(new Request("GET", "/"));
130108
assertEquals(200, response.getStatusLine().getStatusCode());
@@ -139,19 +117,10 @@ private RestClient buildRestClient() {
139117
return RestClient.builder(new HttpHost("https", address.getHostString(), address.getPort())).build();
140118
}
141119

142-
private static SSLContext getSslContext(boolean server, String keyStoreType) throws Exception {
120+
@Override
121+
public SSLContext getSslContext(boolean server, String keyStoreType, SecureRandom secureRandom, String fileExtension) throws Exception {
143122
SSLContext sslContext;
144123
char[] password = "password".toCharArray();
145-
SecureRandom secureRandom;
146-
String fileExtension;
147-
148-
if (CryptoServicesRegistrar.isInApprovedOnlyMode()) {
149-
secureRandom = SecureRandom.getInstance("DEFAULT", "BCFIPS");
150-
fileExtension = ".bcfks";
151-
} else {
152-
secureRandom = SecureRandom.getInstanceStrong();
153-
fileExtension = ".jks";
154-
}
155124

156125
try (
157126
InputStream trustStoreFile = RestClientBuilderIntegTests.class.getResourceAsStream("/test_truststore" + fileExtension);

client/rest/src/test/java/org/opensearch/client/RestClientFipsAwareTestCase.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,27 @@
88

99
package org.opensearch.client;
1010

11+
import javax.net.ssl.SSLContext;
12+
13+
import java.security.SecureRandom;
14+
1115
import static org.opensearch.client.RestClientTestCase.inFipsJvm;
1216

1317
public interface RestClientFipsAwareTestCase {
1418

15-
default void makeRequest() throws Exception {
19+
default SSLContext getSslContext(boolean server) throws Exception {
20+
String keyStoreType = inFipsJvm() ? "BCFKS" : "JKS";
21+
String fileExtension = inFipsJvm() ? ".bcfks" : ".jks";
22+
SecureRandom secureRandom;
23+
1624
if (inFipsJvm()) {
17-
makeRequest("BCFKS");
25+
secureRandom = SecureRandom.getInstance("DEFAULT", "BCFIPS");
1826
} else {
19-
makeRequest("JKS");
27+
secureRandom = SecureRandom.getInstanceStrong();
2028
}
29+
30+
return getSslContext(server, keyStoreType, secureRandom, fileExtension);
2131
}
2232

23-
void makeRequest(String keyStoreType) throws Exception;
33+
SSLContext getSslContext(boolean server, String keyStoreType, SecureRandom secureRandom, String fileExtension) throws Exception;
2434
}

client/sniffer/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*/
3030
apply plugin: 'opensearch.build'
3131
apply plugin: 'opensearch.publish'
32+
apply from: "$rootDir/gradle/fips.gradle"
3233

3334
java {
3435
targetCompatibility = JavaVersion.VERSION_11
@@ -47,6 +48,9 @@ dependencies {
4748
api "commons-codec:commons-codec:${versions.commonscodec}"
4849
api "commons-logging:commons-logging:${versions.commonslogging}"
4950
api "com.fasterxml.jackson.core:jackson-core:${versions.jackson}"
51+
fipsRuntimeOnly "org.bouncycastle:bc-fips:${versions.bouncycastle_jce}"
52+
fipsRuntimeOnly "org.bouncycastle:bctls-fips:${versions.bouncycastle_tls}"
53+
fipsRuntimeOnly "org.bouncycastle:bcutil-fips:${versions.bouncycastle_util}"
5054

5155
testImplementation project(":client:test")
5256
testImplementation "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}"
@@ -57,6 +61,10 @@ dependencies {
5761
testImplementation "net.bytebuddy:byte-buddy-agent:${versions.bytebuddy}"
5862
}
5963

64+
tasks.named("dependencyLicenses").configure {
65+
mapping from: /bc.*/, to: 'bouncycastle'
66+
}
67+
6068
tasks.named('forbiddenApisMain').configure {
6169
//client does not depend on server, so only jdk signatures should be checked
6270
replaceSignatureFiles 'jdk-signatures'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ee9ac432cf08f9a9ebee35d7cf8a45f94959a7ab
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
9cc33650ede63bc1a8281ed5c8e1da314d50bc76
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a1857cd639295b10cc90e6d31ecbc523cdafcc19

0 commit comments

Comments
 (0)