Skip to content
Merged
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
33 changes: 33 additions & 0 deletions service/integration/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,39 @@ func (s *AttributesSuite) Test_ListAttribute() {
}
}

func (s *AttributesSuite) Test_ListAttribute_FqnsIncluded() {
// create an attribute
attr := &attributes.CreateAttributeRequest{
Name: "list_attribute_fqns_new_attr",
NamespaceId: fixtureNamespaceID,
Rule: policy.AttributeRuleTypeEnum_ATTRIBUTE_RULE_TYPE_ENUM_ALL_OF,
Values: []string{"value1", "value2", "value3"},
}
createdAttr, err := s.db.PolicyClient.CreateAttribute(s.ctx, attr)
s.Require().NoError(err)
s.NotNil(createdAttr)

list, err := s.db.PolicyClient.ListAllAttributes(s.ctx, policydb.StateActive, fixtureNamespaceID)
s.Require().NoError(err)
s.NotNil(list)

for _, a := range list {
// attr fqn
s.NotEqual("", a.GetFqn())
s.Equal(fmt.Sprintf("https://%s/attr/%s", a.GetNamespace().GetName(), a.GetName()), a.GetFqn())

// namespace fqn
s.NotEqual("", a.GetNamespace().GetFqn())
s.Equal(fmt.Sprintf("https://%s", a.GetNamespace().GetName()), a.GetNamespace().GetFqn())

// value fqns
for _, v := range a.GetValues() {
s.NotEqual("", v.GetFqn())
s.Equal(fmt.Sprintf("https://%s/attr/%s/value/%s", a.GetNamespace().GetName(), a.GetName(), v.GetValue()), v.GetFqn())
}
}
}

func (s *AttributesSuite) Test_ListAttributesByNamespace() {
// get all unique namespace_ids
namespaces := map[string]string{}
Expand Down
3 changes: 1 addition & 2 deletions service/policy/db/attribute_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func attributeValueHydrateItem(row pgx.Row, opts attributeValueSelectOptions) (*
if err := row.Scan(fields...); err != nil {
return nil, db.WrapIfKnownInvalidQueryErr(err)
} else if membersJSON != nil {
members, err = attributesValuesProtojson(membersJSON)
members, err = attributesValuesProtojson(membersJSON, fqn)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -160,7 +160,6 @@ func (c PolicyDBClient) CreateAttributeValue(ctx context.Context, attributeID st
value,
metadataJSON,
)

if err != nil {
return nil, err
}
Expand Down
15 changes: 12 additions & 3 deletions service/policy/db/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func attributesRuleTypeEnumTransformOut(value string) policy.AttributeRuleTypeEn
return policy.AttributeRuleTypeEnum(policy.AttributeRuleTypeEnum_value[AttributeRuleTypeEnumPrefix+value])
}

func attributesValuesProtojson(valuesJSON []byte) ([]*policy.Value, error) {
func attributesValuesProtojson(valuesJSON []byte, attrFqn sql.NullString) ([]*policy.Value, error) {
var (
raw []json.RawMessage
values []*policy.Value
Expand All @@ -45,6 +45,9 @@ func attributesValuesProtojson(valuesJSON []byte) ([]*policy.Value, error) {
if err != nil {
return nil, fmt.Errorf("error unmarshaling a value: %w", err)
}
if attrFqn.Valid && value.GetFqn() == "" {
value.Fqn = fmt.Sprintf("%s/value/%s", attrFqn.String, value.GetValue())
}
values = append(values, value)
}
return values, nil
Expand Down Expand Up @@ -226,7 +229,7 @@ func attributesHydrateItem(row pgx.Row, opts attributesSelectOptions) (*policy.A
}
var v []*policy.Value
if valuesJSON != nil {
v, err = attributesValuesProtojson(valuesJSON)
v, err = attributesValuesProtojson(valuesJSON, fqn)
if err != nil {
slog.Error("could not unmarshal values", slog.String("error", err.Error()))
return nil, err
Expand All @@ -241,13 +244,19 @@ func attributesHydrateItem(row pgx.Row, opts attributesSelectOptions) (*policy.A
}
}

ns := &policy.Namespace{
Id: namespaceID,
Name: namespaceName,
Fqn: fmt.Sprintf("https://%s", namespaceName),
}

attr := &policy.Attribute{
Id: id,
Name: name,
Rule: attributesRuleTypeEnumTransformOut(rule),
Active: &wrapperspb.BoolValue{Value: active},
Metadata: m,
Namespace: &policy.Namespace{Id: namespaceID, Name: namespaceName},
Namespace: ns,
Grants: k,
Fqn: fqn.String,
}
Expand Down