Skip to content

Commit e3e9a42

Browse files
authored
Merge pull request #5 from timogoebel/cleanup
code cleanups
2 parents 1bf1fb5 + ca35213 commit e3e9a42

File tree

7 files changed

+134
-303
lines changed

7 files changed

+134
-303
lines changed

Gemfile.lock

-59
This file was deleted.

Rakefile

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
require 'rake'
32
require 'rake/testtask'
43

@@ -12,4 +11,4 @@ Rake::TestTask.new(:test) do |t|
1211
t.libs << 'test'
1312
t.test_files = FileList['test/**/*_test.rb']
1413
t.verbose = true
15-
end
14+
end
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
module Proxy::AdRealm
2-
class ConfigurationLoader
3-
def load_classes
4-
require 'smart_proxy_realm_ad/provider'
5-
end
2+
class ConfigurationLoader
3+
def load_classes
4+
require 'smart_proxy_realm_ad/provider'
5+
end
66

7-
def load_dependency_injection_wirings(container_instance, settings)
8-
container_instance.dependency:realm_provider_impl,
9-
lambda {
10-
::Proxy::AdRealm::Provider.new(
11-
settings[:realm],
12-
settings[:keytab_path],
13-
settings[:principal],
14-
settings[:domain_controller],
15-
settings[:ou]
16-
)
17-
}
18-
end
7+
def load_dependency_injection_wirings(container_instance, settings)
8+
container_instance.dependency :realm_provider_impl,
9+
lambda {
10+
::Proxy::AdRealm::Provider.new(
11+
settings[:realm],
12+
settings[:keytab_path],
13+
settings[:principal],
14+
settings[:domain_controller],
15+
settings[:ou]
16+
)
17+
}
1918
end
19+
end
2020
end

lib/smart_proxy_realm_ad/plugin.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
require 'smart_proxy_realm_ad/version'
22

33
module Proxy::AdRealm
4-
class Plugin < Proxy::Provider
5-
load_classes ::Proxy::AdRealm::ConfigurationLoader
6-
load_dependency_injection_wirings ::Proxy::AdRealm::ConfigurationLoader
4+
class Plugin < Proxy::Provider
5+
load_classes ::Proxy::AdRealm::ConfigurationLoader
6+
load_dependency_injection_wirings ::Proxy::AdRealm::ConfigurationLoader
77

8-
validate_presence :realm, :keytab_path, :principal, :domain_controller
8+
validate_presence :realm, :keytab_path, :principal, :domain_controller
99

10-
plugin :realm_ad, ::Proxy::AdRealm::VERSION
11-
end
12-
end
10+
plugin :realm_ad, ::Proxy::AdRealm::VERSION
11+
end
12+
end

lib/smart_proxy_realm_ad/provider.rb

+108-115
Original file line numberDiff line numberDiff line change
@@ -3,121 +3,114 @@
33
require 'passgen'
44

