<commit_before>#
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright (c) 2012 Opscode, Inc.
#
# All Rights Reserved
#

require 'mixlib/config'
require 'chef/mash'
require 'chef/json_compat'
require 'chef/mixin/deep_merge'
require 'securerandom'

module PrivateChef
  extend(Mixlib::Config)

  # options are 'standalone', 'manual', 'ha', and 'tier'
  topology "standalone"

  couchdb Mash.new
  rabbitmq Mash.new
  opscode_solr Mash.new
  opscode_expander Mash.new
  opscode_chef Mash.new
  opscode_erchef Mash.new
  opscode_webui Mash.new
  lb Mash.new
  mysql Mash.new
  postgresql Mash.new
  redis Mash.new
  opscode_authz Mash.new
  opscode_certificate Mash.new
  opscode_org_creator Mash.new
  opscode_account Mash.new
  bookshelf Mash.new
  bootstrap Mash.new
  drbd Mash.new
  keepalived Mash.new
  nagios Mash.new
  nrpe Mash.new
  nginx Mash.new
  log_retention Mash.new
  log_rotation Mash.new

  servers Mash.new
  backend_vips Mash.new
  api_fqdn nil
  node nil

  notification_email nil
  from_email nil
  database_type nil
  role nil
  user Mash.new

  ldap Mash.new

  allowed_webui_subnets []

  class << self

    def server(name=nil, opts={})
      if name
        PrivateChef["servers"] ||= Mash.new
        PrivateChef["servers"][name] = Mash.new(opts)
      end
      PrivateChef["servers"]
    end

    def servers
      PrivateChef["servers"]
    end

    def backend_vip(name=nil, opts={})
      if name
        PrivateChef['backend_vips'] ||= Mash.new
        PrivateChef['backend_vips']["fqdn"] = name
        opts.each do |k,v|
          PrivateChef['backend_vips'][k] = v
        end
      end
      PrivateChef['backend_vips']
    end

    # guards against creating secrets on non-bootstrap node
    def generate_hex_if_bootstrap(chars, ha_guard)
      if ha_guard
        Chef::Log.fatal("Attempt to create secrets on non-bootstrap node in an H/A topology, please copy /etc/opscode/* around instead.")
        exit 44
      end
      SecureRandom.hex(chars)
    end

    def generate_secrets(node_name)
      existing_secrets ||= Hash.new
      if File.exists?("/etc/opscode/private-chef-secrets.json")
        existing_secrets = Chef::JSONCompat.from_json(File.read("/etc/opscode/private-chef-secrets.json"))
      end
      existing_secrets.each do |k, v|
        v.each do |pk, p|
          PrivateChef[k][pk] = p
        end
      end

      me = PrivateChef["servers"][node_name]
      ha_guard = PrivateChef['topology'] == "ha" && !me['bootstrap']

      PrivateChef['rabbitmq']['password'] ||= generate_hex_if_bootstrap(50, ha_guard)
      PrivateChef['rabbitmq']['jobs_password'] ||= generate_hex_if_bootstrap(50, ha_guard)
      PrivateChef['opscode_webui']['cookie_secret'] ||= generate_hex_if_bootstrap(50, ha_guard)
      PrivateChef['mysql']['sql_password'] ||= generate_hex_if_bootstrap(50, ha_guard)
      PrivateChef['postgresql']['sql_password'] ||= generate_hex_if_bootstrap(50, ha_guard)
      PrivateChef['postgresql']['sql_ro_password'] ||= generate_hex_if_bootstrap(50, ha_guard)
      PrivateChef['opscode_account']['session_secret_key'] ||= generate_hex_if_bootstrap(50, ha_guard)
      PrivateChef['nagios']['admin_password'] ||= generate_hex_if_bootstrap(50, ha_guard)
      PrivateChef['drbd']['shared_secret'] ||= generate_hex_if_bootstrap(30, ha_guard)
      PrivateChef['keepalived']['vrrp_instance_password'] ||= generate_hex_if_bootstrap(50, ha_guard)
      PrivateChef['opscode_authz']['superuser_id'] ||= generate_hex_if_bootstrap(16, ha_guard)
      PrivateChef['bookshelf']['access_key_id'] ||= generate_hex_if_bootstrap(20, ha_guard)
      PrivateChef['bookshelf']['secret_access_key'] ||= generate_hex_if_bootstrap(40, ha_guard)

      if File.directory?("/etc/opscode")
        File.open("/etc/opscode/private-chef-secrets.json", "w") do |f|
          f.puts(
            Chef::JSONCompat.to_json_pretty({
              'rabbitmq' => {
                'password' => PrivateChef['rabbitmq']['password'],
                'jobs_password' => PrivateChef['rabbitmq']['jobs_password'],
              },
              'opscode_webui' => {
                'cookie_secret' => PrivateChef['opscode_webui']['cookie_secret'],
              },
              'mysql' => {
                'sql_password' => PrivateChef['mysql']['sql_password'],
              },
              'postgresql' => {
                'sql_password' => PrivateChef['postgresql']['sql_password'],
                'sql_ro_password' => PrivateChef['postgresql']['sql_ro_password']
              },
              'opscode_account' => {
                'session_secret_key' => PrivateChef['opscode_account']['session_secret_key']
              },
              'nagios' => {
                'admin_password' => PrivateChef['nagios']['admin_password']
              },
              'drbd' => {
                'shared_secret' => PrivateChef['drbd']['shared_secret']
              },
              'keepalived' => {
                'vrrp_instance_password' => PrivateChef['keepalived']['vrrp_instance_password']
              },
              'opscode_authz' => {
                'superuser_id' => PrivateChef['opscode_authz']['superuser_id']
              },
              'bookshelf' => {
                'access_key_id' => PrivateChef['bookshelf']['access_key_id'],
                'secret_access_key' => PrivateChef['bookshelf']['secret_access_key']
              }
            })
          )
          system("chmod 0600 /etc/opscode/private-chef-secrets.json")
        end
      end
    end

    def generate_hash
      results = { "private_chef" => {} }
      [
        "couchdb",
        "rabbitmq",
        "opscode_solr",
        "opscode_expander",
        "opscode_chef",
        "opscode_erchef",
        "opscode_webui",
        "lb",
        "mysql",
        "postgresql",
        "redis",
        "opscode_authz",
        "opscode_certificate",
        "opscode_org_creator",
        "opscode_account",
        "bookshelf",
        "bootstrap",
        "drbd",
        "keepalived",
        "nagios",
        "nrpe",
        "nginx",
        "ldap",
        "user"
      ].each do |key|
        rkey = key.gsub('_', '-')
        results['private_chef'][rkey] = PrivateChef[key]
      end
      results['private_chef']['database_type'] = PrivateChef['database_type']
      results['private_chef']['notification_email'] = PrivateChef['notification_email']
      results['private_chef']['from_email'] = PrivateChef['from_email']
      results['private_chef']['role'] = PrivateChef['role']
      results['private_chef']['logs'] = {}
      results['private_chef']['logs']['log_retention'] = PrivateChef['log_retention']
      results['private_chef']['logs']['log_rotation'] = PrivateChef['log_rotation']
      results
    end

    def gen_api_fqdn
      PrivateChef["lb"]["api_fqdn"] ||= PrivateChef['api_fqdn']
      PrivateChef["lb"]["web_ui_fqdn"] ||= PrivateChef['api_fqdn']
      PrivateChef["nginx"]["server_name"] ||= PrivateChef['api_fqdn']
      PrivateChef["nginx"]["url"] ||= "https://#{PrivateChef['api_fqdn']}"
    end

    def gen_nrpe_allowed_hosts
      nrpe_allowed_hosts = [ "127.0.0.1" ]
      if PrivateChef['backend_vips']['ipaddress']<commit_msg>ensure `estatsd` attributes are configurable in private-chef config<commit_after>#
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright (c) 2012 Opscode, Inc.
#
# All Rights Reserved
#

