Skip to content

Commit

Permalink
[Spotless] Applying Google Code Format for datasource files #6 (opens…
Browse files Browse the repository at this point in the history
…earch-project#1939)

* Spotless apply for datasources.

Signed-off-by: Mitchell Gale <Mitchell.Gale@improving.com>

* ignore checkstyle for datasources

Signed-off-by: Mitchell Gale <Mitchell.Gale@improving.com>

---------

Signed-off-by: Mitchell Gale <Mitchell.Gale@improving.com>
Signed-off-by: Mitchell Gale <Mitchell.gale@improving.com>
Signed-off-by: Mitchell Gale <Mitchell.Gale@improving.com>
  • Loading branch information
MitchellGale committed Aug 14, 2023
1 parent 4e4b8be commit 99c2375
Show file tree
Hide file tree
Showing 40 changed files with 813 additions and 819 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ repositories {
spotless {
java {
target fileTree('.') {
include 'core/**/*.java'
include 'datasources/**/*.java',
'core/**/*.java'
exclude '**/build/**', '**/build-*/**'
}
importOrder()
Expand Down
3 changes: 3 additions & 0 deletions datasources/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

checkstyleTest.ignoreFailures = true
checkstyleMain.ignoreFailures = true

test {
useJUnitPlatform()
testLogging {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import java.util.Map;

public enum AuthenticationType {

BASICAUTH("basicauth"), AWSSIGV4AUTH("awssigv4");
BASICAUTH("basicauth"),
AWSSIGV4AUTH("awssigv4");

private String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
import org.opensearch.sql.datasource.model.DataSourceMetadata;

/**
* Interface for datasource authorization helper.
* The implementation of this class helps in determining
* if authorization is required and the roles associated with the user.
* Interface for datasource authorization helper. The implementation of this class helps in
* determining if authorization is required and the roles associated with the user.
*/
public interface DataSourceUserAuthorizationHelper {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,39 @@ public class DataSourceUserAuthorizationHelperImpl implements DataSourceUserAuth
private final Client client;

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

private List<String> getUserRoles() {
String userString = client.threadPool()
.getThreadContext().getTransient(
ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT);
String userString =
client
.threadPool()
.getThreadContext()
.getTransient(ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT);
User user = User.parse(userString);
return user.getRoles();
}


@Override
public void authorizeDataSource(DataSourceMetadata dataSourceMetadata) {
if (isAuthorizationRequired()
&& !dataSourceMetadata.getName().equals(DEFAULT_DATASOURCE_NAME)) {
boolean isAuthorized = false;
for (String role : getUserRoles()) {
if (dataSourceMetadata.getAllowedRoles().contains(role)
|| role.equals("all_access")) {
if (dataSourceMetadata.getAllowedRoles().contains(role) || role.equals("all_access")) {
isAuthorized = true;
break;
}
}
if (!isAuthorized) {
throw new SecurityException(
String.format("User is not authorized to access datasource %s. "
String.format(
"User is not authorized to access datasource %s. "
+ "User should be mapped to any of the roles in %s for access.",
dataSourceMetadata.getName(), dataSourceMetadata.getAllowedRoles().toString()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ public interface Encryptor {
* @return String plainText.
*/
String decrypt(String encryptedText);

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,40 @@ public class EncryptorImpl implements Encryptor {
@Override
public String encrypt(String plainText) {
validate(masterKey);
final AwsCrypto crypto = AwsCrypto.builder()
.withCommitmentPolicy(CommitmentPolicy.RequireEncryptRequireDecrypt)
.build();
final AwsCrypto crypto =
AwsCrypto.builder()
.withCommitmentPolicy(CommitmentPolicy.RequireEncryptRequireDecrypt)
.build();

JceMasterKey jceMasterKey
= JceMasterKey.getInstance(new SecretKeySpec(masterKey.getBytes(), "AES"), "Custom",
"opensearch.config.master.key", "AES/GCM/NoPadding");
JceMasterKey jceMasterKey =
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));
final CryptoResult<byte[], JceMasterKey> encryptResult =
crypto.encryptData(jceMasterKey, plainText.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptResult.getResult());
}

@Override
public String decrypt(String encryptedText) {
validate(masterKey);
final AwsCrypto crypto = AwsCrypto.builder()
.withCommitmentPolicy(CommitmentPolicy.RequireEncryptRequireDecrypt)
.build();
final AwsCrypto crypto =
AwsCrypto.builder()
.withCommitmentPolicy(CommitmentPolicy.RequireEncryptRequireDecrypt)
.build();

JceMasterKey jceMasterKey
= JceMasterKey.getInstance(new SecretKeySpec(masterKey.getBytes(), "AES"), "Custom",
"opensearch.config.master.key", "AES/GCM/NoPadding");
JceMasterKey jceMasterKey =
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));
final CryptoResult<byte[], JceMasterKey> decryptedResult =
crypto.decryptData(jceMasterKey, Base64.getDecoder().decode(encryptedText));
return new String(decryptedResult.getResult());
}

Expand All @@ -65,6 +73,4 @@ private void validate(String masterKey) {
+ "admin/datasources.rst#master-key-config-for-encrypting-credential-information");
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@

package org.opensearch.sql.datasources.exceptions;

/**
* DataSourceNotFoundException.
*/
/** DataSourceNotFoundException. */
public class DataSourceNotFoundException extends RuntimeException {
public DataSourceNotFoundException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.datasources.exceptions;

import com.google.gson.Gson;
Expand All @@ -12,27 +11,20 @@
import lombok.Getter;
import org.opensearch.core.rest.RestStatus;

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

protected Throwable exception;

private final int status;

@Getter
private final String type;
@Getter private final String type;

@Getter
private final String reason;
@Getter private final String reason;

@Getter
private final String details;
@Getter private final String details;

/**
* Error Message Constructor.
*/
/** Error Message Constructor. */
public ErrorMessage(Throwable exception, int status) {
this.exception = exception;
this.status = status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

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


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

import java.io.IOException;
Expand All @@ -17,15 +16,11 @@
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.sql.datasource.model.DataSourceMetadata;

public class CreateDataSourceActionRequest
extends ActionRequest {
public class CreateDataSourceActionRequest extends ActionRequest {

@Getter
private DataSourceMetadata dataSourceMetadata;
@Getter private DataSourceMetadata dataSourceMetadata;

/**
* Constructor of CreateDataSourceActionRequest from StreamInput.
*/
/** Constructor of CreateDataSourceActionRequest from StreamInput. */
public CreateDataSourceActionRequest(StreamInput in) throws IOException {
super(in);
}
Expand All @@ -38,9 +33,8 @@ public CreateDataSourceActionRequest(DataSourceMetadata dataSourceMetadata) {
public ActionRequestValidationException validate() {
if (this.dataSourceMetadata.getName().equals(DEFAULT_DATASOURCE_NAME)) {
ActionRequestValidationException exception = new ActionRequestValidationException();
exception
.addValidationError(
"Not allowed to create datasource with name : " + DEFAULT_DATASOURCE_NAME);
exception.addValidationError(
"Not allowed to create datasource with name : " + DEFAULT_DATASOURCE_NAME);
return exception;
} else {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
import org.opensearch.core.common.io.stream.StreamOutput;

@RequiredArgsConstructor
public class CreateDataSourceActionResponse
extends ActionResponse {
public class CreateDataSourceActionResponse extends ActionResponse {

@Getter
private final String result;
@Getter private final String result;

public CreateDataSourceActionResponse(StreamInput in) throws IOException {
super(in);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

public class DeleteDataSourceActionRequest extends ActionRequest {

@Getter
private String dataSourceName;
@Getter private String dataSourceName;

/** Constructor of DeleteDataSourceActionRequest from StreamInput. */
public DeleteDataSourceActionRequest(StreamInput in) throws IOException {
Expand All @@ -34,18 +33,15 @@ public DeleteDataSourceActionRequest(String dataSourceName) {
public ActionRequestValidationException validate() {
if (StringUtils.isEmpty(this.dataSourceName)) {
ActionRequestValidationException exception = new ActionRequestValidationException();
exception
.addValidationError("Datasource Name cannot be empty or null");
exception.addValidationError("Datasource Name cannot be empty or null");
return exception;
} else if (this.dataSourceName.equals(DEFAULT_DATASOURCE_NAME)) {
ActionRequestValidationException exception = new ActionRequestValidationException();
exception
.addValidationError(
"Not allowed to delete datasource with name : " + DEFAULT_DATASOURCE_NAME);
exception.addValidationError(
"Not allowed to delete datasource with name : " + DEFAULT_DATASOURCE_NAME);
return exception;
} else {
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
@RequiredArgsConstructor
public class DeleteDataSourceActionResponse extends ActionResponse {

@Getter
private final String result;
@Getter private final String result;

public DeleteDataSourceActionResponse(StreamInput in) throws IOException {
super(in);
Expand All @@ -29,5 +28,4 @@ public DeleteDataSourceActionResponse(StreamInput in) throws IOException {
public void writeTo(StreamOutput streamOutput) throws IOException {
streamOutput.writeString(result);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@
@NoArgsConstructor
public class GetDataSourceActionRequest extends ActionRequest {

@Getter
private String dataSourceName;
@Getter private String dataSourceName;

/**
* Constructor of GetDataSourceActionRequest from StreamInput.
*/
/** Constructor of GetDataSourceActionRequest from StreamInput. */
public GetDataSourceActionRequest(StreamInput in) throws IOException {
super(in);
}
Expand All @@ -37,13 +34,11 @@ public GetDataSourceActionRequest(String dataSourceName) {
public ActionRequestValidationException validate() {
if (this.dataSourceName != null && this.dataSourceName.equals(DEFAULT_DATASOURCE_NAME)) {
ActionRequestValidationException exception = new ActionRequestValidationException();
exception
.addValidationError(
"Not allowed to fetch datasource with name : " + DEFAULT_DATASOURCE_NAME);
exception.addValidationError(
"Not allowed to fetch datasource with name : " + DEFAULT_DATASOURCE_NAME);
return exception;
} else {
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
@RequiredArgsConstructor
public class GetDataSourceActionResponse extends ActionResponse {

@Getter
private final String result;
@Getter private final String result;

public GetDataSourceActionResponse(StreamInput in) throws IOException {
super(in);
Expand All @@ -29,5 +28,4 @@ public GetDataSourceActionResponse(StreamInput in) throws IOException {
public void writeTo(StreamOutput streamOutput) throws IOException {
streamOutput.writeString(result);
}

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

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


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

import java.io.IOException;
Expand All @@ -17,11 +16,9 @@
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.sql.datasource.model.DataSourceMetadata;

public class UpdateDataSourceActionRequest
extends ActionRequest {
public class UpdateDataSourceActionRequest extends ActionRequest {

@Getter
private DataSourceMetadata dataSourceMetadata;
@Getter private DataSourceMetadata dataSourceMetadata;

/** Constructor of UpdateDataSourceActionRequest from StreamInput. */
public UpdateDataSourceActionRequest(StreamInput in) throws IOException {
Expand All @@ -36,9 +33,8 @@ public UpdateDataSourceActionRequest(DataSourceMetadata dataSourceMetadata) {
public ActionRequestValidationException validate() {
if (this.dataSourceMetadata.getName().equals(DEFAULT_DATASOURCE_NAME)) {
ActionRequestValidationException exception = new ActionRequestValidationException();
exception
.addValidationError(
"Not allowed to update datasource with name : " + DEFAULT_DATASOURCE_NAME);
exception.addValidationError(
"Not allowed to update datasource with name : " + DEFAULT_DATASOURCE_NAME);
return exception;
} else {
return null;
Expand Down
Loading

0 comments on commit 99c2375

Please sign in to comment.