Class: HttpdConfigmapGenerator::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/httpd_configmap_generator/base.rb,
lib/httpd_configmap_generator/base/pam.rb,
lib/httpd_configmap_generator/base/file.rb,
lib/httpd_configmap_generator/base/config.rb,
lib/httpd_configmap_generator/base/command.rb,
lib/httpd_configmap_generator/base/network.rb,
lib/httpd_configmap_generator/base/kerberos.rb

Direct Known Subclasses

ActiveDirectory, ConfigMap, Export, Ipa, Principal, Saml, Sssd, Update

Constant Summary collapse

APACHE_USER =
"apache".freeze
HTTP_KEYTAB =
"/etc/http.keytab".freeze
IPA_COMMAND =
"/usr/bin/ipa".freeze
KERBEROS_CONFIG_FILE =
"/etc/krb5.conf".freeze
LDAP_ATTRS =
{
  "mail"        => "REMOTE_USER_EMAIL",
  "givenname"   => "REMOTE_USER_FIRSTNAME",
  "sn"          => "REMOTE_USER_LASTNAME",
  "displayname" => "REMOTE_USER_FULLNAME",
  "domainname"  => "REMOTE_USER_DOMAIN"
}.freeze
PAM_CONFIG =
"/etc/pam.d/httpd-auth".freeze
SSSD_CONFIG =
"/etc/sssd/sssd.conf".freeze
TIMESTAMP_FORMAT =
"%Y%m%d_%H%M%S".freeze
HOSTNAME_COMMAND =
"/usr/bin/hostname".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Returns a new instance of Base.



31
32
33
34
# File 'lib/httpd_configmap_generator/base.rb', line 31

def initialize(opts = {})
  @opts = opts
  @realm = @domain = nil
end

Instance Attribute Details

#optsObject

Returns the value of attribute opts.



29
30
31
# File 'lib/httpd_configmap_generator/base.rb', line 29

def opts
  @opts
end

Instance Method Details

#command_run(executable, options = {}) ⇒ Object



5
6
7
8
9
10
# File 'lib/httpd_configmap_generator/base/command.rb', line 5

def command_run(executable, options = {})
  if opts && opts[:debug]
    debug_msg("Running Command: #{AwesomeSpawn.build_command_line(executable, options)}")
  end
  AwesomeSpawn.run(executable, options)
end

#command_run!(executable, options = {}) ⇒ Object



12
13
14
15
16
17
# File 'lib/httpd_configmap_generator/base/command.rb', line 12

def command_run!(executable, options = {})
  if opts && opts[:debug]
    debug_msg("Running Command: #{AwesomeSpawn.build_command_line(executable, options)}")
  end
  AwesomeSpawn.run!(executable, options)
end

#config_file_backup(path) ⇒ Object



6
7
8
9
10
11
# File 'lib/httpd_configmap_generator/base/config.rb', line 6

def config_file_backup(path)
  if File.exist?(path)
    timestamp = Time.current.strftime(TIMESTAMP_FORMAT)
    FileUtils.copy(path, "#{path}.#{timestamp}")
  end
end

#configure_pamObject



3
4
5
6
7
# File 'lib/httpd_configmap_generator/base/pam.rb', line 3

def configure_pam
  info_msg("Configuring PAM")
  debug_msg("- Creating #{PAM_CONFIG}")
  cp_template(PAM_CONFIG, template_directory)
end

#cp_template(file, src_dir, dest_dir = "/") ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/httpd_configmap_generator/base/file.rb', line 10

def cp_template(file, src_dir, dest_dir = "/")
  src_path  = path_join(src_dir, file)
  dest_path = path_join(dest_dir, file.gsub(".erb", ""))
  if src_path.to_s.include?(".erb")
    File.write(dest_path, ERB.new(File.read(src_path), nil, '-').result(binding))
  else
    FileUtils.cp(src_path, dest_path)
  end
end

#create_target_directory(file_path) ⇒ Object



31
32
33
34
35
36
# File 'lib/httpd_configmap_generator/base/file.rb', line 31

def create_target_directory(file_path)
  dirname = File.dirname(file_path)
  return if File.exist?(dirname)
  debug_msg("Creating directory #{dirname} ...")
  FileUtils.mkdir_p(dirname)
end

#debug_msg(msg) ⇒ Object



44
45
46
# File 'lib/httpd_configmap_generator/base.rb', line 44

def debug_msg(msg)
  STDOUT.puts(msg) if opts[:debug]
end

#delete_target_file(file_path) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/httpd_configmap_generator/base/file.rb', line 20

def delete_target_file(file_path)
  if File.exist?(file_path)
    if opts[:force]
      info_msg("File #{file_path} exists, forcing a delete")
      File.delete(file_path)
    else
      raise "File #{file_path} already exist"
    end
  end
end

#domainObject



9
10
11
# File 'lib/httpd_configmap_generator/base/network.rb', line 9

def domain
  domain_from_host(opts[:host])
end

#domain_from_host(host) ⇒ Object



13
14
15
# File 'lib/httpd_configmap_generator/base/network.rb', line 13

def domain_from_host(host)
  host.gsub(/^([^.]+\.)/, '') if host.present? && host.include?('.')
end

#enable_kerberos_dns_lookupsObject



