Skip to content
6 changes: 6 additions & 0 deletions service/integration/attribute_values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sort"
"strings"
"testing"
"time"

"github.com/opentdf/platform/protocol/go/common"
"github.com/opentdf/platform/protocol/go/policy"
Expand Down Expand Up @@ -285,14 +286,19 @@ func (s *AttributeValuesSuite) Test_UpdateAttributeValue() {

// create a value
attrDef := s.f.GetAttributeKey("example.net/attr/attr1")
start := time.Now().Add(-time.Second)
created, err := s.db.PolicyClient.CreateAttributeValue(s.ctx, attrDef.Id, &attributes.CreateAttributeValueRequest{
Value: "created value testing update",
Metadata: &common.MetadataMutable{
Labels: labels,
},
})
end := time.Now().Add(time.Second)
metadata := created.GetMetadata()
updatedAt := metadata.GetUpdatedAt()
createdAt := metadata.GetCreatedAt()
s.True(createdAt.AsTime().After(start))
s.True(createdAt.AsTime().Before(end))
s.NoError(err)
s.NotNil(created)

Expand Down
6 changes: 6 additions & 0 deletions service/integration/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log/slog"
"strings"
"testing"
"time"

"github.com/opentdf/platform/protocol/go/common"
"github.com/opentdf/platform/protocol/go/policy"
Expand Down Expand Up @@ -404,9 +405,14 @@ func (s *AttributesSuite) Test_UpdateAttribute() {
Labels: labels,
},
}
start := time.Now().Add(-time.Second)
created, err := s.db.PolicyClient.CreateAttribute(s.ctx, attr)
end := time.Now().Add(time.Second)
metadata := created.GetMetadata()
updatedAt := metadata.GetUpdatedAt()
createdAt := metadata.GetCreatedAt()
s.True(createdAt.AsTime().After(start))
s.True(createdAt.AsTime().Before(end))
s.NoError(err)
s.NotNil(created)

Expand Down
6 changes: 6 additions & 0 deletions service/integration/namespaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log/slog"
"strings"
"testing"
"time"

"github.com/opentdf/platform/protocol/go/common"
"github.com/opentdf/platform/protocol/go/policy"
Expand Down Expand Up @@ -158,14 +159,19 @@ func (s *NamespacesSuite) Test_UpdateNamespace() {
"update": updatedLabel,
"new": newLabel,
}
start := time.Now().Add(-time.Second)
created, err := s.db.PolicyClient.CreateNamespace(s.ctx, &namespaces.CreateNamespaceRequest{
Name: "updating-namespace.com",
Metadata: &common.MetadataMutable{
Labels: labels,
},
})
end := time.Now().Add(time.Second)
metadata := created.GetMetadata()
createdAt := metadata.GetCreatedAt()
updatedAt := metadata.GetUpdatedAt()
s.True(createdAt.AsTime().After(start))
s.True(createdAt.AsTime().Before(end))

s.NoError(err)
s.NotNil(created)
Expand Down
6 changes: 6 additions & 0 deletions service/integration/resource_mappings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log/slog"
"testing"
"time"

"github.com/opentdf/platform/protocol/go/common"
"github.com/opentdf/platform/protocol/go/policy/resourcemapping"
Expand Down Expand Up @@ -192,15 +193,20 @@ func (s *ResourceMappingsSuite) Test_UpdateResourceMapping() {
updateTerms := []string{"updated term1", "updated term 2"}

attrValue := s.f.GetAttributeValueKey("example.com/attr/attr2/value/value2")
start := time.Now().Add(-time.Second)
createdMapping, err := s.db.PolicyClient.CreateResourceMapping(s.ctx, &resourcemapping.CreateResourceMappingRequest{
AttributeValueId: attrValue.Id,
Metadata: &common.MetadataMutable{
Labels: labels,
},
Terms: terms,
})
end := time.Now().Add(time.Second)
metadata := createdMapping.GetMetadata()
updatedAt := metadata.GetUpdatedAt()
createdAt := metadata.GetCreatedAt()
s.True(createdAt.AsTime().After(start))
s.True(createdAt.AsTime().Before(end))
s.NoError(err)
s.NotNil(createdMapping)

Expand Down
13 changes: 11 additions & 2 deletions service/integration/subject_mappings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"log/slog"
"testing"
"time"

"github.com/opentdf/platform/protocol/go/common"
"github.com/opentdf/platform/protocol/go/policy"
Expand Down Expand Up @@ -203,10 +204,14 @@ func (s *SubjectMappingsSuite) TestUpdateSubjectMapping_Actions() {
Actions: []*policy.Action{aTransmit, aCustomUpload},
ExistingSubjectConditionSetId: fixtureScs.Id,
}

start := time.Now().Add(-time.Second)
created, err := s.db.PolicyClient.CreateSubjectMapping(context.Background(), newSubjectMapping)
end := time.Now().Add(time.Second)
metadata := created.GetMetadata()
updatedAt := metadata.GetUpdatedAt()
createdAt := metadata.GetCreatedAt()
s.True(createdAt.AsTime().After(start))
s.True(createdAt.AsTime().Before(end))
s.Require().NoError(err)
s.NotNil(created)

Expand Down Expand Up @@ -630,10 +635,14 @@ func (s *SubjectMappingsSuite) TestUpdateSubjectConditionSet_NewSubjectSets() {
{},
},
}

