m/n/core: factor out gRPC/TLS into rpc and identity libraries
This is an annoying large change, which started its life as me pulling
the 'let's add tests for authentication' thread, and ended up in
unifying a whole bunch of dispersed logic under two new libraries.
Notable changes:
- m/n/core/identity now contains the NodeCertificate (now called Node)
and NodeCredentials types. These used to exist in the cluster code,
but were factored out to prevent loops between the curator, the
cluster enrolment logic, and other code. They can now be shared by
nearly all of the node code, removing the need for some conversions
between subsystems/packages.
- Alongside Node{,Credentials} types, the identity package contains
code that creates x509 certificate templates and verifies x509
certificates, and has functions specific to nodes and users - not
clients and servers. This allows moving most of the rest of
certificate checking code into a single set of functions, and allows
us to test this logic thoroughly.
- pki.{Client,Server,CA} are not used by the node core code anymore,
and can now be moved to kubernetes-specific code (as that was their
original purpose and that's their only current use).
- m/n/core/rpc has been refactored to deduplicate code between the
local/external gRPC servers and unary/stream interceptors for these
servers, also allowing for more thorough testing and unified
behaviour between all.
- A PeerInfo structure is now injected into all gRPC handlers, and is
unified to contain information both about nodes, users, and possibly
unauthenticated callers.
- The AAA.Escrow implementation now makes use of PeerInfo in order to
retrieve the client's certificate, instead of rolling its own logic.
- The EphemeralClusterCredentials test helper has been moved to the rpc
library, and now returns identity objects, allowing for simplified
test code (less juggling of bare public keys and
{x509,tls}.Certificate objects).
Change-Id: I9284966b4f18c0d7628167ca3168b4b4037808c1
Reviewed-on: https://review.monogon.dev/c/monogon/+/325
Reviewed-by: Lorenz Brun <lorenz@monogon.tech>
diff --git a/metropolis/node/core/rpc/methodinfo.go b/metropolis/node/core/rpc/methodinfo.go
new file mode 100644
index 0000000..0a597fa
--- /dev/null
+++ b/metropolis/node/core/rpc/methodinfo.go
@@ -0,0 +1,86 @@
+package rpc
+
+import (
+ "fmt"
+ "regexp"
+
+ "google.golang.org/grpc/codes"
+ "google.golang.org/grpc/status"
+ "google.golang.org/protobuf/proto"
+ "google.golang.org/protobuf/reflect/protoreflect"
+ "google.golang.org/protobuf/reflect/protoregistry"
+
+ epb "source.monogon.dev/metropolis/proto/ext"
+)
+
+// methodInfo is the parsed information for a given RPC method, as configured by
+// the metropolis.common.ext.authorization extension.
+type methodInfo struct {
+ // unauthenticated is true if the method is defined as 'unauthenticated', ie.
+ // that all requests should be passed to the gRPC handler without any
+ // authentication or authorization performed.
+ unauthenticated bool
+ // need is a map of permissions that the caller needs to have in order to be
+ // allowed to call this method. If not empty, unauthenticated cannot be set to
+ // true.
+ need map[epb.Permission]bool
+}
+
+var (
+ // reMethodName matches a /some.service/Method string from
+ // {Stream,Unary}ServerInfo.FullMethod.
+ reMethodName = regexp.MustCompile(`^/([^/]+)/([^/.]+)$`)
+)
+
+// getMethodInfo returns the methodInfo for a given method name, as retrieved
+// from grpc.{Stream,Unary}ServerInfo.FullMethod, or nil if the method could not
+// be found.
+//
+// SECURITY: If the given method does not have any
+// metropolis.common.ext.authorization annotations, a methodInfo which requires
+// authorization but no permissions is returned, defaulting to a mildly secure
+// default of a method that can be called by any authenticated user.
+func getMethodInfo(methodName string) (*methodInfo, error) {
+ m := reMethodName.FindStringSubmatch(methodName)
+ if len(m) != 3 {
+ return nil, status.Errorf(codes.InvalidArgument, "invalid method name %q", methodName)
+ }
+ // Convert /foo.bar/Method to foo.bar.Method, which is used by the protoregistry.
+ methodName = fmt.Sprintf("%s.%s", m[1], m[2])
+ desc, err := protoregistry.GlobalFiles.FindDescriptorByName(protoreflect.FullName(methodName))
+ if err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, "could not retrieve descriptor for method: %v", err)
+ }
+ method, ok := desc.(protoreflect.MethodDescriptor)
+ if !ok {
+ return nil, status.Error(codes.InvalidArgument, "querying method name did not yield a MethodDescriptor")
+ }
+
+ // Get authorization extension, defaults to no options set.
+ if !proto.HasExtension(method.Options(), epb.E_Authorization) {
+ return nil, status.Errorf(codes.Internal, "method does not provide Authorization extension, failing safe")
+ }
+ authz, ok := proto.GetExtension(method.Options(), epb.E_Authorization).(*epb.Authorization)
+ if !ok {
+ return nil, status.Errorf(codes.Internal, "method contains Authorization extension with wrong type, failing safe")
+ }
+ if authz == nil {
+ return nil, status.Errorf(codes.Internal, "method contains nil Authorization extension, failing safe")
+ }
+
+ // If unauthenticated connections are allowed, return immediately.
+ if authz.AllowUnauthenticated && len(authz.Need) == 0 {
+ return &methodInfo{
+ unauthenticated: true,
+ }, nil
+ }
+
+ // Otherwise, return needed permissions.
+ res := &methodInfo{
+ need: make(map[epb.Permission]bool),
+ }
+ for _, n := range authz.Need {
+ res.need[n] = true
+ }
+ return res, nil
+}