Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Spotless] Applying Google Code Format for datasource files #6 #325

Merged
merged 2 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ repositories {
spotless {
java {
target fileTree('.') {
include '**/*.java'
include 'datasources/**/*.java'
exclude '**/build/**', '**/build-*/**'
}
// importOrder()
Expand All @@ -95,7 +95,7 @@ spotless {
removeUnusedImports()
trimTrailingWhitespace()
endWithNewline()
// googleJavaFormat('1.17.0').reflowLongStrings().groupArtifact('com.google.googlejavaformat:google-java-format')
googleJavaFormat('1.17.0').reflowLongStrings().groupArtifact('com.google.googlejavaformat:google-java-format')
}
}

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 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(

Check warning on line 36 in datasources/src/main/java/org/opensearch/sql/datasources/model/transport/CreateDataSourceActionRequest.java

View check run for this annotation

Codecov / codecov/patch

datasources/src/main/java/org/opensearch/sql/datasources/model/transport/CreateDataSourceActionRequest.java#L36

Added line #L36 was not covered by tests
"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 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");

Check warning on line 36 in datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DeleteDataSourceActionRequest.java

View check run for this annotation

Codecov / codecov/patch

datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DeleteDataSourceActionRequest.java#L36

Added line #L36 was not covered by tests
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(

Check warning on line 40 in datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DeleteDataSourceActionRequest.java

View check run for this annotation

Codecov / codecov/patch

datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DeleteDataSourceActionRequest.java#L40

Added line #L40 was not covered by tests
"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 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(

Check warning on line 37 in datasources/src/main/java/org/opensearch/sql/datasources/model/transport/GetDataSourceActionRequest.java

View check run for this annotation

Codecov / codecov/patch

datasources/src/main/java/org/opensearch/sql/datasources/model/transport/GetDataSourceActionRequest.java#L37

Added line #L37 was not covered by tests
"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);
}

}
Loading
Loading