Skip to content

Commit fcaaeea

Browse files
authored
fix(policy): return created_at and updated_at timestamps in CREATE metadata (#557)
1 parent 50fa686 commit fcaaeea

File tree

11 files changed

+108
-30
lines changed

11 files changed

+108
-30
lines changed

service/integration/attribute_values_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"sort"
77
"strings"
88
"testing"
9+
"time"
910

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

286287
// create a value
287288
attrDef := s.f.GetAttributeKey("example.net/attr/attr1")
289+
start := time.Now().Add(-time.Second)
288290
created, err := s.db.PolicyClient.CreateAttributeValue(s.ctx, attrDef.Id, &attributes.CreateAttributeValueRequest{
289291
Value: "created value testing update",
290292
Metadata: &common.MetadataMutable{
291293
Labels: labels,
292294
},
293295
})
296+
end := time.Now().Add(time.Second)
294297
metadata := created.GetMetadata()
295298
updatedAt := metadata.GetUpdatedAt()
299+
createdAt := metadata.GetCreatedAt()
300+
s.True(createdAt.AsTime().After(start))
301+
s.True(createdAt.AsTime().Before(end))
296302
s.NoError(err)
297303
s.NotNil(created)
298304

service/integration/attributes_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log/slog"
77
"strings"
88
"testing"
9+
"time"
910

1011
"github.com/opentdf/platform/protocol/go/common"
1112
"github.com/opentdf/platform/protocol/go/policy"
@@ -404,9 +405,14 @@ func (s *AttributesSuite) Test_UpdateAttribute() {
404405
Labels: labels,
405406
},
406407
}
408+
start := time.Now().Add(-time.Second)
407409
created, err := s.db.PolicyClient.CreateAttribute(s.ctx, attr)
410+
end := time.Now().Add(time.Second)
408411
metadata := created.GetMetadata()
409412
updatedAt := metadata.GetUpdatedAt()
413+
createdAt := metadata.GetCreatedAt()
414+
s.True(createdAt.AsTime().After(start))
415+
s.True(createdAt.AsTime().Before(end))
410416
s.NoError(err)
411417
s.NotNil(created)
412418

service/integration/namespaces_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log/slog"
77
"strings"
88
"testing"
9+
"time"
910

1011
"github.com/opentdf/platform/protocol/go/common"
1112
"github.com/opentdf/platform/protocol/go/policy"
@@ -158,14 +159,19 @@ func (s *NamespacesSuite) Test_UpdateNamespace() {
158159
"update": updatedLabel,
159160
"new": newLabel,
160161
}
162+
start := time.Now().Add(-time.Second)
161163
created, err := s.db.PolicyClient.CreateNamespace(s.ctx, &namespaces.CreateNamespaceRequest{
162164
Name: "updating-namespace.com",
163165
Metadata: &common.MetadataMutable{
164166
Labels: labels,
165167
},
166168
})
169+
end := time.Now().Add(time.Second)
167170
metadata := created.GetMetadata()
171+
createdAt := metadata.GetCreatedAt()
168172
updatedAt := metadata.GetUpdatedAt()
173+
s.True(createdAt.AsTime().After(start))
174+
s.True(createdAt.AsTime().Before(end))
169175

170176
s.NoError(err)
171177
s.NotNil(created)

service/integration/resource_mappings_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"log/slog"
77
"testing"
8+
"time"
89

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

194195
attrValue := s.f.GetAttributeValueKey("example.com/attr/attr2/value/value2")
196+
start := time.Now().Add(-time.Second)
195197
createdMapping, err := s.db.PolicyClient.CreateResourceMapping(s.ctx, &resourcemapping.CreateResourceMappingRequest{
196198
AttributeValueId: attrValue.Id,
197199
Metadata: &common.MetadataMutable{
198200
Labels: labels,
199201
},
200202
Terms: terms,
201203
})
204+
end := time.Now().Add(time.Second)
202205
metadata := createdMapping.GetMetadata()
203206
updatedAt := metadata.GetUpdatedAt()
207+
createdAt := metadata.GetCreatedAt()
208+
s.True(createdAt.AsTime().After(start))
209+
s.True(createdAt.AsTime().Before(end))
204210
s.NoError(err)
205211
s.NotNil(createdMapping)
206212

service/integration/subject_mappings_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"log/slog"
66
"testing"
7+
"time"
78

