Module: Xolo::Admin::Validate
- Defined in:
- lib/xolo/admin/validate.rb
Overview
A collection of methods to convert and validate values used in Xolo.
These methods will convert target values if possible, e.g. if a value should be an Integer, a String containing an integer will be converted before validation.
If the converted value is valid, it will be returned, otherwise a Xolo::InvalidDataError will be raised.
Constant Summary collapse
- TRUE_RE =
True can be specified as ‘true’ or ‘y’ or ‘yes’ case insensitively. Anything else is false.
/(\Atrue\z)|(\Ay(es)?\z)/i.freeze
- TITLE_ATTRS =
Convenience - constants from classes
Xolo::Admin::Title::ATTRIBUTES
- VERSION_ATTRS =
Xolo::Admin::Version::ATTRIBUTES
- SSVC_ICON_MIME_TYPES =
Self Service icons must be one of these mime types, as determined by the output of ‘file -b -I`
%w[ image/jpeg image/png image/gif ].freeze
- MIN_TITLE_DESC_LENGTH =
The minimum length of a Titles Description.
25- OS_VERSION_RE =
OS versions (min/max) must match this regex.
-
Start of string
-
two digits
-
optionally
-
a dot
-
one or more digits
-
optionally
-
a dot
-
one or more digits
-
-
-
/\A\d\d(\.\d+){0,2}\z/.freeze
Class Method Summary collapse
-
.included(includer) ⇒ Object
when this module is included.
Instance Method Summary collapse
-
#bad_jamf_groups(group_ary) ⇒ Array<String>
Jamf groups that do not exist.
-
#current_title_type(opts) ⇒ Object
given our current options, are we working with a subscribed or managed title?.
-
#expand_use_title(ka) ⇒ String
expand Xolo::Admin::Version::USE_TITLE_FOR_KILLAPP into the proper killall string.
-
#raise_consistency_error(msg) ⇒ void
These methods all raise this error.
-
#raise_invalid_data_error(val, msg) ⇒ Object
Thes methods all raise this error.
-
#validate_app_bundle_id(val) ⇒ String
validate a title app_bundle_id.
-
#validate_app_name(val) ⇒ String
validate a title app_name.
-
#validate_boolean(val) ⇒ Boolean
validate boolean options.
-
#validate_cli_cmd_opts ⇒ Object
Validate the command options acquired from the command line.
-
#validate_cli_command ⇒ Object
is the given command valid?.
-
#validate_cli_title ⇒ Object
were we given a title?.
-
#validate_cli_version ⇒ Object
were we given a version?.
-
#validate_contact_email(val) ⇒ String
The valid value.
-
#validate_deploy_groups(val) ⇒ Array<String>
validate an array of jamf groups to use as MDM deployment targets.
-
#validate_editor(val) ⇒ void
Does the chosen editor exist and is it executable?.
-
#validate_excluded_groups(val) ⇒ Array<String>
validate an array of jamf groups to use as exclusions.
-
#validate_expiration(val) ⇒ Integer
validate a titles expiration.
-
#validate_expire_paths(val) ⇒ Array<String>
validate a title expiration paths.
-
#validate_hostname(val) ⇒ void
Try to fetch a known route from the given xolo server.
-
#validate_internal_consistency(opts) ⇒ void
Given an ostruct of options that have been individually validated, and combined with any current_opt_values as needed, check the data for internal consistency.
-
#validate_killapps(val) ⇒ Array<Array<String>>
The valid value.
-
#validate_max_os(val) ⇒ Gem::Version
The valid value.
-
#validate_min_os(val) ⇒ String
The valid value.
-
#validate_min_os_and_max_os(opts) ⇒ void
min_os must be <= max_os max_os must be >= min_os.
-
#validate_patch_source(val) ⇒ Pathname
validate a patch source name.
-
#validate_pilot_groups(val) ⇒ Array<String>
validate an array of jamf groups to use as pilots for testing a version before releasing it.
-
#validate_pkg_to_upload(val) ⇒ Pathname
validate a path to a jamf_pkg_file.
-
#validate_publish_date(val) ⇒ Date
The valid value.
-
#validate_publisher(val) ⇒ String
validate a title publisher.
-
#validate_pw(val) ⇒ void
Password (and username) will be validated via the server.
-
#validate_release_groups(val) ⇒ Array<String>
validate an array of jamf group names to use as targets when released.
-
#validate_release_to_all_allowed ⇒ void
check if the current admin is allowed to set a title’s release groups to ‘all’.
-
#validate_scope_targets_and_exclusions(opts) ⇒ void
groups that will be scope targets (pilot or release) cannot also be in the exclusions.
-
#validate_self_service_category(val) ⇒ Boolean, String
validate a self_service_category.
-
#validate_self_service_icon(val) ⇒ Pathname
validate a path to a self_service_icon.
-
#validate_single_killapp(ka) ⇒ String
Validate an individual killapp string.
-
#validate_subbed_v_managed(opts) ⇒ Object
Complain about using options not meant for the chosen title type.
-
#validate_title(val) ⇒ String
validate a Xolo title.
- #validate_title_consistency(opts) ⇒ void
-
#validate_title_consistency_app_and_script(opts) ⇒ void
if app_name or app_bundle_id is given, can’t use –version-script, and vice versa.
-
#validate_title_consistency_app_name_and_id(opts) ⇒ void
If using app_name and bundle id, both must be given.
-
#validate_title_consistency_app_or_script(opts) ⇒ void
if managed must use version_script, or app.
-
#validate_title_consistency_display_name_and_publisher(opts) ⇒ Object
If managing this title, display_name and publisher must be given.
-
#validate_title_consistency_expire_paths(opts) ⇒ void
if expiration is > 0, there must be at least one expiration path.
-
#validate_title_consistency_no_all_in_ssvc(opts) ⇒ void
if target_group is all, can’t be in self service.
-
#validate_title_consistency_patch_source_and_title_id(opts) ⇒ void
If subscribing to a title both patch_source and title_id must be given.
-
#validate_title_consistency_ssvc_needs_category(opts) ⇒ void
if in self service, a category must be assigned.
-
#validate_title_consistency_title_id_exists(opts) ⇒ void
If subscribing to a title, the title_id must exist in the given patch_source.
-
#validate_title_consistency_uninstall(opts) ⇒ void
if uninstall script, cant have uninstall ids and vice versa.
-
#validate_title_desc(val) ⇒ Boolean, String
validate a title description.
-
#validate_title_display_name(val) ⇒ String
validate a title display-name.
-
#validate_title_id(val) ⇒ Pathname
validate a patch title id.
-
#validate_type(val) ⇒ String
validate the title type.
-
#validate_uninstall_ids(val) ⇒ Array<String>
validate a title uninstall ids: - an array of package identifiers OR - ‘none’ to unset the value.
-
#validate_uninstall_script(val) ⇒ String
validate a title uninstall script: - a path to a script which must start with ‘#!’ OR - ‘none’ to unset the value.
-
#validate_version(val) ⇒ String
TODO: validate a Xolo Version.
- #validate_version_consistency(opts) ⇒ void
-
#validate_version_script(val) ⇒ Pathname
validate a title version script.
Class Method Details
.included(includer) ⇒ Object
when this module is included
64 65 66 |
# File 'lib/xolo/admin/validate.rb', line 64 def self.included(includer) Xolo.verbose_include includer, self end |
Instance Method Details
#bad_jamf_groups(group_ary) ⇒ Array<String>
Returns Jamf groups that do not exist.
475 476 477 478 479 |
# File 'lib/xolo/admin/validate.rb', line 475 def bad_jamf_groups(group_ary) group_ary = [group_ary] unless group_ary.is_a? Array group_ary - jamf_computer_group_names end |
#current_title_type(opts) ⇒ Object
given our current options, are we working with a subscribed or managed title?
859 860 861 |
# File 'lib/xolo/admin/validate.rb', line 859 def current_title_type(opts) opts[:subscribed] ? Xolo::SUBSCRIBED : Xolo::MANAGED end |
#expand_use_title(ka) ⇒ String
expand Xolo::Admin::Version::USE_TITLE_FOR_KILLAPP into the proper killall string
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 |
# File 'lib/xolo/admin/validate.rb', line 677 def (ka) return ka unless ka == Xolo::Admin::Version::USE_TITLE_FOR_KILLAPP title = Xolo::Admin::Title.fetch cli_cmd.title, server_cnx unless title.app_name raise_invalid_data_error Xolo::Admin::Version::USE_TITLE_FOR_KILLAPP, 'Title does not use app_name' end unless title.app_bundle_id raise_invalid_data_error Xolo::Admin::Version::USE_TITLE_FOR_KILLAPP, 'Title does not use app_bundle_id' end "#{title.app_name};#{title.app_bundle_id}" end |
#raise_consistency_error(msg) ⇒ void
This method returns an undefined value.
These methods all raise this error
823 824 825 |
# File 'lib/xolo/admin/validate.rb', line 823 def raise_consistency_error(msg) raise Xolo::InvalidDataError, msg end |
#raise_invalid_data_error(val, msg) ⇒ Object
Thes methods all raise this error
73 74 75 |
# File 'lib/xolo/admin/validate.rb', line 73 def raise_invalid_data_error(val, msg) raise Xolo::InvalidDataError, "'#{val}': #{msg}" end |
#validate_app_bundle_id(val) ⇒ String
validate a title app_bundle_id. Must include at least one dot
298 299 300 301 302 303 |
# File 'lib/xolo/admin/validate.rb', line 298 def validate_app_bundle_id(val) val = val.to_s.strip return val if val.include? Xolo::DOT raise_invalid_data_error val, TITLE_ATTRS[:app_bundle_id][:invalid_msg] end |
#validate_app_name(val) ⇒ String
validate a title app_name. Must end with .app
285 286 287 288 289 290 |
# File 'lib/xolo/admin/validate.rb', line 285 def validate_app_name(val) val = val.to_s.strip return val if val.end_with? Xolo::DOTAPP raise_invalid_data_error val, TITLE_ATTRS[:app_name][:invalid_msg] end |
#validate_boolean(val) ⇒ Boolean
validate boolean options
Never raises an error, just returns true of false based on the string value
529 530 531 |
# File 'lib/xolo/admin/validate.rb', line 529 def validate_boolean(val) val.to_s =~ TRUE_RE ? true : false end |
#validate_cli_cmd_opts ⇒ Object
Validate the command options acquired from the command line. Walkthru will validate them individually as they are entered.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/xolo/admin/validate.rb', line 120 def validate_cli_cmd_opts cmd = cli_cmd.command opts_defs = Xolo::Admin::Options::COMMANDS[cmd][:opts] return if opts_defs.empty? opts_defs.each do |key, deets| # skip things not given on the command line next unless cli_cmd_opts["#{key}_given"] if deets[:add_only] && edit_command? raise ArgumentError, "Option '--#{key.to_s.gsub '_', '-'}' is only valid for 'add-' commands." elsif deets[:edit_only] && add_command? raise ArgumentError, "Option '--#{key.to_s.gsub '_', '-'}' is only valid for 'edit-' commands." end # skip things that shouldn't be validated next unless deets[:validate] # if the item is not required, and 'none' is given, set the # value to nil and we're done unless deets[:required] if cli_cmd_opts[key].is_a?(Array) && cli_cmd_opts[key].include?(Xolo::NONE) cli_cmd_opts[key] = [] next elsif cli_cmd_opts[key] == Xolo::NONE cli_cmd_opts[key] = nil next end end # If an item is :multi, it is an array. # If it has only one item, split it on commas # This handles multi items being given multiple times as CLI opts, or # as comma-sep values in CLI opts or walkthru. # if deets[:multi] && cli_cmd_opts[key].size == 1 cli_cmd_opts[key] = cli_cmd_opts[key].first.split(Xolo::COMMA_SEP_RE) end validation_method = case deets[:validate] when Symbol deets[:validate] when TrueClass "validate_#{key}" end # nothing to do if no validation method next unless validation_method # run the validation, which raises an error if invalid, or returns # the converted value if OK - the converted value replaces the original in the # cmd_opts # puts "Validating #{key}, value '#{cli_cmd_opts[key]}', with #{validation_method}" if debug? cli_cmd_opts[key] = send validation_method, cli_cmd_opts[key] # puts "Valid Value for #{key} is now '#{cli_cmd_opts[key]}'" if debug? end # if we are here, eveything on the commandline checked out, so now # go through the opts_defs keys, and for any that were not given on the command line, # add the value for that key from current_opt_values to Xolo::Admin::Options.cli_cmd_opts # which will then be passed for internal consistency validation. end |
#validate_cli_command ⇒ Object
is the given command valid?
79 80 81 82 83 84 |
# File 'lib/xolo/admin/validate.rb', line 79 def validate_cli_command return if Xolo::Admin::Options::COMMANDS.key? cli_cmd.command msg = cli_cmd.command.pix_empty? ? "Usage: #{usage}" : "Unknown command: '#{cli_cmd.command}'" raise ArgumentError, msg end |
#validate_cli_title ⇒ Object
were we given a title?
88 89 90 91 92 93 94 95 96 |
# File 'lib/xolo/admin/validate.rb', line 88 def validate_cli_title # this command doesn't need a title arg return if Xolo::Admin::Options::COMMANDS[cli_cmd.command][:target] == :none raise ArgumentError, "No title provided!\nUsage: #{usage}" unless cli_cmd.title # this validates the format validate_title cli_cmd.title end |
#validate_cli_version ⇒ Object
were we given a version?
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/xolo/admin/validate.rb', line 100 def validate_cli_version # this command doesn't need a version arg return unless version_command? || title_or_version_command? # TODO: # If this is an 'add-' command, ensure the version # doesn't already exist. # Otherwise, make sure it does already exist # vers = cli_cmd.version return unless vers.to_s.empty? || vers.start_with?(Xolo::DASH) raise ArgumentError, "No version provided with '#{cli_cmd.command}' command!\nUsage: #{usage}" end |
#validate_contact_email(val) ⇒ String
Returns The valid value.
665 666 667 668 669 670 |
# File 'lib/xolo/admin/validate.rb', line 665 def validate_contact_email(val) val = val.to_s.strip return val if val =~ /\A\S+@\S+\.\S+\z/ raise_invalid_data_error val, TITLE_ATTRS[:contact_email][:invalid_msg] end |
#validate_deploy_groups(val) ⇒ Array<String>
validate an array of jamf groups to use as MDM deployment targets. ‘none’ is also acceptable
460 461 462 463 464 465 466 467 468 469 470 |
# File 'lib/xolo/admin/validate.rb', line 460 def validate_deploy_groups(val) val = [val] unless val.is_a? Array return [] if val.include? Xolo::NONE bad_grps = bad_jamf_groups(val) return val if bad_grps.empty? bad_grps = "No Such Groups: #{bad_grps.join(Xolo::COMMA_JOIN)}" raise_invalid_data_error bad_grps, 'Invalid group(s). Must exist in Jamf Pro.' end |
#validate_editor(val) ⇒ void
This method returns an undefined value.
Does the chosen editor exist and is it executable?
805 806 807 808 809 810 811 |
# File 'lib/xolo/admin/validate.rb', line 805 def validate_editor(val) tool = val.split(' -')[0].strip tool = Pathname.new tool raise_invalid_data_error val, Xolo::Admin::Configuration::KEYS[:editor][:invalid_msg] unless tool.executable? val end |
#validate_excluded_groups(val) ⇒ Array<String>
validate an array of jamf groups to use as exclusions. ‘none’ is also acceptable
excluded groups cannot be in the release groups or pilot groups
440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/xolo/admin/validate.rb', line 440 def validate_excluded_groups(val) val = [val] unless val.is_a? Array return [] if val.include? Xolo::NONE bad_grps = bad_jamf_groups(val) return val if bad_grps.empty? bad_grps = "No Such Groups: #{bad_grps.join(Xolo::COMMA_JOIN)}" raise_invalid_data_error bad_grps, TITLE_ATTRS[:excluded_groups][:invalid_msg] end |
#validate_expiration(val) ⇒ Integer
validate a titles expiration. Must be a non-negative integer
487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'lib/xolo/admin/validate.rb', line 487 def validate_expiration(val) val = val.to_s if val.pix_integer? val = val.to_i return Xolo::NONE if val.zero? return val if val.positive? elsif val == Xolo::NONE return val end raise_invalid_data_error val, TITLE_ATTRS[:expiration][:invalid_msg] end |
#validate_expire_paths(val) ⇒ Array<String>
validate a title expiration paths. Must one or more full paths starting with a / and containing at least one more.
507 508 509 510 511 512 513 514 515 516 517 518 519 |
# File 'lib/xolo/admin/validate.rb', line 507 def validate_expire_paths(val) val = [val] unless val.is_a? Array return [] if val.include? Xolo::NONE val.map!(&:to_s) bad_paths = [] val.each { |path| bad_paths << path unless path =~ %r{\A/\w.*/.*\z} } return val if bad_paths.empty? raise_invalid_data_error bad_paths.join(Xolo::COMMA_JOIN), TITLE_ATTRS[:expire_paths][:invalid_msg] end |
#validate_hostname(val) ⇒ void
This method returns an undefined value.
Try to fetch a known route from the given xolo server
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 |
# File 'lib/xolo/admin/validate.rb', line 745 def validate_hostname(val) if val.downcase == 'x' val = nil return end response = server_cnx(host: val).get Xolo::Admin::Connection::PING_ROUTE return val if response.body == Xolo::Admin::Connection::PING_RESPONSE raise_invalid_data_error val, Xolo::Admin::Configuration::KEYS[:hostname][:invalid_msg] rescue Faraday::ConnectionFailed raise_invalid_data_error val, Xolo::Admin::Configuration::KEYS[:hostname][:invalid_msg] rescue Faraday::SSLError raise_invalid_data_error val, 'SSL Error. Be sure to use the fully qualified hostname.' end |
#validate_internal_consistency(opts) ⇒ void
This method returns an undefined value.
Given an ostruct of options that have been individually validated, and combined with any current_opt_values as needed, check the data for internal consistency. The unset values in the ostruct should be nil. ‘none’ is used for unsetting values, in the CLI and walkthru, and is converted to nil during validation if appropriate.
836 837 838 839 840 841 842 843 844 845 846 |
# File 'lib/xolo/admin/validate.rb', line 836 def validate_internal_consistency(opts) return unless add_command? || edit_command? show_debug "Validating internal consistency for opts: #{opts.to_h}" if version_command? validate_version_consistency opts elsif title_command? validate_title_consistency opts end end |
#validate_killapps(val) ⇒ Array<Array<String>>
Returns The valid value.
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 |
# File 'lib/xolo/admin/validate.rb', line 645 def validate_killapps(val) val = [val] unless val.is_a? Array return Xolo::X if val == [Xolo::X] return Xolo::NONE if val.include? Xolo::NONE val.map!(&:to_s) # if one of the killapps is Xolo::Admin::Version::USE_TITLE_FOR_KILLAPP # convert it into the appropriate string val.map! { |ka| (ka) } val.each { |ka| validate_single_killapp(ka) } val end |
#validate_max_os(val) ⇒ Gem::Version
Returns The valid value.
631 632 633 634 635 636 637 638 639 |
# File 'lib/xolo/admin/validate.rb', line 631 def validate_max_os(val) return if val.pix_empty? || val == Xolo::NONE raise VERSION_ATTRS[:max_os][:invalid_msg] unless val =~ OS_VERSION_RE val rescue StandardError => e raise_invalid_data_error val, e.to_s end |
#validate_min_os(val) ⇒ String
Returns The valid value.
613 614 615 616 617 618 619 620 621 622 623 624 625 |
# File 'lib/xolo/admin/validate.rb', line 613 def validate_min_os(val) # inherit if needed val = current_opt_values[:min_os] if val.pix_empty? # use the default if still empty or 'none' - we get it from the server val = Xolo::Admin::Options.default_min_os if val.pix_empty? || val == Xolo::NONE raise VERSION_ATTRS[:min_os][:invalid_msg] unless val =~ OS_VERSION_RE val rescue StandardError => e raise_invalid_data_error val, e.to_s end |
#validate_min_os_and_max_os(opts) ⇒ void
This method returns an undefined value.
min_os must be <= max_os max_os must be >= min_os
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 |
# File 'lib/xolo/admin/validate.rb', line 1187 def validate_min_os_and_max_os(opts) # if no max_os, nothing to do return if opts[:max_os].pix_empty? min_os = Gem::Version.new opts[:min_os] max_os = Gem::Version.new opts[:max_os] # if things look OK, we're done return if min_os <= max_os && max_os >= min_os msg = if walkthru? 'Minimum OS must be less than or equal to Maximum OS' else '--max-os must be greater than or equal to --min-os' end raise_consistency_error msg end |
#validate_patch_source(val) ⇒ Pathname
validate a patch source name. Must exist in Jamf Pro and have available titles
324 325 326 327 328 329 |
# File 'lib/xolo/admin/validate.rb', line 324 def validate_patch_source(val) val = val.to_s return val if jamf_patch_sources_with_available_titles.include? val raise_invalid_data_error val, TITLE_ATTRS[:patch_source][:invalid_msg] end |
#validate_pilot_groups(val) ⇒ Array<String>
validate an array of jamf groups to use as pilots for testing a version before releasing it. ‘none’ is also acceptable
NOTE: we will not compare targets to exclusions - we’ll just verify that the jamf groups exist. If a group (or an individual mac) is both a pilot and an exclusion, the exclusion wins.
727 728 729 730 731 732 733 734 735 736 737 |
# File 'lib/xolo/admin/validate.rb', line 727 def validate_pilot_groups(val) val = [val] unless val.is_a? Array return [] if val.include? Xolo::NONE bad_grps = bad_jamf_groups(val) return val if bad_grps.empty? bad_grps = "No Such Groups: #{bad_grps.join(Xolo::COMMA_JOIN)}" raise_invalid_data_error bad_grps, VERSION_ATTRS[:pilot_groups][:invalid_msg] end |
#validate_pkg_to_upload(val) ⇒ Pathname
validate a path to a jamf_pkg_file. Must exist locally and be readable and have the correct ext.
571 572 573 574 575 576 577 |
# File 'lib/xolo/admin/validate.rb', line 571 def validate_pkg_to_upload(val) val = Pathname.new val.to_s.strip show_debug "Validating pkg_to_upload: '#{val}'. Checking if file? #{val.file?}, readable? #{val.readable?}, extname '#{val.extname}'" return val if val.file? && val.readable? && (Xolo::OK_PKG_EXTS.include? val.extname) raise_invalid_data_error val, VERSION_ATTRS[:pkg_to_upload][:invalid_msg] end |
#validate_publish_date(val) ⇒ Date
Returns The valid value.
598 599 600 601 602 603 604 605 606 607 |
# File 'lib/xolo/admin/validate.rb', line 598 def validate_publish_date(val) val = Time.now.to_s if val.pix_empty? val = Time.parse val.to_s # TODO: ? Ensure this date is >= the prev. version and <= the next return val raise VERSION_ATTRS[:publish_date][:invalid_msg] rescue StandardError => e raise_invalid_data_error val, e.to_s end |
#validate_publisher(val) ⇒ String
validate a title publisher. Must be 3+ chars long
272 273 274 275 276 277 |
# File 'lib/xolo/admin/validate.rb', line 272 def validate_publisher(val) val = val.to_s.strip return val if val.length >= 3 raise_invalid_data_error val, TITLE_ATTRS[:publisher][:invalid_msg] end |
#validate_pw(val) ⇒ void
This method returns an undefined value.
Password (and username) will be validated via the server
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 |
# File 'lib/xolo/admin/validate.rb', line 767 def validate_pw(val) if val.downcase == 'x' val = Xolo::BLANK return end pw = val hostname = walkthru? ? walkthru_cmd_opts[:hostname] : cli_cmd_opts[:hostname] admin = walkthru? ? walkthru_cmd_opts[:admin] : cli_cmd_opts[:admin] no_gui = walkthru? ? walkthru_cmd_opts[:no_gui] : cli_cmd_opts[:no_gui] raise Xolo::MissingDataError, 'hostname must be set before password' if hostname.pix_empty? raise Xolo::MissingDataError, 'admin username must be set before password' if admin.pix_empty? pw = config.data_from_command_file_or_string(pw, enforce_secure_mode: true) if no_gui payload = { admin: admin, password: pw }.to_json begin server_cnx(host: hostname).post Xolo::Admin::Connection::LOGIN_ROUTE, payload rescue Faraday::UnauthorizedError raise_invalid_data_error 'User/Password', 'Username or Password is incorrect' end # return the value for storage in the config file if no_gui is true return val if no_gui store_pw admin, val # return a placeholder value saying that the password is stored in the keychain Xolo::Admin::Configuration::CREDENTIALS_IN_KEYCHAIN end |
#validate_release_groups(val) ⇒ Array<String>
validate an array of jamf group names to use as targets when released. ‘all’, or ‘none’ are also acceptable
These groups cannot be in the excluded_group - but that validation happens later in the consistency checks via validate_scope_targets_and_exclusions
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/xolo/admin/validate.rb', line 401 def validate_release_groups(val) val = [val] unless val.is_a? Array if val.include? Xolo::NONE return [] elsif val.include? Xolo::TARGET_ALL validate_release_to_all_allowed return [Xolo::TARGET_ALL] end # non-existant jamf groups bad_grps = bad_jamf_groups(val) return val if bad_grps.empty? bad_grps = "No Such Groups: #{bad_grps.join(Xolo::COMMA_JOIN)}" raise_invalid_data_error bad_grps, TITLE_ATTRS[:release_groups][:invalid_msg] end |
#validate_release_to_all_allowed ⇒ void
This method returns an undefined value.
check if the current admin is allowed to set a title’s release groups to ‘all’
423 424 425 426 427 428 |
# File 'lib/xolo/admin/validate.rb', line 423 def validate_release_to_all_allowed svr_resp = Xolo::Admin::Title.release_to_all_allowed?(server_cnx) return if svr_resp[:allowed] raise Xolo::InvalidDataError, svr_resp[:msg] end |
#validate_scope_targets_and_exclusions(opts) ⇒ void
This method returns an undefined value.
groups that will be scope targets (pilot or release) cannot also be in the exclusions.
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 |
# File 'lib/xolo/admin/validate.rb', line 1070 def validate_scope_targets_and_exclusions(opts) # require 'pp' # puts 'Opts Are:' # pp opts.to_h # pp caller if title_command? excls = opts.excluded_groups tgts = opts.release_groups tgt_type = :release elsif version_command? @title_for_version_validation ||= Xolo::Admin::Title.fetch cli_cmd.title, server_cnx excls = @title_for_version_validation.excluded_groups tgts = opts.pilot_groups tgt_type = :pilot else excls = nil tgts = nil end return unless excls && tgts in_both = excls & tgts return if in_both.empty? raise_consistency_error "These groups are in both #{tgt_type}_groups and the title's excluded_groups: '#{in_both.join "', '"}'" end |
#validate_self_service_category(val) ⇒ Boolean, String
validate a self_service_category. Must exist in Jamf Pro
539 540 541 542 543 544 545 546 |
# File 'lib/xolo/admin/validate.rb', line 539 def validate_self_service_category(val) val = val.to_s # TODO: implement the check via the xolo server return val # if category exists raise_invalid_data_error val, TITLE_ATTRS[:self_service_category][:invalid_msg] end |
#validate_self_service_icon(val) ⇒ Pathname
validate a path to a self_service_icon. Must exist locally and be readable
554 555 556 557 558 559 560 561 562 |
# File 'lib/xolo/admin/validate.rb', line 554 def validate_self_service_icon(val) val = Pathname.new val.to_s.strip if val.file? && val.readable? mimetype = `/usr/bin/file -b -I #{Shellwords.escape val.to_s}`.split(';').first return val if SSVC_ICON_MIME_TYPES.include? mimetype end raise_invalid_data_error val, TITLE_ATTRS[:self_service_icon][:invalid_msg] end |
#validate_single_killapp(ka) ⇒ String
Validate an individual killapp string
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 |
# File 'lib/xolo/admin/validate.rb', line 697 def validate_single_killapp(ka) name, bundle_id = ka.split(Xolo::SEMICOLON_SEP_RE) raise_invalid_data_error name, 'App name required before semicolon' unless name raise_invalid_data_error name, 'App Bundle ID required after semicolon' unless bundle_id # app name must end with .app. Use the err messge from Title/app_name raise_invalid_data_error name, TITLE_ATTRS[:app_name][:invalid_msg] unless name.end_with?(Xolo::DOTAPP) # app bundle id must contain a dot. Use the err messge from Title/app_bundle_id unless bundle_id&.include?(Xolo::DOT) raise_invalid_data_error bundle_id, TITLE_ATTRS[:app_bundle_id][:invalid_msg] end ka end |
#validate_subbed_v_managed(opts) ⇒ Object
Complain about using options not meant for the chosen title type
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 |
# File 'lib/xolo/admin/validate.rb', line 920 def validate_subbed_v_managed(opts) bad_opts = [] ttl_type = current_title_type(opts) Xolo::Admin::Title.cli_opts.each do |key, deets| next unless opts[key] next unless deets[:title_type] next if deets[:title_type] == ttl_type # ignore values not used for this command type next if add_command? && deets[:edit_only] next if edit_command? && deets[:add_only] # ignore values not given on the commandline next if !walkthru? && !opts["#{key}_given"] # display_name is stored for subscribed titles, but only editable for managed next if key == :display_name && ttl_type == Xolo::SUBSCRIBED bad_opts << (walkthru? ? deets[:label] : "--#{key.to_s.gsub('_', '-')}") end return if bad_opts.empty? raise_consistency_error "Cannot be used with a #{ttl_type} title: #{bad_opts.join(', ')}" end |
#validate_title(val) ⇒ String
validate a Xolo title. Must be 2+ chars long, only lowercase alpha-numerics & dashes
Must also exist or not exist, depending on the command we are running
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/xolo/admin/validate.rb', line 197 def validate_title(val) val = val.to_s.strip title_exists = Xolo::Admin::Title.exist?(val, server_cnx) # adding... cant already exist, and name must be OK if cli_cmd.command == Xolo::Admin::Options::ADD_TITLE_CMD err = if title_exists "title '#{val}' already exists in Xolo" elsif val !~ /\A[a-z0-9-][a-z0-9-]+\z/ TITLE_ATTRS[:title][:invalid_msg] else return val end # a command where it must exist elsif Xolo::Admin::Options::MUST_EXIST_COMMANDS.include?(cli_cmd.command) return val if title_exists err = "title '#{val}' doesn't exist in Xolo" # any other command else return val end raise_invalid_data_error val, err end |
#validate_title_consistency(opts) ⇒ void
This method returns an undefined value.
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 |
# File 'lib/xolo/admin/validate.rb', line 867 def validate_title_consistency(opts) # if we are just listing the versions, nothing to do return if cli_cmd.command == Xolo::Admin::Options::LIST_VERSIONS_CMD # order of these matters in some places #### Sub vs Managed # complain about using options not meant for the chosen type # e.g. can't use display name for subscribed titles validate_subbed_v_managed(opts) #### Subscribed Title Validations #### # if subscribed, both patch_source and title_id must be given validate_title_consistency_patch_source_and_title_id(opts) # if subscribed, the title_id must exist in the patch_source validate_title_consistency_title_id_exists(opts) #### Managed Title Validations #### # if managed, must provide display name and publisher, but not allowed if subscribing validate_title_consistency_display_name_and_publisher(opts) # if managed must use version_script, or app validate_title_consistency_app_or_script(opts) # if managed, can't have both app and version_script validate_title_consistency_app_and_script(opts) # if managed and using app_name & bundle id, must have both validate_title_consistency_app_name_and_id(opts) ### General Validations ### # scope targets and exclusions can't overlap validate_scope_targets_and_exclusions(opts) # if uninstall script, can't have uninstall ids, and vice versa validate_title_consistency_uninstall(opts) # if expiration > 0, must have at least one expiration path validate_title_consistency_expire_paths(opts) # if targeting 'all' can't be in self-service validate_title_consistency_no_all_in_ssvc(opts) # if self-service category given, must be in self-service validate_title_consistency_ssvc_needs_category(opts) end |
#validate_title_consistency_app_and_script(opts) ⇒ void
This method returns an undefined value.
if app_name or app_bundle_id is given, can’t use –version-script, and vice versa
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 |
# File 'lib/xolo/admin/validate.rb', line 1029 def validate_title_consistency_app_and_script(opts) return if current_title_type(opts) == Xolo::SUBSCRIBED return unless opts[:version_script] && (opts[:app_bundle_id] || opts[:app_name]) msg = if walkthru? 'Version Script cannot be used with App Name & App Bundle ID' else '--version-script cannot be used with --app-name & --app-bundle-id' end raise_consistency_error msg end |
#validate_title_consistency_app_name_and_id(opts) ⇒ void
This method returns an undefined value.
If using app_name and bundle id, both must be given
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 |
# File 'lib/xolo/admin/validate.rb', line 1049 def validate_title_consistency_app_name_and_id(opts) return if current_title_type(opts) == Xolo::SUBSCRIBED return if opts[:version_script] return if opts[:app_name] && opts[:app_bundle_id] msg = if walkthru? 'App Name & App Bundle ID must both be given if either is.' else '--app-name & --app-bundle-id must both be given if either is.' end raise_consistency_error msg end |
#validate_title_consistency_app_or_script(opts) ⇒ void
This method returns an undefined value.
if managed must use version_script, or app
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 |
# File 'lib/xolo/admin/validate.rb', line 1009 def validate_title_consistency_app_or_script(opts) return if current_title_type(opts) == Xolo::SUBSCRIBED return if opts[:version_script] || (opts[:app_bundle_id] && opts[:app_name]) msg = if walkthru? 'Managed titles must provide Version Script OR App Name & App Bundle ID' else 'Managed titles must provide --version-script OR --app-name & --app-bundle-id' end raise_consistency_error msg end |
#validate_title_consistency_display_name_and_publisher(opts) ⇒ Object
If managing this title, display_name and publisher must be given
990 991 992 993 994 995 996 997 998 999 1000 1001 |
# File 'lib/xolo/admin/validate.rb', line 990 def validate_title_consistency_display_name_and_publisher(opts) return if current_title_type(opts) == Xolo::SUBSCRIBED return if opts[:display_name] && opts[:publisher] msg = if walkthru? 'Display Name and Publisher must both be given for a managed title.' else '--display-name and --publisher must both be given for a managed title.' end raise_consistency_error msg end |
#validate_title_consistency_expire_paths(opts) ⇒ void
This method returns an undefined value.
if expiration is > 0, there must be at least one expiration path
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 |
# File 'lib/xolo/admin/validate.rb', line 1103 def validate_title_consistency_expire_paths(opts) return unless opts.expiration.to_i.positive? return unless opts.expire_paths.pix_empty? msg = if walkthru? 'At least one Expiration Path must be given if Expiration is > 0.' else 'At least one --expiration-path must be given if --expiration is > 0' end raise_consistency_error msg end |
#validate_title_consistency_no_all_in_ssvc(opts) ⇒ void
This method returns an undefined value.
if target_group is all, can’t be in self service
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 |
# File 'lib/xolo/admin/validate.rb', line 1149 def validate_title_consistency_no_all_in_ssvc(opts) return unless opts[:release_groups].to_a.include?(Xolo::TARGET_ALL) && opts[:self_service] msg = if walkthru? "Cannot be in Self Service when Target Group is '#{Xolo::TARGET_ALL}'" else "--self-service cannot be used when --release-groups contains '#{Xolo::TARGET_ALL}'" end raise_consistency_error msg end |
#validate_title_consistency_patch_source_and_title_id(opts) ⇒ void
This method returns an undefined value.
If subscribing to a title both patch_source and title_id must be given
952 953 954 955 956 957 958 959 960 961 962 963 |
# File 'lib/xolo/admin/validate.rb', line 952 def validate_title_consistency_patch_source_and_title_id(opts) return if current_title_type(opts) == Xolo::MANAGED return if opts[:title_id] && opts[:patch_source] msg = if walkthru? 'To subscribe to a title, both Patch Source and Title ID must be given.' else 'To subscribe to a title, both --patch-source and --title-id must be given.' end raise_consistency_error msg end |
#validate_title_consistency_ssvc_needs_category(opts) ⇒ void
This method returns an undefined value.
if in self service, a category must be assigned
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 |
# File 'lib/xolo/admin/validate.rb', line 1167 def validate_title_consistency_ssvc_needs_category(opts) return unless opts[:self_service] return if opts[:self_service_category] msg = if walkthru? 'A Self Service Category must be given if Self Service is true.' else 'A --self-service-category must be provided when using --self-service' end raise_consistency_error msg end |
#validate_title_consistency_title_id_exists(opts) ⇒ void
This method returns an undefined value.
If subscribing to a title, the title_id must exist in the given patch_source
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 |
# File 'lib/xolo/admin/validate.rb', line 971 def validate_title_consistency_title_id_exists(opts) return unless cli_cmd.command == Xolo::Admin::Options::ADD_TITLE_CMD return if current_title_type(opts) == Xolo::MANAGED return unless opts[:title_id] && opts[:patch_source] return if jamf_available_titles.any? do |t| t[:name_id] == opts[:title_id] && t[:source_name] == opts[:patch_source] end msg = if walkthru? 'The given Title ID does not exist in the given Patch Source.' else 'The given --title-id does not exist in the given --patch-source.' end raise_consistency_error msg end |
#validate_title_consistency_uninstall(opts) ⇒ void
This method returns an undefined value.
if uninstall script, cant have uninstall ids and vice versa
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 |
# File 'lib/xolo/admin/validate.rb', line 1123 def validate_title_consistency_uninstall(opts) # walktrhu will have already validated this return if walkthru? if opts.uninstall_script_given && opts.uninstall_ids_given # if uninstall_script is given, uninstall_ids must be unset # and vice versa # raise an error if both are given raise_consistency_error '--uninstall-script cannot be used with --uninstall-ids' elsif opts.uninstall_script_given opts.uninstall_ids = nil elsif opts.uninstall_ids_given opts.uninstall_script_given = nil end end |
#validate_title_desc(val) ⇒ Boolean, String
validate a title description. Must be 20+ chars long
259 260 261 262 263 264 |
# File 'lib/xolo/admin/validate.rb', line 259 def validate_title_desc(val) val = val.to_s.strip return val if val.length >= Xolo::Core::BaseClasses::Title::MIN_TITLE_DESC_LENGTH raise_invalid_data_error val, TITLE_ATTRS[:description][:invalid_msg] end |
#validate_title_display_name(val) ⇒ String
validate a title display-name. Must be 3+ chars long
246 247 248 249 250 251 |
# File 'lib/xolo/admin/validate.rb', line 246 def validate_title_display_name(val) val = val.to_s.strip return val if val =~ /\A\S.+\S\z/ raise_invalid_data_error val, TITLE_ATTRS[:display_name][:invalid_msg] end |
#validate_title_id(val) ⇒ Pathname
validate a patch title id.
337 338 339 340 341 342 |
# File 'lib/xolo/admin/validate.rb', line 337 def validate_title_id(val) val = val.to_s return val if jamf_available_titles.any? { |t| t[:name_id] == val } raise_invalid_data_error val, TITLE_ATTRS[:title_id][:invalid_msg] end |
#validate_type(val) ⇒ String
validate the title type
233 234 235 236 237 238 |
# File 'lib/xolo/admin/validate.rb', line 233 def validate_type(val) val = val.to_s.strip.downcase.to_sym return val if Xolo::TITLE_TYPES.include? val raise_invalid_data_error val, TITLE_ATTRS[:type][:invalid_msg] end |
#validate_uninstall_ids(val) ⇒ Array<String>
validate a title uninstall ids:
-
an array of package identifiers
OR
-
‘none’ to unset the value
TODO: Consistency with expiration.
380 381 382 383 384 385 386 387 388 |
# File 'lib/xolo/admin/validate.rb', line 380 def validate_uninstall_ids(val) val = [val] unless val.is_a? Array return Xolo::X if val == [Xolo::X] return [] if val.include? Xolo::NONE return val raise_invalid_data_error val, TITLE_ATTRS[:uninstall_ids][:invalid_msg] end |
#validate_uninstall_script(val) ⇒ String
validate a title uninstall script:
-
a path to a script which must start with ‘#!’
OR
-
‘none’ to unset the value
TODO: Consistency with expiration.
355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/xolo/admin/validate.rb', line 355 def validate_uninstall_script(val) val = val.to_s.strip return if val == Xolo::NONE script_file = Pathname.new(val). if script_file.readable? script = script_file.read return script_file.to_s if script.start_with? '#!' end raise_invalid_data_error val, TITLE_ATTRS[:uninstall_script][:invalid_msg] end |
#validate_version(val) ⇒ String
TODO: validate a Xolo Version. Must be 2+ chars long, only lowercase alpha-numerics & dashes
590 591 592 |
# File 'lib/xolo/admin/validate.rb', line 590 def validate_version(val) val end |
#validate_version_consistency(opts) ⇒ void
This method returns an undefined value.
852 853 854 855 |
# File 'lib/xolo/admin/validate.rb', line 852 def validate_version_consistency(opts) validate_scope_targets_and_exclusions(opts) validate_min_os_and_max_os(opts) end |
#validate_version_script(val) ⇒ Pathname
validate a title version script. Must start with ‘#!’
311 312 313 314 315 316 |
# File 'lib/xolo/admin/validate.rb', line 311 def validate_version_script(val) val = Pathname.new val.to_s.strip return val if val.file? && val.readable? && val.read.start_with?('#!') raise_invalid_data_error val, TITLE_ATTRS[:version_script][:invalid_msg] end |