Skip to content

Commit

Permalink
fix(certificates): add a new encrypted: bool field to private key s…
Browse files Browse the repository at this point in the history
…chema
  • Loading branch information
azasypkin committed Oct 18, 2023
1 parent 32c5847 commit c379980
Show file tree
Hide file tree
Showing 19 changed files with 182 additions and 104 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ UPDATE utils SET id = 11 WHERE id = 5;
INSERT INTO utils (id, handle, name, keywords, parent_id) VALUES
(5, 'certificates__private_keys', 'Private keys', 'private keys openssl encryption pki rsa dsa ec ecdsa curve ed25519 pkcs8 pkcs12 pem', 4);

-- Change "Self-signed certificates" to "Certificate templates".
UPDATE utils
SET name = 'Certificate templates',
handle = 'certificates__certificate_templates',
keywords = 'digital certificates x509 X.509 ssl tls openssl public private key encryption self-signed pki templates'
WHERE
id = 11;

-- Create table to store private keys.
CREATE TABLE IF NOT EXISTS user_data_certificates_private_keys
(
name TEXT NOT NULL COLLATE NOCASE,
alg BLOB NOT NULL,
pkcs8 BLOB NOT NULL,
encrypted INTEGER NOT NULL,
created_at INTEGER NOT NULL,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
PRIMARY KEY (name, user_id)
Expand Down
12 changes: 6 additions & 6 deletions src/users/api_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
UserData, UserDataKey, UserDataNamespace, UserId, UserSettingsSetter, UserShare,
UserShareId,
},
utils::SelfSignedCertificate,
utils::CertificateTemplate,
};
use anyhow::{bail, Context};
use serde::de::DeserializeOwned;
Expand Down Expand Up @@ -101,8 +101,8 @@ impl<'a, DR: DnsResolver, ET: EmailTransport> UsersApi<'a, DR, ET> {
let user_data_key = user_data_key.into();
match user_data_key.namespace {
UserDataNamespace::Public(namespace) => match namespace {
PublicUserDataNamespace::SelfSignedCertificates => {
self.set_self_signed_certificates_data(user_data).await
PublicUserDataNamespace::CertificateTemplates => {
self.set_certificate_templates_data(user_data).await
}
PublicUserDataNamespace::UserSettings => {
self.set_user_settings_data(user_data).await
Expand Down Expand Up @@ -175,16 +175,16 @@ impl<'a, DR: DnsResolver, ET: EmailTransport> UsersApi<'a, DR, ET> {
.await
}

async fn set_self_signed_certificates_data(
async fn set_certificate_templates_data(
&self,
serialized_user_data: UserData<Vec<u8>>,
) -> anyhow::Result<()> {
DictionaryDataUserDataSetter::upsert(
&self.api.db,
PublicUserDataNamespace::SelfSignedCertificates,
PublicUserDataNamespace::CertificateTemplates,
UserData::new(
serialized_user_data.user_id,
serde_json::from_slice::<BTreeMap<String, Option<SelfSignedCertificate>>>(
serde_json::from_slice::<BTreeMap<String, Option<CertificateTemplate>>>(
&serialized_user_data.value,
)
.with_context(|| {
Expand Down
11 changes: 6 additions & 5 deletions src/users/public_user_data_namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use serde::{Deserialize, Serialize};
pub enum PublicUserDataNamespace {
AutoResponders,
ContentSecurityPolicies,
SelfSignedCertificates,
#[serde(rename = "selfSignedCertificates")]
CertificateTemplates,
UserSettings,
WebPageResourcesTrackers,
}
Expand All @@ -16,7 +17,7 @@ impl AsRef<str> for PublicUserDataNamespace {
match self {
PublicUserDataNamespace::AutoResponders => "autoResponders",
PublicUserDataNamespace::ContentSecurityPolicies => "contentSecurityPolicies",
PublicUserDataNamespace::SelfSignedCertificates => "selfSignedCertificates",
PublicUserDataNamespace::CertificateTemplates => "selfSignedCertificates",
PublicUserDataNamespace::UserSettings => "userSettings",
PublicUserDataNamespace::WebPageResourcesTrackers => "webPageResourcesTrackers",
}
Expand Down Expand Up @@ -47,7 +48,7 @@ mod tests {
);

assert_eq!(
PublicUserDataNamespace::SelfSignedCertificates.as_ref(),
PublicUserDataNamespace::CertificateTemplates.as_ref(),
"selfSignedCertificates"
);

Expand All @@ -69,7 +70,7 @@ mod tests {
insta::with_settings!({ sort_maps => true }, {
assert_json_snapshot!(PublicUserDataNamespace::AutoResponders, @r###""autoResponders""###);
assert_json_snapshot!(PublicUserDataNamespace::ContentSecurityPolicies, @r###""contentSecurityPolicies""###);
assert_json_snapshot!(PublicUserDataNamespace::SelfSignedCertificates, @r###""selfSignedCertificates""###);
assert_json_snapshot!(PublicUserDataNamespace::CertificateTemplates, @r###""selfSignedCertificates""###);
assert_json_snapshot!(PublicUserDataNamespace::UserSettings, @r###""userSettings""###);
assert_json_snapshot!(PublicUserDataNamespace::WebPageResourcesTrackers, @r###""webPageResourcesTrackers""###);
});
Expand All @@ -91,7 +92,7 @@ mod tests {

assert_eq!(
serde_json::from_str::<PublicUserDataNamespace>(r#""selfSignedCertificates""#)?,
PublicUserDataNamespace::SelfSignedCertificates
PublicUserDataNamespace::CertificateTemplates
);

assert_eq!(
Expand Down
14 changes: 7 additions & 7 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ mod webhooks;

pub use self::{
certificates::{
CertificatesApi, ExportFormat, ExtendedKeyUsage, KeyUsage, PrivateKey, PrivateKeyAlgorithm,
PrivateKeyEllipticCurve, PrivateKeySize, SelfSignedCertificate, SignatureAlgorithm,
CertificateTemplate, CertificatesApi, ExportFormat, ExtendedKeyUsage, KeyUsage, PrivateKey,
PrivateKeyAlgorithm, PrivateKeyEllipticCurve, PrivateKeySize, SignatureAlgorithm,
UtilsCertificatesAction, UtilsCertificatesActionResult, Version,
},
util::Util,
Expand Down Expand Up @@ -42,15 +42,15 @@ pub use self::{
#[cfg(test)]
pub mod tests {
use crate::utils::{
ExtendedKeyUsage, KeyUsage, PrivateKeyAlgorithm, SelfSignedCertificate, SignatureAlgorithm,
CertificateTemplate, ExtendedKeyUsage, KeyUsage, PrivateKeyAlgorithm, SignatureAlgorithm,
Version,
};
use time::OffsetDateTime;

pub use super::web_scraping::tests::MockWebPageResourcesTrackerBuilder;

pub struct MockSelfSignedCertificate(SelfSignedCertificate);
impl MockSelfSignedCertificate {
pub struct MockCertificateTemplate(CertificateTemplate);
impl MockCertificateTemplate {
pub fn new<N: Into<String>>(
name: N,
public_key_algorithm: PrivateKeyAlgorithm,
Expand All @@ -59,7 +59,7 @@ pub mod tests {
not_valid_after: OffsetDateTime,
version: Version,
) -> Self {
Self(SelfSignedCertificate {
Self(CertificateTemplate {
name: name.into(),
common_name: None,
country: None,
Expand Down Expand Up @@ -131,7 +131,7 @@ pub mod tests {
self
}

pub fn build(self) -> SelfSignedCertificate {
pub fn build(self) -> CertificateTemplate {
self.0
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/utils/api_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ mod tests {
},
Util {
id: 11,
handle: "certificates__self_signed_certificates",
name: "Self-signed certificates",
handle: "certificates__templates",
name: "Templates",
keywords: Some(
"digital certificates x509 X.509 ssl tls openssl public private key encryption self-signed pki",
"digital certificates x509 X.509 ssl tls openssl public private key encryption self-signed pki templates",
),
utils: None,
},
Expand Down
4 changes: 2 additions & 2 deletions src/utils/certificates.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod certificate_templates;
mod database_ext;
mod export_format;
mod private_keys;
mod self_signed_certificates;
mod utils_certificates_action;
mod utils_certificates_action_result;
mod x509;
Expand All @@ -10,9 +10,9 @@ mod api_ext;

pub use self::{
api_ext::CertificatesApi,
certificate_templates::CertificateTemplate,
export_format::ExportFormat,
private_keys::{PrivateKey, PrivateKeyAlgorithm, PrivateKeyEllipticCurve, PrivateKeySize},
self_signed_certificates::SelfSignedCertificate,
utils_certificates_action::UtilsCertificatesAction,
utils_certificates_action_result::UtilsCertificatesActionResult,
x509::{ExtendedKeyUsage, KeyUsage, SignatureAlgorithm, Version},
Expand Down
Loading

0 comments on commit c379980

Please sign in to comment.