Skip to content

Commit

Permalink
Download certs from security repo (#650)
Browse files Browse the repository at this point in the history
* Download certs from security repo

Signed-off-by: Craig Perkins <[email protected]>

* Remove unused import

Signed-off-by: Craig Perkins <[email protected]>

* Fix ci check

Signed-off-by: Craig Perkins <[email protected]>

* Include setup-java step

Signed-off-by: Craig Perkins <[email protected]>

* getParent

Signed-off-by: Craig Perkins <[email protected]>

* Remove markAsSystemContext

Signed-off-by: Craig Perkins <[email protected]>

* Configure basic auth header

Signed-off-by: Craig Perkins <[email protected]>

* Remove unused imports

Signed-off-by: Craig Perkins <[email protected]>

* Update link to security repo

Signed-off-by: Craig Perkins <[email protected]>

---------

Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks authored Nov 6, 2024
1 parent 546d060 commit 63fa8d2
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 46 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ jobs:
ACTIONS_ALLOW_USE_UNSECURE_NODE_VERSION: true

steps:
# This step uses the setup-java Github action: https://github.com/actions/setup-java
- name: Set Up JDK ${{ matrix.java }}
uses: actions/setup-java@v3
with:
distribution: temurin # Temurin is a distribution of adoptium
java-version: ${{ matrix.java }}
- name: Checkout Branch
uses: actions/checkout@v3
- uses: actions/download-artifact@v3
Expand All @@ -90,9 +96,9 @@ jobs:
- name: Pull and Run Docker for security tests
run: |
plugin=${{ needs.linux-build.outputs.build-test-linux }}
version=`echo $plugin|awk -F- '{print $3}'| cut -d. -f 1-3`
plugin_version=`echo $plugin|awk -F- '{print $3}'| cut -d. -f 1-4`
qualifier=`echo $plugin|awk -F- '{print $4}'| cut -d. -f 1-1`
version=`echo $plugin|awk -F- '{print $4}'| cut -d. -f 1-3`
plugin_version=`echo $plugin|awk -F- '{print $4}'| cut -d. -f 1-4`
qualifier=`echo $plugin|awk -F- '{print $5}'| cut -d. -f 1-1`
if [ -n "$qualifier" ] && [ "$qualifier" != "SNAPSHOT" ]; then
qualifier=-${qualifier}
Expand Down
15 changes: 15 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ buildscript {
//****************************************************************************/

plugins {
id "de.undercouch.download" version "5.3.0"
id 'com.netflix.nebula.ospackage' version "11.10.0"
id 'checkstyle'
}
Expand Down Expand Up @@ -79,6 +80,20 @@ ext {
projectSubstitutions = [:]
licenseFile = rootProject.file('LICENSE.txt')
noticeFile = rootProject.file('NOTICE.txt')

['sample.pem', 'test-kirk.jks'].forEach { file ->
File local = getLayout().getBuildDirectory().file(file).get().getAsFile()
download.run {
src "https://raw.githubusercontent.com/opensearch-project/security/refs/heads/main/bwc-test/src/test/resources/security/" + file
dest local
overwrite false
}
}

processResources {
from(getLayout().getBuildDirectory().file('sample.pem').get().getAsFile())
from(getLayout().getBuildDirectory().file('test-kirk.jks').get().getAsFile())
}
}

java {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ public void run() {
public final void performCleanUp() {
final ThreadContext threadContext = threadPool.getThreadContext();
try (ThreadContext.StoredContext ignore = threadContext.stashContext()) {
// we have to execute under the system context so that if security is enabled the sync is authorized
threadContext.markAsSystemContext();
final Map<String, DiscoveryNode> dataNodes = clusterService.state().nodes().getDataNodes();
List<DiscoveryNode> nodes = Stream.of(dataNodes.values().toArray(new DiscoveryNode[0]))
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.core5.http.message.BasicHeader;
import org.apache.hc.core5.ssl.SSLContextBuilder;
import org.apache.hc.core5.util.Timeout;
Expand All @@ -36,8 +33,11 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -95,7 +95,7 @@ protected RestClient buildClient(Settings settings, HttpHost[] hosts) throws IOE
if (Objects.nonNull(keystore)) {
URI uri = null;
try {
uri = this.getClass().getClassLoader().getResource("security/sample.pem").toURI();
uri = this.getClass().getClassLoader().getResource("sample.pem").toURI();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -145,23 +145,22 @@ protected void wipeAllOSIndices() throws IOException {
}

protected static void configureHttpsClient(RestClientBuilder builder, Settings settings) throws IOException {
Map<String, String> headers = ThreadContext.buildDefaultHeaders(settings);
Map<String, String> headers = new HashMap<>(ThreadContext.buildDefaultHeaders(settings));
if (System.getProperty("user") != null && System.getProperty("password") != null) {
String userName = System.getProperty("user");
String password = System.getProperty("password");
headers.put(
"Authorization",
"Basic " + Base64.getEncoder().encodeToString((userName + ":" + password).getBytes(StandardCharsets.UTF_8))
);
}
Header[] defaultHeaders = new Header[headers.size()];
int i = 0;
for (Map.Entry<String, String> entry : headers.entrySet()) {
defaultHeaders[i++] = new BasicHeader(entry.getKey(), entry.getValue());
}
builder.setDefaultHeaders(defaultHeaders);
builder.setHttpClientConfigCallback(httpClientBuilder -> {
String userName = Optional
.ofNullable(System.getProperty("user"))
.orElseThrow(() -> new RuntimeException("user name is missing"));
String password = Optional
.ofNullable(System.getProperty("password"))
.orElseThrow(() -> new RuntimeException("password is missing"));
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(new HttpHost("localhost", 9200)),
new UsernamePasswordCredentials(userName, password.toCharArray()));
try {
final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create()
.setSslContext(SSLContextBuilder.create().loadTrustMaterial(null, (chains, authType) -> true).build())
Expand All @@ -172,8 +171,7 @@ protected static void configureHttpsClient(RestClientBuilder builder, Settings s
.build();

return httpClientBuilder
.setConnectionManager(connectionManager)
.setDefaultCredentialsProvider(credentialsProvider);
.setConnectionManager(connectionManager);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
25 changes: 0 additions & 25 deletions src/test/resources/security/sample.pem

This file was deleted.

Binary file removed src/test/resources/security/test-kirk.jks
Binary file not shown.

0 comments on commit 63fa8d2

Please sign in to comment.