Skip to content

Commit

Permalink
Merge pull request #205 from hashcloak/add-lint-tools
Browse files Browse the repository at this point in the history
utils: add lint tools
  • Loading branch information
sc0Vu authored Nov 28, 2023
2 parents 0629f3e + f9fac7e commit 31c2e56
Show file tree
Hide file tree
Showing 25 changed files with 1,665 additions and 90 deletions.
5 changes: 4 additions & 1 deletion client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ default: lint test

.PHONY: lint
lint:
# go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.44.0
# cd ../tools && go install github.com/golangci/golangci-lint/cmd/golangci-lint
# $(GOPATH)/bin/golangci-lint run -e gosec ./... --timeout=2m
# cd ../ test && go install honnef.co/go/tools/cmd/[email protected]
# $(GOPATH)/bin/staticcheck ./...
go vet ./...
go fmt ./...
go mod tidy

Expand Down
6 changes: 3 additions & 3 deletions client/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ func (tcCfg *Katzenmint) validate() error {
return err
}
if tcCfg.PrimaryAddress == "" {
return errors.New("Primary address is missing")
return errors.New("primary address is missing")
}
if tcCfg.DatabaseName == "" || tcCfg.DatabaseDir == "" {
return errors.New("Database name or directory is missing")
return errors.New("database name or directory is missing")
}
if tcCfg.RPCAddress == "" {
return errors.New("RPC address is missing")
return errors.New("rpc address is missing")
}
return nil
}
Expand Down
8 changes: 3 additions & 5 deletions client/pkiclient/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ import (
)

var (
errNotSupported = errors.New("pkiclient: operation not supported")
errHalted = errors.New("pkiclient: client was halted")
errHalted = errors.New("pkiclient: client was halted")

fetchBacklog = 8
lruMaxSize = 8
epochRetrieveInterval = 3 * time.Second
fetchBacklog = 8
lruMaxSize = 8
)

