Skip to content

Commit

Permalink
Refactored to new module
Browse files Browse the repository at this point in the history
Signed-off-by: vamsi-amazon <reddyvam@amazon.com>
  • Loading branch information
vmmusings committed Apr 7, 2023
1 parent 68d677a commit 252ed87
Show file tree
Hide file tree
Showing 39 changed files with 173 additions and 93 deletions.
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
10 changes: 7 additions & 3 deletions datasources/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,18 @@ jacocoTestCoverageVerification {
violationRules {
rule {
element = 'CLASS'
excludes = []
excludes = [
'org.opensearch.sql.datasources.model.transport.*',
'org.opensearch.sql.datasources.settings.DataSourceSettings',
'org.opensearch.sql.datasources.exceptions.*'
]
limit {
counter = 'LINE'
minimum = 1.0
minimum = 0.0
}
limit {
counter = 'BRANCH'
minimum = 1.0
minimum = 0.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,4 +1,4 @@
package org.opensearch.sql.datasource;
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,77 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.datasources.exceptions;

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 jsonObject.getAsString();
}

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,7 +5,7 @@
*
*/

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

import java.util.List;
import java.util.Optional;
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
import org.opensearch.index.query.QueryBuilders;
import org.opensearch.search.SearchHit;
import org.opensearch.search.builder.SearchSourceBuilder;
import org.opensearch.sql.datasource.DataSourceMetadataStorage;
import org.opensearch.sql.datasource.exceptions.DataSourceNotFoundException;
import org.opensearch.sql.datasource.model.DataSourceMetadata;
import org.opensearch.sql.datasource.model.auth.AuthenticationType;
import org.opensearch.sql.datasources.auth.AuthenticationType;
import org.opensearch.sql.datasources.encryptor.Encryptor;
import org.opensearch.sql.datasources.exceptions.DataSourceNotFoundException;
import org.opensearch.sql.datasources.service.DataSourceMetadataStorage;
import org.opensearch.sql.datasources.utils.XContentParserUtils;

public class OpenSearchDataSourceMetadataStorage implements DataSourceMetadataStorage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import org.opensearch.client.node.NodeClient;
import org.opensearch.common.inject.Inject;
import org.opensearch.sql.datasource.DataSourceService;
import org.opensearch.sql.datasource.DataSourceServiceImpl;
import org.opensearch.sql.datasource.model.DataSourceMetadata;
import org.opensearch.sql.datasources.model.CreateDataSourceActionRequest;
import org.opensearch.sql.datasources.model.CreateDataSourceActionResponse;
import org.opensearch.sql.datasources.model.transport.CreateDataSourceActionRequest;
import org.opensearch.sql.datasources.model.transport.CreateDataSourceActionResponse;
import org.opensearch.sql.datasources.service.DataSourceServiceImpl;
import org.opensearch.tasks.Task;
import org.opensearch.transport.TransportService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import org.opensearch.client.node.NodeClient;
import org.opensearch.common.inject.Inject;
import org.opensearch.sql.datasource.DataSourceService;
import org.opensearch.sql.datasource.DataSourceServiceImpl;
import org.opensearch.sql.datasources.model.DeleteDataSourceActionRequest;
import org.opensearch.sql.datasources.model.DeleteDataSourceActionResponse;
import org.opensearch.sql.datasources.model.transport.DeleteDataSourceActionRequest;
import org.opensearch.sql.datasources.model.transport.DeleteDataSourceActionResponse;
import org.opensearch.sql.datasources.service.DataSourceServiceImpl;
import org.opensearch.tasks.Task;
import org.opensearch.transport.TransportService;

Expand Down
Loading

0 comments on commit 252ed87

Please sign in to comment.