<commit_before>#
# Copyright:: Copyright (c) 2015
# License:: Apache License, Version 2.0
#
# 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.
#

# The unifiedpush module in this file is used to parse /etc/unifiedpush/unifiedpush.rb.
#
# Warning to the reader:
# Because the Ruby DSL in /etc/unifiedpush/unifiedpush.rb does not accept hyphens (_) in
# section names, this module translates names like 'unifiedpush_server' to the
# correct 'unifiedpush-server' in the `generate_hash` method. This module is the only
# place in the cookbook where we write 'unifiedpush_server'.

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

module Unifiedpush
  extend(Mixlib::Config)

  bootstrap Mash.new
  global Mash.new
  user Mash.new
  java Mash.new
  cassandra Mash.new
  cassandra_config Mash.new
  postgresql Mash.new
  unifiedpush_server Mash.new
  keycloak_server Mash.new
  nginx Mash.new
  logging Mash.new
  logrotate Mash.new
  node nil
  external_url nil

  class << self

    # guards against creating secrets on non-bootstrap node
    def generate_hex(chars)
      SecureRandom.hex(chars)
    end

    def generate_secrets(node_name)
      SecretsHelper.read_unifiedpush_secrets

      # Note: If you add another secret to generate here make sure it gets written to disk in SecretsHelper.write_to_unifiedpush_secrets
      Unifiedpush['unifiedpush_server']['secret_token'] ||= generate_hex(64)

      # Note: Besides the section below, unifiedpush-secrets.json will also change
      # in CiHelper in libraries/helper.rb
      SecretsHelper.write_to_unifiedpush_secrets
    end

    def parse_external_url
      return unless external_url

      uri = URI(external_url.to_s)
      portal_uri = URI(external_portal_url.to_s)

      info("Installing according to external_url -> " + uri.host + " and " portal_uri.host)

      unless uri.host
        raise "Unifiedpush external URL must include a schema and FQDN, e.g. http://aerobase.example.com/"
      end

      Unifiedpush['unifiedpush_server']['server_host'] = uri.host

      case uri.scheme
      when "http"
        Unifiedpush['unifiedpush_server']['server_https'] = false
      when "https"
        Unifiedpush['unifiedpush_server']['server_https'] = true
        Unifiedpush['nginx']['ssl_certificate'] ||= "/etc/unifiedpush/ssl/#{uri.host}.crt"
        Unifiedpush['nginx']['ssl_certificate_key'] ||= "/etc/unifiedpush/ssl/#{uri.host}.key"
      else
        raise "Unsupported external URL scheme: #{uri.scheme}"
      end

      unless ["", "/"].include?(uri.path)
        raise "Unsupported external URL path: #{uri.path}"
      end

      Unifiedpush['global']['portal_fqdn'] = external_portal_url.to_s
      Unifiedpush['unifiedpush_server']['server_port'] = uri.port
    end

    def parse_postgresql_settings
      # If the user wants to run the internal Postgres service using an alternative
      # DB username, host or port, then those settings should also be applied to
      # unifiedpush.
      [<commit_msg>Support EXTERNAL_PORTAL_URL during installation<commit_after>#
# Copyright:: Copyright (c) 2015
# License:: Apache License, Version 2.0
#
# 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.
#

# The unifiedpush module in this file is used to parse /etc/unifiedpush/unifiedpush.rb.
#
# Warning to the reader:
# Because the Ruby DSL in /etc/unifiedpush/unifiedpush.rb does not accept hyphens (_) in
# section names, this module translates names like 'unifiedpush_server' to the
# correct 'unifiedpush-server' in the `generate_hash` method. This module is the only
# place in the cookbook where we write 'unifiedpush_server'.

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

module Unifiedpush
  extend(Mixlib::Config)

  bootstrap Mash.new
  global Mash.new
  user Mash.new
  java Mash.new
  cassandra Mash.new
  cassandra_config Mash.new
  postgresql Mash.new
  unifiedpush_server Mash.new
  keycloak_server Mash.new
  nginx Mash.new
  logging Mash.new
  logrotate Mash.new
  node nil
  external_url nil

  class << self

    # guards against creating secrets on non-bootstrap node
    def generate_hex(chars)
      SecureRandom.hex(chars)
    end

    def generate_secrets(node_name)
      SecretsHelper.read_unifiedpush_secrets

      # Note: If you add another secret to generate here make sure it gets written to disk in SecretsHelper.write_to_unifiedpush_secrets
      Unifiedpush['unifiedpush_server']['secret_token'] ||= generate_hex(64)

      # Note: Besides the section below, unifiedpush-secrets.json will also change
      # in CiHelper in libraries/helper.rb
      SecretsHelper.write_to_unifiedpush_secrets
    end

    def parse_external_url
      return unless external_url

      uri = URI(external_url.to_s)
      portal_uri = URI(external_portal_url.to_s)

      info("Installing according to external_url -> " + uri.host + " and " + portal_uri.host)

      unless uri.host
        raise "Unifiedpush external URL must include a schema and FQDN, e.g. http://aerobase.example.com/"
      end

      Unifiedpush['unifiedpush_server']['server_host'] = uri.host

      case uri.scheme
      when "http"
        Unifiedpush['unifiedpush_server']['server_https'] = false
      when "https"
        Unifiedpush['unifiedpush_server']['server_https'] = true
        Unifiedpush['nginx']['ssl_certificate'] ||= "/etc/unifiedpush/ssl/#{uri.host}.crt"
        Unifiedpush['nginx']['ssl_certificate_key'] ||= "/etc/unifiedpush/ssl/#{uri.host}.key"
      else
        raise "Unsupported external URL scheme: #{uri.scheme}"
      end

      unless ["", "/"].include?(uri.path)
        raise "Unsupported external URL path: #{uri.path}"
      end

      Unifiedpush['global']['portal_fqdn'] = external_portal_url.to_s
      Unifiedpush['unifiedpush_server']['server_port'] = uri.port
    end

    def parse_postgresql_settings
      # If the user wants to run the internal Postgres service using an alternative
      # DB username, host or port, then those settings should also be applied to
      # unifiedpush.
      [
{'fraction_non_alphanumeric': 0.08790749512398997, 'fraction_numerical': 0.0033435497353023124, 'mean_word_length': 3.62862669245648, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '2275808', 'n_tokens_mistral': 2186, 'n_tokens_neox': 2125, 'n_words': 673}