From 5f42828ba09ad3e0d252bc50f487b247496cd1bc Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Wed, 6 Mar 2024 17:57:40 +0530 Subject: [PATCH 1/3] add nil check Signed-off-by: MUzairS15 --- models/meshmodel/core/policies/rego_policy_relationship.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/models/meshmodel/core/policies/rego_policy_relationship.go b/models/meshmodel/core/policies/rego_policy_relationship.go index 9c133657..b581882b 100644 --- a/models/meshmodel/core/policies/rego_policy_relationship.go +++ b/models/meshmodel/core/policies/rego_policy_relationship.go @@ -71,6 +71,9 @@ 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) (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), From 592146975c0e2371e80ef2068ae57a298857a380 Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Wed, 6 Mar 2024 17:58:28 +0530 Subject: [PATCH 2/3] init store with relationships from registry Signed-off-by: MUzairS15 --- .../core/policies/rego_policy_relationship.go | 47 +++++-------------- 1 file changed, 11 insertions(+), 36 deletions(-) diff --git a/models/meshmodel/core/policies/rego_policy_relationship.go b/models/meshmodel/core/policies/rego_policy_relationship.go index b581882b..91c36701 100644 --- a/models/meshmodel/core/policies/rego_policy_relationship.go +++ b/models/meshmodel/core/policies/rego_policy_relationship.go @@ -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" @@ -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, @@ -61,14 +44,6 @@ 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 { From c4b460d1109ecb6b36894f0b7515c3dc17964c47 Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Wed, 6 Mar 2024 18:09:02 +0530 Subject: [PATCH 3/3] run gofmt Signed-off-by: MUzairS15 --- generators/artifacthub/package.go | 15 +++++++-------- generators/generator.go | 8 ++++---- generators/github/package_manager.go | 2 -- generators/github/package_test.go | 6 +++--- generators/github/url.go | 6 +++--- utils/helm/helm.go | 4 ++-- utils/utils.go | 2 +- 7 files changed, 20 insertions(+), 23 deletions(-) diff --git a/generators/artifacthub/package.go b/generators/artifacthub/package.go index b6d645e7..f37e94b2 100644 --- a/generators/artifacthub/package.go +++ b/generators/artifacthub/package.go @@ -152,8 +152,7 @@ func GetAllAhHelmPackages() ([]AhPackage, error) { fmt.Println(err) continue } - - + pkgs = append(pkgs, *parseArtifacthubResponse(res)) time.Sleep(500 * time.Millisecond) } @@ -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, } } diff --git a/generators/generator.go b/generators/generator.go index 959239b3..2ea8a050 100644 --- a/generators/generator.go +++ b/generators/generator.go @@ -11,7 +11,7 @@ import ( const ( artifactHub = "artifacthub" - gitHub = "github" + gitHub = "github" ) func NewGenerator(registrant, url, packageName string) (models.PackageManager, error) { @@ -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)) -} \ No newline at end of file +} diff --git a/generators/github/package_manager.go b/generators/github/package_manager.go index 4f230a09..98d3d7b9 100644 --- a/generators/github/package_manager.go +++ b/generators/github/package_manager.go @@ -1,7 +1,6 @@ package github import ( - "fmt" "net/url" "github.com/layer5io/meshkit/models" @@ -27,7 +26,6 @@ func (ghpm GitHubPackageManager) GetPackage() (models.Package, error) { } ghPackage, err := downloader.GetContent() if err != nil { - fmt.Println("ERRRRERERERE",ErrGenerateGitHubPackage(err, ghpm.PackageName) ) return nil, ErrGenerateGitHubPackage(err, ghpm.PackageName) } return ghPackage, nil diff --git a/generators/github/package_test.go b/generators/github/package_test.go index a84aedf0..12447718 100644 --- a/generators/github/package_test.go +++ b/generators/github/package_test.go @@ -23,11 +23,11 @@ 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) @@ -35,7 +35,7 @@ func TestGenerateCompFromGitHub(t *testing.T) { // 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) diff --git a/generators/github/url.go b/generators/github/url.go index 1db31f9d..3b63b2aa 100644 --- a/generators/github/url.go +++ b/generators/github/url.go @@ -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 } diff --git a/utils/helm/helm.go b/utils/helm/helm.go index 763c9bf0..1e2509e1 100644 --- a/utils/helm/helm.go +++ b/utils/helm/helm.go @@ -57,7 +57,7 @@ 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 } @@ -65,7 +65,7 @@ func ConvertToK8sManifest(path string, w io.Writer) error { }) if err != nil { return err - } + } } else { err := writeToFile(w, path) if err != nil { diff --git a/utils/utils.go b/utils/utils.go index db81989f..e34723b2 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -357,7 +357,7 @@ func WriteJSONToFile[K any](outputPath string, data K) error { return nil } -func CreateDirectory(path string) error{ +func CreateDirectory(path string) error { err := os.MkdirAll(path, 0755) if err != nil { err = ErrCreateDir(err, path)