89
"github.com/opentdf/platform/protocol/go/common"
910
"github.com/opentdf/platform/protocol/go/policy"
@@ -203,10 +204,14 @@ func (s *SubjectMappingsSuite) TestUpdateSubjectMapping_Actions() {
203204
Actions: []*policy.Action{aTransmit, aCustomUpload},
204205
ExistingSubjectConditionSetId: fixtureScs.Id,
205206
}
206-
207+
start := time.Now().Add(-time.Second)
207208
created, err := s.db.PolicyClient.CreateSubjectMapping(context.Background(), newSubjectMapping)
209+
end := time.Now().Add(time.Second)
208210
metadata := created.GetMetadata()
209211
updatedAt := metadata.GetUpdatedAt()
212+
createdAt := metadata.GetCreatedAt()
213+
s.True(createdAt.AsTime().After(start))
214+
s.True(createdAt.AsTime().Before(end))
210215
s.Require().NoError(err)
211216
s.NotNil(created)
212217

@@ -630,10 +635,14 @@ func (s *SubjectMappingsSuite) TestUpdateSubjectConditionSet_NewSubjectSets() {
630635
{},
631636
},
632637
}
633-
638+
start := time.Now().Add(-time.Second)
634639
created, err := s.db.PolicyClient.CreateSubjectConditionSet(context.Background(), newConditionSet)
640+
end := time.Now().Add(time.Second)
635641
metadata := created.GetMetadata()
636642
updatedAt := metadata.GetUpdatedAt()
643+
createdAt := metadata.GetCreatedAt()
644+
s.True(createdAt.AsTime().After(start))
645+
s.True(createdAt.AsTime().Before(end))
637646
s.Require().NoError(err)
638647
s.NotNil(created)
639648

service/policy/db/attribute_values.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func createAttributeValueSql(
145145
value,
146146
metadata,
147147
).
148-
Suffix("RETURNING id").
148+
Suffix(createSuffix).
149149
ToSql()
150150
}
151151

@@ -162,14 +162,15 @@ func (c PolicyDBClient) CreateAttributeValue(ctx context.Context, attributeID st
162162
value,
163163
metadataJSON,
164164
)
165+
165166
if err != nil {
166167
return nil, err
167168
}
168169

169170
var id string
170171
if r, err := c.QueryRow(ctx, sql, args); err != nil {
171172
return nil, err
172-
} else if err := r.Scan(&id); err != nil {
173+
} else if err := r.Scan(&id, &metadataJSON); err != nil {
173174
return nil, db.WrapIfKnownInvalidQueryErr(err)
174175
}
175176

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

198+
if err = unmarshalMetadata(metadataJSON, metadata); err != nil {
199+
return nil, err
200+
}
201+
197202
// Update FQN
198203
c.upsertAttrFqn(ctx, attrFqnUpsertOptions{valueId: id})
199204

