pre-commit + stricter formatting + pre-commit fixes

This commit is contained in:
David Dworken 2022-04-07 21:40:22 -07:00
parent c2465d7c99
commit f2e6de2eb3
9 changed files with 59 additions and 44 deletions

5
.errcheck_excludes.txt Normal file
View File

@ -0,0 +1,5 @@
(net/http.ResponseWriter).Write
(*gorm.io/gorm.DB).AutoMigrate
os.Setenv
(*os.File).Close
(io.ReadCloser).Close

16
.pre-commit-config.yaml Normal file
View File

@ -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

View File

@ -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
}

View File

@ -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

View File

@ -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 {

View File

@ -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)
}

View File

@ -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 {

View File

@ -109,5 +109,4 @@ func TestESubmitThenQuery(t *testing.T) {
if len(retrievedEntries) != 2 {
t.Fatalf("Expected to retrieve 2 entries, found %d", len(retrievedEntries))
}
}

View File

@ -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) {