Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
8 changes: 8 additions & 0 deletions service/integration/attribute_values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ func (s *AttributeValuesSuite) Test_GetAttributeValue() {
s.Equal(len(f.Members), len(v.GetMembers()))
// s.Equal(f.AttributeDefinitionId, v.AttributeId)
s.Equal("https://example.com/attr/attr1/value/value1", v.GetFqn())
metadata := v.GetMetadata()
createdAt := metadata.GetCreatedAt()
updatedAt := metadata.GetUpdatedAt()
s.True(createdAt.IsValid() && createdAt.AsTime().Unix() > 0)
s.True(updatedAt.IsValid() && updatedAt.AsTime().Unix() > 0)
}

func (s *AttributeValuesSuite) Test_GetAttributeValue_NotFound() {
Expand Down Expand Up @@ -286,6 +291,8 @@ func (s *AttributeValuesSuite) Test_UpdateAttributeValue() {
Labels: labels,
},
})
metadata := created.GetMetadata()
updatedAt := metadata.GetUpdatedAt()
s.NoError(err)
s.NotNil(created)

Expand Down Expand Up @@ -315,6 +322,7 @@ func (s *AttributeValuesSuite) Test_UpdateAttributeValue() {
s.NotNil(got)
s.Equal(created.GetId(), got.GetId())
s.EqualValues(expectedLabels, got.GetMetadata().GetLabels())
s.True(got.GetMetadata().GetUpdatedAt().AsTime().After(updatedAt.AsTime()))
}

func (s *AttributeValuesSuite) Test_UpdateAttributeValue_WithInvalidId_Fails() {
Expand Down
8 changes: 8 additions & 0 deletions service/integration/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ func (s *AttributesSuite) Test_GetAttribute() {
s.Equal(f.Name, gotAttr.GetName())
s.Equal(fmt.Sprintf("%s%s", policydb.AttributeRuleTypeEnumPrefix, f.Rule), gotAttr.GetRule().Enum().String())
s.Equal(f.NamespaceId, gotAttr.GetNamespace().GetId())
metadata := gotAttr.GetMetadata()
createdAt := metadata.GetCreatedAt()
updatedAt := metadata.GetUpdatedAt()
s.True(createdAt.IsValid() && createdAt.AsTime().Unix() > 0)
s.True(updatedAt.IsValid() && updatedAt.AsTime().Unix() > 0)
}
}

Expand Down Expand Up @@ -400,6 +405,8 @@ func (s *AttributesSuite) Test_UpdateAttribute() {
},
}
created, err := s.db.PolicyClient.CreateAttribute(s.ctx, attr)
metadata := created.GetMetadata()
updatedAt := metadata.GetUpdatedAt()
s.NoError(err)
s.NotNil(created)

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

func (s *AttributesSuite) Test_UpdateAttribute_WithInvalidIdFails() {
Expand Down
10 changes: 9 additions & 1 deletion service/integration/namespaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ func (s *NamespacesSuite) Test_GetNamespace() {
s.NotNil(gotNamespace)
// name retrieved by ID equal to name used to create
s.Equal(test.Name, gotNamespace.GetName())
metadata := gotNamespace.GetMetadata()
createdAt := metadata.GetCreatedAt()
updatedAt := metadata.GetUpdatedAt()
s.True(createdAt.IsValid() && createdAt.AsTime().Unix() > 0)
s.True(updatedAt.IsValid() && updatedAt.AsTime().Unix() > 0)
}

// Getting a namespace with an nonExistent id should fail
Expand Down Expand Up @@ -153,13 +158,15 @@ func (s *NamespacesSuite) Test_UpdateNamespace() {
"update": updatedLabel,
"new": newLabel,
}

created, err := s.db.PolicyClient.CreateNamespace(s.ctx, &namespaces.CreateNamespaceRequest{
Name: "updating-namespace.com",
Metadata: &common.MetadataMutable{
Labels: labels,
},
})
metadata := created.GetMetadata()
updatedAt := metadata.GetUpdatedAt()

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

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

func (s *NamespacesSuite) Test_UpdateNamespace_DoesNotExist_ShouldFail() {
Expand Down
8 changes: 8 additions & 0 deletions service/integration/resource_mappings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ func (s *ResourceMappingsSuite) Test_GetResourceMapping() {
s.True(testedMembers, "expected to test at least one attribute value member")
}
equalMembers(s.T(), av, mapping.GetAttributeValue(), false)
metadata := mapping.GetMetadata()
createdAt := metadata.GetCreatedAt()
updatedAt := metadata.GetUpdatedAt()
s.True(createdAt.IsValid() && createdAt.AsTime().Unix() > 0)
s.True(updatedAt.IsValid() && updatedAt.AsTime().Unix() > 0)
}
}

