Skip to content

Commit

Permalink
Merge pull request #471 from MUzairS15/MUzairS15/policies/enhancements
Browse files Browse the repository at this point in the history
[Policies] Enhance policy engine instantiation
  • Loading branch information
Mohd Uzair authored Mar 6, 2024
2 parents 1a7ff18 + c4b460d commit 25a7323
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 56 deletions.
15 changes: 7 additions & 8 deletions generators/artifacthub/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ func GetAllAhHelmPackages() ([]AhPackage, error) {
fmt.Println(err)
continue
}



pkgs = append(pkgs, *parseArtifacthubResponse(res))
time.Sleep(500 * time.Millisecond)
}
Expand Down Expand Up @@ -189,12 +188,12 @@ func parseArtifacthubResponse(response map[string]interface{}) *AhPackage {
}

return &AhPackage{
Name: name,
Version: version,
Repository: repoName,
RepoUrl: repoURL,
Name: name,
Version: version,
Repository: repoName,
RepoUrl: repoURL,
VerifiedPublisher: verified,
CNCF: cncf,
Official: official,
CNCF: cncf,
Official: official,
}
}
8 changes: 4 additions & 4 deletions generators/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

const (
artifactHub = "artifacthub"
gitHub = "github"
gitHub = "github"
)

func NewGenerator(registrant, url, packageName string) (models.PackageManager, error) {
Expand All @@ -20,13 +20,13 @@ func NewGenerator(registrant, url, packageName string) (models.PackageManager, e
case artifactHub:
return artifacthub.ArtifactHubPackageManager{
PackageName: packageName,
SourceURL: url,
SourceURL: url,
}, nil
case gitHub:
return github.GitHubPackageManager{
PackageName: packageName,
SourceURL: url,
SourceURL: url,
}, nil
}
return nil, ErrUnsupportedRegistrant(fmt.Errorf("generator not implemented for the registrant %s", registrant))
}
}
6 changes: 3 additions & 3 deletions generators/github/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ func TestGenerateCompFromGitHub(t *testing.T) {
// want: 337,
// },
// { // Source pointing to a file in a repo
// ghPackageManager: GitHubPackageManager{
// ghPackageManager: GitHubPackageManager{
// PackageName: "k8s-config-connector",
// SourceURL: "git://github.com/GoogleCloudPlatform/k8s-config-connector/master/crds/accesscontextmanager_v1alpha1_accesscontextmanageraccesslevelcondition.yaml",
// },
// want: 1,
// want: 1,
// },

// { // Source pointing to a directly downloadable file (not a repo per se)
// ghPackageManager: GitHubPackageManager{
// PackageName: "k8s-config-connector",
// SourceURL: "git://github.com/GoogleCloudPlatform/k8s-config-connector/master/crds/accesscontextmanager_v1alpha1_accesscontextmanageraccesslevelcondition.yaml",
// },
// want: 1,
// want: 1,
// },

{ // Source pointing to a directly downloadable file (not a repo per se)
Expand Down
6 changes: 3 additions & 3 deletions generators/github/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ func (u URL) GetContent() (models.Package, error) {
return nil, err
}
return GitHubPackage{
Name: u.PackageName,
filePath: manifestFilePath,
version: version,
Name: u.PackageName,
filePath: manifestFilePath,
version: version,
SourceURL: u.URL.String(),
}, nil
}
Expand Down
50 changes: 14 additions & 36 deletions models/meshmodel/core/policies/rego_policy_relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ package policies

import (
"context"
"encoding/json"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"

"github.com/layer5io/meshkit/models/meshmodel/core/v1alpha1"
"github.com/layer5io/meshkit/models/meshmodel/registry"
"github.com/layer5io/meshkit/utils"
"github.com/open-policy-agent/opa/rego"
"github.com/open-policy-agent/opa/storage"
Expand All @@ -25,34 +21,21 @@ type Rego struct {
policyDir string
}

func NewRegoInstance(policyDir string, relationshipDir string) (*Rego, error) {
var relationships []v1alpha1.RelationshipDefinition
func NewRegoInstance(policyDir string, regManager *registry.RegistryManager) (*Rego, error) {
var txn storage.Transaction
var store storage.Store

ctx := context.Background()
registeredRelationships, _, _ := regManager.GetEntities(&v1alpha1.RelationshipFilter{})

err := filepath.Walk(relationshipDir, func(path string, info fs.FileInfo, err error) error {
var relationship v1alpha1.RelationshipDefinition
if !info.IsDir() {
byt, err := os.ReadFile(path)
if err != nil {
return utils.ErrReadingLocalFile(err)
}
err = json.Unmarshal(byt, &relationship)
if err != nil {
return utils.ErrUnmarshal(err)
}
relationships = append(relationships, relationship)
if len(registeredRelationships) > 0 {
data := map[string]interface{}{
"relationships": registeredRelationships,
}
return nil
})
store = inmem.NewFromObject(data)
txn, _ = store.NewTransaction(ctx, storage.WriteParams)

if err != nil {
return nil, err
}

data := mapRelationshipsWithSubType(&relationships)
store := inmem.NewFromObject(data)
txn, _ := store.NewTransaction(ctx, storage.WriteParams)

return &Rego{
store: store,
ctx: ctx,
Expand All @@ -61,16 +44,11 @@ func NewRegoInstance(policyDir string, relationshipDir string) (*Rego, error) {
}, nil
}

func mapRelationshipsWithSubType(relationships *[]v1alpha1.RelationshipDefinition) map[string]interface{} {
relMap := make(map[string]interface{}, len(*relationships))
for _, relationship := range *relationships {
relMap[strings.ToLower(relationship.SubType)] = relationship
}
return relMap
}

// RegoPolicyHandler takes the required inputs and run the query against all the policy files provided
func (r *Rego) RegoPolicyHandler(regoQueryString string, designFile []byte) (interface{}, error) {
if r == nil {
return nil, ErrEval(fmt.Errorf("policy engine is not yet ready"))
}
regoEngine, err := rego.New(
rego.Query(regoQueryString),
rego.Load([]string{r.policyDir}, nil),
Expand Down
4 changes: 2 additions & 2 deletions utils/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ func ConvertToK8sManifest(path string, w io.Writer) error {
pathInfo, _ := os.Stat(path)
if pathInfo.IsDir() {
err := filepath.WalkDir(path, func(path string, d fs.DirEntry, _err error) error {
err := writeToFile(w, path)
err := writeToFile(w, path)
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
}
} else {
err := writeToFile(w, path)
if err != nil {
Expand Down

0 comments on commit 25a7323

Please sign in to comment.