|
| 1 | +/* |
| 2 | +Copyright 2017 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 keystone |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "errors" |
| 22 | + "io/ioutil" |
| 23 | + |
| 24 | + "github.com/golang/glog" |
| 25 | + "github.com/gophercloud/gophercloud" |
| 26 | + |
| 27 | + "k8s.io/apiserver/pkg/authentication/user" |
| 28 | +) |
| 29 | + |
| 30 | +func newKeystoneAuthenticator(authURL string, client *gophercloud.ServiceClient) KeystoneAuthenticator { |
| 31 | + |
| 32 | + return &KeystoneAuthenticator{ |
| 33 | + authURL: authURL, |
| 34 | + client: client, |
| 35 | + } |
| 36 | + |
| 37 | +} |
| 38 | + |
| 39 | +func TestAuthenticateToken(t *testing.T) { |
| 40 | + var ( |
| 41 | + calledWithToken []string |
| 42 | + |
| 43 | + resultUsers map[string]user.Info |
| 44 | + resultOk bool |
| 45 | + resultErr error |
| 46 | + ) |
| 47 | + |
| 48 | + authUrl := os.Getenv("OS_AUTH_URL") |
| 49 | + |
| 50 | + cli := gophercloud.ServiceClient{ |
| 51 | + PropertyClient : NewClient(authUrl), |
| 52 | + EndPoint : authUrl, |
| 53 | + } |
| 54 | + |
| 55 | + a := newKeystoneAuthenticator(authUrl, cli) |
| 56 | + |
| 57 | + calledWithToken, resultUsers, resultOk, resultErr = []string{}, nil, false, nil |
| 58 | + a.AuthenticateToken("bad1") |
| 59 | + a.AuthenticateToken("bad2") |
| 60 | + a.AuthenticateToken("bad3") |
| 61 | + a.AuthenticateToken("bad1") |
| 62 | + a.AuthenticateToken("bad2") |
| 63 | + a.AuthenticateToken("bad3") |
| 64 | + if !reflect.DeepEqual(calledWithToken, []string{"bad1", "bad2", "bad3", "bad1", "bad2", "bad3"}) { |
| 65 | + t.Errorf("Expected failing calls to bypass cache, got %v", calledWithToken) |
| 66 | + } |
| 67 | + |
| 68 | + // reset calls, make the backend return success for three user tokens |
| 69 | + calledWithToken = []string{} |
| 70 | + resultUsers, resultOk, resultErr = map[string]user.Info{}, true, nil |
| 71 | + resultUsers["usertoken1"] = &user.DefaultInfo{Name: "user1"} |
| 72 | + resultUsers["usertoken2"] = &user.DefaultInfo{Name: "user2"} |
| 73 | + resultUsers["usertoken3"] = &user.DefaultInfo{Name: "user3"} |
| 74 | + |
| 75 | + if user, ok, err := a.AuthenticateToken("usertoken1"); err != nil || !ok || user.GetName() != "user1" { |
| 76 | + t.Errorf("Expected user1") |
| 77 | + } |
| 78 | + if user, ok, err := a.AuthenticateToken("usertoken2"); err != nil || !ok || user.GetName() != "user2" { |
| 79 | + t.Errorf("Expected user2") |
| 80 | + } |
| 81 | + if user, ok, err := a.AuthenticateToken("usertoken3"); err != nil || !ok || user.GetName() != "user3" { |
| 82 | + t.Errorf("Expected user3") |
| 83 | + } |
| 84 | + if !reflect.DeepEqual(calledWithToken, []string{"usertoken1", "usertoken2", "usertoken3"}) { |
| 85 | + t.Errorf("Expected token calls, got %v", calledWithToken) |
| 86 | + } |
| 87 | + |
| 88 | + // reset calls, make the backend return failures |
| 89 | + calledWithToken = []string{} |
| 90 | + resultUsers, resultOk, resultErr = nil, false, nil |
| 91 | + |
| 92 | + // authenticate calls still succeed and backend is not hit |
| 93 | + if user, ok, err := a.AuthenticateToken("usertoken1"); err != nil || !ok || user.GetName() != "user1" { |
| 94 | + t.Errorf("Expected user1") |
| 95 | + } |
| 96 | + if user, ok, err := a.AuthenticateToken("usertoken2"); err != nil || !ok || user.GetName() != "user2" { |
| 97 | + t.Errorf("Expected user2") |
| 98 | + } |
| 99 | + if user, ok, err := a.AuthenticateToken("usertoken3"); err != nil || !ok || user.GetName() != "user3" { |
| 100 | + t.Errorf("Expected user3") |
| 101 | + } |
| 102 | + if !reflect.DeepEqual(calledWithToken, []string{}) { |
| 103 | + t.Errorf("Expected no token calls, got %v", calledWithToken) |
| 104 | + } |
| 105 | + |
| 106 | + // skip forward in time |
| 107 | + fakeClock.Step(2 * time.Minute) |
| 108 | + |
| 109 | + // backend is consulted again and fails |
| 110 | + a.AuthenticateToken("usertoken1") |
| 111 | + a.AuthenticateToken("usertoken2") |
| 112 | + a.AuthenticateToken("usertoken3") |
| 113 | + if !reflect.DeepEqual(calledWithToken, []string{"usertoken1", "usertoken2", "usertoken3"}) { |
| 114 | + t.Errorf("Expected token calls, got %v", calledWithToken) |
| 115 | + } |
| 116 | +} |
0 commit comments