Files
w3cjandClaude Opus 5.5 02ba74e614 build(lint): stricter linters, and the fixes they need
golangci-lint now also runs govet's nilness, unusedwrite, hostport and
waitgroup analyzers, nilnesserr, durationcheck, canonicalheader,
errchkjson, forcetypeassert, contextcheck and gochecksumtype.

- errchkjson checks each JSON encoding's error where the value could
  fail and drops it where errchkjson proves it can't, and reports
  structs without exported fields; tests may drop theirs.
- forcetypeassert holds the code; tests are exempt.
- gochecksumtype: agentlogin's format is a sum type (//sumtype:decl),
  and a switch on one names every case, default or not.
- canonicalheader: WWW-Authenticate, X-Request-ID, X-Api-Key.
- contextcheck: ctx threaded through carry, peekClientHello and sniOf,
  and through inject and value, whose cached lookup takes
  WithTimeout(WithoutCancel(ctx)), so two forbidigo nolints go;
  openTransparent takes its dialer's ctx. nolints stay where
  contextcheck can't follow: ReverseProxy's Rewrite, WithoutCancel
  alone, cobra's cmd.Context, e2e's runner window.
- No-behaviour fixes: scrubList for redact's list, the loopback
  forward's ports kept with its listeners, upgradetest's Key keeps its
  public key, a CA's leaves read through leafOf.

Contributing's Go lint rules lists each one and the sum-type convention.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-15 00:17:50 -07:00

83 lines
2.1 KiB
Go

package sshagent
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"github.com/devvmhq/devvm/internal/state"
)
// AddedKey is a key added to devvm's own agent, as agent-keys.json in
// devvm's data folder records it: its file, an absolute path, its
// fingerprint when it was added (SHA256:…), and how it's loaded.
type AddedKey struct {
File string `json:"file"`
Fingerprint string `json:"fingerprint,omitempty"`
AddOptions
}
// keysFile is agent-keys.json.
type keysFile struct {
Keys []AddedKey `json:"keys"`
}
// added reads the keys added to the agent. A missing file has none; a key
// devvm can't use (a relative path, a lifetime it doesn't take) is a
// state.CorruptError naming the file.
func (a *Agent) added() ([]AddedKey, error) {
path := a.Dirs.AgentKeys()
data, err := os.ReadFile(path) //nolint:gosec // paths come from paths.Dirs
switch {
case errors.Is(err, fs.ErrNotExist):
return nil, nil
case err != nil:
return nil, err
}
var f keysFile
if err := json.Unmarshal(data, &f); err != nil {
return nil, &state.CorruptError{Path: path, Reason: err.Error()}
}
for i, k := range f.Keys {
if err := k.check(); err != nil {
return nil, &state.CorruptError{Path: path, Reason: fmt.Sprintf("keys[%d]: %v", i, err)}
}
}
return f.Keys, nil
}
// saveAdded replaces agent-keys.json with keys.
func (a *Agent) saveAdded(keys []AddedKey) error {
for _, k := range keys {
if err := k.check(); err != nil {
return err
}
}
data, _ := json.MarshalIndent(keysFile{Keys: append([]AddedKey{}, keys...)}, "", " ")
return state.WriteFile(a.Dirs.AgentKeys(), append(data, '\n'))
}
// check refuses a key whose file isn't an absolute path, or whose lifetime
// --lifetime doesn't take.
func (k AddedKey) check() error {
if !filepath.IsAbs(k.File) {
return fmt.Errorf("key file %q is not an absolute path", k.File)
}
if k.Lifetime != "" {
return CheckLifetime(k.Lifetime)
}
return nil
}
// lifetime is how long the agent keeps the key once loaded: its own, else
// DefaultLifetime.
func (k AddedKey) lifetime() string {
if k.Lifetime != "" {
return k.Lifetime
}
return DefaultLifetime
}