Skip to content

Commit

Permalink
Refactored all the datasource releated code to new datasources module
Browse files Browse the repository at this point in the history
Signed-off-by: vamsi-amazon <reddyvam@amazon.com>
  • Loading branch information
vamsimanohar committed Apr 8, 2023
1 parent 68d677a commit a9111a9
Show file tree
Hide file tree
Showing 48 changed files with 1,206 additions and 179 deletions.
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
6 changes: 5 additions & 1 deletion datasources/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ jacocoTestCoverageVerification {
violationRules {
rule {
element = 'CLASS'
excludes = []
excludes = [
'org.opensearch.sql.datasources.settings.DataSourceSettings',
'org.opensearch.sql.datasources.exceptions.*',
'org.opensearch.sql.datasources.rest.*'
]
limit {
counter = 'LINE'
minimum = 1.0
Expand Down
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 @@ -12,7 +12,6 @@
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.datasources.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.datasources.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.datasources.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.datasources.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.datasources.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.datasources.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.datasources.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.datasources.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 @@ -28,23 +28,23 @@
import org.opensearch.rest.RestChannel;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.RestStatus;
import org.opensearch.sql.datasource.exceptions.DataSourceNotFoundException;
import org.opensearch.sql.datasource.model.DataSourceMetadata;
import org.opensearch.sql.datasources.exceptions.DataSourceNotFoundException;
import org.opensearch.sql.datasources.exceptions.ErrorMessage;
import org.opensearch.sql.datasources.model.transport.CreateDataSourceActionRequest;
import org.opensearch.sql.datasources.model.transport.CreateDataSourceActionResponse;
import org.opensearch.sql.datasources.model.transport.DeleteDataSourceActionRequest;
import org.opensearch.sql.datasources.model.transport.DeleteDataSourceActionResponse;
import org.opensearch.sql.datasources.model.transport.GetDataSourceActionRequest;
import org.opensearch.sql.datasources.model.transport.GetDataSourceActionResponse;
import org.opensearch.sql.datasources.model.transport.UpdateDataSourceActionRequest;
import org.opensearch.sql.datasources.model.transport.UpdateDataSourceActionResponse;
import org.opensearch.sql.datasources.transport.TransportCreateDataSourceAction;
import org.opensearch.sql.datasources.transport.TransportDeleteDataSourceAction;
import org.opensearch.sql.datasources.transport.TransportGetDataSourceAction;
import org.opensearch.sql.datasources.transport.TransportUpdateDataSourceAction;
import org.opensearch.sql.datasources.utils.Scheduler;
import org.opensearch.sql.datasources.utils.XContentParserUtils;
import org.opensearch.sql.opensearch.response.error.ErrorMessageFactory;
import org.opensearch.sql.datasources.model.CreateDataSourceActionRequest;
import org.opensearch.sql.datasources.model.CreateDataSourceActionResponse;
import org.opensearch.sql.datasources.model.DeleteDataSourceActionRequest;
import org.opensearch.sql.datasources.model.DeleteDataSourceActionResponse;
import org.opensearch.sql.datasources.model.GetDataSourceActionRequest;
import org.opensearch.sql.datasources.model.GetDataSourceActionResponse;
import org.opensearch.sql.datasources.model.UpdateDataSourceActionRequest;
import org.opensearch.sql.datasources.model.UpdateDataSourceActionResponse;


public class RestDataSourceQueryAction extends BaseRestHandler {
Expand Down Expand Up @@ -237,7 +237,7 @@ private void handleException(Exception e, RestChannel restChannel) {
private void reportError(final RestChannel channel, final Exception e, final RestStatus status) {
channel.sendResponse(
new BytesRestResponse(
status, ErrorMessageFactory.createErrorMessage(e, status.getStatus()).toString()));
status, new ErrorMessage(e, status.getStatus()).toString()));
}

private static boolean isClientError(Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.opensearch.sql.datasource;
package org.opensearch.sql.datasources.service;

import org.opensearch.sql.datasource.model.DataSource;
import org.opensearch.sql.datasource.model.DataSourceMetadata;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.opensearch.sql.datasource;
package org.opensearch.sql.datasources.service;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
*
*/

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

import java.util.List;
import java.util.Optional;
import javax.xml.crypto.Data;
import org.opensearch.sql.datasource.model.DataSource;
import org.opensearch.sql.datasource.model.DataSourceMetadata;

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

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

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

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import javax.xml.crypto.Data;
import org.opensearch.sql.common.utils.StringUtils;
import org.opensearch.sql.datasource.exceptions.DataSourceNotFoundException;
import org.opensearch.sql.datasource.DataSourceService;
import org.opensearch.sql.datasource.model.DataSource;
import org.opensearch.sql.datasource.model.DataSourceMetadata;
import org.opensearch.sql.datasource.model.DataSourceType;
import org.opensearch.sql.datasources.auth.DataSourceUserAuthorizationHelper;
import org.opensearch.sql.datasources.exceptions.DataSourceNotFoundException;
import org.opensearch.sql.storage.DataSourceFactory;

/**
Expand Down
Loading

0 comments on commit a9111a9

Please sign in to comment.