Module: Xolo::Server::Helpers::JamfPro

Included in:
Title, Version
Defined in:
lib/xolo/server/helpers/jamf_pro.rb

Overview

constants and methods for accessing the Jamf Pro server from the Xolo server

This is used both as a ‘helper’ in the Sinatra server, and an included mixin for the Xolo::Server::Title and Xolo::Server::Version classes.

This means methods here are available in instances of those classes, and in all routes, views, and helpers in Sinatra.

Constant Summary collapse

PATCH_REPORT_UNKNOWN_VERSION =

Constants

'UNKNOWN_VERSION'
PATCH_REPORT_JPAPI_PAGE_SIZE =
500

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(extender) ⇒ Object

when this module is extended



53
54
55
# File 'lib/xolo/server/helpers/jamf_pro.rb', line 53

def self.extended(extender)
  Xolo.verbose_extend extender, self
end

.included(includer) ⇒ Object

when this module is included



48
49
50
# File 'lib/xolo/server/helpers/jamf_pro.rb', line 48

def self.included(includer)
  Xolo.verbose_include includer, self
end

Instance Method Details

#jamf_cnx(refresh: false) ⇒ Jamf::Connection

A connection to Jamf Pro via ruby-jss.

We don’t use the default connection but use this method to create standalone ones as needed and ensure they are disconnected, (or will timeout) when we are done.

TODO: allow using APIClients

Returns:

  • (Jamf::Connection)

    A connection object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/xolo/server/helpers/jamf_pro.rb', line 89

def jamf_cnx(refresh: false)
  if refresh
    @jamf_cnx = nil
    log_debug 'Jamf: Refreshing Jamf connection'
  end

  return @jamf_cnx if @jamf_cnx

  cnx_opts = {
    name: "jamf-pro-cnx-#{Time.now.strftime('%F-%T')}",
    host: Xolo::Server.config.jamf_hostname,
    port: Xolo::Server.config.jamf_port,
    verify_cert: Xolo::Server.config.jamf_verify_cert,
    ssl_version: Xolo::Server.config.jamf_ssl_version,
    open_timeout: Xolo::Server.config.jamf_open_timeout,
    timeout: Xolo::Server.config.jamf_timeout
  }

  if Xolo::Server.config.jamf_use_api_client
    cnxtype = 'API Client'
    cnx_opts[:client_id] = Xolo::Server.config.jamf_api_user
    cnx_opts[:client_secret] = Xolo::Server.config.jamf_api_pw
  else
    cnxtype = 'User'
    cnx_opts[:user] = Xolo::Server.config.jamf_api_user
    cnx_opts[:pw] = Xolo::Server.config.jamf_api_pw
  end

  @jamf_cnx = Jamf::Connection.new(**cnx_opts)

  log_debug "Jamf: Connected to Jamf Pro at #{@jamf_cnx.base_url} as #{cnxtype} '#{Xolo::Server.config.jamf_api_user}'. KeepAlive: false, Expires: #{@jamf_cnx.token.expires}. cnx ID: #{@jamf_cnx.object_id}"
  # log_debug "jamf_cnx caller:\n..#{caller_locations(1, 10).map(&:to_s).join("\n..")}"
  @jamf_cnx
end

#jamf_gui_urlString

Returns The start of the Jamf Pro URL for GUI/WebApp access.

Returns:

  • (String)

    The start of the Jamf Pro URL for GUI/WebApp access



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/xolo/server/helpers/jamf_pro.rb', line 66

def jamf_gui_url
  return @jamf_gui_url if @jamf_gui_url

  host = Xolo::Server.config.jamf_gui_hostname
  host ||= Xolo::Server.config.jamf_hostname

  port = Xolo::Server.config.jamf_gui_port
  port ||= Xolo::Server.config.jamf_port

  @jamf_gui_url = "https://#{host}:#{port}"
end

#jamf_obj_name_pfx_baseString

If we’re running on a test server, Jamf objects have a different prefix which is ‘xolo-’ prod servers and ‘xolotest-’ on test servers.

Returns:

  • (String)

    The prefix for Jamf object names



166
167
168
169
170
171
172
# File 'lib/xolo/server/helpers/jamf_pro.rb', line 166

def jamf_obj_name_pfx_base
  if Xolo::Server.config.test_server
    Xolo::Server::JAMF_TEST_OBJECT_NAME_PFX
  else
    Xolo::Server::JAMF_OBJECT_NAME_PFX
  end
end

#jamf_xolo_category_idObject

The id of the ‘xolo’ category in Jamf Pro.s



126
127
128
129
130
131
132
133
# File 'lib/xolo/server/helpers/jamf_pro.rb', line 126

def jamf_xolo_category_id
  @jamf_xolo_category_id ||=
    if Jamf::Category.all_names(cnx: jamf_cnx).include? Xolo::Server::JAMF_XOLO_CATEGORY
      Jamf::Category.valid_id(Xolo::Server::JAMF_XOLO_CATEGORY, cnx: jamf_cnx).to_s
    else
      Jamf::Category.create(name: Xolo::Server::JAMF_XOLO_CATEGORY, cnx: jamf_cnx).save
    end
end

#valid_forced_exclusion_group_nameString

if there’s a forced_exclusion group defined in the server config return it’s name, but only if it exists in jamf. If it doesn’t return nil and alert someone

Returns:

  • (String)

    The valid name of the forced exclusion group



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/xolo/server/helpers/jamf_pro.rb', line 141

def valid_forced_exclusion_group_name
  return @valid_forced_exclusion_group_name if defined?(@valid_forced_exclusion_group_name)

  the_grp_name = Xolo::Server.config.forced_exclusion

  if the_grp_name
    if Jamf::ComputerGroup.all_names(cnx: jamf_cnx).include? the_grp_name
      @valid_forced_exclusion_group_name = the_grp_name
    else
      msg = "ERROR: The forced_exclusion group '#{Xolo::Server.config.forced_exclusion}' in xolo server config does not exist in Jamf"
      log_error msg, alert: true
      @valid_forced_exclusion_group_name = nil
    end

  # not in config
  else
    @valid_forced_exclusion_group_name = nil
  end
end