Skip to content

Commit 7b4dc52

Browse files
committed
Handle join errors and existing accounts
1 parent 17d0a0a commit 7b4dc52

File tree

2 files changed

+77
-8
lines changed

2 files changed

+77
-8
lines changed

config/realm_ad.yml.example

+3
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@
2222

2323
# Optional: use the fqdn of the host to generate the computername
2424
#:computername_use_fqdn: false
25+
26+
# Optional: Ignore computer account already exists error (should only be used for computers already joined to Active Directory)
27+
#:ignore_computername_exists: true

lib/smart_proxy_realm_ad/provider.rb

+74-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Provider
88
include Proxy::Util
99
include Proxy::Kerberos
1010

11-
attr_reader :realm, :keytab_path, :principal, :domain_controller, :domain, :ou, :computername_prefix, :computername_hash, :computername_use_fqdn
11+
attr_reader :realm, :keytab_path, :principal, :domain_controller, :domain, :ou, :computername_prefix, :computername_hash, :computername_use_fqdn, :ignore_computername_exists
1212

1313
def initialize(options = {})
1414
@realm = options[:realm]
@@ -20,6 +20,7 @@ def initialize(options = {})
2020
@computername_prefix = options[:computername_prefix]
2121
@computername_hash = options[:computername_hash]
2222
@computername_use_fqdn = options[:computername_use_fqdn]
23+
@ignore_computername_exists = options.fetch(:ignore_computername_exists, false)
2324
logger.info 'Proxy::AdRealm: initialize...'
2425
end
2526

@@ -95,14 +96,18 @@ def radcli_connect
9596
conn
9697
end
9798

99+
MAX_RETRIES = 100
100+
RETRY_DELAY = 0.3
101+
98102
def radcli_join(hostfqdn, computername, password)
99-
# Join computer
100-
enroll = Adcli::AdEnroll.new(@adconn)
101-
enroll.set_computer_name(computername)
102-
enroll.set_host_fqdn(hostfqdn)
103-
enroll.set_domain_ou(@ou) if @ou
104-
enroll.set_computer_password(password)
105-
enroll.join
103+
enroll = setup_enroll(hostfqdn, computername, password)
104+
begin
105+
enroll.join
106+
logger.info "Successfully joined computer #{computername} with FQDN #{hostfqdn}")
107+
true
108+
rescue RuntimeError => ex
109+
handle_runtime_error(ex, enroll)
110+
end
106111
end
107112

108113
def generate_password
@@ -126,5 +131,66 @@ def radcli_delete(computername)
126131
enroll.set_domain_ou(@ou) if @ou
127132
enroll.delete
128133
end
134+
135+
private
136+
137+
def setup_enroll(hostfqdn, computername, password)
138+
enroll = Adcli::AdEnroll.new(@adconn)
139+
enroll.set_computer_name(computername)
140+
enroll.set_host_fqdn(hostfqdn)
141+
enroll.set_domain_ou(@ou) if @ou
142+
enroll.set_computer_password(password)
143+
enroll
144+
end
145+
146+
def handle_runtime_error(ex, enroll)
147+
if ex.message =~ /Authentication error/
148+
retry_authentication_error(enroll)
149+
elsif ex.message =~ /already exists/
150+
handle_already_exists_error
151+
else
152+
log_error("Failed to join computer: #{ex.message}")
153+
raise ex
154+
end
155+
end
156+
157+
def retry_authentication_error(enroll)
158+
MAX_RETRIES.times do |i|
159+
sleep(RETRY_DELAY)
160+
begin
161+
if enroll.respond_to?(:update)
162+
enroll.update
163+
else
164+
enroll.password
165+
end
166+
log_info("Successfully updated computer after authentication error")
167+
return true
168+
rescue RuntimeError => ex
169+
if i >= MAX_RETRIES - 1 || ex.message !~ /Authentication error/
170+
log_error("Failed to update computer after #{MAX_RETRIES} attempts: #{ex.message}")
171+
raise ex
172+
end
173+
end
174+
end
175+
end
176+
177+
def handle_already_exists_error
178+
if ignore_computername_exists
179+
log_info("Computer name already exists, but ignoring as per configuration")
180+
true
181+
else
182+
log_error("Computer name already exists and cannot proceed")
183+
raise "Computer name already exists"
184+
end
185+
end
186+
187+
def log_info(message)
188+
logger.info "Proxy::AdRealm: #{message}"
189+
end
190+
191+
def log_error(message)
192+
logger.error "Proxy::AdRealm: #{message}"
193+
end
194+
129195
end
130196
end

0 commit comments

Comments
 (0)