8000 [pull] main from kcp-dev:main by pull[bot] · Pull Request #178 · warmchang/kcp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[pull] main from kcp-dev:main #178

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 Git 8000 Hub? Sign in to your account

Merged
merged 2 commits into from
May 19, 2025
Merged
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
10 changes: 9 additions & 1 deletion cli/pkg/workspace/plugin/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
Expand Down Expand Up @@ -424,20 +425,27 @@ func getAPIBindings(ctx context.Context, kcpClusterClient kcpclientset.ClusterIn
func findUnresolvedPermissionClaims(out io.Writer, apiBindings []apisv1alpha2.APIBinding) error {
for _, binding := range apiBindings {
for _, exportedClaim := range binding.Status.ExportPermissionClaims {
var found, ack bool
var found, ack, verbsMatch bool
var verbsExpected, verbsActual sets.Set[string]
for _, specClaim := range binding.Spec.PermissionClaims {
if !exportedClaim.Equal(specClaim.PermissionClaim) {
continue
}
found = true
ack = (specClaim.State == apisv1alpha2.ClaimAccepted) || specClaim.State == apisv1alpha2.ClaimRejected
verbsExpected = sets.New(exportedClaim.Verbs...)
verbsActual = sets.New(specClaim.Verbs...)
verbsMatch = verbsActual.Difference(verbsExpected).Len() == 0 && verbsExpected.Difference(verbsActual).Len() == 0
}
if !found {
fmt.Fprintf(out, "Warning: claim for %s exported but not specified on APIBinding %s\nAdd this claim to the APIBinding's Spec.\n", exportedClaim.String(), binding.Name)
}
if !ack {
fmt.Fprintf(out, "Warning: claim for %s specified on APIBinding %s but not accepted or rejected.\n", exportedClaim.String(), binding.Name)
}
if !verbsMatch {
fmt.Fprintf(out, "Warning: allowed verbs (%s) on claim for %s on APIBinding %s do not match expected verbs (%s).\n", strings.Join(verbsActual.UnsortedList(), ","), exportedClaim.String(), binding.Name, strings.Join(verbsExpected.UnsortedList(), ","))
}
}
}
return nil
Expand Down
33 changes: 33 additions & 0 deletions config/crds/apis.kcp.io_apibindings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,20 @@ spec:
- Accepted
- Rejected
type: string
verbs:
description: |-
verbs is a list of supported API operation types (this includes
but is not limited to get, list, watch, create, update, patch,
delete, deletecollection, and proxy).
items:
type: string
minItems: 1
type: array
x-kubernetes-list-type: set
required:
- resource
- state
- verbs
type: object
x-kubernetes-validations:
- message: either "all" or "resourceSelector" must be set
Expand Down Expand Up @@ -641,8 +652,19 @@ spec:
- message: at least one field must be set
rule: has(self.__namespace__) || has(self.name)
type: array
verbs:
description: |-
verbs is a list of supported API operation types (this includes
but is not limited to get, list, watch, create, update, patch,
delete, deletecollection, and proxy).
items:
type: string
minItems: 1
type: array
x-kubernetes-list-type: set
required:
- resource
- verbs
type: object
x-kubernetes-validations:
- message: either "all" or "resourceSelector" must be set
Expand Down Expand Up @@ -821,8 +843,19 @@ spec:
- message: at least one field must be set
rule: has(self.__namespace__) || has(self.name)
type: array
verbs:
description: |-
verbs is a list of supported API operation types (this includes
but is not limited to get, list, watch, create, update, patch,
delete, deletecollection, and proxy).
items:
type: string
minItems: 1
type: array
x-kubernetes-list-type: set
required:
- resource
- verbs
type: object
x-kubernetes-validations:
- message: either "all" or "resourceSelector" must be set
Expand Down
11 changes: 11 additions & 0 deletions config/crds/apis.kcp.io_apiexports.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,19 @@ spec:
- message: at least one field must be set
rule: has(self.__namespace__) || has(self.name)
type: array
verbs:
description: |-
verbs is a list of supported API operation types (this includes
but is not limited to get, list, watch, create, update, patch,
delete, deletecollection, and proxy).
items:
type: string
minItems: 1
type: array
x-kubernetes-list-type: set
required:
- resource
- verbs
type: object
x-kubernetes-validations:
- message: either "all" or "resourceSelector" must be set
Expand Down
48 changes: 48 additions & 0 deletions pkg/admission/apibinding/apibinding_admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package apibinding

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -92,6 +93,13 @@ func (o *apiBindingAdmission) Admit(ctx context.Context, a admission.Attributes,
return apierrors.NewInternalError(err)
}

if a.GetResource().GroupResource() == apisv1alpha1.Resource("apibindings") {
ab := &apisv1alpha1.APIBinding{}
if err := validateOverhangingPermissionClaims(ctx, a, ab); err != nil {
return admission.NewForbidden(a, err)
}
}

if a.GetResource().GroupResource() != apisv1alpha2.Resource("apibindings") {
return nil
}
Expand Down Expand Up @@ -312,3 +320,43 @@ func (o *apiBindingAdmission) SetKcpInformers(local, global kcpinformers.SharedI
indexers.ByLogicalClusterPathAndName: indexers.IndexByLogicalClusterPathAndName,
})
}

