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
dae4e82d
Commit
dae4e82d
authored
Jun 16, 2015
by
Jordan Liggitt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add system: prefix to service account usernames
parent
9f60f3ce
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
4 deletions
+27
-4
jwt.go
pkg/serviceaccount/jwt.go
+7
-3
jwt_test.go
pkg/serviceaccount/jwt_test.go
+20
-1
No files found.
pkg/serviceaccount/jwt.go
View file @
dae4e82d
...
...
@@ -32,7 +32,7 @@ import (
)
const
(
ServiceAccountUsernamePrefix
=
"serviceaccount"
ServiceAccountUsernamePrefix
=
"s
ystem:s
erviceaccount"
ServiceAccountUsernameSeparator
=
":"
Issuer
=
"kubernetes/serviceaccount"
...
...
@@ -84,11 +84,15 @@ func MakeUsername(namespace, name string) string {
// SplitUsername returns the namespace and ServiceAccount name embedded in the given username,
// or an error if the username is not a valid name produced by MakeUsername
func
SplitUsername
(
username
string
)
(
string
,
string
,
error
)
{
if
!
strings
.
HasPrefix
(
username
,
ServiceAccountUsernamePrefix
+
ServiceAccountUsernameSeparator
)
{
return
""
,
""
,
fmt
.
Errorf
(
"Username must be in the form %s"
,
MakeUsername
(
"namespace"
,
"name"
))
}
username
=
strings
.
TrimPrefix
(
username
,
ServiceAccountUsernamePrefix
+
ServiceAccountUsernameSeparator
)
parts
:=
strings
.
Split
(
username
,
ServiceAccountUsernameSeparator
)
if
len
(
parts
)
!=
3
||
parts
[
0
]
!=
ServiceAccountUsernamePrefix
||
len
(
parts
[
1
])
==
0
||
len
(
parts
[
2
])
==
0
{
if
len
(
parts
)
!=
2
||
len
(
parts
[
0
])
==
0
||
len
(
parts
[
1
])
==
0
{
return
""
,
""
,
fmt
.
Errorf
(
"Username must be in the form %s"
,
MakeUsername
(
"namespace"
,
"name"
))
}
return
parts
[
1
],
parts
[
2
],
nil
return
parts
[
0
],
parts
[
1
],
nil
}
// JWTTokenGenerator returns a TokenGenerator that generates signed JWT tokens, using the given privateKey.
...
...
pkg/serviceaccount/jwt_test.go
View file @
dae4e82d
...
...
@@ -121,7 +121,7 @@ func TestReadPublicKey(t *testing.T) {
}
func
TestTokenGenerateAndValidate
(
t
*
testing
.
T
)
{
expectedUserName
:=
"serviceaccount:test:my-service-account"
expectedUserName
:=
"s
ystem:s
erviceaccount:test:my-service-account"
expectedUserUID
:=
"12345"
// Related API objects
...
...
@@ -242,3 +242,22 @@ func TestTokenGenerateAndValidate(t *testing.T) {
}
}
}
func
TestMakeSplitUsername
(
t
*
testing
.
T
)
{
username
:=
MakeUsername
(
"ns"
,
"name"
)
ns
,
name
,
err
:=
SplitUsername
(
username
)
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error %v"
,
err
)
}
if
ns
!=
"ns"
||
name
!=
"name"
{
t
.
Errorf
(
"Expected ns/name, got %s/%s"
,
ns
,
name
)
}
invalid
:=
[]
string
{
"test"
,
"system:serviceaccount"
,
"system:serviceaccount:"
,
"system:serviceaccount:ns"
,
"system:serviceaccount:ns:name:extra"
}
for
_
,
n
:=
range
invalid
{
_
,
_
,
err
:=
SplitUsername
(
"test"
)
if
err
==
nil
{
t
.
Errorf
(
"Expected error for %s"
,
n
)
}
}
}
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