Skip to content

Commit 45ce1ec

Browse files
authored
Merge pull request #4545 from RaphaelFakhri/fix-bind-accept-all-identity-hash
Keep identity hash when binding with --accept-all-permission-claims
2 parents cc872e1 + 504ca69 commit 45ce1ec

2 files changed

Lines changed: 89 additions & 29 deletions

File tree

‎staging/src/github.com/kcp-dev/cli/pkg/bind/plugin/bind.go‎

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -213,35 +213,7 @@ func (b *BindOptions) Run(ctx context.Context) error {
213213
return fmt.Errorf("failed to get APIExport %q: %w", apiExportName, err)
214214
}
215215

216-
// Iterate over all permission claims defined in the APIExport
217-
for _, claim := range apiExport.Spec.PermissionClaims {
218-
// Build an AcceptablePermissionClaim using the SDK structs
219-
parsedClaim := apisv1alpha2.AcceptablePermissionClaim{
220-
ScopedPermissionClaim: apisv1alpha2.ScopedPermissionClaim{
221-
PermissionClaim: apisv1alpha2.PermissionClaim{
222-
GroupResource: apisv1alpha2.GroupResource{
223-
Group: claim.Group,
224-
Resource: claim.Resource,
225-
},
226-
Verbs: []string{"*"},
227-
},
228-
Selector: apisv1alpha2.PermissionClaimSelector{MatchAll: true},
229-
},
230-
State: apisv1alpha2.ClaimAccepted, // default; may be overridden following
231-
}
232-
233-
// Add to accepted claims if user requested accept-all
234-
if b.AcceptAllPermissionClaims {
235-
parsedClaim.State = apisv1alpha2.ClaimAccepted
236-
b.acceptedPermissionClaims = append(b.acceptedPermissionClaims, parsedClaim)
237-
}
238-
239-
// Add to rejected claims if user requested reject-all
240-
if b.RejectAllPermissionClaims {
241-
parsedClaim.State = apisv1alpha2.ClaimRejected
242-
b.rejectedPermissionClaims = append(b.rejectedPermissionClaims, parsedClaim)
243-
}
244-
}
216+
b.addAllPermissionClaims(apiExport.Spec.PermissionClaims)
245217
}
246218

247219
apiBinding, err := b.newAPIBinding(preferredAPIBindingVersion)
@@ -277,6 +249,40 @@ func (b *BindOptions) Run(ctx context.Context) error {
277249
return nil
278250
}
279251

252+
// addAllPermissionClaims accepts or rejects every given APIExport permission claim,
253+
// depending on whether --accept-all-permission-claims or --reject-all-permission-claims is set.
254+
func (b *BindOptions) addAllPermissionClaims(claims []apisv1alpha1.PermissionClaim) {
255+
for _, claim := range claims {
256+
// Build an AcceptablePermissionClaim using the SDK structs
257+
parsedClaim := apisv1alpha2.AcceptablePermissionClaim{
258+
ScopedPermissionClaim: apisv1alpha2.ScopedPermissionClaim{
259+
PermissionClaim: apisv1alpha2.PermissionClaim{
260+
GroupResource: apisv1alpha2.GroupResource{
261+
Group: claim.Group,
262+
Resource: claim.Resource,
263+
},
264+
Verbs: []string{"*"},
265+
IdentityHash: claim.IdentityHash,
266+
},
267+
Selector: apisv1alpha2.PermissionClaimSelector{MatchAll: true},
268+
},
269+
State: apisv1alpha2.ClaimAccepted, // default; may be overridden following
270+
}
271+
272+
// Add to accepted claims if user requested accept-all
273+
if b.AcceptAllPermissionClaims {
274+
parsedClaim.State = apisv1alpha2.ClaimAccepted
275+
b.acceptedPermissionClaims = append(b.acceptedPermissionClaims, parsedClaim)
276+
}
277+
278+
// Add to rejected claims if user requested reject-all
279+
if b.RejectAllPermissionClaims {
280+
parsedClaim.State = apisv1alpha2.ClaimRejected
281+
b.rejectedPermissionClaims = append(b.rejectedPermissionClaims, parsedClaim)
282+
}
283+
}
284+
}
285+
280286
func (b *BindOptions) newAPIBinding(preferredAPIBindingVersion string) (apishelpers.APIBinding, error) {
281287
path, apiExportName := logicalcluster.NewPath(b.APIExportRef).Split()
282288

‎staging/src/github.com/kcp-dev/cli/pkg/bind/plugin/bind_test.go‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package plugin
1919
import (
2020
"testing"
2121

22+
apisv1alpha1 "github.com/kcp-dev/sdk/apis/apis/v1alpha1"
2223
apisv1alpha2 "github.com/kcp-dev/sdk/apis/apis/v1alpha2"
2324
)
2425

@@ -262,3 +263,56 @@ func TestBindOptionsAcceptRejectAll(t *testing.T) {
262263
}
263264
})
264265
}
266+
267+
// TestAddAllPermissionClaims verifies that accept-all and reject-all copy the
268+
// group, resource and identity hash of every APIExport permission claim.
269+
func TestAddAllPermissionClaims(t *testing.T) {
270+
t.Parallel()
271+
272+
exportClaims := []apisv1alpha1.PermissionClaim{
273+
{GroupResource: apisv1alpha1.GroupResource{Group: "", Resource: "configmaps"}},
274+
{GroupResource: apisv1alpha1.GroupResource{Group: "example.kcp.io", Resource: "widgets"}, IdentityHash: "abc123"},
275+
}
276+
277+
testCases := []struct {
278+
description string
279+
acceptAll bool
280+
wantState apisv1alpha2.AcceptablePermissionClaimState
281+
}{
282+
{description: "accept-all", acceptAll: true, wantState: apisv1alpha2.ClaimAccepted},
283+
{description: "reject-all", acceptAll: false, wantState: apisv1alpha2.ClaimRejected},
284+
}
285+
286+
for _, c := range testCases {
287+
t.Run(c.description, func(t *testing.T) {
288+
t.Parallel()
289+
b := &BindOptions{
290+
AcceptAllPermissionClaims: c.acceptAll,
291+
RejectAllPermissionClaims: !c.acceptAll,
292+
}
293+
b.addAllPermissionClaims(exportClaims)
294+
295+
got := b.rejectedPermissionClaims
296+
other := b.acceptedPermissionClaims
297+
if c.acceptAll {
298+
got, other = other, got
299+
}
300+
if len(other) != 0 {
301+
t.Errorf("expected no claims in the opposite list, got %d", len(other))
302+
}
303+
if len(got) != len(exportClaims) {
304+
t.Fatalf("expected %d claims, got %d", len(exportClaims), len(got))
305+
}
306+
for i, claim := range got {
307+
want := exportClaims[i]
308+
if claim.Group != want.Group || claim.Resource != want.Resource || claim.IdentityHash != want.IdentityHash {
309+
t.Errorf("claim %d: expected %s.%s with identity hash %q, got %s.%s with identity hash %q",
310+
i, want.Resource, want.Group, want.IdentityHash, claim.Resource, claim.Group, claim.IdentityHash)
311+
}
312+
if claim.State != c.wantState {
313+
t.Errorf("claim %d: expected state %s, got %s", i, c.wantState, claim.State)
314+
}
315+
}
316+
})
317+
}
318+
}

0 commit comments

Comments
 (0)