Skip to content

Commit

Permalink
Added change requests #3026
Browse files Browse the repository at this point in the history
  • Loading branch information
lorriborri committed May 24, 2024
1 parent 4d2082f commit 311dbee
Show file tree
Hide file tree
Showing 27 changed files with 582 additions and 478 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import static com.mercedesbenz.sechub.commons.pds.PDSDefaultParameterKeyConstants.*;
import static com.mercedesbenz.sechub.commons.pds.PDSDefaultRuntimeKeyConstants.*;
import static com.mercedesbenz.sechub.wrapper.prepare.cli.PrepareWrapperKeyConstants.*;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -34,8 +33,8 @@ public class PrepareWrapperEnvironment {
/* PDS prepare environment setup */
/*********************************/

@Value("${" + KEY_PDS_PREPARE_UPLOAD_DIRECTORY + "}")
private String pdsPrepareUploadFolderDirectory;
@Value("${" + RT_KEY_PDS_JOB_WORKSPACE_LOCATION + "}")
private String pdsJobWorkspaceLocation;

public String getSechubConfigurationModelAsJson() {
return sechubConfigurationModelAsJson;
Expand All @@ -49,8 +48,8 @@ public String getPdsUserMessagesFolder() {
return pdsUserMessagesFolder;
}

public String getPdsPrepareUploadFolderDirectory() {
return pdsPrepareUploadFolderDirectory;
public String getPdsJobWorkspaceLocation() {
return pdsJobWorkspaceLocation;
}

public String getSechubStoragePath() {
Expand All @@ -60,4 +59,5 @@ public String getSechubStoragePath() {
public String getSechubJobUUID() {
return sechubJobUUID;
}

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

public class PrepareWrapperKeyConstants {

/**
* Folder for PDS prepare where remote data gets downloaded to be uploaded to
* the shared storage
*/
// TODO: 23.05.24 laura diese variable weg -> ersetzen durch WORKSPACE_DIRECTORY
// + xxx
public static final String KEY_PDS_PREPARE_UPLOAD_DIRECTORY = "pds.prepare.upload.directory";

/**
* Prepare process timeout in seconds for prepare processes started with process
* builder, default is -1 prepare process timeout can not be higher than the pds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import static com.mercedesbenz.sechub.wrapper.prepare.cli.PrepareWrapperEnvironmentVariables.PDS_PREPARE_CREDENTIAL_USERNAME;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;

import javax.crypto.SealedObject;
Expand All @@ -26,4 +28,15 @@ default void addSealedUserCredentials(SecHubRemoteCredentialUserData user, HashM
credentialMap.put(PDS_PREPARE_CREDENTIAL_USERNAME, sealedUsername);
credentialMap.put(PDS_PREPARE_CREDENTIAL_PASSWORD, sealedPassword);
}

default void createDownloadDirectory(Path path) {
if (Files.exists(path)) {
return;
}
try {
Files.createDirectories(path);
} catch (IOException e) {
throw new RuntimeException("Error while creating download directory: " + path.toString(), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,62 +1,52 @@
package com.mercedesbenz.sechub.wrapper.prepare.modules;

import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.SealedObject;

public abstract class ToolContext {

private static final String UPLOAD_DIRECTORY_NAME = "upload";
private String location;
private String uploadDirectory;
private Map<String, SealedObject> credentialMap;
private Path workingDirectory;
protected Path uploadDirectory;
protected Path toolDownloadDirectory;

private Map<String, SealedObject> credentialMap = new HashMap<>();

public void setLocation(String location) {
this.location = location;
}

public ToolContext(ToolContextBuilder builder) {
this.location = builder.location;
this.uploadDirectory = builder.uploadDirectory;
this.credentialMap = builder.credentialMap;
public void workingDirectory(Path workingDirectory) {
if (workingDirectory == null) {
throw new IllegalArgumentException("Upload directory may not be null!");
}
this.workingDirectory = workingDirectory;
this.uploadDirectory = workingDirectory.resolve(UPLOAD_DIRECTORY_NAME);
}

public void setCredentialMap(Map<String, SealedObject> credentialMap) {
this.credentialMap = credentialMap;
}

public String getLocation() {
return location;
}

public String getUploadDirectory() {
public Path getUploadDirectory() {
return uploadDirectory;
}

public Map<String, SealedObject> getCredentialMap() {
return Collections.unmodifiableMap(credentialMap);
}

public abstract static class ToolContextBuilder {
private String location;
private String uploadDirectory;
private Map<String, SealedObject> credentialMap = new HashMap<>();

public abstract ToolContext build();

public ToolContextBuilder setLocation(String location) {
if (location == null || location.isEmpty()) {
throw new IllegalArgumentException("Defined Location must not be null or empty.");
}
this.location = location;
return this;
}

public ToolContextBuilder setUploadDirectory(String uploadDirectory) {
if (uploadDirectory == null || uploadDirectory.isEmpty()) {
throw new IllegalArgumentException("Defined PDS Prepare Upload Directory must not be null or empty.");
}
this.uploadDirectory = uploadDirectory;
return this;
}

public ToolContextBuilder setCredentialMap(Map<String, SealedObject> credentialMap) {
if (!(credentialMap == null)) {
this.credentialMap = credentialMap;
}
return this;
}
public Path getToolDownloadDirectory() {
return toolDownloadDirectory;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.mercedesbenz.sechub.wrapper.prepare.cli.PrepareWrapperKeyConstants.KEY_PDS_PREPARE_PROCESS_TIMEOUT_SECONDS;

import java.io.IOException;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
Expand All @@ -26,7 +27,7 @@ public abstract class WrapperTool {
@Value("${" + KEY_PDS_PREPARE_PROCESS_TIMEOUT_SECONDS + ":-1}")
private int pdsPrepareProcessTimeoutSeconds;

protected abstract void cleanUploadDirectory(String uploadDirectory) throws IOException;
protected abstract void cleanUploadDirectory(Path directory) throws IOException;

protected void waitForProcessToFinish(ProcessAdapter process) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,34 @@
package com.mercedesbenz.sechub.wrapper.prepare.modules.git;

import java.nio.file.Path;

import com.mercedesbenz.sechub.wrapper.prepare.modules.ToolContext;

public class GitContext extends ToolContext {
private boolean cloneWithoutHistory;

private String filename;
static final String DOWNLOAD_DIRECTORY_NAME = "git-download";
private boolean cloneWithoutHistory;
private String repositoryName = "git-repository";

private GitContext(GitContextBuilder builder) {
super(builder);
this.cloneWithoutHistory = builder.cloneWithoutHistory;
this.filename = builder.filename;
public void setCloneWithoutHistory(boolean cloneWithoutHistory) {
this.cloneWithoutHistory = cloneWithoutHistory;
}

public boolean isCloneWithoutHistory() {
return cloneWithoutHistory;
public void setWorkingDirectory(Path workingDirectory) {
super.workingDirectory(workingDirectory);
toolDownloadDirectory = workingDirectory.resolve(DOWNLOAD_DIRECTORY_NAME);
}

public String getFilename() {
return filename;
public String getRepositoryName() {
return repositoryName;
}

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

private String filename = "GitDownloadDirectory";

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

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

@Override
public GitContext build() {
return new GitContext(this);
}
public boolean isCloneWithoutHistory() {
return cloneWithoutHistory;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static com.mercedesbenz.sechub.wrapper.prepare.cli.PrepareWrapperEnvironmentVariables.PDS_PREPARE_CREDENTIAL_PASSWORD;
import static com.mercedesbenz.sechub.wrapper.prepare.cli.PrepareWrapperEnvironmentVariables.PDS_PREPARE_CREDENTIAL_USERNAME;

import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;

Expand All @@ -26,19 +26,23 @@ public class JGitAdapter {

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

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

CloneCommand command = Git.cloneRepository().setURI(location).setDirectory(Paths.get(uploadDirectory).toFile());
/*@formatter:off*/
CloneCommand command = Git.cloneRepository()
.setURI(location).
setDirectory(downloadDirectory.resolve(Path.of(gitContext.getRepositoryName())).toFile());
/*@formatter:on*/

if (username != null && password != null) {
LOG.debug("Cloning private repository: " + location + " with username and password to: " + uploadDirectory);
LOG.debug("Cloning private repository: " + location + " with username and password to: " + downloadDirectory);
command = command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
} else {
LOG.debug("Cloning public repository: " + location + " to: " + uploadDirectory);
LOG.debug("Cloning public repository: " + location + " to: " + downloadDirectory);
}

if (gitContext.isCloneWithoutHistory()) {
Expand Down
Loading

0 comments on commit 311dbee

Please sign in to comment.