require 'mixlib/config'
require 'chef/mash'
require 'chef/json_compat'
require 'chef/mixin/deep_merge'
require 'securerandom'

module PrivateChef
  extend(Mixlib::Config)

  # options are 'standalone', 'manual', 'ha', and 'tier'
  topology "standalone"

  couchdb Mash.new
  rabbitmq Mash.new
  opscode_solr Mash.new
  opscode_expander Mash.new
  opscode_chef Mash.new
  opscode_erchef Mash.new
  opscode_webui Mash.new
  lb Mash.new
  mysql Mash.new
  postgresql Mash.new
  redis Mash.new
  opscode_authz Mash.new
  opscode_certificate Mash.new
  opscode_org_creator Mash.new
  opscode_account Mash.new
  bookshelf Mash.new
  bootstrap Mash.new
  drbd Mash.new
  keepalived Mash.new
  nagios Mash.new
  nrpe Mash.new
  estatsd Mash.new
  nginx Mash.new
  log_retention Mash.new
  log_rotation Mash.new

  servers Mash.new
  backend_vips Mash.new
  api_fqdn nil
  node nil

  notification_email nil
  from_email nil
  database_type nil
  role nil
  user Mash.new

  ldap Mash.new

  allowed_webui_subnets []

  class << self

    def server(name=nil, opts={})
      if name
        PrivateChef["servers"] ||= Mash.new
        PrivateChef["servers"][name] = Mash.new(opts)
      end
      PrivateChef["servers"]
    end

    def servers
      PrivateChef["servers"]
    end

    def backend_vip(name=nil, opts={})
      if name
        PrivateChef['backend_vips'] ||= Mash.new
        PrivateChef['backend_vips']["fqdn"] = name
        opts.each do |k,v|
          PrivateChef['backend_vips'][k] = v
        end
      end
      PrivateChef['backend_vips']
    end

    # guards against creating secrets on non-bootstrap node
    def generate_hex_if_bootstrap(chars, ha_guard)
      if ha_guard
        Chef::Log.fatal("Attempt to create secrets on non-bootstrap node in an H/A topology, please copy /etc/opscode/* around instead.")
        exit 44
      end
      SecureRandom.hex(chars)
    end

    def generate_secrets(node_name)
      existing_secrets ||= Hash.new
      if File.exists?("/etc/opscode/private-chef-secrets.json")
        existing_secrets = Chef::JSONCompat.from_json(File.read("/etc/opscode/private-chef-secrets.json"))
      end
      existing_secrets.each do |k, v|
        v.each do |pk, p|
          PrivateChef[k][pk] = p
        end
      end

      me = PrivateChef["servers"][node_name]
      ha_guard = PrivateChef['topology'] == "ha" && !me['bootstrap']

      PrivateChef['rabbitmq']['password'] ||= generate_hex_if_bootstrap(50, ha_guard)
      PrivateChef['rabbitmq']['jobs_password'] ||= generate_hex_if_bootstrap(50, ha_guard)
      PrivateChef['opscode_webui']['cookie_secret'] ||= generate_hex_if_bootstrap(50, ha_guard)
      PrivateChef['mysql']['sql_password'] ||= generate_hex_if_bootstrap(50, ha_guard)
      PrivateChef['postgresql']['sql_password'] ||= generate_hex_if_bootstrap(50, ha_guard)
      PrivateChef['postgresql']['sql_ro_password'] ||= generate_hex_if_bootstrap(50, ha_guard)
      PrivateChef['opscode_account']['session_secret_key'] ||= generate_hex_if_bootstrap(50, ha_guard)
      PrivateChef['nagios']['admin_password'] ||= generate_hex_if_bootstrap(50, ha_guard)
      PrivateChef['drbd']['shared_secret'] ||= generate_hex_if_bootstrap(30, ha_guard)
      PrivateChef['keepalived']['vrrp_instance_password'] ||= generate_hex_if_bootstrap(50, ha_guard)
      PrivateChef['opscode_authz']['superuser_id'] ||= generate_hex_if_bootstrap(16, ha_guard)
      PrivateChef['bookshelf']['access_key_id'] ||= generate_hex_if_bootstrap(20, ha_guard)
      PrivateChef['bookshelf']['secret_access_key'] ||= generate_hex_if_bootstrap(40, ha_guard)

      if File.directory?("/etc/opscode")
        File.open("/etc/opscode/private-chef-secrets.json", "w") do |f|
          f.puts(
            Chef::JSONCompat.to_json_pretty({
              'rabbitmq' => {
                'password' => PrivateChef['rabbitmq']['password'],
                'jobs_password' => PrivateChef['rabbitmq']['jobs_password'],
              },
              'opscode_webui' => {
                'cookie_secret' => PrivateChef['opscode_webui']['cookie_secret'],
              },
              'mysql' => {
                'sql_password' => PrivateChef['mysql']['sql_password'],
              },
              'postgresql' => {
                'sql_password' => PrivateChef['postgresql']['sql_password'],
                'sql_ro_password' => PrivateChef['postgresql']['sql_ro_password']
              },
              'opscode_account' => {
                'session_secret_key' => PrivateChef['opscode_account']['session_secret_key']
              },
              'nagios' => {
                'admin_password' => PrivateChef['nagios']['admin_password']
              },
              'drbd' => {
                'shared_secret' => PrivateChef['drbd']['shared_secret']
              },
              'keepalived' => {
                'vrrp_instance_password' => PrivateChef['keepalived']['vrrp_instance_password']
              },
              'opscode_authz' => {
                'superuser_id' => PrivateChef['opscode_authz']['superuser_id']
              },
              'bookshelf' => {
                'access_key_id' => PrivateChef['bookshelf']['access_key_id'],
                'secret_access_key' => PrivateChef['bookshelf']['secret_access_key']
              }
            })
          )
          system("chmod 0600 /etc/opscode/private-chef-secrets.json")
        end
      end
    end

    def generate_hash
      results = { "private_chef" => {} }
      [
        "couchdb",
        "rabbitmq",
        "opscode_solr",
        "opscode_expander",
        "opscode_chef",
        "opscode_erchef",
        "opscode_webui",
        "lb",
        "mysql",
        "postgresql",
        "redis",
        "opscode_authz",
        "opscode_certificate",
        "opscode_org_creator",
        "opscode_account",
        "bookshelf",
        "bootstrap",
        "drbd",
        "keepalived",
        "nagios",
        "nrpe",
        "estatsd",
        "nginx",
        "ldap",
        "user"
      ].each do |key|
        rkey = key.gsub('_', '-')
        results['private_chef'][rkey] = PrivateChef[key]
      end
      results['private_chef']['database_type'] = PrivateChef['database_type']
      results['private_chef']['notification_email'] = PrivateChef['notification_email']
      results['private_chef']['from_email'] = PrivateChef['from_email']
      results['private_chef']['role'] = PrivateChef['role']
      results['private_chef']['logs'] = {}
      results['private_chef']['logs']['log_retention'] = PrivateChef['log_retention']
      results['private_chef']['logs']['log_rotation'] = PrivateChef['log_rotation']
      results
    end

    def gen_api_fqdn
      PrivateChef["lb"]["api_fqdn"] ||= PrivateChef['api_fqdn']
      PrivateChef["lb"]["web_ui_fqdn"] ||= PrivateChef['api_fqdn']
      PrivateChef["nginx"]["server_name"] ||= PrivateChef['api_fqdn']
      PrivateChef["nginx"]["url"] ||= "https://#{PrivateChef['api_fqdn']}"
    end

    def gen_nrpe_allowed_hosts
      nrpe_allowed_hosts = [ "127.0.0.1" ]
      if PrivateChef['backend_vips']['ipaddress']
{'fraction_non_alphanumeric': 0.13815698717064423, 'fraction_numerical': 0.005793902607256173, 'mean_word_length': 2.8205533596837946, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 57, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '982032', 'n_tokens_mistral': 4932, 'n_tokens_neox': 4807, 'n_words': 723}