Skip to content

Commit

Permalink
fix: [#418] Add more examples regex usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
030 committed Dec 31, 2023
1 parent a86e9a1 commit bfd15bd
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-20.04
steps:
- uses: actions/[email protected]
- uses: schubergphilis/mcvs-docker-action@v0.2.1
- uses: 030/mcvs-docker-action@18-trivyignore-validation
with:
dockle-accept-key: libcrypto3,libssl3
token: ${{ secrets.GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions .trivyignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Accept the risk until 2023-01-01
CVE-2019-14697 exp:2024-01-01
CVE-2019-14697 exp:2023-01-01
1 change: 1 addition & 0 deletions cmd/n3dr/repositoriesV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Examples:
FQDN: n3drURL,
HTTPS: &https,
Pass: n3drPass,
Regex: regex,
RepoName: n3drRepo,
SkipErrors: skipErrors,
User: n3drUser,
Expand Down
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Examples

regex

## repositoriesV2

Use the [basePathPrefix](./repositoriesV2/BASE_PATH_PREFIX.md) subcommand if
Expand Down
26 changes: 22 additions & 4 deletions internal/app/n3dr/artifactsv2/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"os"
"path/filepath"
"regexp"
"sync"
"time"

Expand Down Expand Up @@ -86,7 +87,7 @@ func (n *Nexus3) download(checksum, downloadedFileChecksum string, asset *models
return nil
}

func (n *Nexus3) downloadSingleArtifact(asset *models.AssetXO, repo string) {
func (n *Nexus3) downloadSingleArtifact(asset *models.AssetXO, repo string) error {
shaType, checksum := artifacts.Checksum(asset)

log.WithFields(log.Fields{
Expand All @@ -101,6 +102,17 @@ func (n *Nexus3) downloadSingleArtifact(asset *models.AssetXO, repo string) {
}
if !filesToBeSkipped {
file := filepath.Join(n.DownloadDirName, repo, assetPath)

// skip download of artifact if it does not match the regex
r, err := regexp.Compile(n.Regex)
if err != nil {
return err
}
if !r.MatchString(file) {
log.Debugf("file: '%s' skipped as it does not match regex: '%s'", file, n.Regex)
return nil
}

downloadedFileChecksum, err := artifacts.ChecksumLocalFile(file, shaType)
if err != nil {
panic(err)
Expand All @@ -110,6 +122,8 @@ func (n *Nexus3) downloadSingleArtifact(asset *models.AssetXO, repo string) {
panic(err)
}
}

return nil
}

func (n *Nexus3) downloadIfChecksumMismatchLocalFile(continuationToken, repo string) error {
Expand All @@ -129,13 +143,17 @@ func (n *Nexus3) downloadIfChecksumMismatchLocalFile(continuationToken, repo str
for _, item := range resp.GetPayload().Items {
for _, asset := range item.Assets {
if n.WithoutWaitGroups || n.WithoutWaitGroupArtifacts {
n.downloadSingleArtifact(asset, repo)
if err := n.downloadSingleArtifact(asset, repo); err != nil {
return err
}
} else {
wg.Add(1)
go func(asset *models.AssetXO) {
defer wg.Done()

n.downloadSingleArtifact(asset, repo)
if err := n.downloadSingleArtifact(asset, repo); err != nil {
panic(err)
}
}(asset)
}
}
Expand Down Expand Up @@ -199,7 +217,7 @@ func (n *Nexus3) repository(repo *models.AbstractAPIRepository) {
func (n *Nexus3) Backup() error {
var wg sync.WaitGroup

cn := connection.Nexus3{BasePathPrefix: n.BasePathPrefix, FQDN: n.FQDN, DownloadDirName: n.DownloadDirName, Pass: n.Pass, User: n.User, HTTPS: n.HTTPS, DockerHost: n.DockerHost, DockerPort: n.DockerPort, DockerPortSecure: n.DockerPortSecure}
cn := connection.Nexus3{BasePathPrefix: n.BasePathPrefix, FQDN: n.FQDN, DownloadDirName: n.DownloadDirName, Pass: n.Pass, User: n.User, HTTPS: n.HTTPS, DockerHost: n.DockerHost, DockerPort: n.DockerPort, DockerPortSecure: n.DockerPortSecure, Regex: n.Regex}
a := artifacts.Nexus3{Nexus3: &cn}
repos, err := a.Repos()
if err != nil {
Expand Down
14 changes: 12 additions & 2 deletions internal/app/n3dr/artifactsv2/upload/maven2/snapshot/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
)

type Nexus3 struct {
HTTPS, SkipErrors bool
DownloadDirName, FQDN, Pass, RepoFormat, RepoName, User string
HTTPS, SkipErrors bool
DownloadDirName, FQDN, Pass, Regex, RepoFormat, RepoName, User string
}

func (n *Nexus3) statusCode(resp *http.Response) error {
Expand All @@ -39,6 +39,16 @@ func (n *Nexus3) statusCode(resp *http.Response) error {
}

func (n *Nexus3) readRetryAndUpload(path string) error {
// skip upload of artifact if it does not match the regex
r, err := regexp.Compile(n.Regex)
if err != nil {
return err
}
if !r.MatchString(path) {
log.Debugf("file: '%s' skipped as it does not match regex: '%s'", path, n.Regex)
return nil
}

log.Debugf("reading path: '%s' and uploading it", path)
f, err := os.Open(filepath.Clean(path))
if err != nil {
Expand Down
12 changes: 11 additions & 1 deletion internal/app/n3dr/artifactsv2/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,16 @@ func (n *Nexus3) ReadLocalDirAndUploadArtifacts(localDiskRepoHome, localDiskRepo
return err
}

// skip upload of artifact if it does not match the regex
r, err := regexp.Compile(n.Regex)
if err != nil {
return err
}
if !r.MatchString(path) {
log.Debugf("file: '%s' skipped as it does not match regex: '%s'", path, n.Regex)
return nil
}

filesToBeSkipped, err := artifacts.FilesToBeSkipped(filepath.Ext(path))
if err != nil {
return err
Expand Down Expand Up @@ -704,7 +714,7 @@ func (n *Nexus3) maven2SnapshotsUpload(localDiskRepo string) {
log.Tracef("VersionPolicy: '%s'", vp)

if strings.EqualFold(vp, "snapshot") {
s := snapshot.Nexus3{DownloadDirName: n.DownloadDirName, FQDN: n.FQDN, HTTPS: *n.HTTPS, Pass: n.Pass, RepoFormat: "maven2", RepoName: localDiskRepo, SkipErrors: n.SkipErrors, User: n.User}
s := snapshot.Nexus3{DownloadDirName: n.DownloadDirName, FQDN: n.FQDN, HTTPS: *n.HTTPS, Pass: n.Pass, RepoFormat: "maven2", Regex: n.Regex, RepoName: localDiskRepo, SkipErrors: n.SkipErrors, User: n.User}

if err := s.Upload(); err != nil {
uploaded, errRegex := regexp.MatchString("bad status: 400 Repository does not allow updating assets", err.Error())
Expand Down
2 changes: 1 addition & 1 deletion internal/app/n3dr/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type Nexus3 struct {
AwsBucket, AwsID, AwsRegion, AwsSecret, BasePathPrefix, DockerHost, DownloadDirName, DownloadDirNameZip, Pass, RepoName, User string
AwsBucket, AwsID, AwsRegion, AwsSecret, BasePathPrefix, DockerHost, DownloadDirName, DownloadDirNameZip, Pass, Regex, RepoName, User string
DockerPort int32
DockerPortSecure, SkipErrors, StrictContentTypeValidation, WithoutWaitGroups, WithoutWaitGroupArtifacts, WithoutWaitGroupRepositories, ZIP bool
HTTPS *bool `validate:"required"`
Expand Down

0 comments on commit bfd15bd

Please sign in to comment.