Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve UUIDs to names using Keycloak #1

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down
18 changes: 16 additions & 2 deletions reporting/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand All @@ -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,
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions sync/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down