Class: IPA::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ipa/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: nil, ca_cert: '/etc/ipa/ca.crt') ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ipa/client.rb', line 17

def initialize(host: nil, ca_cert: '/etc/ipa/ca.crt')
  raise ArgumentError, 'Missing FreeIPA host' unless host

  @uri = URI.parse("https://#{host}/ipa/session/json")

  @http = HTTPClient.new
  @http.ssl_config.set_trust_ca(ca_cert)
  @headers = {'referer' => "https://#{uri.host}/ipa/json", 'Content-Type' => 'application/json', 'Accept' => 'application/json'}

  self.(host)
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



15
16
17
# File 'lib/ipa/client.rb', line 15

def headers
  @headers
end

#httpObject (readonly)

Returns the value of attribute http.



15
16
17
# File 'lib/ipa/client.rb', line 15

def http
  @http
end

#uriObject (readonly)

Returns the value of attribute uri.



15
16
17
# File 'lib/ipa/client.rb', line 15

def uri
  @uri
end

Instance Method Details

#api_post(method: nil, item: [], params: {}) ⇒ Object

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ipa/client.rb', line 44

def api_post(method: nil, item: [], params: {})
  raise ArgumentError, 'Missing method in API request' unless method

  if Time.new.to_i > @session_timeout then
    self.
  end

  request = {}
  request[:method] = method
  request[:params] = [[item || []], params]
  resp = self.http.post(self.uri, request.to_json, self.headers)
  JSON.parse(resp.body)
end

#host_add(hostname: nil, all: false, force: false, random: nil, userpassword: nil, params: {}) ⇒ Object

Raises:

  • (ArgumentError)


91
92
93
94
95
96
97
98
99
100
# File 'lib/ipa/client.rb', line 91

def host_add(hostname: nil, all: false, force: false, random: nil, userpassword: nil, params: {})
  raise ArgumentError, 'Hostname is required' unless hostname

  params[:all] = all
  params[:force] = force
  params[:random] = random unless random.nil?
  params[:userpassword] = userpassword unless userpassword.nil?

  self.api_post(method: 'host_add', item: hostname, params: params)
end

#host_del(hostname: nil, params: {}) ⇒ Object

Raises:

  • (ArgumentError)


102
103
104
105
106
# File 'lib/ipa/client.rb', line 102

def host_del(hostname: nil, params: {})
  raise ArgumentError, 'Hostname is required' unless hostname

  self.api_post(method: 'host_del', item: hostname, params: params)
end

#host_exists?(hostname) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
125
126
127
128
129
# File 'lib/ipa/client.rb', line 122

def host_exists?(hostname)
  resp = self.host_show(hostname: hostname)
  if resp['error']
    false
  else
    true
  end
end

#host_find(hostname: nil, all: false, params: {}) ⇒ Object



108
109
110
111
112
# File 'lib/ipa/client.rb', line 108

def host_find(hostname: nil, all: false, params: {})
  params[:all] = all

  self.api_post(method: 'host_find', item: hostname, params: params)
end

#host_show(hostname: nil, all: false, params: {}) ⇒ Object

Raises:

  • (ArgumentError)


114
115
116
117
118
119
120
# File 'lib/ipa/client.rb', line 114

def host_show(hostname: nil, all: false, params: {})
  raise ArgumentError, 'Hostname is required' unless hostname

  params[:all] = all

  self.api_post(method: 'host_show', item: hostname, params: params)
end

#hostgroup_add(hostgroup: nil, description: nil, all: false, params: {}) ⇒ Object

Raises:

  • (ArgumentError)


66
67
68
69
70
71
72
73
74
# File 'lib/ipa/client.rb', line 66

def hostgroup_add(hostgroup: nil, description: nil, all: false, params: {})
  raise ArgumentError, 'Hostgroup is required' unless hostgroup
  raise ArgumentError, 'description is required' unless description

  params[:all] = all
  params[:description] = description

  self.api_post(method: 'hostgroup_add', item: hostgroup, params: params)
end

#hostgroup_add_member(hostgroup: nil, hostnames: nil, params: {}) ⇒ Object

Raises:

  • (ArgumentError)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ipa/client.rb', line 76

def hostgroup_add_member(hostgroup: nil, hostnames: nil, params: {})
  raise ArgumentError, 'Hostgroup is required' unless hostgroup
  raise ArgumentError, 'Hostnames is required' unless hostnames
  params[:all] = true

  if hostnames.kind_of?(Array)
    params[:host] = hostnames
  end
  if hostnames.kind_of?(String)
    params[:host] = [hostnames]
  end

  self.api_post(method: 'hostgroup_add_member', item: hostgroup, params: params)
end

#hostgroup_show(hostgroup: nil, all: false, params: {}) ⇒ Object

Raises:

  • (ArgumentError)


58
59
60
61
62
63
64
# File 'lib/ipa/client.rb', line 58

def hostgroup_show(hostgroup: nil,all: false, params: {})
  raise ArgumentError, 'Hostgroup is required' unless hostgroup

  params[:all] = all

  self.api_post(method: 'hostgroup_show', item: hostgroup, params: params)
end

#login(host) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ipa/client.rb', line 29

def (host)
  # Set the timeout to 15 minutes
  @session_timeout = (Time.new.to_i + 900)

  gssapi = GSSAPI::Simple.new(@uri.host, 'HTTP')
  # Initiate the security context
  token = gssapi.init_context

   = URI.parse("https://#{host}/ipa/session/login_kerberos")
   = {:method => "ping", :params => [[], {}]}
   = {'referer' => "https://#{uri.host}/ipa/ui/index.html", 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => "Negotiate #{Base64.strict_encode64(token)}"}

  self.http.post(, .to_json, )
end