From 29e3f358bd0f1f29a1fcd45d23e438bbb46bb82b Mon Sep 17 00:00:00 2001 From: Copybara Service Date: Thu, 4 Sep 2025 05:27:55 -0700 Subject: [PATCH] Support preconfigured args in stdinservice. PiperOrigin-RevId: 802983375 --- .../fleetspeak_stdinservice/config.pb.go | 28 +++++++++++++------ .../fleetspeak_stdinservice/config.proto | 4 +++ .../fleetspeak_stdinservice/messages.pb.go | 1 + .../fleetspeak_stdinservice/messages.proto | 1 + .../src/client/stdinservice/stdinservice.go | 6 +++- .../client/stdinservice/stdinservice_test.go | 7 +++-- 6 files changed, 35 insertions(+), 12 deletions(-) diff --git a/fleetspeak/src/client/stdinservice/proto/fleetspeak_stdinservice/config.pb.go b/fleetspeak/src/client/stdinservice/proto/fleetspeak_stdinservice/config.pb.go index f20bc0ba..1751a723 100644 --- a/fleetspeak/src/client/stdinservice/proto/fleetspeak_stdinservice/config.pb.go +++ b/fleetspeak/src/client/stdinservice/proto/fleetspeak_stdinservice/config.pb.go @@ -28,6 +28,9 @@ type Config struct { unknownFields protoimpl.UnknownFields Cmd string `protobuf:"bytes,1,opt,name=cmd,proto3" json:"cmd,omitempty"` + // Command line arguments. + // These arguments are prepended to the arguments specified in the message. + Args []string `protobuf:"bytes,2,rep,name=args,proto3" json:"args,omitempty"` } func (x *Config) Reset() { @@ -67,6 +70,13 @@ func (x *Config) GetCmd() string { return "" } +func (x *Config) GetArgs() []string { + if x != nil { + return x.Args + } + return nil +} + var File_fleetspeak_src_client_stdinservice_proto_fleetspeak_stdinservice_config_proto protoreflect.FileDescriptor var file_fleetspeak_src_client_stdinservice_proto_fleetspeak_stdinservice_config_proto_rawDesc = []byte{ @@ -76,15 +86,17 @@ var file_fleetspeak_src_client_stdinservice_proto_fleetspeak_stdinservice_config 0x73, 0x70, 0x65, 0x61, 0x6b, 0x5f, 0x73, 0x74, 0x64, 0x69, 0x6e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x17, 0x66, 0x6c, 0x65, 0x65, 0x74, 0x73, 0x70, 0x65, 0x61, 0x6b, 0x2e, 0x73, 0x74, 0x64, 0x69, - 0x6e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x22, 0x1a, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, + 0x6e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x22, 0x2e, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x10, 0x0a, 0x03, 0x63, 0x6d, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, - 0x03, 0x63, 0x6d, 0x64, 0x42, 0x5f, 0x5a, 0x5d, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, - 0x6f, 0x6d, 0x2f, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x66, 0x6c, 0x65, 0x65, 0x74, 0x73, - 0x70, 0x65, 0x61, 0x6b, 0x2f, 0x66, 0x6c, 0x65, 0x65, 0x74, 0x73, 0x70, 0x65, 0x61, 0x6b, 0x2f, - 0x73, 0x72, 0x63, 0x2f, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x2f, 0x73, 0x74, 0x64, 0x69, 0x6e, - 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x66, 0x6c, - 0x65, 0x65, 0x74, 0x73, 0x70, 0x65, 0x61, 0x6b, 0x5f, 0x73, 0x74, 0x64, 0x69, 0x6e, 0x73, 0x65, - 0x72, 0x76, 0x69, 0x63, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x03, 0x63, 0x6d, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x61, 0x72, 0x67, 0x73, 0x18, 0x02, 0x20, 0x03, + 0x28, 0x09, 0x52, 0x04, 0x61, 0x72, 0x67, 0x73, 0x42, 0x5f, 0x5a, 0x5d, 0x67, 0x69, 0x74, 0x68, + 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x66, 0x6c, + 0x65, 0x65, 0x74, 0x73, 0x70, 0x65, 0x61, 0x6b, 0x2f, 0x66, 0x6c, 0x65, 0x65, 0x74, 0x73, 0x70, + 0x65, 0x61, 0x6b, 0x2f, 0x73, 0x72, 0x63, 0x2f, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x2f, 0x73, + 0x74, 0x64, 0x69, 0x6e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x2f, 0x66, 0x6c, 0x65, 0x65, 0x74, 0x73, 0x70, 0x65, 0x61, 0x6b, 0x5f, 0x73, 0x74, 0x64, + 0x69, 0x6e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x33, } var ( diff --git a/fleetspeak/src/client/stdinservice/proto/fleetspeak_stdinservice/config.proto b/fleetspeak/src/client/stdinservice/proto/fleetspeak_stdinservice/config.proto index b7e7d195..065a6f0e 100644 --- a/fleetspeak/src/client/stdinservice/proto/fleetspeak_stdinservice/config.proto +++ b/fleetspeak/src/client/stdinservice/proto/fleetspeak_stdinservice/config.proto @@ -8,4 +8,8 @@ option go_package = "github.com/google/fleetspeak/fleetspeak/src/client/stdinser // in ClientServiceConfig.config. message Config { string cmd = 1; + + // Command line arguments. + // These arguments are prepended to the arguments specified in the message. + repeated string args = 2; } diff --git a/fleetspeak/src/client/stdinservice/proto/fleetspeak_stdinservice/messages.pb.go b/fleetspeak/src/client/stdinservice/proto/fleetspeak_stdinservice/messages.pb.go index 24644fd3..93f64620 100644 --- a/fleetspeak/src/client/stdinservice/proto/fleetspeak_stdinservice/messages.pb.go +++ b/fleetspeak/src/client/stdinservice/proto/fleetspeak_stdinservice/messages.pb.go @@ -29,6 +29,7 @@ type InputMessage struct { // The data to be forwarded to the service. Input []byte `protobuf:"bytes,1,opt,name=input,proto3" json:"input,omitempty"` // Command line arguments. + // These arguments are appended to the arguments specified in the config. Args []string `protobuf:"bytes,2,rep,name=args,proto3" json:"args,omitempty"` } diff --git a/fleetspeak/src/client/stdinservice/proto/fleetspeak_stdinservice/messages.proto b/fleetspeak/src/client/stdinservice/proto/fleetspeak_stdinservice/messages.proto index 2658865c..66f8d8a1 100644 --- a/fleetspeak/src/client/stdinservice/proto/fleetspeak_stdinservice/messages.proto +++ b/fleetspeak/src/client/stdinservice/proto/fleetspeak_stdinservice/messages.proto @@ -11,6 +11,7 @@ message InputMessage { bytes input = 1; // Command line arguments. + // These arguments are appended to the arguments specified in the config. repeated string args = 2; } diff --git a/fleetspeak/src/client/stdinservice/stdinservice.go b/fleetspeak/src/client/stdinservice/stdinservice.go index 3a06d143..7d5e5adc 100644 --- a/fleetspeak/src/client/stdinservice/stdinservice.go +++ b/fleetspeak/src/client/stdinservice/stdinservice.go @@ -81,7 +81,11 @@ func (s *StdinService) ProcessMessage(ctx context.Context, m *fspb.Message) erro var stdout, stderr bytes.Buffer - cmd := exec.CommandContext(ctx, s.ssConf.Cmd, im.Args...) + var args []string + args = append(args, s.ssConf.Args...) + args = append(args, im.Args...) + + cmd := exec.CommandContext(ctx, s.ssConf.Cmd, args...) cmd.Stdin = bytes.NewBuffer(im.Input) cmd.Stdout = &stdout cmd.Stderr = &stderr diff --git a/fleetspeak/src/client/stdinservice/stdinservice_test.go b/fleetspeak/src/client/stdinservice/stdinservice_test.go index db41f60a..911e7b81 100644 --- a/fleetspeak/src/client/stdinservice/stdinservice_test.go +++ b/fleetspeak/src/client/stdinservice/stdinservice_test.go @@ -32,7 +32,8 @@ func TestStdinServiceWithEcho(t *testing.T) { s, err := Factory(&fspb.ClientServiceConfig{ Name: "EchoService", Config: anypbtest.New(t, &sspb.Config{ - Cmd: "python", + Cmd: "python", + Args: []string{"-c", `import sys; sys.stdout.write(" ".join(sys.argv[1:]))`}, }), }) if err != nil { @@ -51,7 +52,7 @@ func TestStdinServiceWithEcho(t *testing.T) { &fspb.Message{ MessageType: "StdinServiceInputMessage", Data: anypbtest.New(t, &sspb.InputMessage{ - Args: []string{"-c", `import sys; sys.stdout.write("foo bar")`}, + Args: []string{"a", "b"}, }), }) if err != nil { @@ -70,7 +71,7 @@ func TestStdinServiceWithEcho(t *testing.T) { t.Fatal(err) } - wantStdout := []byte("foo bar") + wantStdout := []byte("a b") if !bytes.Equal(om.Stdout, wantStdout) { t.Fatalf("unexpected output; got %q, want %q", om.Stdout, wantStdout) }