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

Dockerize application using vault token #27

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ RUN mvn -f /home/app/pom.xml clean package -DskipTests
FROM openjdk:11-jre-slim
COPY --from=build /home/app/target/rabbitmq-scheduler-0.0.1-SNAPSHOT.jar /usr/local/lib/rabbitmq-scheduler-0.0.1-SNAPSHOT.jar
EXPOSE 8083

ENV VAULT_TOKEN="000000000000000"

ENTRYPOINT ["java","-jar","/usr/local/lib/rabbitmq-scheduler-0.0.1-SNAPSHOT.jar"]

5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-vault-config</artifactId>
</dependency>

<dependency>
<groupId>com.box</groupId>
<artifactId>box-java-sdk</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rabbitMq.rabbitmqscheduler.Controller;

import com.rabbitMq.rabbitmqscheduler.DTO.StopRequest;
import com.rabbitMq.rabbitmqscheduler.DTO.TransferJobResponse;
import com.rabbitMq.rabbitmqscheduler.DTO.transferFromODS.RequestFromODS;
import com.rabbitMq.rabbitmqscheduler.DTO.TransferJobRequest;
Expand Down Expand Up @@ -30,4 +31,11 @@ public TransferJobResponse receiveRequest(@RequestBody RequestFromODS odsTransfe
response.setMessage("Job Submitted");
return response;
}
@PostMapping(value = "/stopJob")
public Boolean stopJob(@RequestBody StopRequest stopRequest) {
logger.info("Received request to stop job with id: " + stopRequest.toString());
messageSender.sendStopJobRequest(stopRequest);
return true;
}

}
10 changes: 10 additions & 0 deletions src/main/java/com/rabbitMq/rabbitmqscheduler/DTO/StopRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.rabbitMq.rabbitmqscheduler.DTO;

import lombok.Data;