start := time.Now().Add(-time.Second)
created, err := s.db.PolicyClient.CreateSubjectConditionSet(context.Background(), newConditionSet)
end := time.Now().Add(time.Second)
metadata := created.GetMetadata()
updatedAt := metadata.GetUpdatedAt()
createdAt := metadata.GetCreatedAt()
s.True(createdAt.AsTime().After(start))
s.True(createdAt.AsTime().Before(end))
s.Require().NoError(err)
s.NotNil(created)

Expand Down
21 changes: 13 additions & 8 deletions service/policy/db/attribute_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func createAttributeValueSql(
value,
metadata,
).
Suffix("RETURNING id").
Suffix(createSuffix).
ToSql()
}

Expand All @@ -162,14 +162,15 @@ func (c PolicyDBClient) CreateAttributeValue(ctx context.Context, attributeID st
value,
metadataJSON,
)

if err != nil {
return nil, err
}

var id string
if r, err := c.QueryRow(ctx, sql, args); err != nil {
return nil, err
} else if err := r.Scan(&id); err != nil {
} else if err := r.Scan(&id, &metadataJSON); err != nil {
return nil, db.WrapIfKnownInvalidQueryErr(err)
}

Expand All @@ -194,6 +195,10 @@ func (c PolicyDBClient) CreateAttributeValue(ctx context.Context, attributeID st
members = append(members, attr)
}

if err = unmarshalMetadata(metadataJSON, metadata); err != nil {
return nil, err
}

// Update FQN
c.upsertAttrFqn(ctx, attrFqnUpsertOptions{valueId: id})

