zuul-jobs/test-playbooks/encrypt-file.yaml
Ian Wienand ad7093c17b encrypt-file : role to encrypt a file
This is a role that takes some ASCII gpg keys, and encrypts a file
with them.

Change-Id: If2fe7921ff051a1c5d0589f5e32fba26d30ae96c
2022-02-19 08:05:40 +11:00

115 lines
4.2 KiB
YAML

- hosts: all
tasks:
- name: Make a fake file
tempfile:
state: file
register: _tempfile
- name: Add some data
copy:
content: 'Hello, I am encrypted'
dest: '{{ _tempfile.path }}'
- name: Setup encryption variables
set_fact:
encrypt_file_keys:
- name: 'zuul-jobs-test-1'
key_id: '0xD0A3C69F209B3B8E'
gpg_asc: |
-----BEGIN PGP PUBLIC KEY BLOCK-----
mDMEYgxZHhYJKwYBBAHaRw8BAQdAl9ZJaMvAc4kcO2mWFCZ5Em0xl7kRc3QYgtg0
+98sqoO0EHp1dWwtam9icy10ZXN0LTGIlAQTFgoAPBYhBKNWMSQoy8kXXIv/T9Cj
xp8gmzuOBQJiDFkeAhsDBQsJCAcCAyICAQYVCgkICwIEFgIDAQIeBwIXgAAKCRDQ
o8afIJs7jqlUAQCVxaINjS5/rd1oCAp19lHrscMhFmQBOtxyU7CTDoCrCAEAh9mH
scfOGRWhxiwg0+dXe6RE/C3Kk13kdN8pfTOIGA24OARiDFkeEgorBgEEAZdVAQUB
AQdAl53uFFzrhnTTmq0YbDRtQ5KrMJYYNahImPzzrvVajW4DAQgHiHgEGBYKACAW
IQSjVjEkKMvJF1yL/0/Qo8afIJs7jgUCYgxZHgIbDAAKCRDQo8afIJs7jqE7AP9M
LRe/tJ+SeHexDI1m9tmo6xcID7UOJW8eIuuwi3kjZgEAl+WqJfqjBxJmBWIjTZcV
zA2T4i8ViORhXLo0oohQVwE=
=/liI
-----END PGP PUBLIC KEY BLOCK-----
- name: 'zuul-jobs-test-2'
key_id: '0x4E1BA7A3AB674E6F'
gpg_asc: |
-----BEGIN PGP PUBLIC KEY BLOCK-----
mDMEYgxZkRYJKwYBBAHaRw8BAQdA1/5ta4i1G+NGqRW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SaXp+dsR04367XacSJ3aQD/Tu6q45tF0t4G0dQIpzxT
jnHN/zEM7eyW45Jf/V8migI=
=CRYD
-----END PGP PUBLIC KEY BLOCK-----
- name: Encrypt file
include_role:
name: encrypt-file
vars:
encrypt_file: '{{ _tempfile.path }}'
encrypt_file_recipients:
- zuul-jobs-test-2
- zuul-jobs-test-3
- name: Remove temporary file
file:
path: '{{ _tempfile.path }}'
state: absent
when: _tempfile.path is defined
- name: Check output file
stat:
path: '{{ _tempfile.path }}.gpg'
register: _output
- name: Ensure exists
fail:
msg: 'Output file not found'
when: not _output.stat.exists
- name: Dump gpg packets
command: gpg --list-packets '{{ _tempfile.path }}.gpg'
register: _gpg_output
# Because it can't decrypt, gpg give an error. But we're
# interested in the encryption packets so expect this.
failed_when: _gpg_output.rc != 2
- name: Show gpg command output
debug:
var: _gpg_output
- name: Validate packets
assert:
that:
- "'zuul-jobs-test-1' not in _gpg_output.stdout"
- "'zuul-jobs-test-2' in _gpg_output.stdout"
- "'zuul-jobs-test-3' in _gpg_output.stdout"
- name: Remove output file
file:
path: '{{ _tempfile.path }}.gpg'
state: absent