Skip to content

Commit 86a1f1a

Browse files
dtretyakovsylvestre
authored andcommitted
Support authentication in WebDAV storage (#1608)
It brings ability to provide the following type of credentials: - username/password - token
1 parent 4a8b5f5 commit 86a1f1a

File tree

6 files changed

+51
-7
lines changed

6 files changed

+51
-7
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ bincode = "1"
3131
blake3 = "1"
3232
byteorder = "1.0"
3333
bytes = "1"
34-
opendal = { version= "0.27.1", optional=true }
34+
opendal = { version= "0.29.1", optional=true }
3535
reqsign = {version="0.8.5", optional=true}
3636
clap = { version = "4.1.11", features = ["derive", "env", "wrap_help"] }
3737
directories = "4.0.1"

docs/Webdav.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# WebDAV
22

3-
Set `SCCACHE_WEBDAV_ENDPOINT` to a wevdav service endpoint to store cache in a webdav service. Set `SCCACHE_WEBDAV_KEY_PREFIX` to specify the key prefix of cache.
3+
Set `SCCACHE_WEBDAV_ENDPOINT` to a webdav service endpoint to store cache in a webdav service. Set `SCCACHE_WEBDAV_KEY_PREFIX` to specify the key prefix of cache.
44

55
The webdav cache is compatible with:
66

@@ -9,3 +9,9 @@ The webdav cache is compatible with:
99
- [Gradle Build Cache](https://docs.gradle.org/current/userguide/build_cache.html)
1010

1111
Users can set `SCCACHE_WEBDAV_ENDPOINT` to those services directly.
12+
13+
## Credentials
14+
15+
Sccache is able to load credentials from the following sources:
16+
- Set `SCCACHE_WEBDAV_USERNAME`/`SCCACHE_WEBDAV_PASSWORD` to specify the username/password pair for basic authentication.
17+
- Set `SCCACHE_WEBDAV_TOKEN` to specify the token value for bearer token authentication.

src/cache/cache.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,16 @@ pub fn storage_from_config(
557557
#[cfg(feature = "webdav")]
558558
CacheType::Webdav(ref c) => {
559559
debug!("Init webdav cache with endpoint {}", c.endpoint);
560-
let storage = WebdavCache::build(&c.endpoint, &c.key_prefix)
561-
.map_err(|err| anyhow!("create webdav cache failed: {err:?}"))?;
560+
561+
let storage = WebdavCache::build(
562+
&c.endpoint,
563+
&c.key_prefix,
564+
c.username.as_deref(),
565+
c.password.as_deref(),
566+
c.token.as_deref(),
567+
)
568+
.map_err(|err| anyhow!("create webdav cache failed: {err:?}"))?;
569+
562570
return Ok(Arc::new(storage));
563571
}
564572
#[allow(unreachable_patterns)]

src/cache/webdav.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,25 @@ pub struct WebdavCache;
2020

2121
impl WebdavCache {
2222
/// Create a new `WebdavCache`.
23-
pub fn build(endpoint: &str, key_prefix: &str) -> Result<Operator> {
23+
pub fn build(
24+
endpoint: &str,
25+
key_prefix: &str,
26+
username: Option<&str>,
27+
password: Option<&str>,
28+
token: Option<&str>,
29+
) -> Result<Operator> {
2430
let mut builder = Webdav::default();
2531
builder.endpoint(endpoint);
2632
builder.root(key_prefix);
33+
if let Some(username) = username {
34+
builder.username(username);
35+
}
36+
if let Some(password) = password {
37+
builder.password(password);
38+
}
39+
if let Some(token) = token {
40+
builder.token(token);
41+
}
2742

2843
let op = Operator::create(builder)?
2944
.layer(LoggingLayer::default())

src/config.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ pub struct RedisCacheConfig {
233233
pub struct WebdavCacheConfig {
234234
pub endpoint: String,
235235
pub key_prefix: String,
236+
pub username: Option<String>,
237+
pub password: Option<String>,
238+
pub token: Option<String>,
236239
}
237240

238241
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
@@ -665,10 +668,16 @@ fn config_from_env() -> Result<EnvConfig> {
665668
.filter(|s| !s.is_empty())
666669
.unwrap_or_default()
667670
.to_owned();
671+
let username = env::var("SCCACHE_WEBDAV_USERNAME").ok();
672+
let password = env::var("SCCACHE_WEBDAV_PASSWORD").ok();
673+
let token = env::var("SCCACHE_WEBDAV_TOKEN").ok();
668674

669675
Some(WebdavCacheConfig {
670676
endpoint,
671677
key_prefix,
678+
username,
679+
password,
680+
token,
672681
})
673682
} else {
674683
None
@@ -1171,6 +1180,9 @@ no_credentials = true
11711180
[cache.webdav]
11721181
endpoint = "http://127.0.0.1:8080"
11731182
key_prefix = "webdavprefix"
1183+
username = "webdavusername"
1184+
password = "webdavpassword"
1185+
token = "webdavtoken"
11741186
"#;
11751187

11761188
let file_config: FileConfig = toml::from_str(CONFIG_STR).expect("Is valid toml.");
@@ -1213,6 +1225,9 @@ key_prefix = "webdavprefix"
12131225
webdav: Some(WebdavCacheConfig {
12141226
endpoint: "http://127.0.0.1:8080".to_string(),
12151227
key_prefix: "webdavprefix".into(),
1228+
username: Some("webdavusername".to_string()),
1229+
password: Some("webdavpassword".to_string()),
1230+
token: Some("webdavtoken".to_string()),
12161231
})
12171232
},
12181233
dist: DistConfig {

0 commit comments

Comments
 (0)