-
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.
feat: add support for CocoaPods and Nuget ecosystems
Similar to the Gems scanner, CocoaPods and Nuget are powered by Snyk's PURL test API. Disabled by default so these new ecosystems won't be scanned unless explicitly opted-in.
- Loading branch information
1 parent
5b7b921
commit 324d70c
Showing
17 changed files
with
443 additions
and
49 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
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
46 changes: 46 additions & 0 deletions
46
core/src/main/java/io/snyk/plugins/artifactory/scanner/cocoapods/CocoapodsPackage.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,46 @@ | ||
package io.snyk.plugins.artifactory.scanner.cocoapods; | ||
|
||
import org.slf4j.Logger; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
public class CocoapodsPackage { | ||
private static final Logger LOG = getLogger(CocoapodsPackage.class); | ||
private final String name; | ||
private final String version; | ||
|
||
public CocoapodsPackage(String name, String version) { | ||
this.name = name; | ||
this.version = version; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getVersion() { | ||
return version; | ||
} | ||
|
||
public static Optional<CocoapodsPackage> parse( | ||
String artifactoryPackageName | ||
) { | ||
if (artifactoryPackageName == null) { | ||
LOG.warn("Unexpected package name: null"); | ||
return Optional.empty(); | ||
} | ||
|
||
String[] nameVersion = artifactoryPackageName.replace(".tar.gz", "") | ||
.replaceFirst("(?s)-(?!.*?-)", "!") | ||
.split("!"); | ||
|
||
if (nameVersion.length != 2) { | ||
LOG.warn("Unexpected Cocoapods package name: {}", artifactoryPackageName); | ||
return Optional.empty(); | ||
} | ||
|
||
return Optional.of(new CocoapodsPackage(nameVersion[0], nameVersion[1])); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
core/src/main/java/io/snyk/plugins/artifactory/scanner/cocoapods/CocoapodsScanner.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,41 @@ | ||
package io.snyk.plugins.artifactory.scanner.cocoapods; | ||
|
||
import io.snyk.plugins.artifactory.exception.CannotScanException; | ||
import io.snyk.plugins.artifactory.model.TestResult; | ||
import io.snyk.plugins.artifactory.scanner.PackageScanner; | ||
import io.snyk.plugins.artifactory.scanner.SnykDetailsUrl; | ||
import io.snyk.plugins.artifactory.scanner.purl.PurlScanner; | ||
import org.artifactory.fs.FileLayoutInfo; | ||
import org.artifactory.repo.RepoPath; | ||
import org.slf4j.Logger; | ||
|
||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
public class CocoapodsScanner implements PackageScanner { | ||
|
||
private static final Logger LOG = getLogger(CocoapodsScanner.class); | ||
private final PurlScanner purlScanner; | ||
|
||
public CocoapodsScanner(PurlScanner purlScanner) { | ||
this.purlScanner = purlScanner; | ||
} | ||
|
||
@Override | ||
public TestResult scan(FileLayoutInfo fileLayoutInfo, RepoPath repoPath) { | ||
LOG.debug("Cocoapods: repoPath.getName() {}", repoPath.getName()); | ||
|
||
CocoapodsPackage pckg = CocoapodsPackage.parse(repoPath.getName()) | ||
.orElseThrow(() -> new CannotScanException("Unexpected Cocoapods package name" + repoPath.getName())); | ||
|
||
String purl = "pkg:cocoapods/" + pckg.getName() + "@" + pckg.getVersion(); | ||
|
||
String packageDetailsUrl = getModuleDetailsURL(pckg.getName(), pckg.getVersion()); | ||
|
||
return purlScanner.scan(purl, packageDetailsUrl); | ||
} | ||
|
||
public static String getModuleDetailsURL(String name, String version) { | ||
return SnykDetailsUrl.create("cocoapods", name, version).toString(); | ||
} | ||
} | ||
|
47 changes: 47 additions & 0 deletions
47
core/src/main/java/io/snyk/plugins/artifactory/scanner/nuget/NugetPackage.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,47 @@ | ||
package io.snyk.plugins.artifactory.scanner.nuget; | ||
|
||
|
||
import org.slf4j.Logger; | ||
|
||
import java.util.Optional; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
public class NugetPackage { | ||
private static final Logger LOG = getLogger(NugetPackage.class); | ||
private final String name; | ||
private final String version; | ||
|
||
public NugetPackage(String name, String version) { | ||
this.name = name; | ||
this.version = version; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getVersion() { | ||
return version; | ||
} | ||
|
||
public static Optional<NugetPackage> parse(String artifactoryPackageName) { | ||
if (artifactoryPackageName == null) { | ||
LOG.warn("Unexpected Nuget package name: null"); | ||
return Optional.empty(); | ||
} | ||
|
||
Pattern pattern = Pattern.compile("\\.([0-9]+\\..*)\\.nupkg"); | ||
Matcher matcher = pattern.matcher(artifactoryPackageName); | ||
if (!matcher.find()) { | ||
LOG.warn("Unexpected Nuget package name: {}", artifactoryPackageName); | ||
return Optional.empty(); | ||
} | ||
String name = artifactoryPackageName.substring(0, matcher.start()); | ||
String version = matcher.group(1); | ||
|
||
return Optional.of(new NugetPackage(name, version)); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
core/src/main/java/io/snyk/plugins/artifactory/scanner/nuget/NugetScanner.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,34 @@ | ||
package io.snyk.plugins.artifactory.scanner.nuget; | ||
|
||
import io.snyk.plugins.artifactory.exception.CannotScanException; | ||
import io.snyk.plugins.artifactory.model.TestResult; | ||
import io.snyk.plugins.artifactory.scanner.PackageScanner; | ||
import io.snyk.plugins.artifactory.scanner.SnykDetailsUrl; | ||
import io.snyk.plugins.artifactory.scanner.purl.PurlScanner; | ||
import org.artifactory.fs.FileLayoutInfo; | ||
import org.artifactory.repo.RepoPath; | ||
|
||
public class NugetScanner implements PackageScanner { | ||
|
||
private final PurlScanner purlScanner; | ||
|
||
public NugetScanner(PurlScanner purlScanner) { | ||
this.purlScanner = purlScanner; | ||
} | ||
|
||
@Override | ||
public TestResult scan(FileLayoutInfo fileLayoutInfo, RepoPath repoPath) { | ||
NugetPackage pckg = NugetPackage.parse(repoPath.getName()) | ||
.orElseThrow(() -> new CannotScanException("Unexpected Nuget package name: " + repoPath.getName())); | ||
|
||
String purl = "pkg:nuget/" + pckg.getName() + "@" + pckg.getVersion(); | ||
|
||
String packageDetailsUrl = getModuleDetailsURL(pckg.getName(), pckg.getVersion()); | ||
|
||
return purlScanner.scan(purl, packageDetailsUrl); | ||
} | ||
|
||
public static String getModuleDetailsURL(String name, String version) { | ||
return SnykDetailsUrl.create("nuget", name, version).toString(); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
core/src/main/java/io/snyk/plugins/artifactory/scanner/purl/PurlScanner.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,50 @@ | ||
package io.snyk.plugins.artifactory.scanner.purl; | ||
|
||
import io.snyk.plugins.artifactory.exception.SnykAPIFailureException; | ||
import io.snyk.plugins.artifactory.model.TestResult; | ||
import io.snyk.plugins.artifactory.scanner.TestResultConverter; | ||
import io.snyk.sdk.api.SnykClient; | ||
import io.snyk.sdk.api.SnykResult; | ||
import io.snyk.sdk.model.purl.PurlIssues; | ||
import org.slf4j.Logger; | ||
|
||
import java.net.URLEncoder; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
public class PurlScanner { | ||
|
||
private static final Logger LOG = getLogger(PurlScanner.class); | ||
|
||
private final SnykClient snykClient; | ||
private final String orgId; | ||
|
||
public PurlScanner(SnykClient snykClient, String orgId) { | ||
this.snykClient = snykClient; | ||
this.orgId = orgId; | ||
} | ||
|
||
public TestResult scan(String purl, String packageDetailsUrl) { | ||
SnykResult<PurlIssues> result; | ||
try { | ||
LOG.debug("Running Snyk test: {}", packageDetailsUrl); | ||
result = snykClient.get(PurlIssues.class, request -> | ||
request | ||
.withPath(String.format("rest/orgs/%s/packages/%s/issues", | ||
URLEncoder.encode(orgId, UTF_8), | ||
URLEncoder.encode(purl, UTF_8)) | ||
) | ||
.withQueryParam("version", "2024-10-15") | ||
); | ||
} catch (Exception e) { | ||
throw new SnykAPIFailureException(e); | ||
} | ||
|
||
PurlIssues testResult = result.get().orElseThrow(() -> new SnykAPIFailureException(result)); | ||
testResult.packageDetailsUrl = packageDetailsUrl; | ||
|
||
return TestResultConverter.convert(testResult); | ||
} | ||
|
||
} |
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
Oops, something went wrong.