From 0bfb02f9601a8e32065dcb14e25fd733f0e0e99e Mon Sep 17 00:00:00 2001 From: Melora Hugues Date: Sun, 6 Oct 2024 11:28:26 +0200 Subject: [PATCH] Add some debug logs --- polyculeconnect/cmd/serve/serve.go | 2 +- .../internal/db/authrequest/authrequest.go | 6 ++++-- .../internal/db/backend/backend.go | 6 +++++- polyculeconnect/internal/db/client/client.go | 2 ++ polyculeconnect/internal/storage/storage.go | 11 +++++++++++ polyculeconnect/polyculeconnect.db | Bin 126976 -> 126976 bytes 6 files changed, 23 insertions(+), 4 deletions(-) diff --git a/polyculeconnect/cmd/serve/serve.go b/polyculeconnect/cmd/serve/serve.go index 32a48dd..4e40a90 100644 --- a/polyculeconnect/cmd/serve/serve.go +++ b/polyculeconnect/cmd/serve/serve.go @@ -73,7 +73,7 @@ func serve() { // - only init when using them in a request? for _, c := range backendConfs { logger.L.Debugf("Initializing backend %s", c.Name) - b, err := client.New(context.Background(), c) + b, err := client.New(context.Background(), c, logger.L) if err != nil { utils.Failf("failed to init backend client: %s", err.Error()) } diff --git a/polyculeconnect/internal/db/authrequest/authrequest.go b/polyculeconnect/internal/db/authrequest/authrequest.go index 3506dee..2505d1c 100644 --- a/polyculeconnect/internal/db/authrequest/authrequest.go +++ b/polyculeconnect/internal/db/authrequest/authrequest.go @@ -8,6 +8,7 @@ import ( "fmt" "git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/model" + "git.faercol.me/faercol/polyculeconnect/polyculeconnect/logger" "github.com/google/uuid" ) @@ -25,6 +26,7 @@ type sqlAuthRequestDB struct { } func (db *sqlAuthRequestDB) GetAuthRequestByID(ctx context.Context, id uuid.UUID) (*model.AuthRequest, error) { + logger.L.Debugf("Getting auth request with id %s", id) query := fmt.Sprintf(`SELECT %s FROM "auth_request_2" WHERE "id" = ?`, authRequestRows) row := db.db.QueryRowContext(ctx, query, id) @@ -38,12 +40,11 @@ func (db *sqlAuthRequestDB) GetAuthRequestByID(ctx context.Context, id uuid.UUID return nil, fmt.Errorf("invalid format for scopes: %w", err) } - fmt.Println(res) - return &res, nil } func (db *sqlAuthRequestDB) CreateAuthRequest(ctx context.Context, req model.AuthRequest) error { + logger.L.Debugf("Creating a new auth request between client app %s and backend %s", req.ClientID, req.BackendID) tx, err := db.db.BeginTx(ctx, nil) if err != nil { return fmt.Errorf("failed to start transaction: %w", err) @@ -55,6 +56,7 @@ func (db *sqlAuthRequestDB) CreateAuthRequest(ctx context.Context, req model.Aut return fmt.Errorf("failed to serialize scopes: %w", err) } + // TODO: when the old table is done, rename into auth_request query := fmt.Sprintf(`INSERT INTO "auth_request_2" (%s) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`, authRequestRows) _, err = tx.ExecContext(ctx, query, req.ID, req.ClientID, req.BackendID, diff --git a/polyculeconnect/internal/db/backend/backend.go b/polyculeconnect/internal/db/backend/backend.go index b68f3ef..362ea44 100644 --- a/polyculeconnect/internal/db/backend/backend.go +++ b/polyculeconnect/internal/db/backend/backend.go @@ -7,6 +7,7 @@ import ( "fmt" "git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/model" + "git.faercol.me/faercol/polyculeconnect/polyculeconnect/logger" "github.com/google/uuid" ) @@ -46,18 +47,21 @@ func backendFromRow(row scannable) (*model.Backend, error) { } func (db *sqlBackendDB) GetBackendByName(ctx context.Context, name string) (*model.Backend, error) { + logger.L.Debugf("Getting backend with name %s from DB", name) query := fmt.Sprintf(`SELECT %s FROM "backend" WHERE "name" = ?`, backendRows) row := db.db.QueryRowContext(ctx, query, name) return backendFromRow(row) } func (db *sqlBackendDB) GetBackendByID(ctx context.Context, id uuid.UUID) (*model.Backend, error) { + logger.L.Debugf("Getting backend with ID %s from DB", id) query := fmt.Sprintf(`SELECT %s FROM "backend" WHERE "id" = ?`, backendRows) row := db.db.QueryRowContext(ctx, query, id) return backendFromRow(row) } func (db *sqlBackendDB) GetAllBackends(ctx context.Context) ([]*model.Backend, error) { + logger.L.Debug("Getting all backends from DB") rows, err := db.db.QueryContext(ctx, fmt.Sprintf(`SELECT %s FROM "backend"`, backendRows)) if err != nil { return nil, err @@ -71,7 +75,7 @@ func (db *sqlBackendDB) GetAllBackends(ctx context.Context) ([]*model.Backend, e } res = append(res, b) } - return res, nil + return res, rows.Err() } func (db *sqlBackendDB) AddBackend(ctx context.Context, newBackend *model.Backend) error { diff --git a/polyculeconnect/internal/db/client/client.go b/polyculeconnect/internal/db/client/client.go index 0b90045..7155b3f 100644 --- a/polyculeconnect/internal/db/client/client.go +++ b/polyculeconnect/internal/db/client/client.go @@ -8,6 +8,7 @@ import ( "fmt" "git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/model" + "git.faercol.me/faercol/polyculeconnect/polyculeconnect/logger" _ "github.com/mattn/go-sqlite3" ) @@ -50,6 +51,7 @@ func clientFromRow(row *sql.Row) (*model.Client, error) { } func (db *sqlClientDB) GetClientByID(ctx context.Context, id string) (*model.Client, error) { + logger.L.Debugf("Getting client app with ID %s from DB", id) query := fmt.Sprintf(`SELECT %s FROM "client" WHERE "id" = ?`, clientRows) row := db.db.QueryRowContext(ctx, query, id) return clientFromRow(row) diff --git a/polyculeconnect/internal/storage/storage.go b/polyculeconnect/internal/storage/storage.go index 902c8a1..adc6b45 100644 --- a/polyculeconnect/internal/storage/storage.go +++ b/polyculeconnect/internal/storage/storage.go @@ -8,6 +8,7 @@ import ( "git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/db" "git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/model" + "git.faercol.me/faercol/polyculeconnect/polyculeconnect/logger" "github.com/go-jose/go-jose/v4" "github.com/google/uuid" "github.com/zitadel/oidc/v3/pkg/oidc" @@ -27,6 +28,11 @@ type Storage struct { Auth storage interface */ func (s *Storage) CreateAuthRequest(ctx context.Context, req *oidc.AuthRequest, userID string) (op.AuthRequest, error) { + + // userID should normally be an empty string (to verify), we don't get it in our workflow from what I saw + // TODO: check this is indeed not needed / never present + logger.L.Debug("Creating a new auth request") + // validate that the connector is correct backendName, ok := stringFromCtx(ctx, "backendName") if !ok { @@ -44,6 +50,8 @@ func (s *Storage) CreateAuthRequest(ctx context.Context, req *oidc.AuthRequest, return nil, fmt.Errorf("failed to save auth request: %w", err) } + logger.L.Debugf("Created a new auth request for backend %s", backendName) + return opReq, nil } @@ -104,6 +112,8 @@ func (s *Storage) KeySet(ctx context.Context) ([]op.Key, error) { */ func (s *Storage) getClientWithDetails(ctx context.Context, authRequestID uuid.UUID) (op.Client, error) { + logger.L.Debug("Trying to get client details from auth request") + authRequest, err := s.LocalStorage.AuthRequestStorage().GetAuthRequestByID(ctx, authRequestID) if err != nil { return nil, fmt.Errorf("failed to get authRequest from local storage: %w", err) @@ -127,6 +137,7 @@ func (s *Storage) getClientWithDetails(ctx context.Context, authRequestID uuid.U // but a request is always associated to a backend, and we really need both, so we have no // choice here. I'll maybe need to have a more elegant solution later, but not choice for now func (s *Storage) GetClientByClientID(ctx context.Context, id string) (op.Client, error) { + logger.L.Debugf("Selecting client app with ID %s", id) authRequestID, err := uuid.Parse(id) if err != nil { diff --git a/polyculeconnect/polyculeconnect.db b/polyculeconnect/polyculeconnect.db index 0cc1857cd47957be263f1156fd7837ffb2342048..f5995337c4aa454fb3d8a0e88e48c280f49b483d 100644 GIT binary patch delta 1386 zcma)*ziSjh6vwkB=91j)Zkm9gNTMkOnPc9}d-G!MN2OK-E|_bZF9GT7ZJ7dCEg#PtXH z(nDiB8toN}RtbjMYi6bK3^YZI(rT^vf%}@hxeuIiwajfLM8(ZeOQALAmTq%ialX4% zbkHV!7F~?Ya;X`TubFMgu-brDQh0@$#-UIl3@s5inv)zs^A1XieBc2SFG_}vBr{|c z^XMIq9yPZx1_fII4oL|ugfJJ-K?miL6mqCp9c*C8gG(nbSh`8pJU}4t$Dz6sIOHUN zlqNB1+V7x%wGlxn)>sD&LD$Sts;jw7I1sK0ZVZ~nDiah9Au4szbfJTasEtdd(hzl; zfyIW^QhR1h>ZqFN4Ay@`xg2sWNh6XPHJ$IEO|89mI-y9$G*ghwT5p-sDnukO%EfUg zudr>Z2_?u2=v)U)PIx4A4AQQXQV^L7pqRo?H`V|Ii+?~(MXDk*M6ket=t8Mp2W{#Q zyr~s4V2ELwZ1R)?^CmVfM3Iz|<4~$(@>D1dhybJCpY5Q*v}>0QgIXtOx;o9YNvFtD zS5XU2_80W}Z~0Aywn8(MI@J)#)pO{{+G+M_X1UA{r-~27QgNqvQhq64mXGuA<-7ba z|5)r5-^%{U;#u}A-@Y}ykxlaK$~HL3Q!3ivB+nyngWHohKWLMac-(J;leoFi23y~@ n|0mpho18?VxpU0Y-PRi?+hXIugRxkwg$