cem_acpt 0.2.9-universal-java-17 → 0.2.10-universal-java-17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d4181b3fae3e4d3f0029314896873f2f604374cc4583473b0b05e689464d346f
4
- data.tar.gz: 5e01be5d223962785a18e685f17cc1c3cd393f705cd2b5df174327a8631e1598
3
+ metadata.gz: 5d59f3f4432694cd766fc5ca304ade0cad8cb681849181256d8c1ced3ba68438
4
+ data.tar.gz: eff162b600769222a8d0670e2a528cc389eae0407e35a1d6671c1518248c0480
5
5
  SHA512:
6
- metadata.gz: e9dc631cf11d32f85405358a8bc4e8501afc290a15ed68e80688472de838cfdd81cf09e9a10d346f396de6f1c2505188c443c60e0d4b106b346fd77f079c311c
7
- data.tar.gz: 7866ca7c6272369e15c2fbf3f2dd69ce6d6caf6d059b08ad686e7b2fcaf5d63d5d093dbfac076c4c68f3ac7c07f4bcacc2bcd01107667531e069821c00e90313
6
+ metadata.gz: a8124eec84379d5737fc4e04327ded0de1da8cb43c724a51cfd892c150823b58b14f9f6f99ec24adff5a7bc69302595e79de34501326c1c6e85fe1b0fa3c99cc
7
+ data.tar.gz: c6ea839545a9c00779aec17f331a9769a743baf7e05654f25d9efaa0e7edd8490dc528342ba75cdef171dc79328ad0431b204310c53d736f0b8bd2faf71ac6b4
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cem_acpt (0.2.9-universal-java-17)
4
+ cem_acpt (0.2.10-universal-java-17)
5
5
  concurrent-ruby (>= 1.1, < 2.0)
6
6
  deep_merge (>= 1.2, < 2.0)
7
7
  ed25519 (>= 1.2, < 2.0)
@@ -36,10 +36,8 @@ module CemAcpt
36
36
  # Creates a SSH key and a SSH known hosts file for the acceptance test suite
37
37
  def new_test_ssh_key
38
38
  log('Creating ephemeral SSH key and known hosts file for acceptance test suites...')
39
- @ssh_priv_key, @ssh_pub_key = CemAcpt::Utils::SSH.ephemeral_ssh_key
40
- @ssh_known_hosts = CemAcpt::Utils::SSH.acpt_known_hosts
41
- CemAcpt::Utils::SSH.set_ssh_file_permissions(@ssh_priv_key, @ssh_pub_key, @ssh_known_hosts)
42
- log('Successfully created SSH files...')
39
+ @ssh_priv_key, @ssh_pub_key, @ssh_known_hosts = CemAcpt::Utils::SSH::Ephemeral.create
40
+ log('Successfully created SSH files.')
43
41
  log("SSH private key: #{@ssh_priv_key}", :debug)
44
42
  log("SSH public key: #{@ssh_pub_key}", :debug)
45
43
  log("SSH known hosts: #{@ssh_known_hosts}", :debug)
@@ -48,7 +46,9 @@ module CemAcpt
48
46
  # Deletes acceptance test suite SSH files
49
47
  def clean_test_ssh_key
50
48
  log('Deleting ephemeral ssh keys and acpt_known_hosts if they exist...')
51
- [@ssh_priv_key, @ssh_pub_key, @ssh_known_hosts].map { |f| File.delete(f) if File.exist?(f) }
49
+ cleaned = CemAcpt::Utils::SSH::Ephemeral.clean
50
+ log('Successfully cleaned ephemeral ssh files.')
51
+ log("Deleted: #{cleaned}", :debug)
52
52
  end
53
53
 
54
54
  # Prints a period to the terminal every 5 seconds in a single line to keep the terminal
@@ -62,8 +62,8 @@ module CemAcpt
62
62
  end
63
63
 
64
64
  def clean_up_test_suite(opts)
65
- @ctx.node_inventory.clear!
66
- @ctx.node_inventory.clean_local_files
65
+ @ctx&.node_inventory&.clear!
66
+ @ctx&.node_inventory&.clean_local_files
67
67
  clean_test_ssh_key unless opts[:no_ephemeral_ssh_key]
68
68
  @run_handler&.destroy_test_nodes
