Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
809f278b
Commit
809f278b
authored
Oct 16, 2018
by
Mike Danese
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make token cache include audience in hash key
parent
e5227216
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
48 additions
and
16 deletions
+48
-16
BUILD
...src/k8s.io/apiserver/pkg/authentication/token/cache/BUILD
+2
-0
cache_striped.go
...apiserver/pkg/authentication/token/cache/cache_striped.go
+8
-8
cache_test.go
...io/apiserver/pkg/authentication/token/cache/cache_test.go
+3
-2
cached_token_authenticator.go
.../authentication/token/cache/cached_token_authenticator.go
+13
-6
cached_token_authenticator_test.go
...entication/token/cache/cached_token_authenticator_test.go
+22
-0
No files found.
staging/src/k8s.io/apiserver/pkg/authentication/token/cache/BUILD
View file @
809f278b
...
@@ -17,6 +17,7 @@ go_test(
...
@@ -17,6 +17,7 @@ go_test(
"//staging/src/k8s.io/apimachinery/pkg/util/clock:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/clock:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/authentication/authenticator:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/authentication/authenticator:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/authentication/user:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/authentication/user:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/endpoints/request:go_default_library",
"//vendor/github.com/pborman/uuid:go_default_library",
"//vendor/github.com/pborman/uuid:go_default_library",
],
],
)
)
...
@@ -34,6 +35,7 @@ go_library(
...
@@ -34,6 +35,7 @@ go_library(
"//staging/src/k8s.io/apimachinery/pkg/util/cache:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/cache:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/clock:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/clock:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/authentication/authenticator:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/authentication/authenticator:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/endpoints/request:go_default_library",
],
],
)
)
...
...
staging/src/k8s.io/apiserver/pkg/authentication/token/cache/cache_striped.go
View file @
809f278b
...
@@ -24,36 +24,36 @@ import (
...
@@ -24,36 +24,36 @@ import (
// split cache lookups across N striped caches
// split cache lookups across N striped caches
type
stripedCache
struct
{
type
stripedCache
struct
{
stripeCount
uint32
stripeCount
uint32
keyFunc
func
(
string
)
uint32
hashFunc
func
(
string
)
uint32
caches
[]
cache
caches
[]
cache
}
}
type
key
Func
func
(
string
)
uint32
type
hash
Func
func
(
string
)
uint32
type
newCacheFunc
func
()
cache
type
newCacheFunc
func
()
cache
func
newStripedCache
(
stripeCount
int
,
keyFunc
key
Func
,
newCacheFunc
newCacheFunc
)
cache
{
func
newStripedCache
(
stripeCount
int
,
hash
hash
Func
,
newCacheFunc
newCacheFunc
)
cache
{
caches
:=
[]
cache
{}
caches
:=
[]
cache
{}
for
i
:=
0
;
i
<
stripeCount
;
i
++
{
for
i
:=
0
;
i
<
stripeCount
;
i
++
{
caches
=
append
(
caches
,
newCacheFunc
())
caches
=
append
(
caches
,
newCacheFunc
())
}
}
return
&
stripedCache
{
return
&
stripedCache
{
stripeCount
:
uint32
(
stripeCount
),
stripeCount
:
uint32
(
stripeCount
),
keyFunc
:
keyFunc
,
hashFunc
:
hash
,
caches
:
caches
,
caches
:
caches
,
}
}
}
}
func
(
c
*
stripedCache
)
get
(
key
string
)
(
*
cacheRecord
,
bool
)
{
func
(
c
*
stripedCache
)
get
(
key
string
)
(
*
cacheRecord
,
bool
)
{
return
c
.
caches
[
c
.
key
Func
(
key
)
%
c
.
stripeCount
]
.
get
(
key
)
return
c
.
caches
[
c
.
hash
Func
(
key
)
%
c
.
stripeCount
]
.
get
(
key
)
}
}
func
(
c
*
stripedCache
)
set
(
key
string
,
value
*
cacheRecord
,
ttl
time
.
Duration
)
{
func
(
c
*
stripedCache
)
set
(
key
string
,
value
*
cacheRecord
,
ttl
time
.
Duration
)
{
c
.
caches
[
c
.
key
Func
(
key
)
%
c
.
stripeCount
]
.
set
(
key
,
value
,
ttl
)
c
.
caches
[
c
.
hash
Func
(
key
)
%
c
.
stripeCount
]
.
set
(
key
,
value
,
ttl
)
}
}
func
(
c
*
stripedCache
)
remove
(
key
string
)
{
func
(
c
*
stripedCache
)
remove
(
key
string
)
{
c
.
caches
[
c
.
key
Func
(
key
)
%
c
.
stripeCount
]
.
remove
(
key
)
c
.
caches
[
c
.
hash
Func
(
key
)
%
c
.
stripeCount
]
.
remove
(
key
)
}
}
func
fnv
Key
Func
(
key
string
)
uint32
{
func
fnv
Hash
Func
(
key
string
)
uint32
{
f
:=
fnv
.
New32
()
f
:=
fnv
.
New32
()
f
.
Write
([]
byte
(
key
))
f
.
Write
([]
byte
(
key
))
return
f
.
Sum32
()
return
f
.
Sum32
()
...
...
staging/src/k8s.io/apiserver/pkg/authentication/token/cache/cache_test.go
View file @
809f278b
...
@@ -22,6 +22,7 @@ import (
...
@@ -22,6 +22,7 @@ import (
"time"
"time"
"github.com/pborman/uuid"
"github.com/pborman/uuid"
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/apiserver/pkg/authentication/authenticator"
"k8s.io/apiserver/pkg/authentication/authenticator"
"k8s.io/apiserver/pkg/authentication/user"
"k8s.io/apiserver/pkg/authentication/user"
...
@@ -36,11 +37,11 @@ func BenchmarkSimpleCache(b *testing.B) {
...
@@ -36,11 +37,11 @@ func BenchmarkSimpleCache(b *testing.B) {
}
}
func
TestStripedCache
(
t
*
testing
.
T
)
{
func
TestStripedCache
(
t
*
testing
.
T
)
{
testCache
(
newStripedCache
(
32
,
fnv
Key
Func
,
func
()
cache
{
return
newSimpleCache
(
128
,
clock
.
RealClock
{})
}),
t
)
testCache
(
newStripedCache
(
32
,
fnv
Hash
Func
,
func
()
cache
{
return
newSimpleCache
(
128
,
clock
.
RealClock
{})
}),
t
)
}
}
func
BenchmarkStripedCache
(
b
*
testing
.
B
)
{
func
BenchmarkStripedCache
(
b
*
testing
.
B
)
{
benchmarkCache
(
newStripedCache
(
32
,
fnv
Key
Func
,
func
()
cache
{
return
newSimpleCache
(
128
,
clock
.
RealClock
{})
}),
b
)
benchmarkCache
(
newStripedCache
(
32
,
fnv
Hash
Func
,
func
()
cache
{
return
newSimpleCache
(
128
,
clock
.
RealClock
{})
}),
b
)
}
}
func
benchmarkCache
(
cache
cache
,
b
*
testing
.
B
)
{
func
benchmarkCache
(
cache
cache
,
b
*
testing
.
B
)
{
...
...
staging/src/k8s.io/apiserver/pkg/authentication/token/cache/cached_token_authenticator.go
View file @
809f278b
...
@@ -18,10 +18,12 @@ package cache
...
@@ -18,10 +18,12 @@ package cache
import
(
import
(
"context"
"context"
"fmt"
"time"
"time"
utilclock
"k8s.io/apimachinery/pkg/util/clock"
utilclock
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/apiserver/pkg/authentication/authenticator"
"k8s.io/apiserver/pkg/authentication/authenticator"
"k8s.io/apiserver/pkg/endpoints/request"
)
)
// cacheRecord holds the three return values of the authenticator.Token AuthenticateToken method
// cacheRecord holds the three return values of the authenticator.Token AuthenticateToken method
...
@@ -59,15 +61,16 @@ func newWithClock(authenticator authenticator.Token, successTTL, failureTTL time
...
@@ -59,15 +61,16 @@ func newWithClock(authenticator authenticator.Token, successTTL, failureTTL time
authenticator
:
authenticator
,
authenticator
:
authenticator
,
successTTL
:
successTTL
,
successTTL
:
successTTL
,
failureTTL
:
failureTTL
,
failureTTL
:
failureTTL
,
cache
:
newStripedCache
(
32
,
fnv
Key
Func
,
func
()
cache
{
return
newSimpleCache
(
128
,
clock
)
}),
cache
:
newStripedCache
(
32
,
fnv
Hash
Func
,
func
()
cache
{
return
newSimpleCache
(
128
,
clock
)
}),
}
}
}
}
// AuthenticateToken implements authenticator.Token
// AuthenticateToken implements authenticator.Token
func
(
a
*
cachedTokenAuthenticator
)
AuthenticateToken
(
ctx
context
.
Context
,
token
string
)
(
*
authenticator
.
Response
,
bool
,
error
)
{
func
(
a
*
cachedTokenAuthenticator
)
AuthenticateToken
(
ctx
context
.
Context
,
token
string
)
(
*
authenticator
.
Response
,
bool
,
error
)
{
// TODO(mikedanese): The key needs to incorporate any relevant data in the
auds
,
_
:=
request
.
AudiencesFrom
(
ctx
)
// context.
if
record
,
ok
:=
a
.
cache
.
get
(
token
);
ok
{
key
:=
keyFunc
(
auds
,
token
)
if
record
,
ok
:=
a
.
cache
.
get
(
key
);
ok
{
return
record
.
resp
,
record
.
ok
,
record
.
err
return
record
.
resp
,
record
.
ok
,
record
.
err
}
}
...
@@ -75,10 +78,14 @@ func (a *cachedTokenAuthenticator) AuthenticateToken(ctx context.Context, token
...
@@ -75,10 +78,14 @@ func (a *cachedTokenAuthenticator) AuthenticateToken(ctx context.Context, token
switch
{
switch
{
case
ok
&&
a
.
successTTL
>
0
:
case
ok
&&
a
.
successTTL
>
0
:
a
.
cache
.
set
(
token
,
&
cacheRecord
{
resp
:
resp
,
ok
:
ok
,
err
:
err
},
a
.
successTTL
)
a
.
cache
.
set
(
key
,
&
cacheRecord
{
resp
:
resp
,
ok
:
ok
,
err
:
err
},
a
.
successTTL
)
case
!
ok
&&
a
.
failureTTL
>
0
:
case
!
ok
&&
a
.
failureTTL
>
0
:
a
.
cache
.
set
(
token
,
&
cacheRecord
{
resp
:
resp
,
ok
:
ok
,
err
:
err
},
a
.
failureTTL
)
a
.
cache
.
set
(
key
,
&
cacheRecord
{
resp
:
resp
,
ok
:
ok
,
err
:
err
},
a
.
failureTTL
)
}
}
return
resp
,
ok
,
err
return
resp
,
ok
,
err
}
}
func
keyFunc
(
auds
[]
string
,
token
string
)
string
{
return
fmt
.
Sprintf
(
"%#v|%v"
,
auds
,
token
)
}
staging/src/k8s.io/apiserver/pkg/authentication/token/cache/cached_token_authenticator_test.go
View file @
809f278b
...
@@ -25,6 +25,7 @@ import (
...
@@ -25,6 +25,7 @@ import (
utilclock
"k8s.io/apimachinery/pkg/util/clock"
utilclock
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/apiserver/pkg/authentication/authenticator"
"k8s.io/apiserver/pkg/authentication/authenticator"
"k8s.io/apiserver/pkg/authentication/user"
"k8s.io/apiserver/pkg/authentication/user"
"k8s.io/apiserver/pkg/endpoints/request"
)
)
func
TestCachedTokenAuthenticator
(
t
*
testing
.
T
)
{
func
TestCachedTokenAuthenticator
(
t
*
testing
.
T
)
{
...
@@ -104,3 +105,24 @@ func TestCachedTokenAuthenticator(t *testing.T) {
...
@@ -104,3 +105,24 @@ func TestCachedTokenAuthenticator(t *testing.T) {
t
.
Errorf
(
"Expected token calls, got %v"
,
calledWithToken
)
t
.
Errorf
(
"Expected token calls, got %v"
,
calledWithToken
)
}
}
}
}
func
TestCachedTokenAuthenticatorWithAudiences
(
t
*
testing
.
T
)
{
resultUsers
:=
make
(
map
[
string
]
user
.
Info
)
fakeAuth
:=
authenticator
.
TokenFunc
(
func
(
ctx
context
.
Context
,
token
string
)
(
*
authenticator
.
Response
,
bool
,
error
)
{
auds
,
_
:=
request
.
AudiencesFrom
(
ctx
)
return
&
authenticator
.
Response
{
User
:
resultUsers
[
auds
[
0
]
+
token
]},
true
,
nil
})
fakeClock
:=
utilclock
.
NewFakeClock
(
time
.
Now
())
a
:=
newWithClock
(
fakeAuth
,
time
.
Minute
,
0
,
fakeClock
)
resultUsers
[
"audAusertoken1"
]
=
&
user
.
DefaultInfo
{
Name
:
"user1"
}
resultUsers
[
"audBusertoken1"
]
=
&
user
.
DefaultInfo
{
Name
:
"user1-different"
}
if
u
,
ok
,
_
:=
a
.
AuthenticateToken
(
request
.
WithAudiences
(
context
.
Background
(),
[]
string
{
"audA"
}),
"usertoken1"
);
!
ok
||
u
.
User
.
GetName
()
!=
"user1"
{
t
.
Errorf
(
"Expected user1"
)
}
if
u
,
ok
,
_
:=
a
.
AuthenticateToken
(
request
.
WithAudiences
(
context
.
Background
(),
[]
string
{
"audB"
}),
"usertoken1"
);
!
ok
||
u
.
User
.
GetName
()
!=
"user1-different"
{
t
.
Errorf
(
"Expected user1-different"
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment