datasources: querier: single-tenant auth (#102049)
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package query
|
package query
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
@@ -13,6 +14,7 @@ import (
|
|||||||
common "k8s.io/kube-openapi/pkg/common"
|
common "k8s.io/kube-openapi/pkg/common"
|
||||||
"k8s.io/kube-openapi/pkg/spec3"
|
"k8s.io/kube-openapi/pkg/spec3"
|
||||||
|
|
||||||
|
claims "github.com/grafana/authlib/types"
|
||||||
query "github.com/grafana/grafana/pkg/apis/query/v0alpha1"
|
query "github.com/grafana/grafana/pkg/apis/query/v0alpha1"
|
||||||
"github.com/grafana/grafana/pkg/expr"
|
"github.com/grafana/grafana/pkg/expr"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
@@ -38,6 +40,8 @@ type QueryAPIBuilder struct {
|
|||||||
userFacingDefaultError string
|
userFacingDefaultError string
|
||||||
features featuremgmt.FeatureToggles
|
features featuremgmt.FeatureToggles
|
||||||
|
|
||||||
|
authorizer authorizer.Authorizer
|
||||||
|
|
||||||
tracer tracing.Tracer
|
tracer tracing.Tracer
|
||||||
metrics *queryMetrics
|
metrics *queryMetrics
|
||||||
parser *queryParser
|
parser *queryParser
|
||||||
@@ -49,6 +53,7 @@ type QueryAPIBuilder struct {
|
|||||||
|
|
||||||
func NewQueryAPIBuilder(features featuremgmt.FeatureToggles,
|
func NewQueryAPIBuilder(features featuremgmt.FeatureToggles,
|
||||||
client clientapi.DataSourceClientSupplier,
|
client clientapi.DataSourceClientSupplier,
|
||||||
|
ar authorizer.Authorizer,
|
||||||
registry query.DataSourceApiServerRegistry,
|
registry query.DataSourceApiServerRegistry,
|
||||||
legacy service.LegacyDataSourceLookup,
|
legacy service.LegacyDataSourceLookup,
|
||||||
registerer prometheus.Registerer,
|
registerer prometheus.Registerer,
|
||||||
@@ -75,6 +80,7 @@ func NewQueryAPIBuilder(features featuremgmt.FeatureToggles,
|
|||||||
concurrentQueryLimit: 4,
|
concurrentQueryLimit: 4,
|
||||||
log: log.New("query_apiserver"),
|
log: log.New("query_apiserver"),
|
||||||
client: client,
|
client: client,
|
||||||
|
authorizer: ar,
|
||||||
registry: registry,
|
registry: registry,
|
||||||
parser: newQueryParser(reader, legacy, tracer, log.New("query_parser")),
|
parser: newQueryParser(reader, legacy, tracer, log.New("query_parser")),
|
||||||
metrics: newQueryMetrics(registerer),
|
metrics: newQueryMetrics(registerer),
|
||||||
@@ -105,11 +111,25 @@ func RegisterAPIService(features featuremgmt.FeatureToggles,
|
|||||||
return nil, nil // skip registration unless explicitly added (or all experimental are added)
|
return nil, nil // skip registration unless explicitly added (or all experimental are added)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ar := authorizer.AuthorizerFunc(
|
||||||
|
func(ctx context.Context, attr authorizer.Attributes) (authorized authorizer.Decision, reason string, err error) {
|
||||||
|
// we only verify that we have a valid user.
|
||||||
|
// the "real" check will happen when the specific
|
||||||
|
// data sources are loaded.
|
||||||
|
_, ok := claims.AuthInfoFrom(ctx)
|
||||||
|
if !ok {
|
||||||
|
return authorizer.DecisionDeny, "valid user is required", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return authorizer.DecisionAllow, "", nil
|
||||||
|
})
|
||||||
|
|
||||||
builder, err := NewQueryAPIBuilder(
|
builder, err := NewQueryAPIBuilder(
|
||||||
features,
|
features,
|
||||||
&CommonDataSourceClientSupplier{
|
&CommonDataSourceClientSupplier{
|
||||||
Client: client.NewQueryClientForPluginClient(pluginClient, pCtxProvider),
|
Client: client.NewQueryClientForPluginClient(pluginClient, pCtxProvider),
|
||||||
},
|
},
|
||||||
|
ar,
|
||||||
client.NewDataSourceRegistryFromStore(pluginStore, dataSourcesService),
|
client.NewDataSourceRegistryFromStore(pluginStore, dataSourcesService),
|
||||||
legacy, registerer, tracer,
|
legacy, registerer, tracer,
|
||||||
)
|
)
|
||||||
@@ -167,7 +187,7 @@ func (b *QueryAPIBuilder) GetOpenAPIDefinitions() common.GetOpenAPIDefinitions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *QueryAPIBuilder) GetAuthorizer() authorizer.Authorizer {
|
func (b *QueryAPIBuilder) GetAuthorizer() authorizer.Authorizer {
|
||||||
return nil // default is OK
|
return b.authorizer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *QueryAPIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.OpenAPI, error) {
|
func (b *QueryAPIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.OpenAPI, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user