69
69
  @keep_terminal_alive&.kill
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CemAcpt
4
+ module Logging
5
+ module Formatter
6
+ class << self
7
+ def for(log_format)
8
+ all.find { |f| f.log_format == log_format.downcase.to_sym }.get
9
+ end
10
+
11
+ private
12
+
13
+ def all
14
+ @all ||= [FileFormatter.new, JSONFormatter.new, TextFormatter.new, GithubActionFormatter.new]
15
+ end
16
+ end
17
+
18
+ class FileFormatter
19
+ attr_reader :log_format
20
+
21
+ def initialize
22
+ @log_format = :file
23
+ end
24
+
25
+ def get
26
+ proc do |severity, datetime, progname, msg|
27
+ format(severity, datetime, progname, msg)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def format(severity, datetime, _progname, msg)
34
+ "[#{datetime}] #{severity}: #{msg}\n"
35
+ end
36
+ end
37
+
38
+ class JSONFormatter < FileFormatter
39
+ def initialize
40
+ super
41
+ @log_format = :json
42
+ end
43
+
44
+ private
45
+
46
+ def format(severity, datetime, progname, msg)
47
+ require 'json'
48
+ {
49
+ timestamp: datetime,
50
+ progname: progname,
51
+ severity: severity,
52
+ message: msg,
53
+ }.to_json + "\n"
54
+ end
55
+ end
56
+
57
+ class TextFormatter < FileFormatter
58
+ def initialize
59
+ super
60
+ @log_format = :text
61
+ end
62
+
63
+ private
64
+
65
+ def format(severity, _datetime, _progname, msg)
66
+ "#{severity} - #{msg}\n"
67
+ end
68
+ end
69
+
70
+ class GithubActionFormatter < FileFormatter
71
+ SEV_MAP = {
72
+ 'DEBUG' => '::debug',
73
+ 'INFO' => '::notice',
74
+ 'WARN' => '::warning',
75
+ 'ERROR' => '::error',
76
+ 'FATAL' => '::error',
77
+ }.freeze
78
+
79
+ def initialize
80
+ super
81
+ @log_format = :github_action
82
+ end
83
+
84
+ private
85
+
86
+ def format(severity, _datetime, _progname, msg)
87
+ if severity == 'DEBUG'
88
+ "#{SEV_MAP[severity]}::{#{msg}}\n"
89
+ else
90
+ "#{SEV_MAP[severity]} #{msg}\n"
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'logger'
4
+ require 'cem_acpt/logging/formatter'
4
5
 
5
6
  module CemAcpt
6
7
  # Logging for CemAcpt
@@ -92,41 +93,7 @@ module CemAcpt
92
93
  # @param f [Symbol] the log format style to set
93
94
  # @return [Proc] the proc to be passed to Logger#formatter=
94
95
  def new_log_formatter(f)
95
- case f.downcase.to_sym
96
- when :json
97
- require 'json'
98
- @current_log_format = :json
99
- proc do |severity, datetime, progname, msg|
100
- {
101
- timestamp: datetime,
102
- progname: progname,
103
- severity: severity,
104
- message: msg,
105
- }.to_json + "\n"
106
- end
107
- when :text
108
- @current_log_format = :text
109
- proc do |severity, _datetime, _progname, msg|
110
- "#{severity} - #{msg}\n"
111
- end
112
- when :github_action
113
- @current_log_format = :github_action
114
- sev_map = {
115
- 'DEBUG' => '::debug::',
116
- 'INFO' => '::notice::',
117
- 'WARN' => '::warning::',
118
- 'ERROR' => '::error::',
119
- 'FATAL' => '::error::',
120
- }
121
- proc do |severity, _datetime, _progname, msg|
122
- "#{sev_map[severity]}{#{msg}}\n"
123
- end
124
- else
125
- @current_log_format = :file
126
- proc do |severity, datetime, _progname, msg|
127
- "[#{datetime}] #{severity}: #{msg}\n"
128
- end
129
- end
96
+ CemAcpt::Logging::Formatter.for(f)
130
97
  end
131
98
 
132
99
  # Returns the current log config if set, or the default if not.
@@ -81,6 +81,40 @@ module CemAcpt
81
81
 
82
82
  # SSH-related utilities
83
83
  module SSH
84
+ module Ephemeral
85
+ PRIV_KEY = 'acpt_test_key'
86
+ CREATE_OPTS = {
87
+ type: 'rsa',
88
+ bits: '4096',
89
+ comment: 'Ephemeral for cem_acpt',
90
+ password: '',
91
+ known_hosts: 'acpt_known_hosts',
92
+ overwrite_known_hosts: true,
93
+ }.freeze
94
+
95
+ class << self
96
+ attr_accessor :ephemeral_keydir
97
+ end
98
+
99
+ def self.create(keydir: CemAcpt::Utils::SSH.default_keydir)
100
+ self.ephemeral_keydir = keydir
101
+ @priv_key, @pub_key, @known_hosts = CemAcpt::Utils::SSH.create(PRIV_KEY, keydir: ephemeral_keydir, **CREATE_OPTS)
102
+ [@priv_key, @pub_key, @known_hosts]
103
+ end
104
+
105
+ def self.clean
106
+ [@priv_key, @pub_key, @known_hosts].each_with_object([]) do |f, arr|
107
+ next unless f
108
+
109
+ path = CemAcpt::Utils::SSH.file_path(f, keydir: ephemeral_keydir)
110
+ if ::File.exist?(path)
111
+ ::File.delete(path)
112
+ arr << path
113
+ end
114
+ end
115
+ end
116
+ end
117
+
84
118
  def self.ssh_keygen
