From 6f8cba45f681eb716d839fd83d117267f92f8ed3 Mon Sep 17 00:00:00 2001 From: Marius Kleidl Date: Fri, 20 Nov 2026 08:54:22 +0100 Subject: [PATCH] hooks/grpc: Support RejectTermination in gRPC hook responses The gRPC HookResponse message had no field corresponding to hooks.HookResponse.RejectTermination, so pre-terminate hooks using the gRPC backend could not reject an upload termination, unlike the file, HTTP and plugin backends. Add a rejectTermination field (number 5) to the HookResponse message, regenerate the Go and Python bindings and map the field when converting gRPC responses. The Python example server now shows how to reject a termination. Because the gRPC backend converts between the Go hook types and the Protocol Buffers messages by hand, add generic tests that catch any field which is missing or not mapped: - Field parity between each Go type and its proto message. - Full request and response round trips through a real gRPC server, with fixtures that must set every field. Also cover header forwarding, retries on transient errors and validation of the secure Setup options. --- examples/hooks/grpc/hook_pb2.py | 12 +- examples/hooks/grpc/server.py | 9 + pkg/hooks/grpc/grpc.go | 1 + pkg/hooks/grpc/grpc_test.go | 383 ++++++++++++++++++++++++++++++++ pkg/hooks/grpc/proto/hook.pb.go | 55 +++-- pkg/hooks/grpc/proto/hook.proto | 6 + 6 files changed, 440 insertions(+), 26 deletions(-) create mode 100644 pkg/hooks/grpc/grpc_test.go diff --git a/examples/hooks/grpc/hook_pb2.py b/examples/hooks/grpc/hook_pb2.py index b4a4c04db..2aa44e442 100644 --- a/examples/hooks/grpc/hook_pb2.py +++ b/examples/hooks/grpc/hook_pb2.py @@ -24,7 +24,7 @@ -DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\nhook.proto\x12\x05proto\"8\n\x0bHookRequest\x12\x0c\n\x04type\x18\x01 \x01(\t\x12\x1b\n\x05\x65vent\x18\x02 \x01(\x0b\x32\x0c.proto.Event\"Q\n\x05\x45vent\x12\x1f\n\x06upload\x18\x01 \x01(\x0b\x32\x0f.proto.FileInfo\x12\'\n\x0bhttpRequest\x18\x02 \x01(\x0b\x32\x12.proto.HTTPRequest\"\xc9\x02\n\x08\x46ileInfo\x12\n\n\x02id\x18\x01 \x01(\t\x12\x0c\n\x04size\x18\x02 \x01(\x03\x12\x16\n\x0esizeIsDeferred\x18\x03 \x01(\x08\x12\x0e\n\x06offset\x18\x04 \x01(\x03\x12/\n\x08metaData\x18\x05 \x03(\x0b\x32\x1d.proto.FileInfo.MetaDataEntry\x12\x11\n\tisPartial\x18\x06 \x01(\x08\x12\x0f\n\x07isFinal\x18\x07 \x01(\x08\x12\x16\n\x0epartialUploads\x18\x08 \x03(\t\x12-\n\x07storage\x18\t \x03(\x0b\x32\x1c.proto.FileInfo.StorageEntry\x1a/\n\rMetaDataEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\x1a.\n\x0cStorageEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\"\xec\x01\n\x0f\x46ileInfoChanges\x12\n\n\x02id\x18\x01 \x01(\t\x12\x36\n\x08metaData\x18\x02 \x03(\x0b\x32$.proto.FileInfoChanges.MetaDataEntry\x12\x34\n\x07storage\x18\x03 \x03(\x0b\x32#.proto.FileInfoChanges.StorageEntry\x1a/\n\rMetaDataEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\x1a.\n\x0cStorageEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\"\x9d\x01\n\x0bHTTPRequest\x12\x0e\n\x06method\x18\x01 \x01(\t\x12\x0b\n\x03uri\x18\x02 \x01(\t\x12\x12\n\nremoteAddr\x18\x03 \x01(\t\x12.\n\x06header\x18\x04 \x03(\x0b\x32\x1e.proto.HTTPRequest.HeaderEntry\x1a-\n\x0bHeaderEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\"\x93\x01\n\x0cHookResponse\x12)\n\x0chttpResponse\x18\x01 \x01(\x0b\x32\x13.proto.HTTPResponse\x12\x14\n\x0crejectUpload\x18\x02 \x01(\x08\x12.\n\x0e\x63hangeFileInfo\x18\x04 \x01(\x0b\x32\x16.proto.FileInfoChanges\x12\x12\n\nstopUpload\x18\x03 \x01(\x08\"\x90\x01\n\x0cHTTPResponse\x12\x12\n\nstatusCode\x18\x01 \x01(\x03\x12/\n\x06header\x18\x02 \x03(\x0b\x32\x1f.proto.HTTPResponse.HeaderEntry\x12\x0c\n\x04\x62ody\x18\x03 \x01(\t\x1a-\n\x0bHeaderEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\x32\x46\n\x0bHookHandler\x12\x37\n\nInvokeHook\x12\x12.proto.HookRequest\x1a\x13.proto.HookResponse\"\x00\x42\x12Z\x10hooks/grpc/protob\x06proto3') +DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\nhook.proto\x12\x05proto\"8\n\x0bHookRequest\x12\x0c\n\x04type\x18\x01 \x01(\t\x12\x1b\n\x05\x65vent\x18\x02 \x01(\x0b\x32\x0c.proto.Event\"Q\n\x05\x45vent\x12\x1f\n\x06upload\x18\x01 \x01(\x0b\x32\x0f.proto.FileInfo\x12\'\n\x0bhttpRequest\x18\x02 \x01(\x0b\x32\x12.proto.HTTPRequest\"\xc9\x02\n\x08\x46ileInfo\x12\n\n\x02id\x18\x01 \x01(\t\x12\x0c\n\x04size\x18\x02 \x01(\x03\x12\x16\n\x0esizeIsDeferred\x18\x03 \x01(\x08\x12\x0e\n\x06offset\x18\x04 \x01(\x03\x12/\n\x08metaData\x18\x05 \x03(\x0b\x32\x1d.proto.FileInfo.MetaDataEntry\x12\x11\n\tisPartial\x18\x06 \x01(\x08\x12\x0f\n\x07isFinal\x18\x07 \x01(\x08\x12\x16\n\x0epartialUploads\x18\x08 \x03(\t\x12-\n\x07storage\x18\t \x03(\x0b\x32\x1c.proto.FileInfo.StorageEntry\x1a/\n\rMetaDataEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\x1a.\n\x0cStorageEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\"\xec\x01\n\x0f\x46ileInfoChanges\x12\n\n\x02id\x18\x01 \x01(\t\x12\x36\n\x08metaData\x18\x02 \x03(\x0b\x32$.proto.FileInfoChanges.MetaDataEntry\x12\x34\n\x07storage\x18\x03 \x03(\x0b\x32#.proto.FileInfoChanges.StorageEntry\x1a/\n\rMetaDataEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\x1a.\n\x0cStorageEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\"\x9d\x01\n\x0bHTTPRequest\x12\x0e\n\x06method\x18\x01 \x01(\t\x12\x0b\n\x03uri\x18\x02 \x01(\t\x12\x12\n\nremoteAddr\x18\x03 \x01(\t\x12.\n\x06header\x18\x04 \x03(\x0b\x32\x1e.proto.HTTPRequest.HeaderEntry\x1a-\n\x0bHeaderEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\"\xae\x01\n\x0cHookResponse\x12)\n\x0chttpResponse\x18\x01 \x01(\x0b\x32\x13.proto.HTTPResponse\x12\x14\n\x0crejectUpload\x18\x02 \x01(\x08\x12.\n\x0e\x63hangeFileInfo\x18\x04 \x01(\x0b\x32\x16.proto.FileInfoChanges\x12\x12\n\nstopUpload\x18\x03 \x01(\x08\x12\x19\n\x11rejectTermination\x18\x05 \x01(\x08\"\x90\x01\n\x0cHTTPResponse\x12\x12\n\nstatusCode\x18\x01 \x01(\x03\x12/\n\x06header\x18\x02 \x03(\x0b\x32\x1f.proto.HTTPResponse.HeaderEntry\x12\x0c\n\x04\x62ody\x18\x03 \x01(\t\x1a-\n\x0bHeaderEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\x32\x46\n\x0bHookHandler\x12\x37\n\nInvokeHook\x12\x12.proto.HookRequest\x1a\x13.proto.HookResponse\"\x00\x42\x12Z\x10hooks/grpc/protob\x06proto3') _globals = globals() _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) @@ -65,11 +65,11 @@ _globals['_HTTPREQUEST_HEADERENTRY']._serialized_start=846 _globals['_HTTPREQUEST_HEADERENTRY']._serialized_end=891 _globals['_HOOKRESPONSE']._serialized_start=894 - _globals['_HOOKRESPONSE']._serialized_end=1041 - _globals['_HTTPRESPONSE']._serialized_start=1044 - _globals['_HTTPRESPONSE']._serialized_end=1188 + _globals['_HOOKRESPONSE']._serialized_end=1068 + _globals['_HTTPRESPONSE']._serialized_start=1071 + _globals['_HTTPRESPONSE']._serialized_end=1215 _globals['_HTTPRESPONSE_HEADERENTRY']._serialized_start=846 _globals['_HTTPRESPONSE_HEADERENTRY']._serialized_end=891 - _globals['_HOOKHANDLER']._serialized_start=1190 - _globals['_HOOKHANDLER']._serialized_end=1260 + _globals['_HOOKHANDLER']._serialized_start=1217 + _globals['_HOOKHANDLER']._serialized_end=1287 # @@protoc_insertion_point(module_scope) diff --git a/examples/hooks/grpc/server.py b/examples/hooks/grpc/server.py index 15c8b3a4c..a335b0aeb 100644 --- a/examples/hooks/grpc/server.py +++ b/examples/hooks/grpc/server.py @@ -37,6 +37,15 @@ def InvokeHook(self, hook_request, context): hook_response.changeFileInfo.metaData['filename'] = metaData['filename'] hook_response.changeFileInfo.metaData['creation_time'] = time.ctime() + # Example: Use the pre-terminate hook to prevent completed uploads from being + # deleted. The termination is rejected with a custom HTTP response. + if hook_request.type == 'pre-terminate': + upload = hook_request.event.upload + if not upload.sizeIsDeferred and upload.offset == upload.size: + hook_response.rejectTermination = True + hook_response.httpResponse.statusCode = 403 + hook_response.httpResponse.body = 'completed uploads cannot be deleted' + # Example: Use the post-finish hook to print information about a completed upload, # including its storage location. if hook_request.type == 'post-finish': diff --git a/pkg/hooks/grpc/grpc.go b/pkg/hooks/grpc/grpc.go index 5d7b134df..f69b211c7 100644 --- a/pkg/hooks/grpc/grpc.go +++ b/pkg/hooks/grpc/grpc.go @@ -147,6 +147,7 @@ func getHeader(httpHeader http.Header) (hookHeader map[string]string) { func unmarshal(res *pb.HookResponse) (hookRes hooks.HookResponse) { hookRes.RejectUpload = res.RejectUpload hookRes.StopUpload = res.StopUpload + hookRes.RejectTermination = res.RejectTermination httpRes := res.HttpResponse if httpRes != nil { diff --git a/pkg/hooks/grpc/grpc_test.go b/pkg/hooks/grpc/grpc_test.go new file mode 100644 index 000000000..22e01d005 --- /dev/null +++ b/pkg/hooks/grpc/grpc_test.go @@ -0,0 +1,383 @@ +package grpc + +import ( + "context" + "net" + "net/http" + "reflect" + "slices" + "strings" + "sync" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/tus/tusd/v2/pkg/handler" + "github.com/tus/tusd/v2/pkg/hooks" + pb "github.com/tus/tusd/v2/pkg/hooks/grpc/proto" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/metadata" + "google.golang.org/grpc/status" + "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/reflect/protoreflect" +) + +// testHookServer is a gRPC hook server whose behavior is controlled by a function. +// It records all requests and their incoming metadata. +type testHookServer struct { + pb.UnimplementedHookHandlerServer + + handle func(call int, req *pb.HookRequest) (*pb.HookResponse, error) + + mu sync.Mutex + requests []*pb.HookRequest + metadata []metadata.MD +} + +func (s *testHookServer) InvokeHook(ctx context.Context, req *pb.HookRequest) (*pb.HookResponse, error) { + md, _ := metadata.FromIncomingContext(ctx) + + s.mu.Lock() + s.requests = append(s.requests, req) + s.metadata = append(s.metadata, md) + call := len(s.requests) + s.mu.Unlock() + + return s.handle(call, req) +} + +func (s *testHookServer) calls() int { + s.mu.Lock() + defer s.mu.Unlock() + return len(s.requests) +} + +// startTestHookServer starts a gRPC hook server on a random local port and returns +// a set up GrpcHook that talks to it. configure can adjust the GrpcHook before Setup. +func startTestHookServer(t *testing.T, srv *testHookServer, configure func(*GrpcHook)) *GrpcHook { + t.Helper() + + lis, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + + server := grpc.NewServer() + pb.RegisterHookHandlerServer(server, srv) + go server.Serve(lis) + t.Cleanup(server.Stop) + + hook := &GrpcHook{ + Endpoint: lis.Addr().String(), + } + if configure != nil { + configure(hook) + } + require.NoError(t, hook.Setup()) + return hook +} + +// TestFieldParity ensures that the Go hook types and the Protocol Buffers messages +// have the same fields. The gRPC backend converts between them by hand, so a field +// that is added to one side but not the other would otherwise be silently dropped. +func TestFieldParity(t *testing.T) { + tests := []struct { + goType reflect.Type + message proto.Message + // ignored lists Go fields which are intentionally not transmitted. + ignored []string + }{ + {reflect.TypeFor[hooks.HookRequest](), &pb.HookRequest{}, nil}, + {reflect.TypeFor[handler.HookEvent](), &pb.Event{}, []string{"Context"}}, + {reflect.TypeFor[handler.FileInfo](), &pb.FileInfo{}, nil}, + {reflect.TypeFor[handler.HTTPRequest](), &pb.HTTPRequest{}, nil}, + {reflect.TypeFor[hooks.HookResponse](), &pb.HookResponse{}, nil}, + {reflect.TypeFor[handler.HTTPResponse](), &pb.HTTPResponse{}, nil}, + {reflect.TypeFor[handler.FileInfoChanges](), &pb.FileInfoChanges{}, nil}, + } + + for _, test := range tests { + desc := test.message.ProtoReflect().Descriptor() + t.Run(test.goType.String(), func(t *testing.T) { + protoFields := map[string]bool{} + fields := desc.Fields() + for i := 0; i < fields.Len(); i++ { + protoFields[strings.ToLower(string(fields.Get(i).Name()))] = true + } + + goFields := map[string]bool{} + for field := range test.goType.Fields() { + if !field.IsExported() || slices.Contains(test.ignored, field.Name) { + continue + } + name := strings.ToLower(field.Name) + goFields[name] = true + assert.True(t, protoFields[name], "Go field %s.%s has no counterpart in proto message %s", test.goType, field.Name, desc.FullName()) + } + + for name := range protoFields { + assert.True(t, goFields[name], "proto field %s.%s has no counterpart in Go type %s", desc.FullName(), name, test.goType) + } + }) + } +} + +// TestResponseRoundTrip sends a hook response with every field set from a gRPC server +// and checks that all values arrive in the hooks.HookResponse. +func TestResponseRoundTrip(t *testing.T) { + a := assert.New(t) + + response := &pb.HookResponse{ + HttpResponse: &pb.HTTPResponse{ + StatusCode: 418, + Header: map[string]string{"X-Hello": "world"}, + Body: "body", + }, + RejectUpload: true, + ChangeFileInfo: &pb.FileInfoChanges{ + Id: "new-id", + MetaData: map[string]string{"filename": "new.txt"}, + Storage: map[string]string{"Path": "/somewhere"}, + }, + StopUpload: true, + RejectTermination: true, + } + assertProtoFullyPopulated(t, response.ProtoReflect()) + + expected := hooks.HookResponse{ + HTTPResponse: handler.HTTPResponse{ + StatusCode: 418, + Header: handler.HTTPHeader{"X-Hello": "world"}, + Body: "body", + }, + RejectUpload: true, + ChangeFileInfo: handler.FileInfoChanges{ + ID: "new-id", + MetaData: handler.MetaData{"filename": "new.txt"}, + Storage: map[string]string{"Path": "/somewhere"}, + }, + StopUpload: true, + RejectTermination: true, + } + assertGoFullyPopulated(t, reflect.ValueOf(expected), "HookResponse") + + srv := &testHookServer{ + handle: func(int, *pb.HookRequest) (*pb.HookResponse, error) { + return response, nil + }, + } + hook := startTestHookServer(t, srv, nil) + + res, err := hook.InvokeHook(hooks.HookRequest{Type: hooks.HookPreCreate}) + a.NoError(err) + a.Equal(expected, res) +} + +// TestRequestRoundTrip sends a hook request with every field set to a gRPC server +// and checks that all values arrive in the pb.HookRequest. +func TestRequestRoundTrip(t *testing.T) { + a := assert.New(t) + + request := hooks.HookRequest{ + Type: hooks.HookPostFinish, + Event: handler.HookEvent{ + Context: context.Background(), + Upload: handler.FileInfo{ + ID: "id", + Size: 100, + SizeIsDeferred: true, + Offset: 42, + MetaData: handler.MetaData{"filename": "a.txt"}, + IsPartial: true, + IsFinal: true, + PartialUploads: []string{"a", "b"}, + Storage: map[string]string{"Type": "filestore"}, + }, + HTTPRequest: handler.HTTPRequest{ + Method: "PATCH", + URI: "/files/id", + RemoteAddr: "127.0.0.1:1234", + Header: http.Header{ + "X-Single": {"one"}, + // Only the first value of a header is transmitted. + "X-Multi": {"first", "second"}, + // Headers without values are dropped. + "X-Empty": {}, + }, + }, + }, + } + assertGoFullyPopulated(t, reflect.ValueOf(request), "HookRequest") + + expected := &pb.HookRequest{ + Type: "post-finish", + Event: &pb.Event{ + Upload: &pb.FileInfo{ + Id: "id", + Size: 100, + SizeIsDeferred: true, + Offset: 42, + MetaData: map[string]string{"filename": "a.txt"}, + IsPartial: true, + IsFinal: true, + PartialUploads: []string{"a", "b"}, + Storage: map[string]string{"Type": "filestore"}, + }, + HttpRequest: &pb.HTTPRequest{ + Method: "PATCH", + Uri: "/files/id", + RemoteAddr: "127.0.0.1:1234", + Header: map[string]string{ + "X-Single": "one", + "X-Multi": "first", + }, + }, + }, + } + assertProtoFullyPopulated(t, expected.ProtoReflect()) + + srv := &testHookServer{ + handle: func(int, *pb.HookRequest) (*pb.HookResponse, error) { + return &pb.HookResponse{}, nil + }, + } + hook := startTestHookServer(t, srv, nil) + + _, err := hook.InvokeHook(request) + a.NoError(err) + if a.Len(srv.requests, 1) { + a.True(proto.Equal(expected, srv.requests[0]), "unexpected request:\n%v", srv.requests[0]) + } +} + +func TestForwardHeaders(t *testing.T) { + a := assert.New(t) + + srv := &testHookServer{ + handle: func(int, *pb.HookRequest) (*pb.HookResponse, error) { + return &pb.HookResponse{}, nil + }, + } + hook := startTestHookServer(t, srv, func(g *GrpcHook) { + g.ForwardHeaders = []string{"Authorization", "X-Missing"} + }) + + _, err := hook.InvokeHook(hooks.HookRequest{ + Type: hooks.HookPreCreate, + Event: handler.HookEvent{ + HTTPRequest: handler.HTTPRequest{ + Header: http.Header{ + "Authorization": {"Bearer token"}, + "X-Other": {"not forwarded"}, + }, + }, + }, + }) + a.NoError(err) + + if a.Len(srv.metadata, 1) { + md := srv.metadata[0] + a.Equal([]string{"Bearer token"}, md.Get("Authorization")) + a.Empty(md.Get("X-Missing")) + a.Empty(md.Get("X-Other")) + } +} + +func TestRetries(t *testing.T) { + setup := func(t *testing.T, handle func(call int, req *pb.HookRequest) (*pb.HookResponse, error)) (*GrpcHook, *testHookServer) { + srv := &testHookServer{handle: handle} + hook := startTestHookServer(t, srv, func(g *GrpcHook) { + // MaxRetries is the total number of attempts. + g.MaxRetries = 3 + g.Backoff = time.Millisecond + }) + return hook, srv + } + + t.Run("succeeds after transient errors", func(t *testing.T) { + hook, srv := setup(t, func(call int, _ *pb.HookRequest) (*pb.HookResponse, error) { + if call < 3 { + return nil, status.Error(codes.Unavailable, "try again") + } + return &pb.HookResponse{RejectUpload: true}, nil + }) + + res, err := hook.InvokeHook(hooks.HookRequest{Type: hooks.HookPreCreate}) + assert.NoError(t, err) + assert.True(t, res.RejectUpload) + assert.Equal(t, 3, srv.calls()) + }) + + t.Run("fails after all attempts", func(t *testing.T) { + hook, srv := setup(t, func(int, *pb.HookRequest) (*pb.HookResponse, error) { + return nil, status.Error(codes.Unavailable, "down") + }) + + _, err := hook.InvokeHook(hooks.HookRequest{Type: hooks.HookPreCreate}) + assert.Equal(t, codes.Unavailable, status.Code(err)) + assert.Equal(t, 3, srv.calls()) + }) + + t.Run("does not retry non-retriable errors", func(t *testing.T) { + hook, srv := setup(t, func(int, *pb.HookRequest) (*pb.HookResponse, error) { + return nil, status.Error(codes.Internal, "broken") + }) + + _, err := hook.InvokeHook(hooks.HookRequest{Type: hooks.HookPreCreate}) + assert.Equal(t, codes.Internal, status.Code(err)) + assert.Equal(t, 1, srv.calls()) + }) +} + +func TestSetupSecureValidation(t *testing.T) { + t.Run("missing server certificate", func(t *testing.T) { + hook := &GrpcHook{Endpoint: "127.0.0.1:0", Secure: true} + assert.ErrorContains(t, hook.Setup(), "no gRPC server TLS certificate file was provided") + }) + + t.Run("unreadable server certificate", func(t *testing.T) { + hook := &GrpcHook{ + Endpoint: "127.0.0.1:0", + Secure: true, + ServerTLSCertificateFilePath: t.TempDir() + "/missing.pem", + } + assert.Error(t, hook.Setup()) + }) +} + +// assertGoFullyPopulated fails if any exported field in v (recursively through structs) +// has its zero value. This forces test fixtures to be extended when new fields are added. +func assertGoFullyPopulated(t *testing.T, v reflect.Value, path string) { + t.Helper() + + if v.Kind() != reflect.Struct { + assert.False(t, v.IsZero(), "fixture field %s is not set", path) + return + } + + for i := 0; i < v.NumField(); i++ { + field := v.Type().Field(i) + if !field.IsExported() { + continue + } + assertGoFullyPopulated(t, v.Field(i), path+"."+field.Name) + } +} + +// assertProtoFullyPopulated fails if any field in msg (recursively through messages) +// is not set. This forces test fixtures to be extended when new fields are added. +func assertProtoFullyPopulated(t *testing.T, msg protoreflect.Message) { + t.Helper() + + fields := msg.Descriptor().Fields() + for i := 0; i < fields.Len(); i++ { + fd := fields.Get(i) + if !msg.Has(fd) { + assert.Fail(t, "fixture field is not set", "%s", fd.FullName()) + continue + } + if fd.Kind() == protoreflect.MessageKind && !fd.IsList() && !fd.IsMap() { + assertProtoFullyPopulated(t, msg.Get(fd).Message()) + } + } +} diff --git a/pkg/hooks/grpc/proto/hook.pb.go b/pkg/hooks/grpc/proto/hook.pb.go index d130d6ac9..769921778 100644 --- a/pkg/hooks/grpc/proto/hook.pb.go +++ b/pkg/hooks/grpc/proto/hook.pb.go @@ -463,6 +463,11 @@ type HookResponse struct { // it is ignored. Use the HTTPResponse field to send details about the stop // to the client. StopUpload bool `protobuf:"varint,3,opt,name=stopUpload,proto3" json:"stopUpload,omitempty"` + // RejectTermination will cause the termination of the upload to be rejected, keeping the upload. + // This value is only respected for pre-terminate hooks. For other hooks, + // it is ignored. Use the HTTPResponse field to send details about the rejection + // to the client. + RejectTermination bool `protobuf:"varint,5,opt,name=rejectTermination,proto3" json:"rejectTermination,omitempty"` } func (x *HookResponse) Reset() { @@ -525,6 +530,13 @@ func (x *HookResponse) GetStopUpload() bool { return false } +func (x *HookResponse) GetRejectTermination() bool { + if x != nil { + return x.RejectTermination + } + return false +} + // HTTPResponse contains basic details of an outgoing HTTP response. type HTTPResponse struct { state protoimpl.MessageState @@ -667,7 +679,7 @@ var file_pkg_hooks_grpc_proto_hook_proto_rawDesc = []byte{ 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, - 0xcb, 0x01, 0x0a, 0x0c, 0x48, 0x6f, 0x6f, 0x6b, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, + 0xf9, 0x01, 0x0a, 0x0c, 0x48, 0x6f, 0x6f, 0x6b, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x37, 0x0a, 0x0c, 0x68, 0x74, 0x74, 0x70, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x13, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x48, 0x54, 0x54, 0x50, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x52, 0x0c, 0x68, 0x74, 0x74, @@ -679,25 +691,28 @@ var file_pkg_hooks_grpc_proto_hook_proto_rawDesc = []byte{ 0x6c, 0x65, 0x49, 0x6e, 0x66, 0x6f, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x52, 0x0e, 0x63, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x46, 0x69, 0x6c, 0x65, 0x49, 0x6e, 0x66, 0x6f, 0x12, 0x1e, 0x0a, 0x0a, 0x73, 0x74, 0x6f, 0x70, 0x55, 0x70, 0x6c, 0x6f, 0x61, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, - 0x08, 0x52, 0x0a, 0x73, 0x74, 0x6f, 0x70, 0x55, 0x70, 0x6c, 0x6f, 0x61, 0x64, 0x22, 0xb6, 0x01, - 0x0a, 0x0c, 0x48, 0x54, 0x54, 0x50, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1e, - 0x0a, 0x0a, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x43, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x03, 0x52, 0x0a, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x37, - 0x0a, 0x06, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1f, - 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x48, 0x54, 0x54, 0x50, 0x52, 0x65, 0x73, 0x70, 0x6f, - 0x6e, 0x73, 0x65, 0x2e, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, - 0x06, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x12, 0x12, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, - 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x1a, 0x39, 0x0a, 0x0b, 0x48, - 0x65, 0x61, 0x64, 0x65, 0x72, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, - 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, - 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, - 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x32, 0x46, 0x0a, 0x0b, 0x48, 0x6f, 0x6f, 0x6b, 0x48, 0x61, - 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x12, 0x37, 0x0a, 0x0a, 0x49, 0x6e, 0x76, 0x6f, 0x6b, 0x65, 0x48, - 0x6f, 0x6f, 0x6b, 0x12, 0x12, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x48, 0x6f, 0x6f, 0x6b, - 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, - 0x48, 0x6f, 0x6f, 0x6b, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x12, - 0x5a, 0x10, 0x68, 0x6f, 0x6f, 0x6b, 0x73, 0x2f, 0x67, 0x72, 0x70, 0x63, 0x2f, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x08, 0x52, 0x0a, 0x73, 0x74, 0x6f, 0x70, 0x55, 0x70, 0x6c, 0x6f, 0x61, 0x64, 0x12, 0x2c, 0x0a, + 0x11, 0x72, 0x65, 0x6a, 0x65, 0x63, 0x74, 0x54, 0x65, 0x72, 0x6d, 0x69, 0x6e, 0x61, 0x74, 0x69, + 0x6f, 0x6e, 0x18, 0x05, 0x20, 0x01, 0x28, 0x08, 0x52, 0x11, 0x72, 0x65, 0x6a, 0x65, 0x63, 0x74, + 0x54, 0x65, 0x72, 0x6d, 0x69, 0x6e, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0xb6, 0x01, 0x0a, 0x0c, + 0x48, 0x54, 0x54, 0x50, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1e, 0x0a, 0x0a, + 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x43, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, + 0x52, 0x0a, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x37, 0x0a, 0x06, + 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1f, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x48, 0x54, 0x54, 0x50, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x2e, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x06, 0x68, + 0x65, 0x61, 0x64, 0x65, 0x72, 0x12, 0x12, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x03, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x1a, 0x39, 0x0a, 0x0b, 0x48, 0x65, 0x61, + 0x64, 0x65, 0x72, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, + 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, + 0x3a, 0x02, 0x38, 0x01, 0x32, 0x46, 0x0a, 0x0b, 0x48, 0x6f, 0x6f, 0x6b, 0x48, 0x61, 0x6e, 0x64, + 0x6c, 0x65, 0x72, 0x12, 0x37, 0x0a, 0x0a, 0x49, 0x6e, 0x76, 0x6f, 0x6b, 0x65, 0x48, 0x6f, 0x6f, + 0x6b, 0x12, 0x12, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x48, 0x6f, 0x6f, 0x6b, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x48, 0x6f, + 0x6f, 0x6b, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x12, 0x5a, 0x10, + 0x68, 0x6f, 0x6f, 0x6b, 0x73, 0x2f, 0x67, 0x72, 0x70, 0x63, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/pkg/hooks/grpc/proto/hook.proto b/pkg/hooks/grpc/proto/hook.proto index b6f4adc47..a809e4d2d 100644 --- a/pkg/hooks/grpc/proto/hook.proto +++ b/pkg/hooks/grpc/proto/hook.proto @@ -130,6 +130,12 @@ message HookResponse { // it is ignored. Use the HTTPResponse field to send details about the stop // to the client. bool stopUpload = 3; + + // RejectTermination will cause the termination of the upload to be rejected, keeping the upload. + // This value is only respected for pre-terminate hooks. For other hooks, + // it is ignored. Use the HTTPResponse field to send details about the rejection + // to the client. + bool rejectTermination = 5; } // HTTPResponse contains basic details of an outgoing HTTP response.