Expand All @@ -216,7 +221,7 @@ func getAttributeValueSql(id string, opts attributeValueSelectOptions) (string,
"'value', vmv.value, " +
"'active', vmv.active, " +
"'members', vmv.members || ARRAY[]::UUID[], " +
getMetadataField("vmv", true) +
constructMetadata("vmv", true) +
"'attribute', JSON_BUILD_OBJECT(" +
"'id', vmv.attribute_definition_id )"
if opts.withFqn {
Expand All @@ -228,7 +233,7 @@ func getAttributeValueSql(id string, opts attributeValueSelectOptions) (string,
"av.value",
"av.active",
members,
getMetadataField("av", false),
constructMetadata("av", false),
"av.attribute_definition_id",
}
if opts.withFqn {
Expand Down Expand Up @@ -281,7 +286,7 @@ func listAttributeValuesSql(attribute_id string, opts attributeValueSelectOption
"'value', vmv.value, " +
"'active', vmv.active, " +
"'members', vmv.members || ARRAY[]::UUID[], " +
getMetadataField("vmv", true) +
constructMetadata("vmv", true) +
"'attribute', JSON_BUILD_OBJECT(" +
"'id', vmv.attribute_definition_id )"
if opts.withFqn {
Expand All @@ -293,7 +298,7 @@ func listAttributeValuesSql(attribute_id string, opts attributeValueSelectOption
"av.value",
"av.active",
members,
getMetadataField("av", false),
constructMetadata("av", false),
"av.attribute_definition_id",
}
if opts.withFqn {
Expand Down Expand Up @@ -352,7 +357,7 @@ func listAllAttributeValuesSql(opts attributeValueSelectOptions) (string, []inte
"'value', vmv.value, " +
"'active', vmv.active, " +
"'members', vmv.members || ARRAY[]::UUID[], " +
getMetadataField("vmv", true) +
constructMetadata("vmv", true) +
"'attribute', JSON_BUILD_OBJECT(" +
"'id', vmv.attribute_definition_id )"
if opts.withFqn {
Expand All @@ -364,7 +369,7 @@ func listAllAttributeValuesSql(opts attributeValueSelectOptions) (string, []inte
"av.value",
"av.active",
members,
getMetadataField("av", false),
constructMetadata("av", false),
"av.attribute_definition_id",
}
if opts.withFqn {
Expand Down
14 changes: 9 additions & 5 deletions service/policy/db/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func attributesSelect(opts attributesSelectOptions) sq.SelectBuilder {
t.Field("id"),
t.Field("name"),
t.Field("rule"),
getMetadataField(t.Name(), false),
constructMetadata(t.Name(), false),
t.Field("namespace_id"),
t.Field("active"),
nt.Field("name"),
Expand Down Expand Up @@ -152,10 +152,10 @@ func attributesSelect(opts attributesSelectOptions) sq.SelectBuilder {
"JSON_AGG(JSON_BUILD_OBJECT(" +
"'id', " + smT.Field("id") + "," +
"'actions', " + smT.Field("actions") + "," +
getMetadataField(smT.Name(), true) +
constructMetadata(smT.Name(), true) +
"'subject_condition_set', JSON_BUILD_OBJECT(" +
"'id', " + scsT.Field("id") + "," +
getMetadataField(scsT.Name(), true) +
constructMetadata(scsT.Name(), true) +
"'subject_sets', " + scsT.Field("condition") +
")" +
")) AS sub_maps_arr " +
Expand Down Expand Up @@ -468,7 +468,7 @@ func createAttributeSql(namespaceId string, name string, rule string, metadata [
Insert(t.Name()).
Columns("namespace_id", "name", "rule", "metadata").
Values(namespaceId, name, rule, metadata).
Suffix("RETURNING \"id\"").
Suffix(createSuffix).
ToSql()
}

Expand All @@ -488,10 +488,14 @@ func (c PolicyDBClient) CreateAttribute(ctx context.Context, r *attributes.Creat
var id string
if r, err := c.QueryRow(ctx, sql, args); err != nil {
return nil, err
} else if err := r.Scan(&id); err != nil {
} else if err := r.Scan(&id, &metadataJSON); err != nil {
return nil, db.WrapIfKnownInvalidQueryErr(err)
}

if err = unmarshalMetadata(metadataJSON, metadata); err != nil {
return nil, err
}

// Update the FQN
c.upsertAttrFqn(ctx, attrFqnUpsertOptions{attributeId: id})

Expand Down
12 changes: 8 additions & 4 deletions service/policy/db/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func getNamespaceSql(id string, opts namespaceSelectOptions) (string, []interfac
t.Field("id"),
t.Field("name"),
t.Field("active"),
getMetadataField("", false),
constructMetadata("", false),
}

if opts.withFqn {
Expand Down Expand Up @@ -126,7 +126,7 @@ func listNamespacesSql(opts namespaceSelectOptions) (string, []interface{}, erro
t.Field("id"),
t.Field("name"),
t.Field("active"),
getMetadataField("", false),
constructMetadata("", false),
}

if opts.withFqn {
Expand Down Expand Up @@ -180,7 +180,7 @@ func createNamespaceSql(name string, metadata []byte) (string, []interface{}, er
Insert(t.Name()).
Columns("name", "metadata").
Values(name, metadata).
Suffix("RETURNING \"id\"").
Suffix(createSuffix).
ToSql()
}

Expand All @@ -199,10 +199,14 @@ func (c PolicyDBClient) CreateNamespace(ctx context.Context, r *namespaces.Creat
var id string
if r, e := c.QueryRow(ctx, sql, args); e != nil {
return nil, e
} else if e := r.Scan(&id); e != nil {
} else if e = r.Scan(&id, &metadataJSON); e != nil {
return nil, db.WrapIfKnownInvalidQueryErr(e)
}

if err = unmarshalMetadata(metadataJSON, m); err != nil {
return nil, err
}

// Update FQN
c.upsertAttrFqn(ctx, attrFqnUpsertOptions{namespaceId: id})

Expand Down
10 changes: 7 additions & 3 deletions service/policy/db/resource_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func resourceMappingSelect() sq.SelectBuilder {
")) FILTER (WHERE vmv.id IS NOT NULL ), '[]')"
return db.NewStatementBuilder().Select(
t.Field("id"),
getMetadataField(t.Name(), false),
constructMetadata(t.Name(), false),
t.Field("terms"),
"JSON_BUILD_OBJECT("+
"'id', av.id,"+
Expand Down Expand Up @@ -111,7 +111,7 @@ func createResourceMappingSQL(attributeValueID string, metadata []byte, terms []
metadata,
terms,
).
Suffix("RETURNING \"id\"").
Suffix(createSuffix).
ToSql()
}

Expand All @@ -132,7 +132,7 @@ func (c PolicyDBClient) CreateResourceMapping(ctx context.Context, r *resourcema
}

var id string
if err := row.Scan(&id); err != nil {
if err := row.Scan(&id, &metadataJSON); err != nil {
return nil, db.WrapIfKnownInvalidQueryErr(err)
}

Expand All @@ -142,6 +142,10 @@ func (c PolicyDBClient) CreateResourceMapping(ctx context.Context, r *resourcema
return nil, db.WrapIfKnownInvalidQueryErr(err)
}

if err = unmarshalMetadata(metadataJSON, metadata); err != nil {
return nil, err
}

return &policy.ResourceMapping{
Id: id,
Metadata: metadata,
Expand Down
Loading