class Proxy::AdRealm::Provider

Attributes

computername_hash[R]
computername_prefix[R]
computername_use_fqdn[R]
domain[R]
domain_controller[R]
keytab_path[R]
ou[R]
principal[R]
realm[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/smart_proxy_realm_ad/provider.rb, line 13
def initialize(options = {})
  @realm = options[:realm]
  @keytab_path = options[:keytab_path]
  @principal = options[:principal]
  @domain_controller = options[:domain_controller]
  @domain = options[:realm].downcase
  @ou = options[:ou]
  @computername_prefix = options[:computername_prefix]
  @computername_hash = options.fetch(:computername_hash, false)
  @computername_use_fqdn = options.fetch(:computername_use_fqdn, false)
  logger.info 'Proxy::AdRealm: initialize...'
end

Public Instance Methods

check_realm(realm) click to toggle source
# File lib/smart_proxy_realm_ad/provider.rb, line 26
def check_realm(realm)
  raise Exception, "Unknown realm #{realm}" unless realm.casecmp(@realm).zero?
end
create(realm, hostfqdn, params) click to toggle source
# File lib/smart_proxy_realm_ad/provider.rb, line 34
def create(realm, hostfqdn, params)
  logger.info "Proxy::AdRealm: create... #{realm}, #{hostfqdn}, #{params}"
  check_realm(realm)
  kinit_radcli_connect

  password = generate_password
  result = { randompassword: password }

  computername = hostfqdn_to_computername(hostfqdn)

  if params[:rebuild] == 'true'
    radcli_password(computername, password)
  else
    radcli_join(hostfqdn, computername, password)
  end

  JSON.pretty_generate(result)
end
delete(realm, hostfqdn) click to toggle source
# File lib/smart_proxy_realm_ad/provider.rb, line 53
def delete(realm, hostfqdn)
  logger.info "Proxy::AdRealm: delete... #{realm}, #{hostfqdn}"
  kinit_radcli_connect
  check_realm(realm)
  computername = hostfqdn_to_computername(hostfqdn)
  radcli_delete(computername)
end
find(_hostfqdn) click to toggle source
# File lib/smart_proxy_realm_ad/provider.rb, line 30
def find(_hostfqdn)
  true
end

Private Instance Methods

apply_computername_prefix?(computername) click to toggle source
# File lib/smart_proxy_realm_ad/provider.rb, line 80
def apply_computername_prefix?(computername)
  !computername_prefix.nil? && !computername_prefix.empty? && (computername_hash || !computername[0, computername_prefix.size].casecmp(computername_prefix).zero?)
end
generate_password() click to toggle source
# File lib/smart_proxy_realm_ad/provider.rb, line 109
def generate_password
  characters = ('A'..'Z').to_a + ('a'..'z').to_a + (0..9).to_a
  Array.new(20) { characters.sample }.join
end
hostfqdn_to_computername(hostfqdn) click to toggle source
# File lib/smart_proxy_realm_ad/provider.rb, line 63
def hostfqdn_to_computername(hostfqdn)
  computername = hostfqdn

  # strip the domain from the host
  computername = computername.split('.').first unless computername_use_fqdn

  # generate the SHA256 hexdigest from the computername
  computername = Digest::SHA256.hexdigest(computername) if computername_hash

  # apply prefix if it has not already been applied
  computername = computername_prefix + computername if apply_computername_prefix?(computername)

  # limit length to 15 characters and upcase the computername
  # see https://support.microsoft.com/en-us/kb/909264
  computername[0, 15].upcase
end
kinit_radcli_connect() click to toggle source
# File lib/smart_proxy_realm_ad/provider.rb, line 84
def kinit_radcli_connect
  init_krb5_ccache(@keytab_path, @principal)
  @adconn = radcli_connect
end
radcli_connect() click to toggle source
# File lib/smart_proxy_realm_ad/provider.rb, line 89
def radcli_connect
  # Connect to active directory
  conn = Adcli::AdConn.new(@domain)
  conn.set_domain_realm(@realm)
  conn.set_domain_controller(@domain_controller)
  conn.set_login_ccache_name('')
  conn.connect
  conn
end
radcli_delete(computername) click to toggle source
# File lib/smart_proxy_realm_ad/provider.rb, line 123
def radcli_delete(computername)
  # Delete a computer's account
  enroll = Adcli::AdEnroll.new(@adconn)
  enroll.set_computer_name(computername)
  enroll.set_domain_ou(@ou) if @ou
  enroll.delete
end
radcli_join(hostfqdn, computername, password) click to toggle source
# File lib/smart_proxy_realm_ad/provider.rb, line 99
def radcli_join(hostfqdn, computername, password)
  # Join computer
  enroll = Adcli::AdEnroll.new(@adconn)
  enroll.set_computer_name(computername)
  enroll.set_host_fqdn(hostfqdn)
  enroll.set_domain_ou(@ou) if @ou
  enroll.set_computer_password(password)
  enroll.join
end
radcli_password(computername, password) click to toggle source
# File lib/smart_proxy_realm_ad/provider.rb, line 114
def radcli_password(computername, password)
  # Reset a computer's password
  enroll = Adcli::AdEnroll.new(@adconn)
  enroll.set_computer_name(computername)
  enroll.set_domain_ou(@ou) if @ou
  enroll.set_computer_password(password)
  enroll.password
end