Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not apply APIs when parent page is missing #991

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions internal/admission/api/base/pages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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 base

import (
"github.com/gravitee-io/gravitee-kubernetes-operator/api/model/api/base"
"github.com/gravitee-io/gravitee-kubernetes-operator/api/v1alpha1"
"github.com/gravitee-io/gravitee-kubernetes-operator/internal/core"
"github.com/gravitee-io/gravitee-kubernetes-operator/internal/errors"
)

func validatePages(api core.ApiDefinitionObject) *errors.AdmissionErrors {
errs := errors.NewAdmissionErrors()
pages := make(map[string]*base.Page)

switch t := api.(type) {
case *v1alpha1.ApiDefinition:
if t.Spec.Pages != nil {
for k, v := range *t.Spec.Pages {
pages[k] = v.Page
}
}
case *v1alpha1.ApiV4Definition:
if t.Spec.Pages != nil {
for k, v := range *t.Spec.Pages {
pages[k] = v.Page
}
}
}

for name, page := range pages {
if page.Parent != nil && !parentFound(pages, page.Parent) {
errs.AddSeveref("can not apply API [%s]. Parent page [%s] can not be found for page [%s]",
api.GetName(),
*page.Parent,
name,
)
}
}

return errs
}

func parentFound(pages map[string]*base.Page, parent *string) bool {
if _, ok := pages[*parent]; !ok {
return false
}

return true
}
1 change: 1 addition & 0 deletions internal/admission/api/base/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func ValidateCreate(ctx context.Context, obj runtime.Object) *errors.AdmissionEr
errs.Add(validatePlans(api))
errs.Add(validateNoConflictingPath(ctx, api))
errs.MergeWith(validateResourceOrRefs(ctx, api))
errs.MergeWith(validatePages(api))
}

return errs
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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"

"github.com/gravitee-io/gravitee-kubernetes-operator/internal/errors"

v2 "github.com/gravitee-io/gravitee-kubernetes-operator/internal/admission/api/v2"
"github.com/gravitee-io/gravitee-kubernetes-operator/test/internal/integration/assert"
"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.WithContext, func() {
interval := constants.Interval
ctx := context.Background()
admissionCtrl := v2.AdmissionCtrl{}

It("should return error on api creation with missing parent page", func() {
fixtures1 := fixture.
Builder().
WithContext(constants.ContextWithCredentialsFile).
Build().
Apply()

fixtures2 := fixture.
Builder().
WithAPI(constants.ApiWithPagesFile).
Build()

fixtures2.Context = fixtures1.Context

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

p := "missing-parent-page"
(*fixtures2.API.Spec.Pages)["markdown"].Parent = &p

Consistently(func() error {
_, err := admissionCtrl.ValidateCreate(ctx, fixtures2.API)
return assert.Equals(
"error",
errors.NewSeveref(
"can not apply API [%s]. Parent page [missing-parent-page] can not be found for page [markdown]",
fixtures2.API.Name,
),
err,
)
}, constants.ConsistentTimeout, interval).Should(Succeed())
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 v4

import (
"context"

"github.com/gravitee-io/gravitee-kubernetes-operator/internal/errors"
"github.com/gravitee-io/gravitee-kubernetes-operator/test/internal/integration/assert"

v4 "github.com/gravitee-io/gravitee-kubernetes-operator/internal/admission/api/v4"
"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.WithContext, func() {
interval := constants.Interval
ctx := context.Background()
admissionCtrl := v4.AdmissionCtrl{}

It("should return error on api creation with missing parent page", func() {
fixtures1 := fixture.
Builder().
WithContext(constants.ContextWithCredentialsFile).
Build().
Apply()

fixtures2 := fixture.
Builder().
WithAPIv4(constants.ApiV4WithPagesFile).
Build()

fixtures2.Context = fixtures1.Context

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

p := "missing-parent-page"
(*fixtures2.APIv4.Spec.Pages)["markdown"].Parent = &p

Consistently(func() error {
_, err := admissionCtrl.ValidateCreate(ctx, fixtures2.APIv4)
return assert.Equals(
"error",
errors.NewSeveref(
"can not apply API [%s]. Parent page [missing-parent-page] can not be found for page [markdown]",
fixtures2.APIv4.Name,
),
err,
)
}, constants.ConsistentTimeout, interval).Should(Succeed())
})
})
2 changes: 2 additions & 0 deletions test/internal/integration/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ const (
ApiResourceOauth2GenericFile = "apim/api_resource/api-resource-oauth2-generic.yml"

ApiWithTemplateAnnotation = "apim/api_definition/v2/api-with-template-annotation.yml"
ApiWithPagesFile = "apim/api_definition/v2/api-with-page-markdown.yml"

// V4 APIS.
ApiV4 = "apim/api_definition/v4/api-v4.yml"
Expand Down Expand Up @@ -131,6 +132,7 @@ const (
ApiV4WithJWTPlanFile = "apim/api_definition/v4/api-v4-with-jwt-plan.yml"
NativeApiV4 = "apim/api_definition/v4/api-v4-native.yml"
NativeApiV4WithContext = "apim/api_definition/v4/api-v4-native-with-context.yml"
ApiV4WithPagesFile = "apim/api_definition/v4/api-v4-with-page-markdown.yml"

IngressPEMRegistry = "ingress/ingress-pem-registry.yml"
Ingress404ResponseTemplate = "ingress/ingress-response-404-config-map.yml"
Expand Down