Skip to content

Commit 23befd0

Browse files
committed
(maint) - remove Binford2k module
1 parent c8de198 commit 23befd0

File tree

11 files changed

+99
-101
lines changed

11 files changed

+99
-101
lines changed

.rubocop_todo.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ RSpec/NamedSubject:
5959
RSpec/StubbedMock:
6060
Exclude:
6161
- 'spec/functions/node_encrypt_spec.rb'
62-
- 'spec/unit/puppet_x/binford2k/node_encrypt_spec.rb'
62+
- 'spec/unit/puppet_x/node_encrypt_spec.rb'
6363

6464
# Offense count: 1
6565
# This cop supports unsafe autocorrection (--autocorrect-all).
6666
# Configuration parameters: EnforcedStyle.
6767
# SupportedStyles: nested, compact
6868
Style/ClassAndModuleChildren:
6969
Exclude:
70-
- 'lib/puppet_x/binford2k/node_encrypt.rb'
70+
- 'lib/puppet_x/node_encrypt.rb'

lib/puppet/face/node/encrypt.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
require 'puppet/face'
4-
require 'puppet_x/binford2k/node_encrypt'
4+
require 'puppet_x/node_encrypt'
55

66
Puppet::Face.define(:node, '0.0.1') do
77
action :encrypt do
@@ -45,7 +45,7 @@
4545
text = args.join(' ')
4646
end
4747

48-
PuppetX::Binford2k::NodeEncrypt.encrypt(text, options[:target])
48+
PuppetX::NodeEncrypt.encrypt(text, options[:target])
4949
end
5050
end
5151

@@ -76,11 +76,11 @@
7676

7777
when_invoked do |options|
7878
if options.include? :data
79-
PuppetX::Binford2k::NodeEncrypt.decrypt(options[:data])
79+
PuppetX::NodeEncrypt.decrypt(options[:data])
8080
elsif options.include? :env
81-
PuppetX::Binford2k::NodeEncrypt.decrypt(ENV.fetch(options[:env], nil))
81+
PuppetX::NodeEncrypt.decrypt(ENV.fetch(options[:env], nil))
8282
else
83-
PuppetX::Binford2k::NodeEncrypt.decrypt($stdin.read)
83+
PuppetX::NodeEncrypt.decrypt($stdin.read)
8484
end
8585
end
8686
end

lib/puppet/functions/node_decrypt.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require_relative '../../puppet_x/binford2k/node_encrypt'
3+
require_relative '../../puppet_x/node_encrypt'
44

55
# Decrypt data with node_encrypt. This is intended to be used as a
66
# Deferred function on the _agent_ via the node_encrypted::secret wrapper.
@@ -12,7 +12,7 @@
1212

1313
def decrypt(content)
1414
Puppet::Pops::Types::PSensitiveType::Sensitive.new(
15-
PuppetX::Binford2k::NodeEncrypt.decrypt(content),
15+
PuppetX::NodeEncrypt.decrypt(content),
1616
)
1717
end
1818
end

lib/puppet/functions/node_encrypt.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require_relative '../../puppet_x/binford2k/node_encrypt'
3+
require_relative '../../puppet_x/node_encrypt'
44

55
# @summary
66
# Encrypt data with node_encrypt.
@@ -16,7 +16,7 @@
1616

1717
def simple_encrypt(content)
1818
certname = closure_scope['clientcert']
19-
PuppetX::Binford2k::NodeEncrypt.encrypt(content, certname)
19+
PuppetX::NodeEncrypt.encrypt(content, certname)
2020
end
2121

2222
def sensitive_encrypt(content)

lib/puppet/parser/functions/node_encrypt.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require_relative '../../../puppet_x/binford2k/node_encrypt'
3+
require_relative '../../../puppet_x/node_encrypt'
44

