Module: MetadataJsonLint

Defined in:
lib/metadata_json_lint.rb,
lib/metadata-json-lint/schema.rb,
lib/metadata-json-lint/version_requirement.rb

Defined Under Namespace

Classes: Schema, VersionRequirement

Constant Summary collapse

MIN_PUPPET_VER =
'4.10.0'.freeze
INVALID_ESCAPE_REGEX =

Regex looks for:

  1. Invalid escape sequences (\x or incomplete \u)
%r{\\[^"/bfnrtu]|\\u(?![0-9a-fA-F]{4})}

Class Method Summary collapse

Class Method Details

.contains_invalid_escape?(content) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/metadata_json_lint.rb', line 75

def contains_invalid_escape?(content)
  content.match?(INVALID_ESCAPE_REGEX)
end

.error(check, msg) ⇒ Object



278
279
280
281
282
# File 'lib/metadata_json_lint.rb', line 278

def error(check, msg)
  @errors ||= []

  @errors << format_error(check, msg)
end

.format_error(check, msg) ⇒ Object



261
262
263
264
265
266
267
268
# File 'lib/metadata_json_lint.rb', line 261

def format_error(check, msg)
  case options[:format]
  when :json
    { check: check, msg: msg }
  else
    "#{check}: #{msg}"
  end
end

.misses_newline_at_end?(content) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/metadata_json_lint.rb', line 80

def misses_newline_at_end?(content)
  content[-1] != "\n"
end

.optionsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/metadata_json_lint.rb', line 15

def options
  @options ||= Struct.new(
    :fail_on_warnings,
    :strict_license,
    :strict_dependencies,
    :strict_puppet_version,
    :format,
  ).new(
    true, # fail_on_warnings
    true, # strict_license
    false, # strict_dependencies
    false, # strict_puppet_version
    'text', # format
  )
end

.parse(metadata) {|options| ... } ⇒ Object

Yields:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/metadata_json_lint.rb', line 85

def parse()
  @errors = []
  @warnings = []

  # Small hack to use the module settings as defaults but allow overriding for different rake tasks
  options = options().clone
  # Configuration from rake tasks
  yield options if block_given?

  (, options) do |level, check, message|
    send(level, check, message)
  end

  if !@errors.empty? || !@warnings.empty?
    result = @errors.empty? ? "Warnings found in #{}" : "Errors found in #{}"

    case options[:format]
    when :json
      puts JSON.fast_generate(result: result, warnings: @warnings, errors: @errors)
    else
      @warnings.each { |warn| puts "(WARN) #{warn}" }
      @errors.each { |err| puts "(ERROR) #{err}" }
      puts result
    end

    return false if !@errors.empty? || (!@warnings.empty? && (options[:fail_on_warnings] == true))
  end

  true
end

.runObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/metadata_json_lint.rb', line 32

def run
  OptionParser.new do |opts|
    opts.banner = 'Usage: metadata-json-lint [options] [metadata.json]'

    opts.on('--[no-]strict-dependencies',
            "Fail on open-ended module version dependencies. Defaults to '#{options[:strict_dependencies]}'.") do |v|
      options[:strict_dependencies] = v
    end

    opts.on('--[no-]strict-license',
            "Don't fail on strict license check. Defaults to '#{options[:strict_license]}'.") do |v|
      options[:strict_license] = v
    end

    opts.on('--[no-]fail-on-warnings', "Fail on any warnings. Defaults to '#{options[:fail_on_warnings]}'.") do |v|
      options[:fail_on_warnings] = v
    end

    opts.on('--[no-]strict-puppet-version',
            "Fail on strict Puppet Version check based on current supported Puppet versions. Defaults to '#{options[:strict_puppet_version]}'.") do |v|
      options[:strict_puppet_version] = v
    end

    opts.on('-f', '--format FORMAT', %i[text json],
            'The format in which results will be output (text, json)') do |format|
      options[:format] = format
    end
  end.parse!

  mj = if ARGV[0].nil?
         if File.readable?('metadata.json')
           'metadata.json'
         else
           abort('Error: metadata.json is not readable or does not exist.')
         end
       else
         ARGV[0]
       end

  exit(MetadataJsonLint.parse(mj) ? 0 : 1)
end

.validate_dependencies!(deps) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/metadata_json_lint.rb', line 216

def validate_dependencies!(deps)
  dep_names = []
  deps.each do |dep|
    warn :dependencies, "Duplicate dependencies on #{dep['name']}" if dep_names.include?(dep['name'])
    dep_names << dep['name']

    begin
      requirement = VersionRequirement.new(dep.fetch('version_requirement', ''))
    rescue ArgumentError => e
      # Raised when the version_requirement provided could not be parsed
      error :dependencies, "Invalid 'version_requirement' field in metadata.json: #{e}"
      # Skip to the next dependency
      next
    end
    validate_version_requirement!(dep, requirement)

    # 'version_range' is no longer used by the forge
    # See https://tickets.puppetlabs.com/browse/PUP-2781
    if dep.key?('version_range')
      warn :dependencies, "Dependency #{dep['name']} has a 'version_range' attribute " \
                          'which is no longer used by the forge.'
    end
  end
end

.validate_metadata(metadata, options) {|:error, :file, 'metadata.json does not have a valid newline at the end'| ... } ⇒ Object

Yields:

  • (:error, :file, 'metadata.json does not have a valid newline at the end')


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/metadata_json_lint.rb', line 117

def (, options)
  begin
    f = File.read()
  rescue StandardError => e
    yield :error, :file, "Unable to read metadata file: #{e.message.split(' @ ').first}"
    return
  end

  yield :error, :file, 'metadata.json does not have a valid newline at the end' if misses_newline_at_end?(f)

  if contains_invalid_escape?(f)
    yield :error, :file, 'Unable to parse metadata.json: Invalid escape character in string'
    return
  end

  begin
    parsed = JSON.parse(f)
  rescue JSON::ParserError => e
    yield :error, :file, "Unable to parse metadata.json: #{e.message}"
    return
  end

  # Validate basic structure against JSON schema
  schema_errors = Schema.new.validate(parsed)
  schema_errors.each do |err|
    yield :error, ((err[:field] == 'root') ? :required_fields : err[:field]), err[:message]
  end

  validate_dependencies!(parsed['dependencies']) if parsed['dependencies']

  # Deprecated fields
  # From: https://docs.puppetlabs.com/puppet/latest/reference/modules_publishing.html#write-a-metadatajson-file
  deprecated_fields = %w[types checksum]
  deprecated_fields.each do |field|
    yield :error, :deprecated_fields, "Deprecated field '#{field}' found in metadata.json." unless parsed[field].nil?
  end

  # The nested 'requirements' name of 'pe' is deprecated as well.
  # https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/puppet-users/nkRPvG4q0Oo/GmXa109aJQAJ
  validate_requirements!(parsed['requirements']) if parsed['requirements']

  # Shoulds/recommendations
  # From: https://docs.puppetlabs.com/puppet/latest/reference/modules_publishing.html#write-a-metadatajson-file
  #
  return unless options[:strict_license] && !parsed['license'].nil? && !SpdxLicenses.exist?(parsed['license']) && parsed['license'] != 'proprietary'

  msg = "License identifier #{parsed['license']} is not in the SPDX list: http://spdx.org/licenses/"
  yield :warn, :license, msg
end

.validate_puppet_ver!(requirement) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/metadata_json_lint.rb', line 200

def validate_puppet_ver!(requirement)
  if options[:strict_puppet_version] && requirement.open_ended?
    warn(:requirement, "Puppet has an open ended version requirement #{requirement.ver_range}")
  end

  if options[:strict_puppet_version] && requirement.puppet_eol?
    warn(:requirement, "#{requirement.min} is no longer supported. Minimum supported version is #{MIN_PUPPET_VER}")
  end

  return unless requirement.mixed_syntax?

  warn(:requirement, 'Mixing "x" or "*" version syntax with operators is not recommended in ' \
                     "metadata.json, use one style in the puppet version: #{requirement.instance_variable_get(:@requirement)}")
end

.validate_requirements!(requirements) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/metadata_json_lint.rb', line 180

def validate_requirements!(requirements)
  return unless requirements.is_a?(Array)

  requirements.each do |requirement|
    warn :requirements, "The 'pe' requirement is no longer supported by the Forge." if requirement['name'] == 'pe'

    begin
      puppet_req = VersionRequirement.new(requirement.fetch('version_requirement', ''))
    rescue ArgumentError => e
      # Raised when the version_requirement provided could not be parsed
      error :requirements, "Invalid 'version_requirement' field in metadata.json: #{e}"
    end

    validate_puppet_ver!(puppet_req) unless puppet_req.instance_variable_get(:@requirement).nil?
  end

  validate_requirements_unique(requirements)
end

.validate_requirements_unique(requirements) ⇒ Object



168
169
170
171
172
173
174
175
176
177
# File 'lib/metadata_json_lint.rb', line 168

def validate_requirements_unique(requirements)
  names = requirements.map { |x| x['name'] }
  counts = Hash.new(0)

  names.each { |name| counts[name.downcase] += 1 }

  counts.each do |k, v|
    error :requirements, "Duplicate entries in the 'requirements' list with the name '#{k}'" if v > 1
  end
end

.validate_version_requirement!(dep, requirement) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/metadata_json_lint.rb', line 242

def validate_version_requirement!(dep, requirement)
  # Open ended dependency
  # From: https://docs.puppet.com/puppet/latest/reference/modules_metadata.html#best-practice-set-an-upper-bound-for-dependencies
  if options[:strict_dependencies] && requirement.open_ended?
    msg = "Dependency #{dep['name']} has an open " \
          "ended dependency version requirement #{dep['version_requirement']}"
    warn(:dependencies, msg)
  end

  # Mixing operator and wildcard version syntax
  # From: https://docs.puppet.com/puppet/latest/modules_metadata.html#version-specifiers
  # Supported in Puppet 5 and higher, but the syntax is unclear and incompatible with older versions
  return unless requirement.mixed_syntax?

  warn(:dependencies, 'Mixing "x" or "*" version syntax with operators is not recommended in ' \
                      "metadata.json, use one style in the #{dep['name']} dependency: #{dep['version_requirement']}")
end

.warn(check, msg) ⇒ Object



271
272
273
274
275
# File 'lib/metadata_json_lint.rb', line 271

def warn(check, msg)
  @warnings ||= []

  @warnings << format_error(check, msg)
end