Skip to content

Commit

Permalink
Refactoring datasource changes to a new module. (opensearch-project#1504
Browse files Browse the repository at this point in the history
)

* Reafactor into new datasources module

Signed-off-by: vamsi-amazon <reddyvam@amazon.com>

* Refactored all the datasource releated code to new datasources module

Signed-off-by: vamsi-amazon <reddyvam@amazon.com>

* More unit tests

Signed-off-by: vamsi-amazon <reddyvam@amazon.com>

---------

Signed-off-by: vamsi-amazon <reddyvam@amazon.com>
  • Loading branch information
vamsi-amazon authored Apr 10, 2023
1 parent e805151 commit 7584f79
Show file tree
Hide file tree
Showing 56 changed files with 1,625 additions and 494 deletions.
1 change: 0 additions & 1 deletion common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ dependencies {
api group: 'com.google.guava', name: 'guava', version: '31.0.1-jre'
api group: 'org.apache.logging.log4j', name: 'log4j-core', version:'2.17.1'
api group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
api 'com.amazonaws:aws-encryption-sdk-java:2.4.0'

testImplementation group: 'junit', name: 'junit', version: '4.13.2'
testImplementation group: 'org.assertj', name: 'assertj-core', version: '3.9.1'
Expand Down
3 changes: 2 additions & 1 deletion config/checkstyle/google_checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@
<property name="max" value="100"/>
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
</module>

<module name="SuppressWarningsFilter" />
<module name="TreeWalker">
<module name="SuppressWarningsHolder" />
<module name="OuterTypeFilename"/>
<module name="IllegalTokenText">
<property name="tokens" value="STRING_LITERAL, CHAR_LITERAL"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.opensearch.sql.datasource.DataSourceService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

package org.opensearch.sql.storage;

import java.util.Map;
import org.opensearch.sql.datasource.DataSourceService;
import org.opensearch.sql.datasource.model.DataSource;
import org.opensearch.sql.datasource.model.DataSourceMetadata;
Expand Down
82 changes: 82 additions & 0 deletions datasources/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

plugins {
id 'java-library'
id "io.freefair.lombok"
id 'jacoco'
}

repositories {
mavenCentral()
}

dependencies {
implementation project(':core')
implementation project(':protocol')
implementation group: 'org.opensearch', name: 'opensearch', version: "${opensearch_version}"
implementation group: 'org.opensearch', name: 'opensearch-x-content', version: "${opensearch_version}"
implementation group: 'org.opensearch', name: 'common-utils', version: "${opensearch_build}"
implementation group: 'commons-io', name: 'commons-io', version: '2.8.0'
implementation 'com.amazonaws:aws-encryption-sdk-java:2.4.0'

testImplementation group: 'junit', name: 'junit', version: '4.13.2'
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
testImplementation group: 'net.bytebuddy', name: 'byte-buddy-agent', version: '1.12.13'
testImplementation group: 'org.hamcrest', name: 'hamcrest-library', version: '2.1'
testImplementation group: 'org.mockito', name: 'mockito-core', version: '5.2.0'
testImplementation group: 'org.mockito', name: 'mockito-junit-jupiter', version: '5.2.0'
testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
exceptionFormat "full"
}
}

jacocoTestReport {
reports {
html.enabled true
xml.enabled true
}
afterEvaluate {
classDirectories.setFrom(files(classDirectories.files.collect {
fileTree(dir: it)
}))
}
}
test.finalizedBy(project.tasks.jacocoTestReport)

jacocoTestCoverageVerification {
violationRules {
rule {
element = 'CLASS'
excludes = [
'org.opensearch.sql.datasources.settings.DataSourceSettings',
'org.opensearch.sql.datasources.exceptions.*',
'org.opensearch.sql.datasources.model.*',
'org.opensearch.sql.datasources.rest.*'
]
limit {
counter = 'LINE'
minimum = 1.0
}
limit {
counter = 'BRANCH'
minimum = 0.9
}
}
}
afterEvaluate {
classDirectories.setFrom(files(classDirectories.files.collect {
fileTree(dir: it)
}))
}
}
check.dependsOn jacocoTestCoverageVerification
jacocoTestCoverageVerification.dependsOn jacocoTestReport
3 changes: 3 additions & 0 deletions datasources/lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This file is generated by the 'io.freefair.lombok' Gradle plugin
config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
*/

package org.opensearch.sql.datasource.model.auth;
package org.opensearch.sql.datasources.auth;

import java.util.Collections;
import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.opensearch.sql.datasource;
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.sql.datasources.auth;

import java.util.List;
import org.opensearch.sql.datasource.model.DataSourceMetadata;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.sql.plugin.datasource;
package org.opensearch.sql.datasources.auth;

import static org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT;
import static org.opensearch.sql.analysis.DataSourceSchemaIdentifierNameResolver.DEFAULT_DATASOURCE_NAME;

import java.util.List;
import lombok.AllArgsConstructor;
import org.opensearch.client.Client;
import org.opensearch.commons.ConfigConstants;
import org.opensearch.commons.authuser.User;
import org.opensearch.sql.datasource.DataSourceUserAuthorizationHelper;
import org.opensearch.sql.datasource.model.DataSourceMetadata;

@AllArgsConstructor
Expand All @@ -21,13 +20,15 @@ public class DataSourceUserAuthorizationHelperImpl implements DataSourceUserAuth

