Skip to content

Commit 460d506

Browse files
Accept authenticated proxy params via Scala CLI config file
1 parent 4f51aa1 commit 460d506

File tree

4 files changed

+107
-12
lines changed

4 files changed

+107
-12
lines changed

modules/cli/src/main/scala/scala/cli/ScalaCli.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package scala.cli
22

3-
import coursier.proxy.SetupProxy
43
import sun.misc.{Signal, SignalHandler}
54

65
import java.io.{ByteArrayOutputStream, File, PrintStream}
@@ -173,7 +172,7 @@ object ScalaCli {
173172

174173
(new BouncycastleSignerMaker).maybeInit()
175174

176-
SetupProxy.setup()
175+
coursier.Resolve.proxySetup()
177176

178177
// Getting killed by SIGPIPE quite often when on musl (in the "static" native
179178
// image), but also sometimes on glibc, or even on macOS, when we use domain

modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
733733
sudoTest()
734734
}
735735

736-
def authProxyTest(): Unit = {
736+
def authProxyTest(legacySetup: Boolean): Unit = {
737737
val okDir = os.rel / "ok"
738738
val wrongDir = os.rel / "wrong"
739739
val inputs = TestInputs(
@@ -759,10 +759,44 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
759759
s"-D$scheme.proxyProtocol=http"
760760
)
761761
}
762-
val proxyArgs = authProperties("localhost", 9083, "jack", "insecure")
763-
val wrongProxyArgs = authProperties("localhost", 9084, "wrong", "nope")
764-
val image = Constants.authProxyTestImage
762+
val proxyArgs =
763+
if (legacySetup) authProperties("localhost", 9083, "jack", "insecure")
764+
else Nil
765+
val wrongProxyArgs =
766+
if (legacySetup) authProperties("localhost", 9084, "wrong", "nope")
767+
else Nil
768+
def setupProxyConfig(
769+
cwd: os.Path,
770+
env: Map[String, String],
771+
host: String,
772+
port: Int,
773+
user: String,
774+
password: String
775+
): Unit = {
776+
os.proc(TestUtil.cli, "config", "httpProxy.address", s"http://$host:$port")
777+
.call(cwd = cwd, env = env)
778+
os.proc(TestUtil.cli, "config", "httpProxy.user", s"value:$user")
779+
.call(cwd = cwd, env = env)
780+
os.proc(TestUtil.cli, "config", "httpProxy.password", s"value:$password")
781+
.call(cwd = cwd, env = env)
782+
}
783+
val image = Constants.authProxyTestImage
765784
inputs.fromRoot { root =>
785+
val configDir = root / "configs"
786+
os.makeDir(configDir, "rwx------")
787+
val configFile = configDir / "config.json"
788+
val wrongConfigFile = configDir / "wrong-config.json"
789+
val (configEnv, wrongConfigEnv) =
790+
if (legacySetup)
791+
(Map.empty[String, String], Map.empty[String, String])
792+
else {
793+
val csEnv = TestUtil.putCsInPathViaEnv(root / "bin")
794+
val configEnv0 = Map("SCALA_CLI_CONFIG" -> configFile.toString) ++ csEnv
795+
val wrongConfigEnv0 = Map("SCALA_CLI_CONFIG" -> wrongConfigFile.toString) ++ csEnv
796+
setupProxyConfig(root, configEnv0, "localhost", 9083, "jack", "insecure")
797+
setupProxyConfig(root, wrongConfigEnv0, "localhost", 9084, "wrong", "nope")
798+
(configEnv0, wrongConfigEnv0)
799+
}
766800
DockerServer.withServer(image, root.toString, 80 -> 9083) { _ =>
767801
DockerServer.withServer(image, root.toString, 80 -> 9084) { _ =>
768802

@@ -775,7 +809,7 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
775809
"--cache",
776810
os.rel / "tmp-cache-ok"
777811
)
778-
.call(cwd = root / okDir)
812+
.call(cwd = root / okDir, env = configEnv)
779813
val okOutput = okRes.out.trim()
780814
expect(okOutput == "Hello proxy")
781815

@@ -788,7 +822,12 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
788822
"--cache",
789823
os.rel / "tmp-cache-wrong"
790824
)
791-
.call(cwd = root / wrongDir, mergeErrIntoOut = true, check = false)
825+
.call(
826+
cwd = root / wrongDir,
827+
env = wrongConfigEnv,
828+
mergeErrIntoOut = true,
829+
check = false
830+
)
792831
val wrongOutput = wrongRes.out.trim()
793832
expect(wrongRes.exitCode == 1)
794833
expect(wrongOutput.contains(
@@ -799,14 +838,21 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
799838
}
800839
}
801840

802-
def runAuthProxyTest: Boolean =
841+
def runAuthProxyTests: Boolean =
803842
Properties.isLinux || (Properties.isMac && !TestUtil.isCI)
804-
if (runAuthProxyTest)
843+
if (runAuthProxyTests) {
844+
test("auth proxy (legacy)") {
845+
TestUtil.retry() {
846+
authProxyTest(legacySetup = true)
847+
}
848+
}
849+
805850
test("auth proxy") {
806851
TestUtil.retry() {
807-
authProxyTest()
852+
authProxyTest(legacySetup = false)
808853
}
809854
}
855+
}
810856

811857
test("UTF-8") {
812858
val message = "Hello from TestÅÄÖåäö"

website/docs/guides/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Configuration
3-
sidebar_position: 9
3+
sidebar_position: 7
44
---
55

66
`scala-cli` can be configured in two ways:

website/docs/guides/proxies.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: Proxies
3+
sidebar_position: 8
4+
---
5+
6+
## HTTP proxies
7+
8+
### Configuration
9+
10+
If you can only download artifacts through a proxy, you need to configure it beforehand, like
11+
```text
12+
scala-cli config httpProxy.address http://proxy.company.com
13+
```
14+
15+
Replace `proxy.company.com` by the address of your proxy.
16+
17+
Change `http://` to `https://` if your proxy is accessible via HTTPS.
18+
19+
### Authentication
20+
21+
If your proxy requires authentication, set your user and password with
22+
```text
23+
scala-cli config httpProxy.user _encoded_user_
24+
scala-cli config httpProxy.password _encoded_password_
25+
```
26+
27+
Replace `_encoded_user_` and `_encoded_password_` by your actual user and password, following
28+
the [password option format](../reference/password-options.md). They should typically look like
29+
`env:ENV_VAR_NAME`, `file:/path/to/file`, or `command:command to run`.
30+
31+
## Default repositories
32+
33+
If you don't rely on proxies, but rather download artifacts through different Maven repositories,
34+
set those repositories like:
35+
```text
36+
scala-cli config repositories.default https://first-repo.company.com https://second-repo.company.com
37+
```
38+
39+
## Mirrors
40+
41+
If you're fine directly downloading artifacts from the internet, but would rather have some
42+
repositories requests go through a repository of yours, configure mirror repositories, like
43+
```text
44+
scala-cli config repositories.mirrors https://repo1.maven.org/maven2=https://repository.company.com/maven
45+
```
46+
47+
To have all requests to a Maven repository go through a repository of yours, do
48+
```text
49+
scala-cli config repositories.mirrors maven:*=https://repository.company.com/maven
50+
```

0 commit comments

Comments
 (0)