Skip to content

Commit 763c2ae

Browse files
committed
LoadKeys(): slog.Debug() + refactor
* Now follows symlinks * TPM file suffix is now .tpm, not tpm
1 parent c1206e1 commit 763c2ae

File tree

2 files changed

+41
-36
lines changed

2 files changed

+41
-36
lines changed

agent/agent.go

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ func (a *Agent) List() ([]*agent.Key, error) {
127127
Comment: string(k.Comment),
128128
})
129129
}
130+
130131
return agentKeys, nil
131132
}
132133

@@ -268,35 +269,46 @@ func (a *Agent) Unlock(passphrase []byte) error {
268269
}
269270

270271
func LoadKeys(keyDir string) (map[string]*key.Key, error) {
271-
keys := map[string]*key.Key{}
272-
err := filepath.WalkDir(keyDir,
273-
func(path string, d fs.DirEntry, err error) error {
274-
if err != nil {
275-
return err
276-
}
277-
if d.IsDir() {
278-
return nil
279-
}
280-
if !strings.HasSuffix(path, "tpm") {
281-
return nil
282-
}
283-
f, err := os.ReadFile(path)
284-
if err != nil {
285-
return fmt.Errorf("failed reading %s", path)
286-
}
287-
k, err := key.DecodeKey(f)
288-
if err != nil {
289-
slog.Debug("not a TPM-sealed key", slog.String("key_path", path), slog.String("error", err.Error()))
290-
return nil
291-
}
292-
keys[k.Fingerprint()] = k
293-
return nil
294-
},
295-
)
272+
keyDir, err := filepath.EvalSymlinks(keyDir)
296273
if err != nil {
297274
return nil, err
298275
}
299-
return keys, nil
276+
277+
keys := make(map[string]*key.Key)
278+
279+
walkFunc := func(path string, d fs.DirEntry, err error) error {
280+
if err != nil {
281+
return err
282+
}
283+
284+
if d.IsDir() {
285+
return nil
286+
}
287+
288+
if !strings.HasSuffix(path, ".tpm") {
289+
slog.Debug("skipping key: does not have .tpm suffix", slog.String("name", path))
290+
return nil
291+
}
292+
293+
f, err := os.ReadFile(path)
294+
if err != nil {
295+
return fmt.Errorf("failed reading %s", path)
296+
}
297+
298+
k, err := key.DecodeKey(f)
299+
if err != nil {
300+
slog.Debug("not a TPM sealed key", slog.String("key_path", path), slog.String("error", err.Error()))
301+
return nil
302+
}
303+
304+
keys[k.Fingerprint()] = k
305+
306+
slog.Debug("added TPM key", slog.String("name", path))
307+
return nil
308+
}
309+
310+
err = filepath.WalkDir(keyDir, walkFunc)
311+
return keys, err
300312
}
301313

302314
func NewAgent(listener *net.UnixListener, agents []agent.ExtendedAgent, tpmFetch func() transport.TPMCloser, pin func(*key.Key) ([]byte, error)) *Agent {

cmd/ssh-tpm-agent/main.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,6 @@ func main() {
165165
keyDir = utils.SSHDir()
166166
}
167167

168-
fi, err := os.Lstat(keyDir)
169-
if err != nil {
170-
slog.Error(err.Error())
171-
os.Exit(1)
172-
}
173-
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
174-
slog.Info("Not following symbolic link", slog.String("key_directory", keyDir))
175-
}
176-
177168
if term.IsTerminal(int(os.Stdin.Fd())) {
178169
slog.Info("Warning: ssh-tpm-agent is meant to run as a background daemon.")
179170
slog.Info("Running multiple instances is likely to lead to conflicts.")
@@ -227,7 +218,9 @@ func main() {
227218
}()
228219

229220
if !noLoad {
230-
agent.LoadKeys(keyDir)
221+
if err := agent.LoadKeys(keyDir); err != nil {
222+
slog.Error("loading keys", slog.String("error", err.Error()))
223+
}
231224
}
232225

233226
agent.Wait()

0 commit comments

Comments
 (0)