Expand Down Expand Up @@ -194,6 +199,8 @@ func (s *ResourceMappingsSuite) Test_UpdateResourceMapping() {
},
Terms: terms,
})
metadata := createdMapping.GetMetadata()
updatedAt := metadata.GetUpdatedAt()
s.NoError(err)
s.NotNil(createdMapping)

Expand Down Expand Up @@ -232,6 +239,7 @@ func (s *ResourceMappingsSuite) Test_UpdateResourceMapping() {
s.Equal(createdMapping.GetAttributeValue().GetId(), got.GetAttributeValue().GetId())
s.Equal(updateTerms, got.GetTerms())
s.EqualValues(expectedLabels, got.GetMetadata().GetLabels())
s.True(got.GetMetadata().GetUpdatedAt().AsTime().After(updatedAt.AsTime()))
}

func (s *ResourceMappingsSuite) Test_UpdateResourceMappingWithUnknownIdFails() {
Expand Down
16 changes: 16 additions & 0 deletions service/integration/subject_mappings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ func (s *SubjectMappingsSuite) TestUpdateSubjectMapping_Actions() {
}

created, err := s.db.PolicyClient.CreateSubjectMapping(context.Background(), newSubjectMapping)
metadata := created.GetMetadata()
updatedAt := metadata.GetUpdatedAt()
s.Require().NoError(err)
s.NotNil(created)

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

func (s *SubjectMappingsSuite) TestUpdateSubjectMapping_SubjectConditionSetId() {
Expand Down Expand Up @@ -370,6 +373,11 @@ func (s *SubjectMappingsSuite) TestGetSubjectMapping() {
s.Equal(fixture.AttributeValueId, got.GetId())
s.NotEmpty(got.GetMembers())
equalMembers(s.T(), got, sm.GetAttributeValue(), false)
metadata := sm.GetMetadata()
createdAt := metadata.GetCreatedAt()
updatedAt := metadata.GetUpdatedAt()
s.True(createdAt.IsValid() && createdAt.AsTime().Unix() > 0)
s.True(updatedAt.IsValid() && updatedAt.AsTime().Unix() > 0)
}

func (s *SubjectMappingsSuite) TestGetSubjectMapping_NonExistentId_Fails() {
Expand Down Expand Up @@ -533,6 +541,11 @@ func (s *SubjectMappingsSuite) TestGetSubjectConditionSet_ById() {
s.Require().NoError(err)
s.NotNil(scs)
s.Equal(fixture.Id, scs.GetId())
metadata := scs.GetMetadata()
createdAt := metadata.GetCreatedAt()
updatedAt := metadata.GetUpdatedAt()
s.True(createdAt.IsValid() && createdAt.AsTime().Unix() > 0)
s.True(updatedAt.IsValid() && updatedAt.AsTime().Unix() > 0)
}

func (s *SubjectMappingsSuite) TestGetSubjectConditionSet_WithNoId_Fails() {
Expand Down Expand Up @@ -619,6 +632,8 @@ func (s *SubjectMappingsSuite) TestUpdateSubjectConditionSet_NewSubjectSets() {
}

created, err := s.db.PolicyClient.CreateSubjectConditionSet(context.Background(), newConditionSet)
metadata := created.GetMetadata()
updatedAt := metadata.GetUpdatedAt()
s.Require().NoError(err)
s.NotNil(created)

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

func (s *SubjectMappingsSuite) TestUpdateSubjectConditionSet_AllAllowedFields() {
Expand Down
12 changes: 6 additions & 6 deletions service/policy/db/attribute_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func getAttributeValueSql(id string, opts attributeValueSelectOptions) (string,
"'value', vmv.value, " +
"'active', vmv.active, " +
"'members', vmv.members || ARRAY[]::UUID[], " +
"'metadata', vmv.metadata, " +
getMetadataField("vmv", true) +
"'attribute', JSON_BUILD_OBJECT(" +
"'id', vmv.attribute_definition_id )"
if opts.withFqn {
Expand All @@ -228,7 +228,7 @@ func getAttributeValueSql(id string, opts attributeValueSelectOptions) (string,
"av.value",
"av.active",
members,
"av.metadata",
getMetadataField("av", false),
"av.attribute_definition_id",
}
if opts.withFqn {
Expand Down Expand Up @@ -281,7 +281,7 @@ func listAttributeValuesSql(attribute_id string, opts attributeValueSelectOption
"'value', vmv.value, " +
"'active', vmv.active, " +
"'members', vmv.members || ARRAY[]::UUID[], " +
"'metadata', vmv.metadata, " +
getMetadataField("vmv", true) +
"'attribute', JSON_BUILD_OBJECT(" +
"'id', vmv.attribute_definition_id )"
if opts.withFqn {
Expand All @@ -293,7 +293,7 @@ func listAttributeValuesSql(attribute_id string, opts attributeValueSelectOption
"av.value",
"av.active",
members,
"av.metadata",
getMetadataField("av", false),
"av.attribute_definition_id",
}
if opts.withFqn {
Expand Down Expand Up @@ -352,7 +352,7 @@ func listAllAttributeValuesSql(opts attributeValueSelectOptions) (string, []inte
"'value', vmv.value, " +
"'active', vmv.active, " +
"'members', vmv.members || ARRAY[]::UUID[], " +
"'metadata', vmv.metadata, " +
getMetadataField("vmv", true) +
"'attribute', JSON_BUILD_OBJECT(" +
"'id', vmv.attribute_definition_id )"
if opts.withFqn {
Expand All @@ -364,7 +364,7 @@ func listAllAttributeValuesSql(opts attributeValueSelectOptions) (string, []inte
"av.value",
"av.active",
members,
"av.metadata",
getMetadataField("av", false),
"av.attribute_definition_id",
}
if opts.withFqn {
Expand Down
6 changes: 3 additions & 3 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"),
t.Field("metadata"),
getMetadataField(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") + "," +
"'metadata', " + smT.Field("metadata") + "," +
getMetadataField(smT.Name(), true) +
"'subject_condition_set', JSON_BUILD_OBJECT(" +
"'id', " + scsT.Field("id") + "," +
"'metadata', " + scsT.Field("metadata") + "," +
getMetadataField(scsT.Name(), true) +
"'subject_sets', " + scsT.Field("condition") +
")" +
")) AS sub_maps_arr " +
Expand Down
4 changes: 2 additions & 2 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"),
t.Field("metadata"),
getMetadataField("", 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"),
t.Field("metadata"),
getMetadataField("", false),
}

if opts.withFqn {
Expand Down
2 changes: 1 addition & 1 deletion 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"),
t.Field("metadata"),
getMetadataField(t.Name(), false),
t.Field("terms"),
"JSON_BUILD_OBJECT("+
"'id', av.id,"+
Expand Down
6 changes: 3 additions & 3 deletions service/policy/db/subject_mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func subjectConditionSetSelect() sq.SelectBuilder {
t := Tables.SubjectConditionSet
return db.NewStatementBuilder().Select(
t.Field("id"),
t.Field("metadata"),
getMetadataField("", false),
t.Field("condition"),
)
}
Expand Down Expand Up @@ -171,10 +171,10 @@ func subjectMappingSelect() sq.SelectBuilder {
return db.NewStatementBuilder().Select(
t.Field("id"),
t.Field("actions"),
t.Field("metadata"),
getMetadataField(t.Name(), false),
"JSON_BUILD_OBJECT("+
"'id', "+scsT.Field("id")+", "+
"'metadata', "+scsT.Field("metadata")+", "+
getMetadataField(scsT.Name(), true)+
"'subject_sets', "+scsT.Field("condition")+
") AS subject_condition_set",
"JSON_BUILD_OBJECT("+
Expand Down
15 changes: 15 additions & 0 deletions service/policy/db/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package db

func getMetadataField(table string, isJSON bool) string {
if table != "" {
table += "."
}
metadata := "JSON_STRIP_NULLS(JSON_BUILD_OBJECT('labels', " + table + "metadata->'labels', 'created_at', " + table + "created_at, 'updated_at', " + table + "updated_at))"

if isJSON {
metadata = "'metadata', " + metadata + ", "
} else {
metadata += " AS metadata"
}
return metadata
}