| 1 | module signaturefixtures |
| 2 | |
| 3 | import os |
| 4 | |
| 5 | pub struct Keyring { |
| 6 | pub: |
| 7 | path string |
| 8 | fingerprint string |
| 9 | } |
| 10 | |
| 11 | pub fn command(tool string, args []string) !string { |
| 12 | mut process := os.new_process(tool) |
| 13 | process.set_args(args) |
| 14 | process.set_redirect_stdio() |
| 15 | process.set_stdin_path('/dev/null') |
| 16 | process.run() |
| 17 | defer { process.close() } |
| 18 | output := process.stdout_slurp() |
| 19 | diagnostic := process.stderr_slurp() |
| 20 | process.wait() |
| 21 | if process.code != 0 { return error('${tool} exited ${process.code}: ${diagnostic}') } |
| 22 | return output |
| 23 | } |
| 24 | |
| 25 | pub fn (ring Keyring) gpg(args []string) !string { |
| 26 | mut all := ['--homedir', ring.path, '--batch', '--no-tty'] |
| 27 | all << args |
| 28 | return command('gpg', all) |
| 29 | } |
| 30 | |
| 31 | pub fn empty(path string) !Keyring { |
| 32 | os.mkdir_all(path)! |
| 33 | os.chmod(path, 0o700)! |
| 34 | // direct trust makes every trust class testable without network or a web of trust. |
| 35 | os.write_file(os.join_path(path, 'gpg.conf'), 'no-auto-key-retrieve\nauto-key-locate clear\ntrust-model direct\n')! |
| 36 | ring := Keyring{ path: path } |
| 37 | ring.gpg(['--list-keys'])! |
| 38 | return ring |
| 39 | } |
| 40 | |
| 41 | pub fn create(path string) !Keyring { |
| 42 | ring := empty(path)! |
| 43 | fingerprint := ring.generate('Native ALPM Fixture <alpm-fixture@example.invalid>')! |
| 44 | return Keyring{ path: path, fingerprint: fingerprint } |
| 45 | } |
| 46 | |
| 47 | pub fn (ring Keyring) generate(uid string) !string { |
| 48 | ring.gpg(['--pinentry-mode', 'loopback', '--passphrase', '', '--quick-generate-key', uid, 'ed25519', |
| 49 | 'sign', '0'])! |
| 50 | return ring.find_fingerprint(uid) |
| 51 | } |
| 52 | |
| 53 | pub fn (ring Keyring) generate_at(uid string, timestamp i64, expiry string) !string { |
| 54 | ring.gpg(['--faked-system-time', '${timestamp}!', '--pinentry-mode', 'loopback', '--passphrase', |
| 55 | '', '--quick-generate-key', uid, 'ed25519', 'sign', expiry])! |
| 56 | return ring.find_fingerprint(uid) |
| 57 | } |
| 58 | |
| 59 | fn (ring Keyring) find_fingerprint(uid string) !string { |
| 60 | listing := ring.gpg(['--with-colons', '--fingerprint', '--list-keys', uid])! |
| 61 | for line in listing.split_into_lines() { |
| 62 | fields := line.split(':') |
| 63 | if fields.len > 9 && fields[0] == 'fpr' { return fields[9] } |
| 64 | } |
| 65 | return error('generated key has no fingerprint') |
| 66 | } |
| 67 | |
| 68 | pub fn (ring Keyring) sign_at(path string, fingerprint string, timestamp i64, expiry string) ! { |
| 69 | ring.gpg(['--yes', '--faked-system-time', '${timestamp}!', '--pinentry-mode', 'loopback', |
| 70 | '--passphrase', '', '--default-sig-expire', expiry, '--local-user', fingerprint, '--output', |
| 71 | path + '.sig', '--detach-sign', '--', path])! |
| 72 | } |
| 73 | |
| 74 | pub fn (ring Keyring) sign(path string, fingerprints []string, armored bool) ! { |
| 75 | mut args := ['--yes', '--pinentry-mode', 'loopback', '--passphrase', ''] |
| 76 | if armored { args << '--armor' } |
| 77 | for fingerprint in fingerprints { args << ['--local-user', fingerprint] } |
| 78 | args << ['--output', path + '.sig', '--detach-sign', '--', path] |
| 79 | ring.gpg(args)! |
| 80 | } |
| 81 | |
| 82 | pub fn (ring Keyring) trust(fingerprint string, value int) ! { |
| 83 | path := os.join_path(ring.path, 'fixture-ownertrust') |
| 84 | os.write_file(path, '${fingerprint}:${value}:\n')! |
| 85 | ring.gpg(['--import-ownertrust', path])! |
| 86 | } |
| 87 | |
| 88 | pub fn cleanup(path string) ! { |
| 89 | if !os.is_dir(path) { return } |
| 90 | command('gpgconf', ['--homedir', path, '--kill', 'all'])! |
| 91 | os.rmdir_all(path)! |
| 92 | } |
| 93 | |