Skip to main content

rc_crypto/
issuer.rs

1// Copyright 2026-Present Datadog, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Abstract issuance of a [`Certificate`] from a [`CertificateSigningRequest`].
16
17use std::{sync::Arc, time::Duration};
18
19use crate::certificate::{Certificate, csr::CertificateSigningRequest};
20
21/// A convenience type def over an opaque error.
22pub type BoxErr = Box<dyn std::error::Error + Send + Sync + 'static>;
23
24/// Opaque errors returned by a [`CertIssuer`].
25#[derive(Debug, thiserror::Error)]
26pub enum IssueError {
27    /// The caller MAY retry the same request with an expectation of success.
28    #[error("retryable request error: {0}")]
29    Retryable(BoxErr),
30
31    /// The caller SHOULD NOT retry the same request; it will certainly fail.
32    #[error("fatal request error: {0}")]
33    Fatal(BoxErr),
34}
35
36/// A [`CertIssuer`] attempts to issue a [`Certificate`] using the parameters
37/// specified in the [`CertificateSigningRequest`].
38pub trait CertIssuer: Send + Sync + std::fmt::Debug {
39    /// Return a certificate for the provided `csr`.
40    fn issue_cert_for(
41        &self,
42        csr: &CertificateSigningRequest,
43        ttl: Duration,
44    ) -> impl Future<Output = Result<Certificate, IssueError>> + Send;
45}
46
47impl<T> CertIssuer for Arc<T>
48where
49    T: CertIssuer,
50{
51    fn issue_cert_for(
52        &self,
53        csr: &CertificateSigningRequest,
54        ttl: Duration,
55    ) -> impl Future<Output = Result<Certificate, IssueError>> + Send {
56        T::issue_cert_for(self, csr, ttl)
57    }
58}