1use {
2 crate::component::bindings::fastly::compute::{backend, http_types, types},
3 crate::config::{Backend, ClientCertInfo},
4 crate::error::Error,
5 crate::sandbox::Sandbox,
6 crate::secret_store::SecretLookup,
7 crate::wiggle_abi::SecretStoreError,
8 http::HeaderValue,
9 http::Uri,
10 std::mem::take,
11 std::time::Duration,
12 wasmtime::component::{Resource, ResourceTable},
13};
14
15pub(crate) async fn register_dynamic_backend(
16 sandbox: &mut Sandbox,
17 table: &mut ResourceTable,
18 prefix: String,
19 target: String,
20 options: Resource<backend::DynamicBackendOptions>,
21) -> Result<Resource<String>, types::Error> {
22 let options = take(table.get_mut(&options)?);
23
24 let name = prefix.as_str();
25 let origin_name = target.as_str();
26
27 let override_host = if let Some(host_override) = options.host_override {
28 if host_override.is_empty() {
29 return Err(types::Error::InvalidArgument);
30 }
31
32 if host_override.len() > 1024 {
33 return Err(types::Error::InvalidArgument);
34 }
35
36 Some(HeaderValue::from_bytes(host_override.as_bytes())?)
37 } else {
38 None
39 };
40
41 let use_tls = options.use_tls;
42 let scheme = if use_tls { "https" } else { "http" };
43
44 let ca_certs = if use_tls {
45 if let Some(ca_cert) = options.ca_cert {
46 if ca_cert.is_empty() {
47 return Err(types::Error::InvalidArgument);
48 }
49
50 if ca_cert.len() > (64 * 1024) {
51 return Err(types::Error::InvalidArgument);
52 }
53
54 let mut byte_cursor = std::io::Cursor::new(ca_cert.as_bytes());
55 rustls_pemfile::certs(&mut byte_cursor)?
56 .drain(..)
57 .map(rustls::Certificate)
58 .collect()
59 } else {
60 vec![]
61 }
62 } else {
63 vec![]
64 };
65
66 let mut cert_host = if let Some(cert_hostname) = options.cert_hostname {
67 if cert_hostname.is_empty() {
68 return Err(types::Error::InvalidArgument);
69 }
70
71 if cert_hostname.len() > 1024 {
72 return Err(types::Error::InvalidArgument);
73 }
74
75 Some(cert_hostname)
76 } else {
77 None
78 };
79
80 let use_sni = if let Some(sni_hostname) = options.sni_hostname {
81 if sni_hostname.is_empty() {
82 false
83 } else if sni_hostname.len() > 1024 {
84 return Err(types::Error::InvalidArgument);
85 } else {
86 if let Some(cert_host) = &cert_host {
87 if cert_host != &sni_hostname {
88 return Err(types::Error::InvalidArgument);
90 }
91 } else {
92 cert_host = Some(sni_hostname);
93 }
94
95 true
96 }
97 } else {
98 true
99 };
100
101 let client_cert = if let Some((client_cert, client_key)) = options.client_cert {
102 let key_lookup = sandbox
103 .secret_lookup(client_key)
104 .ok_or(Error::SecretStoreError(
105 SecretStoreError::InvalidSecretHandle(client_key),
106 ))?;
107 let key = match &key_lookup {
108 SecretLookup::Standard {
109 store_name,
110 secret_name,
111 } => sandbox
112 .secret_stores()
113 .get_store(store_name)
114 .ok_or(Error::SecretStoreError(
115 SecretStoreError::InvalidSecretHandle(client_key),
116 ))?
117 .get_secret(secret_name)
118 .ok_or(Error::SecretStoreError(
119 SecretStoreError::InvalidSecretHandle(client_key),
120 ))?
121 .plaintext(),
122
123 SecretLookup::Injected { plaintext } => plaintext,
124 };
125
126 Some(ClientCertInfo::new(client_cert.as_bytes(), key)?)
127 } else {
128 None
129 };
130
131 let grpc = options.grpc;
132
133 let first_byte_timeout = if options.first_byte_timeout != 0 {
134 Some(Duration::from_millis(options.first_byte_timeout as u64))
135 } else {
136 None
137 };
138
139 let between_bytes_timeout = if options.between_bytes_timeout != 0 {
140 Some(Duration::from_millis(options.between_bytes_timeout as u64))
141 } else {
142 None
143 };
144
145 let uri = Uri::builder()
146 .scheme(scheme)
147 .authority(origin_name)
148 .path_and_query("/")
149 .build()
150 .map_err(|_e| types::Error::InvalidArgument)?;
151
152 let new_backend = Backend {
153 uri,
154 override_host,
155 cert_host,
156 use_sni,
157 grpc,
158 first_byte_timeout,
159 between_bytes_timeout,
160 client_cert,
161 ca_certs,
162 health: crate::config::BackendHealth::Unknown,
163 };
164
165 if !sandbox.add_backend(name, new_backend) {
166 return Err(Error::BackendNameRegistryError(name.to_string()).into());
167 }
168
169 let res = table.push(prefix)?;
170
171 Ok(res)
172}
173
174pub(crate) fn exists(sandbox: &mut Sandbox, backend: &str) -> bool {
175 sandbox.backend(backend).is_some()
176}
177
178pub(crate) fn is_healthy(
179 sandbox: &mut Sandbox,
180 backend: &str,
181) -> Result<backend::BackendHealth, types::Error> {
182 let backend = sandbox.backend(backend).ok_or(Error::InvalidArgument)?;
183 match &backend.health {
184 crate::config::BackendHealth::Unknown => Ok(backend::BackendHealth::Unknown),
185 crate::config::BackendHealth::Healthy => Ok(backend::BackendHealth::Healthy),
186 crate::config::BackendHealth::Unhealthy => Ok(backend::BackendHealth::Unhealthy),
187 }
188}
189
190pub(crate) fn is_dynamic(sandbox: &mut Sandbox, backend: &str) -> Result<bool, types::Error> {
191 if sandbox.dynamic_backend(backend).is_some() {
192 Ok(true)
193 } else if sandbox.backend(backend).is_some() {
194 Ok(false)
195 } else {
196 Err(Error::InvalidArgument.into())
197 }
198}
199
200pub(crate) fn get_host(
201 sandbox: &mut Sandbox,
202 backend: &str,
203 max_len: u64,
204) -> Result<String, types::Error> {
205 let backend = sandbox.backend(backend).ok_or(Error::InvalidArgument)?;
206 let host = backend.uri.host().ok_or(Error::InvalidArgument)?;
207
208 if host.len() > usize::try_from(max_len).unwrap() {
209 return Err(types::Error::BufferLen(u64::try_from(host.len()).unwrap()));
212 }
213
214 Ok(host.to_owned())
215}
216
217pub(crate) fn get_override_host(
218 sandbox: &mut Sandbox,
219 backend: &str,
220 max_len: u64,
221) -> Result<Option<Vec<u8>>, types::Error> {
222 let backend = sandbox.backend(backend).ok_or(Error::InvalidArgument)?;
223 if let Some(host) = backend.override_host.as_ref() {
224 let host = host.to_str()?;
225
226 if host.len() > usize::try_from(max_len).unwrap() {
227 return Err(types::Error::BufferLen(u64::try_from(host.len()).unwrap()));
228 }
229
230 Ok(Some(host.as_bytes().to_owned()))
231 } else {
232 Ok(None)
233 }
234}
235
236pub(crate) fn get_port(sandbox: &mut Sandbox, backend: &str) -> Result<u16, types::Error> {
237 let backend = sandbox.backend(backend).ok_or(Error::InvalidArgument)?;
238 match backend.uri.port_u16() {
239 Some(port) => Ok(port),
240 None => {
241 if backend.uri.scheme() == Some(&http::uri::Scheme::HTTPS) {
242 Ok(443)
243 } else {
244 Ok(80)
245 }
246 }
247 }
248}
249
250pub(crate) fn get_connect_timeout_ms(
251 sandbox: &mut Sandbox,
252 backend: &str,
253) -> Result<u32, types::Error> {
254 let _ = sandbox.backend(backend).ok_or(Error::InvalidArgument)?;
256 Err(Error::Unsupported {
257 msg: "connection timing is not actually supported in Viceroy",
258 }
259 .into())
260}
261
262pub(crate) fn get_first_byte_timeout_ms(
263 sandbox: &mut Sandbox,
264 backend: &str,
265) -> Result<u32, types::Error> {
266 let _ = sandbox.backend(backend).ok_or(Error::InvalidArgument)?;
268 Err(Error::Unsupported {
269 msg: "connection timing is not actually supported in Viceroy",
270 }
271 .into())
272}
273
274pub(crate) fn get_between_bytes_timeout_ms(
275 sandbox: &mut Sandbox,
276 backend: &str,
277) -> Result<u32, types::Error> {
278 let _ = sandbox.backend(backend).ok_or(Error::InvalidArgument)?;
280 Err(Error::Unsupported {
281 msg: "connection timing is not actually supported in Viceroy",
282 }
283 .into())
284}
285
286pub(crate) fn is_tls(sandbox: &mut Sandbox, backend: &str) -> Result<bool, types::Error> {
287 let backend = sandbox.backend(backend).ok_or(Error::InvalidArgument)?;
288 Ok(backend.uri.scheme() == Some(&http::uri::Scheme::HTTPS))
289}
290
291pub(crate) fn get_tls_min_version(
292 sandbox: &mut Sandbox,
293 backend: &str,
294) -> Result<Option<http_types::TlsVersion>, types::Error> {
295 let _ = sandbox.backend(backend).ok_or(Error::InvalidArgument)?;
297 Err(Error::Unsupported {
299 msg: "tls version flags are not supported in Viceroy",
300 }
301 .into())
302}
303
304pub(crate) fn get_tls_max_version(
305 sandbox: &mut Sandbox,
306 backend: &str,
307) -> Result<Option<http_types::TlsVersion>, types::Error> {
308 let _ = sandbox.backend(backend).ok_or(Error::InvalidArgument)?;
310 Err(Error::Unsupported {
312 msg: "tls version flags are not supported in Viceroy",
313 }
314 .into())
315}
316
317pub(crate) fn get_http_keepalive_time(
318 _sandbox: &mut Sandbox,
319 _backend: &str,
320) -> Result<backend::TimeoutMs, types::Error> {
321 Err(Error::Unsupported {
322 msg: "backend.get-http-keepalive-time is not supported in Viceroy",
323 }
324 .into())
325}
326
327pub(crate) fn get_tcp_keepalive_enable(
328 _sandbox: &mut Sandbox,
329 _backend: &str,
330) -> Result<bool, types::Error> {
331 Err(Error::Unsupported {
332 msg: "backend.get-tcp-keepalive-enable is not supported in Viceroy",
333 }
334 .into())
335}
336
337pub(crate) fn get_tcp_keepalive_interval(
338 _sandbox: &mut Sandbox,
339 _backend: &str,
340) -> Result<backend::TimeoutSecs, types::Error> {
341 Err(Error::Unsupported {
342 msg: "backend.get-tcp-keepalive-interval is not supported in Viceroy",
343 }
344 .into())
345}
346
347pub(crate) fn get_tcp_keepalive_probes(
348 _sandbox: &mut Sandbox,
349 _backend: &str,
350) -> Result<backend::ProbeCount, types::Error> {
351 Err(Error::Unsupported {
352 msg: "backend.get-tcp-keepalive-probes is not supported in Viceroy",
353 }
354 .into())
355}
356
357pub(crate) fn get_tcp_keepalive_time(
358 _sandbox: &mut Sandbox,
359 _backend: &str,
360) -> Result<backend::TimeoutSecs, types::Error> {
361 Err(Error::Unsupported {
362 msg: "backend.get_tcp_keepalive_time not supported in Viceroy",
363 }
364 .into())
365}