@@ -216,7 +221,7 @@ func getAttributeValueSql(id string, opts attributeValueSelectOptions) (string,
216221
"'value', vmv.value, " +
217222
"'active', vmv.active, " +
218223
"'members', vmv.members || ARRAY[]::UUID[], " +
219-
getMetadataField("vmv", true) +
224+
constructMetadata("vmv", true) +
220225
"'attribute', JSON_BUILD_OBJECT(" +
221226
"'id', vmv.attribute_definition_id )"
222227
if opts.withFqn {
@@ -228,7 +233,7 @@ func getAttributeValueSql(id string, opts attributeValueSelectOptions) (string,
228233
"av.value",
229234
"av.active",
230235
members,
231-
getMetadataField("av", false),
236+
constructMetadata("av", false),
232237
"av.attribute_definition_id",
233238
}
234239
if opts.withFqn {
@@ -281,7 +286,7 @@ func listAttributeValuesSql(attribute_id string, opts attributeValueSelectOption
281286
"'value', vmv.value, " +
282287
"'active', vmv.active, " +
283288
"'members', vmv.members || ARRAY[]::UUID[], " +
284-
getMetadataField("vmv", true) +
289+
constructMetadata("vmv", true) +
285290
"'attribute', JSON_BUILD_OBJECT(" +
286291
"'id', vmv.attribute_definition_id )"
287292
if opts.withFqn {
@@ -293,7 +298,7 @@ func listAttributeValuesSql(attribute_id string, opts attributeValueSelectOption
293298
"av.value",
294299
"av.active",
295300
members,
296-
getMetadataField("av", false),
301+
constructMetadata("av", false),
297302
"av.attribute_definition_id",
298303
}
299304
if opts.withFqn {
@@ -352,7 +357,7 @@ func listAllAttributeValuesSql(opts attributeValueSelectOptions) (string, []inte
352357
"'value', vmv.value, " +
353358
"'active', vmv.active, " +
354359
"'members', vmv.members || ARRAY[]::UUID[], " +
355-
getMetadataField("vmv", true) +
360+
constructMetadata("vmv", true) +
356361
"'attribute', JSON_BUILD_OBJECT(" +
357362
"'id', vmv.attribute_definition_id )"
358363
if opts.withFqn {
@@ -364,7 +369,7 @@ func listAllAttributeValuesSql(opts attributeValueSelectOptions) (string, []inte
364369
"av.value",
365370
"av.active",
366371
members,
367-
getMetadataField("av", false),
372+
constructMetadata("av", false),
368373
"av.attribute_definition_id",
369374
}
370375
if opts.withFqn {

service/policy/db/attributes.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func attributesSelect(opts attributesSelectOptions) sq.SelectBuilder {
8080
t.Field("id"),
8181
t.Field("name"),
8282
t.Field("rule"),
83-
getMetadataField(t.Name(), false),
83+
constructMetadata(t.Name(), false),
8484
t.Field("namespace_id"),
8585
t.Field("active"),
8686
nt.Field("name"),
@@ -152,10 +152,10 @@ func attributesSelect(opts attributesSelectOptions) sq.SelectBuilder {
152152
"JSON_AGG(JSON_BUILD_OBJECT(" +
153153
"'id', " + smT.Field("id") + "," +
154154
"'actions', " + smT.Field("actions") + "," +
155-
getMetadataField(smT.Name(), true) +
155+
constructMetadata(smT.Name(), true) +
156156
"'subject_condition_set', JSON_BUILD_OBJECT(" +
157157
"'id', " + scsT.Field("id") + "," +
158-
getMetadataField(scsT.Name(), true) +
158+
constructMetadata(scsT.Name(), true) +
159159
"'subject_sets', " + scsT.Field("condition") +
160160
")" +
161161
")) AS sub_maps_arr " +
@@ -468,7 +468,7 @@ func createAttributeSql(namespaceId string, name string, rule string, metadata [
468468
Insert(t.Name()).
469469
Columns("namespace_id", "name", "rule", "metadata").
470470
Values(namespaceId, name, rule, metadata).
471-
Suffix("RETURNING \"id\"").
471+
Suffix(createSuffix).
472472
ToSql()
473473
}
474474

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

495+
if err = unmarshalMetadata(metadataJSON, metadata); err != nil {
496+
return nil, err
497+
}
498+
495499
// Update the FQN
496500
c.upsertAttrFqn(ctx, attrFqnUpsertOptions{attributeId: id})
497501

service/policy/db/namespaces.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func getNamespaceSql(id string, opts namespaceSelectOptions) (string, []interfac
7777
t.Field("id"),
7878
t.Field("name"),
7979
t.Field("active"),
80-
getMetadataField("", false),
80+
constructMetadata("", false),
8181
}
8282

8383
if opts.withFqn {
@@ -126,7 +126,7 @@ func listNamespacesSql(opts namespaceSelectOptions) (string, []interface{}, erro
126126
t.Field("id"),
127127
t.Field("name"),
128128
t.Field("active"),
129-
getMetadataField("", false),
129+
constructMetadata("", false),
130130
}
131131

132132
if opts.withFqn {
@@ -180,7 +180,7 @@ func createNamespaceSql(name string, metadata []byte) (string, []interface{}, er
180180
Insert(t.Name()).
181181
Columns("name", "metadata").
182182
Values(name, metadata).
183-
Suffix("RETURNING \"id\"").
183+
Suffix(createSuffix).
184184
ToSql()
185185
}
186186

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

206+
if err = unmarshalMetadata(metadataJSON, m); err != nil {
207+
return nil, err
208+
}
209+
206210
// Update FQN
207211
c.upsertAttrFqn(ctx, attrFqnUpsertOptions{namespaceId: id})
208212

service/policy/db/resource_mapping.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func resourceMappingSelect() sq.SelectBuilder {
7979
")) FILTER (WHERE vmv.id IS NOT NULL ), '[]')"
8080
return db.NewStatementBuilder().Select(
8181
t.Field("id"),
82-
getMetadataField(t.Name(), false),
82+
constructMetadata(t.Name(), false),
8383
t.Field("terms"),
8484
"JSON_BUILD_OBJECT("+
8585
"'id', av.id,"+
@@ -111,7 +111,7 @@ func createResourceMappingSQL(attributeValueID string, metadata []byte, terms []
111111
metadata,
112112
terms,
113113
).
114-
Suffix("RETURNING \"id\"").
114+
Suffix(createSuffix).
115115
ToSql()
116116
}
117117

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

134134
var id string
135-
if err := row.Scan(&id); err != nil {
135+
if err := row.Scan(&id, &metadataJSON); err != nil {
136136
return nil, db.WrapIfKnownInvalidQueryErr(err)
137137
}
138138

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

145+
if err = unmarshalMetadata(metadataJSON, metadata); err != nil {
146+
return nil, err
147+
}
148+
145149
return &policy.ResourceMapping{
146150
Id: id,
147151
Metadata: metadata,

0 commit comments

Comments
 (0)