Skip to main content

lux_lib/upload/
mod.rs

1use std::{env, io};
2
3use crate::operations::SearchAndDownloadError;
4use crate::package::SpecRevIterator;
5use crate::progress::{Progress, ProgressBar};
6use crate::project::project_toml::RemoteProjectTomlValidationError;
7use crate::remote_package_db::RemotePackageDB;
8use crate::rockspec::Rockspec;
9use crate::TOOL_VERSION;
10use crate::{config::Config, project::Project};
11
12use bon::Builder;
13use reqwest::StatusCode;
14use reqwest::{
15    multipart::{Form, Part},
16    Client,
17};
18use serde::Deserialize;
19use serde_enum_str::Serialize_enum_str;
20use thiserror::Error;
21use url::Url;
22
23#[cfg(feature = "gpgme")]
24use gpgme::{Context, Data};
25#[cfg(feature = "gpgme")]
26use std::io::Read;
27
28/// A rocks package uploader, providing fine-grained control
29/// over how a package should be uploaded.
30#[derive(Builder)]
31#[builder(start_fn = new, finish_fn(name = _build, vis = ""))]
32pub struct ProjectUpload<'a> {
33    project: Project,
34    api_key: Option<ApiKey>,
35    #[cfg(feature = "gpgme")]
36    sign_protocol: SignatureProtocol,
37    config: &'a Config,
38    progress: &'a Progress<ProgressBar>,
39    package_db: &'a RemotePackageDB,
40}
41
42impl<State> ProjectUploadBuilder<'_, State>
43where
44    State: project_upload_builder::State + project_upload_builder::IsComplete,
45{
46    /// Upload a package to a luarocks server.
47    pub async fn upload_to_luarocks(self) -> Result<(), UploadError> {
48        let args = self._build();
49        upload_from_project(args).await
50    }
51}
52
53#[derive(Deserialize, Debug)]
54pub struct VersionCheckResponse {
55    version: String,
56}
57
58#[derive(Error, Debug)]
59pub enum ToolCheckError {
60    #[error("error parsing tool check URL:\n{0}")]
61    ParseError(#[from] url::ParseError),
62    #[error("error sending HTTP request:\n{0}")]
63    Request(#[from] reqwest::Error),
64    #[error(r#"`lux` is out of date with {0}'s expected tool version.
65    `lux` is at version {TOOL_VERSION}, server is at {server_version}"#, server_version = _1.version)]
66    ToolOutdated(String, VersionCheckResponse),
67}
68
69#[derive(Error, Debug)]
70pub enum UserCheckError {
71    #[error("error parsing user check URL:\n{0}")]
72    ParseError(#[from] url::ParseError),
73    #[error(transparent)]
74    Request(#[from] reqwest::Error),
75    #[error("invalid API key provided")]
76    UserNotFound,
77    #[error("server {0} responded with error status: {1}")]
78    Server(Url, StatusCode),
79}
80
81#[derive(Error, Debug)]
82pub enum RockCheckError {
83    #[error("parse error while checking rock status on server:\n{0}")]
84    ParseError(#[from] url::ParseError),
85    #[error("HTTP request error while checking rock status on server:\n{0}")]
86    Request(#[from] reqwest::Error),
87}
88
89#[derive(Error, Debug)]
90#[error(transparent)]
91pub enum UploadError {
92    #[error("error parsing upload URL:\n{0}")]
93    ParseError(#[from] url::ParseError),
94    #[error("HTPP request error while uploading:\n{0}")]
95    Request(#[from] reqwest::Error),
96    #[error("server {0} responded with error status: {1}")]
97    Server(Url, StatusCode),
98    #[error("client error when requesting {0}\nStatus code: {1}")]
99    Client(Url, StatusCode),
100    RockCheck(#[from] RockCheckError),
101    #[error("a package with the same rockspec content already exists on the server: {0}")]
102    RockExists(Url),
103    #[error("unable to read rockspec: {0}")]
104    RockspecRead(#[from] std::io::Error),
105    #[cfg(feature = "gpgme")]
106    #[error(
107        r#"{0}.
108
109    HINT: Please ensure that a GPG agent is running and that a valid GPG signing key is registered.
110          If you'd like to skip the signing step, supply `--sign-protocol none`
111        "#
112    )]
113    Signature(#[from] gpgme::Error),
114    ToolCheck(#[from] ToolCheckError),
115    UserCheck(#[from] UserCheckError),
116    ApiKeyUnspecified(#[from] ApiKeyUnspecified),
117    ValidationError(#[from] RemoteProjectTomlValidationError),
118    #[error(
119        "unsupported version: `{0}`.\nLux can upload packages with a SemVer version, 'dev' or 'scm'"
120    )]
121    UnsupportedVersion(String),
122    #[error("{0}")] // We don't know the concrete error type
123    Rockspec(String),
124    #[error("the maximum supported number of rockspec revisions per version has been exceeded")]
125    MaxSpecRevsExceeded,
126    #[error("rock already exists on server. Error downloading existing rockspec:\n{0}")]
127    SearchAndDownload(#[from] SearchAndDownloadError),
128    #[error("error computing rockspec hash:\n{0}")]
129    Hash(io::Error),
130}
131
132pub struct ApiKey(String);
133
134#[derive(Error, Debug)]
135#[error("no API key provided! Please set the $LUX_API_KEY environment variable")]
136pub struct ApiKeyUnspecified;
137
138impl ApiKey {
139    /// Retrieves the rocks API key from the `$LUX_API_KEY` environment
140    /// variable and seals it in this struct.
141    pub fn new() -> Result<Self, ApiKeyUnspecified> {
142        Ok(Self(
143            env::var("LUX_API_KEY").map_err(|_| ApiKeyUnspecified)?,
144        ))
145    }
146
147    /// Creates an API key from a String.
148    ///
149    /// # Safety
150    ///
151    /// This struct is designed to be sealed without a [`Display`](std::fmt::Display) implementation
152    /// so that it can never accidentally be printed.
153    ///
154    /// Ensure that you do not do anything else with the API key string prior to sealing it in this
155    /// struct.
156    pub unsafe fn from(str: String) -> Self {
157        Self(str)
158    }
159
160    /// Retrieves the underlying API key as a [`String`].
161    ///
162    /// # Safety
163    ///
164    /// Strings may accidentally be printed as part of its [`Display`](std::fmt::Display)
165    /// implementation. Ensure that you never pass this variable somewhere it may be displayed.
166    pub unsafe fn get(&self) -> &String {
167        &self.0
168    }
169}
170
171#[derive(Serialize_enum_str, Clone, PartialEq, Eq)]
172#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
173#[cfg_attr(feature = "clap", clap(rename_all = "lowercase"))]
174#[serde(rename_all = "lowercase")]
175#[derive(Default)]
176#[cfg(not(feature = "gpgme"))]
177pub enum SignatureProtocol {
178    #[default]
179    None,
180}
181
182#[derive(Serialize_enum_str, Clone, PartialEq, Eq)]
183#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
184#[cfg_attr(feature = "clap", clap(rename_all = "lowercase"))]
185#[serde(rename_all = "lowercase")]
186#[derive(Default)]
187#[cfg(feature = "gpgme")]
188pub enum SignatureProtocol {
189    None,
190    Assuan,
191    CMS,
192    #[default]
193    Default,
194    G13,
195    GPGConf,
196    OpenPGP,
197    Spawn,
198    UIServer,
199}
200
201#[cfg(feature = "gpgme")]
202impl From<SignatureProtocol> for gpgme::Protocol {
203    fn from(val: SignatureProtocol) -> Self {
204        match val {
205            SignatureProtocol::Default => gpgme::Protocol::Default,
206            SignatureProtocol::OpenPGP => gpgme::Protocol::OpenPgp,
207            SignatureProtocol::CMS => gpgme::Protocol::Cms,
208            SignatureProtocol::GPGConf => gpgme::Protocol::GpgConf,
209            SignatureProtocol::Assuan => gpgme::Protocol::Assuan,
210            SignatureProtocol::G13 => gpgme::Protocol::G13,
211            SignatureProtocol::UIServer => gpgme::Protocol::UiServer,
212            SignatureProtocol::Spawn => gpgme::Protocol::Spawn,
213            SignatureProtocol::None => unreachable!(),
214        }
215    }
216}
217
218async fn upload_from_project(args: ProjectUpload<'_>) -> Result<(), UploadError> {
219    let project = args.project;
220    let api_key = args.api_key.unwrap_or(ApiKey::new()?);
221    #[cfg(feature = "gpgme")]
222    let protocol = args.sign_protocol;
223    let config = args.config;
224    let progress = args.progress;
225    let package_db = args.package_db;
226
227    let client = Client::builder().https_only(true).build()?;
228
229    helpers::ensure_tool_version(&client, config.server()).await?;
230    helpers::ensure_user_exists(&client, &api_key, config.server()).await?;
231
232    let (rockspec, rockspec_content) =
233        helpers::generate_rockspec(&project, &client, &api_key, config, progress, package_db)
234            .await?;
235
236    #[cfg(not(feature = "gpgme"))]
237    let signed: Option<String> = None;
238
239    #[cfg(feature = "gpgme")]
240    let signed = if let SignatureProtocol::None = protocol {
241        None
242    } else {
243        let mut ctx = Context::from_protocol(protocol.into())?;
244        let mut signature = Data::new()?;
245
246        ctx.set_armor(true);
247        ctx.sign_detached(rockspec_content.clone(), &mut signature)?;
248
249        let mut signature_str = String::new();
250        signature.read_to_string(&mut signature_str)?;
251
252        Some(signature_str)
253    };
254
255    let rockspec = Part::text(rockspec_content)
256        .file_name(format!(
257            "{}-{}.rockspec",
258            rockspec.package(),
259            rockspec.version()
260        ))
261        .mime_str("application/octet-stream")?;
262
263    let multipart = {
264        let multipart = Form::new().part("rockspec_file", rockspec);
265
266        match signed {
267            Some(signature) => {
268                let part = Part::text(signature).file_name("project.rockspec.sig");
269                multipart.part("rockspec_sig", part)
270            }
271            None => multipart,
272        }
273    };
274
275    let response = client
276        .post(unsafe { helpers::url_for_method(config.server(), &api_key, "upload")? })
277        .multipart(multipart)
278        .send()
279        .await?;
280
281    let status = response.status();
282    if status.is_client_error() {
283        Err(UploadError::Client(config.server().clone(), status))
284    } else if status.is_server_error() {
285        Err(UploadError::Server(config.server().clone(), status))
286    } else {
287        Ok(())
288    }
289}
290
291mod helpers {
292    use std::collections::HashMap;
293
294    use super::*;
295    use crate::hash::HasIntegrity;
296    use crate::operations::Download;
297    use crate::package::{PackageName, PackageSpec, PackageVersion};
298    use crate::project::project_toml::RemoteProjectToml;
299    use crate::upload::RockCheckError;
300    use crate::upload::{ToolCheckError, UserCheckError};
301    use reqwest::Client;
302    use ssri::Integrity;
303    use url::Url;
304
305    /// WARNING: This function is unsafe,
306    /// because it adds the unmasked API key to the URL.
307    /// When using URLs created by this function,
308    /// pay attention not to leak the API key in errors.
309    pub(crate) unsafe fn url_for_method(
310        server_url: &Url,
311        api_key: &ApiKey,
312        endpoint: &str,
313    ) -> Result<Url, url::ParseError> {
314        server_url
315            .join("api/1/")?
316            .join(&format!("{}/", api_key.get()))?
317            .join(endpoint)
318    }
319
320    pub(crate) async fn ensure_tool_version(
321        client: &Client,
322        server_url: &Url,
323    ) -> Result<(), ToolCheckError> {
324        let url = server_url.join("api/tool_version")?;
325        let response: VersionCheckResponse = client
326            .post(url)
327            .json(&("current", TOOL_VERSION))
328            .send()
329            .await?
330            .json()
331            .await?;
332
333        if response.version == TOOL_VERSION {
334            Ok(())
335        } else {
336            Err(ToolCheckError::ToolOutdated(
337                server_url.to_string(),
338                response,
339            ))
340        }
341    }
342
343    pub(crate) async fn ensure_user_exists(
344        client: &Client,
345        api_key: &ApiKey,
346        server_url: &Url,
347    ) -> Result<(), UserCheckError> {
348        let response = client
349            .get(unsafe { url_for_method(server_url, api_key, "status")? })
350            .send()
351            .await?;
352        let status = response.status();
353        if status.is_client_error() {
354            Err(UserCheckError::UserNotFound)
355        } else if status.is_server_error() {
356            Err(UserCheckError::Server(server_url.clone(), status))
357        } else {
358            Ok(())
359        }
360    }
361
362    pub(crate) async fn generate_rockspec(
363        project: &Project,
364        client: &Client,
365        api_key: &ApiKey,
366        config: &Config,
367        progress: &Progress<ProgressBar>,
368        package_db: &RemotePackageDB,
369    ) -> Result<(RemoteProjectToml, String), UploadError> {
370        for specrev in SpecRevIterator::new() {
371            let rockspec = project.toml().into_remote(Some(specrev))?;
372
373            let rockspec_content = rockspec
374                .to_lua_remote_rockspec_string()
375                .map_err(|err| UploadError::Rockspec(err.to_string()))?;
376
377            if let PackageVersion::StringVer(ver) = rockspec.version() {
378                return Err(UploadError::UnsupportedVersion(ver.to_string()));
379            }
380            if helpers::rock_exists(
381                client,
382                api_key,
383                rockspec.package(),
384                rockspec.version(),
385                config.server(),
386            )
387            .await?
388            {
389                let package =
390                    PackageSpec::new(rockspec.package().clone(), rockspec.version().clone());
391                let existing_rockspec = Download::new(&package.into(), config, progress)
392                    .package_db(package_db)
393                    .download_rockspec()
394                    .await?
395                    .rockspec;
396                let existing_rockspec_hash = existing_rockspec.hash().map_err(UploadError::Hash)?;
397                let rockspec_content_hash = Integrity::from(&rockspec_content);
398                if existing_rockspec_hash
399                    .matches(&rockspec_content_hash)
400                    .is_some()
401                {
402                    return Err(UploadError::RockExists(config.server().clone()));
403                }
404            } else {
405                return Ok((rockspec, rockspec_content));
406            }
407        }
408        Err(UploadError::MaxSpecRevsExceeded)
409    }
410
411    async fn rock_exists(
412        client: &Client,
413        api_key: &ApiKey,
414        name: &PackageName,
415        version: &PackageVersion,
416        server: &Url,
417    ) -> Result<bool, RockCheckError> {
418        let server_response_raw_json = client
419            .get(unsafe { url_for_method(server, api_key, "check_rockspec")? })
420            .query(&(
421                ("package", name.to_string()),
422                ("version", version.to_string()),
423            ))
424            .send()
425            .await?
426            .error_for_status()?
427            .text()
428            .await?;
429        let response_map: Option<HashMap<String, serde_json::Value>> =
430            serde_json::from_str(&server_response_raw_json).ok();
431        Ok(response_map.is_some_and(|response_map| {
432            response_map.contains_key("module") && response_map.contains_key("version")
433        }))
434    }
435}