Skip to content

Commit

Permalink
Merge pull request #142 from 030/139-anonymous
Browse files Browse the repository at this point in the history
[GH-139] Anonymous mode.
  • Loading branch information
030 authored Jul 18, 2020
2 parents ce83b4b + dbdb17f commit fa943e0
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 24 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -201,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
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down
18 changes: 9 additions & 9 deletions cli/req.go
Original file line number Diff line number Diff line change
@@ -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.Debug("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.Debug("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)
Expand Down
3 changes: 0 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 14 additions & 4 deletions integration-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -158,6 +167,7 @@ main(){
password
files
upload
anonymous
backup
repositories
regex
Expand Down
4 changes: 2 additions & 2 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: n3dr # you probably want to 'snapcraft register <name>'
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.
Expand Down Expand Up @@ -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"
8 changes: 3 additions & 5 deletions tests.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]]
}

0 comments on commit fa943e0

Please sign in to comment.