private Boolean isAuthorizationRequired() {
String userString = client.threadPool()
.getThreadContext().getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT);
.getThreadContext().getTransient(
ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT);
return userString != null;
}

private List<String> getUserRoles() {
String userString = client.threadPool()
.getThreadContext().getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT);
.getThreadContext().getTransient(
ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT);
User user = User.parse(userString);
return user.getRoles();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
*/

package org.opensearch.sql.common.encryptor;
package org.opensearch.sql.datasources.encryptor;

public interface Encryptor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
*/

package org.opensearch.sql.common.encryptor;
package org.opensearch.sql.datasources.encryptor;

import com.amazonaws.encryptionsdk.AwsCrypto;
import com.amazonaws.encryptionsdk.CommitmentPolicy;
Expand All @@ -29,8 +29,8 @@ public String encrypt(String plainText) {
.build();

JceMasterKey jceMasterKey
= JceMasterKey.getInstance(new SecretKeySpec(masterKey.getBytes(), "AES"), "Custom", "",
"AES/GCM/NoPadding");
= JceMasterKey.getInstance(new SecretKeySpec(masterKey.getBytes(), "AES"), "Custom",
"opensearch.config.master.key", "AES/GCM/NoPadding");

final CryptoResult<byte[], JceMasterKey> encryptResult = crypto.encryptData(jceMasterKey,
plainText.getBytes(StandardCharsets.UTF_8));
Expand All @@ -44,8 +44,8 @@ public String decrypt(String encryptedText) {
.build();

JceMasterKey jceMasterKey
= JceMasterKey.getInstance(new SecretKeySpec(masterKey.getBytes(), "AES"), "Custom", "",
"AES/GCM/NoPadding");
= JceMasterKey.getInstance(new SecretKeySpec(masterKey.getBytes(), "AES"), "Custom",
"opensearch.config.master.key", "AES/GCM/NoPadding");

final CryptoResult<byte[], JceMasterKey> decryptedResult
= crypto.decryptData(jceMasterKey, Base64.getDecoder().decode(encryptedText));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
*/

package org.opensearch.sql.datasource.exceptions;
package org.opensearch.sql.datasources.exceptions;

/**
* DataSourceNotFoundException.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.datasources.exceptions;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import lombok.Getter;
import org.opensearch.rest.RestStatus;

/**
* Error Message.
*/
public class ErrorMessage {

protected Throwable exception;

private final int status;

@Getter
private final String type;

@Getter
private final String reason;

@Getter
private final String details;

/**
* Error Message Constructor.
*/
public ErrorMessage(Throwable exception, int status) {
this.exception = exception;
this.status = status;

this.type = fetchType();
this.reason = fetchReason();
this.details = fetchDetails();
}

private String fetchType() {
return exception.getClass().getSimpleName();
}

protected String fetchReason() {
return status == RestStatus.BAD_REQUEST.getStatus()
? "Invalid Request"
: "There was internal problem at backend";
}

protected String fetchDetails() {
// Some exception prints internal information (full class name) which is security concern
return emptyStringIfNull(exception.getLocalizedMessage());
}

private String emptyStringIfNull(String str) {
return str != null ? str : "";
}

@Override
public String toString() {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("status", status);
jsonObject.add("error", getErrorAsJson());
return new Gson().toJson(jsonObject);
}

private JsonObject getErrorAsJson() {
JsonObject errorJson = new JsonObject();
errorJson.addProperty("type", type);
errorJson.addProperty("reason", reason);
errorJson.addProperty("details", details);
return errorJson;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
*/

package org.opensearch.sql.plugin.model;
package org.opensearch.sql.datasources.model.transport;


import static org.opensearch.sql.analysis.DataSourceSchemaIdentifierNameResolver.DEFAULT_DATASOURCE_NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
*/

package org.opensearch.sql.plugin.model;
package org.opensearch.sql.datasources.model.transport;

import java.io.IOException;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
*/

package org.opensearch.sql.plugin.model;
package org.opensearch.sql.datasources.model.transport;

import static org.opensearch.sql.analysis.DataSourceSchemaIdentifierNameResolver.DEFAULT_DATASOURCE_NAME;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
*/

package org.opensearch.sql.plugin.model;
package org.opensearch.sql.datasources.model.transport;

import java.io.IOException;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
*/

package org.opensearch.sql.plugin.model;
package org.opensearch.sql.datasources.model.transport;

import static org.opensearch.sql.analysis.DataSourceSchemaIdentifierNameResolver.DEFAULT_DATASOURCE_NAME;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
*/

package org.opensearch.sql.plugin.model;
package org.opensearch.sql.datasources.model.transport;

import java.io.IOException;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@
*
*/

package org.opensearch.sql.plugin.model;
package org.opensearch.sql.datasources.model.transport;


import static org.opensearch.sql.analysis.DataSourceSchemaIdentifierNameResolver.DEFAULT_DATASOURCE_NAME;

import java.io.IOException;
import lombok.Getter;
import org.json.JSONObject;
import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.sql.datasource.model.DataSourceMetadata;
import org.opensearch.sql.protocol.response.format.JsonResponseFormatter;

public class UpdateDataSourceActionRequest
extends ActionRequest {
Expand Down
Loading

0 comments on commit 7584f79

Please sign in to comment.