-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssl-renewal.php
138 lines (116 loc) · 4.97 KB
/
ssl-renewal.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* Plugin Name: SSL Renewal
* Plugin URI: https://tomascordero.com
* Description: A simple plugin to renew SSL certificate with Let's Encrypt and wordpress CLI
* Author: Tomas Cordero
* Author URI: https://tomascordero.com
* Text Domain: ssl-renewal
* Domain Path: /languages
* Version: 0.1.0
*
* @package Ssl_Renewal
*/
require plugin_dir_path( __FILE__ ) . '/vendor/autoload.php';
if (defined('WP_CLI') && WP_CLI) {
WP_CLI::add_command('ssl:renew', 'SSL_Renewal_Command');
WP_CLI::add_command('ssl:dns-challenge', 'SSL_DNS_Challenge');
WP_CLI::add_command('ssl:install', 'SSL_Move_Files');
}
use Aws\Route53\Route53Client;
use Aws\Exception\AwsException;
class SSL_Renewal_Command {
public function __invoke($args, $assoc_args) {
if (!isset($assoc_args['domain'])) {
WP_CLI::error('Missing required argument: domain. ex: --domain=example.com');
return;
}
$domain = $assoc_args['domain'];
$wildcard = "*.{$assoc_args['domain']}";
$zoneId = getenv('AWS_ROUTE53_ZONE_ID') ?: AWS_ROUTE53_ZONE_ID;
WP_CLI::log('Starting SSL renewal process...');
// Step 1: Request a new SSL certificate with Certbot (DNS Challenge)
exec("sudo certbot certonly --manual --preferred-challenges dns --manual-auth-hook 'wp ssl:dns-challenge' -d {$domain} -d {$wildcard}", $output, $return_var);
if ($return_var !== 0) {
WP_CLI::error('Certbot failed! Check logs.');
return;
}
WP_CLI::success('SSL renewed successfully!');
}
}
// WP-CLI command to handle DNS challenge
class SSL_DNS_Challenge {
public function __invoke($args, $assoc_args) {
$domain = getenv('CERTBOT_DOMAIN');
$recordValue = getenv('CERTBOT_VALIDATION');
if (!$domain || !$recordValue) {
WP_CLI::error('Missing required arguments: domain and value.');
return;
}
WP_CLI::log("Adding DNS TXT record for {$domain}...");
// Call Route 53 API to add TXT record
try {
$client = new Route53Client([
'version' => 'latest',
'region' => AWS_REGION,
'credentials' => [
'key' => AWS_ACCESS_KEY_ID,
'secret' => AWS_SECRET_ACCESS_KEY,
],
]);
$zoneId = AWS_ROUTE53_ZONE_ID;
$result = $client->changeResourceRecordSets([
'HostedZoneId' => $zoneId,
'ChangeBatch' => [
'Changes' => [
[
'Action' => 'UPSERT',
'ResourceRecordSet' => [
'Name' => "_acme-challenge.{$domain}",
'Type' => 'TXT',
'TTL' => 60,
'ResourceRecords' => [['Value' => "\"{$recordValue}\""]],
],
],
],
],
]);
WP_CLI::success("DNS record added! Waiting for propagation... once done, run 'wp ssl:install --domain={$domain}'");
} catch (AwsException $e) {
WP_CLI::error("AWS Route 53 Error: " . $e->getMessage());
}
}
}
class SSL_Move_Files {
public function __invoke($args, $assoc_args) {
$domain = $assoc_args['domain'];
WP_CLI::log("Moving certificate files to web server...");
try {
$result_code = 0;
exec("sudo mv /opt/bitnami/apache2/conf/bitnami/certs/server.crt /opt/bitnami/apache2/conf/bitnami/certs/server.crt.old", result_code: $result_code);
if ($result_code !== 0) {
WP_CLI::error("Error moving server.crt file!");
return;
}
exec("sudo mv /opt/bitnami/apache2/conf/bitnami/certs/server.key /opt/bitnami/apache2/conf/bitnami/certs/server.key.old", result_code: $result_code);
if ($result_code !== 0) {
WP_CLI::error("Error moving server.key file!");
return;
}
exec("sudo ln -sf /etc/letsencrypt/live/$domain/privkey.pem /opt/bitnami/apache2/conf/bitnami/certs/server.key", result_code: $result_code);
if ($result_code !== 0) {
WP_CLI::error("Error moving privkey.pem file!");
return;
}
exec("sudo ln -sf /etc/letsencrypt/live/$domain/fullchain.pem /opt/bitnami/apache2/conf/bitnami/certs/server.crt", result_code: $result_code);
if ($result_code !== 0) {
WP_CLI::error("Error moving fullchain.pem file!");
return;
}
} catch(\Exception $e) {
WP_CLI::error("Error moving certificate files: " . $e->getMessage());
return;
}
WP_CLI::success("Certificate files moved successfully!");
}
}