Add some debug logs

This commit is contained in:
Melora Hugues 2024-10-06 11:28:26 +02:00
parent 9206c8e41e
commit 0bfb02f960
6 changed files with 23 additions and 4 deletions

View file

@ -73,7 +73,7 @@ func serve() {
// - only init when using them in a request? // - only init when using them in a request?
for _, c := range backendConfs { for _, c := range backendConfs {
logger.L.Debugf("Initializing backend %s", c.Name) 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 { if err != nil {
utils.Failf("failed to init backend client: %s", err.Error()) utils.Failf("failed to init backend client: %s", err.Error())
} }

View file

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/model" "git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/model"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/logger"
"github.com/google/uuid" "github.com/google/uuid"
) )
@ -25,6 +26,7 @@ type sqlAuthRequestDB struct {
} }
func (db *sqlAuthRequestDB) GetAuthRequestByID(ctx context.Context, id uuid.UUID) (*model.AuthRequest, error) { 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) query := fmt.Sprintf(`SELECT %s FROM "auth_request_2" WHERE "id" = ?`, authRequestRows)
row := db.db.QueryRowContext(ctx, query, id) 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) return nil, fmt.Errorf("invalid format for scopes: %w", err)
} }
fmt.Println(res)
return &res, nil return &res, nil
} }
func (db *sqlAuthRequestDB) CreateAuthRequest(ctx context.Context, req model.AuthRequest) error { 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) tx, err := db.db.BeginTx(ctx, nil)
if err != nil { if err != nil {
return fmt.Errorf("failed to start transaction: %w", err) 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) 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) 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, _, err = tx.ExecContext(ctx, query,
req.ID, req.ClientID, req.BackendID, req.ID, req.ClientID, req.BackendID,

View file

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/model" "git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/model"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/logger"
"github.com/google/uuid" "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) { 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) query := fmt.Sprintf(`SELECT %s FROM "backend" WHERE "name" = ?`, backendRows)
row := db.db.QueryRowContext(ctx, query, name) row := db.db.QueryRowContext(ctx, query, name)
return backendFromRow(row) return backendFromRow(row)
} }
func (db *sqlBackendDB) GetBackendByID(ctx context.Context, id uuid.UUID) (*model.Backend, error) { 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) query := fmt.Sprintf(`SELECT %s FROM "backend" WHERE "id" = ?`, backendRows)
row := db.db.QueryRowContext(ctx, query, id) row := db.db.QueryRowContext(ctx, query, id)
return backendFromRow(row) return backendFromRow(row)
} }
func (db *sqlBackendDB) GetAllBackends(ctx context.Context) ([]*model.Backend, error) { 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)) rows, err := db.db.QueryContext(ctx, fmt.Sprintf(`SELECT %s FROM "backend"`, backendRows))
if err != nil { if err != nil {
return nil, err return nil, err
@ -71,7 +75,7 @@ func (db *sqlBackendDB) GetAllBackends(ctx context.Context) ([]*model.Backend, e
} }
res = append(res, b) res = append(res, b)
} }
return res, nil return res, rows.Err()
} }
func (db *sqlBackendDB) AddBackend(ctx context.Context, newBackend *model.Backend) error { func (db *sqlBackendDB) AddBackend(ctx context.Context, newBackend *model.Backend) error {

View file

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/model" "git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/model"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/logger"
_ "github.com/mattn/go-sqlite3" _ "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) { 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) query := fmt.Sprintf(`SELECT %s FROM "client" WHERE "id" = ?`, clientRows)
row := db.db.QueryRowContext(ctx, query, id) row := db.db.QueryRowContext(ctx, query, id)
return clientFromRow(row) return clientFromRow(row)

View file

@ -8,6 +8,7 @@ import (
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/db" "git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/db"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/model" "git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/model"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/logger"
"github.com/go-jose/go-jose/v4" "github.com/go-jose/go-jose/v4"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/zitadel/oidc/v3/pkg/oidc" "github.com/zitadel/oidc/v3/pkg/oidc"
@ -27,6 +28,11 @@ type Storage struct {
Auth storage interface Auth storage interface
*/ */
func (s *Storage) CreateAuthRequest(ctx context.Context, req *oidc.AuthRequest, userID string) (op.AuthRequest, error) { 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 // validate that the connector is correct
backendName, ok := stringFromCtx(ctx, "backendName") backendName, ok := stringFromCtx(ctx, "backendName")
if !ok { 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) 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 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) { 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) authRequest, err := s.LocalStorage.AuthRequestStorage().GetAuthRequestByID(ctx, authRequestID)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get authRequest from local storage: %w", err) 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 // 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 // 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) { 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) authRequestID, err := uuid.Parse(id)
if err != nil { if err != nil {

Binary file not shown.