85
119
  bin_path = `#{ENV['SHELL']} -c 'command -v ssh-keygen'`.chomp
86
120
  raise 'Cannot find ssh-keygen! Install it and verify PATH' unless bin_path
@@ -97,26 +131,52 @@ module CemAcpt
97
131
  ssh_dir
98
132
  end
99
133
 
100
- def self.ephemeral_ssh_key(type: 'rsa', bits: '4096', comment: nil, keydir: default_keydir)
101
- raise ArgumentError, 'keydir does not exist' unless ::File.directory?(keydir)
134
+ def self.file_path(file_name, keydir: default_keydir)
135
+ ::File.join(keydir, file_name)
136
+ end
137
+
138
+ # Takes a file name (not path) and optional SSH key directory and returns the paths
139
+ # to the private key and public key based on the file name given.
140
+ # @param file_name [String] The base name for the keys
141
+ # @param keydir [String] An optional SSH key directory
142
+ def self.key_paths(file_name, keydir: default_keydir)
143
+ [file_path(file_name, keydir: keydir), file_path("#{file_name}.pub", keydir: keydir)]
144
+ end
102
145
 
103
- keyfile = ::File.join(keydir, 'acpt_test_key')
104
- keygen_cmd = [ssh_keygen, "-t #{type}", "-b #{bits}", "-f #{keyfile}", '-N ""']
146
+ def self.create(key_name, type: 'rsa', bits: '4096', comment: nil, password: '', known_hosts: nil, overwrite_known_hosts: true, keydir: default_keydir)
147
+ raise ArgumentError, "Key directory #{keydir} does not exist" unless ::File.directory?(keydir)
148
+
149
+ keys = key_paths(key_name, keydir: keydir)
150
+ # If we don't delete an existing key file, generation will fail
151
+ keys.each { |f| ::File.delete(f) if ::File.exist?(f) }
152
+ keygen_cmd = [ssh_keygen, "-t #{type}", "-b #{bits}", "-f #{keys[0]}", "-N '#{password}'"]
105
153
  keygen_cmd << "-C \"#{comment}\"" if comment
106
154
  _, stderr, status = Open3.capture3(keygen_cmd.join(' '))
107
- raise "Failed to generate ephemeral SSH key: #{stderr}" unless status.success?
155
+ raise "Failed to generate ephemeral SSH key: STDOUT: #{stdout}; STDERR: #{stderr}" unless status.success?
108
156
 
109
- [keyfile, "#{keyfile}.pub"]
157
+ keys << create_known_hosts(known_hosts, overwrite: overwrite_known_hosts, keydir: keydir)
158
+ set_ssh_file_permissions(*keys)
159
+ keys
110
160
  end
111
161
 
112
- def self.acpt_known_hosts(keydir: default_keydir, file_name: 'acpt_known_hosts', overwrite: true)
113
- kh_file = ::File.join(keydir, file_name)
162
+ def self.create_known_hosts(known_hosts, overwrite: true, keydir: default_keydir)
163
+ return nil unless known_hosts
164
+
165
+ kh_file = file_path(known_hosts, keydir: keydir)
114
166
  ::File.open(kh_file, 'w') { |f| f.write("\n") } unless ::File.exist?(kh_file) && !overwrite
115
167
  kh_file
116
168
  end
117
169
 
118
- def self.set_ssh_file_permissions(priv_key, pub_key, known_hosts)
119
- CemAcpt::Utils::File.set_permissions(0o600, priv_key, pub_key, known_hosts)
170
+ def self.set_ssh_file_permissions(*files)
171
+ CemAcpt::Utils::File.set_permissions(0o600, *files.uniq.compact)
172
+ end
173
+
174
+ def self.ephemeral_ssh_key(keydir: default_keydir)
175
+ CemAcpt::Utils::SSH::Ephemeral.create(keydir: keydir)
176
+ end
177
+
178
+ def self.clean_ephemeral_keys
179
+ CemAcpt::Utils::SSH::Ephemeral.clean
120
180
  end
121
181
  end
122
182
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CemAcpt
4
- VERSION = '0.2.9'
4
+ VERSION = '0.2.10'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cem_acpt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.9
4
+ version: 0.2.10
5
5
  platform: universal-java-17
6
6
  authors:
7
7
  - puppetlabs
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-06-28 00:00:00.000000000 Z
11
+ date: 2022-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -160,6 +160,7 @@ files:
160
160
  - lib/cem_acpt/core_extensions.rb
161
161
  - lib/cem_acpt/image_name_builder.rb
162
162
  - lib/cem_acpt/logging.rb
163
+ - lib/cem_acpt/logging/formatter.rb
163
164
  - lib/cem_acpt/platform.rb
164
165
  - lib/cem_acpt/platform/base.rb
165
166
  - lib/cem_acpt/platform/base/cmd.rb