55
Puppet::Parser::Functions.newfunction(:node_encrypt,
66
type: :rvalue,
@@ -14,5 +14,5 @@
1414
content = content.unwrap if defined?(Puppet::Pops::Types::PSensitiveType::Sensitive) && content.is_a?(Puppet::Pops::Types::PSensitiveType::Sensitive)
1515

1616
certname = lookupvar('clientcert')
17-
PuppetX::Binford2k::NodeEncrypt.encrypt(content, certname)
17+
PuppetX::NodeEncrypt.encrypt(content, certname)
1818
end

lib/puppet_x/binford2k/node_encrypt.rb

-80
This file was deleted.

lib/puppet_x/node_encrypt.rb

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# frozen_string_literal: true
2+
3+
module PuppetX
4+
class NodeEncrypt # rubocop:disable Style/Documentation
5+
def self.encrypted?(data)
6+
raise ArgumentError, 'Only strings can be encrypted' unless data.instance_of?(String)
7+
8+
# ridiculously faster than a regex
9+
data.start_with?('-----BEGIN PKCS7-----')
10+
end
11+
12+
def self.encrypt(data, destination)
13+
raise ArgumentError, 'Can only encrypt strings' unless data.instance_of?(String)
14+
raise ArgumentError, 'Need a node name to encrypt for' unless destination.instance_of?(String)
15+
16+
certpath = Puppet.settings[:hostcert]
17+
keypath = Puppet.settings[:hostprivkey]
18+
19+
# A dummy password with at least 4 characters is required here
20+
# since Ruby 2.4 which enforces a minimum password length
21+
# of 4 bytes. This is true even if the key has no password
22+
# at all--in which case the password we supply is ignored.
23+
# We can pass in a dummy here, since we know the certificate
24+
# has no password.
25+
key = OpenSSL::PKey::RSA.new(File.read(keypath), '1234')
26+
cert = OpenSSL::X509::Certificate.new(File.read(certpath))
27+
28+
# if we're on the CA, we've got a copy of the clientcert from the start.
29+
# This allows the module to work with no classification at all on single
30+
# monolithic server setups
31+
destpath = [
32+
"#{Puppet.settings[:signeddir]}/#{destination}.pem",
33+
"#{Puppet.settings[:certdir]}/#{destination}.pem",
34+
].find { |path| File.exist? path }
35+
36+
# for safer upgrades, let's default to the known good pathway for now
37+
if destpath
38+
target = OpenSSL::X509::Certificate.new(File.read(destpath))
39+
else
40+
# if we don't have a cert, check for it in $facts
41+
scope = Puppet.lookup(:global_scope)
42+
43+
if scope.exist?('clientcert_pem')
44+
hostcert = scope.lookupvar('clientcert_pem')
45+
target = OpenSSL::X509::Certificate.new(hostcert)
46+
else
47+
url = 'https://github.com/puppetlabs/puppetlabs-node_encrypt#automatically-distributing-certificates-to-compile-servers'
48+
raise ArgumentError, "Client certificate does not exist. See #{url} for more info."
49+
end
50+
end
51+
52+
signed = OpenSSL::PKCS7.sign(cert, key, data, [], OpenSSL::PKCS7::BINARY)
53+
cipher = OpenSSL::Cipher.new('AES-128-CFB')
54+
55+
OpenSSL::PKCS7.encrypt([target], signed.to_der, cipher, OpenSSL::PKCS7::BINARY).to_s
56+
end
57+
58+
def self.decrypt(data)
59+
raise ArgumentError, 'Can only decrypt strings' unless data.instance_of?(String)
60+
61+
cert = OpenSSL::X509::Certificate.new(File.read(Puppet.settings[:hostcert]))
62+
# Same dummy password as above.
63+
key = OpenSSL::PKey::RSA.new(File.read(Puppet.settings[:hostprivkey]), '1234')
64+
source = OpenSSL::X509::Certificate.new(File.read(Puppet.settings[:localcacert]))
65+
66+
store = OpenSSL::X509::Store.new
67+
store.add_cert(source)
68+
69+
blob = OpenSSL::PKCS7.new(data)
70+
decrypted = blob.decrypt(key, cert)
71+
verified = OpenSSL::PKCS7.new(decrypted)
72+
73+
raise ArgumentError, 'Signature verification failed' unless verified.verify(nil, store, nil, OpenSSL::PKCS7::NOVERIFY)
74+
75+
verified.data
76+
end
77+
end
78+
end

spec/classes/certificates_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
require 'spec_helper'
4-
require 'puppet_x/binford2k/node_encrypt'
4+
require 'puppet_x/node_encrypt'
55

66
describe 'node_encrypt::certificates' do
77
before(:each) do

spec/defines/file_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
require 'spec_helper'
4-
require 'puppet_x/binford2k/node_encrypt'
4+
require 'puppet_x/node_encrypt'
55

66
describe 'node_encrypt::file' do
77
context 'when ensuring present' do
@@ -48,7 +48,7 @@
4848
end
4949

5050
before(:each) do
51-
allow(PuppetX::Binford2k::NodeEncrypt).to receive(:decrypt).with('encrypted').and_return('decrypted')
51+
allow(PuppetX::NodeEncrypt).to receive(:decrypt).with('encrypted').and_return('decrypted')
5252
end
5353

5454
it {

spec/functions/node_encrypt_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
require 'spec_helper'
4-
require 'puppet_x/binford2k/node_encrypt'
4+
require 'puppet_x/node_encrypt'
55

66
describe 'node_encrypt' do
77
let(:node) { 'testhost.example.com' }
@@ -11,13 +11,13 @@
1111
end
1212

1313
it 'receives foobar and returns encrypted' do
14-
expect(PuppetX::Binford2k::NodeEncrypt).to receive(:encrypt).with('foobar', 'testhost.example.com').and_return('encrypted')
14+
expect(PuppetX::NodeEncrypt).to receive(:encrypt).with('foobar', 'testhost.example.com').and_return('encrypted')
1515
expect(scope.function_node_encrypt(['foobar'])).to eq('encrypted')
1616
end
1717

1818
if defined?(Puppet::Pops::Types::PSensitiveType::Sensitive)
1919
it 'receives sensitive value and returns encrypted' do
20-
expect(PuppetX::Binford2k::NodeEncrypt).to receive(:encrypt).with('foobar', 'testhost.example.com').and_return('encrypted')
20+
expect(PuppetX::NodeEncrypt).to receive(:encrypt).with('foobar', 'testhost.example.com').and_return('encrypted')
2121
expect(scope.function_node_encrypt([Puppet::Pops::Types::PSensitiveType::Sensitive.new('foobar')])).to eq('encrypted')
2222
end
2323
end

spec/unit/puppet_x/binford2k/node_encrypt_spec.rb spec/unit/puppet_x/node_encrypt_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require 'openssl'
44
require 'spec_helper'
5-
require 'puppet_x/binford2k/node_encrypt'
5+
require 'puppet_x/node_encrypt'
66

77
ca_crt_pem = "-----BEGIN CERTIFICATE-----
88
MIIGGjCCBAKgAwIBAgIBATANBgkqhkiG9w0BAQsFADBaMVgwVgYDVQQDDE9QdXBw
@@ -252,7 +252,7 @@
252252
uSI28VzZYavkITj+2D6tMys=
253253
-----END PKCS7-----"
254254

255-
describe PuppetX::Binford2k::NodeEncrypt do
255+
describe PuppetX::NodeEncrypt do
256256
let(:node) { 'testhost.example.com' }
257257

258258
it 'decrypts values which have been encrypted' do

0 commit comments

Comments
 (0)