snix_store/proto/
mod.rs

1#![allow(clippy::derive_partial_eq_without_eq, non_snake_case)]
2use bstr::ByteSlice;
3use bytes::Bytes;
4use data_encoding::BASE64;
5// https://github.com/hyperium/tonic/issues/1056
6use nix_compat::{
7    narinfo::{Signature, SignatureError},
8    nixhash::{CAHash, NixHash},
9    store_path::{self, StorePathRef},
10};
11use snix_castore::DirectoryError;
12use thiserror::Error;
13
14mod grpc_pathinfoservice_wrapper;
15
16pub use grpc_pathinfoservice_wrapper::GRPCPathInfoServiceWrapper;
17
18tonic::include_proto!("snix.store.v1");
19
20use snix_castore::proto as castorepb;
21
22#[cfg(feature = "tonic-reflection")]
23/// Compiled file descriptors for implementing [gRPC
24/// reflection](https://github.com/grpc/grpc/blob/master/doc/server-reflection.md) with e.g.
25/// [`tonic_reflection`](https://docs.rs/tonic-reflection).
26pub const FILE_DESCRIPTOR_SET: &[u8] = tonic::include_file_descriptor_set!("snix.store.v1");
27
28#[cfg(test)]
29mod tests;
30
31/// Errors that can occur during the validation of PathInfo messages.
32#[derive(Debug, Error, PartialEq)]
33pub enum ValidatePathInfoError {
34    /// Invalid length of a reference
35    #[error("Invalid length of digest at position {}, expected {}, got {}", .0, store_path::DIGEST_SIZE, .1)]
36    InvalidReferenceDigestLen(usize, usize),
37
38    /// No entry present
39    #[error("No entry present")]
40    NoEntryPresent,
41
42    /// Node fails validation
43    #[error("Invalid root node: {:?}", .0.to_string())]
44    InvalidRootNode(DirectoryError),
45
46    /// Invalid node name encountered. Root nodes in PathInfos have more strict name requirements
47    #[error("Failed to parse {} as StorePath: {}", .0.to_str_lossy(), .1)]
48    InvalidNodeName(Vec<u8>, store_path::Error),
49
50    /// The digest in narinfo.nar_sha256 has an invalid len.
51    #[error("Invalid narinfo.nar_sha256 length: expected {}, got {}", 32, .0)]
52    InvalidNarSha256DigestLen(usize),
53
54    /// The number of references in the narinfo.reference_names field does not match
55    /// the number of references in the .references field.
56    #[error("Inconsistent Number of References: {0} (references) vs {1} (narinfo)")]
57    InconsistentNumberOfReferences(usize, usize),
58
59    /// A string in narinfo.reference_names does not parse to a [store_path::StorePath].
60    #[error("Invalid reference_name at position {0}: {1}")]
61    InvalidNarinfoReferenceName(usize, String),
62
63    /// The digest in the parsed `.narinfo.reference_names[i]` does not match
64    /// the one in `.references[i]`.`
65    #[error("digest in reference_name at position {} does not match digest in PathInfo, expected {}, got {}", .0, BASE64.encode(.1), BASE64.encode(.2))]
66    InconsistentNarinfoReferenceNameDigest(
67        usize,
68        [u8; store_path::DIGEST_SIZE],
69        [u8; store_path::DIGEST_SIZE],
70    ),
71
72    /// The deriver field is invalid.
73    #[error("deriver field is invalid: {0}")]
74    InvalidDeriverField(store_path::Error),
75
76    /// The narinfo field is missing
77    #[error("The narinfo field is missing")]
78    NarInfoFieldMissing,
79
80    /// The ca field is invalid
81    #[error("The ca field is invalid: {0}")]
82    InvalidCaField(ConvertCAError),
83
84    /// The signature at position is invalid
85    #[error("The signature at position {0} is invalid: {1}")]
86    InvalidSignature(usize, SignatureError),
87}
88
89/// Errors that can occur when converting from a [nar_info::Ca] to a (stricter)
90/// [nix_compat::nixhash::CAHash].
91#[derive(Debug, Error, PartialEq)]
92pub enum ConvertCAError {
93    /// Invalid length of a reference
94    #[error("Invalid digest length '{0}' for type {1}")]
95    InvalidReferenceDigestLen(usize, &'static str),
96    /// Unknown Hash type
97    #[error("Unknown hash type: {0}")]
98    UnknownHashType(i32),
99}
100
101impl TryFrom<&nar_info::Ca> for nix_compat::nixhash::CAHash {
102    type Error = ConvertCAError;
103
104    fn try_from(value: &nar_info::Ca) -> Result<Self, Self::Error> {
105        Ok(match value.r#type {
106            typ if typ == nar_info::ca::Hash::NarSha256 as i32 => {
107                Self::Nar(NixHash::Sha256(value.digest[..].try_into().map_err(
108                    |_| ConvertCAError::InvalidReferenceDigestLen(value.digest.len(), "NarSha256"),
109                )?))
110            }
111            typ if typ == nar_info::ca::Hash::NarSha1 as i32 => {
112                Self::Nar(NixHash::Sha1(value.digest[..].try_into().map_err(
113                    |_| ConvertCAError::InvalidReferenceDigestLen(value.digest.len(), "NarSha1"),
114                )?))
115            }
116            typ if typ == nar_info::ca::Hash::NarSha512 as i32 => Self::Nar(NixHash::Sha512(
117                Box::new(value.digest[..].try_into().map_err(|_| {
118                    ConvertCAError::InvalidReferenceDigestLen(value.digest.len(), "NarSha512")
119                })?),
120            )),
121            typ if typ == nar_info::ca::Hash::NarMd5 as i32 => {
122                Self::Nar(NixHash::Md5(value.digest[..].try_into().map_err(|_| {
123                    ConvertCAError::InvalidReferenceDigestLen(value.digest.len(), "NarMd5")
124                })?))
125            }
126            typ if typ == nar_info::ca::Hash::TextSha256 as i32 => {
127                Self::Text(value.digest[..].try_into().map_err(|_| {
128                    ConvertCAError::InvalidReferenceDigestLen(value.digest.len(), "TextSha256")
129                })?)
130            }
131            typ if typ == nar_info::ca::Hash::FlatSha1 as i32 => {
132                Self::Flat(NixHash::Sha1(value.digest[..].try_into().map_err(
133                    |_| ConvertCAError::InvalidReferenceDigestLen(value.digest.len(), "FlatSha1"),
134                )?))
135            }
136            typ if typ == nar_info::ca::Hash::FlatMd5 as i32 => {
137                Self::Flat(NixHash::Md5(value.digest[..].try_into().map_err(|_| {
138                    ConvertCAError::InvalidReferenceDigestLen(value.digest.len(), "FlatMd5")
139                })?))
140            }
141            typ if typ == nar_info::ca::Hash::FlatSha256 as i32 => {
142                Self::Flat(NixHash::Sha256(value.digest[..].try_into().map_err(
143                    |_| ConvertCAError::InvalidReferenceDigestLen(value.digest.len(), "FlatSha256"),
144                )?))
145            }
146            typ if typ == nar_info::ca::Hash::FlatSha512 as i32 => Self::Flat(NixHash::Sha512(
147                Box::new(value.digest[..].try_into().map_err(|_| {
148                    ConvertCAError::InvalidReferenceDigestLen(value.digest.len(), "FlatSha512")
149                })?),
150            )),
151            typ => return Err(ConvertCAError::UnknownHashType(typ)),
152        })
153    }
154}
155
156impl From<&nix_compat::nixhash::CAHash> for nar_info::ca::Hash {
157    fn from(value: &nix_compat::nixhash::CAHash) -> Self {
158        match value {
159            CAHash::Flat(NixHash::Md5(_)) => nar_info::ca::Hash::FlatMd5,
160            CAHash::Flat(NixHash::Sha1(_)) => nar_info::ca::Hash::FlatSha1,
161            CAHash::Flat(NixHash::Sha256(_)) => nar_info::ca::Hash::FlatSha256,
162            CAHash::Flat(NixHash::Sha512(_)) => nar_info::ca::Hash::FlatSha512,
163            CAHash::Nar(NixHash::Md5(_)) => nar_info::ca::Hash::NarMd5,
164            CAHash::Nar(NixHash::Sha1(_)) => nar_info::ca::Hash::NarSha1,
165            CAHash::Nar(NixHash::Sha256(_)) => nar_info::ca::Hash::NarSha256,
166            CAHash::Nar(NixHash::Sha512(_)) => nar_info::ca::Hash::NarSha512,
167            CAHash::Text(_) => nar_info::ca::Hash::TextSha256,
168        }
169    }
170}
171
172impl From<&nix_compat::nixhash::CAHash> for nar_info::Ca {
173    fn from(value: &nix_compat::nixhash::CAHash) -> Self {
174        nar_info::Ca {
175            r#type: Into::<nar_info::ca::Hash>::into(value) as i32,
176            digest: value.hash().digest_as_bytes().to_vec().into(),
177        }
178    }
179}
180
181impl From<crate::pathinfoservice::PathInfo> for PathInfo {
182    fn from(value: crate::pathinfoservice::PathInfo) -> Self {
183        Self {
184            entry: Some(castorepb::Entry::from_name_and_node(
185                value.store_path.to_string().into_bytes().into(),
186                value.node,
187            )),
188            references: value
189                .references
190                .iter()
191                .map(|reference| Bytes::copy_from_slice(reference.digest()))
192                .collect(),
193            narinfo: Some(NarInfo {
194                nar_size: value.nar_size,
195                nar_sha256: Bytes::copy_from_slice(&value.nar_sha256),
196                signatures: value
197                    .signatures
198                    .iter()
199                    .map(|sig| nar_info::Signature {
200                        name: sig.name().to_string(),
201                        data: Bytes::copy_from_slice(sig.bytes()),
202                    })
203                    .collect(),
204                reference_names: value.references.iter().map(|r| r.to_string()).collect(),
205                deriver: value.deriver.as_ref().map(|sp| StorePath {
206                    name: (*sp.name()).to_owned(),
207                    digest: Bytes::copy_from_slice(sp.digest()),
208                }),
209                ca: value.ca.as_ref().map(|ca| ca.into()),
210            }),
211        }
212    }
213}
214
215impl TryFrom<PathInfo> for crate::pathinfoservice::PathInfo {
216    type Error = ValidatePathInfoError;
217    fn try_from(value: PathInfo) -> Result<Self, Self::Error> {
218        let narinfo = value
219            .narinfo
220            .ok_or_else(|| ValidatePathInfoError::NarInfoFieldMissing)?;
221
222        // ensure the references have the right number of bytes.
223        for (i, reference) in value.references.iter().enumerate() {
224            if reference.len() != store_path::DIGEST_SIZE {
225                return Err(ValidatePathInfoError::InvalidReferenceDigestLen(
226                    i,
227                    reference.len(),
228                ));
229            }
230        }
231
232        // ensure the number of references there matches PathInfo.references count.
233        if narinfo.reference_names.len() != value.references.len() {
234            return Err(ValidatePathInfoError::InconsistentNumberOfReferences(
235                value.references.len(),
236                narinfo.reference_names.len(),
237            ));
238        }
239
240        // parse references in reference_names.
241        let mut references = vec![];
242        for (i, reference_name_str) in narinfo.reference_names.iter().enumerate() {
243            // ensure thy parse as (non-absolute) store path
244            let reference_names_store_path =
245                StorePathRef::from_bytes(reference_name_str.as_bytes()).map_err(|_| {
246                    ValidatePathInfoError::InvalidNarinfoReferenceName(
247                        i,
248                        reference_name_str.to_owned(),
249                    )
250                })?;
251
252            // ensure their digest matches the one at self.references[i].
253            {
254                // This is safe, because we ensured the proper length earlier already.
255                let reference_digest = value.references[i].to_vec().try_into().unwrap();
256
257                if reference_names_store_path.digest() != &reference_digest {
258                    return Err(
259                        ValidatePathInfoError::InconsistentNarinfoReferenceNameDigest(
260                            i,
261                            reference_digest,
262                            *reference_names_store_path.digest(),
263                        ),
264                    );
265                } else {
266                    references.push(reference_names_store_path.to_owned());
267                }
268            }
269        }
270
271        let nar_sha256_length = narinfo.nar_sha256.len();
272
273        // split value.entry into the name and node components
274        let (name, node) = value
275            .entry
276            .ok_or_else(|| ValidatePathInfoError::NoEntryPresent)?
277            .try_into_name_and_node()
278            .map_err(ValidatePathInfoError::InvalidRootNode)?;
279
280        Ok(Self {
281            // value.node has a valid name according to the castore model but might not parse to a
282            // [StorePath]
283            store_path: nix_compat::store_path::StorePath::from_bytes(name.as_ref()).map_err(
284                |err| ValidatePathInfoError::InvalidNodeName(name.as_ref().to_vec(), err),
285            )?,
286            node,
287            references,
288            nar_size: narinfo.nar_size,
289            nar_sha256: narinfo.nar_sha256.to_vec()[..]
290                .try_into()
291                .map_err(|_| ValidatePathInfoError::InvalidNarSha256DigestLen(nar_sha256_length))?,
292            // If the Deriver field is populated, ensure it parses to a
293            // [StorePath].
294            // We can't check for it to *not* end with .drv, as the .drv files produced by
295            // recursive Nix end with multiple .drv suffixes, and only one is popped when
296            // converting to this field.
297            deriver: narinfo
298                .deriver
299                .map(|deriver| {
300                    nix_compat::store_path::StorePath::from_name_and_digest(
301                        &deriver.name,
302                        &deriver.digest,
303                    )
304                    .map_err(ValidatePathInfoError::InvalidDeriverField)
305                })
306                .transpose()?,
307            signatures: narinfo
308                .signatures
309                .into_iter()
310                .enumerate()
311                .map(|(i, signature)| {
312                    signature.data.to_vec()[..]
313                        .try_into()
314                        .map_err(|_| {
315                            ValidatePathInfoError::InvalidSignature(
316                                i,
317                                SignatureError::InvalidSignatureLen(signature.data.len()),
318                            )
319                        })
320                        .map(|signature_data| Signature::new(signature.name, signature_data))
321                })
322                .collect::<Result<Vec<_>, ValidatePathInfoError>>()?,
323            ca: narinfo
324                .ca
325                .as_ref()
326                .map(TryFrom::try_from)
327                .transpose()
328                .map_err(ValidatePathInfoError::InvalidCaField)?,
329        })
330    }
331}