3
4
5
6
7
8
9
10
11
# File 'lib/httpd_configmap_generator/base/kerberos.rb', line 3

def enable_kerberos_dns_lookups
  info_msg("Configuring Kerberos DNS Lookups")
  config_file_backup(KERBEROS_CONFIG_FILE)
  krb5config = File.read(KERBEROS_CONFIG_FILE)
  krb5config[/(\s*)dns_lookup_kdc(\s*)=(\s*)(.*)/, 4] = 'true'   if krb5config[/(\s*)dns_lookup_kdc(\s*)=/]
  krb5config[/(\s*)dns_lookup_realm(\s*)=(\s*)(.*)/, 4] = 'true' if krb5config[/(\s*)dns_lookup_realm(\s*)=/]
  debug_msg("- Updating #{KERBEROS_CONFIG_FILE}")
  File.write(KERBEROS_CONFIG_FILE, krb5config)
end

#err_msg(msg) ⇒ Object



36
37
38
# File 'lib/httpd_configmap_generator/base.rb', line 36

def err_msg(msg)
  STDERR.puts(msg)
end

#fetch_network_file(source_file, target_file) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/httpd_configmap_generator/base/network.rb', line 26

def fetch_network_file(source_file, target_file)
  require "net/http"

  delete_target_file(target_file)
  create_target_directory(target_file)
  info_msg("Downloading #{source_file} ...")
  result = Net::HTTP.get_response(URI(source_file))
  raise "Failed to fetch URL file source #{source_file}" unless result.kind_of?(Net::HTTPSuccess)
  File.write(target_file, result.body)
end

#file_binary?(file) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/httpd_configmap_generator/base/file.rb', line 49

def file_binary?(file)
  data = File.read(file)
  ascii = control = binary = total = 0
  data[0..512].each_byte do |c|
    total += 1
    if c < 32
      control += 1
    elsif c >= 32 && c <= 128
      ascii += 1
    else
      binary += 1
    end
  end
  control.to_f / ascii > 0.1 || binary.to_f / ascii > 0.05
end

#host_reachable?(host) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
# File 'lib/httpd_configmap_generator/base/network.rb', line 17

def host_reachable?(host)
  require "net/ping"
  Net::Ping::External.new(host).ping
end

#info_msg(msg) ⇒ Object



40
41
42
# File 'lib/httpd_configmap_generator/base.rb', line 40

def info_msg(msg)
  STDOUT.puts(msg)
end

#log_command_error(err) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/httpd_configmap_generator/base/command.rb', line 19

def log_command_error(err)
  err_msg("Command Error: #{err}")
  if err.kind_of?(AwesomeSpawn::CommandResultError)
    err_msg("stdout: #{err.result.output}")
    err_msg("stderr: #{err.result.error}")
  else
    err_msg(err.backtrace)
  end
end

#optional_optionsObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/httpd_configmap_generator/base.rb', line 57

def optional_options
  {
    :force => { :description => "Force configuration if configured already",
                :short       => "-f",
                :default     => false },
    :debug => { :description => "Enable debugging",
                :short       => "-d",
                :default     => false }
  }
end

#path_join(*args) ⇒ Object



43
44
45
46
47
# File 'lib/httpd_configmap_generator/base/file.rb', line 43

def path_join(*args)
  path = Pathname.new(args.shift)
  args.each { |path_seg| path = path.join("./#{path_seg}") }
  path
end

#realmObject



5
6
7
# File 'lib/httpd_configmap_generator/base/network.rb', line 5

def realm
  domain.upcase
end

#required_optionsObject



48
49
50
51
52
53
54
55
# File 'lib/httpd_configmap_generator/base.rb', line 48

def required_options
  {
    :host   => { :description => "Application Domain",
                 :short       => "-h" },
    :output => { :description => "Configuration map file to create",
                 :short       => "-o" }
  }
end

#rm_file(file, dir = "/") ⇒ Object



38
39
40
41
# File 'lib/httpd_configmap_generator/base/file.rb', line 38

def rm_file(file, dir = "/")
  path = path_join(dir, file)
  File.delete(path) if File.exist?(path)
end

#run_configure(opts) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/httpd_configmap_generator/base.rb', line 68

def run_configure(opts)
  validate_options(opts)
  @opts = opts
  unconfigure if configured? && opts[:force]
  raise "#{self.class.name} Already Configured" if configured?
  unless ENV["HTTPD_AUTH_TYPE"]
    raise "Not running in httpd_configmap_generator container - Skipping #{self.class.name} configuration"
  end
  configure(opts)
end

#template_directoryObject



5
6
7
8
# File 'lib/httpd_configmap_generator/base/file.rb', line 5

def template_directory
  @template_directory ||=
    Pathname.new(Gem::Specification.find_by_name("httpd_configmap_generator").full_gem_path).join("templates")
end

#update_hostname(host) ⇒ Object



22
23
24
# File 'lib/httpd_configmap_generator/base/network.rb', line 22

def update_hostname(host)
  command_run!(HOSTNAME_COMMAND, :params => [host]) if command_run(HOSTNAME_COMMAND).output.strip != host
end

#validate_options(_options) ⇒ Object



79
80
81
# File 'lib/httpd_configmap_generator/base.rb', line 79

def validate_options(_options)
  nil
end