inapt 0.3.4

A minimal Debian/Ubuntu APT repository proxy written in Rust. Exposes a valid APT repo structure over HTTP, sourcing .deb packages from GitHub Releases.
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
use chrono::{DateTime, Utc};

use super::entity::*;

pub trait AptRepositoryWriter: Send + Sync + 'static {
    fn synchronize(&self) -> impl Future<Output = anyhow::Result<()>> + Send;
}

/// Represents a logical APT repository (suite/component/arch).
pub trait AptRepositoryReader: Send + Sync + 'static {
    /// List all available packages for a given architecture.
    fn list_packages(
        &self,
        arch: &str,
    ) -> impl Future<Output = anyhow::Result<Vec<Package>>> + Send;

    /// Get the Release metadata for the repository.
    fn release_metadata(
        &self,
    ) -> impl Future<Output = anyhow::Result<Option<ReleaseMetadata>>> + Send;

    /// Get the InRelease document: the Release metadata as an OpenPGP cleartext-signed message.
    fn signed_release_metadata(
        &self,
    ) -> impl Future<Output = anyhow::Result<Option<String>>> + Send;

    /// Get the signature of the Release file.
    fn release_gpg_signature(&self) -> impl Future<Output = anyhow::Result<Option<String>>> + Send;

    /// Get the Packages file content for a given architecture.
    fn packages_file(&self, arch: &str) -> impl Future<Output = anyhow::Result<String>> + Send {
        async {
            let list = self.list_packages(arch).await?;
            Ok(list
                .into_iter()
                .map(|package| package.serialize().to_string())
                .collect::<Vec<_>>()
                .join("\n"))
        }
    }

    /// Get the Translation-en file content.
    /// Returns the translation entries for all unique packages across all architectures.
    fn translation_file(&self) -> impl Future<Output = anyhow::Result<String>> + Send;

    /// Get the package for a given package name and filename
    fn package(
        &self,
        name: &str,
        filename: &str,
    ) -> impl Future<Output = anyhow::Result<Option<Package>>> + Send;

    /// Find architecture metadata by hash (SHA256) for by-hash support.
    /// Returns the architecture name if the hash matches either plain or compressed Packages file.
    fn find_architecture_by_hash(
        &self,
        hash: &str,
    ) -> impl Future<Output = anyhow::Result<Option<ArchitectureHashMatch>>> + Send;
}

/// Represents a match found when looking up an architecture by hash.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ArchitectureHashMatch {
    /// The architecture name (e.g., "amd64", "arm64")
    pub architecture: String,
    /// Whether the hash matched the compressed (.gz) variant
    pub compressed: bool,
}

#[cfg(test)]
mockall::mock! {
    pub AptRepositoryService {}

    impl Clone for AptRepositoryService {
        fn clone(&self) -> Self;
    }

    impl AptRepositoryReader for AptRepositoryService {
        /// List all available packages for a given architecture.
        fn list_packages(
            &self,
            arch: &str,
        ) -> impl Future<Output = anyhow::Result<Vec<Package>>> + Send;

        /// Get the Release metadata for the repository.
        fn release_metadata(&self) -> impl Future<Output = anyhow::Result<Option<ReleaseMetadata>>> + Send;

        /// Get the InRelease document: the Release metadata as an OpenPGP cleartext-signed message.
        fn signed_release_metadata(&self) -> impl Future<Output = anyhow::Result<Option<String>>> + Send;

        /// Get the signature of the Release file.
        fn release_gpg_signature(&self) -> impl Future<Output = anyhow::Result<Option<String>>> + Send;

        /// Get the Packages file content for a given architecture.
        fn packages_file(&self, arch: &str) -> impl Future<Output = anyhow::Result<String>> + Send;

        /// Get the Translation-en file content.
        fn translation_file(&self) -> impl Future<Output = anyhow::Result<String>> + Send;

        /// Get the package for a given package name and filename
        fn package(
            &self,
            name: &str,
            filename: &str,
        ) -> impl Future<Output = anyhow::Result<Option<Package>>> + Send;

        /// Find architecture metadata by hash (SHA256) for by-hash support.
        fn find_architecture_by_hash(
            &self,
            hash: &str,
        ) -> impl Future<Output = anyhow::Result<Option<ArchitectureHashMatch>>> + Send;
    }
}