55
module Proxy::AdRealm
6-
class Provider
7-
include Proxy::Log
8-
include Proxy::Util
9-
include Proxy::Kerberos
10-
11-
def initialize(realm, keytab_path, principal, domain_controller, ou)
12-
@realm = realm
13-
@keytab_path = keytab_path
14-
@principal = principal
15-
@domain_controller = domain_controller
16-
@domain = realm.downcase
17-
@ou = ou
18-
logger.info "Proxy::AdRealm: initialize... #{@realm}, #{@keytab_path}, #{@principal}, #{@domain_controller}, #{@domain}, #{ou}"
19-
end
20-
21-
def check_realm realm
22-
raise Exception.new "Unknown realm #{realm}" unless realm.casecmp(@realm).zero?
23-
end
24-
25-
def find hostfqdn
26-
true
27-
end
28-
29-
def create realm, hostfqdn, params
30-
logger.info "Proxy::AdRealm: create... #{realm}, #{hostfqdn}, #{params}"
31-
check_realm realm
32-
kinit_radcli_connect
33-
34-
password = generate_password
35-
result = { :randompassword => password }
36-
37-
if params[:rebuild] == "true"
38-
do_host_rebuild hostfqdn, password
39-
else
40-
do_host_create hostfqdn, password
41-
end
42-
43-
JSON.pretty_generate(result)
44-
end
45-
46-
def delete realm, hostfqdn
47-
logger.info "Proxy::AdRealm: delete... #{realm}, #{hostfqdn}"
48-
kinit_radcli_connect
49-
check_realm realm
50-
radcli_delete hostfqdn
51-
end
52-
53-
private
54-
55-
def hostfqdn_to_hostname host_fqdn
56-
begin
57-
host_fqdn_split = host_fqdn.split('.')
58-
host_fqdn_split[0]
59-
rescue => e
60-
logger.debug "hostfqdn_to_hostname error: #{e}"
61-
raise e
62-
end
63-
end
64-
65-
def do_host_create hostfqdn, password
66-
hostname = hostfqdn_to_hostname hostfqdn
67-
radcli_join hostfqdn, hostname, password
68-
end
69-
70-
def do_host_rebuild hostfqdn, password
71-
hostname = hostfqdn_to_hostname hostfqdn
72-
radcli_password hostname, password
73-
74-
end
75-
76-
def kinit_radcli_connect
77-
init_krb5_ccache @keytab_path, @principal
78-
@adconn = radcli_connect()
79-
end
80-
81-
def radcli_connect
82-
# Connect to active directory
83-
conn = Adcli::AdConn.new(@domain)
84-
conn.set_domain_realm(@realm)
85-
conn.set_domain_controller(@domain_controller)
86-
conn.set_login_ccache_name("")
87-
conn.connect()
88-
return conn
89-
end
90-
91-
def radcli_join hostfqdn, hostname, password
92-
# Join computer
93-
enroll = Adcli::AdEnroll.new(@adconn)
94-
enroll.set_computer_name(hostname)
95-
enroll.set_host_fqdn(hostfqdn)
96-
enroll.set_domain_ou(@ou) if @ou
97-
enroll.set_computer_password(password)
98-
enroll.join()
99-
end
100-
101-
def generate_password
102-
Passgen::generate(:length => 20)
103-
end
104-
105-
def radcli_password hostname, password
106-
# Reset a computer's password
107-
enroll = Adcli::AdEnroll.new(@adconn)
108-
enroll.set_computer_name(hostname)
109-
enroll.set_domain_ou(@ou) if @ou
110-
enroll.set_computer_password(password)
111-
enroll.password()
112-
end
113-
114-
def radcli_delete hostname
115-
# Delete a computer's account
116-
enroll = Adcli::AdEnroll.new(@adconn)
117-
enroll.set_computer_name(hostname)
118-
enroll.set_domain_ou(@ou) if @ou
119-
enroll.delete()
120-
end
6+
class Provider
7+
include Proxy::Log
8+
include Proxy::Util
9+
include Proxy::Kerberos
10+
11+
def initialize(realm, keytab_path, principal, domain_controller, ou)
12+
@realm = realm
13+
@keytab_path = keytab_path
14+
@principal = principal
15+
@domain_controller = domain_controller
16+
@domain = realm.downcase
17+
@ou = ou
18+
logger.info "Proxy::AdRealm: initialize... #{@realm}, #{@keytab_path}, #{@principal}, #{@domain_controller}, #{@domain}, #{@ou}"
19+
end
20+
21+
def check_realm(realm)
22+
raise Exception, "Unknown realm #{realm}" unless realm.casecmp(@realm).zero?
23+
end
24+
25+
def find(_hostfqdn)
26+
true
27+
end
28+
29+
def create(realm, hostfqdn, params)
30+
logger.info "Proxy::AdRealm: create... #{realm}, #{hostfqdn}, #{params}"
31+
check_realm(realm)
32+
kinit_radcli_connect
33+
34+
password = generate_password
35+
result = { randompassword: password }
36+
37+
if params[:rebuild] == 'true'
38+
do_host_rebuild(hostfqdn, password)
39+
else
40+
do_host_create(hostfqdn, password)
41+
end
42+
43+
JSON.pretty_generate(result)
44+
end
45+
46+
def delete(realm, hostfqdn)
47+
logger.info "Proxy::AdRealm: delete... #{realm}, #{hostfqdn}"
48+
kinit_radcli_connect
49+
check_realm(realm)
50+
radcli_delete(hostfqdn)
51+
end
52+
53+
private
54+
55+
def hostfqdn_to_hostname(host_fqdn)
56+
host_fqdn_split = host_fqdn.split('.')
57+
host_fqdn_split.first
58+
end
59+
60+
def do_host_create(hostfqdn, password)
61+
hostname = hostfqdn_to_hostname(hostfqdn)
62+
radcli_join(hostfqdn, hostname, password)
63+
end
64+
65+
def do_host_rebuild(hostfqdn, password)
66+
hostname = hostfqdn_to_hostname hostfqdn
67+
radcli_password(hostname, password)
68+
end
69+
70+
def kinit_radcli_connect
71+
init_krb5_ccache(@keytab_path, @principal)
72+
@adconn = radcli_connect
73+
end
74+
75+
def radcli_connect
76+
# Connect to active directory
77+
conn = Adcli::AdConn.new(@domain)
78+
conn.set_domain_realm(@realm)
79+
conn.set_domain_controller(@domain_controller)
80+
conn.set_login_ccache_name('')
81+
conn.connect
82+
conn
83+
end
84+
85+
def radcli_join(hostfqdn, hostname, password)
86+
# Join computer
87+
enroll = Adcli::AdEnroll.new(@adconn)
88+
enroll.set_computer_name(hostname)
89+
enroll.set_host_fqdn(hostfqdn)
90+
enroll.set_domain_ou(@ou) if @ou
91+
enroll.set_computer_password(password)
92+
enroll.join
93+
end
94+
95+
def generate_password
96+
Passgen.generate(:length => 20)
97+
end
98+
99+
def radcli_password(hostname, password)
100+
# Reset a computer's password
101+
enroll = Adcli::AdEnroll.new(@adconn)
102+
enroll.set_computer_name(hostname)
103+
enroll.set_domain_ou(@ou) if @ou
104+
enroll.set_computer_password(password)
105+
enroll.password
106+
end
121107

108+
def radcli_delete(hostname)
109+
# Delete a computer's account
110+
enroll = Adcli::AdEnroll.new(@adconn)
111+
enroll.set_computer_name(hostname)
112+
enroll.set_domain_ou(@ou) if @ou
113+
enroll.delete
122114
end
115+
end
123116
end

lib/smart_proxy_realm_ad/version.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Proxy
22
module AdRealm
3-
VERSION = '0.1'
3+
VERSION = '0.1'.freeze
44
end
5-
end
5+
end

0 commit comments

Comments
 (0)