1

I have...

  1. List of tasks tagged with "must match" labels.
  2. User tagged with labels. Now I want to filter only the tasks that match all task-tags with user-tags(user may have extra tags).
// rego-input:
{"
  user":"user-1",
  "tags":{"loc":"NY","type":"L1","group":"admin"}
  
}

// rego-data:
{
  "proj":"abc1",
  "tasks":[
    {"tags":{"loc":"NJ","type":"L1"},"title":"task-1"},
    {"tags":{"loc":"NY","type":"L1"},"title":"task-2"},
    {"tags":{"loc":"PA","type":"L2"},"title":"task-3"}]
}

// expected rego-output:
{
  "user":"user-1",
  "tasks":[
    {"tags":{"loc":"NY","type":"L1"},"title":"task-2"}
  ]
}

What is the right rego to get above output?, appreciate the inputs.

sriba
  • 745
  • 1
  • 6
  • 13

2 Answers2

1

Here is a simple way of doing it.

package match.tags

import future.keywords.in

user := input.user
tasks[t] {
    some user_tags in input
    some data_tags in data.tasks[a]
    user_tags.loc == data_tags.loc
    user_tags.type == data_tags.type
    t := data.tasks[a]
}

Playground link with example: https://play.openpolicyagent.org/p/6oCED9daS7

  • Thanks @peteroneilljr. I should elaborated about the tags. they are not predefined set of keys, so i can't do "tags.loc/type", they could be any numbers tag keys and any names/literals can be the tag key - in this case it's "loc,type", for other tasks it could be "abc, xyz, xto...". so the check have to be dynamic on any key strings in a loop or similar. – sriba Sep 20 '22 at 01:30
0

I got something close... https://play.openpolicyagent.org/p/wRYVBGTezT

usr = input.user
jobs2[v]  {
    some task in data.tasks
    some tk, tv in task.tags
    tv == input.tags[tk]
    v := task.title
}

But, it's not doing match-all(tags), instead it's returning task-1,2 by matching type "L1". wondering how to force the match-all...

sriba
  • 745
  • 1
  • 6
  • 13
  • There is no "force" function in OPA. Rego is designed to return exactly what you request. Was there something missing from the example code I shared? – peteroneilljr Oct 18 '22 at 17:26