Skip to content

Commit c76afe0

Browse files
authored
(PE-35906) Adding plans for backing up and restoring CA (#400)
Utilising puppet_backup for backing up and restoring CA and SSL certificates on a target
1 parent bddd9a5 commit c76afe0

File tree

6 files changed

+172
-0
lines changed

6 files changed

+172
-0
lines changed

REFERENCE.md

+59
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,11 @@
8585
#### Public Plans
8686

8787
* [`peadm::add_database`](#peadm--add_database)
88+
* [`peadm::backup_ca`](#peadm--backup_ca)
8889
* [`peadm::convert`](#peadm--convert): Convert an existing PE cluster to a PEAdm-managed cluster
8990
* [`peadm::install`](#peadm--install): Install a new PE cluster
9091
* [`peadm::modify_certificate`](#peadm--modify_certificate): Modify the certificate of one or more targets
92+
* [`peadm::restore_ca`](#peadm--restore_ca)
9193
* [`peadm::status`](#peadm--status): Return status information from one or more PE clusters in a table format
9294
* [`peadm::upgrade`](#peadm--upgrade): Upgrade a PEAdm-managed cluster
9395

@@ -1527,6 +1529,31 @@ Optional[Enum[
15271529

15281530
Default value: `undef`
15291531

1532+
### <a name="peadm--backup_ca"></a>`peadm::backup_ca`
1533+
1534+
The peadm::backup_ca class.
1535+
1536+
#### Parameters
1537+
1538+
The following parameters are available in the `peadm::backup_ca` plan:
1539+
1540+
* [`target`](#-peadm--backup_ca--target)
1541+
* [`output_directory`](#-peadm--backup_ca--output_directory)
1542+
1543+
##### <a name="-peadm--backup_ca--target"></a>`target`
1544+
1545+
Data type: `Peadm::SingleTargetSpec`
1546+
1547+
1548+
1549+
##### <a name="-peadm--backup_ca--output_directory"></a>`output_directory`
1550+
1551+
Data type: `Optional[String]`
1552+
1553+
1554+
1555+
Default value: `'/tmp'`
1556+
15301557
### <a name="peadm--convert"></a>`peadm::convert`
15311558

15321559
This plan sets required certificate extensions on PE nodes, and configures
@@ -1965,6 +1992,38 @@ Data type: `Boolean`
19651992

19661993
Default value: `false`
19671994

1995+
### <a name="peadm--restore_ca"></a>`peadm::restore_ca`
1996+
1997+
The peadm::restore_ca class.
1998+
1999+
#### Parameters
2000+
2001+
The following parameters are available in the `peadm::restore_ca` plan:
2002+
2003+
* [`target`](#-peadm--restore_ca--target)
2004+
* [`file_path`](#-peadm--restore_ca--file_path)
2005+
* [`recovery_directory`](#-peadm--restore_ca--recovery_directory)
2006+
2007+
##### <a name="-peadm--restore_ca--target"></a>`target`
2008+
2009+
Data type: `Peadm::SingleTargetSpec`
2010+
2011+
2012+
2013+
##### <a name="-peadm--restore_ca--file_path"></a>`file_path`
2014+
2015+
Data type: `String`
2016+
2017+
2018+
2019+
##### <a name="-peadm--restore_ca--recovery_directory"></a>`recovery_directory`
2020+
2021+
Data type: `Optional[String]`
2022+
2023+
2024+
2025+
Default value: `'/tmp/peadm_recovery'`
2026+
19682027
### <a name="peadm--status"></a>`peadm::status`
19692028

19702029
Return status information from one or more PE clusters in a table format

documentation/backup_restore_ca.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Backup and Restore Puppet Enterprise CA
2+
3+
## Overview
4+
Backup and restore plans for the Puppet Enterprise CA. This utilises the [puppet_backup](https://www.puppet.com/docs/pe/2023.4/backing_up_and_restoring_pe.html) tool. This plan has scope set to only CERTS, and will backup CA and SSL certificates. The backup plan will create a tarball of the CA and store it by default in the `/tmp` directory. The restore plan will restore the CA from the tarball at the path you provide.
5+
6+
## Notes
7+
There can be some downtime associated with the restore process. Restore will stop PE services, restore the CA, and then start the PE services. This can take a few minutes.
8+
9+
## Usage
10+
11+
### Backup
12+
13+
```bash
14+
peadm backup_ca target=primary.example.com
15+
```
16+
17+
Backup will output the path to a timestamped folder containing the backup file. The backup file will be named `backup_ca.tgz`. At this stage the backup file can be copied to a safe location.
18+
19+
Optionaly "output_directory" can be specified to change the location of the backup file.
20+
21+
```bash
22+
peadm::backup_ca target=primary.example.com output_directory=/custompath
23+
```
24+
25+
### Restore
26+
27+
```bash
28+
peadm::restore_ca target=primary2.example.com path=/tmp/backup_ca.tgz file_path=/tmp/backup_ca.tgz
29+
```
30+
31+
Restore will stop PE services, restore the CA, and then start the PE services. This can take a few minutes.
32+
33+
Optionaly "recovery_directory" can be specified to change the temporary location where the backup file will be unzipped.
34+
35+
```bash
36+
peadm::restore_ca target=primary2.example.com path=/tmp/backup_ca.tgz file_path=/tmp/backup_ca.tgz recovery_directory=/custompath
37+
```
38+
39+

plans/backup_ca.pp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
plan peadm::backup_ca(
2+
Peadm::SingleTargetSpec $target,
3+
Optional[String] $output_directory = '/tmp',
4+
) {
5+
out::message('# Backing up ca and ssl certificates')
6+
# lint:ignore:strict_indent
7+
8+
$timestamp = Timestamp.new().strftime('%Y-%m-%dT%H%M%SZ')
9+
$backup_directory = "${output_directory}/pe-backup-${timestamp}"
10+
11+
# Create backup folder
12+
apply($target) {
13+
file { $backup_directory :
14+
ensure => 'directory',
15+
owner => 'root',
16+
group => 'root',
17+
mode => '0700',
18+
}
19+
}
20+
21+
run_command(@("CMD"), $target)
22+
/opt/puppetlabs/bin/puppet-backup create --dir=${shellquote($backup_directory)} --name=ca_backup.tgz --scope=certs
23+
| CMD
24+
# lint:endignore
25+
return({ 'path' => "${backup_directory}/ca_backup.tgz" })
26+
}

plans/restore_ca.pp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
plan peadm::restore_ca(
2+
Peadm::SingleTargetSpec $target,
3+
String $file_path,
4+
Optional[String] $recovery_directory = '/tmp/peadm_recovery',
5+
) {
6+
out::message('# Restoring ca and ssl certificates')
7+
8+
# lint:ignore:strict_indent
9+
run_command(@("CMD"/L), $target)
10+
/opt/puppetlabs/bin/puppet-backup restore \
11+
--scope=certs \
12+
--tempdir=${shellquote($recovery_directory)} \
13+
--force \
14+
${shellquote($file_path)}
15+
| CMD
16+
}

spec/plans/backup_ca_spec.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'spec_helper'
2+
3+
describe 'peadm::backup_ca' do
4+
include BoltSpec::Plans
5+
6+
let(:params) { { 'target' => 'myserver.example.com' } }
7+
8+
it 'will create backup directory and run puppet-backup command' do
9+
allow_apply
10+
expect_out_message.with_params('# Backing up ca and ssl certificates')
11+
allow_any_command
12+
expect(run_plan('peadm::backup_ca', params)).to be_ok
13+
end
14+
end

spec/plans/restore_ca_spec.rb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'spec_helper'
2+
3+
describe 'peadm::restore_ca' do
4+
include BoltSpec::Plans
5+
6+
let(:params) do
7+
{
8+
'target' => 'myserver.example.com',
9+
'file_path' => '/tmp/backup_ca.tgz'
10+
}
11+
end
12+
13+
it 'will run puppet-backup command' do
14+
expect_out_message.with_params('# Restoring ca and ssl certificates')
15+
allow_any_command
16+
expect(run_plan('peadm::restore_ca', params)).to be_ok
17+
end
18+
end

0 commit comments

Comments
 (0)