@@ -3,8 +3,10 @@ package cli
33import (
44 "context"
55 "fmt"
6+ "log"
67 "log/slog"
78 "os"
9+ "os/exec"
810 "os/signal"
911 "path/filepath"
1012 "strings"
@@ -26,6 +28,9 @@ type Config struct {
2628 LogLevel string
2729 LogDir string
2830 Unprivileged bool
31+ ProxyPort int64
32+ PprofEnabled bool
33+ PprofPort int64
2934}
3035
3136// NewCommand creates and returns the root serpent command
@@ -84,6 +89,26 @@ func BaseCommand() *serpent.Command {
8489 Description : "Run in unprivileged mode (no network isolation, uses proxy environment variables)." ,
8590 Value : serpent .BoolOf (& config .Unprivileged ),
8691 },
92+ {
93+ Flag : "proxy-port" ,
94+ Env : "PROXY_PORT" ,
95+ Description : "Set a port for HTTP proxy." ,
96+ Default : "8080" ,
97+ Value : serpent .Int64Of (& config .ProxyPort ),
98+ },
99+ {
100+ Flag : "pprof" ,
101+ Env : "BOUNDARY_PPROF" ,
102+ Description : "Enable pprof profiling server." ,
103+ Value : serpent .BoolOf (& config .PprofEnabled ),
104+ },
105+ {
106+ Flag : "pprof-port" ,
107+ Env : "BOUNDARY_PPROF_PORT" ,
108+ Description : "Set port for pprof profiling server." ,
109+ Default : "6060" ,
110+ Value : serpent .Int64Of (& config .PprofPort ),
111+ },
87112 },
88113 Handler : func (inv * serpent.Invocation ) error {
89114 args := inv .Args
@@ -92,15 +117,47 @@ func BaseCommand() *serpent.Command {
92117 }
93118}
94119
120+ func isChild () bool {
121+ return os .Getenv ("CHILD" ) == "true"
122+ }
123+
95124// Run executes the boundary command with the given configuration and arguments
96125func Run (ctx context.Context , config Config , args []string ) error {
97- ctx , cancel := context .WithCancel (ctx )
98- defer cancel ()
99-
100126 logger , err := setupLogging (config )
101127 if err != nil {
102128 return fmt .Errorf ("could not set up logging: %v" , err )
103129 }
130+
131+ if isChild () {
132+ logger .Info ("boundary CHILD process is started" )
133+
134+ vethNetJail := os .Getenv ("VETH_JAIL_NAME" )
135+ err := jail .SetupChildNetworking (vethNetJail )
136+ if err != nil {
137+ return fmt .Errorf ("failed to setup child networking: %v" , err )
138+ }
139+ logger .Info ("child networking is successfully configured" )
140+
141+ // Program to run
142+ bin := args [0 ]
143+ args = args [1 :]
144+
145+ cmd := exec .Command (bin , args ... )
146+ cmd .Stdin = os .Stdin
147+ cmd .Stdout = os .Stdout
148+ cmd .Stderr = os .Stderr
149+ err = cmd .Run ()
150+ if err != nil {
151+ log .Printf ("failed to run %s: %v" , bin , err )
152+ return err
153+ }
154+
155+ return nil
156+ }
157+
158+ ctx , cancel := context .WithCancel (ctx )
159+ defer cancel ()
160+
104161 username , uid , gid , homeDir , configDir := util .GetUserInfo ()
105162
106163 // Get command arguments
@@ -147,7 +204,7 @@ func Run(ctx context.Context, config Config, args []string) error {
147204 // Create jailer with cert path from TLS setup
148205 jailer , err := createJailer (jail.Config {
149206 Logger : logger ,
150- HttpProxyPort : 8080 ,
207+ HttpProxyPort : int ( config . ProxyPort ) ,
151208 Username : username ,
152209 Uid : uid ,
153210 Gid : gid ,
@@ -161,11 +218,14 @@ func Run(ctx context.Context, config Config, args []string) error {
161218
162219 // Create boundary instance
163220 boundaryInstance , err := boundary .New (ctx , boundary.Config {
164- RuleEngine : ruleEngine ,
165- Auditor : auditor ,
166- TLSConfig : tlsConfig ,
167- Logger : logger ,
168- Jailer : jailer ,
221+ RuleEngine : ruleEngine ,
222+ Auditor : auditor ,
223+ TLSConfig : tlsConfig ,
224+ Logger : logger ,
225+ Jailer : jailer ,
226+ ProxyPort : int (config .ProxyPort ),
227+ PprofEnabled : config .PprofEnabled ,
228+ PprofPort : int (config .PprofPort ),
169229 })
170230 if err != nil {
171231 return fmt .Errorf ("failed to create boundary instance: %v" , err )
@@ -191,15 +251,26 @@ func Run(ctx context.Context, config Config, args []string) error {
191251 // Execute command in boundary
192252 go func () {
193253 defer cancel ()
194- cmd := boundaryInstance .Command (args )
195- cmd .Stderr = os .Stderr
196- cmd .Stdout = os .Stdout
197- cmd .Stdin = os .Stdin
254+ cmd := boundaryInstance .Command (os .Args )
255+
256+ logger .Debug ("Executing command in boundary" , "command" , strings .Join (os .Args , " " ))
257+ err := cmd .Start ()
258+ if err != nil {
259+ logger .Error ("Command failed to start" , "error" , err )
260+ return
261+ }
262+
263+ err = boundaryInstance .ConfigureAfterCommandExecution (cmd .Process .Pid )
264+ if err != nil {
265+ logger .Error ("configuration after command execution failed" , "error" , err )
266+ return
267+ }
198268
199- logger .Debug ("Executing command in boundary" , "command" , strings . Join ( args , " " ) )
200- err : = cmd .Run ()
269+ logger .Debug ("waiting on a child process to finish" )
270+ err = cmd .Wait ()
201271 if err != nil {
202272 logger .Error ("Command execution failed" , "error" , err )
273+ return
203274 }
204275 }()
205276
0 commit comments