diff --git a/cmd/mapt/cmd/params/params.go b/cmd/mapt/cmd/params/params.go index 2b5171b61..845fef274 100644 --- a/cmd/mapt/cmd/params/params.go +++ b/cmd/mapt/cmd/params/params.go @@ -105,6 +105,11 @@ const ( glRunnerUnsecure string = "glrunner-unsecure" glRunnerUnsecureDesc string = "when set, the runner service runs as the default OS user instead of a dedicated system account; by default a locked-down gitlab-runner system user is created" + glRunnerHTTPProxy string = "glrunner-http-proxy" + glRunnerHTTPProxyDesc string = "HTTP proxy URL for the runner's jobs (e.g., http://proxy.example.com:3128). Sets HTTP_PROXY and HTTPS_PROXY in upper and lower case" + glRunnerNoProxy string = "glrunner-no-proxy" + glRunnerNoProxyDesc string = "Comma-separated host names, domains or IP addresses the runner's jobs reach without the proxy. CIDR ranges are rejected" + GlRunnerConcurrent string = "glrunner-concurrent" GlRunnerConcurrentDesc string = "maximum number of jobs the runner executes concurrently" GlRunnerConcurrentPowerDefault int = 2 @@ -438,6 +443,8 @@ func AddGitLabRunnerFlags(fs *pflag.FlagSet) { fs.StringP(glRunnerURL, "", glRunnerURLDefault, glRunnerURLDesc) fs.StringSlice(glRunnerTags, nil, glRunnerTagsDesc) fs.Bool(glRunnerUnsecure, false, glRunnerUnsecureDesc) + fs.StringP(glRunnerHTTPProxy, "", "", glRunnerHTTPProxyDesc) + fs.StringP(glRunnerNoProxy, "", "", glRunnerNoProxyDesc) } @@ -448,6 +455,10 @@ func GitLabRunnerArgs(platform *gitlab.Platform, arch *gitlab.Arch) *gitlab.GitL logging.Error("--glrunner-project-id and --glrunner-group-id are mutually exclusive; ignoring GitLab runner configuration") return nil } + env, err := gitlab.ProxyEnv(viper.GetString(glRunnerHTTPProxy), viper.GetString(glRunnerNoProxy)) + if err != nil { + logging.Fatalf("invalid GitLab runner proxy settings: %v", err) + } return &gitlab.GitLabRunnerArgs{ GitLabToken: viper.GetString(glRunnerToken), ProjectID: viper.GetString(glRunnerProjectID), @@ -458,8 +469,12 @@ func GitLabRunnerArgs(platform *gitlab.Platform, arch *gitlab.Arch) *gitlab.GitL Arch: arch, Unsecure: viper.GetBool(glRunnerUnsecure), Concurrent: viper.GetInt(GlRunnerConcurrent), + Env: env, } } + if viper.GetString(glRunnerHTTPProxy) != "" || viper.GetString(glRunnerNoProxy) != "" { + logging.Warn("--glrunner-http-proxy and --glrunner-no-proxy are ignored without --glrunner-token") + } return nil } diff --git a/cmd/mapt/cmd/params/params_test.go b/cmd/mapt/cmd/params/params_test.go index 21c361eaf..41e62f32e 100644 --- a/cmd/mapt/cmd/params/params_test.go +++ b/cmd/mapt/cmd/params/params_test.go @@ -1,6 +1,7 @@ package params import ( + "reflect" "testing" "github.com/spf13/viper" @@ -60,3 +61,50 @@ func TestGitLabRunnerArgsKeepsPlatform(t *testing.T) { } } } + +func TestGitLabRunnerArgsProxyEnv(t *testing.T) { + t.Cleanup(viper.Reset) + viper.Set(glRunnerToken, "glpat-test") + viper.Set(glRunnerProjectID, "123") + viper.Set(glRunnerHTTPProxy, "http://proxy.example.com:3128") + viper.Set(glRunnerNoProxy, "localhost,.example.com") + + linux := gitlab.Linux + args := GitLabRunnerArgs(&linux, &gitlab.Amd64) + if args == nil { + t.Fatal("GitLabRunnerArgs returned nil") + } + want := []string{ + "HTTP_PROXY=http://proxy.example.com:3128", + "http_proxy=http://proxy.example.com:3128", + "HTTPS_PROXY=http://proxy.example.com:3128", + "https_proxy=http://proxy.example.com:3128", + "NO_PROXY=localhost,.example.com", + "no_proxy=localhost,.example.com", + } + if !reflect.DeepEqual(args.Env, want) { + t.Errorf("Env =\n%q\nwant\n%q", args.Env, want) + } +} + +func TestGitLabRunnerArgsWithoutProxy(t *testing.T) { + t.Cleanup(viper.Reset) + viper.Set(glRunnerToken, "glpat-test") + viper.Set(glRunnerProjectID, "123") + + linux := gitlab.Linux + args := GitLabRunnerArgs(&linux, &gitlab.Amd64) + if args == nil || args.Env != nil { + t.Errorf("GitLabRunnerArgs() = %+v; want args with no Env", args) + } +} + +func TestGitLabRunnerArgsProxyWithoutToken(t *testing.T) { + t.Cleanup(viper.Reset) + viper.Set(glRunnerHTTPProxy, "http://proxy.example.com:3128") + + linux := gitlab.Linux + if args := GitLabRunnerArgs(&linux, &gitlab.Amd64); args != nil { + t.Errorf("GitLabRunnerArgs() = %+v; want nil without a token", args) + } +} diff --git a/docs/gitlab-runner.md b/docs/gitlab-runner.md index 93f793fd3..58889eb1f 100644 --- a/docs/gitlab-runner.md +++ b/docs/gitlab-runner.md @@ -61,6 +61,34 @@ After obtaining the token and the project/group ID, you can deploy a runner usin **Optional flags:** * `--glrunner-tags` - Comma-separated tags (e.g., `aws,mapt,fedora`) +* `--glrunner-http-proxy` - Proxy URL for the jobs when the host network needs an egress proxy (e.g., `http://proxy.example.com:3128`). It is written to the runner's `environment` as `HTTP_PROXY`, `HTTPS_PROXY`, `http_proxy` and `https_proxy` +* `--glrunner-no-proxy` - Comma-separated host names, domains or IP addresses the jobs reach directly, written as `NO_PROXY` and `no_proxy`. Requires `--glrunner-http-proxy`. CIDR ranges such as `10.0.0.0/8` are rejected, because some clients (curl on RHEL 9, for example) ignore them without an error + +Both proxy flags need `--glrunner-token`; without it no runner is set up and they are ignored. + +> *NOTE:* The proxy settings only reach the jobs. Downloading and registering the runner, the runner service and the image pulls made by Podman on the host use the host's own proxy configuration; see [#974](https://github.com/redhat-developer/mapt/issues/974). + +> *NOTE:* The proxy URL, including any credentials in it, ends up in: +> * the instance user data, which any process on the host can read from the cloud's instance metadata service. With `--glrunner-unsecure`, jobs run as the host login user, so job code can read it too +> * the Pulumi state in the `--backed-url` backend, because the user data is a resource input +> * `/var/lib/cloud/instance/user-data.txt` on Linux hosts that use cloud-init +> * `/etc/gitlab-runner/config.toml` +> +> Prefer a proxy that allows clients by source address, or a credential used only for this runner. Percent-encode special characters such as `@`, `:`, `/`, `?` and `#` in a user name or password. + +To deploy a Fedora runner on AWS behind an egress proxy: + +```bash +mapt aws fedora create --spot \ + --glrunner-token="glpat-xxxxxxxxxxxxxxxxxxxx" \ + --glrunner-url="https://gitlab.com" \ + --glrunner-project-id="12345678" \ + --glrunner-http-proxy="http://proxy.example.com:3128" \ + --glrunner-no-proxy="localhost,127.0.0.1,.internal.example.com" \ + --project-name mapt-fedora-aws \ + --backed-url file:///Users/tester/workspace \ + --conn-details-output /Users/tester/workspace/conn-details +``` To deploy a Fedora runner on AWS as a project runner: diff --git a/pkg/integrations/gitlab/glrunner.go b/pkg/integrations/gitlab/glrunner.go index 0036a7742..fcdbb2a5c 100644 --- a/pkg/integrations/gitlab/glrunner.go +++ b/pkg/integrations/gitlab/glrunner.go @@ -54,9 +54,23 @@ func (args *GitLabRunnerArgs) GetUserDataValues() *integrations.UserDataValues { PowerVSRegion: args.PowerVSRegion, PowerVSDNS: args.PowerVSDNS, PowerVSMTU: args.PowerVSMTU, + RunnerEnv: quotedEnv(args), } } +// quotedEnv quotes each KEY=VALUE pair for the platform's setup script. +func quotedEnv(args *GitLabRunnerArgs) []string { + quote := shellQuote + if args.Platform != nil && *args.Platform == Windows { + quote = powershellQuote + } + quoted := make([]string, 0, len(args.Env)) + for _, kv := range args.Env { + quoted = append(quoted, quote(kv)) + } + return quoted +} + func (args *GitLabRunnerArgs) GetSetupScriptTemplate() string { templateConfig := string(snippets[*runnerArgs.Platform][:]) return templateConfig diff --git a/pkg/integrations/gitlab/proxy.go b/pkg/integrations/gitlab/proxy.go new file mode 100644 index 000000000..f6d5fe1df --- /dev/null +++ b/pkg/integrations/gitlab/proxy.go @@ -0,0 +1,98 @@ +package gitlab + +import ( + "fmt" + "net" + "net/url" + "regexp" + "strings" +) + +// hostPattern matches a host name or a domain suffix such as ".example.com". Labels may +// contain underscores (_svc.internal), and a fully qualified name may end in a dot. +var hostPattern = regexp.MustCompile(`^\.?[A-Za-z0-9_]([A-Za-z0-9_-]*[A-Za-z0-9_])?(\.[A-Za-z0-9_]([A-Za-z0-9_-]*[A-Za-z0-9_])?)*\.?$`) + +const encodeHint = "percent-encode characters such as @ : / ? # in a user name or password" + +// ProxyEnv returns the proxy variables for the runner's job environment. Programs read +// different names, so each variable is set in upper and lower case. It returns nil when +// httpProxy is empty. +func ProxyEnv(httpProxy, noProxy string) ([]string, error) { + if httpProxy == "" { + if noProxy != "" { + return nil, fmt.Errorf("--glrunner-no-proxy needs --glrunner-http-proxy") + } + return nil, nil + } + if err := validateProxyURL(httpProxy); err != nil { + return nil, err + } + env := []string{ + "HTTP_PROXY=" + httpProxy, + "http_proxy=" + httpProxy, + "HTTPS_PROXY=" + httpProxy, + "https_proxy=" + httpProxy, + } + if noProxy != "" { + entries, err := noProxyEntries(noProxy) + if err != nil { + return nil, err + } + list := strings.Join(entries, ",") + env = append(env, "NO_PROXY="+list, "no_proxy="+list) + } + return env, nil +} + +// validateProxyURL checks the proxy URL. Errors show the URL without its password, +// or not at all when it does not parse. +func validateProxyURL(raw string) error { + u, err := url.Parse(raw) + if err != nil || u.Hostname() == "" { + return fmt.Errorf("--glrunner-http-proxy must be a URL such as http://proxy.example.com:3128; %s", encodeHint) + } + if u.Scheme != "http" && u.Scheme != "https" { + return fmt.Errorf("--glrunner-http-proxy must use http or https, got %q", u.Redacted()) + } + if (u.Path != "" && u.Path != "/") || u.RawQuery != "" || u.Fragment != "" { + return fmt.Errorf("--glrunner-http-proxy must not have a path, query or fragment, got %q; %s", u.Redacted(), encodeHint) + } + return nil +} + +// noProxyEntries splits and checks a NO_PROXY list. It accepts host names, domains, IP +// literals and "*". A CIDR is rejected because some clients, such as curl 7.76.1 on +// RHEL 9, ignore it without a message and send the request to the proxy. Repeated +// entries are dropped, keeping the first. +func noProxyEntries(list string) ([]string, error) { + var entries []string + seen := map[string]bool{} + for _, raw := range strings.Split(list, ",") { + entry := strings.TrimSpace(raw) + if entry == "" || seen[strings.ToLower(entry)] { + continue + } + seen[strings.ToLower(entry)] = true + if _, _, err := net.ParseCIDR(entry); err == nil { + return nil, fmt.Errorf("--glrunner-no-proxy entry %q is a CIDR range, which some clients (such as curl on RHEL 9) ignore without an error; list the host names or IP addresses instead", entry) + } + if entry != "*" && net.ParseIP(strings.Trim(entry, "[]")) == nil && !hostPattern.MatchString(entry) { + return nil, fmt.Errorf("--glrunner-no-proxy entry %q is not a host name, domain, IP address or *", entry) + } + entries = append(entries, entry) + } + if len(entries) == 0 { + return nil, fmt.Errorf("--glrunner-no-proxy has no entries, got %q", list) + } + return entries, nil +} + +// shellQuote quotes a value for bash. +func shellQuote(v string) string { + return "'" + strings.ReplaceAll(v, "'", `'\''`) + "'" +} + +// powershellQuote quotes a value for PowerShell. +func powershellQuote(v string) string { + return "'" + strings.ReplaceAll(v, "'", "''") + "'" +} diff --git a/pkg/integrations/gitlab/proxy_test.go b/pkg/integrations/gitlab/proxy_test.go new file mode 100644 index 000000000..e193794a3 --- /dev/null +++ b/pkg/integrations/gitlab/proxy_test.go @@ -0,0 +1,209 @@ +package gitlab + +import ( + "os" + "os/exec" + "path/filepath" + "reflect" + "strings" + "testing" +) + +func TestProxyEnv(t *testing.T) { + env, err := ProxyEnv("http://proxy.example.com:3128", "localhost, .internal.example.com,10.0.0.5,::1,*") + if err != nil { + t.Fatalf("ProxyEnv: %v", err) + } + want := []string{ + "HTTP_PROXY=http://proxy.example.com:3128", + "http_proxy=http://proxy.example.com:3128", + "HTTPS_PROXY=http://proxy.example.com:3128", + "https_proxy=http://proxy.example.com:3128", + "NO_PROXY=localhost,.internal.example.com,10.0.0.5,::1,*", + "no_proxy=localhost,.internal.example.com,10.0.0.5,::1,*", + } + if !reflect.DeepEqual(env, want) { + t.Errorf("ProxyEnv =\n%q\nwant\n%q", env, want) + } +} + +func TestProxyEnvWithoutNoProxy(t *testing.T) { + env, err := ProxyEnv("https://proxy.example.com", "") + if err != nil { + t.Fatalf("ProxyEnv: %v", err) + } + if len(env) != 4 || strings.Contains(strings.Join(env, " "), "NO_PROXY") { + t.Errorf("ProxyEnv = %q; want only the four proxy variables", env) + } +} + +func TestProxyEnvUnset(t *testing.T) { + env, err := ProxyEnv("", "") + if err != nil || env != nil { + t.Errorf("ProxyEnv(\"\", \"\") = %q, %v; want nil, nil", env, err) + } +} + +func TestProxyEnvErrors(t *testing.T) { + cases := map[string]struct { + httpProxy, noProxy, wantErr string + }{ + "CIDR in no-proxy": {"http://proxy:3128", "localhost,192.168.0.0/16", "192.168.0.0/16\" is a CIDR range"}, + "IPv6 CIDR in no-proxy": {"http://proxy:3128", "fd00::/8", "is a CIDR range"}, + "invalid no-proxy entry": {"http://proxy:3128", "bad host", "is not a host name"}, + "no-proxy without proxy": {"", "localhost", "needs --glrunner-http-proxy"}, + "proxy without scheme": {"proxy.example.com:3128", "", "--glrunner-http-proxy"}, + "proxy with bad scheme": {"ftp://proxy.example.com", "", "must use http or https"}, + "proxy without host": {"http://:3128", "", "must be a URL"}, + "unescaped # in password": {"http://user:p#ss@proxy:3128", "", "percent-encode"}, + // "user:12" parses as host and port, so the error comes from the fragment check. + "unescaped # after digits": {"http://user:12#x@proxy:3128", "", "percent-encode"}, + "unescaped ? in password": {"http://user:p?ss@proxy:3128", "", "percent-encode"}, + "proxy with path": {"http://proxy:3128/path/", "", "must not have a path"}, + "only separators": {"http://proxy:3128", " , ,", "has no entries"}, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + _, err := ProxyEnv(tc.httpProxy, tc.noProxy) + if err == nil || !strings.Contains(err.Error(), tc.wantErr) { + t.Errorf("ProxyEnv(%q, %q) error = %v; want it to contain %q", tc.httpProxy, tc.noProxy, err, tc.wantErr) + } + }) + } +} + +func TestProxyEnvErrorsHideThePassword(t *testing.T) { + for _, proxy := range []string{ + "ftp://user:s3cret@proxy:3128", + "http://user:s3cret@proxy:3128/path", + "http://user:s3cret#@proxy:3128", + } { + _, err := ProxyEnv(proxy, "") + if err == nil || strings.Contains(err.Error(), "s3cret") { + t.Errorf("ProxyEnv(%q) error = %v; want an error without the password", proxy, err) + } + } +} + +func TestProxyEnvAccepts(t *testing.T) { + for _, proxy := range []string{ + "http://proxy.example.com:3128/", + "http://user:p%23ss@proxy.example.com:3128", + "https://[fd00::1]:3128", + } { + if _, err := ProxyEnv(proxy, ""); err != nil { + t.Errorf("ProxyEnv(%q): %v", proxy, err) + } + } +} + +func TestNoProxyEntries(t *testing.T) { + env, err := ProxyEnv("http://proxy:3128", "example.com.,_svc.internal,.corp.example.com.,localhost,LOCALHOST,example.com.") + if err != nil { + t.Fatalf("ProxyEnv: %v", err) + } + want := "NO_PROXY=example.com.,_svc.internal,.corp.example.com.,localhost" + if env[4] != want { + t.Errorf("NO_PROXY = %q, want %q", env[4], want) + } +} + +// registerArgs runs the rendered `gitlab-runner register` command with a fake +// gitlab-runner and returns the arguments it received, one per line. +func registerArgs(t *testing.T, snippet string) []string { + t.Helper() + if _, err := exec.LookPath("bash"); err != nil { + t.Skip("bash not available") + } + start := strings.Index(snippet, "sudo gitlab-runner register") + if start < 0 { + t.Fatal("snippet has no gitlab-runner register command") + } + var lines []string + for _, line := range strings.Split(snippet[start:], "\n") { + lines = append(lines, line) + if !strings.HasSuffix(strings.TrimSpace(line), `\`) { + break + } + } + register := strings.Replace(strings.Join(lines, "\n"), "sudo gitlab-runner", "fake_runner", 1) + script := "fake_runner() { printf '%s\\n' \"$@\"; }\n_docker_dns_args=()\n" + register + "\n" + path := filepath.Join(t.TempDir(), "register.sh") + if err := os.WriteFile(path, []byte(script), 0o600); err != nil { + t.Fatalf("write script: %v", err) + } + out, err := exec.Command("bash", path).CombinedOutput() + if err != nil { + t.Fatalf("register command failed: %v\n%s", err, out) + } + return strings.Split(strings.TrimSpace(string(out)), "\n") +} + +func envArgs(args []string) []string { + var env []string + for i := 0; i+1 < len(args); i++ { + if args[i] == "--env" { + env = append(env, args[i+1]) + } + } + return env +} + +func TestSnippetRegistersProxyEnv(t *testing.T) { + // A proxy password may contain shell characters; they must reach the runner unchanged. + proxy := `http://user:p'a$s@proxy.example.com:3128` + env, err := ProxyEnv(proxy, "localhost,.example.com") + if err != nil { + t.Fatalf("ProxyEnv: %v", err) + } + + for _, platform := range []Platform{Linux, Darwin} { + t.Run(string(platform), func(t *testing.T) { + args := newRunnerArgs("", Amd64) + args.Platform = &platform + args.Env = env + got := envArgs(registerArgs(t, renderSnippet(t, args))) + if !reflect.DeepEqual(got, env) { + t.Errorf("register --env values =\n%q\nwant\n%q", got, env) + } + }) + } +} + +func TestSnippetLinuxPowerRegistersProxyEnv(t *testing.T) { + env, err := ProxyEnv("http://proxy.example.com:3128", "localhost") + if err != nil { + t.Fatalf("ProxyEnv: %v", err) + } + args := newPowerArgs("us-south", testResolvers, 1450) + args.Env = env + registered := registerArgs(t, renderSnippet(t, args)) + if got := envArgs(registered); !reflect.DeepEqual(got, env) { + t.Errorf("register --env values =\n%q\nwant\n%q", got, env) + } + if !strings.Contains(strings.Join(registered, "\n"), "--docker-volumes") { + t.Errorf("the arguments after --env were lost: %q", registered) + } +} + +func TestSnippetWithoutProxyHasNoEnv(t *testing.T) { + for _, platform := range []Platform{Linux, Darwin, Windows} { + args := newRunnerArgs("", Amd64) + args.Platform = &platform + if snippet := renderSnippet(t, args); strings.Contains(snippet, "--env") { + t.Errorf("%s snippet without a proxy must not pass --env", platform) + } + } +} + +func TestSnippetWindowsQuotesProxyEnv(t *testing.T) { + windows := Windows + args := newRunnerArgs("", Amd64) + args.Platform = &windows + args.Env = []string{"HTTP_PROXY=http://us'er@proxy:3128"} + snippet := renderSnippet(t, args) + want := " --env 'HTTP_PROXY=http://us''er@proxy:3128' `\n --executor \"shell\"" + if !strings.Contains(snippet, want) { + t.Errorf("Windows snippet missing %q:\n%s", want, snippet) + } +} diff --git a/pkg/integrations/gitlab/snippet-darwin.sh b/pkg/integrations/gitlab/snippet-darwin.sh index da43b5f57..9132ff616 100644 --- a/pkg/integrations/gitlab/snippet-darwin.sh +++ b/pkg/integrations/gitlab/snippet-darwin.sh @@ -9,6 +9,9 @@ sudo gitlab-runner register \ --url "{{ .RepoURL }}" \ --token "{{ .Token }}" \ --name "{{ .Name }}" \ +{{- range .RunnerEnv }} + --env {{ . }} \ +{{- end }} --executor "shell" # Install and start as LaunchDaemon diff --git a/pkg/integrations/gitlab/snippet-linux.sh b/pkg/integrations/gitlab/snippet-linux.sh index 27b2d6a9d..b388ecddb 100644 --- a/pkg/integrations/gitlab/snippet-linux.sh +++ b/pkg/integrations/gitlab/snippet-linux.sh @@ -356,6 +356,9 @@ sudo gitlab-runner register \ --docker-network-mode "$PODMAN_NET_NAME" \ {{- end}} "${_docker_dns_args[@]}" \ +{{- range .RunnerEnv }} + --env {{ . }} \ +{{- end }} --docker-volumes "/etc/containers/executor-containers.conf:/etc/containers/containers.conf:ro" {{- if not .Unsecure}} diff --git a/pkg/integrations/gitlab/snippet-windows.ps1 b/pkg/integrations/gitlab/snippet-windows.ps1 index a83f3414a..930aef826 100644 --- a/pkg/integrations/gitlab/snippet-windows.ps1 +++ b/pkg/integrations/gitlab/snippet-windows.ps1 @@ -8,6 +8,9 @@ C:\GitLab-Runner\gitlab-runner.exe register ` --url "{{ .RepoURL }}" ` --token "{{ .Token }}" ` --name "{{ .Name }}" ` +{{- range .RunnerEnv }} + --env {{ . }} ` +{{- end }} --executor "shell" # Install and start as Windows service diff --git a/pkg/integrations/gitlab/types.go b/pkg/integrations/gitlab/types.go index 5686db15a..df26ee27e 100644 --- a/pkg/integrations/gitlab/types.go +++ b/pkg/integrations/gitlab/types.go @@ -43,4 +43,7 @@ type GitLabRunnerArgs struct { // setup script applies it to the shared podman network, because the // runner's network_mtu key has no effect in network_mode "podman". PowerVSMTU int + // Env holds KEY=VALUE pairs for the runner's job environment, such as the proxy + // variables from ProxyEnv. They are written to the environment list of [[runners]]. + Env []string } diff --git a/pkg/integrations/integrations.go b/pkg/integrations/integrations.go index da569ff74..884e5ebf9 100644 --- a/pkg/integrations/integrations.go +++ b/pkg/integrations/integrations.go @@ -23,6 +23,9 @@ type UserDataValues struct { Ephemeral bool RunnerImageRepo string RunnerImageRepoVersion string + // RunnerEnv holds KEY=VALUE pairs for the runner's job environment, already quoted + // for the script they are rendered into. + RunnerEnv []string } type IntegrationConfig interface {