Repository navigation
Expand file tree
/
Copy pathrecord.go
More file actions
52 lines (45 loc) · 1.49 KB
/
Copy pathrecord.go
File metadata and controls
52 lines (45 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// SPDX-License-Identifier: Apache-2.0
package auth
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
"github.com/gopherium/framework/gonsole"
)
// Record returns the hook that stores one row naming the acting account and the command it applied.
func Record(cfg Config) func(ctx context.Context, call gonsole.Call, command string) error {
return func(ctx context.Context, call gonsole.Call, command string) error {
timeout, err := cfg.recordTimeout(call.Env)
if err != nil {
return err
}
bounded, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
err = cfg.withStores(bounded, call, func(stores Stores) error {
return stores.Records.Insert(bounded, entryOf(call, command))
})
if err != nil {
return fmt.Errorf("record %s: %w", command, err)
}
return nil
}
}
// recordTimeout returns how long storing one record may take, as the setting or the fallback names it.
func (c Config) recordTimeout(env gonsole.Env) (time.Duration, error) {
timeout, err := env.Duration("COMMAND_RECORD_TIMEOUT", c.RecordTimeout)
if err != nil {
return 0, err
}
if timeout <= 0 {
return 0, fmt.Errorf("gonsole/auth: the record timeout must stand above zero, got %v", timeout)
}
return timeout, nil
}
// entryOf returns the entry recording command as call ran it, under a new UUIDv7.
func entryOf(call gonsole.Call, command string) Entry {
return Entry{
ID: uuid.Must(uuid.NewV7()).String(), Actor: address(call.Actor), Command: command,
Args: call.Args, Flags: call.Flags,
}
}