@Data
public class StopRequest {
Long jobId;
String transferNodeName;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.rabbitMq.rabbitmqscheduler.DTO;

public class StopResponses {
private boolean success;
private String errorMessage;

public StopResponses(boolean success, String errorMessage) {
this.success = success;
this.errorMessage = errorMessage;
}

public boolean isSuccess() {
return success;
}

public String getErrorMessage() {
return errorMessage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ public static void main(String[] args) {
SpringApplication.run(RabbitmqSchedulerApplication.class, args);
}

}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.rabbitMq.rabbitmqscheduler.Sender;

import com.rabbitMq.rabbitmqscheduler.DTO.StopRequest;
import com.rabbitMq.rabbitmqscheduler.DTO.StopResponses;
import com.rabbitMq.rabbitmqscheduler.DTO.TransferJobRequest;
import com.rabbitMq.rabbitmqscheduler.DTO.TransferParams;
import com.rabbitMq.rabbitmqscheduler.DTO.transferFromODS.RequestFromODS;
import com.rabbitMq.rabbitmqscheduler.Enums.EndPointType;
import lombok.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -54,6 +57,27 @@ public void sendTransferRequest(TransferJobRequest odsTransferRequest, RequestFr
}
logger.info("Processed Job: {}", odsTransferRequest);
}
public StopResponses sendStopJobRequest(StopRequest stopRequest) {
String errorMessage = null;
boolean success = true;

if (stopRequest.getTransferNodeName() == null || stopRequest.getTransferNodeName().isEmpty()) {
errorMessage = "Failed to send stop job request. Transfer node name" + stopRequest.getTransferNodeName() + " is not provided.";
success = false;
} else {
try {
rmqTemplate.convertAndSend(exchange, stopRequest.getTransferNodeName(), stopRequest.getJobId());
} catch (AmqpException e) {
errorMessage = "Error occurred while sending stop job request: " + e.getMessage();
success = false;
}
}

return new StopResponses(success, errorMessage);


}


/**
* The Transfer params to send using the routingKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
@Service
public class GDriveExpander extends DestinationChunkSize implements FileExpander{

Logger logger = LoggerFactory.getLogger(GDriveExpander.class);
@Value("${gdrive.client.id}")
private String gDriveClientId;

Expand All @@ -35,6 +36,7 @@ public class GDriveExpander extends DestinationChunkSize implements FileExpander
@Value("${gdrive.appname}")
private String gdriveAppName;

private final String REQUEST_FILE_FIELDS = "id, name, size, mimeType, modifiedTime, md5Checksum, trashed, parents";
private Drive client;

@Override
Expand All @@ -59,54 +61,76 @@ public void createClient(EndpointCredential credential) {
public List<EntityInfo> expandedFileSystem(List<EntityInfo> userSelectedResources, String basePath) {
Stack<File> fileListStack = new Stack<>();
List<EntityInfo> fileInfoList = new ArrayList<>();
if(userSelectedResources.isEmpty()){
googleDriveLister(fileListStack, fileInfoList, "", "");
}else{
for(EntityInfo fileInfo : userSelectedResources){
String fileQuery = "'" + fileInfo.getId() + "' in parents and trashed=false";
googleDriveLister(fileListStack, fileInfoList, fileQuery, fileInfo.getId());
}
EntityInfo info = getMetadataForfile(fileInfo.getId());
if(info==null){
fileInfoList.addAll(googleDriveLister(fileListStack, fileQuery, fileInfo.getId()));
} else{
fileInfoList.add(info);
}
}

while(!fileListStack.isEmpty()){
File file = fileListStack.pop();
String fileQuery = "'" + file.getId() + "' in parents and mimeType != 'application/vnd.google-apps.folder' and trashed=false";
googleDriveLister(fileListStack, fileInfoList, fileQuery, file.getId());
googleDriveLister(fileListStack, fileQuery, file.getId());
}
return fileInfoList;
}

private void googleDriveLister(Stack<File> fileListStack, List<EntityInfo> fileInfoList, String fileQuery, String parentFolderId) {
private List<EntityInfo> googleDriveLister(Stack<File> fileListStack, String fileQuery, String folderId) {
FileList fileList;
String pageToken = "";

List<EntityInfo> folderFileList = new ArrayList<>();
try {
do {
fileList = this.client.files().list()
.setQ(fileQuery)
.setFields("nextPageToken, files(id, name, parents, size, mimeType)")
.setPageToken(pageToken)
.execute();

for(File file : fileList.getFiles()){
if(file.getId().equals(parentFolderId)){
if(file.getId().equals(folderId)){//the folder that u query appears in the result
continue;
}
if(file.getMimeType().equals("application/vnd.google-apps.folder")){
fileListStack.add(file);
}else{
fileInfoList.add(googleFileToEntityInfo(file));
folderFileList.add(getMetadataForfile(file.getId()));
}
}
pageToken = fileList.getNextPageToken();
} while (pageToken != null);
} catch (IOException e) {
logger.error("Error listing files from Google drive",e);
}
return folderFileList;
}

public EntityInfo getMetadataForfile(String fileId){
try {
File file = this.client.files().get(fileId)
.setFields(REQUEST_FILE_FIELDS)
.execute();
return googleFileToEntityInfo(file);
} catch(IOException e){
e.printStackTrace();
}
return null;
}

private EntityInfo googleFileToEntityInfo(File googleFile){
if(googleFile.getId() == null || googleFile.getParents() == null || googleFile.getSize() == null || googleFile.getMd5Checksum() == null){
return null;
}
EntityInfo entityInfo = new EntityInfo();
entityInfo.setId(googleFile.getId());
entityInfo.setSize(googleFile.getSize());
entityInfo.setPath(String.valueOf(googleFile.getParents()));
entityInfo.setChecksum(googleFile.getMd5Checksum());
return entityInfo;
}
}
9 changes: 9 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
spring.application.name=ODSTransferSchedulerService
server.port=8061

#Vault config
spring.cloud.vault.host = host.docker.internal
spring.cloud.vault.port = 8200
spring.cloud.vault.scheme = http
spring.cloud.vault.authentication = TOKEN
spring.cloud.vault.token = ${VAULT_TOKEN}
spring.cloud.vault.kv.enabled = true
spring.config.import = vault://onedatashare/transfer_scheduler

#Eureka config
eureka.client.enabled=true
eureka.client.serviceUrl.defaultZone= http://${EUREKA_USER:admin}:${EUREKA_PASS:admin}@${EUREKA_URI:localhost:8090}/eureka
Expand Down