Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global exit timeout #62

Merged
merged 1 commit into from
Sep 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions cmd/piv-agent/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type agentTypeFlag map[string]uint
// ServeCmd represents the listen command.
type ServeCmd struct {
LoadKeyfile bool `kong:"default=true,help='Load the key file from ~/.ssh/id_ed25519'"`
ExitTimeout time.Duration `kong:"default=32m,help='Exit after this period to drop transaction and key file passphrase cache'"`
ExitTimeout time.Duration `kong:"default=12h,help='Exit after this period to drop transaction and key file passphrase cache, even if service is in use'"`
IdleTimeout time.Duration `kong:"default=32m,help='Exit after this period of disuse'"`
AgentTypes agentTypeFlag `kong:"default='ssh=0;gpg=1',help='Agent types to handle'"`
}

Expand Down Expand Up @@ -62,15 +63,15 @@ func (cmd *ServeCmd) Run(log *zap.Logger) error {
// prepare dependencies
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
exit := time.NewTicker(cmd.ExitTimeout)
idle := time.NewTicker(cmd.IdleTimeout)
g := errgroup.Group{}
// start SSH agent if given in agent-type flag
if _, ok := cmd.AgentTypes["ssh"]; ok {
log.Debug("starting SSH server")
g.Go(func() error {
s := server.NewSSH(log)
a := ssh.NewAgent(p, log, cmd.LoadKeyfile)
err := s.Serve(ctx, a, ls[cmd.AgentTypes["ssh"]], exit, cmd.ExitTimeout)
err := s.Serve(ctx, a, ls[cmd.AgentTypes["ssh"]], idle, cmd.IdleTimeout)
cancel()
return err
})
Expand All @@ -85,7 +86,7 @@ func (cmd *ServeCmd) Run(log *zap.Logger) error {
log.Debug("starting GPG server")
g.Go(func() error {
s := server.NewGPG(p, &pinentry.PINEntry{}, log, fallbackKeys)
err := s.Serve(ctx, ls[cmd.AgentTypes["gpg"]], exit, cmd.ExitTimeout)
err := s.Serve(ctx, ls[cmd.AgentTypes["gpg"]], idle, cmd.IdleTimeout)
if err != nil {
log.Debug("exiting GPG server", zap.Error(err))
} else {
Expand All @@ -95,12 +96,17 @@ func (cmd *ServeCmd) Run(log *zap.Logger) error {
return err
})
}
exit := time.NewTicker(cmd.ExitTimeout)
loop:
for {
select {
case <-ctx.Done():
log.Debug("exit done")
break loop
case <-idle.C:
log.Debug("idle timeout")
cancel()
break loop
case <-exit.C:
log.Debug("exit timeout")
cancel()
Expand Down