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

PDS prepare upload remote data to shared storage #3131

Merged
merged 29 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c98952d
Added upload classes and archive support for prepare #3026
lorriborri May 14, 2024
6e5f747
moved git download into folder #3026
lorriborri May 15, 2024
8b6c45a
Added checksum for upload files
lorriborri May 16, 2024
74f6c0c
Added test for UploadService #3026
lorriborri May 21, 2024
00a4ff3
Added unit tests for upload #3026
lorriborri May 22, 2024
e8ea8e6
Refactored PDSStorageConstants #3026
lorriborri May 24, 2024
4d2082f
Merge branch 'develop' into feature-3026-pds-prepare-upload-service
lorriborri May 24, 2024
43a6c11
Added change requests #3026
lorriborri May 24, 2024
d2b7d1c
Added change requests #3026
lorriborri May 27, 2024
e1eb1e2
Moved LogSanitizer in pds-commons-core #3026
lorriborri May 27, 2024
75ac56e
Added UploadException and LogSanitazier #3026
lorriborri May 28, 2024
55187d0
Added UsageException for wrong configurations and download fails #3026
lorriborri May 28, 2024
51e2555
Initial integration test #3026
lorriborri May 29, 2024
26da4f6
Added PDS_SORAGE environment variables to be adde to the envrionment …
lorriborri May 29, 2024
6054e12
Added integrationtest #3026
lorriborri May 31, 2024
129e07c
Added change requests #3026
lorriborri May 31, 2024
86034e2
Merge branch 'develop' into feature-3026-pds-prepare-upload-service
lorriborri May 31, 2024
c0d27f6
Removed unnecessary ENV #3026
lorriborri May 31, 2024
8561617
Refactoring + changes #3026
de-jcup Jun 3, 2024
ba0f3ab
Reduced log output and speed up integration tests #3197, #3198
de-jcup Jun 7, 2024
2dd0cb7
Fixed storage problem and enhanced integration tests #3026
de-jcup Jun 7, 2024
083b221
Refactoring and bugfixes #3026
de-jcup Jun 10, 2024
6b535c4
Skopeo call changes #3026
de-jcup Jun 10, 2024
63ce627
Changed directory cleanup from process to plain java #3026
de-jcup Jun 10, 2024
30e7e64
Changed git prepare module cleanup behavior and documentatino #3026
de-jcup Jun 11, 2024
a4ac055
Merge branch 'develop' into feature-3026-pds-prepare-upload-service
de-jcup Jun 11, 2024
87d2b9c
Review changes #3026
de-jcup Jun 12, 2024
2e87924
Merge branch 'develop' into feature-3026-pds-prepare-upload-service
de-jcup Jun 13, 2024
a1b1809
Review changes #3026
de-jcup Jun 13, 2024
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
6 changes: 6 additions & 0 deletions sechub-wrapper-prepare/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ dependencies {
implementation project(':sechub-commons-model')
implementation project(':sechub-commons-core')
implementation project(':sechub-commons-pds')
implementation project(':sechub-commons-archive')
implementation project(':sechub-adapter')
implementation project(':sechub-storage-core')
implementation project(':sechub-storage-sharedvolume-spring')
implementation project(':sechub-storage-s3-aws')

implementation spring_boot_dependency.logback_classic

implementation library.jgit_core
implementation project(path: ':sechub-pds')
lorriborri marked this conversation as resolved.
Show resolved Hide resolved


/* test */
testImplementation project(':sechub-testframework')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import com.mercedesbenz.sechub.commons.model.SecHubMessage;
import com.mercedesbenz.sechub.commons.model.SecHubMessageType;
import com.mercedesbenz.sechub.wrapper.prepare.prepare.PrepareWrapperPreparationService;
import com.mercedesbenz.sechub.wrapper.prepare.prepare.PrepareWrapperStorageService;
import com.mercedesbenz.sechub.wrapper.prepare.prepare.PrepareWrapperResultStorageService;

@Component
public class PrepareWrapperCLI implements CommandLineRunner {
Expand All @@ -27,7 +27,7 @@ public class PrepareWrapperCLI implements CommandLineRunner {
PrepareWrapperPreparationService preparationService;

@Autowired
PrepareWrapperStorageService storageService;
PrepareWrapperResultStorageService storageService;

@Override
public void run(String... args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public class PrepareWrapperEnvironment {
@Value("${" + RT_KEY_PDS_JOB_USER_MESSAGES_FOLDER + "}")
private String pdsUserMessagesFolder;

@Value("${" + PARAM_KEY_PDS_CONFIG_SECHUB_STORAGE_PATH + "}")
private String sechubStoragePath;

@Value("${" + PARAM_KEY_SECHUB_JOB_UUID + "}")
private String sechubJobUUID;

/*********************************/
/* PDS prepare environment setup */
/*********************************/
Expand All @@ -47,4 +53,11 @@ public String getPdsPrepareUploadFolderDirectory() {
return pdsPrepareUploadFolderDirectory;
}

public String getSechubStoragePath() {
return sechubStoragePath;
}

public String getSechubJobUUID() {
return sechubJobUUID;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.springframework.stereotype.Component;

import com.mercedesbenz.sechub.commons.TextFileWriter;
import com.mercedesbenz.sechub.commons.archive.ArchiveSupport;
import com.mercedesbenz.sechub.commons.core.security.CheckSumSupport;
import com.mercedesbenz.sechub.commons.pds.PDSProcessAdapterFactory;

@Component
Expand All @@ -20,4 +22,14 @@ PDSProcessAdapterFactory createPDSProcessAdapterFactory() {
return new PDSProcessAdapterFactory();
}

@Bean
ArchiveSupport createArchiveSupport() {
return new ArchiveSupport();
}

@Bean
CheckSumSupport createCheckSumSupport() {
return new CheckSumSupport();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,37 @@
public class GitContext extends ToolContext {
private boolean cloneWithoutHistory;

private String filename;

private GitContext(GitContextBuilder builder) {
super(builder);
this.cloneWithoutHistory = builder.cloneWithoutHistory;
this.filename = builder.filename;
}

public boolean isCloneWithoutHistory() {
return cloneWithoutHistory;
}

public String getFilename() {
return filename;
lorriborri marked this conversation as resolved.
Show resolved Hide resolved
}

public static class GitContextBuilder extends ToolContextBuilder {
private boolean cloneWithoutHistory = true;

private String filename = "GitDownloadDirectory";
lorriborri marked this conversation as resolved.
Show resolved Hide resolved

public GitContextBuilder setCloneWithoutHistory(boolean cloneWithoutHistory) {
this.cloneWithoutHistory = cloneWithoutHistory;
return this;
}

public GitContextBuilder setFilename(String filename) {
this.filename = filename;
return this;
}

@Override
public GitContext build() {
return new GitContext(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@ public class JGitAdapter {

public void clone(GitContext gitContext) {
String location = transformLocationToURL(gitContext.getLocation());
String uploadDirectory = gitContext.getUploadDirectory() + "/" + gitContext.getFilename();
Map<String, SealedObject> credentialMap = gitContext.getCredentialMap();

String username = getUserNameFromMap(credentialMap);
String password = getPasswordFromMap(credentialMap);

CloneCommand command = Git.cloneRepository().setURI(location).setDirectory(Paths.get(gitContext.getUploadDirectory()).toFile());
CloneCommand command = Git.cloneRepository().setURI(location).setDirectory(Paths.get(uploadDirectory).toFile());
lorriborri marked this conversation as resolved.
Show resolved Hide resolved

if (username != null && password != null) {
LOG.debug("Cloning private repository: " + location + " with username and password to: " + gitContext.getUploadDirectory());
LOG.debug("Cloning private repository: " + location + " with username and password to: " + uploadDirectory);
lorriborri marked this conversation as resolved.
Show resolved Hide resolved
command = command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
} else {
LOG.debug("Cloning public repository: " + location + " to: " + gitContext.getUploadDirectory());
LOG.debug("Cloning public repository: " + location + " to: " + uploadDirectory);
lorriborri marked this conversation as resolved.
Show resolved Hide resolved
}

if (gitContext.isCloneWithoutHistory()) {
Expand All @@ -47,7 +48,7 @@ public void clone(GitContext gitContext) {

try (Git git = command.call()) {
} catch (GitAPIException e) {
throw new RuntimeException("Error while cloning from repository: " + location + " with " + username + " " + password, e);
throw new RuntimeException("Error while cloning from repository: " + location, e);
lorriborri marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.mercedesbenz.sechub.commons.core.security.CryptoAccess;
import com.mercedesbenz.sechub.commons.model.*;
import com.mercedesbenz.sechub.wrapper.prepare.prepare.PrepareWrapperContext;
import com.mercedesbenz.sechub.wrapper.prepare.upload.FileNameSupport;
import com.mercedesbenz.sechub.wrapper.prepare.upload.PrepareWrapperUploadService;

@Service
public class PrepareWrapperModuleGit implements PrepareWrapperModule {
Expand All @@ -41,6 +43,12 @@ public class PrepareWrapperModuleGit implements PrepareWrapperModule {
@Autowired
GitInputValidator gitInputValidator;

@Autowired
PrepareWrapperUploadService uploadService;

@Autowired
FileNameSupport filesSupport;

public boolean isAbleToPrepare(PrepareWrapperContext context) {

if (!pdsPrepareModuleGitEnabled) {
Expand Down Expand Up @@ -86,6 +94,8 @@ public void prepare(PrepareWrapperContext context) throws IOException {
throw new IOException("Download of git repository was not successful.");
}
cleanup(context);
// TODO: 15.05.24 laura proper upload
lorriborri marked this conversation as resolved.
Show resolved Hide resolved
uploadService.upload(context);
}

boolean isMatchingGitType(String type) {
Expand All @@ -97,11 +107,15 @@ boolean isMatchingGitType(String type) {

boolean isDownloadSuccessful(PrepareWrapperContext context) {
// check if download folder contains git
Path path = Paths.get(context.getEnvironment().getPdsPrepareUploadFolderDirectory());
if (Files.isDirectory(path)) {
String gitFile = ".git";
Path gitPath = Paths.get(path + "/" + gitFile);
return Files.exists(gitPath);
String uploadFolder = context.getEnvironment().getPdsPrepareUploadFolderDirectory();
lorriborri marked this conversation as resolved.
Show resolved Hide resolved
if (Files.isDirectory(Path.of(uploadFolder))) {
String gitRepo = filesSupport.getSubfolderFileNameFromDirectory(uploadFolder);
Path path = Paths.get(uploadFolder + "/" + gitRepo).toAbsolutePath();
if (Files.isDirectory(path)) {
String gitFile = ".git";
Path gitPath = Paths.get(path + "/" + gitFile);
return Files.exists(gitPath);
}
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.mercedesbenz.sechub.wrapper.prepare.cli.PrepareWrapperEnvironment;

@Service
public class PrepareWrapperStorageService {
public class PrepareWrapperResultStorageService {

@Autowired
PrepareWrapperEnvironment environment;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.mercedesbenz.sechub.wrapper.prepare.upload;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.stereotype.Component;

@Component
public class FileNameSupport {
public String getTarFileNameFromDirectory(String folder) {
lorriborri marked this conversation as resolved.
Show resolved Hide resolved
lorriborri marked this conversation as resolved.
Show resolved Hide resolved
// TODO: 22.05.24 laura we will use this method in skopeo modul for download
lorriborri marked this conversation as resolved.
Show resolved Hide resolved
// check
lorriborri marked this conversation as resolved.
Show resolved Hide resolved
Path path = Path.of(folder);
if (Files.isDirectory(path)) {
try (Stream<Path> walk = Files.walk(path)) {
List<String> result = walk.filter(p -> !Files.isDirectory(p)) // not a directory
.map(p -> p.toString().toLowerCase()) // convert path to string
lorriborri marked this conversation as resolved.
Show resolved Hide resolved
.filter(f -> f.endsWith(".tar")) // check end with
.toList(); // collect all matched to a List
if (result.size() == 1) {
return result.get(0);
} else {
throw new RuntimeException("Error while try to find .tar file.");
}
} catch (IOException e) {
throw new RuntimeException("Error while try to find .tar file.", e);
}
} else {
throw new IllegalArgumentException("Parameter " + folder + " is not a directory");
lorriborri marked this conversation as resolved.
Show resolved Hide resolved
}
}

public String getSubfolderFileNameFromDirectory(String folder) {
Set<String> files = listFilesUsingJavaIO(new File(folder).getAbsolutePath());
lorriborri marked this conversation as resolved.
Show resolved Hide resolved
if (files.size() == 1) {
return files.iterator().next();
} else if (files.isEmpty()) {
throw new IllegalArgumentException("Download directory is empty: " + folder);
} else {
throw new IllegalArgumentException("Download directory contains more than one subfolder: " + folder);
}
}

private Set<String> listFilesUsingJavaIO(String dir) {
lorriborri marked this conversation as resolved.
Show resolved Hide resolved
lorriborri marked this conversation as resolved.
Show resolved Hide resolved
/* @formatter:off */
lorriborri marked this conversation as resolved.
Show resolved Hide resolved
return Stream.of(Objects.requireNonNull(new File(dir).listFiles())).
lorriborri marked this conversation as resolved.
Show resolved Hide resolved
filter(File::isDirectory).
map(File::getName).
collect(Collectors.toSet());
/* @formatter:on */
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.mercedesbenz.sechub.wrapper.prepare.upload;

import java.io.IOException;
import java.nio.file.Path;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mercedesbenz.sechub.commons.archive.ArchiveSupport;
import com.mercedesbenz.sechub.commons.model.SecHubConfigurationModel;
import com.mercedesbenz.sechub.wrapper.prepare.prepare.PrepareWrapperContext;

@Service
public class PrepareWrapperArchiveCreator {

@Autowired
ArchiveSupport archiveSupport;

@Autowired
PrepareWrapperSechubConfigurationSupport sechubConfigurationSupport;

public void create(PrepareWrapperContext context) throws IOException {
// replace remote with filesystem entry in configuration model
lorriborri marked this conversation as resolved.
Show resolved Hide resolved
SecHubConfigurationModel model = sechubConfigurationSupport.replaceRemoteDataWithFilesystem(context);
String uploadDirectory = context.getEnvironment().getPdsPrepareUploadFolderDirectory();
lorriborri marked this conversation as resolved.
Show resolved Hide resolved

final Path workingDirectory = Path.of(uploadDirectory);
lorriborri marked this conversation as resolved.
Show resolved Hide resolved
archiveSupport.createArchives(model, workingDirectory, workingDirectory);
}

}
Loading
Loading