Skip to content

Commit

Permalink
Implement allowlist for puppet module content
Browse files Browse the repository at this point in the history
This implements puppetlabs/puppet-specifications#157

* By default every file is ignored
* Only files from the official specification for puppet modules are
  added to the allowlist
* support for .pdkignore, .pmtignore and .gitignore is removed
  • Loading branch information
bastelfreak committed May 25, 2024
1 parent ea9757d commit 4053aaf
Showing 1 changed file with 23 additions and 37 deletions.
60 changes: 23 additions & 37 deletions lib/puppet/modulebuilder/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@
module Puppet::Modulebuilder
# Class to build Puppet Modules from source
class Builder
DEFAULT_IGNORED = [
'/.*',
'/pkg/',
'~*',
'/coverage',
'/checksums.json',
'/REVISION',
'/spec/fixtures/modules/',
'/vendor/'
IGNORED = [
'*',
'!/manifests',
'!/README*',
'!/metadata.json',
'!/LICENSE',
'!/hiera.yaml',
'!/data',
'!/templates',
'!/files',
'!/CHANGELOG*',
'!/docs',
'!/REFERENCE.md',
'!/locales',
'!/scripts',
'!/tasks',
'!/plans',
'!/types',
'!/bolt_plugin.json',
].freeze

attr_reader :destination, :logger
Expand Down Expand Up @@ -168,21 +178,6 @@ def warn_symlink(path)
from: symlink_path.relative_path_from(module_path), to: symlink_path.realpath.relative_path_from(module_path))
end

# Select the most appropriate ignore file in the module directory.
#
# In order of preference, we first try `.pdkignore`, then `.pmtignore`
# and finally `.gitignore`.
#
# @return [String] The path to the file containing the patterns of file
# paths to ignore.
def ignore_file
@ignore_file ||= [
File.join(source, '.pdkignore'),
File.join(source, '.pmtignore'),
File.join(source, '.gitignore')
].find { |file| file_exists?(file) && file_readable?(file) }
end

# Checks if the path contains any non-ASCII characters.
#
# Java will throw an error when it encounters a path containing
Expand Down Expand Up @@ -251,20 +246,11 @@ def build_package
def ignored_files
require 'pathspec'

@ignored_files ||=
begin
ignored = if ignore_file.nil?
PathSpec.new
else
PathSpec.new(read_file(ignore_file, open_args: 'rb:UTF-8'))
end

ignored = ignored.add("/#{File.basename(destination)}/") if File.realdirpath(destination).start_with?(File.realdirpath(source))
ignored = PathSpec.new
ignored = ignored.add("/#{File.basename(destination)}/") if File.realdirpath(destination).start_with?(File.realdirpath(source))
ignored = ignored.add(IGNORED.join("\n"))

DEFAULT_IGNORED.each { |r| ignored.add(r) }

ignored
end
ignored
end

# Create a temporary build directory where the files to be included in
Expand Down

0 comments on commit 4053aaf

Please sign in to comment.