Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: permission to casbin policy mapping
Browse files Browse the repository at this point in the history
adityathebe committed Jan 17, 2025
1 parent 68c9532 commit 4f558de
Showing 3 changed files with 21 additions and 12 deletions.
4 changes: 2 additions & 2 deletions models/permission.go
Original file line number Diff line number Diff line change
@@ -104,7 +104,7 @@ func (t *Permission) Condition() string {

if len(t.ObjectSelector) > 0 {
// TODO: Find a way to pass in the JSON encoded string instead of encoding with base64
rule = append(rule, fmt.Sprintf(`matchResourceSelector(r.obj, %q)`, base64.StdEncoding.EncodeToString([]byte(t.ObjectSelector))))
rule = append(rule, fmt.Sprintf(`matchResourceSelector(r.obj, '%s')`, base64.StdEncoding.EncodeToString([]byte(t.ObjectSelector))))
}

if t.ComponentID != nil {
@@ -129,7 +129,7 @@ func (t *Permission) Condition() string {
agents = append(agents, fmt.Sprintf("'%s'", agentID))
}

rule = append(rule, fmt.Sprintf(`"matchPerm(r.obj, (%s), '%s')"`, strings.Join(agents, ","), collections.SortedMap(t.Tags)))
rule = append(rule, fmt.Sprintf(`matchPerm(r.obj, (%s), '%s')`, strings.Join(agents, ","), collections.SortedMap(t.Tags)))
}

return strings.Join(rule, " && ")
4 changes: 2 additions & 2 deletions models/permission_test.go
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ func TestPermission_Condition(t *testing.T) {
perm: Permission{
Agents: pq.StringArray([]string{"aws", "azure"}),
},
expected: `"matchPerm(r.obj, ('aws','azure'), '')"`,
expected: `matchPerm(r.obj, ('aws','azure'), '')`,
},
{
name: "tags",
@@ -48,7 +48,7 @@ func TestPermission_Condition(t *testing.T) {
"cluster": "aws",
},
},
expected: `"matchPerm(r.obj, (), 'cluster=aws')"`,
expected: `matchPerm(r.obj, (), 'cluster=aws')`,
},
}

25 changes: 17 additions & 8 deletions models/playbooks.go
Original file line number Diff line number Diff line change
@@ -423,10 +423,19 @@ func (p *PlaybookRun) String(db *gorm.DB) string {
}

type RBACAttribute struct {
Playbook *Playbook
Component *Component
Config *ConfigItem
Check *Check
Playbook Playbook `json:"playbook"`
Component Component `json:"component"`
Config ConfigItem `json:"config"`
Check Check `json:"check"`
}

func (r RBACAttribute) AsMap() map[string]any {
return map[string]any{
"component": r.Component.AsMap(),
"config": r.Config.AsMap(),
"check": r.Check.AsMap(),
"playbook": r.Playbook.AsMap(),
}
}

func (run *PlaybookRun) GetRBACAttributes(db *gorm.DB) (*RBACAttribute, error) {
@@ -436,30 +445,30 @@ func (run *PlaybookRun) GetRBACAttributes(db *gorm.DB) (*RBACAttribute, error) {
if err := db.First(&playbook, run.PlaybookID).Error; err != nil {
return nil, err
}
output.Playbook = &playbook
output.Playbook = playbook

if run.ComponentID != nil {
var component Component
if err := db.First(&component, run.ComponentID).Error; err != nil {
return nil, err
}
output.Component = &component
output.Component = component
}

if run.CheckID != nil {
var check Check
if err := db.First(&check, run.CheckID).Error; err != nil {
return nil, err
}
output.Check = &check
output.Check = check
}

if run.ConfigID != nil {
var config ConfigItem
if err := db.First(&config, run.ConfigID).Error; err != nil {
return nil, err
}
output.Config = &config
output.Config = config
}

return &output, nil

0 comments on commit 4f558de

Please sign in to comment.