Develop a custom authorization service based on the gRPC protocol
Service Mesh (ASM) supports authentication and authorization configurations and provides extension capabilities that integrate custom HTTP and gRPC authorization services into the mesh authorization process. Develop a custom authorization service that uses the gRPC protocol to determine whether a request is authorized.
Background information
For the architecture of custom authorization services, see Develop a custom HTTP-based authorization service. Unlike a custom authorization service based on the HTTP protocol, a custom authorization service based on the gRPC protocol does not receive an HTTP request. Instead, it receives a gRPC message specific to Envoy. Determine whether to allow the request based on the information that Envoy provides.
Configuration overview
In the ASM console, associate a custom authorization service. After the association, configure an authorization policy to specify the mesh proxies that use the authorization service. For instructions, see Register the gRPC authorization service.
A custom authorization service based on the gRPC protocol has fewer configuration items than a custom authorization service based on the HTTP protocol. It has no header-related configuration. All headers are available directly in the gRPC authorization request.
Develop a gRPC custom authorization service
ASM is compatible with the open-source Istio service mesh. The Istio community provides sample code for a custom authorization service. This code implements custom authorization services for both the HTTP and gRPC protocols. The main logic of the gRPC part described in this topic is in the extAuthzServerV3 struct:
type extAuthzServerV3 struct{}
func (s *extAuthzServerV3) Check(_ context.Context, request *authv3.CheckRequest) (*authv3.CheckResponse, error)
func (s *extAuthzServerV3) allow(request *authv3.CheckRequest) *authv3.CheckResponse
func (s *extAuthzServerV3) deny(request *authv3.CheckRequest) *authv3.CheckResponse
func (s *extAuthzServerV3) logRequest(allow string, request *authv3.CheckRequest)
This struct mainly implements the AuthorizationServer interface, which is defined as follows:
type AuthorizationServer interface {
// Performs authorization check based on the attributes associated with the
// incoming request, and returns status `OK` or not `OK`.
Check(context.Context, *CheckRequest) (*CheckResponse, error)
}
The main logic is in the implementation of the Check function:
// Check implements gRPC v3 check request.
func (s *extAuthzServerV3) Check(_ context.Context, request *authv3.CheckRequest) (*authv3.CheckResponse, error) {
attrs := request.GetAttributes()
// Determine whether to allow or deny the request.
allow := false
checkHeaderValue, contains := attrs.GetRequest().GetHttp().GetHeaders()[checkHeader]
if contains {
allow = checkHeaderValue == allowedValue
} else {
allow = attrs.Source != nil && strings.HasSuffix(attrs.Source.Principal, "/sa/"+*serviceAccount)
}
if allow {
return s.allow(request), nil
}
return s.deny(request), nil
}
This function checks a header of the request. If the value of this header is allowedValue, the function returns the result of the allow function. If the request does not have this header, the function checks whether the value of attrs.Source.Principal is a preconfigured value.
For details about each field in authv3.CheckRequest, see Envoy Proto or the Envoy go-control-plane documentation.
The sample code provided by Istio also contains an extAuthzServerV2 struct. Envoy has deprecated the V2-related APIs, so implement only the V3 version of the gRPC interface described here.
Configure the custom authorization service
After you deploy the custom authorization service developed in the previous step to a Container Service for Kubernetes (ACK) cluster, connect the service. For instructions, see Integrate a gRPC custom authorization service.