loki backend mode forward-oauth (#48401)
* loki: backend: add forward oauth credentials functionality * removed obsolete comment
This commit is contained in:
+26
-16
@@ -16,16 +16,23 @@ import (
|
||||
)
|
||||
|
||||
type LokiAPI struct {
|
||||
client *http.Client
|
||||
url string
|
||||
log log.Logger
|
||||
client *http.Client
|
||||
url string
|
||||
log log.Logger
|
||||
oauthToken string
|
||||
}
|
||||
|
||||
func newLokiAPI(client *http.Client, url string, log log.Logger) *LokiAPI {
|
||||
return &LokiAPI{client: client, url: url, log: log}
|
||||
func newLokiAPI(client *http.Client, url string, log log.Logger, oauthToken string) *LokiAPI {
|
||||
return &LokiAPI{client: client, url: url, log: log, oauthToken: oauthToken}
|
||||
}
|
||||
|
||||
func makeDataRequest(ctx context.Context, lokiDsUrl string, query lokiQuery) (*http.Request, error) {
|
||||
func addOauthHeader(req *http.Request, oauthToken string) {
|
||||
if oauthToken != "" {
|
||||
req.Header.Set("Authorization", oauthToken)
|
||||
}
|
||||
}
|
||||
|
||||
func makeDataRequest(ctx context.Context, lokiDsUrl string, query lokiQuery, oauthToken string) (*http.Request, error) {
|
||||
qs := url.Values{}
|
||||
qs.Set("query", query.Expr)
|
||||
|
||||
@@ -78,12 +85,7 @@ func makeDataRequest(ctx context.Context, lokiDsUrl string, query lokiQuery) (*h
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// NOTE:
|
||||
// 1. we are missing "dynamic" http params, like OAuth data.
|
||||
// this never worked before (and it is not needed for alerting scenarios),
|
||||
// so it is not a regression.
|
||||
// twe need to have that when we migrate to backend-queries.
|
||||
//
|
||||
addOauthHeader(req, oauthToken)
|
||||
|
||||
if query.VolumeQuery {
|
||||
req.Header.Set("X-Query-Tags", "Source=logvolhist")
|
||||
@@ -136,7 +138,7 @@ func makeLokiError(body io.ReadCloser) error {
|
||||
}
|
||||
|
||||
func (api *LokiAPI) DataQuery(ctx context.Context, query lokiQuery) (*loghttp.QueryResponse, error) {
|
||||
req, err := makeDataRequest(ctx, api.url, query)
|
||||
req, err := makeDataRequest(ctx, api.url, query, api.oauthToken)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -165,7 +167,7 @@ func (api *LokiAPI) DataQuery(ctx context.Context, query lokiQuery) (*loghttp.Qu
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
func makeRawRequest(ctx context.Context, lokiDsUrl string, resourceURL string) (*http.Request, error) {
|
||||
func makeRawRequest(ctx context.Context, lokiDsUrl string, resourceURL string, oauthToken string) (*http.Request, error) {
|
||||
lokiUrl, err := url.Parse(lokiDsUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -176,11 +178,19 @@ func makeRawRequest(ctx context.Context, lokiDsUrl string, resourceURL string) (
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return http.NewRequestWithContext(ctx, "GET", url.String(), nil)
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", url.String(), nil)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
addOauthHeader(req, oauthToken)
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (api *LokiAPI) RawQuery(ctx context.Context, resourceURL string) ([]byte, error) {
|
||||
req, err := makeRawRequest(ctx, api.url, resourceURL)
|
||||
req, err := makeRawRequest(ctx, api.url, resourceURL, api.oauthToken)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user