Skip to content

Commit 45f13aa

Browse files
authored
Use sensitive output. (#106)
1 parent f95e360 commit 45f13aa

File tree

6 files changed

+26
-37
lines changed

6 files changed

+26
-37
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/go-chi/cors v1.2.1
1111
github.com/go-chi/render v1.0.3
1212
github.com/gorilla/websocket v1.5.3
13-
github.com/launchrctl/launchr v0.18.0
13+
github.com/launchrctl/launchr v0.18.3-0.20250320175144-edbfae1fb9fb
1414
github.com/oapi-codegen/nethttp-middleware v1.0.2
1515
github.com/oapi-codegen/oapi-codegen/v2 v2.4.1
1616
github.com/oapi-codegen/runtime v1.1.1

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
265265
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
266266
github.com/launchrctl/launchr v0.18.0 h1:Kp5iguwl5rx1qo2M3A2eMlhTeefgL5UDKFzP/7B45UE=
267267
github.com/launchrctl/launchr v0.18.0/go.mod h1:hhJSGcxn1FhD267u0JfNu6u65naTQAVxElnqUTY9nag=
268+
github.com/launchrctl/launchr v0.18.3-0.20250316225153-b280479eabd0 h1:cOlcoOCphR/qR0Fw2/W8xjbFHF0kuYbRnIZ6M8CXosI=
269+
github.com/launchrctl/launchr v0.18.3-0.20250316225153-b280479eabd0/go.mod h1:hhJSGcxn1FhD267u0JfNu6u65naTQAVxElnqUTY9nag=
270+
github.com/launchrctl/launchr v0.18.3-0.20250320175144-edbfae1fb9fb h1:RpFawKeAYg7VW72zxUUSSwedGkZXyT0XxwjMxXkqoLY=
271+
github.com/launchrctl/launchr v0.18.3-0.20250320175144-edbfae1fb9fb/go.mod h1:hhJSGcxn1FhD267u0JfNu6u65naTQAVxElnqUTY9nag=
268272
github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4=
269273
github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ5ajtkr5xPLts4=
270274
github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4=

server/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type launchrServer struct {
3030
wsMutex sync.Mutex
3131
customize FrontendCustomize
3232
logsDirPath string
33+
app launchr.App
3334
}
3435

3536
// FrontendCustomize stores variables to customize web appearance.
@@ -354,7 +355,7 @@ func (l *launchrServer) RunAction(w http.ResponseWriter, r *http.Request, id str
354355

355356
// Prepare action for run.
356357
// Can we fetch directly json?
357-
streams, err := createFileStreams(l.logsDirPath, runID)
358+
streams, err := createFileStreams(l.logsDirPath, runID, l.app)
358359
if err != nil {
359360
sendError(w, http.StatusInternalServerError, "Error preparing streams")
360361
}

server/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func Run(ctx context.Context, app launchr.App, opts *RunOptions) error {
9494
apiPrefix: opts.APIPrefix,
9595
customize: opts.FrontendCustomize,
9696
logsDirPath: opts.LogsDirPath,
97+
app: app,
9798
}
9899
app.GetService(&store.actionMngr)
99100
app.GetService(&store.cfg)

server/streams.go

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io"
77
"os"
88
"path/filepath"
9-
"strings"
109

1110
"github.com/launchrctl/launchr"
1211
)
@@ -16,35 +15,14 @@ type fileStreams interface {
1615
}
1716

1817
// webCli implements Streams interface.
19-
// @todo Maybe refactor original streams.
2018
type webCli struct {
21-
in *launchr.In
22-
out *launchr.Out
23-
err io.Writer
19+
launchr.Streams
2420
files []*os.File
2521
}
2622

27-
// In returns the reader used for stdin
28-
func (cli *webCli) In() *launchr.In {
29-
return cli.in
30-
}
31-
32-
// Out returns the writer used for stdout
33-
func (cli *webCli) Out() *launchr.Out {
34-
return cli.out
35-
}
36-
37-
// Err returns the writer used for stderr
38-
func (cli *webCli) Err() io.Writer {
39-
return cli.err
40-
}
41-
4223
// Close implements io.Closer.
4324
func (cli *webCli) Close() (err error) {
44-
for i := 0; i < len(cli.files); i++ {
45-
_ = cli.files[i].Close()
46-
}
47-
return nil
25+
return cli.Streams.Close()
4826
}
4927

5028
// GetStreamData implements fileStreams.
@@ -93,7 +71,14 @@ func (w *wrappedWriter) Write(p []byte) (int, error) {
9371
return w.w.Write(p)
9472
}
9573

96-
func createFileStreams(streamsDir, runId string) (*webCli, error) {
74+
func (w *wrappedWriter) Close() error {
75+
if c, ok := w.w.(io.Closer); ok {
76+
return c.Close()
77+
}
78+
return nil
79+
}
80+
81+
func createFileStreams(streamsDir, runId string, app launchr.App) (*webCli, error) {
9782
outfile, err := os.Create(filepath.Join(streamsDir, runId+"-out.txt"))
9883
if err != nil {
9984
return nil, fmt.Errorf("error creating output file: %w", err)
@@ -116,9 +101,7 @@ func createFileStreams(streamsDir, runId string) (*webCli, error) {
116101

117102
// Build and return webCli
118103
return &webCli{
119-
files: []*os.File{outfile, errfile},
120-
in: launchr.NewIn(io.NopCloser(strings.NewReader(""))),
121-
out: launchr.NewOut(out),
122-
err: errWriter,
104+
Streams: launchr.NewBasicStreams(nil, app.SensitiveWriter(out), app.SensitiveWriter(errWriter)),
105+
files: []*os.File{outfile, errfile},
123106
}, nil
124107
}

web_runner.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import (
2121
)
2222

2323
const (
24-
backgroundEnvVar = "LAUNCHR_BACKGROUND"
24+
backgroundEnvVar = launchr.EnvVar("web_background")
2525
serverInfoFilename = "server-info.json"
2626
)
2727

2828
func isBackGroundEnv() bool {
29-
return len(os.Getenv(backgroundEnvVar)) == 1
29+
return len(backgroundEnvVar.Get()) == 1
3030
}
3131

3232
func (p *Plugin) runWeb(ctx context.Context, webOpts webFlags) error {
@@ -72,7 +72,7 @@ func (p *Plugin) runWeb(ctx context.Context, webOpts webFlags) error {
7272

7373
func (p *Plugin) runBackgroundWeb(ctx context.Context, flags webFlags, pidFile string) error {
7474
if isBackGroundEnv() {
75-
err := redirectOutputs(flags.PluginDir)
75+
err := redirectOutputs(p.app, flags.PluginDir)
7676
if err != nil {
7777
return err
7878
}
@@ -158,7 +158,7 @@ func runBackgroundCmd(pidFile string) (int, error) {
158158

159159
// Prepare the command to restart itself in the background
160160
command := exec.Command(os.Args[0], os.Args[1:]...) //nolint G204
161-
command.Env = append(os.Environ(), backgroundEnvVar+"=1")
161+
command.Env = append(os.Environ(), backgroundEnvVar.EnvString("1"))
162162

163163
// Set platform-specific process ID
164164
setSysProcAttr(command)
@@ -176,7 +176,7 @@ func runBackgroundCmd(pidFile string) (int, error) {
176176
return command.Process.Pid, nil
177177
}
178178

179-
func redirectOutputs(dir string) error {
179+
func redirectOutputs(app launchr.App, dir string) error {
180180
err := launchr.EnsurePath(dir)
181181
if err != nil {
182182
return fmt.Errorf("can't create plugin temporary directory")
@@ -188,7 +188,7 @@ func redirectOutputs(dir string) error {
188188
}
189189

190190
// Redirect log messages to a file.
191-
launchr.Log().SetOutput(outLog)
191+
launchr.Log().SetOutput(app.SensitiveWriter(outLog))
192192
// Discard console output because it's intended for user interaction.
193193
launchr.Term().SetOutput(io.Discard)
194194

0 commit comments

Comments
 (0)