<commit_before>#
# Cookbook:: sudo
# Resource:: default
#
# Author:: <NAME> (<<EMAIL>>)
# Author:: <NAME> (<<EMAIL>>)
#
# Copyright:: 2011-2018, <NAME>
# Copyright:: 2012-2018, <NAME>
# Copyright:: 2015-2018, Chef Software, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# acording to the sudo man pages sudo will ignore files in an include dir that have a `.` or `~`
# We convert either to `__`
property :filename, String, name_property: true, coerce: proc { |x| x.gsub(/[\.~]/, '__') }
property :user, String
property :group, String
property :commands, Array, default: ['ALL']
property :host, String, default: 'ALL'
property :runas, String, default: 'ALL'
property :nopasswd, [true, false], default: false
property :noexec, [true, false], default: false
property :template, String
property :variables, [Hash, nil], default: nil
property :defaults, Array, default: []
property :command_aliases, Array, default: []
property :setenv, [true, false], default: false
property :env_keep_add, Array, default: []
property :env_keep_subtract, Array, default: []
property :visudo_path, String
property :config_prefix, String, default: lazy { config_prefix }

# default config prefix paths based on platform
def config_prefix
  case node['platform_family']
  when 'smartos', 'freebsd'
    '/usr/local/etc'
  else
    '/etc'
  end
end

action :install do
  Chef::Log.warn('The sudo :install action has been renamed :create. Please update your cookbook code for the new action')
  action_create
end

# Default action - install a single sudoer
action :create do
  validate_properties

  if docker? # don't even put this into resource collection unless we're in docker
    declare_resource(:package, "foo") do
      action :nothing
      not_if 'which sudo'
    end.run_action(:install)
  end

  target = "#{new_resource.config_prefix}/sudoers.d/"
  declare_resource(:directory, target) unless ::File.exist?(target)

  Chef::Log.warn("#{new_resource.filename} will be rendered, but will not take effect because node['authorization']['sudo']['include_sudoers_d'] is set to false!") unless node['authorization']['sudo']['include_sudoers_d']
  render_sudoer
end

# Removes a user from the sudoers group
action :delete do
  file "#{new_resource.config_prefix}/sudoers.d/#{new_resource.filename}" do
    action :delete
  end
end

action_class do
  # Ensure that the inputs are valid (we cannot just use the resource for this)
  def validate_properties
    # if group, user, and template are nil, throw an exception
    raise 'You must provide a user, group, or template properties!' if new_resource.user.nil? && new_resource.group.nil? && new_resource.foreign_template.nil?

    # if specifying user group and template at the same time fail
    raise 'You cannot specify user, group, and template properties at the same time!' if !new_resource.user.nil? && !new_resource.group.nil? && !new_resource.template.nil?
  end

  # Validate the given resource (template) by writing it out to a file and then
  # ensuring that file's contents pass `visudo -c`
  def validate_fragment!(resource)
    file = Tempfile.new('sudoer')

    begin
      file.write(capture(resource))
      file.rewind<commit_msg>Make :create the default action<commit_after>#
# Cookbook:: sudo
# Resource:: default
#
# Author:: <NAME> (<<EMAIL>>)
# Author:: <NAME> (<<EMAIL>>)
#
# Copyright:: 2011-2018, <NAME>
# Copyright:: 2012-2018, <NAME>
# Copyright:: 2015-2018, Chef Software, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# acording to the sudo man pages sudo will ignore files in an include dir that have a `.` or `~`
# We convert either to `__`
property :filename, String, name_property: true, coerce: proc { |x| x.gsub(/[\.~]/, '__') }
property :user, String
property :group, String
property :commands, Array, default: ['ALL']
property :host, String, default: 'ALL'
property :runas, String, default: 'ALL'
property :nopasswd, [true, false], default: false
property :noexec, [true, false], default: false
property :template, String
property :variables, [Hash, nil], default: nil
property :defaults, Array, default: []
property :command_aliases, Array, default: []
property :setenv, [true, false], default: false
property :env_keep_add, Array, default: []
property :env_keep_subtract, Array, default: []
property :visudo_path, String
property :config_prefix, String, default: lazy { config_prefix }

# default config prefix paths based on platform
def config_prefix
  case node['platform_family']
  when 'smartos', 'freebsd'
    '/usr/local/etc'
  else
    '/etc'
  end
end

# Default action - install a single sudoer
action :create do
  validate_properties

  if docker? # don't even put this into resource collection unless we're in docker
    declare_resource(:package, "foo") do
      action :nothing
      not_if 'which sudo'
    end.run_action(:install)
  end

  target = "#{new_resource.config_prefix}/sudoers.d/"
  declare_resource(:directory, target) unless ::File.exist?(target)

  Chef::Log.warn("#{new_resource.filename} will be rendered, but will not take effect because node['authorization']['sudo']['include_sudoers_d'] is set to false!") unless node['authorization']['sudo']['include_sudoers_d']
  render_sudoer
end

action :install do
  Chef::Log.warn('The sudo :install action has been renamed :create. Please update your cookbook code for the new action')
  action_create
end

# Removes a user from the sudoers group
action :delete do
  file "#{new_resource.config_prefix}/sudoers.d/#{new_resource.filename}" do
    action :delete
  end
end

action_class do
  # Ensure that the inputs are valid (we cannot just use the resource for this)
  def validate_properties
    # if group, user, and template are nil, throw an exception
    raise 'You must provide a user, group, or template properties!' if new_resource.user.nil? && new_resource.group.nil? && new_resource.foreign_template.nil?

    # if specifying user group and template at the same time fail
    raise 'You cannot specify user, group, and template properties at the same time!' if !new_resource.user.nil? && !new_resource.group.nil? && !new_resource.template.nil?
  end

  # Validate the given resource (template) by writing it out to a file and then
  # ensuring that file's contents pass `visudo -c`
  def validate_fragment!(resource)
    file = Tempfile.new('sudoer')

    begin
      file.write(capture(resource))
      file.rewind
{'fraction_non_alphanumeric': 0.10904903820102953, 'fraction_numerical': 0.007586020048767272, 'mean_word_length': 4.631578947368421, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4585326', 'n_tokens_mistral': 2210, 'n_tokens_neox': 2107, 'n_words': 845}