Skip to content

Commit d4ff731

Browse files
committed
feat(ssl): add command to print DNS TXT records for SSL challenge
1 parent b460376 commit d4ff731

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

src/helper/class-ee-site.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,94 @@ public function ssl_verify( $args = [], $assoc_args = [], $www_or_non_www = fals
17191719
return true;
17201720
}
17211721

1722+
/**
1723+
* Prints the DNS TXT record(s) required for DNS-based SSL challenge for a site.
1724+
*
1725+
* ## OPTIONS
1726+
*
1727+
* [<site-name>]
1728+
* : Name of website.
1729+
*
1730+
* [--format=<format>]
1731+
* : Render output in a particular format.
1732+
* ---
1733+
* default: table
1734+
* options:
1735+
* - table
1736+
* - csv
1737+
* - yaml
1738+
* - json
1739+
* - count
1740+
* - text
1741+
* ---
1742+
*
1743+
* ## EXAMPLES
1744+
*
1745+
* # Show DNS challenge info for a site
1746+
* $ ee site ssl-dns-info example.com
1747+
* $ ee site ssl-dns-info example.com --format=json
1748+
*
1749+
* @subcommand ssl-dns-info
1750+
*/
1751+
public function ssl_dns_info( $args, $assoc_args ) {
1752+
$args = auto_site_name( $args, 'site', __FUNCTION__ );
1753+
$this->site_data = get_site_info( $args, false, true, false );
1754+
1755+
$site_url = $this->site_data->site_url;
1756+
$wildcard = ! empty( $this->site_data->site_ssl_wildcard );
1757+
$alias_domains = empty( $this->site_data->alias_domains ) ? [] : explode( ',', $this->site_data->alias_domains );
1758+
$domains = $this->get_cert_domains( $site_url, $wildcard );
1759+
$domains = array_unique( array_merge( $domains, $alias_domains ) );
1760+
1761+
$preferred_challenge = get_preferred_ssl_challenge( $domains );
1762+
$is_dns = $wildcard || $preferred_challenge === 'dns';
1763+
1764+
if ( ! $is_dns ) {
1765+
\EE::log( 'This site does not use DNS-based (DNS-01) SSL challenge.' );
1766+
1767+
return;
1768+
}
1769+
1770+
$format = \EE\Utils\get_flag_value( $assoc_args, 'format', 'table' );
1771+
$client = new \EE\Site\Type\Site_Letsencrypt();
1772+
$rows = [];
1773+
foreach ( $domains as $domain ) {
1774+
if ( $client->hasDomainAuthorizationChallenge( $domain ) ) {
1775+
$challenge = $client->loadDomainAuthorizationChallenge( $domain );
1776+
if ( method_exists( $challenge, 'toArray' ) ) {
1777+
$data = $challenge->toArray();
1778+
$record_name = isset( $data['dnsRecordName'] ) ? $data['dnsRecordName'] : '_acme-challenge.' . $domain;
1779+
if ( isset( $data['dnsRecordValue'] ) ) {
1780+
$record_value = $data['dnsRecordValue'];
1781+
} elseif ( isset( $data['payload'] ) ) {
1782+
// Compute digest for DNS-01 TXT value
1783+
$keyAuthorization = $data['payload'];
1784+
$digest = rtrim( strtr( base64_encode( hash( 'sha256', $keyAuthorization, true ) ), '+/', '-_' ), '=' );
1785+
$record_value = $digest;
1786+
} else {
1787+
$record_value = '';
1788+
}
1789+
$rows[] = [
1790+
'domain' => $domain,
1791+
'record_name' => $record_name,
1792+
'record_value' => $record_value,
1793+
];
1794+
} else {
1795+
\EE::warning( "Could not extract DNS challenge for $domain." );
1796+
}
1797+
} else {
1798+
\EE::warning( "No pending DNS challenge found for $domain. (Try running 'ee site ssl-verify $site_url' if you are setting up SSL)" );
1799+
}
1800+
}
1801+
if ( $rows ) {
1802+
$formatter = new \EE\Formatter( $assoc_args, [ 'domain', 'record_name', 'record_value' ] );
1803+
$formatter->display_items( $rows );
1804+
} else {
1805+
\EE::log( 'No DNS challenge records found for this site.' );
1806+
}
1807+
}
1808+
1809+
17221810
/**
17231811
* Renews letsencrypt ssl certificates.
17241812
*

0 commit comments

Comments
 (0)