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

feat(policy): 1659 implement transaction support in policy service #1684

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions service/pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (t Table) Field(field string) string {
// We can rename this but wanted to get mocks working.
type PgxIface interface {
Acquire(ctx context.Context) (*pgxpool.Conn, error)
Begin(ctx context.Context) (pgx.Tx, error)
Exec(context.Context, string, ...any) (pgconn.CommandTag, error)
QueryRow(context.Context, string, ...any) pgx.Row
Query(context.Context, string, ...any) (pgx.Rows, error)
Expand Down
19 changes: 19 additions & 0 deletions service/policy/db/policy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package db

import (
"context"

"github.com/opentdf/platform/protocol/go/common"
"github.com/opentdf/platform/service/logger"
"github.com/opentdf/platform/service/pkg/db"
Expand All @@ -19,6 +21,23 @@ type PolicyDBClient struct {
*Queries
}

func (c *PolicyDBClient) RunInTx(ctx context.Context, f func(txClient *PolicyDBClient) error) error {
// todo: could abstract Pgx.Tx to a common interface
tx, err := c.Client.Pgx.Begin(ctx)
if err != nil {
return err
}

txClient := &PolicyDBClient{c.Client, c.logger, c.Queries.WithTx(tx)}

err = f(txClient)
if err != nil {
return tx.Rollback(ctx)
}

return tx.Commit(ctx)
}

var (
TableAttributes = "attribute_definitions"
TableAttributeValues = "attribute_values"
Expand Down
59 changes: 41 additions & 18 deletions service/policy/namespaces/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,23 @@ func (ns NamespacesService) CreateNamespace(ctx context.Context, req *namespaces
}
rsp := &namespaces.CreateNamespaceResponse{}

n, err := ns.dbClient.CreateNamespace(ctx, req)
var namespace *policy.Namespace
err := ns.dbClient.RunInTx(ctx, func(tx *policydb.PolicyDBClient) error {
n, err := tx.CreateNamespace(ctx, req)
namespace = n
return err
})
if err != nil {
ns.logger.Audit.PolicyCRUDFailure(ctx, auditParams)
return nil, db.StatusifyError(err, db.ErrTextCreationFailed, slog.String("name", req.GetName()))
}

auditParams.ObjectID = n.GetId()
auditParams.Original = n
auditParams.ObjectID = namespace.GetId()
auditParams.Original = namespace
ns.logger.Audit.PolicyCRUDSuccess(ctx, auditParams)

ns.logger.Debug("created new namespace", slog.String("name", req.GetName()))
rsp.Namespace = n
rsp.Namespace = namespace

return rsp, nil
}
Expand All @@ -120,16 +125,25 @@ func (ns NamespacesService) UpdateNamespace(ctx context.Context, req *namespaces
ObjectID: namespaceID,
}

original, err := ns.dbClient.GetNamespace(ctx, namespaceID)
if err != nil {
ns.logger.Audit.PolicyCRUDFailure(ctx, auditParams)
return nil, db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("id", namespaceID))
}
var original, updated *policy.Namespace
err := ns.dbClient.RunInTx(ctx, func(tx *policydb.PolicyDBClient) error {
o, err := tx.GetNamespace(ctx, namespaceID)
if err != nil {
return db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("id", namespaceID))
}
original = o

updated, err := ns.dbClient.UpdateNamespace(ctx, namespaceID, req)
u, err := tx.UpdateNamespace(ctx, namespaceID, req)
if err != nil {
return db.StatusifyError(err, db.ErrTextUpdateFailed, slog.String("id", namespaceID))
}
updated = u

return nil
})
if err != nil {
ns.logger.Audit.PolicyCRUDFailure(ctx, auditParams)
return nil, db.StatusifyError(err, db.ErrTextUpdateFailed, slog.String("id", namespaceID))
return nil, err
}

auditParams.Original = original
Expand All @@ -156,16 +170,25 @@ func (ns NamespacesService) DeactivateNamespace(ctx context.Context, req *namesp
ObjectID: namespaceID,
}

original, err := ns.dbClient.GetNamespace(ctx, namespaceID)
if err != nil {
ns.logger.Audit.PolicyCRUDFailure(ctx, auditParams)
return nil, db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("id", namespaceID))
}
var original, updated *policy.Namespace
err := ns.dbClient.RunInTx(ctx, func(tx *policydb.PolicyDBClient) error {
o, err := tx.GetNamespace(ctx, namespaceID)
if err != nil {
return db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("id", namespaceID))
}
original = o

u, err := tx.DeactivateNamespace(ctx, namespaceID)
if err != nil {
return db.StatusifyError(err, db.ErrTextDeletionFailed, slog.String("id", namespaceID))
}
updated = u

updated, err := ns.dbClient.DeactivateNamespace(ctx, namespaceID)
return nil
})
if err != nil {
ns.logger.Audit.PolicyCRUDFailure(ctx, auditParams)
return nil, db.StatusifyError(err, db.ErrTextDeletionFailed, slog.String("id", namespaceID))
return nil, err
}

auditParams.Original = original
Expand Down
Loading