-
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.
- Loading branch information
1 parent
67b705c
commit 930d590
Showing
20 changed files
with
302 additions
and
15 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
3 changes: 1 addition & 2 deletions
3
core/src/main/java/io/snyk/plugins/artifactory/exception/SnykAPIFailureException.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
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
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
35 changes: 35 additions & 0 deletions
35
core/src/main/java/io/snyk/plugins/artifactory/scanner/rubygems/RubyGemsPackage.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,35 @@ | ||
package io.snyk.plugins.artifactory.scanner.rubygems; | ||
|
||
import java.util.Optional; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class RubyGemsPackage { | ||
private final String name; | ||
private final String version; | ||
|
||
public RubyGemsPackage(String name, String version) { | ||
this.name = name; | ||
this.version = version; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getVersion() { | ||
return version; | ||
} | ||
|
||
public static Optional<RubyGemsPackage> parse(String artifactoryPackageName) { | ||
if(artifactoryPackageName == null) { | ||
return Optional.empty(); | ||
} | ||
Pattern pattern = Pattern.compile("(.*)-([^-]+)\\.gem", Pattern.CASE_INSENSITIVE); | ||
Matcher matcher = pattern.matcher(artifactoryPackageName); | ||
if(!matcher.matches()) { | ||
return Optional.empty(); | ||
} | ||
return Optional.of(new RubyGemsPackage(matcher.group(1), matcher.group(2))); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
core/src/main/java/io/snyk/plugins/artifactory/scanner/rubygems/RubyGemsScanner.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,63 @@ | ||
package io.snyk.plugins.artifactory.scanner.rubygems; | ||
|
||
import io.snyk.plugins.artifactory.exception.CannotScanException; | ||
import io.snyk.plugins.artifactory.exception.SnykAPIFailureException; | ||
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.TestResultConverter; | ||
import io.snyk.sdk.api.SnykClient; | ||
import io.snyk.sdk.api.SnykResult; | ||
import io.snyk.sdk.model.purl.PurlIssues; | ||
import org.artifactory.fs.FileLayoutInfo; | ||
import org.artifactory.repo.RepoPath; | ||
import org.slf4j.Logger; | ||
|
||
import java.net.URLEncoder; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
public class RubyGemsScanner implements PackageScanner { | ||
|
||
private static final Logger LOG = getLogger(RubyGemsScanner.class); | ||
private final SnykClient snykClient; | ||
private final String orgId; | ||
|
||
public RubyGemsScanner(SnykClient snykClient, String orgId) { | ||
this.snykClient = snykClient; | ||
this.orgId = orgId; | ||
} | ||
|
||
@Override | ||
public TestResult scan(FileLayoutInfo fileLayoutInfo, RepoPath repoPath) { | ||
RubyGemsPackage pckg = RubyGemsPackage.parse(repoPath.getName()) | ||
.orElseThrow(() -> new CannotScanException("Unexpected Ruby Gems package name: " + repoPath.getName())); | ||
|
||
String purl = "pkg:gem/" + pckg.getName() + "@" + pckg.getVersion(); | ||
|
||
SnykResult<PurlIssues> result; | ||
try { | ||
LOG.debug("Running Snyk test: {}, name: {}, version: {}", repoPath, pckg.getName(), pckg.getVersion()); | ||
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 = getModuleDetailsURL(pckg.getName(), pckg.getVersion()); | ||
|
||
return TestResultConverter.convert(testResult); | ||
} | ||
|
||
public static String getModuleDetailsURL(String name, String version) { | ||
return SnykDetailsUrl.create("rubygems", name, version).toString(); | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
core/src/test/java/io/snyk/plugins/artifactory/scanner/rubygems/RubyGemsPackageTest.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,28 @@ | ||
package io.snyk.plugins.artifactory.scanner.rubygems; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class RubyGemsPackageTest { | ||
@Test | ||
void parse() { | ||
assertThat(RubyGemsPackage.parse("mustermann-3.0.3.gem")).isPresent(); | ||
assertThat(RubyGemsPackage.parse("mustermann-3.0.3.gem").get().getName()).isEqualTo("mustermann"); | ||
assertThat(RubyGemsPackage.parse("mustermann-3.0.3.gem").get().getVersion()).isEqualTo("3.0.3"); | ||
|
||
assertThat(RubyGemsPackage.parse("rack-protection-4.1.1.gem")).isPresent(); | ||
assertThat(RubyGemsPackage.parse("rack-protection-4.1.1.gem").get().getName()).isEqualTo("rack-protection"); | ||
assertThat(RubyGemsPackage.parse("rack-protection-4.1.1.gem").get().getVersion()).isEqualTo("4.1.1"); | ||
} | ||
|
||
@Test | ||
void parse_null() { | ||
assertThat(RubyGemsPackage.parse(null)).isEmpty(); | ||
} | ||
|
||
@Test | ||
void parse_invalidName() { | ||
assertThat(RubyGemsPackage.parse("mustermann")).isEmpty(); | ||
} | ||
} |
Oops, something went wrong.