Skip to content

Commit

Permalink
Merge pull request #426 from MUzairS15/MUzairS15/feat/policies
Browse files Browse the repository at this point in the history
add rego query in relationship definitions
  • Loading branch information
Mohd Uzair authored Dec 4, 2023
2 parents 36620b8 + f100fd1 commit 955b388
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
11 changes: 9 additions & 2 deletions models/meshmodel/core/policies/rego_policy_relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package policies
import (
"context"
"encoding/json"
"fmt"
"io/fs"
"os"
"path/filepath"
Expand Down Expand Up @@ -69,7 +70,7 @@ func mapRelationshipsWithSubType(relationships *[]v1alpha1.RelationshipDefinitio
}

// RegoPolicyHandler takes the required inputs and run the query against all the policy files provided
func (r *Rego) RegoPolicyHandler(regoQueryString string, designFile []byte) (map[string]interface{}, error) {
func (r *Rego) RegoPolicyHandler(regoQueryString string, designFile []byte) (interface{}, error) {
regoEngine, err := rego.New(
rego.Query(regoQueryString),
rego.Load([]string{r.policyDir}, nil),
Expand All @@ -93,7 +94,13 @@ func (r *Rego) RegoPolicyHandler(regoQueryString string, designFile []byte) (map
}

if !eval_result.Allowed() {
return eval_result[0].Expressions[0].Value.(map[string]interface{}), nil
if len(eval_result) > 0 {
if len(eval_result[0].Expressions) > 0 {
return eval_result[0].Expressions[0].Value, nil
}
return nil, ErrEval(fmt.Errorf("evaluation results are empty"))
}
return nil, ErrEval(fmt.Errorf("evaluation results are empty"))
}

return nil, ErrEval(err)
Expand Down
4 changes: 4 additions & 0 deletions models/meshmodel/core/v1alpha1/relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type RelationshipDefinition struct {
DisplayHostName string `json:"displayhostname"`
Metadata map[string]interface{} `json:"metadata" yaml:"metadata"`
SubType string `json:"subType" yaml:"subType" gorm:"subType"`
RegoQuery string `json:"rego_query" yaml:"rego_query" gorm:"rego_query"`
Selectors map[string]interface{} `json:"selectors" yaml:"selectors"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
Expand All @@ -35,6 +36,7 @@ type RelationshipDefinitionDB struct {
TypeMeta
Metadata []byte `json:"metadata" yaml:"metadata"`
SubType string `json:"subType" yaml:"subType"`
RegoQuery string `json:"rego_query" yaml:"rego_query" gorm:"rego_query"`
Selectors []byte `json:"selectors" yaml:"selectors"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
Expand Down Expand Up @@ -127,6 +129,7 @@ func (rdb *RelationshipDefinitionDB) GetRelationshipDefinition(m Model) (r Relat
r.SubType = rdb.SubType
r.Kind = rdb.Kind
r.Model = m
r.RegoQuery = rdb.RegoQuery
return
}

Expand Down Expand Up @@ -160,5 +163,6 @@ func (r *RelationshipDefinition) GetRelationshipDefinitionDB() (rdb Relationship
rdb.Kind = r.Kind
rdb.SubType = r.SubType
rdb.ModelID = r.Model.ID
rdb.RegoQuery = r.RegoQuery
return
}
2 changes: 1 addition & 1 deletion models/oam/core/v1alpha1/application_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ type ConfigurationSpecComponentScope struct {
type ConfigurationSpecComponentScopeRef struct {
metav1.TypeMeta `json:",inline"`
Name string
}
}

0 comments on commit 955b388

Please sign in to comment.