Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: permission to casbin policy mapping #1275

Merged
merged 1 commit into from
Jan 17, 2025
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
4 changes: 2 additions & 2 deletions models/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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, " && ")
Expand Down
4 changes: 2 additions & 2 deletions models/permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -48,7 +48,7 @@ func TestPermission_Condition(t *testing.T) {
"cluster": "aws",
},
},
expected: `"matchPerm(r.obj, (), 'cluster=aws')"`,
expected: `matchPerm(r.obj, (), 'cluster=aws')`,
},
}

Expand Down
25 changes: 17 additions & 8 deletions models/playbooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
Loading