|
193 | 193 | expect(exported_resources).not_to contain_sshkey('foo.example.com_ed25519') |
194 | 194 | end |
195 | 195 | end |
| 196 | + |
| 197 | + context 'with use_augeas enabled' do |
| 198 | + let :pre_condition do |
| 199 | + <<~PP |
| 200 | + define sshd_config ($ensure = present, $key = undef, $value = undef, $target = undef, $condition = undef) {} |
| 201 | + define sshd_config_subsystem ($command = undef) {} |
| 202 | + PP |
| 203 | + end |
| 204 | + |
| 205 | + let :params do |
| 206 | + { |
| 207 | + use_augeas: true, |
| 208 | + options: { |
| 209 | + 'X11Forwarding' => 'no', |
| 210 | + 'PermitRootLogin' => 'no', |
| 211 | + }, |
| 212 | + options_absent: ['GSSAPIAuthentication'], |
| 213 | + } |
| 214 | + end |
| 215 | + |
| 216 | + it { is_expected.to compile.with_all_deps } |
| 217 | + it { is_expected.not_to contain_concat('/etc/ssh/sshd_config') } |
| 218 | + |
| 219 | + it { |
| 220 | + is_expected.to contain_sshd_config('X11Forwarding').with( |
| 221 | + ensure: 'present', |
| 222 | + key: 'X11Forwarding', |
| 223 | + value: 'no', |
| 224 | + target: '/etc/ssh/sshd_config', |
| 225 | + ) |
| 226 | + } |
| 227 | + |
| 228 | + it { |
| 229 | + is_expected.to contain_sshd_config('PermitRootLogin').with( |
| 230 | + ensure: 'present', |
| 231 | + key: 'PermitRootLogin', |
| 232 | + value: 'no', |
| 233 | + ) |
| 234 | + } |
| 235 | + |
| 236 | + it { |
| 237 | + is_expected.to contain_sshd_config('GSSAPIAuthentication').with( |
| 238 | + ensure: 'absent', |
| 239 | + key: 'GSSAPIAuthentication', |
| 240 | + ) |
| 241 | + } |
| 242 | + end |
| 243 | + |
| 244 | + context 'with use_augeas and match block options' do |
| 245 | + let :pre_condition do |
| 246 | + <<~PP |
| 247 | + define sshd_config ($ensure = present, $key = undef, $value = undef, $target = undef, $condition = undef) {} |
| 248 | + define sshd_config_subsystem ($command = undef) {} |
| 249 | + PP |
| 250 | + end |
| 251 | + |
| 252 | + let :params do |
| 253 | + { |
| 254 | + use_augeas: true, |
| 255 | + options: { |
| 256 | + 'Match User www-data' => { |
| 257 | + 'ChrootDirectory' => '%h', |
| 258 | + 'ForceCommand' => 'internal-sftp', |
| 259 | + }, |
| 260 | + }, |
| 261 | + options_absent: [], |
| 262 | + } |
| 263 | + end |
| 264 | + |
| 265 | + it { is_expected.to compile.with_all_deps } |
| 266 | + |
| 267 | + it { |
| 268 | + is_expected.to contain_sshd_config('ChrootDirectory User www-data').with( |
| 269 | + ensure: 'present', |
| 270 | + condition: 'User www-data', |
| 271 | + key: 'ChrootDirectory', |
| 272 | + value: '%h', |
| 273 | + ) |
| 274 | + } |
| 275 | + |
| 276 | + it { |
| 277 | + is_expected.to contain_sshd_config('ForceCommand User www-data').with( |
| 278 | + ensure: 'present', |
| 279 | + condition: 'User www-data', |
| 280 | + key: 'ForceCommand', |
| 281 | + value: 'internal-sftp', |
| 282 | + ) |
| 283 | + } |
| 284 | + end |
| 285 | + |
| 286 | + context 'with use_issue_net enabled' do |
| 287 | + let :params do |
| 288 | + { |
| 289 | + use_issue_net: true, |
| 290 | + } |
| 291 | + end |
| 292 | + |
| 293 | + it { is_expected.to compile.with_all_deps } |
| 294 | + |
| 295 | + it { |
| 296 | + is_expected.to contain_file('/etc/issue.net').with( |
| 297 | + ensure: 'file', |
| 298 | + owner: 0, |
| 299 | + group: 0, |
| 300 | + ) |
| 301 | + } |
| 302 | + |
| 303 | + it { is_expected.to contain_file('/etc/issue.net').that_notifies("Service[#{svc_name}]") } |
| 304 | + |
| 305 | + it { |
| 306 | + is_expected.to contain_concat__fragment('banner file').with( |
| 307 | + target: '/etc/ssh/sshd_config', |
| 308 | + content: "Banner /etc/issue.net\n", |
| 309 | + order: '01', |
| 310 | + ) |
| 311 | + } |
| 312 | + end |
| 313 | + |
| 314 | + context 'with include_dir set' do |
| 315 | + let :params do |
| 316 | + { |
| 317 | + include_dir: '/etc/ssh/sshd_config.d', |
| 318 | + } |
| 319 | + end |
| 320 | + |
| 321 | + it { is_expected.to compile.with_all_deps } |
| 322 | + |
| 323 | + it { |
| 324 | + is_expected.to contain_file('/etc/ssh/sshd_config.d').with( |
| 325 | + ensure: 'directory', |
| 326 | + owner: 0, |
| 327 | + group: 0, |
| 328 | + mode: '0700', |
| 329 | + purge: true, |
| 330 | + recurse: true, |
| 331 | + ) |
| 332 | + } |
| 333 | + end |
| 334 | + |
| 335 | + context 'with include_dir and include_dir_purge false' do |
| 336 | + let :params do |
| 337 | + { |
| 338 | + include_dir: '/etc/ssh/sshd_config.d', |
| 339 | + include_dir_purge: false, |
| 340 | + } |
| 341 | + end |
| 342 | + |
| 343 | + it { is_expected.to compile.with_all_deps } |
| 344 | + |
| 345 | + it { |
| 346 | + is_expected.to contain_file('/etc/ssh/sshd_config.d').with( |
| 347 | + ensure: 'directory', |
| 348 | + purge: false, |
| 349 | + recurse: false, |
| 350 | + ) |
| 351 | + } |
| 352 | + end |
| 353 | + |
| 354 | + context 'with include_dir and custom mode' do |
| 355 | + let :params do |
| 356 | + { |
| 357 | + include_dir: '/etc/ssh/sshd_config.d', |
| 358 | + include_dir_mode: '0755', |
| 359 | + } |
| 360 | + end |
| 361 | + |
| 362 | + it { |
| 363 | + is_expected.to contain_file('/etc/ssh/sshd_config.d').with( |
| 364 | + mode: '0755', |
| 365 | + ) |
| 366 | + } |
| 367 | + end |
| 368 | + |
| 369 | + context 'with config_files' do |
| 370 | + let :params do |
| 371 | + { |
| 372 | + include_dir: '/etc/ssh/sshd_config.d', |
| 373 | + config_files: { |
| 374 | + 'hardening' => { |
| 375 | + 'options' => { |
| 376 | + 'PermitRootLogin' => 'no', |
| 377 | + }, |
| 378 | + }, |
| 379 | + 'logging' => { |
| 380 | + 'options' => { |
| 381 | + 'LogLevel' => 'VERBOSE', |
| 382 | + }, |
| 383 | + }, |
| 384 | + }, |
| 385 | + } |
| 386 | + end |
| 387 | + |
| 388 | + it { is_expected.to compile.with_all_deps } |
| 389 | + it { is_expected.to contain_ssh__server__config_file('hardening') } |
| 390 | + it { is_expected.to contain_ssh__server__config_file('logging') } |
| 391 | + |
| 392 | + it { |
| 393 | + is_expected.to contain_concat('/etc/ssh/sshd_config.d/hardening.conf').with( |
| 394 | + ensure: 'present', |
| 395 | + owner: 0, |
| 396 | + group: 0, |
| 397 | + ) |
| 398 | + } |
| 399 | + |
| 400 | + it { |
| 401 | + is_expected.to contain_concat('/etc/ssh/sshd_config.d/logging.conf').with( |
| 402 | + ensure: 'present', |
| 403 | + owner: 0, |
| 404 | + group: 0, |
| 405 | + ) |
| 406 | + } |
| 407 | + end |
| 408 | + |
| 409 | + # Skip OSes where hiera sets include_dir by default (e.g. RedHat 9) |
| 410 | + context 'without include_dir but with config_files', unless: os_facts.dig(:os, 'family') == 'RedHat' && os_facts.dig(:os, 'release', 'major') == '9' do |
| 411 | + let :params do |
| 412 | + { |
| 413 | + config_files: { |
| 414 | + 'hardening' => { |
| 415 | + 'options' => {}, |
| 416 | + }, |
| 417 | + }, |
| 418 | + } |
| 419 | + end |
| 420 | + |
| 421 | + it { is_expected.to compile.with_all_deps } |
| 422 | + it { is_expected.not_to contain_ssh__server__config_file('hardening') } |
| 423 | + end |
196 | 424 | end |
197 | 425 | end |
198 | 426 | end |
0 commit comments