package authz import ( "context" auth "github.com/envoyproxy/go-control-plane/envoy/service/auth/v3" xcontext "github.com/xlgmokha/x/pkg/context" "github.com/xlgmokha/x/pkg/log" "github.com/xlgmokha/x/pkg/x" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls" "google.golang.org/grpc" "google.golang.org/grpc/reflection" ) var Connection xcontext.Key[*grpc.ClientConn] = xcontext.Key[*grpc.ClientConn]("grpc_client") type Server struct { *grpc.Server } func New(ctx context.Context, options ...grpc.ServerOption) *Server { logger := log.From(ctx) server := grpc.NewServer(x.Prepend( options, grpc.UnaryInterceptor(pls.LogGRPC(logger)), grpc.StreamInterceptor(pls.LogGRPCStream(logger)), )...) connection := Connection.From(ctx) if x.IsZero(connection) { auth.RegisterAuthorizationServer(server, NewLocalCheckService()) } else { pls.LogNow(ctx, log.Fields{"authzd": map[string]string{ "target": connection.CanonicalTarget(), "state": connection.GetState().String(), }}) auth.RegisterAuthorizationServer( server, NewRemoteCheckService( auth.NewAuthorizationClient(connection), ), ) } reflection.Register(server) return &Server{ Server: server, } }