From f2e6de2eb33cc48cc67a2a56476753ca97d4e5af Mon Sep 17 00:00:00 2001 From: David Dworken Date: Thu, 7 Apr 2022 21:40:22 -0700 Subject: [PATCH] pre-commit + stricter formatting + pre-commit fixes --- .errcheck_excludes.txt | 5 +++++ .pre-commit-config.yaml | 16 ++++++++++++++++ client/client.go | 2 +- client/client_test.go | 2 +- client/data/data.go | 29 ++++++++++++++--------------- client/lib/lib.go | 29 +++++++++++++---------------- server/server.go | 2 +- server/server_test.go | 1 - shared/testutils.go | 17 ++++++++--------- 9 files changed, 59 insertions(+), 44 deletions(-) create mode 100644 .errcheck_excludes.txt create mode 100644 .pre-commit-config.yaml diff --git a/.errcheck_excludes.txt b/.errcheck_excludes.txt new file mode 100644 index 0000000..a147585 --- /dev/null +++ b/.errcheck_excludes.txt @@ -0,0 +1,5 @@ +(net/http.ResponseWriter).Write +(*gorm.io/gorm.DB).AutoMigrate +os.Setenv +(*os.File).Close +(io.ReadCloser).Close diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..3f787b3 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,16 @@ +repos: + - repo: https://github.com/Bahjat/pre-commit-golang + rev: a4be1d0f860565649a450a8d480e541844c14a07 + hooks: + - id: go-fmt-import + - id: go-vet + - id: gofumpt # requires github.com/mvdan/gofumpt + - id: go-static-check # install https://staticcheck.io/docs/ + - id: golangci-lint # requires github.com/golangci/golangci-lint + - repo: local + hooks: + - id: go-errcheck + name: go-errcheck + entry: errcheck -exclude .errcheck_excludes.txt ./... + language: system + pass_filenames: false diff --git a/client/client.go b/client/client.go index 237d8be..396bb60 100644 --- a/client/client.go +++ b/client/client.go @@ -115,7 +115,7 @@ func displayBannerIfSet() error { return fmt.Errorf("failed to read /api/v1/banner response body: %v", err) } if len(data) > 0 { - fmt.Printf(string(data)) + fmt.Print(string(data)) } return nil } diff --git a/client/client_test.go b/client/client_test.go index 3a5f587..5da6b05 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -17,7 +17,7 @@ import ( ) func RunInteractiveBashCommands(t *testing.T, script string) string { - shared.Check(t, ioutil.WriteFile("/tmp/hishtory-test-in.sh", []byte("set -euo pipefail\n"+script), 0600)) + shared.Check(t, ioutil.WriteFile("/tmp/hishtory-test-in.sh", []byte("set -euo pipefail\n"+script), 0o600)) cmd := exec.Command("bash", "-i") cmd.Stdin = strings.NewReader(script) var out bytes.Buffer diff --git a/client/data/data.go b/client/data/data.go index 8b20891..8333a43 100644 --- a/client/data/data.go +++ b/client/data/data.go @@ -1,11 +1,6 @@ package data import ( - "fmt" - "io" - "strings" - "time" - "crypto/aes" "crypto/cipher" "crypto/hmac" @@ -13,6 +8,10 @@ import ( "crypto/sha256" "encoding/base64" "encoding/json" + "fmt" + "io" + "strings" + "time" "github.com/ddworken/hishtory/shared" "github.com/google/uuid" @@ -20,9 +19,9 @@ import ( ) const ( - KDF_USER_ID = "user_id" - KDF_DEVICE_ID = "device_id" - KDF_ENCRYPTION_KEY = "encryption_key" + KdfUserID = "user_id" + KdfDeviceID = "device_id" + KdfEncryptionKey = "encryption_key" ) type HistoryEntry struct { @@ -42,11 +41,11 @@ func sha256hmac(key, additionalData string) []byte { } func UserId(key string) string { - return base64.URLEncoding.EncodeToString(sha256hmac(key, KDF_USER_ID)) + return base64.URLEncoding.EncodeToString(sha256hmac(key, KdfUserID)) } func EncryptionKey(userSecret string) []byte { - return sha256hmac(userSecret, KDF_ENCRYPTION_KEY) + return sha256hmac(userSecret, KdfEncryptionKey) } func makeAead(userSecret string) (cipher.AEAD, error) { @@ -65,11 +64,11 @@ func makeAead(userSecret string) (cipher.AEAD, error) { func Encrypt(userSecret string, data, additionalData []byte) ([]byte, []byte, error) { aead, err := makeAead(userSecret) if err != nil { - return []byte{}, []byte{}, fmt.Errorf("Failed to make AEAD: %v", err) + return []byte{}, []byte{}, fmt.Errorf("failed to make AEAD: %v", err) } nonce := make([]byte, 12) if _, err := io.ReadFull(rand.Reader, nonce); err != nil { - return []byte{}, []byte{}, fmt.Errorf("Failed to read a nonce: %v", err) + return []byte{}, []byte{}, fmt.Errorf("failed to read a nonce: %v", err) } ciphertext := aead.Seal(nil, nonce, data, additionalData) _, err = aead.Open(nil, nonce, ciphertext, additionalData) @@ -82,11 +81,11 @@ func Encrypt(userSecret string, data, additionalData []byte) ([]byte, []byte, er func Decrypt(userSecret string, data, additionalData, nonce []byte) ([]byte, error) { aead, err := makeAead(userSecret) if err != nil { - return []byte{}, fmt.Errorf("Failed to make AEAD: %v", err) + return []byte{}, fmt.Errorf("failed to make AEAD: %v", err) } plaintext, err := aead.Open(nil, nonce, data, additionalData) if err != nil { - return []byte{}, fmt.Errorf("Failed to decrypt: %v", err) + return []byte{}, fmt.Errorf("failed to decrypt: %v", err) } return plaintext, nil } @@ -112,7 +111,7 @@ func EncryptHistoryEntry(userSecret string, entry HistoryEntry) (shared.EncHisto func DecryptHistoryEntry(userSecret string, entry shared.EncHistoryEntry) (HistoryEntry, error) { if entry.UserId != UserId(userSecret) { - return HistoryEntry{}, fmt.Errorf("Refusing to decrypt history entry with mismatching UserId") + return HistoryEntry{}, fmt.Errorf("refusing to decrypt history entry with mismatching UserId") } plaintext, err := Decrypt(userSecret, entry.EncryptedData, []byte(UserId(userSecret)), entry.Nonce) if err != nil { diff --git a/client/lib/lib.go b/client/lib/lib.go index f4fcf7d..67856c6 100644 --- a/client/lib/lib.go +++ b/client/lib/lib.go @@ -1,9 +1,8 @@ package lib import ( - _ "embed" - "bytes" + _ "embed" // for embedding config.sh "encoding/json" "fmt" "io" @@ -31,10 +30,8 @@ import ( "github.com/ddworken/hishtory/shared" ) -var ( - //go:embed config.sh - CONFIG_SH_CONTENTS string -) +//go:embed config.sh +var ConfigShContents string func getCwd() (string, error) { cwd, err := os.Getwd() @@ -247,11 +244,11 @@ func SetConfig(config ClientConfig) error { return fmt.Errorf("failed to retrieve homedir: %v", err) } clientDir := path.Join(homedir, shared.HISHTORY_PATH) - err = os.MkdirAll(clientDir, 0744) + err = os.MkdirAll(clientDir, 0o744) if err != nil { return fmt.Errorf("failed to create ~/.hishtory/ folder: %v", err) } - err = os.WriteFile(path.Join(homedir, shared.HISHTORY_PATH, shared.CONFIG_PATH), serializedConfig, 0600) + err = os.WriteFile(path.Join(homedir, shared.HISHTORY_PATH, shared.CONFIG_PATH), serializedConfig, 0o600) if err != nil { return fmt.Errorf("failed to write config: %v", err) } @@ -296,7 +293,7 @@ func Install() error { return fmt.Errorf("failed to get user's home directory: %v", err) } clientDir := path.Join(homedir, shared.HISHTORY_PATH) - err = os.MkdirAll(clientDir, 0744) + err = os.MkdirAll(clientDir, 0o744) if err != nil { return fmt.Errorf("failed to create folder for hishtory binary: %v", err) } @@ -319,7 +316,7 @@ func Install() error { func configureBashrc(homedir, binaryPath string) error { // Create the file we're going to source in our bashrc. Do this no matter what in case there are updates to it. bashConfigPath := path.Join(filepath.Dir(binaryPath), "config.sh") - err := ioutil.WriteFile(bashConfigPath, []byte(CONFIG_SH_CONTENTS), 0644) + err := ioutil.WriteFile(bashConfigPath, []byte(ConfigShContents), 0o644) if err != nil { return fmt.Errorf("failed to write config.sh file: %v", err) } @@ -332,7 +329,7 @@ func configureBashrc(homedir, binaryPath string) error { return nil } // Add to bashrc - f, err := os.OpenFile(path.Join(homedir, ".bashrc"), os.O_APPEND|os.O_WRONLY, 0644) + f, err := os.OpenFile(path.Join(homedir, ".bashrc"), os.O_APPEND|os.O_WRONLY, 0o644) if err != nil { return fmt.Errorf("failed to append to bashrc: %v", err) } @@ -353,7 +350,7 @@ func installBinary(homedir string) (string, error) { if err != nil { return "", fmt.Errorf("failed to copy hishtory binary to $PATH: %v", err) } - err = os.Chmod(clientPath, 0700) + err = os.Chmod(clientPath, 0o700) if err != nil { return "", fmt.Errorf("failed to set permissions on hishtory binary: %v", err) } @@ -393,7 +390,7 @@ func Update(url string) error { cmd.Stderr = &stderr err := cmd.Run() if err != nil { - return fmt.Errorf("Failed to download update: %v, stdout=%#v, stderr=%#v", err, stdout.String(), stderr.String()) + return fmt.Errorf("failed to download update: %v, stdout=%#v, stderr=%#v", err, stdout.String(), stderr.String()) } homedir, err := os.UserHomeDir() if err != nil { @@ -401,12 +398,12 @@ func Update(url string) error { } err = syscall.Unlink(path.Join(homedir, shared.HISHTORY_PATH, "hishtory")) if err != nil { - return fmt.Errorf("Failed to unlink %s: %v", path.Join(homedir, shared.HISHTORY_PATH, "hishtory"), err) + return fmt.Errorf("failed to unlink %s: %v", path.Join(homedir, shared.HISHTORY_PATH, "hishtory"), err) } cmd = exec.Command("/tmp/hishtory-client", "install") err = cmd.Run() if err != nil { - return fmt.Errorf("Failed to update: %v", err) + return fmt.Errorf("failed to update: %v", err) } return nil } @@ -423,7 +420,7 @@ func OpenLocalSqliteDb() (*gorm.DB, error) { if err != nil { return nil, fmt.Errorf("failed to get user's home directory: %v", err) } - err = os.MkdirAll(path.Join(homedir, shared.HISHTORY_PATH), 0744) + err = os.MkdirAll(path.Join(homedir, shared.HISHTORY_PATH), 0o744) if err != nil { return nil, fmt.Errorf("failed to create ~/.hishtory dir: %v", err) } diff --git a/server/server.go b/server/server.go index 228b70b..efbd564 100644 --- a/server/server.go +++ b/server/server.go @@ -42,7 +42,7 @@ func apiESubmitHandler(w http.ResponseWriter, r *http.Request) { panic(fmt.Errorf("DB query error: %v", result.Error)) } if len(devices) == 0 { - panic(fmt.Errorf("Found no devices associated with user_id=%s, can't save history entry!", entry.UserId)) + panic(fmt.Errorf("found no devices associated with user_id=%s, can't save history entry", entry.UserId)) } fmt.Printf("apiESubmitHandler: Found %d devices\n", len(devices)) for _, device := range devices { diff --git a/server/server_test.go b/server/server_test.go index 5ad281f..df36af7 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -109,5 +109,4 @@ func TestESubmitThenQuery(t *testing.T) { if len(retrievedEntries) != 2 { t.Fatalf("Expected to retrieve 2 entries, found %d", len(retrievedEntries)) } - } diff --git a/shared/testutils.go b/shared/testutils.go index 5d6ef9b..f33cc24 100644 --- a/shared/testutils.go +++ b/shared/testutils.go @@ -16,8 +16,8 @@ func ResetLocalState(t *testing.T) { t.Fatalf("failed to retrieve homedir: %v", err) } - os.Remove(path.Join(homedir, HISHTORY_PATH, DB_PATH)) - os.Remove(path.Join(homedir, HISHTORY_PATH, CONFIG_PATH)) + _ = os.Remove(path.Join(homedir, HISHTORY_PATH, DB_PATH)) + _ = os.Remove(path.Join(homedir, HISHTORY_PATH, CONFIG_PATH)) } func BackupAndRestore(t *testing.T) func() { @@ -26,11 +26,11 @@ func BackupAndRestore(t *testing.T) func() { t.Fatalf("failed to retrieve homedir: %v", err) } - os.Rename(path.Join(homedir, HISHTORY_PATH, DB_PATH), path.Join(homedir, HISHTORY_PATH, DB_PATH+".bak")) - os.Rename(path.Join(homedir, HISHTORY_PATH, CONFIG_PATH), path.Join(homedir, HISHTORY_PATH, CONFIG_PATH+".bak")) + _ = os.Rename(path.Join(homedir, HISHTORY_PATH, DB_PATH), path.Join(homedir, HISHTORY_PATH, DB_PATH+".bak")) + _ = os.Rename(path.Join(homedir, HISHTORY_PATH, CONFIG_PATH), path.Join(homedir, HISHTORY_PATH, CONFIG_PATH+".bak")) return func() { - os.Rename(path.Join(homedir, HISHTORY_PATH, DB_PATH+".bak"), path.Join(homedir, HISHTORY_PATH, DB_PATH)) - os.Rename(path.Join(homedir, HISHTORY_PATH, CONFIG_PATH+".bak"), path.Join(homedir, HISHTORY_PATH, CONFIG_PATH)) + _ = os.Rename(path.Join(homedir, HISHTORY_PATH, DB_PATH+".bak"), path.Join(homedir, HISHTORY_PATH, DB_PATH)) + _ = os.Rename(path.Join(homedir, HISHTORY_PATH, CONFIG_PATH+".bak"), path.Join(homedir, HISHTORY_PATH, CONFIG_PATH)) } } @@ -68,16 +68,15 @@ func RunTestServer(t *testing.T) func() { } time.Sleep(time.Second * 3) go func() { - cmd.Wait() + _ = cmd.Wait() }() return func() { err := cmd.Process.Kill() if err != nil { t.Fatalf("failed to kill process: %v", err) } - fmt.Println(fmt.Sprintf("stderr=%#v, stdout=%#v", stderr.String(), stdout.String())) + fmt.Printf("stderr=%#v, stdout=%#v\n", stderr.String(), stdout.String()) } - } func Check(t *testing.T, err error) {