All checks were successful
check / check (push) Successful in 2m0s
- Replace .golangci.yml with the canonical strict config (all linters enabled except the standard disable list; lll 88, funlen 80/50, cyclop 15, dupl 100; test files now linted) - Pin the Dockerfile lint stage to golangci/golangci-lint:v2.12.2 by tag and digest (Debian-based) - Fix all ~1550 findings surfaced by the new config: line wrapping, wsl_v5/nlreturn blank lines, noinlineerr splits, err113 sentinel errors, perfsprint/modernize rewrites, goconst constants, thelper, testifylint, noctx CommandContext, testpackage conversions, t.Parallel() where safe, and complexity/dupl helper extraction - Record the change and follow-up items in TODO.md User-visible strings -------------------- No user-visible string changes remain. Every error message this branch composes is byte-identical to the one main composes. The err113 sentinels are shaped so that fmt.Errorf reassembles the original text around them: a sentinel carries the fixed words of the message and the caller supplies the interpolated value in the position it has always occupied. Where the value sits in the middle of the sentence the sentinel therefore holds only a fragment (for example vault.ErrVaultNotFound is "does not exist", composed by its caller as "vault <name> does not exist"); each such sentinel documents the message it participates in. Verified mechanically rather than by inspection: every fmt.Errorf and errors.New call site in both trees was parsed, the Error() text of any sentinel passed to %w substituted in, and the resulting sets of composed message templates compared. All 350 templates main produces are still produced, character for character; the set of messages lost or altered is empty. unlocker list ------------- findUnlockerIDByMetadata now returns (string, error) instead of signalling failure with an empty ID. An unreadable unlockers.d is no longer indistinguishable from "no matching entry", so UnlockersList skips the entry with a warning naming the directory, as it did before the scan was extracted into a helper, rather than emitting a row under a synthesized fallback ID that no unlocker remove or unlocker select can match and that suppresses the current-unlocker marker. The duplicate-check and shell-completion callers skip on the same condition, matching their pre-extraction behavior. Covered by tests in internal/cli/unlockers_list_test.go.
142 lines
3.7 KiB
Go
142 lines
3.7 KiB
Go
package vault
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"path/filepath"
|
|
|
|
"git.eeqj.de/sneak/secret/internal/secret"
|
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
|
"github.com/spf13/afero"
|
|
)
|
|
|
|
// Metadata is an alias for secret.VaultMetadata
|
|
type Metadata = secret.VaultMetadata
|
|
|
|
// UnlockerMetadata is an alias for secret.UnlockerMetadata
|
|
type UnlockerMetadata = secret.UnlockerMetadata
|
|
|
|
// SecretMetadata is an alias for secret.Metadata
|
|
type SecretMetadata = secret.Metadata
|
|
|
|
// Configuration is an alias for secret.Configuration
|
|
type Configuration = secret.Configuration
|
|
|
|
// ComputeDoubleSHA256 computes the double SHA256 hash of data and returns it as hex
|
|
func ComputeDoubleSHA256(data []byte) string {
|
|
firstHash := sha256.Sum256(data)
|
|
secondHash := sha256.Sum256(firstHash[:])
|
|
|
|
return hex.EncodeToString(secondHash[:])
|
|
}
|
|
|
|
// GetNextDerivationIndex finds the next available derivation index for a given mnemonic
|
|
// by deriving the public key for index 0 and using its hash to identify related vaults
|
|
func GetNextDerivationIndex(
|
|
fs afero.Fs, stateDir string, mnemonic string,
|
|
) (uint32, error) {
|
|
// First, derive the public key for index 0 to get our identifier
|
|
identity0, err := agehd.DeriveIdentity(mnemonic, 0)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("failed to derive identity for index 0: %w", err)
|
|
}
|
|
|
|
pubKeyHash := ComputeDoubleSHA256([]byte(identity0.Recipient().String()))
|
|
|
|
vaultsDir := filepath.Join(stateDir, "vaults.d")
|
|
|
|
// Check if vaults directory exists
|
|
exists, err := afero.DirExists(fs, vaultsDir)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("failed to check if vaults directory exists: %w", err)
|
|
}
|
|
|
|
if !exists {
|
|
// No vaults yet, start with index 0
|
|
return 0, nil
|
|
}
|
|
|
|
// Read all vault directories
|
|
entries, err := afero.ReadDir(fs, vaultsDir)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("failed to read vaults directory: %w", err)
|
|
}
|
|
|
|
// Track which indices are in use for this mnemonic
|
|
usedIndices := make(map[uint32]bool)
|
|
|
|
for _, entry := range entries {
|
|
if !entry.IsDir() {
|
|
continue
|
|
}
|
|
|
|
// Try to read vault metadata
|
|
metadataPath := filepath.Join(vaultsDir, entry.Name(), "vault-metadata.json")
|
|
|
|
metadataBytes, err := afero.ReadFile(fs, metadataPath)
|
|
if err != nil {
|
|
// Skip vaults without metadata
|
|
continue
|
|
}
|
|
|
|
var metadata Metadata
|
|
|
|
err = json.Unmarshal(metadataBytes, &metadata)
|
|
if err != nil {
|
|
// Skip vaults with invalid metadata
|
|
continue
|
|
}
|
|
|
|
// Check if this vault uses the same mnemonic by comparing family hashes
|
|
if metadata.MnemonicFamilyHash == pubKeyHash {
|
|
usedIndices[metadata.DerivationIndex] = true
|
|
}
|
|
}
|
|
|
|
// Find the first available index
|
|
var index uint32
|
|
for usedIndices[index] {
|
|
index++
|
|
}
|
|
|
|
return index, nil
|
|
}
|
|
|
|
// SaveVaultMetadata saves vault metadata to the vault directory
|
|
func SaveVaultMetadata(fs afero.Fs, vaultDir string, metadata *Metadata) error {
|
|
metadataPath := filepath.Join(vaultDir, "vault-metadata.json")
|
|
|
|
metadataBytes, err := json.MarshalIndent(metadata, "", " ")
|
|
if err != nil {
|
|
return fmt.Errorf("failed to marshal vault metadata: %w", err)
|
|
}
|
|
|
|
err = afero.WriteFile(fs, metadataPath, metadataBytes, secret.FilePerms)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to write vault metadata: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// LoadVaultMetadata loads vault metadata from the vault directory
|
|
func LoadVaultMetadata(fs afero.Fs, vaultDir string) (*Metadata, error) {
|
|
metadataPath := filepath.Join(vaultDir, "vault-metadata.json")
|
|
|
|
metadataBytes, err := afero.ReadFile(fs, metadataPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read vault metadata: %w", err)
|
|
}
|
|
|
|
var metadata Metadata
|
|
|
|
err = json.Unmarshal(metadataBytes, &metadata)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to unmarshal vault metadata: %w", err)
|
|
}
|
|
|
|
return &metadata, nil
|
|
}
|