-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
52 lines (47 loc) · 1.2 KB
/
auth.go
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
package main
import (
"context"
"log"
"runtime"
"strings"
"time"
authdump "github.com/Jille/etcd-auth-dump"
"github.com/Jille/etcd-postgresql-sync/database"
"github.com/Jille/etcd-postgresql-sync/database/gendb"
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
"google.golang.org/grpc/codes"
)
var (
lastSyncedAuthRevision uint64
)
func authSyncLoop(ctx context.Context) {
for {
if err := syncAuth(ctx); err != nil {
log.Printf("Error syncing authentication config: %v", err)
}
time.Sleep(10 * time.Minute)
}
}
func syncAuth(ctx context.Context) error {
commands, rev, err := authdump.Dump(ctx, c, lastSyncedAuthRevision)
if err != nil {
if err == authdump.ErrUnchanged {
return nil
}
if ee, ok := err.(rpctypes.EtcdError); ok && ee.Code() == codes.PermissionDenied {
log.Printf("Fatal error syncing authentication (does the syncer have role root?): %v [disabling authentication syncing]", err)
runtime.Goexit()
}
return err
}
if err := database.RunTransaction(ctx, func(q *gendb.Queries) error {
if err := q.DeleteAuth(ctx); err != nil {
return err
}
return q.SetAuth(ctx, strings.Join(commands, "\n"))
}); err != nil {
return err
}
lastSyncedAuthRevision = rev
return nil
}