Skip to content

Commit

Permalink
chore: extract ecosystem enum [OSM-2231]
Browse files Browse the repository at this point in the history
Adding new ecosystem support in small steps because the diff for the
original work is too massive. First baby step = introducing the
Ecosystem enum and extracting the file matching logic to it.
  • Loading branch information
jacek-rzrz committed Oct 28, 2024
1 parent 4adb712 commit 00d2a7b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.snyk.plugins.artifactory.scanner;

import io.snyk.plugins.artifactory.configuration.PluginConfiguration;

import java.util.Optional;

public enum Ecosystem {

MAVEN(PluginConfiguration.SCANNER_PACKAGE_TYPE_MAVEN),
NPM(PluginConfiguration.SCANNER_PACKAGE_TYPE_NPM),
PYPI(PluginConfiguration.SCANNER_PACKAGE_TYPE_PYPI),
;

private final PluginConfiguration configProperty;

Ecosystem(PluginConfiguration configProperty) {
this.configProperty = configProperty;
}

public PluginConfiguration getConfigProperty() {
return configProperty;
}

static Optional<Ecosystem> fromPackagePath(String path) {
if (path.endsWith(".jar")) {
return Optional.of(MAVEN);
}

if (path.endsWith(".tgz")) {
return Optional.of(NPM);
}

if (path.endsWith(".whl") || path.endsWith(".tar.gz") || path.endsWith(".zip") || path.endsWith(".egg")) {
return Optional.of(PYPI);
}

return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.Optional;

import static io.snyk.plugins.artifactory.configuration.ArtifactProperty.*;
import static io.snyk.plugins.artifactory.configuration.PluginConfiguration.*;
import static io.snyk.sdk.util.Predicates.distinctByKey;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -58,28 +57,21 @@ public void scanArtifact(@Nonnull RepoPath repoPath) {
}

protected PackageScanner getScannerForPackageType(String path) {
if (path.endsWith(".jar")) {
if (configurationModule.getPropertyOrDefault(SCANNER_PACKAGE_TYPE_MAVEN).equals("true")) {
return mavenScanner;
}
throw new CannotScanException(format("Plugin Property \"%s\" is not \"true\".", SCANNER_PACKAGE_TYPE_MAVEN.propertyKey()));
Ecosystem ecosystem = Ecosystem.fromPackagePath(path).orElseThrow(() -> new CannotScanException("Artifact is not supported."));
if(!configurationModule.getPropertyOrDefault(ecosystem.getConfigProperty()).equals("true")) {
throw new CannotScanException(format("Plugin Property \"%s\" is not \"true\".", ecosystem.getConfigProperty().propertyKey()));
}

if (path.endsWith(".tgz")) {
if (configurationModule.getPropertyOrDefault(SCANNER_PACKAGE_TYPE_NPM).equals("true")) {
switch (ecosystem) {
case MAVEN:
return mavenScanner;
case NPM:
return npmScanner;
}
throw new CannotScanException(format("Plugin Property \"%s\" is not \"true\".", SCANNER_PACKAGE_TYPE_NPM.propertyKey()));
}

if (path.endsWith(".whl") || path.endsWith(".tar.gz") || path.endsWith(".zip") || path.endsWith(".egg")) {
if (configurationModule.getPropertyOrDefault(SCANNER_PACKAGE_TYPE_PYPI).equals("true")) {
case PYPI:
return pythonScanner;
}
throw new CannotScanException(format("Plugin Property \"%s\" is not \"true\".", SCANNER_PACKAGE_TYPE_PYPI.propertyKey()));
default:
throw new IllegalStateException("Unsupported ecosystem: " + ecosystem.name());
}

throw new CannotScanException("Artifact is not supported.");
}

protected void updateProperties(RepoPath repoPath, TestResult testResult) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.snyk.plugins.artifactory.scanner;

import org.junit.jupiter.api.Test;

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

class EcosystemTest {

@Test
void testGetScannerForPackageType() {
assertEquals(Ecosystem.MAVEN, Ecosystem.fromPackagePath("myArtifact.jar").orElse(null));
assertEquals(Ecosystem.NPM, Ecosystem.fromPackagePath("myArtifact.tgz").orElse(null));
assertEquals(Ecosystem.PYPI, Ecosystem.fromPackagePath("myArtifact.whl").orElse(null));
assertEquals(Ecosystem.PYPI, Ecosystem.fromPackagePath("myArtifact.tar.gz").orElse(null));
assertEquals(Ecosystem.PYPI, Ecosystem.fromPackagePath("myArtifact.zip").orElse(null));
assertEquals(Ecosystem.PYPI, Ecosystem.fromPackagePath("myArtifact.egg").orElse(null));
assertNull(Ecosystem.fromPackagePath("file.txt").orElse(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,6 @@

public class ScannerModuleTest {

@Test
void testGetScannerForPackageType() {
Properties properties = new Properties();
properties.put(SCANNER_PACKAGE_TYPE_MAVEN.propertyKey(), "true");
properties.put(SCANNER_PACKAGE_TYPE_NPM.propertyKey(), "true");
properties.put(SCANNER_PACKAGE_TYPE_PYPI.propertyKey(), "true");

ConfigurationModule configurationModule = new ConfigurationModule(properties);

ScannerModule sm = new ScannerModule(
configurationModule,
mock(Repositories.class),
mock(SnykClient.class));

assertEquals(MavenScanner.class, sm.getScannerForPackageType("myArtifact.jar").getClass());
assertEquals(NpmScanner.class, sm.getScannerForPackageType("myArtifact.tgz").getClass());
assertEquals(PythonScanner.class, sm.getScannerForPackageType("myArtifact.whl").getClass());
assertEquals(PythonScanner.class, sm.getScannerForPackageType("myArtifact.tar.gz").getClass());
assertEquals(PythonScanner.class, sm.getScannerForPackageType("myArtifact.zip").getClass());
assertEquals(PythonScanner.class, sm.getScannerForPackageType("myArtifact.egg").getClass());
assertThrows(CannotScanException.class, () -> sm.getScannerForPackageType("unknown"));
}

@Test
void testGetScannerForPackageType_cannotScanPathsWithDisabledScanners() {
Properties properties = new Properties();
Expand Down

0 comments on commit 00d2a7b

Please sign in to comment.