type cacheEntry struct {
Expand Down
7 changes: 5 additions & 2 deletions genconfig/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ default: lint test
# Exclude S1034: assigning the result of this type assertion to a variable (switch cfg := cfg.(type)) could eliminate type assertions in switch cases
.PHONY: lint
lint:
# go get github.com/golangci/golangci-lint/cmd/[email protected]
# $(GOPATH)/bin/golangci-lint run -e gosec -e S1034 ./... --timeout 2m
# cd ../tools && go install github.com/golangci/golangci-lint/cmd/golangci-lint
# $(GOPATH)/bin/golangci-lint run -e gosec ./... --timeout=2m
# cd ../ test && go install honnef.co/go/tools/cmd/[email protected]
# $(GOPATH)/bin/staticcheck ./...
go vet ./...
go fmt ./...
go mod tidy

Expand Down
23 changes: 11 additions & 12 deletions genconfig/cmd/gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ type katzenpost struct {
currency int
mainnet bool

authIdentity *eddsa.PrivateKey
authPubIdentity string

nodeConfigs []*sConfig.Config
Expand Down Expand Up @@ -627,9 +626,9 @@ func configName(cfg interface{}) string {
}

func identifier(cfg interface{}) string {
switch cfg.(type) {
switch cfg := cfg.(type) {
case *sConfig.Config:
return cfg.(*sConfig.Config).Server.Identifier
return cfg.Server.Identifier
default:
log.Fatalf("identifier() passed unexpected type")
return ""
Expand All @@ -654,12 +653,12 @@ func saveCfg(outputDir string, cfg interface{}) error {
}

// links between mix and providers
func (s *katzenpost) spk(a *sConfig.Config) *eddsa.PublicKey {
priv := filepath.Join(s.outputDir, a.Server.Identifier, "identity.private.pem")
public := filepath.Join(s.outputDir, a.Server.Identifier, "identity.public.pem")
idKey, err := eddsa.Load(priv, public, rand.Reader)
if err != nil {
panic(err)
}
return idKey.PublicKey()
}
// func (s *katzenpost) spk(a *sConfig.Config) *eddsa.PublicKey {
// priv := filepath.Join(s.outputDir, a.Server.Identifier, "identity.private.pem")
// public := filepath.Join(s.outputDir, a.Server.Identifier, "identity.public.pem")
// idKey, err := eddsa.Load(priv, public, rand.Reader)
// if err != nil {
// panic(err)
// }
// return idKey.PublicKey()
// }
7 changes: 5 additions & 2 deletions katzenmint/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ default: lint test

.PHONY: lint
lint:
# go get github.com/golangci/golangci-lint/cmd/[email protected]
# $(GOPATH)/bin/golangci-lint run --timeout 2m0s -e gosec ./...
# cd ../tools && go install github.com/golangci/golangci-lint/cmd/golangci-lint
# $(GOPATH)/bin/golangci-lint run -e gosec ./... --timeout=2m
# cd ../ test && go install honnef.co/go/tools/cmd/[email protected]
# $(GOPATH)/bin/staticcheck ./...
go vet ./...
go fmt ./...
go mod tidy

Expand Down
2 changes: 1 addition & 1 deletion katzenmint/cmd/katzenmint/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func joinNetwork(config *cfg.Config) error {
func registerValidator(cmd *cobra.Command, args []string) error {
_, config, err := initConfig()
if err != nil {
return fmt.Errorf("failed to load config: %v\n", err)
return fmt.Errorf("failed to load config: %v", err)
}
err = joinNetwork(config)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion katzenmint/cmd/katzenmint/showNodeId.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
func showNodeID(cmd *cobra.Command, args []string) error {
_, config, err := initConfig()
if err != nil {
return fmt.Errorf("failed to load config: %v\n", err)
return fmt.Errorf("failed to load config: %v", err)
}
nodeKey, err := p2p.LoadNodeKey(config.NodeKeyFile())
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions katzenmint/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
)

type KatzenmintError struct {
error

Code uint32
Msg string
}
Expand Down
44 changes: 22 additions & 22 deletions katzenmint/s11n/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func VerifyAndParseDescriptor(verifier cert.Verifier, b []byte, epoch uint64) (*
return nil, err
}
if len(signatures) != 1 {
return nil, fmt.Errorf("Expected 1 signature, got: %v", len(signatures))
return nil, fmt.Errorf("expected 1 signature, got: %v", len(signatures))
}

// Verify that the descriptor is signed by the verifier.
Expand All @@ -129,7 +129,7 @@ func VerifyAndParseDescriptor(verifier cert.Verifier, b []byte, epoch uint64) (*

// Ensure the descriptor is well formed.
if d.Version != nodeDescriptorVersion {
return nil, fmt.Errorf("Invalid Descriptor Version: '%v'", d.Version)
return nil, fmt.Errorf("invalid Descriptor Version: '%v'", d.Version)
}
if err = IsDescriptorWellFormed(&d.MixDescriptor, epoch); err != nil {
return nil, err
Expand All @@ -142,38 +142,38 @@ func VerifyAndParseDescriptor(verifier cert.Verifier, b []byte, epoch uint64) (*
// a PKI Document.
func IsDescriptorWellFormed(d *pki.MixDescriptor, epoch uint64) error {
if d.Name == "" {
return fmt.Errorf("Descriptor missing Name")
return fmt.Errorf("descriptor missing Name")
}
if len(d.Name) > constants.NodeIDLength {
return fmt.Errorf("Descriptor Name '%v' exceeds max length", d.Name)
return fmt.Errorf("descriptor Name '%v' exceeds max length", d.Name)
}
if d.LinkKey == nil {
return fmt.Errorf("Descriptor missing LinkKey")
return fmt.Errorf("descriptor missing LinkKey")
}
if d.IdentityKey == nil {
return fmt.Errorf("Descriptor missing IdentityKey")
return fmt.Errorf("descriptor missing IdentityKey")
}
if d.MixKeys[epoch] == nil {
return fmt.Errorf("Descriptor missing MixKey[%v]", epoch)
return fmt.Errorf("descriptor missing MixKey[%v]", epoch)
}
for e := range d.MixKeys {
// TODO: Should this check that the epochs in MixKey are sequential?
if e < epoch || e >= epoch+3 {
return fmt.Errorf("Descriptor contains MixKey for invalid epoch: %v", d)
return fmt.Errorf("descriptor contains MixKey for invalid epoch: %v", d)
}
}
if len(d.Addresses) == 0 {
return fmt.Errorf("Descriptor missing Addresses")
return fmt.Errorf("descriptor missing Addresses")
}
for transport, addrs := range d.Addresses {
if len(addrs) == 0 {
return fmt.Errorf("Descriptor contains empty Address list for transport '%v'", transport)
return fmt.Errorf("descriptor contains empty Address list for transport '%v'", transport)
}

var expectedIPVer int
switch transport {
case pki.TransportInvalid:
return fmt.Errorf("Descriptor contains invalid Transport")
return fmt.Errorf("descriptor contains invalid Transport")
case pki.TransportTCPv4:
expectedIPVer = 4
case pki.TransportTCPv6:
Expand All @@ -182,7 +182,7 @@ func IsDescriptorWellFormed(d *pki.MixDescriptor, epoch uint64) error {
// Unknown transports are only supported between the client and
// provider.
if d.Layer != pki.LayerProvider {
return fmt.Errorf("Non-provider published Transport '%v'", transport)
return fmt.Errorf("non-provider published Transport '%v'", transport)
}
if transport != pki.TransportTCP {
// Ignore transports that don't have validation logic.
Expand All @@ -194,44 +194,44 @@ func IsDescriptorWellFormed(d *pki.MixDescriptor, epoch uint64) error {
for _, v := range addrs {
h, p, err := net.SplitHostPort(v)
if err != nil {
return fmt.Errorf("Descriptor contains invalid address ['%v']'%v': %v", transport, v, err)
return fmt.Errorf("descriptor contains invalid address ['%v']'%v': %v", transport, v, err)
}
if len(h) == 0 {
return fmt.Errorf("Descriptor contains invalid address ['%v']'%v'", transport, v)
return fmt.Errorf("descriptor contains invalid address ['%v']'%v'", transport, v)
}
if port, err := strconv.ParseUint(p, 10, 16); err != nil {
return fmt.Errorf("Descriptor contains invalid address ['%v']'%v': %v", transport, v, err)
return fmt.Errorf("descriptor contains invalid address ['%v']'%v': %v", transport, v, err)
} else if port == 0 {
return fmt.Errorf("Descriptor contains invalid address ['%v']'%v': port is 0", transport, v)
return fmt.Errorf("descriptor contains invalid address ['%v']'%v': port is 0", transport, v)
}
switch expectedIPVer {
case 4, 6:
if ver, err := getIPVer(h); err != nil {
return fmt.Errorf("Descriptor contains invalid address ['%v']'%v': %v", transport, v, err)
return fmt.Errorf("descriptor contains invalid address ['%v']'%v': %v", transport, v, err)
} else if ver != expectedIPVer {
return fmt.Errorf("Descriptor contains invalid address ['%v']'%v': IP version mismatch", transport, v)
return fmt.Errorf("descriptor contains invalid address ['%v']'%v': IP version mismatch", transport, v)
}
default:
// This must be TransportTCP or something else that supports
// "sensible" DNS style hostnames. Validate that they are
// at least somewhat well formed.
if _, err := idna.Lookup.ToASCII(h); err != nil {
return fmt.Errorf("Descriptor contains invalid address ['%v']'%v': %v", transport, v, err)
return fmt.Errorf("descriptor contains invalid address ['%v']'%v': %v", transport, v, err)
}
}
}
}
if len(d.Addresses[pki.TransportTCPv4]) == 0 {
return fmt.Errorf("Descriptor contains no TCPv4 addresses")
return fmt.Errorf("descriptor contains no TCPv4 addresses")
}
switch d.Layer {
case 0:
if d.Kaetzchen != nil {
return fmt.Errorf("Descriptor contains Kaetzchen when a mix")
return fmt.Errorf("descriptor contains Kaetzchen when a mix")
}
case pki.LayerProvider:
if err := validateKaetzchen(d.Kaetzchen); err != nil {
return fmt.Errorf("Descriptor contains invalid Kaetzchen block: %v", err)
return fmt.Errorf("descriptor contains invalid Kaetzchen block: %v", err)
}
default: // it's ok
}
Expand Down
27 changes: 13 additions & 14 deletions katzenmint/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package katzenmint

import (
"bytes"
"encoding/binary"
"math/rand"
"sort"
Expand Down Expand Up @@ -36,19 +35,19 @@ func storageKey(keyPrefix string, keyID []byte, epoch uint64) (key []byte) {
return
}

func unpackStorageKey(key []byte) (keyID []byte, epoch uint64) {
pre := bytes.Index(key, []byte(":"))
post := bytes.LastIndex(key, []byte(":"))
if pre < 0 || post <= pre {
return nil, 0
}
epoch, read := binary.Uvarint(DecodeHex(string(key[pre+1 : post])))
keyID = DecodeHex(string(key[post+1:]))
if read <= 0 {
return nil, 0
}
return
}
// func unpackStorageKey(key []byte) (keyID []byte, epoch uint64) {
// pre := bytes.Index(key, []byte(":"))
// post := bytes.LastIndex(key, []byte(":"))
// if pre < 0 || post <= pre {
// return nil, 0
// }
// epoch, read := binary.Uvarint(DecodeHex(string(key[pre+1 : post])))
// keyID = DecodeHex(string(key[post+1:]))
// if read <= 0 {
// return nil, 0
// }
// return
// }

func sortNodesByPublicKey(nodes []*descriptor) {
dTos := func(d *descriptor) string {
Expand Down
7 changes: 5 additions & 2 deletions plugin/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ default: lint test

.PHONY: lint
lint:
# go get github.com/golangci/golangci-lint/cmd/[email protected]
# $(GOPATH)/bin/golangci-lint run -e gosec ./... --timeout 2m
# cd ../tools && go install github.com/golangci/golangci-lint/cmd/golangci-lint
# $(GOPATH)/bin/golangci-lint run -e gosec ./... --timeout=2m
# cd ../ test && go install honnef.co/go/tools/cmd/[email protected]
# $(GOPATH)/bin/staticcheck ./...
go vet ./...
go fmt ./...
go mod tidy

Expand Down
7 changes: 5 additions & 2 deletions server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ default: lint test

.PHONY: lint
lint:
# go get github.com/golangci/golangci-lint/cmd/[email protected]
# $(GOPATH)/bin/golangci-lint run -e gosec ./... --timeout 2m
# cd ../tools && go install github.com/golangci/golangci-lint/cmd/golangci-lint
# $(GOPATH)/bin/golangci-lint run -e gosec ./... --timeout=2m
# cd ../ test && go install honnef.co/go/tools/cmd/[email protected]
# $(GOPATH)/bin/staticcheck ./...
go vet ./...
go fmt ./...
go mod tidy

Expand Down
1 change: 0 additions & 1 deletion server/cborplugin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
// as described in our Kaetzchen specification document:
//
// https://github.com/katzenpost/docs/blob/master/specs/kaetzchen.rst
//
package cborplugin

import (
Expand Down
5 changes: 2 additions & 3 deletions server/cmd/meson-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package main

import (
Expand Down Expand Up @@ -61,10 +60,10 @@ func main() {
}

// Setup the signal handling.
haltCh := make(chan os.Signal)
haltCh := make(chan os.Signal, 1)
signal.Notify(haltCh, os.Interrupt, syscall.SIGTERM) // nolint

rotateCh := make(chan os.Signal)
rotateCh := make(chan os.Signal, 1)
signal.Notify(rotateCh, syscall.SIGHUP) // nolint

// Start up the server.
Expand Down
4 changes: 2 additions & 2 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ func Store(cfg *Config, fileName string) error {
// returns the Config.
func Load(b []byte) (*Config, error) {
if b == nil {
return nil, errors.New("No nil buffer as config file")
return nil, errors.New("no nil buffer as config file")
}

cfg := new(Config)
Expand All @@ -918,7 +918,7 @@ func Load(b []byte) (*Config, error) {
return nil, err
}
if undecoded := md.Undecoded(); len(undecoded) != 0 {
return nil, fmt.Errorf("config: Undecoded keys in config file: %v", undecoded)
return nil, fmt.Errorf("undecoded keys in config file: %v", undecoded)
}
if err := cfg.FixupAndValidate(); err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 31c2e56

Please sign in to comment.