Skip to content

Commit

Permalink
Update dependencies and cmd pacakge structure
Browse files Browse the repository at this point in the history
  • Loading branch information
janos committed Apr 21, 2022
1 parent d9b2d68 commit 58afa03
Show file tree
Hide file tree
Showing 21 changed files with 429 additions and 514 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15
go-version: 1.18

- name: Checkout
uses: actions/checkout@v1
Expand All @@ -32,8 +32,10 @@ jobs:
${{ runner.OS }}-
- name: Lint
if: matrix.os == 'ubuntu-latest'
run: make lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.45.2
args: --timeout 10m

- name: Vet
if: matrix.os == 'ubuntu-latest'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: 1.15
go-version: 1.18

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v1
Expand Down
10 changes: 4 additions & 6 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ builds:
- -trimpath

ldflags:
- -s -w -X resenje.org/compromised.version={{.Version}} -X resenje.org/compromised.commit={{.ShortCommit}}
- -s -w -X resenje.org/compromised.version={{.Version}}

env:
- CGO_ENABLED=0
Expand All @@ -22,15 +22,13 @@ builds:

goarch:
- amd64
- 386
- '386'
- arm64
- arm

ignore:
- goos: darwin
goarch: 386
- goos: darwin
goarch: arm64
goarch: '386'
- goos: darwin
goarch: arm
- goos: windows
Expand All @@ -39,6 +37,6 @@ builds:
goarch: arm

archives:
- name_template: "{{ tolower .ProjectName }}-{{ tolower .Os }}-{{ tolower .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
- name_template: "{{ tolower .ProjectName }}-{{ tolower .Os }}-{{ tolower .Arch }}{{ end }}"

format: binary
11 changes: 3 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

GO ?= go
GOLANGCI_LINT ?= $$($(GO) env GOPATH)/bin/golangci-lint
GOLANGCI_LINT_VERSION ?= v1.32.2

COMMIT ?= "$(shell git describe --long --dirty --always --match "" || true)"
LDFLAGS ?= -s -w -X resenje.org/compromised.commit="$(COMMIT)"
LDFLAGS ?= -s -w
TAGS += timetzdata

.PHONY: all
all: binary lint vet test
Expand All @@ -26,13 +25,9 @@ dist:
mkdir $@

.PHONY: lint
lint: linter
lint:
$(GOLANGCI_LINT) run

.PHONY: linter
linter:
test -f $(GOLANGCI_LINT) || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$($(GO) env GOPATH)/bin $(GOLANGCI_LINT_VERSION)

.PHONY: vet
vet:
$(GO) vet ./...
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ You may need additional privileges to write to `/usr/local/bin`, but the file ca
Supported operating systems and architectures:

- macOS 64bit `darwin-amd64`
- macOS 64bit `darwin-arm64`
- Linux 64bit `linux-amd64`
- Linux 32bit `linux-386`
- Linux ARM 64bit `linux-arm64`
Expand All @@ -44,7 +45,7 @@ Supported operating systems and architectures:
This tool is implemented using the [Go programming language](https://golang.org) and can also be installed by issuing a `go get` command:

```sh
go get -u resenje.org/compromised/cmd/compromised
go install resenje.org/compromised/cmd/compromised@latest
```

## Usage
Expand Down Expand Up @@ -129,7 +130,7 @@ OPTIONS
Download Pwned passwords SHA1 ordered by hash 7z file from https://haveibeenpwned.com/Passwords. This file is several gigabytes long (version 6 is 10.1GB) so make sure that you have enough disk space.

```sh
wget https://downloads.pwnedpasswords.com/passwords/pwned-passwords-sha1-ordered-by-hash-v6.7z
wget https://downloads.pwnedpasswords.com/passwords/pwned-passwords-sha1-ordered-by-hash-v8.7z
```

Extract a textual file from the downloaded 7z archive. This file is roughly twice in size of 7z archive that contains it, around 24G for version 6. Feel free to remove the 7z archive.
Expand Down Expand Up @@ -213,7 +214,7 @@ The service can be configured with environment variables as well. Variable names
For variables in `compromised.yaml`, capitalize all letters, replace `-` with `_` and prepend `COMPROMISED_` prefix. For example, to set `passwords-db`, the environment variable is `COMPROMISED_PASSWORDS_DB`:

```sh
COMPROMISED_PASSWORDS_DB=/path/to/passwrods-db compromised
COMPROMISED_PASSWORDS_DB=/path/to/passwords-db compromised
```

### Starting the service
Expand All @@ -231,7 +232,7 @@ cat /etc/compromised/compromised.yaml
```

```yaml
passwords-db: /data/storage/compromised/passwrods
passwords-db: /data/storage/compromised/passwords
```
To write logs to files on local filesystem:
Expand All @@ -241,7 +242,7 @@ cat /etc/compromised/compromised.yaml
```

```yaml
passwords-db: /data/storage/compromised/passwrods
passwords-db: /data/storage/compromised/passwords
log-dir: /data/log/compromised
```
Expand Down
25 changes: 8 additions & 17 deletions cmd/compromised/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"path/filepath"
"strings"

"resenje.org/x/application"
xconfig "resenje.org/x/config"

"resenje.org/compromised/cmd/compromised/config"
)
Expand All @@ -26,14 +26,15 @@ func init() {
cfg.Register(config.Name, options)
}