/// Extracts control metadata from a .deb file.
pub trait DebMetadataExtractor: Send + Sync + 'static {
    /// Given a .deb file, extract control fields.
    fn extract_metadata(
        &self,
        path: &std::path::Path,
    ) -> impl Future<Output = anyhow::Result<PackageMetadata>> + Send;
}

#[cfg(test)]
mockall::mock! {
    pub DebMetadataExtractor {}

    impl Clone for DebMetadataExtractor {
        fn clone(&self) -> Self;
    }

    impl DebMetadataExtractor for DebMetadataExtractor {
        fn extract_metadata(
            &self,
            path: &std::path::Path,
        ) -> impl Future<Output = anyhow::Result<PackageMetadata>> + Send;
    }
}

/// Abstracts the source of .deb packages (e.g., GitHub Releases).
pub trait PackageSource: Send + Sync + 'static {
    /// Download a .deb asset.
    fn fetch_deb(
        &self,
        asset: &DebAsset,
    ) -> impl Future<Output = anyhow::Result<temp_file::TempFile>> + Send;

    /// Stream releases with their .deb assets for incremental processing.
    fn stream_releases_with_assets(
        &self,
        repo: &str,
    ) -> impl Future<Output = anyhow::Result<Vec<ReleaseWithAssets>>> + Send;

    /// Download an .apk asset.
    fn fetch_apk(
        &self,
        asset: &ApkAsset,
    ) -> impl Future<Output = anyhow::Result<temp_file::TempFile>> + Send;

    /// Stream releases with their .apk assets for incremental processing.
    fn stream_apk_releases_with_assets(
        &self,
        repo: &str,
    ) -> impl Future<Output = anyhow::Result<Vec<ApkReleaseWithAssets>>> + Send;
}

#[cfg(test)]
mockall::mock! {
    pub PackageSource {}

    impl Clone for PackageSource {
        fn clone(&self) -> Self;
    }

    impl PackageSource for PackageSource {
        fn fetch_deb(
            &self,
            asset: &DebAsset,
        ) -> impl Future<Output = anyhow::Result<temp_file::TempFile>> + Send;
        fn stream_releases_with_assets(
            &self,
            repo: &str,
        ) -> impl Future<Output = anyhow::Result<Vec<ReleaseWithAssets>>> + Send;
        fn fetch_apk(
            &self,
            asset: &ApkAsset,
        ) -> impl Future<Output = anyhow::Result<temp_file::TempFile>> + Send;
        fn stream_apk_releases_with_assets(
            &self,
            repo: &str,
        ) -> impl Future<Output = anyhow::Result<Vec<ApkReleaseWithAssets>>> + Send;
    }
}

/// Caches package metadata and assets.
pub trait ReleaseStore: Send + Sync + 'static {
    fn insert_release(&self, entry: ReleaseMetadata) -> impl Future<Output = ()> + Send;
    fn find_latest_release(&self) -> impl Future<Output = Option<ReleaseMetadata>> + Send;
}

#[cfg(test)]
mockall::mock! {
    pub ReleaseStore {}

    impl Clone for ReleaseStore {
        fn clone(&self) -> Self;
    }

    impl ReleaseStore for ReleaseStore {
        fn insert_release(&self, entry: ReleaseMetadata) -> impl Future<Output = ()> + Send;
        fn find_latest_release(&self) -> impl Future<Output = Option<ReleaseMetadata>> + Send;
    }
}

/// Identifies a GitHub release by its owner, name, and ID.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ReleaseIdentifier {
    pub repo_owner: String,
    pub repo_name: String,
    pub release_id: u64,
}

