Build a new `CertBot` from a `CertBotConfig`.
(config: CertBotConfig)
| 87 | impl CertBot { |
| 88 | /// Build a new `CertBot` from a `CertBotConfig`. |
| 89 | pub async fn build(config: CertBotConfig) -> Result<Self> { |
| 90 | let base_domain = config |
| 91 | .cert_subject_alt_names |
| 92 | .first() |
| 93 | .context("cert_subject_alt_names is empty")? |
| 94 | .trim() |
| 95 | .trim_start_matches("*.") |
| 96 | .trim_end_matches('.') |
| 97 | .to_string(); |
| 98 | let dns01_client = Dns01Client::new_cloudflare( |
| 99 | base_domain, |
| 100 | config.cf_api_token.clone(), |
| 101 | config.cf_api_url.clone(), |
| 102 | ) |
| 103 | .await?; |
| 104 | let acme_client = match fs::read_to_string(&config.credentials_file) { |
| 105 | Ok(credentials) => { |
| 106 | if acme_matches(&credentials, &config.acme_url) { |
| 107 | AcmeClient::load( |
| 108 | dns01_client, |
| 109 | &credentials, |
| 110 | config.max_dns_wait, |
| 111 | config.dns_txt_ttl, |
| 112 | ) |
| 113 | .await? |
| 114 | } else { |
| 115 | create_new_account(&config, dns01_client).await? |
| 116 | } |
| 117 | } |
| 118 | Err(e) if e.kind() == ErrorKind::NotFound => { |
| 119 | if !config.auto_create_account { |
| 120 | return Err(e).context("credentials file not found"); |
| 121 | } |
| 122 | create_new_account(&config, dns01_client).await? |
| 123 | } |
| 124 | Err(e) => { |
| 125 | return Err(e).context("failed to read credentials file"); |
| 126 | } |
| 127 | }; |
| 128 | Ok(Self { |
| 129 | acme_client, |
| 130 | config, |
| 131 | }) |
| 132 | } |
| 133 | |
| 134 | /// Get the ACME account ID. |
| 135 | pub fn account_id(&self) -> &str { |