Skip to content

Commit e812563

Browse files
alkalescentelizabethhealyjakedoublevpflynn-virtru
authored
feat(policy): add created_at and updated_at timestamps to metadata (#538)
Co-authored-by: Elizabeth Healy <[email protected]> Co-authored-by: Jake Van Vorhis <[email protected]> Co-authored-by: Paul Flynn <[email protected]>
1 parent 1949a9c commit e812563

File tree

11 files changed

+79
-16
lines changed

11 files changed

+79
-16
lines changed

service/integration/attribute_values_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ func (s *AttributeValuesSuite) Test_GetAttributeValue() {
8181
s.Equal(len(f.Members), len(v.GetMembers()))
8282
// s.Equal(f.AttributeDefinitionId, v.AttributeId)
8383
s.Equal("https://example.com/attr/attr1/value/value1", v.GetFqn())
84+
metadata := v.GetMetadata()
85+
createdAt := metadata.GetCreatedAt()
86+
updatedAt := metadata.GetUpdatedAt()
87+
s.True(createdAt.IsValid() && createdAt.AsTime().Unix() > 0)
88+
s.True(updatedAt.IsValid() && updatedAt.AsTime().Unix() > 0)
8489
}
8590

8691
func (s *AttributeValuesSuite) Test_GetAttributeValue_NotFound() {
@@ -286,6 +291,8 @@ func (s *AttributeValuesSuite) Test_UpdateAttributeValue() {
286291
Labels: labels,
287292
},
288293
})
294+
metadata := created.GetMetadata()
295+
updatedAt := metadata.GetUpdatedAt()
289296
s.NoError(err)
290297
s.NotNil(created)
291298

@@ -315,6 +322,7 @@ func (s *AttributeValuesSuite) Test_UpdateAttributeValue() {
315322
s.NotNil(got)
316323
s.Equal(created.GetId(), got.GetId())
317324
s.EqualValues(expectedLabels, got.GetMetadata().GetLabels())
325+
s.True(got.GetMetadata().GetUpdatedAt().AsTime().After(updatedAt.AsTime()))
318326
}
319327

320328
func (s *AttributeValuesSuite) Test_UpdateAttributeValue_WithInvalidId_Fails() {

service/integration/attributes_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ func (s *AttributesSuite) Test_GetAttribute() {
300300
s.Equal(f.Name, gotAttr.GetName())
301301
s.Equal(fmt.Sprintf("%s%s", policydb.AttributeRuleTypeEnumPrefix, f.Rule), gotAttr.GetRule().Enum().String())
302302
s.Equal(f.NamespaceId, gotAttr.GetNamespace().GetId())
303+
metadata := gotAttr.GetMetadata()
304+
createdAt := metadata.GetCreatedAt()
305+
updatedAt := metadata.GetUpdatedAt()
306+
s.True(createdAt.IsValid() && createdAt.AsTime().Unix() > 0)
307+
s.True(updatedAt.IsValid() && updatedAt.AsTime().Unix() > 0)
303308
}
304309
}
305310

@@ -400,6 +405,8 @@ func (s *AttributesSuite) Test_UpdateAttribute() {
400405
},
401406
}
402407
created, err := s.db.PolicyClient.CreateAttribute(s.ctx, attr)
408+
metadata := created.GetMetadata()
409+
updatedAt := metadata.GetUpdatedAt()
403410
s.NoError(err)
404411
s.NotNil(created)
405412

@@ -425,6 +432,7 @@ func (s *AttributesSuite) Test_UpdateAttribute() {
425432
s.NotNil(got)
426433
s.Equal(created.GetId(), got.GetId())
427434
s.EqualValues(expectedLabels, got.GetMetadata().GetLabels())
435+
s.True(got.GetMetadata().GetUpdatedAt().AsTime().After(updatedAt.AsTime()))
428436
}
429437

