From 1292c5d702641fe814d1d1ae1cb53e6dfc7c4caa Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Fri, 29 Jul 2022 15:54:13 -0400 Subject: [PATCH] fix authentication transaction; record created identities (#10) --- controller/enable.go | 18 ++++++++++++++++++ controller/store/identity.go | 4 ++-- controller/util.go | 1 + 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/controller/enable.go b/controller/enable.go index 9e95a3ce..5d9d688f 100644 --- a/controller/enable.go +++ b/controller/enable.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "github.com/go-openapi/runtime/middleware" + "github.com/openziti-test-kitchen/zrok/controller/store" "github.com/openziti-test-kitchen/zrok/rest_model_zrok" "github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/identity" "github.com/openziti/edge/rest_management_api_client" @@ -34,6 +35,23 @@ func enableHandler(_ identity.EnableParams, principal *rest_model_zrok.Principal return identity.NewEnableInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) } + tx, err := str.Begin() + if err != nil { + logrus.Errorf("error starting transaction: %v", err) + return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) + } + iid, err := str.CreateIdentity(int(principal.ID), &store.Identity{ZitiId: ident.Payload.Data.ID}, tx) + if err != nil { + logrus.Errorf("error storing created identity: %v", err) + _ = tx.Rollback() + return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) + } + if err := tx.Commit(); err != nil { + logrus.Errorf("error committing: %v", err) + return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) + } + logrus.Infof("recorded identity '%v' with id '%v' for '%v'", ident.Payload.Data.ID, iid, principal.Username) + resp := identity.NewEnableCreated().WithPayload(&rest_model_zrok.EnableResponse{ Identity: ident.Payload.Data.ID, }) diff --git a/controller/store/identity.go b/controller/store/identity.go index 88e56b01..868b2e4e 100644 --- a/controller/store/identity.go +++ b/controller/store/identity.go @@ -11,12 +11,12 @@ type Identity struct { ZitiId string } -func (self *Store) CreateIdentity(accountId int, svc *Service, tx *sqlx.Tx) (int, error) { +func (self *Store) CreateIdentity(accountId int, i *Identity, tx *sqlx.Tx) (int, error) { stmt, err := tx.Prepare("insert into identities (account_id, ziti_id) values (?, ?)") if err != nil { return 0, errors.Wrap(err, "error preparing identities insert statement") } - res, err := stmt.Exec(accountId, svc.ZitiId) + res, err := stmt.Exec(accountId, i.ZitiId) if err != nil { return 0, errors.Wrap(err, "error executing identities insert statement") } diff --git a/controller/util.go b/controller/util.go index 666a17cc..86cafc93 100644 --- a/controller/util.go +++ b/controller/util.go @@ -18,6 +18,7 @@ func ZrokAuthenticate(token string) (*rest_model_zrok.Principal, error) { if err != nil { return nil, err } + defer func() { _ = tx.Rollback() }() if a, err := str.FindAccountWithToken(token, tx); err == nil { principal := rest_model_zrok.Principal{ ID: int64(a.Id),