-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from snyk/chore/resolve-ecosystem-based-on-pa…
…ckage-type chore: resolve ecosystem based on package type
- Loading branch information
Showing
11 changed files
with
194 additions
and
286 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
core/src/main/java/io/snyk/plugins/artifactory/ecosystem/EcosystemResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.snyk.plugins.artifactory.ecosystem; | ||
|
||
import org.artifactory.repo.RepoPath; | ||
|
||
import java.util.Optional; | ||
|
||
public interface EcosystemResolver { | ||
|
||
Optional<Ecosystem> getFor(RepoPath repoPath); | ||
} |
37 changes: 37 additions & 0 deletions
37
.../main/java/io/snyk/plugins/artifactory/ecosystem/RepositoryMetadataEcosystemResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.snyk.plugins.artifactory.ecosystem; | ||
|
||
import org.artifactory.repo.RepoPath; | ||
import org.artifactory.repo.Repositories; | ||
import org.artifactory.repo.RepositoryConfiguration; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Optional; | ||
|
||
public class RepositoryMetadataEcosystemResolver implements EcosystemResolver { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(RepositoryMetadataEcosystemResolver.class); | ||
|
||
private final Repositories repositories; | ||
|
||
public RepositoryMetadataEcosystemResolver(Repositories repositories) { | ||
this.repositories = repositories; | ||
} | ||
|
||
@Override | ||
public Optional<Ecosystem> getFor(RepoPath repoPath) { | ||
RepositoryConfiguration repositoryConfiguration = repositories.getRepositoryConfiguration(repoPath.getRepoKey()); | ||
if(repositoryConfiguration == null) { | ||
LOG.error("No repository configuration for {}", repoPath); | ||
return Optional.empty(); | ||
} | ||
|
||
String packageType = repositoryConfiguration.getPackageType(); | ||
if(packageType == null) { | ||
LOG.error("No package type for {}", repoPath); | ||
return Optional.empty(); | ||
} | ||
|
||
return Ecosystem.fromPackageType(packageType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
core/src/main/java/io/snyk/plugins/artifactory/scanner/ScannerResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.snyk.plugins.artifactory.scanner; | ||
|
||
import io.snyk.plugins.artifactory.configuration.ConfigurationModule; | ||
import io.snyk.plugins.artifactory.configuration.PluginConfiguration; | ||
import io.snyk.plugins.artifactory.ecosystem.Ecosystem; | ||
import io.snyk.sdk.api.v1.SnykClient; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
public class ScannerResolver { | ||
private static final Logger LOG = LoggerFactory.getLogger(ScannerResolver.class); | ||
private final Function<PluginConfiguration, String> getConfig; | ||
private final Map<Ecosystem, PackageScanner> scannerByEcosystem = new HashMap<>(); | ||
|
||
public ScannerResolver(Function<PluginConfiguration, String> getConfig) { | ||
this.getConfig = getConfig; | ||
} | ||
|
||
public ScannerResolver register(Ecosystem ecosystem, PackageScanner scanner) { | ||
scannerByEcosystem.put(ecosystem, scanner); | ||
return this; | ||
} | ||
|
||
public Optional<PackageScanner> getFor(Ecosystem ecosystem) { | ||
PluginConfiguration configKey = ecosystem.getConfigProperty(); | ||
String configValue = getConfig.apply(configKey); | ||
if (!"true".equals(configValue)) { | ||
LOG.info("Snyk scanner disabled for {}. Config: {} = {}", ecosystem.name(), configKey.propertyKey(), configValue); | ||
return Optional.empty(); | ||
} | ||
|
||
PackageScanner scanner = scannerByEcosystem.get(ecosystem); | ||
|
||
if (scanner == null) { | ||
LOG.error("No scanner registered for {}", ecosystem.name()); | ||
} | ||
|
||
return Optional.ofNullable(scanner); | ||
} | ||
|
||
public static ScannerResolver setup(ConfigurationModule configurationModule, SnykClient snykClient) { | ||
return new ScannerResolver(configurationModule::getPropertyOrDefault) | ||
.register(Ecosystem.MAVEN, new MavenScanner(configurationModule, snykClient)) | ||
.register(Ecosystem.NPM, new NpmScanner(configurationModule, snykClient)) | ||
.register(Ecosystem.PYPI, new PythonScanner(configurationModule, snykClient)); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
core/src/test/java/io/snyk/plugins/artifactory/ecosystem/EcosystemTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.snyk.plugins.artifactory.ecosystem; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
class EcosystemTest { | ||
|
||
@Test | ||
void ecosystemByPackageType() { | ||
assertThat(Ecosystem.fromPackageType("maven")).contains(Ecosystem.MAVEN); | ||
assertThat(Ecosystem.fromPackageType("npm")).contains(Ecosystem.NPM); | ||
assertThat(Ecosystem.fromPackageType("pypi")).contains(Ecosystem.PYPI); | ||
assertThat(Ecosystem.fromPackageType("nuget")).isEmpty(); | ||
} | ||
} |
19 changes: 0 additions & 19 deletions
19
core/src/test/java/io/snyk/plugins/artifactory/scanner/EcosystemTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.