430438
func (s *AttributesSuite) Test_UpdateAttribute_WithInvalidIdFails() {

service/integration/namespaces_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ func (s *NamespacesSuite) Test_GetNamespace() {
9999
s.NotNil(gotNamespace)
100100
// name retrieved by ID equal to name used to create
101101
s.Equal(test.Name, gotNamespace.GetName())
102+
metadata := gotNamespace.GetMetadata()
103+
createdAt := metadata.GetCreatedAt()
104+
updatedAt := metadata.GetUpdatedAt()
105+
s.True(createdAt.IsValid() && createdAt.AsTime().Unix() > 0)
106+
s.True(updatedAt.IsValid() && updatedAt.AsTime().Unix() > 0)
102107
}
103108

104109
// Getting a namespace with an nonExistent id should fail
@@ -153,13 +158,15 @@ func (s *NamespacesSuite) Test_UpdateNamespace() {
153158
"update": updatedLabel,
154159
"new": newLabel,
155160
}
156-
157161
created, err := s.db.PolicyClient.CreateNamespace(s.ctx, &namespaces.CreateNamespaceRequest{
158162
Name: "updating-namespace.com",
159163
Metadata: &common.MetadataMutable{
160164
Labels: labels,
161165
},
162166
})
167+
metadata := created.GetMetadata()
168+
updatedAt := metadata.GetUpdatedAt()
169+
163170
s.NoError(err)
164171
s.NotNil(created)
165172

@@ -183,6 +190,7 @@ func (s *NamespacesSuite) Test_UpdateNamespace() {
183190
s.NotNil(got)
184191
s.Equal(created.GetId(), got.GetId())
185192
s.EqualValues(expectedLabels, got.GetMetadata().GetLabels())
193+
s.True(got.GetMetadata().GetUpdatedAt().AsTime().After(updatedAt.AsTime()))
186194
}
187195

188196
func (s *NamespacesSuite) Test_UpdateNamespace_DoesNotExist_ShouldFail() {

service/integration/resource_mappings_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ func (s *ResourceMappingsSuite) Test_GetResourceMapping() {
132132
s.True(testedMembers, "expected to test at least one attribute value member")
133133
}
134134
equalMembers(s.T(), av, mapping.GetAttributeValue(), false)
135+
metadata := mapping.GetMetadata()
136+
createdAt := metadata.GetCreatedAt()
137+
updatedAt := metadata.GetUpdatedAt()
138+
s.True(createdAt.IsValid() && createdAt.AsTime().Unix() > 0)
139+
s.True(updatedAt.IsValid() && updatedAt.AsTime().Unix() > 0)
135140
}
136141
}
137142

@@ -194,6 +199,8 @@ func (s *ResourceMappingsSuite) Test_UpdateResourceMapping() {
194199
},
195200
Terms: terms,
196201
})
202+
metadata := createdMapping.GetMetadata()
203+
updatedAt := metadata.GetUpdatedAt()
197204
s.NoError(err)
198205
s.NotNil(createdMapping)
199206

@@ -232,6 +239,7 @@ func (s *ResourceMappingsSuite) Test_UpdateResourceMapping() {
232239
s.Equal(createdMapping.GetAttributeValue().GetId(), got.GetAttributeValue().GetId())
233240
s.Equal(updateTerms, got.GetTerms())
234241
s.EqualValues(expectedLabels, got.GetMetadata().GetLabels())
242+
s.True(got.GetMetadata().GetUpdatedAt().AsTime().After(updatedAt.AsTime()))
235243
}
236244

