storj 1.1.1

Native Rust Uplink client for the Storj decentralized object store
Documentation
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! Access grants: parse, serialize, share (restrict), and encryption-key override.

use std::sync::Arc;

use crate::encryption::EncryptionKey;
use crate::error::{Error, ErrorKind, Result};
use crate::types::{Config, require_encryption_prefix};

/// Parsed access grant. Cheap to clone (`Arc` internally).
/// 2025 name: `uplink::access::Grant`.
#[derive(Clone)]
pub struct Access {
    inner: Arc<storj_access::Grant>,
}

impl std::fmt::Debug for Access {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Access")
            .field("satellite_address", &self.satellite_address())
            .field("api_key", &"[REDACTED]")
            .finish()
    }
}

impl Access {
    /// Parse a serialized grant (`base58check` protobuf Scope).
    /// 2025: `Grant::new`.
    pub fn parse(serialized: &str) -> Result<Self> {
        let grant = storj_access::Grant::parse(serialized).map_err(|e| {
            Error::new(ErrorKind::InvalidGrant, e.message().to_owned()).with_source(e)
        })?;
        Ok(Self {
            inner: Arc::new(grant),
        })
    }

    /// Serialize for storage or `Share` distribution.
    ///
    /// Unmodified grants return the original string so unknown protobuf fields
    /// are preserved. `share` / `override_encryption_key` re-encode from fields
    /// (and may drop unknown fields, same as Go).
    pub fn serialize(&self) -> Result<String> {
        self.inner
            .serialize()
            .map_err(|e| Error::new(ErrorKind::InvalidGrant, e.message().to_owned()).with_source(e))
    }

    /// Satellite NodeURL, e.g. `12EayRS2…@us1.storj.io:7777`.
    pub fn satellite_address(&self) -> &str {
        self.inner.satellite_addr()
    }

    /// Restrict permissions and (optionally) path prefixes.
    /// Intersection with existing caveats; cannot widen.
    ///
    /// `allow_lock` is mapped onto the granular Object Lock bits (Go v1.14
    /// `Share`); the deprecated coarse lock flag is not granted.
    pub fn share(&self, permission: Permission, prefixes: &[SharePrefix]) -> Result<Self> {
        let mut grant_perm = to_grant_permission(&permission);
        if permission.allow_lock {
            grant_perm.allow_put_object_retention = true;
            grant_perm.allow_get_object_retention = true;
            grant_perm.allow_put_bucket_object_lock_configuration = true;
            grant_perm.allow_get_bucket_object_lock_configuration = true;
        }
        let prefixes: Vec<storj_access::SharePrefix> = prefixes
            .iter()
            .map(|p| storj_access::SharePrefix {
                bucket: p.bucket.clone(),
                prefix: p.prefix.clone(),
            })
            .collect();
        let grant = self
            .inner
            .restrict(&grant_perm, &prefixes)
            .map_err(map_grant_err)?;
        Ok(Self {
            inner: Arc::new(grant),
        })
    }

    /// Multitenancy: replace the encryption key for `bucket/prefix/`.
    /// `prefix` must end with `/` (same as 2025 and Go).
    pub fn override_encryption_key(
        &mut self,
        bucket: &str,
        prefix: &str,
        key: &EncryptionKey,
    ) -> Result<()> {
        require_encryption_prefix(prefix)?;
        let grant = Arc::make_mut(&mut self.inner);
        grant
            .override_encryption_key(bucket, prefix, key.as_bytes())
            .map_err(map_grant_err)
    }

    /// CPU-heavy (Argon2id t=1, m=64MiB, **p=8**). Talks to satellite `ProjectInfo`.
    pub async fn request_with_passphrase(
        satellite_address: &str,
        api_key: &str,
        passphrase: &str,
    ) -> Result<Self> {
        Self::request_with_passphrase_and_config(
            &Config::default(),
            satellite_address,
            api_key,
            passphrase,
        )
        .await
    }

    /// Same as [`Self::request_with_passphrase`] with an explicit config.
    pub async fn request_with_passphrase_and_config(
        config: &Config,
        satellite_address: &str,
        api_key: &str,
        passphrase: &str,
    ) -> Result<Self> {
        let parsed = storj_access::ApiKey::parse(api_key).map_err(|e| {
            Error::new(ErrorKind::InvalidGrant, e.message().to_owned()).with_source(e)
        })?;
        let node = crate::metainfo::parse_satellite_url(satellite_address)?;
        let client =
            crate::metainfo::MetainfoClient::connect(node.clone(), parsed.serialize_raw(), config)
                .await?;
        let info = client.project_info().await?;
        client.close().await;

        let passphrase = passphrase.to_owned();
        let salt = info.project_salt;
        let key = tokio::task::spawn_blocking(move || {
            crate::encryption::derive_root_key(
                passphrase.as_bytes(),
                &salt,
                b"",
                crate::constants::ARGON2_PARALLELISM_REQUEST,
            )
        })
        .await??;

        let mut default_key = [0u8; 32];
        default_key.copy_from_slice(key.as_bytes());
        // Go `EncryptionAccess.LimitTo`: drop the root key when the API key
        // already has path caveats so the grant cannot decrypt outside them.
        let enc = storj_access::EncryptionAccess {
            default_key: Some(default_key),
            default_path_cipher: storj_access::CipherSuite::AES_GCM,
            store_entries: Vec::new(),
            default_encryption_parameters: None,
        }
        .limit_to(&parsed)
        .map_err(map_grant_err)?;
        Ok(Self::from_grant(storj_access::Grant::from_parts(
            node.to_string(),
            parsed.serialize_raw(),
            enc,
        )))
    }

