Authz: add support to use folder api to fetch folder tree (#100038)

* Add FolderStore interface

* Authz: add implementation to use folders api and use it inproc with loopback config

* Add tracing and add rest.Config for talking with folder api using access tokens

* Restructure test to get rid of circular dependencies in tests

* use correct group version kind

---------

Co-authored-by: gamab <gabriel.mabille@grafana.com>
This commit is contained in:
Karl Persson
2025-02-13 11:59:59 +01:00
committed by GitHub
co-authored by gamab
parent ae9837b793
commit 1b1954de28
14 changed files with 367 additions and 172 deletions
+46 -3
View File
@@ -3,11 +3,13 @@
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Kubernetes Authors.
package apistore
package apistore_test
import (
"context"
"fmt"
"os"
"strings"
"testing"
"time"
@@ -16,6 +18,7 @@ import (
"gocloud.dev/blob/fileblob"
"gocloud.dev/blob/memblob"
"k8s.io/apimachinery/pkg/api/apitesting"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
@@ -28,9 +31,11 @@ import (
"k8s.io/apiserver/pkg/storage/storagebackend"
"k8s.io/apiserver/pkg/storage/storagebackend/factory"
grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic"
storagetesting "github.com/grafana/grafana/pkg/apiserver/storage/testing"
infraDB "github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/storage/unified/apistore"
"github.com/grafana/grafana/pkg/storage/unified/resource"
"github.com/grafana/grafana/pkg/storage/unified/sql"
"github.com/grafana/grafana/pkg/storage/unified/sql/db/dbimpl"
@@ -160,7 +165,7 @@ func testSetup(t testing.TB, opts ...setupOption) (context.Context, storage.Inte
client := resource.NewLocalResourceClient(server)
config := storagebackend.NewDefaultConfig(setupOpts.prefix, setupOpts.codec)
store, destroyFunc, err := NewStorage(
store, destroyFunc, err := apistore.NewStorage(
config.ForResource(setupOpts.groupResource),
client,
func(obj runtime.Object) (string, error) {
@@ -176,7 +181,7 @@ func testSetup(t testing.TB, opts ...setupOption) (context.Context, storage.Inte
storage.DefaultNamespaceScopedAttr,
make(map[string]storage.IndexerFunc, 0),
nil,
StorageOptions{},
apistore.StorageOptions{},
)
if err != nil {
return nil, nil, nil, err
@@ -371,3 +376,41 @@ func newPod() runtime.Object {
func newPodList() runtime.Object {
return &example.PodList{}
}
func testKeyParser(val string) (*resource.ResourceKey, error) {
k, err := grafanaregistry.ParseKey(val)
if err != nil {
if strings.HasPrefix(val, "pods/") {
parts := strings.Split(val, "/")
if len(parts) == 2 {
err = nil
k = &grafanaregistry.Key{
Resource: parts[0], // pods
Name: parts[1],
}
} else if len(parts) == 3 {
err = nil
k = &grafanaregistry.Key{
Resource: parts[0], // pods
Namespace: parts[1],
Name: parts[2],
}
}
}
}
if err != nil {
return nil, err
}
if k.Group == "" {
k.Group = "example.apiserver.k8s.io"
}
if k.Resource == "" {
return nil, apierrors.NewInternalError(fmt.Errorf("missing resource in request"))
}
return &resource.ResourceKey{
Namespace: k.Namespace,
Group: k.Group,
Resource: k.Resource,
Name: k.Name,
}, err
}