1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use crate::shapes::FilePath;
use crate::{config_struct, config_unit_enum, is_false};
use rustc_hash::FxHashMap;
use schematic::{Config, ConfigEnum, ValidateError, ValidateResult, env};
fn path_is_required<D, C>(
value: &FilePath,
_data: &D,
_context: &C,
_finalize: bool,
) -> ValidateResult {
if value.as_str().is_empty() {
return Err(ValidateError::new("path must not be empty"));
}
Ok(())
}
config_unit_enum!(
/// The API format of the remote service.
/// @since 1.32.0
#[derive(ConfigEnum)]
pub enum RemoteApi {
/// gRPC(S) endpoints.
#[default]
Grpc,
/// HTTP(S) endpoints.
Http,
}
);
config_struct!(
/// Configures basic HTTP authentication.
/// @since 1.32.0
#[derive(Config)]
pub struct RemoteAuthConfig {
/// A map of HTTP headers to inject into every request.
#[serde(default, skip_serializing_if = "FxHashMap::is_empty")]
pub headers: FxHashMap<String, String>,
/// The name of an environment variable to use as a bearer token.
#[setting(env = "MOON_REMOTE_AUTH_TOKEN")]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub token: Option<String>,
}
);
config_unit_enum!(
/// Supported blob compression levels for gRPC APIs.
/// @since 1.31.0
#[derive(ConfigEnum)]
pub enum RemoteCompression {
/// No compression.
#[default]
None,
/// Zstandard compression.
Zstd,
}
);
impl RemoteCompression {
pub fn is_enabled(&self) -> bool {
!matches!(self, Self::None)
}
}
config_struct!(
/// Configures the action cache (AC) and content-addressable cache (CAS).
/// @since 1.30.0
#[derive(Config)]
#[config(env_prefix = "MOON_REMOTE_CACHE_")]
pub struct RemoteCacheConfig {
/// The compression format to use when uploading/downloading blobs.
/// @since 1.31.0
pub compression: RemoteCompression,
/// Unique instance name for blobs. Will be used as a folder name.
#[setting(default = "moon-outputs")]
pub instance_name: String,
/// When local, only download matching blobs and do not upload new
/// blobs. Blobs will only be uploaded in CI environments.
/// @since 1.40.0
#[setting(parse_env = env::parse_bool)]
#[serde(default, skip_serializing_if = "is_false")]
pub local_read_only: bool,
/// When downloading blobs, verify the digests/hashes in the response
/// match the associated blob contents. This will reduce performance
/// but ensure partial or corrupted blobs won't cause failures.
/// @since 1.36.0
#[setting(parse_env = env::parse_bool)]
#[serde(default, skip_serializing_if = "is_false")]
pub verify_integrity: bool,
}
);
config_struct!(
/// Configures for server-only authentication with TLS.
/// @since 1.30.0
#[derive(Config)]
#[config(env_prefix = "MOON_REMOTE_TLS_")]
pub struct RemoteTlsConfig {
/// If true, assume that the server supports HTTP/2,
/// even if it doesn't provide protocol negotiation via ALPN.
#[setting(env = "MOON_REMOTE_TLS_HTTP2", parse_env = env::parse_bool)]
#[serde(default, skip_serializing_if = "is_false")]
pub assume_http2: bool,
/// A file path, relative from the workspace root, to the
/// certificate authority PEM encoded X509 certificate.
#[setting(validate = path_is_required)]
pub cert: FilePath,
/// The domain name in which to verify the TLS certificate.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub domain: Option<String>,
}
);
config_struct!(
/// Configures for both server and client authentication with mTLS.
/// @since 1.30.0
#[derive(Config)]
#[config(env_prefix = "MOON_REMOTE_MTLS_")]
pub struct RemoteMtlsConfig {
/// If true, assume that the server supports HTTP/2,
/// even if it doesn't provide protocol negotiation via ALPN.
#[setting(env = "MOON_REMOTE_MTLS_HTTP", parse_env = env::parse_bool)]
#[serde(default, skip_serializing_if = "is_false")]
pub assume_http2: bool,
/// A file path, relative from the workspace root, to the
/// certificate authority PEM encoded X509 certificate.
#[setting(validate = path_is_required)]
pub ca_cert: FilePath,
/// A file path, relative from the workspace root, to the
/// client's PEM encoded X509 certificate.
#[setting(validate = path_is_required)]
pub client_cert: FilePath,
/// A file path, relative from the workspace root, to the
/// client's PEM encoded X509 private key.
#[setting(validate = path_is_required)]
pub client_key: FilePath,
/// The domain name in which to verify the TLS certificate.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub domain: Option<String>,
}
);
config_struct!(
/// Configures the remote service, powered by the Bazel Remote Execution API.
/// @since 1.30.0
#[derive(Config)]
pub struct RemoteConfig {
/// The API format of the remote service.
/// @since 1.32.0
#[setting(env = "MOON_REMOTE_API")]
pub api: RemoteApi,
/// Connect to the host using basic HTTP authentication.
/// @since 1.32.0
#[setting(nested)]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub auth: Option<RemoteAuthConfig>,
/// Configures the action cache (AC) and content-addressable cache (CAS).
#[setting(nested)]
pub cache: RemoteCacheConfig,
/// The remote host to connect and send requests to.
/// Supports gRPC protocols.
#[setting(env = "MOON_REMOTE_HOST")]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub host: Option<String>,
/// Connect to the host using server and client authentication with mTLS.
/// This takes precedence over normal TLS.
#[setting(nested)]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub mtls: Option<RemoteMtlsConfig>,
/// Connect to the host using server-only authentication with TLS.
#[setting(nested)]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tls: Option<RemoteTlsConfig>,
}
);
impl RemoteConfig {
pub fn get_host(&self) -> &str {
self.host.as_deref().unwrap()
}
pub fn is_bearer_auth(&self) -> bool {
self.auth.as_ref().is_some_and(|auth| auth.token.is_some())
}
pub fn is_enabled(&self) -> bool {
self.host.as_ref().is_some_and(|host| !host.is_empty())
}
pub fn is_localhost(&self) -> bool {
self.host
.as_ref()
.is_some_and(|host| host.contains("localhost") || host.contains("0.0.0.0"))
}
pub fn is_secure(&self) -> bool {
self.is_bearer_auth() || self.tls.is_some() || self.mtls.is_some()
}
pub fn is_secure_protocol(&self) -> bool {
self.host
.as_ref()
.is_some_and(|host| host.starts_with("https") || host.starts_with("grpcs"))
}
}