fix(agent): minimal env and explicit project dir for compose, resolve every config file's symlinks (#7)
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -43,10 +44,29 @@ func ExecDocker(ctx context.Context, dir string, out io.Writer, args ...string)
|
||||
cmd.Dir = dir
|
||||
cmd.Stdout = out
|
||||
cmd.Stderr = out
|
||||
cmd.Env = dockerEnv()
|
||||
cmd.WaitDelay = 10 * time.Second
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
// dockerEnvVars are the only variables from the agent's environment that
|
||||
// reach docker compose. Compose prefers process variables over the stack's
|
||||
// .env file when it interpolates, so passing the agent's own (HOSTNAME,
|
||||
// AGENT_TOKEN, ...) would change stack configuration and expose the token.
|
||||
var dockerEnvVars = []string{"PATH", "HOME", "DOCKER_HOST", "DOCKER_CONFIG", "DOCKER_TLS_VERIFY", "DOCKER_CERT_PATH"}
|
||||
|
||||
// dockerEnv returns the allowed variables that are set in the agent's
|
||||
// environment, as KEY=value pairs.
|
||||
func dockerEnv() []string {
|
||||
var env []string
|
||||
for _, k := range dockerEnvVars {
|
||||
if v := os.Getenv(k); v != "" {
|
||||
env = append(env, k+"="+v)
|
||||
}
|
||||
}
|
||||
return env
|
||||
}
|
||||
|
||||
// ComposeDocker is the subset of the Docker SDK ComposeExecutor needs.
|
||||
type ComposeDocker interface {
|
||||
SelfInspector
|
||||
@@ -115,7 +135,9 @@ func (e *ComposeExecutor) Execute(ctx context.Context, job protocol.JobStart, ou
|
||||
return "", err
|
||||
}
|
||||
|
||||
base := []string{"compose", "-p", project}
|
||||
// --project-directory is explicit because, with -f, compose would take it
|
||||
// from the first file, which differs for stacks started with the flag.
|
||||
base := []string{"compose", "-p", project, "--project-directory", workingDir}
|
||||
for _, f := range files {
|
||||
base = append(base, "-f", f)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user