2022-01-26 16:02:19 -06:00
|
|
|
package rendering
|
|
|
|
|
|
|
|
import (
|
2023-02-08 03:30:20 -06:00
|
|
|
"bytes"
|
2022-01-26 16:02:19 -06:00
|
|
|
"context"
|
2023-02-08 03:30:20 -06:00
|
|
|
"encoding/gob"
|
2022-01-26 16:02:19 -06:00
|
|
|
"fmt"
|
2023-04-03 11:53:38 -05:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-01-26 16:02:19 -06:00
|
|
|
"time"
|
|
|
|
|
2023-04-03 11:53:38 -05:00
|
|
|
"github.com/golang-jwt/jwt/v4"
|
|
|
|
|
2022-01-26 16:02:19 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2023-04-03 11:53:38 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/metrics"
|
2022-01-26 16:02:19 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/remotecache"
|
2023-04-03 11:53:38 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
2022-01-26 16:02:19 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
const renderKeyPrefix = "render-%s"
|
|
|
|
|
|
|
|
type RenderUser struct {
|
2023-02-08 03:30:20 -06:00
|
|
|
OrgID int64 `json:"org_id"`
|
|
|
|
UserID int64 `json:"user_id"`
|
|
|
|
OrgRole string `json:"org_role"`
|
2022-01-26 16:02:19 -06:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:53:38 -05:00
|
|
|
type renderJWT struct {
|
|
|
|
RenderUser *RenderUser
|
|
|
|
jwt.RegisteredClaims
|
|
|
|
}
|
|
|
|
|
2022-01-26 16:02:19 -06:00
|
|
|
func (rs *RenderingService) GetRenderUser(ctx context.Context, key string) (*RenderUser, bool) {
|
2023-04-03 11:53:38 -05:00
|
|
|
var from string
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
var renderUser *RenderUser
|
|
|
|
|
|
|
|
if looksLikeJWT(key) && rs.features.IsEnabled(featuremgmt.FlagRenderAuthJWT) {
|
|
|
|
from = "jwt"
|
|
|
|
renderUser = rs.getRenderUserFromJWT(key)
|
|
|
|
} else {
|
|
|
|
from = "cache"
|
|
|
|
renderUser = rs.getRenderUserFromCache(ctx, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
found := renderUser != nil
|
|
|
|
success := strconv.FormatBool(found)
|
|
|
|
metrics.MRenderingUserLookupSummary.WithLabelValues(success, from).Observe(float64(time.Since(start)))
|
|
|
|
|
|
|
|
return renderUser, found
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RenderingService) getRenderUserFromJWT(key string) *RenderUser {
|
|
|
|
claims := new(renderJWT)
|
|
|
|
tkn, err := jwt.ParseWithClaims(key, claims, func(_ *jwt.Token) (interface{}, error) {
|
|
|
|
return []byte(rs.Cfg.RendererAuthToken), nil
|
|
|
|
}, jwt.WithValidMethods([]string{jwt.SigningMethodHS512.Alg()}))
|
|
|
|
|
|
|
|
if err != nil || !tkn.Valid {
|
|
|
|
rs.log.Error("Could not get render user from JWT", "err", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return claims.RenderUser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RenderingService) getRenderUserFromCache(ctx context.Context, key string) *RenderUser {
|
2023-03-10 06:57:29 -06:00
|
|
|
val, err := rs.RemoteCacheService.Get(ctx, fmt.Sprintf(renderKeyPrefix, key))
|
2022-01-26 16:02:19 -06:00
|
|
|
if err != nil {
|
2023-04-03 11:53:38 -05:00
|
|
|
rs.log.Error("Could not get render user from remote cache", "err", err)
|
|
|
|
return nil
|
2022-01-26 16:02:19 -06:00
|
|
|
}
|
2023-04-03 11:53:38 -05:00
|
|
|
|
|
|
|
ru := new(RenderUser)
|
2023-02-08 03:30:20 -06:00
|
|
|
buf := bytes.NewBuffer(val)
|
2023-04-03 11:53:38 -05:00
|
|
|
|
2023-02-08 03:30:20 -06:00
|
|
|
err = gob.NewDecoder(buf).Decode(&ru)
|
|
|
|
if err != nil {
|
2023-04-03 11:53:38 -05:00
|
|
|
rs.log.Error("Could not decode render user from remote cache", "err", err)
|
|
|
|
return nil
|
2022-01-26 16:02:19 -06:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:53:38 -05:00
|
|
|
return ru
|
2022-01-26 16:02:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func setRenderKey(cache *remotecache.RemoteCache, ctx context.Context, opts AuthOpts, renderKey string, expiry time.Duration) error {
|
2023-02-08 03:30:20 -06:00
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
err := gob.NewEncoder(buf).Encode(&RenderUser{
|
2022-01-26 16:02:19 -06:00
|
|
|
OrgID: opts.OrgID,
|
|
|
|
UserID: opts.UserID,
|
|
|
|
OrgRole: string(opts.OrgRole),
|
2023-02-08 03:30:20 -06:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-10 06:57:29 -06:00
|
|
|
return cache.Set(ctx, fmt.Sprintf(renderKeyPrefix, renderKey), buf.Bytes(), expiry)
|
2022-01-26 16:02:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func generateAndSetRenderKey(cache *remotecache.RemoteCache, ctx context.Context, opts AuthOpts, expiry time.Duration) (string, error) {
|
|
|
|
key, err := util.GetRandomString(32)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = setRenderKey(cache, ctx, opts, key, expiry)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type longLivedRenderKeyProvider struct {
|
|
|
|
cache *remotecache.RemoteCache
|
|
|
|
log log.Logger
|
|
|
|
renderKey string
|
|
|
|
authOpts AuthOpts
|
|
|
|
sessionOpts SessionOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RenderingService) CreateRenderingSession(ctx context.Context, opts AuthOpts, sessionOpts SessionOpts) (Session, error) {
|
|
|
|
renderKey, err := generateAndSetRenderKey(rs.RemoteCacheService, ctx, opts, sessionOpts.Expiry)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &longLivedRenderKeyProvider{
|
|
|
|
log: rs.log,
|
|
|
|
renderKey: renderKey,
|
|
|
|
cache: rs.RemoteCacheService,
|
|
|
|
authOpts: opts,
|
|
|
|
sessionOpts: sessionOpts,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteRenderKey(cache *remotecache.RemoteCache, log log.Logger, ctx context.Context, renderKey string) {
|
|
|
|
err := cache.Delete(ctx, fmt.Sprintf(renderKeyPrefix, renderKey))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to delete render key", "error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type perRequestRenderKeyProvider struct {
|
|
|
|
cache *remotecache.RemoteCache
|
|
|
|
log log.Logger
|
|
|
|
keyExpiry time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *perRequestRenderKeyProvider) get(ctx context.Context, opts AuthOpts) (string, error) {
|
|
|
|
return generateAndSetRenderKey(r.cache, ctx, opts, r.keyExpiry)
|
|
|
|
}
|
|
|
|
|
2023-04-03 11:53:38 -05:00
|
|
|
func (r *perRequestRenderKeyProvider) afterRequest(ctx context.Context, _ AuthOpts, renderKey string) {
|
2022-01-26 16:02:19 -06:00
|
|
|
deleteRenderKey(r.cache, r.log, ctx, renderKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *longLivedRenderKeyProvider) get(ctx context.Context, opts AuthOpts) (string, error) {
|
|
|
|
if r.sessionOpts.RefreshExpiryOnEachRequest {
|
|
|
|
err := setRenderKey(r.cache, ctx, opts, r.renderKey, r.sessionOpts.Expiry)
|
|
|
|
if err != nil {
|
|
|
|
r.log.Error("Failed to refresh render key", "error", err, "renderKey", r.renderKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r.renderKey, nil
|
|
|
|
}
|
|
|
|
|
2023-04-03 11:53:38 -05:00
|
|
|
func (r *longLivedRenderKeyProvider) afterRequest(_ context.Context, _ AuthOpts, _ string) {
|
2022-01-26 16:02:19 -06:00
|
|
|
// do nothing - renderKey from longLivedRenderKeyProvider is deleted only after session expires
|
|
|
|
// or someone calls session.Dispose()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *longLivedRenderKeyProvider) Dispose(ctx context.Context) {
|
|
|
|
deleteRenderKey(r.cache, r.log, ctx, r.renderKey)
|
|
|
|
}
|
2023-04-03 11:53:38 -05:00
|
|
|
|
|
|
|
type jwtRenderKeyProvider struct {
|
|
|
|
log log.Logger
|
|
|
|
authToken []byte
|
|
|
|
keyExpiry time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *jwtRenderKeyProvider) get(_ context.Context, opts AuthOpts) (string, error) {
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS512, j.buildJWTClaims(opts))
|
|
|
|
return token.SignedString(j.authToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *jwtRenderKeyProvider) buildJWTClaims(opts AuthOpts) renderJWT {
|
|
|
|
return renderJWT{
|
|
|
|
RenderUser: &RenderUser{
|
|
|
|
OrgID: opts.OrgID,
|
|
|
|
UserID: opts.UserID,
|
|
|
|
OrgRole: string(opts.OrgRole),
|
|
|
|
},
|
|
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
|
|
ExpiresAt: jwt.NewNumericDate(time.Now().UTC().Add(j.keyExpiry)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *jwtRenderKeyProvider) afterRequest(_ context.Context, _ AuthOpts, _ string) {
|
|
|
|
// do nothing - the JWT will just expire
|
|
|
|
}
|
|
|
|
|
|
|
|
func looksLikeJWT(key string) bool {
|
|
|
|
return strings.HasPrefix(key, "eyJ")
|
|
|
|
}
|