Skip to content

Commit

Permalink
fix: clean paths before looking for conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
a-cordier committed Sep 10, 2024
1 parent 7ace9d2 commit 1b5ee60
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
14 changes: 12 additions & 2 deletions internal/admission/api/base/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package base
import (
"context"
"net/url"
"slices"
"path/filepath"

"github.com/gravitee-io/gravitee-kubernetes-operator/internal/core"
"github.com/gravitee-io/gravitee-kubernetes-operator/internal/errors"
Expand All @@ -37,7 +37,8 @@ func validateNoConflictingPath(ctx context.Context, api core.ApiDefinitionObject
apiPath,
)
}
if slices.Contains(existingPaths, apiPath) {

if isConflictingPath(existingPaths, apiPath) {
return errors.NewSeveref(
"invalid API context path [%s]. Another API with the same path already exists",
apiPath,
Expand Down Expand Up @@ -65,3 +66,12 @@ func getExistingPaths(ctx context.Context, api core.ApiDefinitionObject) ([]stri
}
return existingPaths, nil
}

func isConflictingPath(existingPaths []string, path string) bool {
for _, existingPath := range existingPaths {
if filepath.Clean(existingPath) == filepath.Clean(path) {
return true
}
}
return false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (C) 2015 The Gravitee team (http://gravitee.io)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v2

import (
"context"

adm "github.com/gravitee-io/gravitee-kubernetes-operator/internal/admission/api/v2"
"github.com/gravitee-io/gravitee-kubernetes-operator/internal/errors"
"github.com/gravitee-io/gravitee-kubernetes-operator/test/internal/integration/constants"
"github.com/gravitee-io/gravitee-kubernetes-operator/test/internal/integration/fixture"
"github.com/gravitee-io/gravitee-kubernetes-operator/test/internal/integration/labels"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Validate create", labels.WithoutContext, func() {
ctx := context.Background()
admissionCtrl := adm.AdmissionCtrl{}

It("should return error on API creation with conflicting path", func() {
fixtures := fixture.
Builder().
WithAPIv4(constants.ApiV4).
Build().
Apply()

By("building an API with path conflict")

existingPath := fixtures.APIv4.GetContextPaths()[0]
conflictingPath := existingPath + "/"

api := fixture.Builder().WithAPI(constants.Api).Build().API
api.Spec.Proxy.VirtualHosts[0].Path = conflictingPath

By("checking that API creation does not pass validation")

_, err := admissionCtrl.ValidateCreate(ctx, api)

Expect(err).To(Equal(
errors.NewSeveref(
"invalid API context path [%s]. Another API with the same path already exists",
conflictingPath,
),
))
})
})

0 comments on commit 1b5ee60

Please sign in to comment.