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
5a6cd558
Commit
5a6cd558
authored
Nov 11, 2016
by
Jeff Lowdermilk
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix race condition in gcp auth provider plugin
parent
dd681f91
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
97 additions
and
8 deletions
+97
-8
gcp.go
plugin/pkg/client/auth/gcp/gcp.go
+29
-8
gcp_test.go
plugin/pkg/client/auth/gcp/gcp_test.go
+68
-0
No files found.
plugin/pkg/client/auth/gcp/gcp.go
View file @
5a6cd558
...
@@ -23,6 +23,7 @@ import (
...
@@ -23,6 +23,7 @@ import (
"net/http"
"net/http"
"os/exec"
"os/exec"
"strings"
"strings"
"sync"
"time"
"time"
"github.com/golang/glog"
"github.com/golang/glog"
...
@@ -74,6 +75,7 @@ func (g *gcpAuthProvider) WrapTransport(rt http.RoundTripper) http.RoundTripper
...
@@ -74,6 +75,7 @@ func (g *gcpAuthProvider) WrapTransport(rt http.RoundTripper) http.RoundTripper
func
(
g
*
gcpAuthProvider
)
Login
()
error
{
return
nil
}
func
(
g
*
gcpAuthProvider
)
Login
()
error
{
return
nil
}
type
cachedTokenSource
struct
{
type
cachedTokenSource
struct
{
lk
sync
.
Mutex
source
oauth2
.
TokenSource
source
oauth2
.
TokenSource
accessToken
string
accessToken
string
expiry
time
.
Time
expiry
time
.
Time
...
@@ -99,11 +101,7 @@ func newCachedTokenSource(accessToken, expiry string, persister restclient.AuthP
...
@@ -99,11 +101,7 @@ func newCachedTokenSource(accessToken, expiry string, persister restclient.AuthP
}
}
func
(
t
*
cachedTokenSource
)
Token
()
(
*
oauth2
.
Token
,
error
)
{
func
(
t
*
cachedTokenSource
)
Token
()
(
*
oauth2
.
Token
,
error
)
{
tok
:=
&
oauth2
.
Token
{
tok
:=
t
.
cachedToken
()
AccessToken
:
t
.
accessToken
,
TokenType
:
"Bearer"
,
Expiry
:
t
.
expiry
,
}
if
tok
.
Valid
()
&&
!
tok
.
Expiry
.
IsZero
()
{
if
tok
.
Valid
()
&&
!
tok
.
Expiry
.
IsZero
()
{
return
tok
,
nil
return
tok
,
nil
}
}
...
@@ -111,16 +109,39 @@ func (t *cachedTokenSource) Token() (*oauth2.Token, error) {
...
@@ -111,16 +109,39 @@ func (t *cachedTokenSource) Token() (*oauth2.Token, error) {
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
cache
:=
t
.
update
(
tok
)
if
t
.
persister
!=
nil
{
if
t
.
persister
!=
nil
{
t
.
cache
[
"access-token"
]
=
tok
.
AccessToken
if
err
:=
t
.
persister
.
Persist
(
cache
);
err
!=
nil
{
t
.
cache
[
"expiry"
]
=
tok
.
Expiry
.
Format
(
time
.
RFC3339Nano
)
if
err
:=
t
.
persister
.
Persist
(
t
.
cache
);
err
!=
nil
{
glog
.
V
(
4
)
.
Infof
(
"Failed to persist token: %v"
,
err
)
glog
.
V
(
4
)
.
Infof
(
"Failed to persist token: %v"
,
err
)
}
}
}
}
return
tok
,
nil
return
tok
,
nil
}
}
func
(
t
*
cachedTokenSource
)
cachedToken
()
*
oauth2
.
Token
{
t
.
lk
.
Lock
()
defer
t
.
lk
.
Unlock
()
return
&
oauth2
.
Token
{
AccessToken
:
t
.
accessToken
,
TokenType
:
"Bearer"
,
Expiry
:
t
.
expiry
,
}
}
func
(
t
*
cachedTokenSource
)
update
(
tok
*
oauth2
.
Token
)
map
[
string
]
string
{
t
.
lk
.
Lock
()
defer
t
.
lk
.
Unlock
()
t
.
accessToken
=
tok
.
AccessToken
t
.
expiry
=
tok
.
Expiry
ret
:=
map
[
string
]
string
{}
for
k
,
v
:=
range
t
.
cache
{
ret
[
k
]
=
v
}
ret
[
"access-token"
]
=
t
.
accessToken
ret
[
"expiry"
]
=
t
.
expiry
.
Format
(
time
.
RFC3339Nano
)
return
ret
}
type
commandTokenSource
struct
{
type
commandTokenSource
struct
{
cmd
string
cmd
string
args
[]
string
args
[]
string
...
...
plugin/pkg/client/auth/gcp/gcp_test.go
View file @
5a6cd558
...
@@ -20,6 +20,7 @@ import (
...
@@ -20,6 +20,7 @@ import (
"fmt"
"fmt"
"reflect"
"reflect"
"strings"
"strings"
"sync"
"testing"
"testing"
"time"
"time"
...
@@ -141,3 +142,70 @@ func TestCmdTokenSource(t *testing.T) {
...
@@ -141,3 +142,70 @@ func TestCmdTokenSource(t *testing.T) {
}
}
}
}
}
}
type
fakePersister
struct
{
lk
sync
.
Mutex
cache
map
[
string
]
string
}
func
(
f
*
fakePersister
)
Persist
(
cache
map
[
string
]
string
)
error
{
f
.
lk
.
Lock
()
defer
f
.
lk
.
Unlock
()
f
.
cache
=
map
[
string
]
string
{}
for
k
,
v
:=
range
cache
{
f
.
cache
[
k
]
=
v
}
return
nil
}
func
(
f
*
fakePersister
)
read
()
map
[
string
]
string
{
ret
:=
map
[
string
]
string
{}
f
.
lk
.
Lock
()
for
k
,
v
:=
range
f
.
cache
{
ret
[
k
]
=
v
}
return
ret
}
type
fakeTokenSource
struct
{
token
*
oauth2
.
Token
err
error
}
func
(
f
*
fakeTokenSource
)
Token
()
(
*
oauth2
.
Token
,
error
)
{
return
f
.
token
,
f
.
err
}
func
TestCachedTokenSource
(
t
*
testing
.
T
)
{
tok
:=
&
oauth2
.
Token
{
AccessToken
:
"fakeaccesstoken"
}
persister
:=
&
fakePersister
{}
source
:=
&
fakeTokenSource
{
token
:
tok
,
err
:
nil
,
}
cache
:=
map
[
string
]
string
{
"foo"
:
"bar"
,
"baz"
:
"bazinga"
,
}
ts
,
err
:=
newCachedTokenSource
(
"fakeaccesstoken"
,
""
,
persister
,
source
,
cache
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
var
wg
sync
.
WaitGroup
wg
.
Add
(
10
)
for
i
:=
0
;
i
<
10
;
i
++
{
go
func
()
{
_
,
err
:=
ts
.
Token
()
if
err
!=
nil
{
t
.
Errorf
(
"unexpected error: %s"
,
err
)
}
wg
.
Done
()
}()
}
wg
.
Wait
()
cache
[
"access-token"
]
=
"fakeaccesstoken"
cache
[
"expiry"
]
=
tok
.
Expiry
.
Format
(
time
.
RFC3339Nano
)
if
got
:=
persister
.
read
();
!
reflect
.
DeepEqual
(
got
,
cache
)
{
t
.
Errorf
(
"got cache %v, want %v"
,
got
,
cache
)
}
}
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