From 9e4b9c6b4294e1ccc75803ab55c29e4fb828d751 Mon Sep 17 00:00:00 2001 From: ben Date: Sun, 19 Jul 2020 01:09:32 +0200 Subject: [PATCH 1/4] [GH-139] Anonymous mode. --- CHANGELOG.md | 7 +++++++ cli/req.go | 18 +++++++++--------- cmd/root.go | 3 --- integration-tests.sh | 18 ++++++++++++++---- snap/snapcraft.yaml | 4 ++-- 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c40f39a4..4e1ddb16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] None +## [4.1.0] - 2020-07-19 +### Added +- Backup by anonymous users + +### Fixed +- Incorrect version in snapcraft + ## [4.0.0] - 2020-07-18 ### Added - Log the name of the n3dr backup zip diff --git a/cli/req.go b/cli/req.go index a13c65cc..003db3e0 100644 --- a/cli/req.go +++ b/cli/req.go @@ -1,28 +1,28 @@ package cli import ( - "errors" "fmt" - "github.com/hashicorp/go-retryablehttp" "io/ioutil" "net/http" "strconv" + "github.com/hashicorp/go-retryablehttp" + log "github.com/sirupsen/logrus" ) -func (n Nexus3) validatePassword() error { +func (n Nexus3) validate() { + if n.User == "" { + log.Warn("Empty user. Verify whether the the subcommand is specified or anonymous mode is used") + } if n.Pass == "" { - return errors.New("Empty password. Verify whether the 'n3drPass' has been defined in ~/.n3dr.yaml") + log.Warn("Empty password. Verify whether the 'n3drPass' has been defined in ~/.n3dr.yaml, the subcommand is specified or anonymous mode is used") } - return nil } func (n Nexus3) request(url string) ([]byte, string, error) { - err := n.validatePassword() - if err != nil { - return nil, "", err - } + n.validate() + log.WithFields(log.Fields{"URL": url, "User": n.User}).Debug("URL Request") req, err := http.NewRequest("GET", url, nil) diff --git a/cmd/root.go b/cmd/root.go index 62cfa0cb..645731af 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -67,9 +67,6 @@ func init() { if err := rootCmd.MarkPersistentFlagRequired("n3drURL"); err != nil { log.Fatal(err) } - if err := rootCmd.MarkPersistentFlagRequired("n3drUser"); err != nil { - log.Fatal(err) - } } // initConfig reads in config file and ENV variables if set. diff --git a/integration-tests.sh b/integration-tests.sh index 22c82349..6483374d 100755 --- a/integration-tests.sh +++ b/integration-tests.sh @@ -64,10 +64,7 @@ upload(){ echo } -backup(){ - echo "Testing backup..." - $TOOL backup -n http://localhost:9999 -u admin -p $PASSWORD -r maven-releases -v "${NEXUS_API_VERSION}" -z - +backupHelper(){ if [ "${NEXUS_VERSION}" == "3.9.0" ]; then count_downloads 300 test_zip 148 @@ -80,6 +77,18 @@ backup(){ cleanup_downloads } +anonymous(){ + echo "Testing backup by anonymous user..." + $TOOL backup -n http://localhost:9999 -r maven-releases -v "${NEXUS_API_VERSION}" -z + backupHelper +} + +backup(){ + echo "Testing backup..." + $TOOL backup -n http://localhost:9999 -u admin -p $PASSWORD -r maven-releases -v "${NEXUS_API_VERSION}" -z + backupHelper +} + regex(){ echo "Testing backup regex..." $TOOL backup -n http://localhost:9999 -u admin -p $PASSWORD -r maven-releases -v "${NEXUS_API_VERSION}" -x 'some/group42' -z @@ -158,6 +167,7 @@ main(){ password files upload + anonymous backup repositories regex diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index ce563086..a675483e 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -1,6 +1,6 @@ name: n3dr # you probably want to 'snapcraft register ' base: core18 # the base snap is the execution environment for this snap -version: '3.6.3' # just for humans, typically '1.2+git' or '1.3.2' +version: '4.1.0' # just for humans, typically '1.2+git' or '1.3.2' summary: Nexus3 Disaster Recovery # 79 char long summary description: | Download all artifacts at once or migrate automatically from Nexus to Nexus. @@ -28,7 +28,7 @@ parts: source: . go-importpath: github.com/030/n3dr override-build: | - GO111MODULE=on CGO_ENABLED=0 go build -ldflags "-X n3dr/cmd.Version=3.6.2" -o $SNAPCRAFT_PART_INSTALL/n3dr + GO111MODULE=on CGO_ENABLED=0 go build -ldflags "-X n3dr/cmd.Version=${SNAPCRAFT_PROJECT_VERSION}" -o $SNAPCRAFT_PART_INSTALL/n3dr $SNAPCRAFT_PART_INSTALL/n3dr --version stage: - "n3dr" From d7691b98cb7f2aa0e8a00e85a72b83cf42907fe0 Mon Sep 17 00:00:00 2001 From: ben Date: Sun, 19 Jul 2020 01:11:26 +0200 Subject: [PATCH 2/4] [GH-139] RefLink forgotten in Changelog. --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e1ddb16..90e51b1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -208,7 +208,8 @@ None ### Added - Download all artifacts from a certain Nexus3 repository. -[Unreleased]: https://github.com/030/n3dr/compare/4.0.0...HEAD +[Unreleased]: https://github.com/030/n3dr/compare/4.1.0...HEAD +[4.1.0]: https://github.com/030/n3dr/compare/4.0.0...4.1.0 [4.0.0]: https://github.com/030/n3dr/compare/3.6.3...4.0.0 [3.6.3]: https://github.com/030/n3dr/compare/3.6.2...3.6.3 [3.6.2]: https://github.com/030/n3dr/compare/3.6.1...3.6.2 From 3c403998dfedae6400fc5e18c9de7b58b55bbca0 Mon Sep 17 00:00:00 2001 From: ben Date: Sun, 19 Jul 2020 01:18:59 +0200 Subject: [PATCH 3/4] [GH-139] Instructions anonymous mode added. --- README.md | 5 +++++ cli/req.go | 4 ++-- integration-tests.sh | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 135feb72..2f98f3bc 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,11 @@ Flags: Use "n3dr [command] --help" for more information about a command. ``` +## Anonymous + +In order to download as a anonymous user, one has to omit the `n3drUser` and +`n3drPass` subcommands. + ## Store the password in a read-only file Define the password in `~/.n3dr.yaml`: diff --git a/cli/req.go b/cli/req.go index 003db3e0..5aa8a15f 100644 --- a/cli/req.go +++ b/cli/req.go @@ -13,10 +13,10 @@ import ( func (n Nexus3) validate() { if n.User == "" { - log.Warn("Empty user. Verify whether the the subcommand is specified or anonymous mode is used") + log.Debug("Empty user. Verify whether the the subcommand is specified or anonymous mode is used") } if n.Pass == "" { - log.Warn("Empty password. Verify whether the 'n3drPass' has been defined in ~/.n3dr.yaml, the subcommand is specified or anonymous mode is used") + log.Debug("Empty password. Verify whether the 'n3drPass' has been defined in ~/.n3dr.yaml, the subcommand is specified or anonymous mode is used") } } diff --git a/integration-tests.sh b/integration-tests.sh index 6483374d..a86b542e 100755 --- a/integration-tests.sh +++ b/integration-tests.sh @@ -79,7 +79,7 @@ backupHelper(){ anonymous(){ echo "Testing backup by anonymous user..." - $TOOL backup -n http://localhost:9999 -r maven-releases -v "${NEXUS_API_VERSION}" -z + $TOOL backup -n http://localhost:9999 -r maven-releases -v "${NEXUS_API_VERSION}" -z -d backupHelper } From dbdb17f07d259732a8cd2e772d32ecce310e7c41 Mon Sep 17 00:00:00 2001 From: ben Date: Sun, 19 Jul 2020 01:33:39 +0200 Subject: [PATCH 4/4] [GH-139] BATS test were broken. --- integration-tests.sh | 2 +- tests.bats | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/integration-tests.sh b/integration-tests.sh index a86b542e..6483374d 100755 --- a/integration-tests.sh +++ b/integration-tests.sh @@ -79,7 +79,7 @@ backupHelper(){ anonymous(){ echo "Testing backup by anonymous user..." - $TOOL backup -n http://localhost:9999 -r maven-releases -v "${NEXUS_API_VERSION}" -z -d + $TOOL backup -n http://localhost:9999 -r maven-releases -v "${NEXUS_API_VERSION}" -z backupHelper } diff --git a/tests.bats b/tests.bats index e684461d..a4ffe9f0 100644 --- a/tests.bats +++ b/tests.bats @@ -6,12 +6,10 @@ [[ "$output" =~ $regex ]] } -@test "invoking n3dr without password specification prints an error" { - echo 'n3drPass: admin123' > ${HOME}/.n3dr.yaml - sed -i "s|n3drPass|nhihidrPass|" ~/.n3dr.yaml - run go run main.go repositories -n http://localhost:9999 -u admin -b +@test "invoking n3dr with unreachable URL exists after 6 attempts" { + run go run main.go repositories -n http://localhost:99999 -u admin -p INCORRECT_PASSWORD -b [ "$status" -eq 1 ] echo $output - regex='.*Empty password. Verify whether the .n3drPass..*has been defined in ~/.n3dr.yaml' + regex='.*http://localhost:99999/service/rest/v1/repositories giving up after 6 attempts' [[ "$output" =~ $regex ]] } \ No newline at end of file