/// Tracks which GitHub releases have been scanned.
pub trait ReleaseTracker: Send + Sync + 'static {
    /// Check which releases from a batch have already been scanned.
    /// Returns a set of release IDs that have been scanned.
    fn filter_scanned_releases(
        &self,
        releases: &[ReleaseIdentifier],
    ) -> impl Future<Output = anyhow::Result<std::collections::HashSet<u64>>> + Send;

    /// Mark multiple releases as scanned in a single batch operation.
    fn mark_releases_scanned(
        &self,
        releases: &[ReleaseIdentifier],
    ) -> impl Future<Output = anyhow::Result<()>> + Send;
}

#[cfg(test)]
mockall::mock! {
    pub ReleaseTracker {}

    impl Clone for ReleaseTracker {
        fn clone(&self) -> Self;
    }

    impl ReleaseTracker for ReleaseTracker {
        fn filter_scanned_releases(
            &self,
            releases: &[ReleaseIdentifier],
        ) -> impl Future<Output = anyhow::Result<std::collections::HashSet<u64>>> + Send;

        fn mark_releases_scanned(
            &self,
            releases: &[ReleaseIdentifier],
        ) -> impl Future<Output = anyhow::Result<()>> + Send;
    }
}

/// Stores individual packages for incremental updates.
pub trait PackageStore: Send + Sync + 'static {
    /// Insert multiple packages into storage in a single batch operation.
    fn insert_packages(
        &self,
        packages: &[Package],
    ) -> impl Future<Output = anyhow::Result<()>> + Send;

    /// Find a package by its asset ID.
    fn find_package_by_asset_id(
        &self,
        asset_id: u64,
    ) -> impl Future<Output = Option<Package>> + Send;

    /// Get all packages for building ReleaseMetadata.
    fn list_all_packages(&self) -> impl Future<Output = anyhow::Result<Vec<Package>>> + Send;
}

#[cfg(test)]
mockall::mock! {
    pub PackageStore {}

    impl Clone for PackageStore {
        fn clone(&self) -> Self;
    }

    impl PackageStore for PackageStore {
        fn insert_packages(
            &self,
            packages: &[Package],
        ) -> impl Future<Output = anyhow::Result<()>> + Send;

        fn find_package_by_asset_id(
            &self,
            asset_id: u64,
        ) -> impl Future<Output = Option<Package>> + Send;

        fn list_all_packages(&self) -> impl Future<Output = anyhow::Result<Vec<Package>>> + Send;
    }
}

pub trait Clock: Send + Sync + 'static {
    fn now() -> DateTime<Utc>;
}

impl Clock for chrono::Utc {
    fn now() -> DateTime<Utc> {
        Utc::now()
    }
}

#[cfg(test)]
mockall::mock! {
    pub Clock {}

    impl Clock for Clock {
        fn now() -> chrono::DateTime<chrono::Utc>;
    }
}

pub trait PGPCipher: Send + Sync + 'static {
    fn sign(&self, data: &str) -> anyhow::Result<String>;

    /// Produce a full OpenPGP Cleartext Signature Framework document
    /// (BEGIN PGP SIGNED MESSAGE header, Hash armor header, dash-escaped
    /// body, and the armored signature) for the given data.
    fn sign_cleartext(&self, data: &str) -> anyhow::Result<String>;
}

#[cfg(test)]
mockall::mock! {
    pub PGPCipher {}

    impl PGPCipher for PGPCipher {
        fn sign(&self, data: &str) -> anyhow::Result<String>;
        fn sign_cleartext(&self, data: &str) -> anyhow::Result<String>;
    }
}

/// Synchronizes APK packages from upstream sources.
pub trait ApkRepositoryWriter: Send + Sync + 'static {
    fn synchronize(&self) -> impl Future<Output = anyhow::Result<()>> + Send;
}

