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
14 changes: 7 additions & 7 deletions service/integration/subject_mappings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ var (
*-------------------- SubjectMappings -------------------
*-------------------------------------------------------*/

func TestSubjectMappingSuite(t *testing.T) {
if testing.Short() {
t.Skip("skipping subject_mappings integration tests")
}
suite.Run(t, new(SubjectMappingsSuite))
}

func (s *SubjectMappingsSuite) TestCreateSubjectMapping_ExistingSubjectConditionSetId() {
fixtureAttrValID := s.f.GetAttributeValueKey("example.net/attr/attr1/value/value2").ID
fixtureSCSId := s.f.GetSubjectConditionSetKey("subject_condition_set1").ID
Expand Down Expand Up @@ -1770,10 +1777,3 @@ func (s *SubjectMappingsSuite) TestUpdateSubjectConditionSet_MetadataVariations(
s.Equal(created.GetId(), got.GetId())
s.Equal(labels, got.GetMetadata().GetLabels())
}

func TestSubjectMappingSuite(t *testing.T) {
if testing.Short() {
t.Skip("skipping subject_mappings integration tests")
}
suite.Run(t, new(SubjectMappingsSuite))
}
9 changes: 5 additions & 4 deletions service/policy/db/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 56 additions & 30 deletions service/policy/db/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1212,24 +1212,28 @@ RETURNING id;
----------------------------------------------------------------

-- name: listSubjectMappings :many
WITH counted AS (
WITH subject_actions AS (
SELECT
sma.subject_mapping_id,
COALESCE(
JSONB_AGG(JSONB_BUILD_OBJECT('id', a.id, 'name', a.name)) FILTER (WHERE a.is_standard = TRUE),
'[]'::JSONB
) AS standard_actions,
COALESCE(
JSONB_AGG(JSONB_BUILD_OBJECT('id', a.id, 'name', a.name)) FILTER (WHERE a.is_standard = FALSE),
'[]'::JSONB
) AS custom_actions
FROM subject_mapping_actions sma
JOIN actions a ON sma.action_id = a.id
GROUP BY sma.subject_mapping_id
), counted AS (
SELECT COUNT(sm.id) AS total
FROM subject_mappings sm
)
SELECT
sm.id,
(
SELECT JSONB_AGG(JSONB_BUILD_OBJECT('id', a.id, 'name', a.name))
FROM actions a
JOIN subject_mapping_actions sma ON sma.action_id = a.id
WHERE sma.subject_mapping_id = sm.id AND a.is_standard = TRUE
) AS standard_actions,
(
SELECT JSONB_AGG(JSONB_BUILD_OBJECT('id', a.id, 'name', a.name))
FROM actions a
JOIN subject_mapping_actions sma ON sma.action_id = a.id
WHERE sma.subject_mapping_id = sm.id AND a.is_standard = FALSE
) AS custom_actions,
sa.standard_actions,
sa.custom_actions,
JSON_STRIP_NULLS(JSON_BUILD_OBJECT('labels', sm.metadata -> 'labels', 'created_at', sm.created_at, 'updated_at', sm.updated_at)) AS metadata,
JSON_BUILD_OBJECT(
'id', scs.id,
Expand All @@ -1245,10 +1249,19 @@ SELECT
counted.total
FROM subject_mappings sm
CROSS JOIN counted
LEFT JOIN subject_actions sa ON sm.id = sa.subject_mapping_id
LEFT JOIN attribute_values av ON sm.attribute_value_id = av.id
LEFT JOIN attribute_fqns fqns ON av.id = fqns.value_id
LEFT JOIN subject_condition_set scs ON scs.id = sm.subject_condition_set_id
GROUP BY av.id, sm.id, scs.id, counted.total, fqns.fqn
GROUP BY
sm.id,
sa.standard_actions,
sa.custom_actions,
sm.metadata, sm.created_at, sm.updated_at, -- for metadata object
scs.id, scs.metadata, scs.created_at, scs.updated_at, scs.condition, -- for subject_condition_set object
av.id, av.value, av.active, -- for attribute_value object
fqns.fqn,
counted.total
LIMIT @limit_
OFFSET @offset_;

Expand Down Expand Up @@ -1281,20 +1294,25 @@ WHERE sm.id = $1
GROUP BY av.id, sm.id, scs.id;

-- name: matchSubjectMappings :many
WITH subject_actions AS (
SELECT
sma.subject_mapping_id,
COALESCE(
JSONB_AGG(JSONB_BUILD_OBJECT('id', a.id, 'name', a.name)) FILTER (WHERE a.is_standard = TRUE),
'[]'::JSONB
) AS standard_actions,
COALESCE(
JSONB_AGG(JSONB_BUILD_OBJECT('id', a.id, 'name', a.name)) FILTER (WHERE a.is_standard = FALSE),
'[]'::JSONB
) AS custom_actions
FROM subject_mapping_actions sma
JOIN actions a ON sma.action_id = a.id
GROUP BY sma.subject_mapping_id
)
SELECT
sm.id,
(
SELECT JSONB_AGG(JSONB_BUILD_OBJECT('id', a.id, 'name', a.name))
FROM actions a
JOIN subject_mapping_actions sma ON sma.action_id = a.id
WHERE sma.subject_mapping_id = sm.id AND a.is_standard = TRUE
) AS standard_actions,
(
SELECT JSONB_AGG(JSONB_BUILD_OBJECT('id', a.id, 'name', a.name))
FROM actions a
JOIN subject_mapping_actions sma ON sma.action_id = a.id
WHERE sma.subject_mapping_id = sm.id AND a.is_standard = FALSE
) AS custom_actions,
sa.standard_actions,
sa.custom_actions,
JSON_BUILD_OBJECT(
'id', scs.id,
'subject_sets', scs.condition
Expand All @@ -1306,14 +1324,23 @@ SELECT
'fqn', fqns.fqn
) AS attribute_value
FROM subject_mappings sm
LEFT JOIN subject_actions sa ON sm.id = sa.subject_mapping_id
LEFT JOIN attribute_values av ON sm.attribute_value_id = av.id
LEFT JOIN attribute_definitions ad ON av.attribute_definition_id = ad.id
LEFT JOIN attribute_namespaces ns ON ad.namespace_id = ns.id
LEFT JOIN attribute_fqns fqns ON av.id = fqns.value_id
LEFT JOIN subject_condition_set scs ON scs.id = sm.subject_condition_set_id
WHERE ns.active = true AND ad.active = true and av.active = true
AND scs.selector_values && @selectors::TEXT[]
GROUP BY av.id, sm.id, scs.id, fqns.fqn;
WHERE
ns.active = TRUE
AND ad.active = TRUE
AND av.active = TRUE
AND scs.selector_values && @selectors::TEXT[]
GROUP BY
sm.id,
sa.standard_actions,
sa.custom_actions,
scs.id, scs.condition,
av.id, av.value, av.active, fqns.fqn;

-- name: createSubjectMapping :one
WITH inserted_mapping AS (
Expand Down Expand Up @@ -1715,4 +1742,3 @@ VALUES ($1);

-- name: deleteAllBaseKeys :execrows
DELETE FROM base_keys;

Loading
Loading