@@ -2,11 +2,14 @@ package config
22
33import (
44 "context"
5+ "encoding/base64"
56 "encoding/json"
67 "fmt"
78 "log"
89 "strings"
910 "time"
11+
12+ "github.com/gluk-w/claworc/control-plane/internal/logutil"
1013)
1114
1215// InstanceOps defines the generic primitives needed to configure an instance.
@@ -34,7 +37,7 @@ func ConfigureInstance(ctx context.Context, ops InstanceOps, name string, models
3437
3538 // Wait for instance to become running
3639 if ! waitForRunning (ctx , ops , name , 120 * time .Second ) {
37- log .Printf ("Timed out waiting for %s to start; models/keys not configured" , name )
40+ log .Printf ("Timed out waiting for %s to start; models/keys not configured" , logutil . SanitizeForLog ( name ) )
3841 return
3942 }
4043
@@ -46,7 +49,7 @@ func ConfigureInstance(ctx context.Context, ops InstanceOps, name string, models
4649 }
4750 data := []byte (strings .Join (lines , "\n " ) + "\n " )
4851 if err := ops .WriteFile (ctx , name , pathClaworcKeys , data ); err != nil {
49- log .Printf ("Error writing API keys for %s: %v" , name , err )
52+ log .Printf ("Error writing API keys for %s: %v" , logutil . SanitizeForLog ( name ) , err )
5053 return
5154 }
5255 }
@@ -63,18 +66,20 @@ func ConfigureInstance(ctx context.Context, ops InstanceOps, name string, models
6366 }
6467 modelJSON , err := json .Marshal (modelConfig )
6568 if err != nil {
66- log .Printf ("Error marshaling model config for %s: %v" , name , err )
69+ log .Printf ("Error marshaling model config for %s: %v" , logutil . SanitizeForLog ( name ) , err )
6770 return
6871 }
72+ // Use base64 encoding to safely pass JSON through shell
73+ b64 := base64 .StdEncoding .EncodeToString (modelJSON )
6974 cmd := []string {"su" , "-" , "claworc" , "-c" ,
70- fmt .Sprintf ("openclaw config set agents.defaults.model '%s' -- json" , string ( modelJSON ) )}
75+ fmt .Sprintf ("openclaw config set agents.defaults.model \" $(echo '%s' | base64 -d) \" -- json" , b64 )}
7176 _ , stderr , code , err := ops .ExecInInstance (ctx , name , cmd )
7277 if err != nil {
73- log .Printf ("Error setting model config for %s: %v" , name , err )
78+ log .Printf ("Error setting model config for %s: %v" , logutil . SanitizeForLog ( name ) , err )
7479 return
7580 }
7681 if code != 0 {
77- log .Printf ("Failed to set model config for %s: %s" , name , stderr )
82+ log .Printf ("Failed to set model config for %s: %s" , logutil . SanitizeForLog ( name ), logutil . SanitizeForLog ( stderr ) )
7883 return
7984 }
8085 }
@@ -83,14 +88,14 @@ func ConfigureInstance(ctx context.Context, ops InstanceOps, name string, models
8388 cmd := []string {"su" , "-" , "claworc" , "-c" , "openclaw gateway stop" }
8489 _ , stderr , code , err := ops .ExecInInstance (ctx , name , cmd )
8590 if err != nil {
86- log .Printf ("Error restarting gateway for %s: %v" , name , err )
91+ log .Printf ("Error restarting gateway for %s: %v" , logutil . SanitizeForLog ( name ) , err )
8792 return
8893 }
8994 if code != 0 {
90- log .Printf ("Failed to restart gateway for %s: %s" , name , stderr )
95+ log .Printf ("Failed to restart gateway for %s: %s" , logutil . SanitizeForLog ( name ), logutil . SanitizeForLog ( stderr ) )
9196 return
9297 }
93- log .Printf ("Models and API keys configured for %s" , name )
98+ log .Printf ("Models and API keys configured for %s" , logutil . SanitizeForLog ( name ) )
9499}
95100
96101func waitForRunning (ctx context.Context , ops InstanceOps , name string , timeout time.Duration ) bool {
0 commit comments