/// Serves an APK repository (APKINDEX and package lookups).
pub trait ApkRepositoryReader: Send + Sync + 'static {
    /// Get the signed APKINDEX.tar.gz content for a given architecture.
    fn apk_index(&self, arch: &str) -> impl Future<Output = anyhow::Result<Vec<u8>>> + Send;

    /// Find an APK package by architecture and filename for download redirect.
    fn apk_package(
        &self,
        arch: &str,
        filename: &str,
    ) -> impl Future<Output = anyhow::Result<Option<ApkPackage>>> + Send;
}

#[cfg(test)]
mockall::mock! {
    pub ApkRepositoryService {}

    impl Clone for ApkRepositoryService {
        fn clone(&self) -> Self;
    }

    impl ApkRepositoryReader for ApkRepositoryService {
        fn apk_index(&self, arch: &str) -> impl Future<Output = anyhow::Result<Vec<u8>>> + Send;

        fn apk_package(
            &self,
            arch: &str,
            filename: &str,
        ) -> impl Future<Output = anyhow::Result<Option<ApkPackage>>> + Send;
    }
}

/// Extracts metadata from an `.apk` file's `.PKGINFO`.
pub trait ApkMetadataExtractor: Send + Sync + 'static {
    /// Given an `.apk` file, extract package metadata.
    fn extract_metadata(
        &self,
        path: &std::path::Path,
    ) -> impl Future<Output = anyhow::Result<ApkMetadata>> + Send;
}

#[cfg(test)]
mockall::mock! {
    pub ApkMetadataExtractor {}

    impl Clone for ApkMetadataExtractor {
        fn clone(&self) -> Self;
    }

    impl ApkMetadataExtractor for ApkMetadataExtractor {
        fn extract_metadata(
            &self,
            path: &std::path::Path,
        ) -> impl Future<Output = anyhow::Result<ApkMetadata>> + Send;
    }
}

/// Stores APK packages for incremental updates.
pub trait ApkPackageStore: Send + Sync + 'static {
    /// Insert multiple APK packages in a single batch operation.
    fn insert_apk_packages(
        &self,
        packages: &[ApkPackage],
    ) -> impl Future<Output = anyhow::Result<()>> + Send;

    /// Find an APK package by its GitHub asset ID.
    fn find_apk_package_by_asset_id(
        &self,
        asset_id: u64,
    ) -> impl Future<Output = anyhow::Result<Option<ApkPackage>>> + Send;

    /// Get all APK packages for building the APKINDEX.
    fn list_all_apk_packages(&self)
    -> impl Future<Output = anyhow::Result<Vec<ApkPackage>>> + Send;
}

#[cfg(test)]
mockall::mock! {
    pub ApkPackageStore {}

    impl Clone for ApkPackageStore {
        fn clone(&self) -> Self;
    }

    impl ApkPackageStore for ApkPackageStore {
        fn insert_apk_packages(
            &self,
            packages: &[ApkPackage],
        ) -> impl Future<Output = anyhow::Result<()>> + Send;

        fn find_apk_package_by_asset_id(
            &self,
            asset_id: u64,
        ) -> impl Future<Output = anyhow::Result<Option<ApkPackage>>> + Send;

        fn list_all_apk_packages(
            &self,
        ) -> impl Future<Output = anyhow::Result<Vec<ApkPackage>>> + Send;
    }
}

/// Signs data with an RSA key for APK repository index signing.
pub trait RsaSigner: Send + Sync + 'static {
    /// Sign raw bytes and return the RSA signature.
    fn sign(&self, data: &[u8]) -> anyhow::Result<Vec<u8>>;

    /// The key filename used in the `.SIGN.RSA.{name}` tar entry.
    fn key_name(&self) -> &str;
}

#[cfg(test)]
mockall::mock! {
    pub RsaSigner {}

    impl RsaSigner for RsaSigner {
        fn sign(&self, data: &[u8]) -> anyhow::Result<Vec<u8>>;
        fn key_name(&self) -> &str;
    }
}