Skip to content

Commit

Permalink
Merge pull request #88 from smlx/cleanup-connections
Browse files Browse the repository at this point in the history
Clean up connections
  • Loading branch information
smlx authored Dec 16, 2021
2 parents 6b582f4 + c7884a8 commit a9ed224
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/piv-agent/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (cmd *ServeCmd) Run(log *zap.Logger) error {
log.Info("startup", zap.String("version", version),
zap.String("build date", date))
p := piv.New(log)
defer p.CloseAll()
// use FDs passed via socket activation
ls, err := sockets.Get(validAgents)
if err != nil {
Expand All @@ -72,6 +73,11 @@ func (cmd *ServeCmd) Run(log *zap.Logger) error {
s := server.NewSSH(log)
a := ssh.NewAgent(p, log, cmd.LoadKeyfile)
err := s.Serve(ctx, a, ls[cmd.AgentTypes["ssh"]], idle, cmd.IdleTimeout)
if err != nil {
log.Debug("exiting SSH server", zap.Error(err))
} else {
log.Debug("exiting SSH server successfully")
}
cancel()
return err
})
Expand Down
11 changes: 11 additions & 0 deletions internal/keyservice/piv/keyservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,14 @@ func (p *KeyService) GetDecrypter(keygrip []byte) (crypto.Decrypter, error) {
}
return &ECDHKey{ecdsa: ecdsaPrivKey}, nil
}

// CloseAll closes all security keys without checking for errors.
// This should be called to clean up connections to `pcscd`.
func (p *KeyService) CloseAll() {
p.log.Debug("closing security keys", zap.Int("count", len(p.securityKeys)))
for _, k := range p.securityKeys {
if err := k.Close(); err != nil {
p.log.Debug("couldn't close key", zap.Error(err))
}
}
}

0 comments on commit a9ed224

Please sign in to comment.