Skip to content

Commit

Permalink
Changed directory cleanup from process to plain java #3026
Browse files Browse the repository at this point in the history
- introduced DirectoryAndFileSupport, wrote tests
- introduced AutoCleanupGitFilesFilter, wrote tests
- updated GitWrapperTest
- separated former git auto clean environment entry to
  KEY_PDS_PREPARE_MODULE_GIT_REMOVE_GIT_FILES_BEFORE_UPLOAD
  KEY_PDS_PREPARE_MODULE_GIT_CLONE_WITHOUT_GIT_HISTORY
- changed environment entries for skopeo as well, everything from
  a module is now prefixed "pds.prepare.module." etc.
  • Loading branch information
de-jcup committed Jun 10, 2024
1 parent 6b535c4 commit d8bcdf8
Show file tree
Hide file tree
Showing 15 changed files with 463 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.mercedesbenz.sechub.commons.archive;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class DirectoryAndFileSupport {

public void cleanDirectories(File parentFolder, FileFilter filterAcceptingFilesToDelete) throws IOException {
if (parentFolder == null) {
throw new IllegalArgumentException("Parent folder may not be null!");
}
cleanDirectoriesRecursive(parentFolder, filterAcceptingFilesToDelete);
}

private void cleanDirectoriesRecursive(File parentFolder, FileFilter filter) throws IOException {
File[] files = parentFolder.listFiles();
for (File file : files) {
if (file.isDirectory()) {
if (filter.accept(file)) {
/* delete the sub directory recursive */
FileUtils.forceDelete(file);
} else {
/* not accepted to delete, but inspect children */
cleanDirectories(file, filter);
}
} else {
/* not directory */
if (filter.accept(file)) {
FileUtils.forceDelete(file);
}
}
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
package com.mercedesbenz.sechub.commons.archive;

import static org.junit.jupiter.api.Assertions.*;

import java.io.File;
import java.io.IOException;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.mercedesbenz.sechub.test.TestFileWriter;
import com.mercedesbenz.sechub.test.TestUtil;

class DirectoryAndFileSupportTest {

private DirectoryAndFileSupport supportToTest;

private TestFileWriter testFileWriter = new TestFileWriter();
private File gitFolder;
private File gitIgnoreInGitFolder;
private File gitAttributesInGitFolder;
private File testDataInGit;
private File subFolder1;
private File testDataInSubFolder1;
private File gitAttributesInSubFolder1;
private File gitIgnoreInSubFolder1;
private File git2InsideSubFolder1;
private File testDataInGit2;
private File subFolder2InSubFolder1;
private File testDataInSubFolder2;
private File parentFolder;

@BeforeEach
void beforeEach() {
supportToTest = new DirectoryAndFileSupport();
}

@Test
void testfile_structure_removing_git_folder_git_attributes_and_git_ignore_files_recursive() throws Exception {
/* prepare */
initTestFileStructure();

/* execute */
supportToTest.cleanDirectories(parentFolder, (file) -> {
if (file.isDirectory()) {
return file.getName().equals(".git");
}
return file.getName().equals(".gitattributes") || file.getName().equals(".gitignore");
});

/* test */
assertFalse(gitIgnoreInGitFolder.exists());
assertFalse(gitAttributesInGitFolder.exists());
assertFalse(testDataInGit.exists());
assertTrue(testDataInSubFolder1.exists());
assertFalse(gitAttributesInSubFolder1.exists());
assertFalse(gitIgnoreInSubFolder1.exists());
assertFalse(gitFolder.exists());
assertFalse(git2InsideSubFolder1.exists());
assertFalse(testDataInGit2.exists());
assertTrue(testDataInSubFolder2.exists());

}

@Test
void testfile_structure_removing_git_attributes_and_git_ignore_files_recursive() throws Exception {
/* prepare */
initTestFileStructure();

/* execute */
supportToTest.cleanDirectories(parentFolder, (file) -> {
if (file.isFile()) {
return file.getName().equals(".gitattributes") || file.getName().equals(".gitignore");
}
return false;
});

/* test */
assertFalse(gitIgnoreInGitFolder.exists());
assertFalse(gitAttributesInGitFolder.exists());
assertTrue(testDataInGit.exists());
assertTrue(testDataInSubFolder1.exists());
assertFalse(gitAttributesInSubFolder1.exists());
assertFalse(gitIgnoreInSubFolder1.exists());
assertTrue(gitFolder.exists());
assertTrue(git2InsideSubFolder1.exists());
assertTrue(testDataInGit2.exists());
assertTrue(testDataInSubFolder2.exists());

}

@Test
void testfile_structure_removing_git_folder_only_recursive() throws Exception {
/* prepare */
initTestFileStructure();

/* execute */
supportToTest.cleanDirectories(parentFolder, (file) -> {
if (file.isDirectory()) {
return file.getName().equals(".git");
}
return false;
});

/* test */
assertFalse(gitIgnoreInGitFolder.exists());
assertFalse(gitAttributesInGitFolder.exists());
assertFalse(testDataInGit.exists());
assertTrue(testDataInSubFolder1.exists());
assertTrue(gitAttributesInSubFolder1.exists());
assertTrue(gitIgnoreInSubFolder1.exists());
assertFalse(gitFolder.exists());
assertFalse(git2InsideSubFolder1.exists());
assertFalse(testDataInGit2.exists());
assertTrue(testDataInSubFolder2.exists());

}

@Test
void testfile_structure_nothing_removed_because_all_not_accepted_recursive() throws Exception {
/* prepare */
initTestFileStructure();

/* execute */
supportToTest.cleanDirectories(parentFolder, (file) -> {
return false;
});

assertAllTestFileStructureFiles(true);

}

@Test
void testfile_structure_all_removed_because_all_accepted_recursive() throws Exception {
/* prepare */
initTestFileStructure();

/* execute */
supportToTest.cleanDirectories(parentFolder, (file) -> {
return true;
});

assertAllTestFileStructureFiles(false);

}

/**
* Creates following file structure:
*
* <pre>
* /.git/.gitignore
* /.git/.gitattributes
* /.git/testdata-inside-git-folder.txt
* /subfolder1/testdata.txt
* /subfolder1/.gitattributes
* /subfolder1/.gitignore
* /subfolder1/.git/testdata-git2.txt
* /subfolder1/subfolder2/testdata-in-subfolder2
* </pre>
*
* @throws IOException
*/
void initTestFileStructure() throws IOException {
parentFolder = TestUtil.createTempDirectoryInBuildFolder("directory-and-file-support").toFile();

gitFolder = new File(parentFolder, ".git");
gitIgnoreInGitFolder = new File(gitFolder, ".gitignore");
gitAttributesInGitFolder = new File(gitFolder, ".gitattributes");
testDataInGit = new File(gitFolder, "testdata-inside-git-folder.txt");

testFileWriter.write(gitIgnoreInGitFolder, "testdata-gitignore-in-gitfolder");
testFileWriter.write(gitAttributesInGitFolder, "testdata-gitattributes-in-gitfolder");
testFileWriter.write(testDataInGit, "testdata-in-git-folder");

subFolder1 = new File(parentFolder, "subfolder1");

testDataInSubFolder1 = new File(subFolder1, "testdata.txt");
gitAttributesInSubFolder1 = new File(subFolder1, ".gitattributes");
gitIgnoreInSubFolder1 = new File(subFolder1, ".gitignore");

testFileWriter.write(testDataInSubFolder1, "testdata-subfolder1");
testFileWriter.write(gitAttributesInSubFolder1, "testdata-gitattributes-in-gitfolder");
testFileWriter.write(gitIgnoreInSubFolder1, "testdata-in-git-folder");

git2InsideSubFolder1 = new File(subFolder1, ".git");
testDataInGit2 = new File(git2InsideSubFolder1, "testdata-git2.txt");
testFileWriter.write(testDataInGit2, "testdata-in-git-folder2");

subFolder2InSubFolder1 = new File(subFolder1, "subfolder2");
testDataInSubFolder2 = new File(subFolder2InSubFolder1, "testdata-in-subfolder2");
testFileWriter.write(testDataInSubFolder2, "testdata-in-subfolder2");

/* check precondition */
assertAllTestFileStructureFiles(true);

}

private void assertAllTestFileStructureFiles(boolean mustExist) {
assertEquals(mustExist, gitFolder.exists());
assertEquals(mustExist, gitIgnoreInGitFolder.exists());
assertEquals(mustExist, gitAttributesInGitFolder.exists());
assertEquals(mustExist, testDataInGit.exists());
assertEquals(mustExist, testDataInSubFolder1.exists());
assertEquals(mustExist, gitAttributesInSubFolder1.exists());
assertEquals(mustExist, gitIgnoreInSubFolder1.exists());
assertEquals(mustExist, git2InsideSubFolder1.exists());
assertEquals(mustExist, testDataInGit2.exists());
assertEquals(mustExist, testDataInSubFolder2.exists());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ public class TestFileWriter {

private static final Logger LOG = LoggerFactory.getLogger(TestFileWriter.class);

public void write(File targetFile, String text) throws IOException {
save(targetFile, text, false);
}

public void save(String text, File targetFile, Charset charset) throws IOException {
internalSave(targetFile, text, true, charset);
}

public void write(File targetFile, String text, boolean overwrite) throws IOException {
save(targetFile, text, overwrite);
}

public void save(File targetFile, String text, boolean overwrite) throws IOException {
internalSave(targetFile, text, overwrite, Charset.forName("UTF-8"));
}
Expand Down
1 change: 1 addition & 0 deletions sechub-wrapper-prepare/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies {
implementation spring_boot_dependency.logback_classic

implementation library.jgit_core
implementation library.apache_commons_io

/* test */
testImplementation project(':sechub-testframework')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.mercedesbenz.sechub.commons.TextFileWriter;
import com.mercedesbenz.sechub.commons.archive.ArchiveSupport;
import com.mercedesbenz.sechub.commons.archive.DirectoryAndFileSupport;
import com.mercedesbenz.sechub.commons.core.security.CheckSumSupport;
import com.mercedesbenz.sechub.commons.pds.DefaultProcessBuilderFactory;
import com.mercedesbenz.sechub.commons.pds.PDSProcessAdapterFactory;
Expand Down Expand Up @@ -45,4 +46,9 @@ ProcessBuilderFactory createProcessBuilderFactory() {
return new DefaultProcessBuilderFactory();
}

@Bean
DirectoryAndFileSupport createDirectoryAndFileSupport() {
return new DirectoryAndFileSupport();
}

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

public class PrepareWrapperKeyConstants {

private static final String PREPARE_MODULE = "pds.prepare.module";
private static final String PREPARE_MODULE_GIT = PREPARE_MODULE + ".git";
private static final String PREPARE_MODULE_SKOPEO = PREPARE_MODULE + ".skopeo";

/**
* 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 All @@ -11,37 +15,22 @@ public class PrepareWrapperKeyConstants {
public static final String KEY_PDS_PREPARE_PROCESS_TIMEOUT_SECONDS = "pds.prepare.process.timeout.seconds";

/**
* Flag to enable the gitWrapper prepare module
* Flag to enable the skopeoWrapper prepare module
*/
public static final String KEY_PDS_PREPARE_MODULE_GIT_ENABLED = "pds.prepare.module.git.enabled";
public static final String KEY_PDS_PREPARE_MODULE_SKOPEO_ENABLED = PREPARE_MODULE_SKOPEO + ".enabled";

/**
* Flag to enable the skopeoWrapper prepare module
* Filename for skopeoWrapper authentication file
*/
public static final String KEY_PDS_PREPARE_MODULE_SKOPEO_ENABLED = "pds.prepare.module.skopeo.enabled";
public static final String KEY_PDS_PREPARE_MODULE_SKOPEO_AUTHENTICATION_FILENAME = PREPARE_MODULE_SKOPEO + ".authentication.filename";

/**
* Flag to clean the gitWrapper folder from git files and clone without history
*/
/* FIXME Albert Tregnaghi, 2024-06-07: rename constant + check logic */
// public static final String KEY_PDS_PREPARE_AUTO_CLEANUP_GIT_FOLDER = "pds.prepare.module.git.history.wanted";
// public static final String KEY_PDS_PREPARE_AUTO_CLEANUP_GIT_FOLDER = "pds.prepare.module.git.autocleanup.enabled";
/*
* FIXME Albert Tregnaghi, 2024-06-07:differ between drop git files and history
* wanted - different usecases!
* Flag to enable the gitWrapper prepare module
*/
/* FIXME Albert Tregnaghi, 2024-06-07: do we need the drop functionality */
public static final String KEY_PDS_PREPARE_AUTO_CLEANUP_GIT_FOLDER = "pds.prepare.module.git.drop.gitfiles";
public static final String KEY_PDS_PREPARE_MODULE_GIT_ENABLED = PREPARE_MODULE_GIT + ".enabled";

/* FIXME Albert Tregnaghi, 2024-06-07: implement */
public static final String KEY_PDS_PREPARE_MODULE_GIT_HISTORY_WANTED = "pds.prepare.module.git.history.wanted";
public static final String KEY_PDS_PREPARE_MODULE_GIT_REMOVE_GIT_FILES_BEFORE_UPLOAD = PREPARE_MODULE_GIT + ".remove.gitfiles.before.upload";

public static final String KEY_PDS_PREPARE_MODULE_GIT_CLONE_WITHOUT_GIT_HISTORY = PREPARE_MODULE_GIT + ".clone.without.git.history";

/**
* Filename for skopeoWrapper authentication file
*/
/*
* FIXME Albert Tregnaghi, 2024-06-07: key correct ? what about
* pds.prepare.module.skopeo.... ?
*/
public static final String KEY_PDS_PREPARE_AUTHENTICATION_FILE_SKOPEO = "pds.prepare.authentication.file.skopeo";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.mercedesbenz.sechub.wrapper.prepare.modules.git;

import java.io.File;
import java.io.FileFilter;

public class AutoCleanupGitFilesFilter implements FileFilter {

public static AutoCleanupGitFilesFilter INSTANCE = new AutoCleanupGitFilesFilter();

private AutoCleanupGitFilesFilter() {
}

@Override
public boolean accept(File file) {
if (file == null) {
return false;
}
String name = file.getName();
if (name == null) {
return false;
}

if (file.isDirectory()) {
return name.equals(".git");
}
return name.equals(".gitattributes") || name.equals(".gitignore");
}

}
Loading

0 comments on commit d8bcdf8

Please sign in to comment.