237245
func (s *ResourceMappingsSuite) Test_UpdateResourceMappingWithUnknownIdFails() {

service/integration/subject_mappings_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ func (s *SubjectMappingsSuite) TestUpdateSubjectMapping_Actions() {
205205
}
206206

207207
created, err := s.db.PolicyClient.CreateSubjectMapping(context.Background(), newSubjectMapping)
208+
metadata := created.GetMetadata()
209+
updatedAt := metadata.GetUpdatedAt()
208210
s.Require().NoError(err)
209211
s.NotNil(created)
210212

@@ -228,6 +230,7 @@ func (s *SubjectMappingsSuite) TestUpdateSubjectMapping_Actions() {
228230
s.Equal(got.GetActions(), newActions)
229231
s.Equal(newSubjectMapping.GetAttributeValueId(), got.GetAttributeValue().GetId())
230232
s.Equal(newSubjectMapping.GetExistingSubjectConditionSetId(), got.GetSubjectConditionSet().GetId())
233+
s.True(got.GetMetadata().GetUpdatedAt().AsTime().After(updatedAt.AsTime()))
231234
}
232235

233236
func (s *SubjectMappingsSuite) TestUpdateSubjectMapping_SubjectConditionSetId() {
@@ -370,6 +373,11 @@ func (s *SubjectMappingsSuite) TestGetSubjectMapping() {
370373
s.Equal(fixture.AttributeValueId, got.GetId())
371374
s.NotEmpty(got.GetMembers())
372375
equalMembers(s.T(), got, sm.GetAttributeValue(), false)
376+
metadata := sm.GetMetadata()
377+
createdAt := metadata.GetCreatedAt()
378+
updatedAt := metadata.GetUpdatedAt()
379+
s.True(createdAt.IsValid() && createdAt.AsTime().Unix() > 0)
380+
s.True(updatedAt.IsValid() && updatedAt.AsTime().Unix() > 0)
373381
}
374382

375383
func (s *SubjectMappingsSuite) TestGetSubjectMapping_NonExistentId_Fails() {
@@ -533,6 +541,11 @@ func (s *SubjectMappingsSuite) TestGetSubjectConditionSet_ById() {
533541
s.Require().NoError(err)
534542
s.NotNil(scs)
535543
s.Equal(fixture.Id, scs.GetId())
544+
metadata := scs.GetMetadata()
545+
createdAt := metadata.GetCreatedAt()
546+
updatedAt := metadata.GetUpdatedAt()
547+
s.True(createdAt.IsValid() && createdAt.AsTime().Unix() > 0)
548+
s.True(updatedAt.IsValid() && updatedAt.AsTime().Unix() > 0)
536549
}
537550

538551
func (s *SubjectMappingsSuite) TestGetSubjectConditionSet_WithNoId_Fails() {
@@ -619,6 +632,8 @@ func (s *SubjectMappingsSuite) TestUpdateSubjectConditionSet_NewSubjectSets() {
619632
}
620633

621634
created, err := s.db.PolicyClient.CreateSubjectConditionSet(context.Background(), newConditionSet)
635+
metadata := created.GetMetadata()
636+
updatedAt := metadata.GetUpdatedAt()
622637
s.Require().NoError(err)
623638
s.NotNil(created)
624639

@@ -658,6 +673,7 @@ func (s *SubjectMappingsSuite) TestUpdateSubjectConditionSet_NewSubjectSets() {
658673
s.Equal(created.GetId(), got.GetId())
659674
s.Equal(len(ss), len(got.GetSubjectSets()))
660675
s.Equal(ss[0].GetConditionGroups()[0].GetConditions()[0].GetSubjectExternalSelectorValue(), got.GetSubjectSets()[0].GetConditionGroups()[0].GetConditions()[0].GetSubjectExternalSelectorValue())
676+
s.True(got.GetMetadata().GetUpdatedAt().AsTime().After(updatedAt.AsTime()))
661677
}
662678

663679
func (s *SubjectMappingsSuite) TestUpdateSubjectConditionSet_AllAllowedFields() {

service/policy/db/attribute_values.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func getAttributeValueSql(id string, opts attributeValueSelectOptions) (string,
216216
"'value', vmv.value, " +
217217
"'active', vmv.active, " +
218218
"'members', vmv.members || ARRAY[]::UUID[], " +
219-
"'metadata', vmv.metadata, " +
219+
getMetadataField("vmv", true) +
220220
"'attribute', JSON_BUILD_OBJECT(" +
221221
"'id', vmv.attribute_definition_id )"
222222
if opts.withFqn {
@@ -228,7 +228,7 @@ func getAttributeValueSql(id string, opts attributeValueSelectOptions) (string,
228228
"av.value",
229229
"av.active",
230230
members,
231-
"av.metadata",
231+
getMetadataField("av", false),
232232
"av.attribute_definition_id",
233233
}
234234
if opts.withFqn {
@@ -281,7 +281,7 @@ func listAttributeValuesSql(attribute_id string, opts attributeValueSelectOption
281281
"'value', vmv.value, " +
282282
"'active', vmv.active, " +
283283
"'members', vmv.members || ARRAY[]::UUID[], " +
284-
"'metadata', vmv.metadata, " +
284+
getMetadataField("vmv", true) +
285285
"'attribute', JSON_BUILD_OBJECT(" +
286286
"'id', vmv.attribute_definition_id )"
287287
if opts.withFqn {
@@ -293,7 +293,7 @@ func listAttributeValuesSql(attribute_id string, opts attributeValueSelectOption
293293
"av.value",
294294
"av.active",
295295
members,
296-
"av.metadata",
296+
getMetadataField("av", false),
297297
"av.attribute_definition_id",
298298
}
299299
if opts.withFqn {
@@ -352,7 +352,7 @@ func listAllAttributeValuesSql(opts attributeValueSelectOptions) (string, []inte
352352
"'value', vmv.value, " +
353353
"'active', vmv.active, " +
354354
"'members', vmv.members || ARRAY[]::UUID[], " +
355-
"'metadata', vmv.metadata, " +
355+
getMetadataField("vmv", true) +
356356
"'attribute', JSON_BUILD_OBJECT(" +
357357
"'id', vmv.attribute_definition_id )"
358358
if opts.withFqn {
@@ -364,7 +364,7 @@ func listAllAttributeValuesSql(opts attributeValueSelectOptions) (string, []inte
364364
"av.value",
365365
"av.active",
366366
members,
367-
"av.metadata",
367+
getMetadataField("av", false),
368368
"av.attribute_definition_id",
369369
}
370370
if opts.withFqn {

service/policy/db/attributes.go

Lines changed: 3 additions & 3 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-
t.Field("metadata"),
83+
getMetadataField(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-
"'metadata', " + smT.Field("metadata") + "," +
155+
getMetadataField(smT.Name(), true) +
156156
"'subject_condition_set', JSON_BUILD_OBJECT(" +
157157
"'id', " + scsT.Field("id") + "," +
158-
"'metadata', " + scsT.Field("metadata") + "," +
158+
getMetadataField(scsT.Name(), true) +
159159
"'subject_sets', " + scsT.Field("condition") +
160160
")" +
161161
")) AS sub_maps_arr " +

service/policy/db/namespaces.go

Lines changed: 2 additions & 2 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-
t.Field("metadata"),
80+
getMetadataField("", 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-
t.Field("metadata"),
129+
getMetadataField("", false),
130130
}
131131

132132
if opts.withFqn {

service/policy/db/resource_mapping.go

Lines changed: 1 addition & 1 deletion
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-
t.Field("metadata"),
82+
getMetadataField(t.Name(), false),
8383
t.Field("terms"),
8484
"JSON_BUILD_OBJECT("+
8585
"'id', av.id,"+

service/policy/db/subject_mappings.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func subjectConditionSetSelect() sq.SelectBuilder {
105105
t := Tables.SubjectConditionSet
106106
return db.NewStatementBuilder().Select(
107107
t.Field("id"),
108-
t.Field("metadata"),
108+
getMetadataField("", false),
109109
t.Field("condition"),
110110
)
111111
}
@@ -171,10 +171,10 @@ func subjectMappingSelect() sq.SelectBuilder {
171171
return db.NewStatementBuilder().Select(
172172
t.Field("id"),
173173
t.Field("actions"),
174-
t.Field("metadata"),
174+
getMetadataField(t.Name(), false),
175175
"JSON_BUILD_OBJECT("+
176176
"'id', "+scsT.Field("id")+", "+
177-
"'metadata', "+scsT.Field("metadata")+", "+
177+
getMetadataField(scsT.Name(), true)+
178178
"'subject_sets', "+scsT.Field("condition")+
179179
") AS subject_condition_set",
180180
"JSON_BUILD_OBJECT("+

0 commit comments

Comments
 (0)