From f5e649183b0893f12ae21f5bbc336b0c8d18aeab Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Tue, 29 Jul 2025 13:29:28 +0200 Subject: [PATCH] do not expose OpenAPI unless it exists --- pkg/registry/apis/datasource/openapi.go | 18 ++++++++++++++++-- pkg/registry/apis/datasource/plugincontext.go | 6 +++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/pkg/registry/apis/datasource/openapi.go b/pkg/registry/apis/datasource/openapi.go index 7058027a190..e3e3df468fa 100644 --- a/pkg/registry/apis/datasource/openapi.go +++ b/pkg/registry/apis/datasource/openapi.go @@ -32,6 +32,20 @@ func (b *DataSourceAPIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.Op return nil, err } + // Hide the resource routes -- explicit ones will be added if defined below + prefix := root + "namespaces/{namespace}/datasources/{name}/resource" + r := oas.Paths.Paths[prefix] + if r != nil && r.Get != nil { + r.Get.Description = "Get resources in the datasource plugin. NOTE, additional routes may exist, but are not exposed via OpenAPI" + r.Delete = nil + r.Head = nil + r.Patch = nil + r.Post = nil + r.Put = nil + r.Options = nil + } + delete(oas.Paths.Paths, prefix+"/{path}") + // Set explicit apiVersion and kind on the datasource ds, ok := oas.Components.Schemas["com.github.grafana.grafana.pkg.apis.datasource.v0alpha1.DataSource"] if !ok { @@ -104,7 +118,7 @@ func (b *DataSourceAPIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.Op if len(custom.Routes) > 0 { ds := oas.Paths.Paths[root+"namespaces/{namespace}/datasources/{name}"] if ds == nil || len(ds.Parameters) < 2 { - return nil, fmt.Errorf("missing parmeters") + return nil, fmt.Errorf("missing Parameters") } prefix := root + "namespaces/{namespace}/datasources/{name}/resource" @@ -124,7 +138,7 @@ func (b *DataSourceAPIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.Op op.Tags = append(op.Tags, "Route") // Custom resource? } } - oas.Paths.Paths[prefix+k] = v // TODO add namepsace + name parameters + oas.Paths.Paths[prefix+k] = v // TODO add namespace + name parameters } } return oas, err diff --git a/pkg/registry/apis/datasource/plugincontext.go b/pkg/registry/apis/datasource/plugincontext.go index af5533fc147..4ba32534ddd 100644 --- a/pkg/registry/apis/datasource/plugincontext.go +++ b/pkg/registry/apis/datasource/plugincontext.go @@ -18,10 +18,10 @@ import ( // Authorization checks will happen within each function, and the user in ctx will // limit which namespace/tenant/org we are talking to type PluginDatasourceProvider interface { - // Get a single datasurce + // Get a single data source (any type) GetDataSource(ctx context.Context, uid string) (*datasourceV0.DataSource, error) - // List all datasources + // List all datasources (any type) ListDataSource(ctx context.Context) (*datasourceV0.DataSourceList, error) // Create a data source @@ -30,7 +30,7 @@ type PluginDatasourceProvider interface { // Update a data source UpdateDataSource(ctx context.Context, ds *datasourceV0.DataSource) (*datasourceV0.DataSource, error) - // Delete datasurce + // Delete a data source (any type) Delete(ctx context.Context, uid string) error // Return settings (decrypted!) for a specific plugin