var cfg = application.NewConfig(config.Name)
var cfg = xconfig.New(config.Name)

func configCmd() {
func configCmd() error {
// Print loaded configuration.
fmt.Print(cfg.String())
return nil
}

func updateConfig() {
func updateConfig() error {
if *configDir == "" {
*configDir = os.Getenv(strings.ToUpper(config.Name) + "_CONFIGDIR")
}
Expand All @@ -47,19 +48,9 @@ func updateConfig() {
if d, err := os.UserConfigDir(); err == nil {
cfg.Dirs = append(cfg.Dirs, filepath.Join(d, config.Name))
}
if err := cfg.Load(); err != nil {
fmt.Fprintln(os.Stderr, "Error: ", err)
os.Exit(2)
}
return cfg.Load()
}

func verifyAndPrepareConfig() {
if err := cfg.VerifyAndPrepare(); err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
if e, ok := err.(*application.HelpError); ok {
fmt.Println()
fmt.Println(e.Help)
}
os.Exit(2)
}
func verifyAndPrepareConfig() error {
return cfg.VerifyAndPrepare()
}
2 changes: 1 addition & 1 deletion cmd/compromised/config/compromised.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func NewCompromisedOptions() *CompromisedOptions {
Listen: ":8080",
ListenInternal: "127.0.0.1:6060",
Headers: map[string]string{
"Server": Name + "/" + compromised.Version,
"Server": Name + "/" + compromised.Version(),
"X-Frame-Options": "SAMEORIGIN",
},
PasswordsDB: "",
Expand Down
18 changes: 0 additions & 18 deletions cmd/compromised/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,10 @@
// that define optional parameters for different parts of the service.
package config

import (
"os"
"path/filepath"
)

var (
// Name is the name of the service.
Name = "compromised"

// BaseDir is the directory where the service's executable is located.
BaseDir = func() string {
path, err := os.Executable()
if err != nil {
panic(err)
}
path, err = filepath.EvalSymlinks(path)
if err != nil {
panic(err)
}
return filepath.Dir(path)
}()

// Dir is default directory where configuration files are located.
Dir = "/etc/compromised"
)
11 changes: 3 additions & 8 deletions cmd/compromised/debug_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,22 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !windows
// +build !windows

package main

import (
"fmt"
"os"
"syscall"

"resenje.org/daemon"
)

func debugDumpCmd() {
func debugDumpCmd() error {
// Send SIGUSR1 signal to a daemonized process.
// Service is able to receive the signal and dump debugging
// information to files or stderr.
err := (&daemon.Daemon{
return (&daemon.Daemon{
PidFileName: options.PidFileName,
}).Signal(syscall.SIGUSR1)
if err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(2)
}
}
9 changes: 4 additions & 5 deletions cmd/compromised/debug_dump_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build windows
// +build windows

package main

import (
"fmt"
"os"
"errors"
)

func debugDumpCmd() {
fmt.Fprintln(os.Stderr, "Debug dump is not supported on Windows")
os.Exit(2)
func debugDumpCmd() error {
return errors.New("debug dump is not supported on Windows")
}
7 changes: 2 additions & 5 deletions cmd/compromised/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package main

import (
"fmt"
"os"
)

var (
Expand Down Expand Up @@ -51,8 +50,6 @@ func helpCmd() {
cli.Usage()
}

func helpUnknownCmd(cmd string) {
fmt.Fprintln(os.Stderr, "unknown command:", cmd)
cli.Usage()
os.Exit(2)
func helpUnknownCmd(cmd string) error {
return fmt.Errorf("unknown command: %s", cmd)
}
20 changes: 8 additions & 12 deletions cmd/compromised/index_passwords.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
filepasswords "resenje.org/compromised/pkg/passwords/file"
)

func indexPasswordsCmd() {
func indexPasswordsCmd() error {
cli := flag.NewFlagSet("index-passwords", flag.ExitOnError)

minHashCount := cli.Uint64("min-hash-count", 1, "Skip hashes with counts lower than specified with this flag.")
Expand All @@ -34,27 +34,23 @@ OPTIONS
}

if err := cli.Parse(os.Args[2:]); err != nil {
cli.Usage()
os.Exit(2)
return err
}

if *help {
cli.Usage()
return
return nil
}

if cli.NArg() != 2 {
fmt.Fprintln(os.Stderr, "index-passwords command requires two arguments: input filename and output directory")
cli.Usage()
os.Exit(2)
return fmt.Errorf("index-passwords command requires two arguments: input filename and output directory")
}

if _, err := filepasswords.Index(cli.Arg(0), cli.Arg(1), &filepasswords.IndexOptions{
_, err := filepasswords.Index(cli.Arg(0), cli.Arg(1), &filepasswords.IndexOptions{
MinHashCount: *minHashCount,
ShardCount: *shardCount,
HashCounting: filepasswords.HashCounting(*hashCounting),
}); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
})

return err
}
Loading

0 comments on commit 58afa03

Please sign in to comment.