diff --git a/main.go b/main.go index 115ede7..84b2ecc 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "github.com/TheLab-ms/access-controller-controller/client" "github.com/TheLab-ms/access-controller-controller/conf" + "github.com/TheLab-ms/access-controller-controller/keycloak" "github.com/TheLab-ms/access-controller-controller/reporting" "github.com/TheLab-ms/access-controller-controller/sync" ) @@ -26,10 +27,12 @@ func main() { } // Sync badge access from keycloak if configured + var kc *keycloak.Keycloak if conf.KeycloakURL == "" { log.Printf("disabling keyvault sync because keycloak URL is not set") } else { - c := sync.NewController(conf, cli) + kc = keycloak.New(conf) + c := sync.NewController(conf, cli, kc) if conf.CallbackURL != "" { err := c.EnsureWebhook(ctx) @@ -53,7 +56,7 @@ func main() { if conf.SwipeScrapeInterval == 0 { log.Printf("disabling reporting controller because swipe scrape interval is zero") } else { - ctrl, err := reporting.NewController(conf, cli) + ctrl, err := reporting.NewController(conf, cli, kc) if err != nil { log.Fatalf("error while configuring reporting controller: %s", err) } diff --git a/reporting/controller.go b/reporting/controller.go index 590e106..fd26e70 100644 --- a/reporting/controller.go +++ b/reporting/controller.go @@ -11,6 +11,7 @@ import ( "github.com/TheLab-ms/access-controller-controller/client" "github.com/TheLab-ms/access-controller-controller/conf" + "github.com/TheLab-ms/access-controller-controller/keycloak" ) const migration = ` @@ -29,10 +30,11 @@ CREATE INDEX IF NOT EXISTS idx_swipes_time ON swipes (time); type Controller struct { db *pgx.Conn client *client.Client + keycloak *keycloak.Keycloak swipeScrapeInterval time.Duration } -func NewController(env *conf.Env, ac *client.Client) (*Controller, error) { +func NewController(env *conf.Env, ac *client.Client, kc *keycloak.Keycloak) (*Controller, error) { db, err := pgx.Connect(pgx.ConnConfig{ Host: env.PostgresHost, User: env.PostgresUser, @@ -50,6 +52,7 @@ func NewController(env *conf.Env, ac *client.Client) (*Controller, error) { return &Controller{ db: db, client: ac, + keycloak: kc, swipeScrapeInterval: env.SwipeScrapeInterval, }, nil } @@ -80,8 +83,19 @@ func (c *Controller) scrape(ctx context.Context) error { } log.Printf("last known swipe event ID: %d", queryStart) + usersByUUID := map[string]*keycloak.AccessUser{} + if c.keycloak != nil { + allUsers, err := c.keycloak.ListUsers(ctx) + if err != nil { + return fmt.Errorf("listing users from Keycloak: %w", err) + } + for _, user := range allUsers { + usersByUUID[user.UUID] = user + } + } + fn := func(swipe *client.CardSwipe) error { - _, err := c.db.Exec("INSERT INTO swipes (id, cardID, doorID, time, name) VALUES ($1, $2, $3, $4, $5) ON CONFLICT DO NOTHING", swipe.ID, swipe.CardID, swipe.DoorID, swipe.Time, swipe.Name) + _, err := c.db.Exec("INSERT INTO swipes (id, cardID, doorID, time, name) VALUES ($1, $2, $3, $4, $5) ON CONFLICT DO NOTHING", swipe.ID, swipe.CardID, swipe.DoorID, swipe.Time, usersByUUID[swipe.Name]) if err != nil { return fmt.Errorf("inserting swipe %d into database: %s", swipe.ID, err) } diff --git a/sync/controller.go b/sync/controller.go index cad81d1..0a12e52 100644 --- a/sync/controller.go +++ b/sync/controller.go @@ -33,10 +33,10 @@ type Controller struct { trigger chan struct{} } -func NewController(c *conf.Env, cli *client.Client) *Controller { +func NewController(c *conf.Env, cli *client.Client, kc *keycloak.Keycloak) *Controller { ctrl := &Controller{ controller: cli, - storage: keycloak.New(c), + storage: kc, conf: c, trigger: make(chan struct{}, 1), }