53 lines
1.6 KiB
Go
53 lines
1.6 KiB
Go
package agents
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"sort"
|
||
|
|
"strings"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"g.fc.run/ai/byre/internal/api"
|
||
|
|
"g.fc.run/ai/byre/internal/clients"
|
||
|
|
)
|
||
|
|
|
||
|
|
const signTimeout = 2 * time.Minute
|
||
|
|
|
||
|
|
func (m *Manager) signer(ctx context.Context, user api.User) func(payload []byte) ([]byte, error) {
|
||
|
|
return func(payload []byte) ([]byte, error) {
|
||
|
|
mine := []clients.Client{}
|
||
|
|
for _, c := range m.clients.List() {
|
||
|
|
if c.Login == user.Login {
|
||
|
|
mine = append(mine, c)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if len(mine) == 0 {
|
||
|
|
return nil, fmt.Errorf("commits are signed on %s's Mac, and none of theirs is connected", user.Login)
|
||
|
|
}
|
||
|
|
sort.SliceStable(mine, func(i, j int) bool {
|
||
|
|
a, b := mine[i].LastActionAt, mine[j].LastActionAt
|
||
|
|
return a != nil && (b == nil || a.After(*b))
|
||
|
|
})
|
||
|
|
client := mine[0]
|
||
|
|
|
||
|
|
ident := fmt.Sprintf("%s <%s>", user.GitName, user.GitEmail)
|
||
|
|
ms := int(signTimeout.Milliseconds())
|
||
|
|
cctx, cancel := context.WithTimeout(ctx, signTimeout+replyGrace)
|
||
|
|
defer cancel()
|
||
|
|
reply, err := m.clients.Call(cctx, client.ID, api.ClientRequest{Op: api.Sign, Data: &payload, Ident: &ident, TimeoutMs: &ms})
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
if reply.ExitCode == nil || reply.Output == nil || reply.Data == nil {
|
||
|
|
return nil, fmt.Errorf("%s's reply has no exit_code, output or data", client.Host)
|
||
|
|
}
|
||
|
|
if *reply.ExitCode == -1 {
|
||
|
|
return nil, fmt.Errorf("%s didn't sign within %s", client.Host, signTimeout)
|
||
|
|
}
|
||
|
|
if *reply.ExitCode != 0 || !strings.Contains(*reply.Output, "[GNUPG:] SIG_CREATED ") {
|
||
|
|
return nil, fmt.Errorf("signing on %s failed with exit status %d: %s", client.Host, *reply.ExitCode, strings.TrimSpace(*reply.Output))
|
||
|
|
}
|
||
|
|
return *reply.Data, nil
|
||
|
|
}
|
||
|
|
}
|