    pub(crate) fn from_grant(grant: storj_access::Grant) -> Self {
        Self {
            inner: Arc::new(grant),
        }
    }

    pub(crate) fn api_key_raw(&self) -> &[u8] {
        self.inner.api_key()
    }

    pub(crate) fn grant(&self) -> &storj_access::Grant {
        &self.inner
    }

    #[cfg(test)]
    pub(crate) fn placeholder(satellite_address: impl Into<String>) -> Self {
        Self {
            inner: Arc::new(storj_access::Grant::from_parts(
                satellite_address.into(),
                Vec::new(),
                storj_access::EncryptionAccess {
                    default_key: Some([1u8; 32]),
                    ..Default::default()
                },
            )),
        }
    }
}

fn to_grant_permission(p: &Permission) -> storj_access::Permission {
    storj_access::Permission {
        allow_download: p.allow_download,
        allow_upload: p.allow_upload,
        allow_list: p.allow_list,
        allow_delete: p.allow_delete,
        allow_lock: false,
        allow_put_object_retention: p.allow_put_object_retention,
        allow_get_object_retention: p.allow_get_object_retention,
        allow_put_object_legal_hold: p.allow_put_object_legal_hold,
        allow_get_object_legal_hold: p.allow_get_object_legal_hold,
        allow_bypass_governance_retention: p.allow_bypass_governance_retention,
        allow_put_bucket_object_lock_configuration: p.allow_put_bucket_object_lock_configuration,
        allow_get_bucket_object_lock_configuration: p.allow_get_bucket_object_lock_configuration,
        not_before: p.not_before,
        not_after: p.not_after,
        max_object_ttl: p.max_object_ttl,
    }
}

fn map_grant_err(e: storj_access::Error) -> Error {
    Error::new(ErrorKind::InvalidGrant, e.message().to_owned()).with_source(e)
}

/// Unencrypted share prefix. Encryption info is derived up to the last `/`.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct SharePrefix {
    /// Bucket name (plaintext).
    pub bucket: String,
    /// Unencrypted object-key prefix. Empty = whole bucket.
    pub prefix: String,
}

impl SharePrefix {
    /// Whole-bucket prefix (2025 `SharePrefix::full_bucket`).
    pub fn full_bucket(bucket: impl Into<String>) -> Self {
        Self {
            bucket: bucket.into(),
            prefix: String::new(),
        }
    }

    /// Prefix inside a bucket. Non-empty `prefix` must end with `/`.
    pub fn new(bucket: impl Into<String>, prefix: impl Into<String>) -> Result<Self> {
        let prefix = prefix.into();
        crate::types::require_trailing_slash_if_nonempty("prefix", &prefix)?;
        Ok(Self {
            bucket: bucket.into(),
            prefix,
        })
    }
}

/// Permission bits for `Access::share`.
///
/// `full()` matches Go `FullPermission()`: four CRUD allows **plus** all
/// granular Object Lock / legal-hold / bypass-governance bits. Does **not**
/// set deprecated `allow_lock` (Go same). Does **not** grant bucket
/// notification configuration.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Permission {
    /// Download object content and metadata.
    pub allow_download: bool,
    /// Create buckets and upload objects.
    pub allow_upload: bool,
    /// List buckets / objects.
    pub allow_list: bool,
    /// Delete buckets / objects.
    pub allow_delete: bool,
    /// Deprecated in Go; `share()` maps this onto the granular lock bits.
    pub allow_lock: bool,
    /// Put object retention.
    pub allow_put_object_retention: bool,
    /// Get object retention.
    pub allow_get_object_retention: bool,
    /// Put object legal hold.
    pub allow_put_object_legal_hold: bool,
    /// Get object legal hold.
    pub allow_get_object_legal_hold: bool,
    /// Bypass governance-mode retention (still requires the request to ask).
    pub allow_bypass_governance_retention: bool,
    /// Put bucket Object Lock configuration.
    pub allow_put_bucket_object_lock_configuration: bool,
    /// Get bucket Object Lock configuration.
    pub allow_get_bucket_object_lock_configuration: bool,
    /// Grant not valid before this time.
    pub not_before: Option<std::time::SystemTime>,
    /// Grant not valid after this time.
    pub not_after: Option<std::time::SystemTime>,
    /// Max TTL for newly uploaded objects.
    pub max_object_ttl: Option<std::time::Duration>,
}

