Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions agent/agentscripts/agentscripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ func (r *Runner) run(ctx context.Context, script codersdk.WorkspaceAgentScript,
)
logger.Info(ctx, "running agent script", slog.F("script", script.Script))

logDir := filepath.Dir(logPath)
if err = r.Filesystem.MkdirAll(logDir, 0o700); err != nil {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: Just wondering if we could define a constant for 0o700. Also, should we make this configurable via an environment variable or config option, with 0o700 as the default?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering if we could define a constant for 0o700

0o700 states the exact permission bits, it feels more intuitive/easier to read.

should we make this configurable via an environment variable or config option, with 0o700 as the default

0o700 seems like a safe private default, the workspace owner can chmod it afterward if they need looser perms. We can revisit this if a use case like this comes up.

return xerrors.Errorf("create script log file directory %q: %w", logDir, err)
}

fileWriter, err := r.Filesystem.OpenFile(logPath, os.O_CREATE|os.O_RDWR, 0o600)
if err != nil {
return xerrors.Errorf("open %s script log file: %w", logPath, err)
Expand Down
44 changes: 44 additions & 0 deletions agent/agentscripts/agentscripts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,50 @@ func TestExecuteBasic(t *testing.T) {
require.Equal(t, "hello", log.Output)
}

func TestExecuteCreatesMissingLogDir(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)

fs := afero.NewOsFs()
logger := testutil.Logger(t)
s, err := agentssh.NewServer(context.Background(), logger, prometheus.NewRegistry(), fs, agentexec.DefaultExecer, nil)
require.NoError(t, err)
t.Cleanup(func() {
_ = s.Close()
})

fLogger := newFakeScriptLogger()
runner := agentscripts.New(agentscripts.Options{
LogDir: t.TempDir(),
DataDirBase: t.TempDir(),
Logger: logger,
SSHServer: s,
Filesystem: fs,
GetScriptLogger: func(uuid.UUID) agentscripts.ScriptLogger {
return fLogger
},
})
defer runner.Close()

logPath := filepath.Join(t.TempDir(), "does", "not", "exist", "install.log")

aAPI := agenttest.NewFakeAgentAPI(t, logger, nil, nil)
err = runner.Init([]codersdk.WorkspaceAgentScript{{
LogSourceID: uuid.New(),
LogPath: logPath,
Script: "echo hello",
}}, aAPI.ScriptCompleted)
require.NoError(t, err)
require.NoError(t, runner.Execute(ctx, agentscripts.ExecuteAllScripts))

log := testutil.TryReceive(ctx, t, fLogger.logs)
require.Equal(t, "hello", log.Output)

exists, err := afero.Exists(fs, logPath)
require.NoError(t, err)
require.True(t, exists, "expected log file to be created at %s", logPath)
}

func TestEnv(t *testing.T) {
t.Parallel()
fLogger := newFakeScriptLogger()
Expand Down
Loading