k8s.io/apiserver@v0.31.1/plugin/pkg/authenticator/token/webhook/round_trip_test.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package webhook implements the authorizer.Authorizer interface using HTTP webhooks.
    18  package webhook
    19  
    20  import (
    21  	"math/rand"
    22  	"reflect"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/google/go-cmp/cmp"
    27  	fuzz "github.com/google/gofuzz"
    28  
    29  	authenticationv1 "k8s.io/api/authentication/v1"
    30  	authenticationv1beta1 "k8s.io/api/authentication/v1beta1"
    31  )
    32  
    33  func TestRoundTrip(t *testing.T) {
    34  	f := fuzz.New()
    35  	seed := time.Now().UnixNano()
    36  	t.Logf("seed = %v", seed)
    37  	f.RandSource(rand.New(rand.NewSource(seed)))
    38  
    39  	for i := 0; i < 1000; i++ {
    40  		original := &authenticationv1.TokenReview{}
    41  		f.Fuzz(&original.Spec)
    42  		f.Fuzz(&original.Status)
    43  		converted := &authenticationv1beta1.TokenReview{
    44  			Spec:   v1SpecToV1beta1Spec(&original.Spec),
    45  			Status: v1StatusToV1beta1Status(original.Status),
    46  		}
    47  		roundtripped := &authenticationv1.TokenReview{
    48  			Spec:   v1beta1SpecToV1Spec(converted.Spec),
    49  			Status: v1beta1StatusToV1Status(&converted.Status),
    50  		}
    51  		if !reflect.DeepEqual(original, roundtripped) {
    52  			t.Errorf("diff %s", cmp.Diff(original, roundtripped))
    53  		}
    54  	}
    55  }
    56  
    57  func v1StatusToV1beta1Status(in authenticationv1.TokenReviewStatus) authenticationv1beta1.TokenReviewStatus {
    58  	return authenticationv1beta1.TokenReviewStatus{
    59  		Authenticated: in.Authenticated,
    60  		User:          v1UserToV1beta1User(in.User),
    61  		Audiences:     in.Audiences,
    62  		Error:         in.Error,
    63  	}
    64  }
    65  
    66  func v1UserToV1beta1User(u authenticationv1.UserInfo) authenticationv1beta1.UserInfo {
    67  	var extra map[string]authenticationv1beta1.ExtraValue
    68  	if u.Extra != nil {
    69  		extra = make(map[string]authenticationv1beta1.ExtraValue, len(u.Extra))
    70  		for k, v := range u.Extra {
    71  			extra[k] = authenticationv1beta1.ExtraValue(v)
    72  		}
    73  	}
    74  	return authenticationv1beta1.UserInfo{
    75  		Username: u.Username,
    76  		UID:      u.UID,
    77  		Groups:   u.Groups,
    78  		Extra:    extra,
    79  	}
    80  }
    81  
    82  func v1beta1SpecToV1Spec(in authenticationv1beta1.TokenReviewSpec) authenticationv1.TokenReviewSpec {
    83  	return authenticationv1.TokenReviewSpec{
    84  		Token:     in.Token,
    85  		Audiences: in.Audiences,
    86  	}
    87  }