Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
982e095f
Commit
982e095f
authored
Sep 14, 2018
by
Daniel Lee
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dsproxy: add mutex protection to the token caches
parent
b5800ffe
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
18 deletions
+27
-18
pkg/api/pluginproxy/access_token_provider.go
+27
-18
No files found.
pkg/api/pluginproxy/access_token_provider.go
View file @
982e095f
...
...
@@ -8,6 +8,7 @@ import (
"net/http"
"net/url"
"strconv"
"sync"
"time"
"golang.org/x/oauth2"
...
...
@@ -17,10 +18,24 @@ import (
)
var
(
tokenCache
=
map
[
string
]
*
jwtToken
{}
oauthJwtTokenCache
=
map
[
string
]
*
oauth2
.
Token
{}
tokenCache
=
tokenCacheType
{
cache
:
map
[
string
]
*
jwtToken
{},
}
oauthJwtTokenCache
=
oauthJwtTokenCacheType
{
cache
:
map
[
string
]
*
oauth2
.
Token
{},
}
)
type
tokenCacheType
struct
{
cache
map
[
string
]
*
jwtToken
sync
.
Mutex
}
type
oauthJwtTokenCacheType
struct
{
cache
map
[
string
]
*
oauth2
.
Token
sync
.
Mutex
}
type
accessTokenProvider
struct
{
route
*
plugins
.
AppPluginRoute
datasourceID
int64
...
...
@@ -40,7 +55,9 @@ func newAccessTokenProvider(dsID int64, pluginRoute *plugins.AppPluginRoute) *ac
}
func
(
provider
*
accessTokenProvider
)
getAccessToken
(
data
templateData
)
(
string
,
error
)
{
if
cachedToken
,
found
:=
tokenCache
[
provider
.
getAccessTokenCacheKey
()];
found
{
tokenCache
.
Lock
()
defer
tokenCache
.
Unlock
()
if
cachedToken
,
found
:=
tokenCache
.
cache
[
provider
.
getAccessTokenCacheKey
()];
found
{
if
cachedToken
.
ExpiresOn
.
After
(
time
.
Now
()
.
Add
(
time
.
Second
*
10
))
{
logger
.
Info
(
"Using token from cache"
)
return
cachedToken
.
AccessToken
,
nil
...
...
@@ -79,7 +96,7 @@ func (provider *accessTokenProvider) getAccessToken(data templateData) (string,
expiresOnEpoch
,
_
:=
strconv
.
ParseInt
(
token
.
ExpiresOnString
,
10
,
64
)
token
.
ExpiresOn
=
time
.
Unix
(
expiresOnEpoch
,
0
)
tokenCache
[
provider
.
getAccessTokenCacheKey
()]
=
&
token
tokenCache
.
cache
[
provider
.
getAccessTokenCacheKey
()]
=
&
token
logger
.
Info
(
"Got new access token"
,
"ExpiresOn"
,
token
.
ExpiresOn
)
...
...
@@ -87,7 +104,9 @@ func (provider *accessTokenProvider) getAccessToken(data templateData) (string,
}
func
(
provider
*
accessTokenProvider
)
getJwtAccessToken
(
ctx
context
.
Context
,
data
templateData
)
(
string
,
error
)
{
if
cachedToken
,
found
:=
oauthJwtTokenCache
[
provider
.
getAccessTokenCacheKey
()];
found
{
oauthJwtTokenCache
.
Lock
()
defer
oauthJwtTokenCache
.
Unlock
()
if
cachedToken
,
found
:=
oauthJwtTokenCache
.
cache
[
provider
.
getAccessTokenCacheKey
()];
found
{
if
cachedToken
.
Expiry
.
After
(
time
.
Now
()
.
Add
(
time
.
Second
*
10
))
{
logger
.
Info
(
"Using token from cache"
)
return
cachedToken
.
AccessToken
,
nil
...
...
@@ -127,7 +146,9 @@ func (provider *accessTokenProvider) getJwtAccessToken(ctx context.Context, data
return
""
,
err
}
oauthJwtTokenCache
[
provider
.
getAccessTokenCacheKey
()]
=
token
oauthJwtTokenCache
.
cache
[
provider
.
getAccessTokenCacheKey
()]
=
token
logger
.
Info
(
"Got new access token"
,
"ExpiresOn"
,
token
.
Expiry
)
return
token
.
AccessToken
,
nil
}
...
...
@@ -139,21 +160,9 @@ var getTokenSource = func(conf *jwt.Config, ctx context.Context) (*oauth2.Token,
return
nil
,
err
}
// logger.Info("interpolatedVal", "token.AccessToken", token.AccessToken)
return
token
,
nil
}
func
(
provider
*
accessTokenProvider
)
getAccessTokenCacheKey
()
string
{
return
fmt
.
Sprintf
(
"%v_%v_%v"
,
provider
.
datasourceID
,
provider
.
route
.
Path
,
provider
.
route
.
Method
)
}
//Export access token lookup
func
GetAccessTokenFromCache
(
datasourceID
int64
,
path
string
,
method
string
)
(
string
,
error
)
{
key
:=
fmt
.
Sprintf
(
"%v_%v_%v"
,
datasourceID
,
path
,
method
)
if
cachedToken
,
found
:=
oauthJwtTokenCache
[
key
];
found
{
return
cachedToken
.
AccessToken
,
nil
}
else
{
return
""
,
fmt
.
Errorf
(
"Key doesnt exist"
)
}
}
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