<commit_before>#
# Cookbook Name:: rabbitmq
# Provider:: user
#
# Copyright 2011-2013, Opscode, 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.
#

def user_exists?(name)
  cmdStr = "rabbitmqctl -q list_users |grep '^#{name}\\b'"
  cmd = Mixlib::ShellOut.new(cmdStr)
  cmd.environment['HOME'] = ENV.fetch('HOME', '/root')
  cmd.run_command
  Chef::Log.debug "rabbitmq_user_exists?: #{cmdStr}"
  Chef::Log.debug "rabbitmq_user_exists?: #{cmd.stdout}"
  begin
    cmd.error!
    true
  rescue
    false
  end
end

def user_has_tag?(name, tag)
  tag = '"\[\]"' if tag.nil?
  cmdStr = "rabbitmqctl -q list_users | grep \"^#{name}\\b\" | grep #{tag}"
  cmd = Mixlib::ShellOut.new(cmdStr)
  cmd.environment['HOME'] = ENV.fetch('HOME', '/root')
  cmd.run_command
  Chef::Log.debug "rabbitmq_user_has_tag?: #{cmdStr}"
  Chef::Log.debug "rabbitmq_user_has_tag?: #{cmd.stdout}"
  begin
    cmd.error!
    true
  rescue Exception => e
    false
  end
end

# does the user have the rights listed on the vhost?
# empty perm_list means we're checking for any permissions
def user_has_permissions?(name, vhost, perm_list = nil)
  vhost = '/' if vhost.nil?
  cmdStr = "rabbitmqctl -q list_user_permissions #{name} | grep \"^#{vhost}\\s\""
  cmd = Mixlib::ShellOut.new(cmdStr)
  cmd.environment['HOME'] = ENV.fetch('HOME', '/root')
  cmd.run_command
  Chef::Log.debug "rabbitmq_user_has_permissions?: #{cmdStr}"
  Chef::Log.debug "rabbitmq_user_has_permissions?: #{cmd.stdout}"
  Chef::Log.debug "rabbitmq_user_has_permissions?: #{cmd.exitstatus}"
  if perm_list.nil? && cmd.stdout.empty? #looking for empty and found nothing
    Chef::Log.debug "rabbitmq_user_has_permissions?: no permissions found"
    return false
  end
  if perm_list == cmd.stdout.split.drop(1) #existing match search
    Chef::Log.debug "rabbitmq_user_has_permissions?: matching permissions already found"
    return true
  end
  Chef::Log.debug "rabbitmq_user_has_permissions?: permissions found but do not match"
  return false
end

action :add do
  unless user_exists?(new_resource.user)
    if new_resource.password.nil? || new_resource.password.empty?
      Chef::Application.fatal!("rabbitmq_user with action :add requires a non-nil/empty password.")
    end
    new_password = <PASSWORD>.gsub("'", "'\\\\''")
    cmdStr = "rabbitmqctl add_user #{new_resource.user} '#{new_password}'"
    execute cmdStr do
      Chef::Log.debug "rabbitmq_user_add: #{cmdStr}"
      Chef::Log.info "Adding RabbitMQ user '#{new_resource.user}'."
      new_resource.updated_by_last_action(true)<commit_msg>Explain why so much escaping is needed to quote the password<commit_after>#
# Cookbook Name:: rabbitmq
# Provider:: user
#
# Copyright 2011-2013, Opscode, 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.
#

def user_exists?(name)
  cmdStr = "rabbitmqctl -q list_users |grep '^#{name}\\b'"
  cmd = Mixlib::ShellOut.new(cmdStr)
  cmd.environment['HOME'] = ENV.fetch('HOME', '/root')
  cmd.run_command
  Chef::Log.debug "rabbitmq_user_exists?: #{cmdStr}"
  Chef::Log.debug "rabbitmq_user_exists?: #{cmd.stdout}"
  begin
    cmd.error!
    true
  rescue
    false
  end
end

def user_has_tag?(name, tag)
  tag = '"\[\]"' if tag.nil?
  cmdStr = "rabbitmqctl -q list_users | grep \"^#{name}\\b\" | grep #{tag}"
  cmd = Mixlib::ShellOut.new(cmdStr)
  cmd.environment['HOME'] = ENV.fetch('HOME', '/root')
  cmd.run_command
  Chef::Log.debug "rabbitmq_user_has_tag?: #{cmdStr}"
  Chef::Log.debug "rabbitmq_user_has_tag?: #{cmd.stdout}"
  begin
    cmd.error!
    true
  rescue Exception => e
    false
  end
end

# does the user have the rights listed on the vhost?
# empty perm_list means we're checking for any permissions
def user_has_permissions?(name, vhost, perm_list = nil)
  vhost = '/' if vhost.nil?
  cmdStr = "rabbitmqctl -q list_user_permissions #{name} | grep \"^#{vhost}\\s\""
  cmd = Mixlib::ShellOut.new(cmdStr)
  cmd.environment['HOME'] = ENV.fetch('HOME', '/root')
  cmd.run_command
  Chef::Log.debug "rabbitmq_user_has_permissions?: #{cmdStr}"
  Chef::Log.debug "rabbitmq_user_has_permissions?: #{cmd.stdout}"
  Chef::Log.debug "rabbitmq_user_has_permissions?: #{cmd.exitstatus}"
  if perm_list.nil? && cmd.stdout.empty? #looking for empty and found nothing
    Chef::Log.debug "rabbitmq_user_has_permissions?: no permissions found"
    return false
  end
  if perm_list == cmd.stdout.split.drop(1) #existing match search
    Chef::Log.debug "rabbitmq_user_has_permissions?: matching permissions already found"
    return true
  end
  Chef::Log.debug "rabbitmq_user_has_permissions?: permissions found but do not match"
  return false
end

action :add do
  unless user_exists?(new_resource.user)
    if new_resource.password.nil? || new_resource.password.empty?
      Chef::Application.fatal!("rabbitmq_user with action :add requires a non-nil/empty password.")
    end
    # To escape single quotes in a shell, you have to close the surrounding single quotes, add
    # in an escaped single quote, and then re-open the original single quotes.
    # Since this string is interpolated once by ruby, and then a second time by the shell, we need
    # to escape the escape character ('\') twice.  This is why the following is such a mess
    # of leaning toothpicks:
    new_password = <PASSWORD>.gsub("'", "'\\\\''")
    cmdStr = "rabbitmqctl add_user #{new_resource.user} '#{new_password}'"
    execute cmdStr do
      Chef::Log.debug "rabbitmq_user_add: #{cmdStr}"
      Chef::Log.info "Adding RabbitMQ user '#{new_resource.user}'."
      new_resource.updated_by_last_action(true)
{'fraction_non_alphanumeric': 0.12517193947730398, 'fraction_numerical': 0.003973712364358857, 'mean_word_length': 4.268921095008052, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '6135436', 'n_tokens_mistral': 2169, 'n_tokens_neox': 2112, 'n_words': 627}