|
| 1 | +require 'spec_helper' |
| 2 | +require 'tempfile' |
| 3 | +require 'open3' |
| 4 | + |
| 5 | +RSpec.describe 'a transport' do |
| 6 | + let(:common_args) { '--verbose --trace --debug --strict=error --modulepath spec/fixtures' } |
| 7 | + |
| 8 | + before(:all) do |
| 9 | + FileUtils.mkdir_p(File.expand_path('~/.puppetlabs/opt/puppet/cache/devices/the_node/state')) |
| 10 | + end |
| 11 | + |
| 12 | + describe 'using `puppet device`' do |
| 13 | + let(:common_args) { super() + ' --target the_node' } |
| 14 | + let(:device_conf) { Tempfile.new('device.conf') } |
| 15 | + let(:device_conf_content) do |
| 16 | + <<DEVICE_CONF |
| 17 | +[the_node] |
| 18 | +type test_device_sensitive |
| 19 | +url file://#{device_credentials.path} |
| 20 | +DEVICE_CONF |
| 21 | + end |
| 22 | + let(:device_credentials) { Tempfile.new('credentials.txt') } |
| 23 | + |
| 24 | + def is_device_apply_supported? |
| 25 | + Gem::Version.new(Puppet::PUPPETVERSION) >= Gem::Version.new('5.3.6') && Gem::Version.new(Puppet::PUPPETVERSION) != Gem::Version.new('5.4.0') |
| 26 | + end |
| 27 | + |
| 28 | + before(:each) do |
| 29 | + skip "No device --apply in puppet before v5.3.6 nor in v5.4.0 (v#{Puppet::PUPPETVERSION} is installed)" unless is_device_apply_supported? |
| 30 | + device_conf.write(device_conf_content) |
| 31 | + device_conf.close |
| 32 | + |
| 33 | + device_credentials.write(device_credentials_content) |
| 34 | + device_credentials.close |
| 35 | + end |
| 36 | + |
| 37 | + after(:each) do |
| 38 | + device_conf.unlink |
| 39 | + device_credentials.unlink |
| 40 | + end |
| 41 | + |
| 42 | + context 'when all sensitive values are valid' do |
| 43 | + let(:device_credentials_content) do |
| 44 | + <<DEVICE_CREDS |
| 45 | +{ |
| 46 | + username: foo |
| 47 | + secret_string: wibble |
| 48 | + optional_secret: bar |
| 49 | + array_secret: [meep] |
| 50 | + variant_secret: 1234567890 |
| 51 | +} |
| 52 | +DEVICE_CREDS |
| 53 | + end |
| 54 | + |
| 55 | + it 'does not throw' do |
| 56 | + Tempfile.create('apply_success') do |f| |
| 57 | + f.write 'notify { "foo": }' |
| 58 | + f.close |
| 59 | + |
| 60 | + stdout_str, _status = Open3.capture2e("puppet device #{common_args} --deviceconfig #{device_conf.path} --apply #{f.path}") |
| 61 | + expect(stdout_str).not_to match %r{Value type mismatch} |
| 62 | + |
| 63 | + expect(stdout_str).to match %r{transport connection_info:} |
| 64 | + expect(stdout_str).to match %r{:username=>"foo"} |
| 65 | + expect(stdout_str).not_to match %r{wibble} |
| 66 | + expect(stdout_str).not_to match %r{bar} |
| 67 | + expect(stdout_str).not_to match %r{meep} |
| 68 | + expect(stdout_str).not_to match %r{1234567890} |
| 69 | + end |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + context 'with a sensitive string value that is invalid' do |
| 74 | + let(:device_credentials_content) do |
| 75 | + <<DEVICE_CREDS |
| 76 | +{ |
| 77 | + username: foo |
| 78 | + secret_string: 12345 |
| 79 | + optional_secret: wibble |
| 80 | + array_secret: [meep] |
| 81 | + variant_secret: 21 |
| 82 | +} |
| 83 | +DEVICE_CREDS |
| 84 | + end |
| 85 | + |
| 86 | + it 'Value type mismatch' do |
| 87 | + Tempfile.create('apply_success') do |f| |
| 88 | + f.write 'notify { "foo": }' |
| 89 | + f.close |
| 90 | + |
| 91 | + stdout_str, _status = Open3.capture2e("puppet device #{common_args} --deviceconfig #{device_conf.path} --apply #{f.path}") |
| 92 | + expect(stdout_str).to match %r{Value type mismatch} |
| 93 | + expect(stdout_str).to match %r{secret_string: << redacted value >> } |
| 94 | + expect(stdout_str).not_to match %r{optional_secret} |
| 95 | + expect(stdout_str).not_to match %r{array_secret} |
| 96 | + expect(stdout_str).not_to match %r{variant_secret} |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + context 'with an optional sensitive string value that is invalid' do |
| 102 | + let(:device_credentials_content) do |
| 103 | + <<DEVICE_CREDS |
| 104 | +{ |
| 105 | + username: foo |
| 106 | + secret_string: wibble |
| 107 | + optional_secret: 12345 |
| 108 | + array_secret: [meep] |
| 109 | + variant_secret: 21 |
| 110 | +} |
| 111 | +DEVICE_CREDS |
| 112 | + end |
| 113 | + |
| 114 | + it 'Value type mismatch' do |
| 115 | + Tempfile.create('apply_success') do |f| |
| 116 | + f.write 'notify { "foo": }' |
| 117 | + f.close |
| 118 | + |
| 119 | + stdout_str, _status = Open3.capture2e("puppet device #{common_args} --deviceconfig #{device_conf.path} --apply #{f.path}") |
| 120 | + expect(stdout_str).to match %r{Value type mismatch} |
| 121 | + expect(stdout_str).not_to match %r{secret_string } |
| 122 | + expect(stdout_str).to match %r{optional_secret: << redacted value >>} |
| 123 | + expect(stdout_str).not_to match %r{array_secret} |
| 124 | + expect(stdout_str).not_to match %r{variant_secret} |
| 125 | + end |
| 126 | + end |
| 127 | + end |
| 128 | + |
| 129 | + context 'with an array of sensitive strings that is invalid' do |
| 130 | + let(:device_credentials_content) do |
| 131 | + <<DEVICE_CREDS |
| 132 | +{ |
| 133 | + username: foo |
| 134 | + secret_string: wibble |
| 135 | + optional_secret: bar |
| 136 | + array_secret: [17] |
| 137 | + variant_secret: 21 |
| 138 | +} |
| 139 | +DEVICE_CREDS |
| 140 | + end |
| 141 | + |
| 142 | + it 'Value type mismatch' do |
| 143 | + Tempfile.create('apply_success') do |f| |
| 144 | + f.write 'notify { "foo": }' |
| 145 | + f.close |
| 146 | + |
| 147 | + stdout_str, _status = Open3.capture2e("puppet device #{common_args} --deviceconfig #{device_conf.path} --apply #{f.path}") |
| 148 | + expect(stdout_str).to match %r{Value type mismatch} |
| 149 | + expect(stdout_str).not_to match %r{secret_string } |
| 150 | + expect(stdout_str).not_to match %r{optional_secret} |
| 151 | + expect(stdout_str).to match %r{array_secret: << redacted value >>} |
| 152 | + expect(stdout_str).not_to match %r{variant_secret} |
| 153 | + end |
| 154 | + end |
| 155 | + end |
| 156 | + |
| 157 | + context 'with an variant containing a sensitive value that is invalid' do |
| 158 | + let(:device_credentials_content) do |
| 159 | + <<DEVICE_CREDS |
| 160 | +{ |
| 161 | + username: foo |
| 162 | + secret_string: wibble |
| 163 | + optional_secret: bar |
| 164 | + array_secret: [meep] |
| 165 | + variant_secret: wobble |
| 166 | +} |
| 167 | +DEVICE_CREDS |
| 168 | + end |
| 169 | + |
| 170 | + it 'Value type mismatch' do |
| 171 | + Tempfile.create('apply_success') do |f| |
| 172 | + f.write 'notify { "foo": }' |
| 173 | + f.close |
| 174 | + |
| 175 | + stdout_str, _status = Open3.capture2e("puppet device #{common_args} --deviceconfig #{device_conf.path} --apply #{f.path}") |
| 176 | + expect(stdout_str).to match %r{Value type mismatch} |
| 177 | + expect(stdout_str).not_to match %r{secret_string } |
| 178 | + expect(stdout_str).not_to match %r{optional_secret} |
| 179 | + expect(stdout_str).not_to match %r{array_secret} |
| 180 | + expect(stdout_str).to match %r{variant_secret: << redacted value >>} |
| 181 | + end |
| 182 | + end |
| 183 | + end |
| 184 | + end |
| 185 | +end |
0 commit comments