sigstore 0.14.0

An experimental crate to interact with sigstore
Documentation
//
// Copyright 2021 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#[cfg(test)]
pub(crate) mod test {
    use crate::errors::{Result, SigstoreError};

    use async_trait::async_trait;
    use oci_client::{
        Reference,
        client::{ImageData, PushResponse},
        manifest::{OciImageIndex, OciManifest},
        secrets::RegistryAuth,
    };

    #[derive(Default)]
    pub struct MockOciClient {
        pub fetch_manifest_digest_response: Option<anyhow::Result<String>>,
        pub pull_response: Option<anyhow::Result<ImageData>>,
        pub pull_manifest_response: Option<anyhow::Result<(OciManifest, String)>>,
        pub push_response: Option<anyhow::Result<PushResponse>>,
        pub pull_referrers_response: Option<anyhow::Result<OciImageIndex>>,
    }

    impl crate::registry::ClientCapabilitiesDeps for MockOciClient {}

    #[async_trait]
    impl crate::registry::ClientCapabilities for MockOciClient {
        async fn fetch_manifest_digest(
            &mut self,
            image: &Reference,
            _auth: &RegistryAuth,
        ) -> Result<String> {
            let mock_response = self
                .fetch_manifest_digest_response
                .as_ref()
                .ok_or_else(|| SigstoreError::RegistryFetchManifestError {
                    image: image.whole(),
                    error: String::from("No fetch_manifest_digest_response provided!"),
                })?;

            match mock_response {
                Ok(r) => Ok(r.clone()),
                Err(e) => Err(SigstoreError::RegistryFetchManifestError {
                    image: image.whole(),
                    error: e.to_string(),
                }),
            }
        }

        async fn pull(
            &mut self,
            image: &Reference,
            _auth: &RegistryAuth,
            _accepted_media_types: Vec<&str>,
        ) -> Result<ImageData> {
            let mock_response =
                self.pull_response
                    .as_ref()
                    .ok_or_else(|| SigstoreError::RegistryPullError {
                        image: image.whole(),
                        error: String::from("No pull_response provided!"),
                    })?;

            match mock_response {
                Ok(r) => Ok(r.clone()),
                Err(e) => Err(SigstoreError::RegistryPullError {
                    image: image.whole(),
                    error: e.to_string(),
                }),
            }
        }

        async fn pull_manifest(
            &mut self,
            image: &Reference,
            _auth: &RegistryAuth,
        ) -> Result<(OciManifest, String)> {
            let mock_response = self.pull_manifest_response.as_ref().ok_or_else(|| {
                SigstoreError::RegistryPullError {
                    image: image.whole(),
                    error: String::from("No pull_manifest_response provided!"),
                }
            })?;

            match mock_response {
                Ok(r) => Ok(r.clone()),
                Err(e) => Err(SigstoreError::RegistryPullError {
                    image: image.whole(),
                    error: e.to_string(),
                }),
            }
        }

        async fn push(
            &mut self,
            image_ref: &oci_client::Reference,
            _layers: &[oci_client::client::ImageLayer],
            _config: oci_client::client::Config,
            _auth: &oci_client::secrets::RegistryAuth,
            _manifest: Option<oci_client::manifest::OciImageManifest>,
        ) -> Result<PushResponse> {
            let mock_response =
                self.push_response
                    .as_ref()
                    .ok_or_else(|| SigstoreError::RegistryPushError {
                        image: image_ref.whole(),
                        error: String::from("No push_response provided!"),
                    })?;

            match mock_response {
                Ok(r) => Ok(PushResponse {
                    config_url: r.config_url.clone(),
                    manifest_url: r.manifest_url.clone(),
                }),
                Err(e) => Err(SigstoreError::RegistryPushError {
                    image: image_ref.whole(),
                    error: e.to_string(),
                }),
            }
        }

        async fn pull_referrers(
            &mut self,
            image: &oci_client::Reference,
            _auth: &oci_client::secrets::RegistryAuth,
            _artifact_type: Option<&str>,
        ) -> Result<OciImageIndex> {
            let mock_response = self.pull_referrers_response.as_ref().ok_or_else(|| {
                SigstoreError::RegistryPullManifestError {
                    image: image.whole(),
                    error: String::from("No pull_referrers_response provided!"),
                }
            })?;

            match mock_response {
                Ok(r) => Ok(r.clone()),
                Err(e) => Err(SigstoreError::RegistryPullManifestError {
                    image: image.whole(),
                    error: e.to_string(),
                }),
            }
        }
    }
}