Auth: Add access token to in-proc communication and ServiceIdentity (#98926)
Use fake access token for in-proc grpc and add ServiceIdentity --------- Co-authored-by: gamab <gabriel.mabille@grafana.com> Co-authored-by: Karl Persson <23356117+kalleep@users.noreply.github.com>
This commit is contained in:
co-authored by
gamab
Karl Persson
parent
eb2d276a42
commit
437b7a565d
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/fullstorydev/grpchan"
|
||||
@@ -12,7 +13,8 @@ import (
|
||||
"google.golang.org/grpc"
|
||||
|
||||
authnlib "github.com/grafana/authlib/authn"
|
||||
claims "github.com/grafana/authlib/types"
|
||||
"github.com/grafana/authlib/types"
|
||||
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/tracing"
|
||||
"github.com/grafana/grafana/pkg/services/authn/grpcutils"
|
||||
@@ -70,8 +72,8 @@ func NewLocalResourceClient(server ResourceServer) ResourceClient {
|
||||
}
|
||||
|
||||
clientInt, _ := authnlib.NewGrpcClientInterceptor(
|
||||
&authnlib.GrpcClientConfig{},
|
||||
authnlib.WithDisableAccessTokenOption(),
|
||||
&authnlib.GrpcClientConfig{TokenRequest: &authnlib.TokenExchangeRequest{}},
|
||||
authnlib.WithTokenClientOption(grpcutils.ProvideInProcExchanger()),
|
||||
authnlib.WithIDTokenExtractorOption(idTokenExtractor),
|
||||
)
|
||||
|
||||
@@ -131,15 +133,28 @@ func NewCloudResourceClient(tracer tracing.Tracer, conn *grpc.ClientConn, cfg au
|
||||
}, nil
|
||||
}
|
||||
|
||||
var authLogger = slog.Default().With("logger", "resource-client-auth-interceptor")
|
||||
|
||||
func idTokenExtractor(ctx context.Context) (string, error) {
|
||||
authInfo, ok := claims.AuthInfoFrom(ctx)
|
||||
if identity.IsServiceIdentity(ctx) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
info, ok := types.AuthInfoFrom(ctx)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("no claims found")
|
||||
}
|
||||
|
||||
extra := authInfo.GetExtra()
|
||||
if token, exists := extra["id-token"]; exists && len(token) != 0 && token[0] != "" {
|
||||
return token[0], nil
|
||||
if token := info.GetIDToken(); len(token) != 0 {
|
||||
return token, nil
|
||||
}
|
||||
|
||||
if !types.IsIdentityType(info.GetIdentityType(), types.TypeAccessPolicy) {
|
||||
authLogger.Warn(
|
||||
"calling resource store as the service without id token or marking it as the service identity",
|
||||
"subject", info.GetSubject(),
|
||||
"uid", info.GetUID(),
|
||||
)
|
||||
}
|
||||
|
||||
return "", nil
|
||||
|
||||
Reference in New Issue
Block a user