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
7197a4c1
Commit
7197a4c1
authored
Sep 05, 2018
by
Erik Sundell
Committed by
Daniel Lee
Sep 14, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adds jwt token signing google auth
parent
b71e7f33
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
79 additions
and
53 deletions
+79
-53
pkg/api/pluginproxy/ds_proxy.go
+78
-53
pkg/plugins/app_plugin.go
+1
-0
No files found.
pkg/api/pluginproxy/ds_proxy.go
View file @
7197a4c1
...
@@ -3,6 +3,7 @@ package pluginproxy
...
@@ -3,6 +3,7 @@ package pluginproxy
import
(
import
(
"bytes"
"bytes"
"context"
"context"
"encoding/json"
"errors"
"errors"
"fmt"
"fmt"
"io/ioutil"
"io/ioutil"
...
@@ -16,13 +17,13 @@ import (
...
@@ -16,13 +17,13 @@ import (
"time"
"time"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go"
"golang.org/x/oauth2/jwt"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/log"
m
"github.com/grafana/grafana/pkg/models"
m
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
"github.com/grafana/grafana/pkg/util"
"golang.org/x/oauth2/jwt"
)
)
var
(
var
(
...
@@ -350,68 +351,92 @@ func (proxy *DataSourceProxy) applyRoute(req *http.Request) {
...
@@ -350,68 +351,92 @@ func (proxy *DataSourceProxy) applyRoute(req *http.Request) {
}
}
}
}
if
proxy
.
route
.
JwtTokenAuth
!=
nil
{
logger
.
Info
(
"getJwtAccessToken"
,
"JwtAccessToken"
,
proxy
.
route
.
JwtTokenAuth
)
if
token
,
err
:=
proxy
.
getJwtAccessToken
(
data
);
err
!=
nil
{
logger
.
Error
(
"Failed to get access token"
,
"error"
,
err
)
}
else
{
req
.
Header
.
Add
(
"Authorization"
,
fmt
.
Sprintf
(
"Bearer %s"
,
token
))
}
}
logger
.
Info
(
"Requesting"
,
"url"
,
req
.
URL
.
String
())
logger
.
Info
(
"Requesting"
,
"url"
,
req
.
URL
.
String
())
}
}
func
(
proxy
*
DataSourceProxy
)
getAccessToken
(
data
templateData
)
(
string
,
error
)
{
func
(
proxy
*
DataSourceProxy
)
getAccessToken
(
data
templateData
)
(
string
,
error
)
{
conf
:=
jwt
.
Config
{
if
cachedToken
,
found
:=
tokenCache
[
proxy
.
getAccessTokenCacheKey
()];
found
{
Email
:
"raintank-production-stackdrive@raintank-production.iam.gserviceaccount.com"
,
if
cachedToken
.
ExpiresOn
.
After
(
time
.
Now
()
.
Add
(
time
.
Second
*
10
))
{
PrivateKey
:
[]
byte
(
"-----BEGIN PRIVATE KEY-----
\n
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCb1u1Srw8ICWfX
\n
b+hh0qyRoWJzHkf4jxdpOjjqiaYqlipf3fCWoyNgHvQSmX2trYbb1Kg+8Nv9/iaz
\n
6el48MvOlRN6WbcMfhFoT8AKkdjYD3DM+vK3C3uXmWeVMHzQimjECFWsX4WRU4fj
\n
LJ44B3svFvShe3bRJpt2e5LdfjcEQER7Bvte/zQi8v1jloHmcptyz8wb9NddVHs9
\n
INwFbrSaUiQnoJTDSIYEMiKDTvZHRenmLz/R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MpSYHe0dlZylxOoM6t573GSeRb1YjHBNqcX86pl/xMEyt7w6
\n
DF8+c1uGCDq+b2ugfHZ6BOGQfNKQYn/rvMhX0mVSxT6SrtVMizIYK/q4AoK8E7rE
\n
GNwXqYbM8qlY692fzwrYBR8Md1KCpGI+nF9+gAOxAoGAAwJO+jr44SRldSSsli0d
\n
DbGRmlMc093MebjwNsO2NGoF8uTRyYIzchP2l57PMPKFX/r5IcchjVh3wHWwtzMS
\n
hB8zfRDj7RFjW0H8U1Qf5k2ID3ACJtxnt+o744Lggqzf9puf4RKwwAjIwJy2lhbi
\n
yA+63fAHMAwG+k22IkBqcu4=
\n
-----END PRIVATE KEY-----
\n
"
),
logger
.
Info
(
"Using token from cache"
)
Scopes
:
[]
string
{
"https://www.googleapis.com/auth/monitoring.read"
},
return
cachedToken
.
AccessToken
,
nil
TokenURL
:
"https://oauth2.googleapis.com/token"
,
}
}
urlInterpolated
,
err
:=
interpolateString
(
proxy
.
route
.
TokenAuth
.
Url
,
data
)
if
err
!=
nil
{
return
""
,
err
}
params
:=
make
(
url
.
Values
)
for
key
,
value
:=
range
proxy
.
route
.
TokenAuth
.
Params
{
interpolatedParam
,
err
:=
interpolateString
(
value
,
data
)
if
err
!=
nil
{
return
""
,
err
}
params
.
Add
(
key
,
interpolatedParam
)
}
getTokenReq
,
_
:=
http
.
NewRequest
(
"POST"
,
urlInterpolated
,
bytes
.
NewBufferString
(
params
.
Encode
()))
getTokenReq
.
Header
.
Add
(
"Content-Type"
,
"application/x-www-form-urlencoded"
)
getTokenReq
.
Header
.
Add
(
"Content-Length"
,
strconv
.
Itoa
(
len
(
params
.
Encode
())))
resp
,
err
:=
client
.
Do
(
getTokenReq
)
if
err
!=
nil
{
return
""
,
err
}
}
defer
resp
.
Body
.
Close
()
var
token
jwtToken
if
err
:=
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
&
token
);
err
!=
nil
{
return
""
,
err
}
expiresOnEpoch
,
_
:=
strconv
.
ParseInt
(
token
.
ExpiresOnString
,
10
,
64
)
token
.
ExpiresOn
=
time
.
Unix
(
expiresOnEpoch
,
0
)
tokenCache
[
proxy
.
getAccessTokenCacheKey
()]
=
&
token
logger
.
Info
(
"Got new access token"
,
"ExpiresOn"
,
token
.
ExpiresOn
)
return
token
.
AccessToken
,
nil
}
func
(
proxy
*
DataSourceProxy
)
getJwtAccessToken
(
data
templateData
)
(
string
,
error
)
{
conf
:=
new
(
jwt
.
Config
)
if
val
,
ok
:=
proxy
.
route
.
JwtTokenAuth
.
Params
[
"client_email"
];
ok
{
interpolatedVal
,
err
:=
interpolateString
(
val
,
data
)
if
err
!=
nil
{
return
""
,
err
}
conf
.
Email
=
interpolatedVal
}
if
val
,
ok
:=
proxy
.
route
.
JwtTokenAuth
.
Params
[
"private_key"
];
ok
{
interpolatedVal
,
err
:=
interpolateString
(
val
,
data
)
if
err
!=
nil
{
return
""
,
err
}
conf
.
PrivateKey
=
[]
byte
(
interpolatedVal
)
}
conf
.
Scopes
=
[]
string
{
"https://www.googleapis.com/auth/monitoring.read"
}
conf
.
TokenURL
=
"https://oauth2.googleapis.com/token"
ctx
:=
context
.
Background
()
ctx
:=
context
.
Background
()
tokenSrc
:=
conf
.
TokenSource
(
ctx
)
tokenSrc
:=
conf
.
TokenSource
(
ctx
)
// logger.Info("Accesstoken", tokenSrc.Token.AccessToken)
token
,
err
:=
tokenSrc
.
Token
()
token
,
err
:=
tokenSrc
.
Token
()
if
err
!
=
nil
{
if
err
=
=
nil
{
logger
.
Info
(
"GetToken"
,
"Error"
,
err
)
return
""
,
err
}
}
logger
.
Info
(
"GetToken"
,
"Token"
,
token
.
AccessToken
)
return
token
.
AccessToken
,
nil
return
token
.
AccessToken
,
nil
// if cachedToken, found := tokenCache[proxy.getAccessTokenCacheKey()]; found {
// if cachedToken.ExpiresOn.After(time.Now().Add(time.Second * 10)) {
// logger.Info("Using token from cache")
// return cachedToken.AccessToken, nil
// }
// }
// urlInterpolated, err := interpolateString(proxy.route.TokenAuth.Url, data)
// if err != nil {
// return "", err
// }
// params := make(url.Values)
// for key, value := range proxy.route.TokenAuth.Params {
// interpolatedParam, err := interpolateString(value, data)
// if err != nil {
// return "", err
// }
// params.Add(key, interpolatedParam)
// }
// getTokenReq, _ := http.NewRequest("POST", urlInterpolated, bytes.NewBufferString(params.Encode()))
// getTokenReq.Header.Add("Content-Type", "application/x-www-form-urlencoded")
// getTokenReq.Header.Add("Content-Length", strconv.Itoa(len(params.Encode())))
// resp, err := client.Do(getTokenReq)
// if err != nil {
// return "", err
// }
// defer resp.Body.Close()
// var token jwtToken
// if err := json.NewDecoder(resp.Body).Decode(&token); err != nil {
// return "", err
// }
// expiresOnEpoch, _ := strconv.ParseInt(token.ExpiresOnString, 10, 64)
// token.ExpiresOn = time.Unix(expiresOnEpoch, 0)
// tokenCache[proxy.getAccessTokenCacheKey()] = &token
// logger.Info("Got new access token", "ExpiresOn", token.ExpiresOn)
// return token.AccessToken, nil
}
}
func
(
proxy
*
DataSourceProxy
)
getAccessTokenCacheKey
()
string
{
func
(
proxy
*
DataSourceProxy
)
getAccessTokenCacheKey
()
string
{
...
...
pkg/plugins/app_plugin.go
View file @
7197a4c1
...
@@ -29,6 +29,7 @@ type AppPluginRoute struct {
...
@@ -29,6 +29,7 @@ type AppPluginRoute struct {
Url
string
`json:"url"`
Url
string
`json:"url"`
Headers
[]
AppPluginRouteHeader
`json:"headers"`
Headers
[]
AppPluginRouteHeader
`json:"headers"`
TokenAuth
*
JwtTokenAuth
`json:"tokenAuth"`
TokenAuth
*
JwtTokenAuth
`json:"tokenAuth"`
JwtTokenAuth
*
JwtTokenAuth
`json:"jwtTokenAuth"`
}
}
type
AppPluginRouteHeader
struct
{
type
AppPluginRouteHeader
struct
{
...
...
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