mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
refactoring (aesthetics)
Co-Authored-By: Gabriel MABILLE <gamab@users.noreply.github.com>
This commit is contained in:
parent
499a31df53
commit
125cb3c834
@ -26,32 +26,27 @@ func NewLocalResourceStoreClient(server ResourceStoreServer) ResourceStoreClient
|
|||||||
// scenario: local in-proc
|
// scenario: local in-proc
|
||||||
channel := &inprocgrpc.Channel{}
|
channel := &inprocgrpc.Channel{}
|
||||||
|
|
||||||
grpcAuthInterceptor := grpcutils.NewInProcGrpcAuthenticator()
|
grpcAuthInt := grpcutils.NewInProcGrpcAuthenticator()
|
||||||
channel.RegisterService(
|
channel.RegisterService(
|
||||||
grpchan.InterceptServer(
|
grpchan.InterceptServer(
|
||||||
&ResourceStore_ServiceDesc,
|
&ResourceStore_ServiceDesc,
|
||||||
grpcAuth.UnaryServerInterceptor(grpcAuthInterceptor.Authenticate),
|
grpcAuth.UnaryServerInterceptor(grpcAuthInt.Authenticate),
|
||||||
grpcAuth.StreamServerInterceptor(grpcAuthInterceptor.Authenticate),
|
grpcAuth.StreamServerInterceptor(grpcAuthInt.Authenticate),
|
||||||
),
|
),
|
||||||
server,
|
server,
|
||||||
)
|
)
|
||||||
|
|
||||||
clientInterceptor, _ := authnlib.NewGrpcClientInterceptor(
|
clientInt, _ := authnlib.NewGrpcClientInterceptor(
|
||||||
&authnlib.GrpcClientConfig{},
|
&authnlib.GrpcClientConfig{},
|
||||||
authnlib.WithDisableAccessTokenOption(),
|
authnlib.WithDisableAccessTokenOption(),
|
||||||
authnlib.WithIDTokenExtractorOption(idTokenExtractor),
|
authnlib.WithIDTokenExtractorOption(idTokenExtractor),
|
||||||
)
|
)
|
||||||
return NewResourceStoreClient(
|
return NewResourceStoreClient(grpchan.InterceptClientConn(channel, clientInt.UnaryClientInterceptor, clientInt.StreamClientInterceptor))
|
||||||
grpchan.InterceptClientConn(
|
|
||||||
channel,
|
|
||||||
clientInterceptor.UnaryClientInterceptor,
|
|
||||||
clientInterceptor.StreamClientInterceptor,
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewResourceStoreClientGRPC(conn *grpc.ClientConn) (ResourceStoreClient, error) {
|
func NewResourceStoreClientGRPC(conn *grpc.ClientConn) (ResourceStoreClient, error) {
|
||||||
// scenario: remote on-prem
|
// scenario: remote on-prem
|
||||||
clientInterceptor, err := authnlib.NewGrpcClientInterceptor(
|
clientInt, err := authnlib.NewGrpcClientInterceptor(
|
||||||
&authnlib.GrpcClientConfig{},
|
&authnlib.GrpcClientConfig{},
|
||||||
authnlib.WithDisableAccessTokenOption(),
|
authnlib.WithDisableAccessTokenOption(),
|
||||||
authnlib.WithIDTokenExtractorOption(idTokenExtractor),
|
authnlib.WithIDTokenExtractorOption(idTokenExtractor),
|
||||||
@ -61,28 +56,12 @@ func NewResourceStoreClientGRPC(conn *grpc.ClientConn) (ResourceStoreClient, err
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewResourceStoreClient(
|
return NewResourceStoreClient(grpchan.InterceptClientConn(conn, clientInt.UnaryClientInterceptor, clientInt.StreamClientInterceptor)), nil
|
||||||
grpchan.InterceptClientConn(
|
|
||||||
conn,
|
|
||||||
clientInterceptor.UnaryClientInterceptor,
|
|
||||||
clientInterceptor.StreamClientInterceptor,
|
|
||||||
)),
|
|
||||||
nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewResourceStoreClientCloud(conn *grpc.ClientConn, cfg *setting.Cfg) (ResourceStoreClient, error) {
|
func NewResourceStoreClientCloud(conn *grpc.ClientConn, cfg *setting.Cfg) (ResourceStoreClient, error) {
|
||||||
// scenario: remote cloud
|
// scenario: remote cloud
|
||||||
grpcClientCfg := grpcutils.ReadGrpcClientConfig(cfg)
|
grpcClientConfig := clientCfgMapping(grpcutils.ReadGrpcClientConfig(cfg))
|
||||||
grpcClientConfig := authnlib.GrpcClientConfig{
|
|
||||||
TokenClientConfig: &authnlib.TokenExchangeConfig{
|
|
||||||
Token: grpcClientCfg.Token,
|
|
||||||
TokenExchangeURL: grpcClientCfg.TokenExchangeURL,
|
|
||||||
},
|
|
||||||
TokenRequest: &authnlib.TokenExchangeRequest{
|
|
||||||
Namespace: grpcClientCfg.TokenNamespace,
|
|
||||||
Audiences: []string{resourceStoreAudience},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
opts := []authnlib.GrpcClientInterceptorOption{
|
opts := []authnlib.GrpcClientInterceptorOption{
|
||||||
authnlib.WithIDTokenExtractorOption(idTokenExtractor),
|
authnlib.WithIDTokenExtractorOption(idTokenExtractor),
|
||||||
@ -90,29 +69,15 @@ func NewResourceStoreClientCloud(conn *grpc.ClientConn, cfg *setting.Cfg) (Resou
|
|||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Env == setting.Dev {
|
if cfg.Env == setting.Dev {
|
||||||
client := &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
|
opts = allowInsecureTransportOpt(&grpcClientConfig, opts)
|
||||||
tokenClient, _ := authnlib.NewTokenExchangeClient(
|
|
||||||
*grpcClientConfig.TokenClientConfig,
|
|
||||||
authnlib.WithHTTPClient(client),
|
|
||||||
)
|
|
||||||
opts = append(opts, authnlib.WithTokenClientOption(tokenClient))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
clientInterceptor, err := authnlib.NewGrpcClientInterceptor(
|
clientInt, err := authnlib.NewGrpcClientInterceptor(&grpcClientConfig, opts...)
|
||||||
&grpcClientConfig,
|
|
||||||
opts...,
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewResourceStoreClient(
|
return NewResourceStoreClient(grpchan.InterceptClientConn(conn, clientInt.UnaryClientInterceptor, clientInt.StreamClientInterceptor)), nil
|
||||||
grpchan.InterceptClientConn(
|
|
||||||
conn,
|
|
||||||
clientInterceptor.UnaryClientInterceptor,
|
|
||||||
clientInterceptor.StreamClientInterceptor,
|
|
||||||
)),
|
|
||||||
nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func idTokenExtractor(ctx context.Context) (string, error) {
|
func idTokenExtractor(ctx context.Context) (string, error) {
|
||||||
@ -143,3 +108,22 @@ func stackIdExtractor(stackID string) func(ctx context.Context) (key string, val
|
|||||||
return authzlib.DefaultStackIDMetadataKey, []string{stackID}, nil
|
return authzlib.DefaultStackIDMetadataKey, []string{stackID}, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func allowInsecureTransportOpt(grpcClientConfig *authnlib.GrpcClientConfig, opts []authnlib.GrpcClientInterceptorOption) []authnlib.GrpcClientInterceptorOption {
|
||||||
|
client := &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
|
||||||
|
tokenClient, _ := authnlib.NewTokenExchangeClient(*grpcClientConfig.TokenClientConfig, authnlib.WithHTTPClient(client))
|
||||||
|
return append(opts, authnlib.WithTokenClientOption(tokenClient))
|
||||||
|
}
|
||||||
|
|
||||||
|
func clientCfgMapping(clientCfg *grpcutils.GrpcClientConfig) authnlib.GrpcClientConfig {
|
||||||
|
return authnlib.GrpcClientConfig{
|
||||||
|
TokenClientConfig: &authnlib.TokenExchangeConfig{
|
||||||
|
Token: clientCfg.Token,
|
||||||
|
TokenExchangeURL: clientCfg.TokenExchangeURL,
|
||||||
|
},
|
||||||
|
TokenRequest: &authnlib.TokenExchangeRequest{
|
||||||
|
Namespace: clientCfg.TokenNamespace,
|
||||||
|
Audiences: []string{resourceStoreAudience},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user