func validateOverhangingPermissionClaims(_ context.Context, _ admission.Attributes, ab *apisv1alpha1.APIBinding) error {
// TODO(xmudrii): Remove this once we are sure that all APIExport objects are
// converted to v1alpha2.
if _, ok := ab.Annotations[apisv1alpha2.PermissionClaimsAnnotation]; ok {
// validate if we can decode overhanging permission claims. If not, we will fail.
var overhanging []apisv1alpha2.PermissionClaim
if err := json.Unmarshal([]byte(ab.Annotations[apisv1alpha2.PermissionClaimsAnnotation]), &overhanging); err != nil {
return field.Invalid(field.NewPath("metadata").Child("annotations").Key(apisv1alpha2.PermissionClaimsAnnotation), ab.Annotations[apisv1alpha2.PermissionClaimsAnnotation], "failed to decode overhanging permission claims")
}

// validate mismatches. We could have mismatches between the spec and the annotation
// (e.g. a resource present in the annotation, but not in the spec).
// We convert to v2 to check for mismatches.
v2Claims := make([]apisv1alpha2.PermissionClaim, len(ab.Spec.PermissionClaims))
for i, v1pc := range ab.Spec.PermissionClaims {
var v2pc apisv1alpha2.PermissionClaim
err := apisv1alpha2.Convert_v1alpha1_PermissionClaim_To_v1alpha2_PermissionClaim(&v1pc.PermissionClaim, &v2pc, nil)
if err != nil {
return field.Invalid(field.NewPath("spec").Child("permissionClaims").Index(i), ab.Spec.PermissionClaims, "failed to convert spec.PermissionClaims")
}
v2Claims = append(v2Claims, v2pc)
}

for _, o := range overhanging {
var found bool
for _, pc := range v2Claims {
if pc.Equal(o) {
found = true

break
}
}
if !found {
return field.Invalid(field.NewPath("metadata").Child("annotations").Key(apisv1alpha2.PermissionClaimsAnnotation), ab.Annotations[apisv1alpha2.PermissionClaimsAnnotation], "permission claims defined in annotation do not match permission claims defined in spec")
}
}
}
return nil
}
152 changes: 152 additions & 0 deletions pkg/admission/apibinding/apibinding_admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package apibinding
import (
"context"
"crypto/sha256"
"encoding/json"
"errors"
"math/big"
"strings"
Expand Down Expand Up @@ -555,3 +556,154 @@ func newExport(path logicalcluster.Path, name string) apiExportBuilder {
},
}}
}

func TestValidateOverhangingPermissionClaims(t *testing.T) {
tests := map[string]struct {
annotations func() map[string]string
permissionClaims []apisv1alpha1.AcceptablePermissionClaim
expectedError string
}{
"NoAnnotations": {
annotations: func() map[string]string { return nil },
permissionClaims: nil,
expectedError: "",
},
"EmptyJSON": {
annotations: func() map[string]string {
pc := apisv1alpha2.PermissionClaim{}
data, err := json.Marshal(pc)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
return map[string]string{
apisv1alpha2.PermissionClaimsAnnotation: string(data),
}
},
permissionClaims: nil,
expectedError: "failed to decode overhanging permission claims",
},
"EmptyPermissionClaimsAndAnnotation": {
annotations: func() map[string]string {
return map[string]string{
apisv1alpha2.PermissionClaimsAnnotation: "[]",
}
},
permissionClaims: []apisv1alpha1.AcceptablePermissionClaim{},
expectedError: "",
},
"ValidJSON": {
annotations: func() map[string]string {
s := []apisv1alpha2.PermissionClaim{{
GroupResource: apisv1alpha2.GroupResource{
Group: "foo",
Resource: "bar",
},
All: true,
IdentityHash: "baz",
Verbs: []string{"get", "list"},
}}
data, err := json.Marshal(s)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
return map[string]string{
apisv1alpha2.ResourceSchemasAnnotation: string(data),
}
},
permissionClaims: []apisv1alpha1.AcceptablePermissionClaim{
{
PermissionClaim: apisv1alpha1.PermissionClaim{
GroupResource: apisv1alpha1.GroupResource{
Group: "foo",
Resource: "bar",
},
All: true,
IdentityHash: "baz",
},
},
},
expectedError: "",
},
"MismatchInAnnotations": {
annotations: func() map[string]string {
s := []apisv1alpha2.PermissionClaim{
{
GroupResource: apisv1alpha2.GroupResource{
Group: "foo",
Resource: "bar",
},
All: true,
IdentityHash: "baz",
Verbs: []string{"get", "list"},
},
{
GroupResource: apisv1alpha2.GroupResource{
Group: "foo",
Resource: "baz",
},
All: true,
IdentityHash: "bar",
Verbs: []string{"get"},
},
}
data, err := json.Marshal(s)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
return map[string]string{
apisv1alpha2.PermissionClaimsAnnotation: string(data),
}
},
permissionClaims: []apisv1alpha1.AcceptablePermissionClaim{
{
PermissionClaim: apisv1alpha1.PermissionClaim{
GroupResource: apisv1alpha1.GroupResource{
Group: "foo",
Resource: "bar",
},
All: true,
IdentityHash: "baz",
},
},
{
PermissionClaim: apisv1alpha1.PermissionClaim{
GroupResource: apisv1alpha1.GroupResource{
Group: "test",
Resource: "schema",
},
All: true,
IdentityHash: "random",
},
},
},
expectedError: "permission claims defined in annotation do not match permission claims defined in spec",
},
"InvalidJSON": {
annotations: func() map[string]string {
return map[string]string{
apisv1alpha2.PermissionClaimsAnnotation: "invalid json",
}
},
permissionClaims: nil,
expectedError: "failed to decode overhanging permission claims",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ae := &apisv1alpha1.APIBinding{
ObjectMeta: metav1.ObjectMeta{
Annotations: tc.annotations(),
},
Spec: apisv1alpha1.APIBindingSpec{
PermissionClaims: tc.permissionClaims,
},
}
err := validateOverhangingPermissionClaims(context.TODO(), nil, ae)
if tc.expectedError == "" {
require.NoError(t, err)
} else {
require.Contains(t, err.Error(), tc.expectedError)
}
})
}
}
Loading
Loading
0