Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
#188 Adding support for dataprepper auth.
Browse files Browse the repository at this point in the history
  • Loading branch information
jzonthemtn committed Jun 5, 2024
1 parent 022585a commit d9b5be4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
4 changes: 4 additions & 0 deletions dataprepper/pipelines.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ chorus-ubi-pipeline:
http:
port: 2021
ssl: false
# authentication:
# http_basic:
# username: ubi
# password: ubi
sink:
- opensearch:
hosts: [ "http://ubi-dev-os:9200" ]
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ services:
logger.level: info
OPENSEARCH_INITIAL_ADMIN_PASSWORD: SuperSecretPassword_123
#ubi.dataprepper.url: "http://dataprepper-dev-os:2021/log/ingest"
#ubi.dataprepper.auth.username: "ubi"
#ubi.dataprepper.auth.password: "ubi"
ulimits:
memlock:
soft: -1
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/org/opensearch/ubi/UbiActionFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@

package org.opensearch.ubi;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
Expand Down Expand Up @@ -44,9 +41,11 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Base64;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -186,11 +185,21 @@ private void sendToDataPrepper(final String dataPrepperUrl, final QueryRequest q

try (CloseableHttpClient httpClient = HttpClients.createDefault()) {

final String dataPrepperUserName = environment.settings().get(UbiSettings.DATA_PREPPER_AUTH_USERNAME);

final HttpPost httpPost = new HttpPost(dataPrepperUrl);

httpPost.setEntity(new StringEntity(queryRequest.toString()));
httpPost.setHeader("Content-type", "application/json");

if(dataPrepperUserName != null) {
final String dataPrepperPassword = environment.settings().get(UbiSettings.DATA_PREPPER_AUTH_PASSWORD);
final String auth = dataPrepperUserName + ":" + dataPrepperPassword;
final byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.ISO_8859_1));
final String authHeader = "Basic " + new String(encodedAuth, StandardCharsets.ISO_8859_1);
httpPost.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
}

AccessController.doPrivileged((PrivilegedAction<Boolean>) () -> {
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
final int status = response.getStatusLine().getStatusCode();
Expand Down
27 changes: 24 additions & 3 deletions src/main/java/org/opensearch/ubi/UbiSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import org.opensearch.common.settings.Setting;

import java.util.Collections;
import java.util.List;

/**
Expand All @@ -23,18 +22,40 @@ public class UbiSettings {
*/
public static final String DATA_PREPPER_URL = "ubi.dataprepper.url";

/**
* The optional username for Data Prepper's http_source.
*/
public static final String DATA_PREPPER_AUTH_USERNAME = "ubi.dataprepper.auth.username";

/**
* The optional password for Data Prepper's http_source.
*/
public static final String DATA_PREPPER_AUTH_PASSWORD = "ubi.dataprepper.auth.password";

private static final Setting<String> DATA_PREPPER_URL_SETTING = Setting.simpleString(
DATA_PREPPER_URL,
Setting.Property.Dynamic,
Setting.Property.NodeScope);

private static final Setting<String> DATA_PREPPER_AUTH_USERNAME_SETTING = Setting.simpleString(
DATA_PREPPER_AUTH_USERNAME,
Setting.Property.Dynamic,
Setting.Property.NodeScope);

private static final Setting<String> DATA_PREPPER_AUTH_PASSWORD_PASSWORD = Setting.simpleString(
DATA_PREPPER_AUTH_PASSWORD,
Setting.Property.Dynamic,
Setting.Property.NodeScope);

/**
* Gets a list of the UBI plugin settings.
* @return A list of the UBI plugin settings.
*/
public static List<Setting<?>> getSettings() {
return Collections.singletonList(
DATA_PREPPER_URL_SETTING
return List.of(
DATA_PREPPER_URL_SETTING,
DATA_PREPPER_AUTH_USERNAME_SETTING,
DATA_PREPPER_AUTH_PASSWORD_PASSWORD
);
}

Expand Down

0 comments on commit d9b5be4

Please sign in to comment.