impl Permission {
    /// Matches Go `FullPermission()`.
    pub fn full() -> Self {
        Self {
            allow_download: true,
            allow_upload: true,
            allow_list: true,
            allow_delete: true,
            allow_lock: false,
            allow_put_object_retention: true,
            allow_get_object_retention: true,
            allow_put_object_legal_hold: true,
            allow_get_object_legal_hold: true,
            allow_bypass_governance_retention: true,
            allow_put_bucket_object_lock_configuration: true,
            allow_get_bucket_object_lock_configuration: true,
            not_before: None,
            not_after: None,
            max_object_ttl: None,
        }
    }

    /// Download + list.
    pub fn read_only() -> Self {
        Self {
            allow_download: true,
            allow_list: true,
            ..Self::default()
        }
    }

    /// Upload + delete.
    pub fn write_only() -> Self {
        Self {
            allow_upload: true,
            allow_delete: true,
            ..Self::default()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_empty_is_invalid_grant() {
        let e = Access::parse("").unwrap_err();
        assert_eq!(e.kind(), ErrorKind::InvalidGrant);
    }

    #[test]
    fn parse_garbage_is_invalid_grant() {
        let e = Access::parse("12abcNotARealGrant").unwrap_err();
        assert_eq!(e.kind(), ErrorKind::InvalidGrant);
        assert!(!e.to_string().contains("not implemented"));
    }

    #[test]
    fn override_requires_trailing_slash() {
        let mut access = Access::placeholder("12id@us1.storj.io:7777");
        let key = EncryptionKey::from_bytes([2u8; 32]);
        let e = access
            .override_encryption_key("app", "user1", &key)
            .unwrap_err();
        assert_eq!(e.kind(), ErrorKind::ObjectKeyInvalid);

        access
            .override_encryption_key("app", "user1/", &key)
            .expect("trailing slash is sufficient");
    }

    #[test]
    fn share_prefix_constructor() {
        assert!(SharePrefix::new("b", "p").is_err());
        assert!(SharePrefix::new("b", "p/").is_ok());
        assert_eq!(SharePrefix::full_bucket("b").prefix, "");
    }

    #[test]
    fn full_permission_matches_go() {
        let p = Permission::full();
        assert!(p.allow_download && p.allow_upload && p.allow_list && p.allow_delete);
        assert!(
            !p.allow_lock,
            "Go FullPermission does not set deprecated AllowLock"
        );
        assert!(p.allow_put_object_retention);
        assert!(p.allow_get_object_retention);
        assert!(p.allow_put_object_legal_hold);
        assert!(p.allow_get_object_legal_hold);
        assert!(p.allow_bypass_governance_retention);
        assert!(p.allow_put_bucket_object_lock_configuration);
        assert!(p.allow_get_bucket_object_lock_configuration);
    }

    #[test]
    fn read_only_and_write_only() {
        let r = Permission::read_only();
        assert!(r.allow_download && r.allow_list);
        assert!(!r.allow_upload && !r.allow_delete);
        assert!(!r.allow_put_object_retention);

        let w = Permission::write_only();
        assert!(w.allow_upload && w.allow_delete);
        assert!(!w.allow_download && !w.allow_list);
    }

    #[test]
    fn debug_redacts_api_key() {
        let a = Access::placeholder("sat");
        let s = format!("{a:?}");
        assert!(s.contains("REDACTED"));
    }

    #[tokio::test]
    async fn request_host_only_unknown_needs_node_id() {
        let e = Access::request_with_passphrase("us1.storj.io:7777", "not-a-key", "pw")
            .await
            .unwrap_err();
        assert_eq!(e.kind(), ErrorKind::InvalidGrant);
        // API key is parsed first; this still fails before dial.
        assert!(
            e.to_string().contains("invalid api key")
                || e.to_string().contains("node id is required")
        );
    }

    #[tokio::test]
    async fn request_invalid_api_key() {
        let e = Access::request_with_passphrase(
            "12EayRS2V1kEsWESU9QMRseFhdxYxKicsiFmxrsLZHeLUtdps3S@127.0.0.1:1",
            "not-a-key",
            "pw",
        )
        .await
        .unwrap_err();
        assert_eq!(e.kind(), ErrorKind::InvalidGrant);
        assert!(e.to_string().contains("invalid api key"));
    }

    #[tokio::test]
    async fn request_unknown_host_reports_node_id_required() {
        let key = storj_access::ApiKey::from_parts(b"head".to_vec(), &[0x11; 32]).serialize();
        let e = Access::request_with_passphrase("us1.storj.io:7777", &key, "pw")
            .await
            .unwrap_err();
        assert_eq!(e.kind(), ErrorKind::InvalidGrant);
        assert!(
            e.to_string()
                .contains("node id is required in satelliteNodeURL")
        );
    }
}