Skip to content

Commit

Permalink
Merge pull request #37 from napptive/PG-426/validate_namespace
Browse files Browse the repository at this point in the history
validate namespace
  • Loading branch information
Carmendelope authored Aug 13, 2021
2 parents 79d25e2 + a5fbe34 commit 7230080
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
15 changes: 15 additions & 0 deletions internal/pkg/server/catalog-manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package catalog_manager

import (
"fmt"
"regexp"
"strings"

"github.com/napptive/catalog-manager/internal/pkg/entities"
Expand All @@ -31,8 +32,16 @@ import (
const (
// readmeFile with the name of the readme file
readmeFile = "readme.md"
// NamespaceRegex with the regular expression to match namspaces.
// * Must not contain two consecutive hyphens
// * Must be lowercase
// * Cannot start or end with hyphen
NamespaceRegex = "^[a-z0-9]+([a-z0-9-{1}][a-z0-9]+)+[a-z0-9]?$"
)

// validNamespace regex parser.
var validNamespace = regexp.MustCompile(NamespaceRegex)

type Manager interface {
// Add Adds a new application in the repository.
Add(requestedAppID string, files []*entities.FileInfo) error
Expand Down Expand Up @@ -96,6 +105,12 @@ func (m *manager) Add(requestedAppID string, files []*entities.FileInfo) error {
return err
}

// Validate namespace
if !validNamespace.Match([]byte(appID.Namespace)) {
return nerrors.NewFailedPreconditionError("Invalid namespace, must contain lowercase letters, can contain single hyphens and numbers.")
}


// if catalogURL is not empty, check it!
if m.catalogURL != "" {
// check that the url of the application matches the url of the catalog
Expand Down
40 changes: 40 additions & 0 deletions internal/pkg/server/catalog-manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,44 @@ var _ = ginkgo.Describe("Catalog handler test", func() {
gomega.Expect(received).Should(gomega.BeEmpty())
})
})

ginkgo.Context("Adding applications", func() {
ginkgo.It("Should be able to add an application", func() {

namespace := "namespace"
appName := "app"
tag := "v1.0"
filesReturned := []*entities.FileInfo{
{
Path: "./app.yaml",
Data: []byte("Kind: ApplicationConfiguration"),
}, {
Path: "./metadata.yaml",
Data: []byte(metadataFile),
}}
metadataProvider.EXPECT().Add(gomock.Any()).Return(nil, nil)
storageProvider.EXPECT().StoreApplication(namespace, appName, tag, gomock.Any()).Return(nil)

manager := NewManager(storageProvider, metadataProvider, "")
err := manager.Add(fmt.Sprintf("%s/%s:%s", namespace, appName, tag), filesReturned)
gomega.Expect(err).Should(gomega.Succeed())
})
ginkgo.It("Should not be able to add an application if the namespace is wrong", func() {

namespace := "Namespace"
appName := "app"
tag := "v1.0"
filesReturned := []*entities.FileInfo{
{
Path: "./app.yaml",
Data: []byte("Kind: ApplicationConfiguration"),
}, {
Path: "./metadata.yaml",
Data: []byte(metadataFile),
}}
manager := NewManager(storageProvider, metadataProvider, "")
err := manager.Add(fmt.Sprintf("%s/%s:%s", namespace, appName, tag), filesReturned)
gomega.Expect(err).ShouldNot(gomega.Succeed())
})
})
})

0 comments on commit 7230080

Please sign in to comment.