diff --git a/jumpcloud.gemspec b/jumpcloud.gemspec index 92ca304..c09c1f4 100644 --- a/jumpcloud.gemspec +++ b/jumpcloud.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'jumpcloud' - s.version = '0.4.0' + s.version = '0.4.1' s.licenses = ['Mozilla Public License v2.0 (https://www.mozilla.org/MPL/2.0/)'] s.summary = "Basic JumpCloud system configuration via api" s.authors = ["Topher Marie"] diff --git a/lib/jumpcloud.rb b/lib/jumpcloud.rb index 8211703..f7276d5 100755 --- a/lib/jumpcloud.rb +++ b/lib/jumpcloud.rb @@ -7,7 +7,7 @@ class JumpCloud attr_accessor :settings - def initialize(args=self.class.get_system_data()) + def initialize(args=self.class.get_system_data) @settings = args end @@ -25,38 +25,54 @@ def self.get_date end def self.parse_config - JSON.parse( IO.read("/opt/jc/jcagent.conf") ) + file = '/opt/jc/jcagent.conf' + fail file_not_found_text(file) unless File.exists?(file) + JSON.parse(IO.read(file)) + rescue JSON::ParserError + raise "Problem parsing #{file} as JSON; it is valid JSON?" end def self.get_key_from_config - parse_config["systemKey"] + key = parse_config["systemKey"] + fail('systemKey not found in configuration!') if key.nil? + key end def self.create_signature(verb, date, system_key) signed_string = "#{verb} /api/systems/#{system_key} HTTP/1.1\ndate: #{date}" - key = OpenSSL::PKey::RSA.new(File.open("/opt/jc/client.key")) + key = OpenSSL::PKey::RSA.new(client_key) Base64.strict_encode64(key.sign(OpenSSL::Digest::SHA256.new, signed_string)) end + def self.client_key + file = '/opt/jc/client.key' + fail file_not_found_text(file) unless File.exists?(file) + File.open(file) + end + + def self.file_not_found_text(path) + "#{path} not found, is the JumpCloud agent installed?" + end + def self.set_system_tags(*tags) - system_data = get_system_data() + system_data = get_system_data system_data["tags"] = tags send_to_server(system_data) end def self.set_system_name(system_name) - system_data = get_system_data() + system_data = get_system_data system_data["displayName"] = system_name send_to_server(system_data) end - def self.set_sshPassEnabled() - system_data = get_system_data() + def self.set_sshPassEnabled + system_data = get_system_data system_data["allowSshPasswordAuthentication"] = true send_to_server(system_data) end - def self.delete_system() + def self.delete_system date = get_date system_key = get_key_from_config signature = create_signature("DELETE", date, system_key) @@ -68,24 +84,22 @@ def self.delete_system() request["Date"] = "#{date}" request["accept"] = "application/json" request["Content-Type"] = "application/json" - response = http.request(request) + http.request(request) end - def self.get_system_data() + def self.get_system_data date = get_date system_key = get_key_from_config signature = create_signature("GET", date, system_key) uri = URI.parse("https://console.jumpcloud.com/api/systems/#{system_key}") - request = Net::HTTP.new(uri.host, uri.port) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Get.new(uri.request_uri) request["Authorization"] = "Signature keyId=\"system/#{system_key}\",headers=\"request-line date\",algorithm=\"rsa-sha256\",signature=\"#{signature}\"" request["Date"] = "#{date}" request["accept"] = "application/json" - response = http.request(request) - return JSON.parse(response.body) + JSON.parse(response.body) end def self.send_to_server(data) @@ -101,7 +115,7 @@ def self.send_to_server(data) request["accept"] = "application/json" request["Content-Type"] = "application/json" request.body = JSON.generate(data) - response = http.request(request) + http.request(request) end end