/build/source/nativelink-store/src/oss_store.rs
Line | Count | Source |
1 | | // Copyright 2026 Trace Machina, Inc. All rights reserved. |
2 | | // |
3 | | // Licensed under the Functional Source License, Version 1.1, Apache 2.0 Future License (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // See LICENSE file for details |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | use core::time::Duration; |
16 | | use std::sync::Arc; |
17 | | |
18 | | use aws_config::{AppName, BehaviorVersion}; |
19 | | use aws_sdk_s3::Client; |
20 | | use aws_sdk_s3::config::endpoint::{Endpoint, EndpointFuture, Params, ResolveEndpoint}; |
21 | | use aws_sdk_s3::config::retry::RetryConfig; |
22 | | use aws_sdk_s3::config::{ |
23 | | Credentials, Region, RequestChecksumCalculation, ResponseChecksumValidation, |
24 | | }; |
25 | | use aws_smithy_runtime_api::client::http::HttpClient as SmithyHttpClient; |
26 | | use aws_smithy_types::endpoint::EndpointAuthScheme; |
27 | | use nativelink_config::stores::{CommonObjectSpec, ExperimentalAwsSpec, ExperimentalOssSpec}; |
28 | | use nativelink_error::{Code, Error, make_err}; |
29 | | use nativelink_util::instant_wrapper::InstantWrapper; |
30 | | |
31 | | use crate::common_s3_utils::TlsClient; |
32 | | use crate::s3_store::S3Store; |
33 | | |
34 | | /// OSS S3-compatible endpoint adapter. Transfer logic stays in `S3Store`. |
35 | | /// See <https://www.alibabacloud.com/help/en/oss/developer-reference/use-aws-sdks-to-access-oss>. |
36 | | #[derive(Debug, Clone, Copy)] |
37 | | pub struct OssStore; |
38 | | |
39 | | #[derive(Debug)] |
40 | | struct CnameResolver(Endpoint); |
41 | | |
42 | | impl ResolveEndpoint for CnameResolver { |
43 | 18 | fn resolve_endpoint(&self, _params: &Params) -> EndpointFuture<'_> { |
44 | 18 | EndpointFuture::ready(Ok(self.0.clone())) |
45 | 18 | } |
46 | | } |
47 | | |
48 | | impl OssStore { |
49 | | #[allow(clippy::new_ret_no_self)] |
50 | 0 | pub fn new<I, NowFn>( |
51 | 0 | spec: &ExperimentalOssSpec, |
52 | 0 | now_fn: NowFn, |
53 | 0 | ) -> Result<Arc<S3Store<NowFn>>, Error> |
54 | 0 | where |
55 | 0 | I: InstantWrapper, |
56 | 0 | NowFn: Fn() -> I + Send + Sync + Unpin + 'static, |
57 | | { |
58 | 0 | let aws_spec = Self::build_aws_spec(spec)?; |
59 | 0 | let http_client = TlsClient::new(&aws_spec.common)?; |
60 | 0 | Self::new_with_http_client(spec, http_client, now_fn) |
61 | 0 | } |
62 | | |
63 | | /// Allows wire tests to supply an HTTP client without contacting OSS. |
64 | | #[allow(clippy::new_ret_no_self)] |
65 | 18 | pub fn new_with_http_client<I, NowFn, C>( |
66 | 18 | spec: &ExperimentalOssSpec, |
67 | 18 | http_client: C, |
68 | 18 | now_fn: NowFn, |
69 | 18 | ) -> Result<Arc<S3Store<NowFn>>, Error> |
70 | 18 | where |
71 | 18 | I: InstantWrapper, |
72 | 18 | NowFn: Fn() -> I + Send + Sync + Unpin + 'static, |
73 | 18 | C: SmithyHttpClient + Clone + 'static, |
74 | | { |
75 | 18 | let aws_spec = Self::build_aws_spec(spec)?0 ; |
76 | 18 | let region = Region::new(spec.region.clone()); |
77 | 18 | let endpoint = Self::endpoint(spec)?0 ; |
78 | 18 | let mut config = aws_sdk_s3::Config::builder() |
79 | 18 | .behavior_version(BehaviorVersion::latest()) |
80 | 18 | .app_name(AppName::new("nativelink").expect("valid app name")) |
81 | 18 | .region(region) |
82 | | // OSS documents virtual-hosted addressing for its S3 interface. |
83 | 18 | .force_path_style(false) |
84 | | // Do not add optional trailing checksums / aws-chunked encoding. |
85 | 18 | .request_checksum_calculation(RequestChecksumCalculation::WhenRequired) |
86 | 18 | .response_checksum_validation(ResponseChecksumValidation::WhenRequired) |
87 | 18 | .retry_config(RetryConfig::standard().with_max_attempts(1)) |
88 | 18 | .timeout_config( |
89 | 18 | aws_config::timeout::TimeoutConfig::builder() |
90 | 18 | .connect_timeout(Duration::from_secs(15)) |
91 | 18 | .operation_attempt_timeout(Duration::from_mins(2)) |
92 | 18 | .build(), |
93 | | ) |
94 | 18 | .credentials_provider(Credentials::new( |
95 | 18 | &spec.access_key_id, |
96 | 18 | &spec.secret_access_key, |
97 | 18 | spec.session_token.clone(), |
98 | 18 | None, |
99 | | "oss-config", |
100 | | )) |
101 | 18 | .http_client(http_client); |
102 | 18 | if spec.cname { |
103 | 3 | config = config.endpoint_resolver(CnameResolver( |
104 | 3 | Endpoint::builder() |
105 | 3 | .url(endpoint) |
106 | 3 | .auth_scheme( |
107 | 3 | EndpointAuthScheme::with_capacity("sigv4", 3) |
108 | 3 | .put("disableDoubleEncoding", true) |
109 | 3 | .put("signingName", "s3") |
110 | 3 | .put("signingRegion", spec.region.clone()), |
111 | 3 | ) |
112 | 3 | .build(), |
113 | 3 | )); |
114 | 15 | } else { |
115 | 15 | config = config.endpoint_url(endpoint); |
116 | 15 | } |
117 | 18 | S3Store::new_with_client_and_jitter( |
118 | 18 | &aws_spec, |
119 | 18 | Client::from_conf(config.build()), |
120 | 18 | spec.retry.make_jitter_fn(), |
121 | 18 | now_fn, |
122 | | ) |
123 | 18 | } |
124 | | |
125 | 51 | pub fn endpoint(spec: &ExperimentalOssSpec) -> Result<String, Error> { |
126 | 51 | if spec.cname && spec.endpoint13 .is_none13 () { |
127 | 1 | return Err(make_err!( |
128 | 1 | Code::InvalidArgument, |
129 | 1 | "OSS cname requires an explicit endpoint bound to the bucket" |
130 | 1 | )); |
131 | 50 | } |
132 | 50 | let endpoint = spec |
133 | 50 | .endpoint |
134 | 50 | .clone() |
135 | 50 | .unwrap_or_else(|| format!30 ("https://s3.oss-{}.aliyuncs.com", spec.region)); |
136 | 50 | let url48 = url::Url::parse(&endpoint) |
137 | 50 | .map_err(|_| make_err!2 (Code::InvalidArgument2 , "Invalid OSS endpoint"))?2 ; |
138 | 48 | if url.scheme() != "https" |
139 | 46 | || url.host_str().is_none() |
140 | 46 | || !url.username().is_empty() |
141 | 44 | || url.password().is_some() |
142 | 44 | || url.path() != "/" |
143 | 42 | || url.query().is_some() |
144 | 40 | || url.fragment().is_some() |
145 | | { |
146 | 10 | return Err(make_err!( |
147 | 10 | Code::InvalidArgument, |
148 | 10 | "OSS endpoint must be an HTTPS URL without credentials, path, query or fragment" |
149 | 10 | )); |
150 | 38 | } |
151 | 38 | Ok(endpoint) |
152 | 51 | } |
153 | | |
154 | 47 | pub fn build_aws_spec(spec: &ExperimentalOssSpec) -> Result<ExperimentalAwsSpec, Error> { |
155 | 47 | if spec.region.is_empty() |
156 | 46 | || !spec |
157 | 46 | .region |
158 | 46 | .bytes() |
159 | 488 | .all46 (|b| b.is_ascii_lowercase() || b46 .is_ascii_digit46 () || b == b'-'46 ) |
160 | | { |
161 | 4 | return Err(make_err!(Code::InvalidArgument, "Invalid OSS region")); |
162 | 43 | } |
163 | 43 | let bucket = spec.bucket.as_bytes(); |
164 | 43 | if !(3..=63).contains(&bucket.len()) |
165 | 41 | || !bucket[0].is_ascii_alphanumeric() |
166 | 40 | || !bucket[bucket.len() - 1].is_ascii_alphanumeric() |
167 | 39 | || !bucket |
168 | 39 | .iter() |
169 | 407 | .all39 (|b| b.is_ascii_lowercase() || b39 .is_ascii_digit39 () || *b == b'-'39 ) |
170 | | { |
171 | 7 | return Err(make_err!(Code::InvalidArgument, "Invalid OSS bucket name")); |
172 | 36 | } |
173 | 36 | if spec.access_key_id.trim().is_empty() || spec.secret_access_key.trim()34 .is_empty34 () { |
174 | 3 | return Err(make_err!( |
175 | 3 | Code::InvalidArgument, |
176 | 3 | "OSS requires explicit access_key_id and secret_access_key" |
177 | 3 | )); |
178 | 33 | } |
179 | 33 | let concurrency = spec.multipart_max_concurrent_uploads.unwrap_or(2); |
180 | 33 | if concurrency == 0 || concurrency > tokio::sync::Semaphore::MAX_PERMITS32 { |
181 | 1 | return Err(make_err!( |
182 | 1 | Code::InvalidArgument, |
183 | 1 | "Invalid OSS multipart concurrency" |
184 | 1 | )); |
185 | 32 | } |
186 | 32 | Self::endpoint(spec)?13 ; |
187 | 19 | Ok(ExperimentalAwsSpec { |
188 | 19 | region: spec.region.clone(), |
189 | 19 | bucket: spec.bucket.clone(), |
190 | 19 | common: CommonObjectSpec { |
191 | 19 | key_prefix: spec.key_prefix.clone(), |
192 | 19 | retry: spec.retry.clone(), |
193 | 19 | multipart_max_concurrent_uploads: Some(concurrency), |
194 | 19 | ..Default::default() |
195 | 19 | }, |
196 | 19 | ..Default::default() |
197 | 19 | }) |
198 | 47 | } |
199 | | } |