-
Notifications
You must be signed in to change notification settings - Fork 10
feat: Add handling for a optional configurable delay in days to prevent package downloads #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
7f7df07
3557538
cc02901
7454575
11d7998
1a341f6
1b145ba
c39d4e7
09453fa
e01ab9f
11da2bc
540c625
371c46e
c82b960
047a6ad
14fb17d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,8 @@ | |
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.time.Instant; | ||
| import java.time.temporal.ChronoUnit; | ||
| import java.util.Optional; | ||
|
|
||
| import static java.lang.String.format; | ||
|
|
@@ -23,10 +25,40 @@ public PackageValidator(ValidationSettings settings) { | |
| } | ||
|
|
||
| public void validate(MonitoredArtifact artifact) { | ||
| validateCreatedDelay(artifact); | ||
| validateVulnerabilityIssues(artifact); | ||
| validateLicenseIssues(artifact); | ||
| } | ||
|
|
||
| private void validateCreatedDelay(MonitoredArtifact artifact) { | ||
| int delayDays = settings.getCreatedDelayDays(); | ||
| if (delayDays <= 0) { | ||
| LOG.debug("Created delay is disabled ({} days)", delayDays); | ||
| return; | ||
| } | ||
|
|
||
| Optional<Instant> createdDate = artifact.getCreatedDate(); | ||
| if (createdDate.isEmpty()) { | ||
| LOG.debug("Created date not available for {}, skipping created delay check", artifact.getPath()); | ||
| return; | ||
| } | ||
|
|
||
| Instant now = Instant.now(); | ||
| long daysSinceCreation = ChronoUnit.DAYS.between(createdDate.get(), now); | ||
|
|
||
| if (daysSinceCreation < delayDays) { | ||
|
||
| LOG.debug("Package created {} days ago, which is less than the delay of {} days: {}", | ||
| daysSinceCreation, delayDays, artifact.getPath()); | ||
| throw new CancelException(format( | ||
| "Artifact was created %d days ago, which is less than the configured delay of %d days: %s", | ||
| daysSinceCreation, delayDays, artifact.getPath() | ||
| ), 403); | ||
| } | ||
|
|
||
| LOG.debug("Package created {} days ago, which exceeds the delay of {} days: {}", | ||
| daysSinceCreation, delayDays, artifact.getPath()); | ||
| } | ||
|
|
||
| private void validateVulnerabilityIssues(MonitoredArtifact artifact) { | ||
| validateIssues( | ||
| artifact.getTestResult().getVulnSummary(), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I'm aware, passing
Optionalas parameters is still generally considered bad practice. It's much better as a return type than as a parameter.Better may just be to make this
Instantthen makegetCreatedDate():There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been changed to be just Instant. The constructor will default the value to null if its not passed.