oficina / agent-access Public

Clone
git clone https://amadan.net/oficina/agent-access

Plain git — no account needed to clone.

Download this file

1package grant
2
3import (
4 "context"
5 "errors"
6)
7
8// A database's opaque subjects belong to one Home namespace for its lifetime.
9// Binding at startup prevents a config change from relabelling live grants.
10func (s *Store) bindIdentityIssuer(ctx context.Context, issuer string) error {
11 tx, err := s.DB.BeginTx(ctx, nil)
12 if err != nil {
13 return err
14 }
15 defer tx.Rollback()
16 // First statement reserves the writer before inspecting any existing grants.
17 if _, err = tx.ExecContext(ctx, `INSERT INTO agent_identity_binding(id,issuer) SELECT 1,? WHERE NOT EXISTS (SELECT 1 FROM agent_grants) ON CONFLICT(id) DO NOTHING`, issuer); err != nil {
18 return err
19 }
20 var bound string
21 if err = tx.QueryRowContext(ctx, `SELECT issuer FROM agent_identity_binding WHERE id=1`).Scan(&bound); err != nil || bound != issuer {
22 return errors.New("grant database identity namespace differs or is unbound; explicit migration required")
23 }
24 return tx.Commit()
25}
26