2024-09-05 14:43:54 +08:00
|
|
|
package iam
|
2024-07-26 22:09:08 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-02-03 21:40:36 +08:00
|
|
|
"strings"
|
2024-07-26 22:09:08 +08:00
|
|
|
|
2024-09-23 17:26:44 +08:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
|
"k8s.io/apiserver/pkg/authorization/authorizer"
|
|
|
|
|
"k8s.io/apiserver/pkg/registry/rest"
|
|
|
|
|
genericapiserver "k8s.io/apiserver/pkg/server"
|
|
|
|
|
common "k8s.io/kube-openapi/pkg/common"
|
2025-02-03 21:40:36 +08:00
|
|
|
"k8s.io/kube-openapi/pkg/spec3"
|
2025-02-03 19:24:35 +08:00
|
|
|
"k8s.io/kube-openapi/pkg/validation/spec"
|
2024-09-23 17:26:44 +08:00
|
|
|
|
2025-01-21 17:06:55 +08:00
|
|
|
"github.com/grafana/authlib/types"
|
2024-09-05 14:43:54 +08:00
|
|
|
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
2024-09-05 19:43:54 +08:00
|
|
|
iamv0 "github.com/grafana/grafana/pkg/apis/iam/v0alpha1"
|
2024-09-05 14:43:54 +08:00
|
|
|
"github.com/grafana/grafana/pkg/infra/db"
|
|
|
|
|
"github.com/grafana/grafana/pkg/registry/apis/iam/legacy"
|
|
|
|
|
"github.com/grafana/grafana/pkg/registry/apis/iam/serviceaccount"
|
|
|
|
|
"github.com/grafana/grafana/pkg/registry/apis/iam/sso"
|
|
|
|
|
"github.com/grafana/grafana/pkg/registry/apis/iam/team"
|
|
|
|
|
"github.com/grafana/grafana/pkg/registry/apis/iam/user"
|
2024-09-23 17:26:44 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
2024-09-05 14:43:54 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/apiserver/builder"
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/ssosettings"
|
|
|
|
|
"github.com/grafana/grafana/pkg/storage/legacysql"
|
2024-07-26 22:09:08 +08:00
|
|
|
)
|
|
|
|
|
|
2024-09-05 14:43:54 +08:00
|
|
|
var _ builder.APIGroupBuilder = (*IdentityAccessManagementAPIBuilder)(nil)
|
2024-07-26 22:09:08 +08:00
|
|
|
|
|
|
|
|
// This is used just so wire has something unique to return
|
2024-09-05 14:43:54 +08:00
|
|
|
type IdentityAccessManagementAPIBuilder struct {
|
2024-09-23 17:26:44 +08:00
|
|
|
store legacy.LegacyIdentityStore
|
|
|
|
|
authorizer authorizer.Authorizer
|
2025-01-21 17:06:55 +08:00
|
|
|
accessClient types.AccessClient
|
2024-09-23 17:26:44 +08:00
|
|
|
|
2025-02-03 19:24:35 +08:00
|
|
|
// non-k8s api route
|
|
|
|
|
display *user.LegacyDisplayREST
|
|
|
|
|
|
2024-09-23 17:26:44 +08:00
|
|
|
// Not set for multi-tenant deployment for now
|
|
|
|
|
sso ssosettings.Service
|
2024-07-26 22:09:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func RegisterAPIService(
|
|
|
|
|
apiregistration builder.APIRegistrar,
|
2024-08-21 15:16:47 +08:00
|
|
|
ssoService ssosettings.Service,
|
2024-08-15 19:38:43 +08:00
|
|
|
sql db.DB,
|
2024-09-23 17:26:44 +08:00
|
|
|
ac accesscontrol.AccessControl,
|
2024-09-05 14:43:54 +08:00
|
|
|
) (*IdentityAccessManagementAPIBuilder, error) {
|
2024-09-23 17:26:44 +08:00
|
|
|
store := legacy.NewLegacySQLStores(legacysql.NewDatabaseProvider(sql))
|
|
|
|
|
authorizer, client := newLegacyAuthorizer(ac, store)
|
|
|
|
|
|
2024-09-05 14:43:54 +08:00
|
|
|
builder := &IdentityAccessManagementAPIBuilder{
|
2024-09-23 17:26:44 +08:00
|
|
|
store: store,
|
|
|
|
|
sso: ssoService,
|
|
|
|
|
authorizer: authorizer,
|
|
|
|
|
accessClient: client,
|
2025-02-03 19:24:35 +08:00
|
|
|
display: user.NewLegacyDisplayREST(store),
|
2024-07-26 22:09:08 +08:00
|
|
|
}
|
|
|
|
|
apiregistration.RegisterAPI(builder)
|
2024-08-21 15:16:47 +08:00
|
|
|
|
2024-08-15 19:38:43 +08:00
|
|
|
return builder, nil
|
2024-07-26 22:09:08 +08:00
|
|
|
}
|
|
|
|
|
|
2024-09-23 17:26:44 +08:00
|
|
|
func NewAPIService(store legacy.LegacyIdentityStore) *IdentityAccessManagementAPIBuilder {
|
|
|
|
|
return &IdentityAccessManagementAPIBuilder{
|
2025-02-03 19:24:35 +08:00
|
|
|
store: store,
|
|
|
|
|
display: user.NewLegacyDisplayREST(store),
|
2024-09-23 17:26:44 +08:00
|
|
|
authorizer: authorizer.AuthorizerFunc(
|
|
|
|
|
func(ctx context.Context, a authorizer.Attributes) (authorizer.Decision, string, error) {
|
|
|
|
|
user, err := identity.GetRequester(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return authorizer.DecisionDeny, "no identity found", err
|
|
|
|
|
}
|
|
|
|
|
if user.GetIsGrafanaAdmin() {
|
|
|
|
|
return authorizer.DecisionAllow, "", nil
|
|
|
|
|
}
|
|
|
|
|
return authorizer.DecisionDeny, "only grafana admins have access for now", nil
|
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-05 14:43:54 +08:00
|
|
|
func (b *IdentityAccessManagementAPIBuilder) GetGroupVersion() schema.GroupVersion {
|
2024-09-05 19:43:54 +08:00
|
|
|
return iamv0.SchemeGroupVersion
|
2024-07-26 22:09:08 +08:00
|
|
|
}
|
|
|
|
|
|
2024-09-05 14:43:54 +08:00
|
|
|
func (b *IdentityAccessManagementAPIBuilder) InstallSchema(scheme *runtime.Scheme) error {
|
2024-09-05 19:43:54 +08:00
|
|
|
iamv0.AddKnownTypes(scheme, iamv0.VERSION)
|
2024-07-26 22:09:08 +08:00
|
|
|
|
|
|
|
|
// Link this version to the internal representation.
|
|
|
|
|
// This is used for server-side-apply (PATCH), and avoids the error:
|
2024-08-21 15:16:47 +08:00
|
|
|
// "no kind is registered for the type"
|
2024-09-05 19:43:54 +08:00
|
|
|
iamv0.AddKnownTypes(scheme, runtime.APIVersionInternal)
|
2024-07-26 22:09:08 +08:00
|
|
|
|
2024-09-05 19:43:54 +08:00
|
|
|
metav1.AddToGroupVersion(scheme, iamv0.SchemeGroupVersion)
|
|
|
|
|
return scheme.SetVersionPriority(iamv0.SchemeGroupVersion)
|
2024-07-26 22:09:08 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-15 12:46:08 +08:00
|
|
|
func (b *IdentityAccessManagementAPIBuilder) UpdateAPIGroupInfo(apiGroupInfo *genericapiserver.APIGroupInfo, _ builder.APIGroupOptions) error {
|
2024-07-26 22:09:08 +08:00
|
|
|
storage := map[string]rest.Storage{}
|
|
|
|
|
|
2024-09-05 19:43:54 +08:00
|
|
|
teamResource := iamv0.TeamResourceInfo
|
2024-10-10 22:47:31 +08:00
|
|
|
storage[teamResource.StoragePath()] = team.NewLegacyStore(b.store, b.accessClient)
|
2024-09-23 17:26:44 +08:00
|
|
|
storage[teamResource.StoragePath("members")] = team.NewLegacyTeamMemberREST(b.store)
|
2024-07-26 22:09:08 +08:00
|
|
|
|
2024-09-05 19:43:54 +08:00
|
|
|
teamBindingResource := iamv0.TeamBindingResourceInfo
|
2024-09-23 17:26:44 +08:00
|
|
|
storage[teamBindingResource.StoragePath()] = team.NewLegacyBindingStore(b.store)
|
2024-08-27 14:31:29 +08:00
|
|
|
|
2024-09-05 19:43:54 +08:00
|
|
|
userResource := iamv0.UserResourceInfo
|
2024-09-23 17:26:44 +08:00
|
|
|
storage[userResource.StoragePath()] = user.NewLegacyStore(b.store, b.accessClient)
|
|
|
|
|
storage[userResource.StoragePath("teams")] = user.NewLegacyTeamMemberREST(b.store)
|
2024-08-21 15:16:47 +08:00
|
|
|
|
2024-09-24 21:13:04 +08:00
|
|
|
serviceAccountResource := iamv0.ServiceAccountResourceInfo
|
2024-09-27 21:53:11 +08:00
|
|
|
storage[serviceAccountResource.StoragePath()] = serviceaccount.NewLegacyStore(b.store, b.accessClient)
|
2024-09-24 21:13:04 +08:00
|
|
|
storage[serviceAccountResource.StoragePath("tokens")] = serviceaccount.NewLegacyTokenREST(b.store)
|
2024-08-21 15:16:47 +08:00
|
|
|
|
2024-09-23 17:26:44 +08:00
|
|
|
if b.sso != nil {
|
2024-09-05 19:43:54 +08:00
|
|
|
ssoResource := iamv0.SSOSettingResourceInfo
|
2024-09-23 17:26:44 +08:00
|
|
|
storage[ssoResource.StoragePath()] = sso.NewLegacyStore(b.sso)
|
2024-07-26 22:09:08 +08:00
|
|
|
}
|
|
|
|
|
|
2024-09-05 19:43:54 +08:00
|
|
|
apiGroupInfo.VersionedResourcesStorageMap[iamv0.VERSION] = storage
|
2024-09-24 10:07:52 +08:00
|
|
|
return nil
|
2024-07-26 22:09:08 +08:00
|
|
|
}
|
|
|
|
|
|
2024-09-05 14:43:54 +08:00
|
|
|
func (b *IdentityAccessManagementAPIBuilder) GetOpenAPIDefinitions() common.GetOpenAPIDefinitions {
|
2024-09-05 19:43:54 +08:00
|
|
|
return iamv0.GetOpenAPIDefinitions
|
2024-07-26 22:09:08 +08:00
|
|
|
}
|
|
|
|
|
|
2025-02-03 21:40:36 +08:00
|
|
|
func (b *IdentityAccessManagementAPIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.OpenAPI, error) {
|
|
|
|
|
oas.Info.Description = "Identity and Access Management"
|
|
|
|
|
|
|
|
|
|
defs := b.GetOpenAPIDefinitions()(func(path string) spec.Ref { return spec.Ref{} })
|
|
|
|
|
defsBase := "github.com/grafana/grafana/pkg/apis/iam/v0alpha1."
|
|
|
|
|
|
|
|
|
|
// Add missing schemas
|
|
|
|
|
for k, v := range defs {
|
|
|
|
|
clean := strings.Replace(k, defsBase, "com.github.grafana.grafana.pkg.apis.iam.v0alpha1.", 1)
|
|
|
|
|
if oas.Components.Schemas[clean] == nil {
|
|
|
|
|
oas.Components.Schemas[clean] = &v.Schema
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
compBase := "com.github.grafana.grafana.pkg.apis.iam.v0alpha1."
|
|
|
|
|
schema := oas.Components.Schemas[compBase+"DisplayList"].Properties["display"]
|
|
|
|
|
schema.Items = &spec.SchemaOrArray{
|
|
|
|
|
Schema: &spec.Schema{
|
|
|
|
|
SchemaProps: spec.SchemaProps{
|
|
|
|
|
AllOf: []spec.Schema{
|
|
|
|
|
{
|
|
|
|
|
SchemaProps: spec.SchemaProps{
|
|
|
|
|
Ref: spec.MustCreateRef("#/components/schemas/" + compBase + "Display"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
oas.Components.Schemas[compBase+"DisplayList"].Properties["display"] = schema
|
|
|
|
|
oas.Components.Schemas[compBase+"DisplayList"].Properties["metadata"] = spec.Schema{
|
|
|
|
|
SchemaProps: spec.SchemaProps{
|
|
|
|
|
AllOf: []spec.Schema{
|
|
|
|
|
{
|
|
|
|
|
SchemaProps: spec.SchemaProps{
|
|
|
|
|
Ref: spec.MustCreateRef("#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.ListMeta"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}},
|
|
|
|
|
}
|
|
|
|
|
oas.Components.Schemas[compBase+"Display"].Properties["identity"] = spec.Schema{
|
|
|
|
|
SchemaProps: spec.SchemaProps{
|
|
|
|
|
AllOf: []spec.Schema{
|
|
|
|
|
{
|
|
|
|
|
SchemaProps: spec.SchemaProps{
|
|
|
|
|
Ref: spec.MustCreateRef("#/components/schemas/" + compBase + "IdentityRef"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}},
|
|
|
|
|
}
|
|
|
|
|
return oas, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-03 19:24:35 +08:00
|
|
|
func (b *IdentityAccessManagementAPIBuilder) GetAPIRoutes() *builder.APIRoutes {
|
|
|
|
|
defs := b.GetOpenAPIDefinitions()(func(path string) spec.Ref { return spec.Ref{} })
|
|
|
|
|
return b.display.GetAPIRoutes(defs)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-05 14:43:54 +08:00
|
|
|
func (b *IdentityAccessManagementAPIBuilder) GetAuthorizer() authorizer.Authorizer {
|
2024-09-23 17:26:44 +08:00
|
|
|
return b.authorizer
|
2024-07-26 22:09:08 +08:00
|
|
|
}
|