From 104fd09244ee04be2c516f3f1dddc570053f0d68 Mon Sep 17 00:00:00 2001 From: tkling Date: Fri, 30 Oct 2015 11:36:37 -0400 Subject: [PATCH 1/8] wrapping file reads in existence checks --- lib/jumpcloud.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/jumpcloud.rb b/lib/jumpcloud.rb index 8211703..464f831 100755 --- a/lib/jumpcloud.rb +++ b/lib/jumpcloud.rb @@ -25,7 +25,9 @@ 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)) end def self.get_key_from_config @@ -34,10 +36,19 @@ def self.get_key_from_config 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' + File.exists?(file) ? File.open(file) : fail(file_not_found_text(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["tags"] = tags From 26fcd1567736fd710f57098af6335ca57c907fe7 Mon Sep 17 00:00:00 2001 From: tkling Date: Fri, 30 Oct 2015 11:39:07 -0400 Subject: [PATCH 2/8] removing parens from parameterless calls --- lib/jumpcloud.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/jumpcloud.rb b/lib/jumpcloud.rb index 464f831..5ed4ae8 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 @@ -50,24 +50,24 @@ def self.file_not_found_text(path) 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) From 4f67878e27eb7167997214986939ba239f66d5cb Mon Sep 17 00:00:00 2001 From: tkling Date: Fri, 30 Oct 2015 11:39:50 -0400 Subject: [PATCH 3/8] unused variable --- lib/jumpcloud.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/jumpcloud.rb b/lib/jumpcloud.rb index 5ed4ae8..53e7f71 100755 --- a/lib/jumpcloud.rb +++ b/lib/jumpcloud.rb @@ -87,7 +87,6 @@ def self.get_system_data() 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) From d559baab9dfab41b991418178beda85e96f09865 Mon Sep 17 00:00:00 2001 From: tkling Date: Fri, 30 Oct 2015 11:44:30 -0400 Subject: [PATCH 4/8] return keyword not needed, removed unused return vars --- lib/jumpcloud.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/jumpcloud.rb b/lib/jumpcloud.rb index 53e7f71..94a7607 100755 --- a/lib/jumpcloud.rb +++ b/lib/jumpcloud.rb @@ -79,7 +79,7 @@ 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() @@ -95,7 +95,7 @@ def self.get_system_data() request["accept"] = "application/json" response = http.request(request) - return JSON.parse(response.body) + JSON.parse(response.body) end def self.send_to_server(data) @@ -111,7 +111,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 From aa231ef4a02dc717ea4401e22a0a2c638b0f58e2 Mon Sep 17 00:00:00 2001 From: tkling Date: Fri, 30 Oct 2015 15:52:46 -0400 Subject: [PATCH 5/8] removing unneeded parens --- lib/jumpcloud.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/jumpcloud.rb b/lib/jumpcloud.rb index 94a7607..747abf7 100755 --- a/lib/jumpcloud.rb +++ b/lib/jumpcloud.rb @@ -42,7 +42,8 @@ def self.create_signature(verb, date, system_key) def self.client_key file = '/opt/jc/client.key' - File.exists?(file) ? File.open(file) : fail(file_not_found_text(file)) + fail file_not_found_text(file) unless File.exists?(file) + File.open(file) end def self.file_not_found_text(path) @@ -82,7 +83,7 @@ def self.delete_system 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) From dd089060267242f49e8a3284c43db537d5092952 Mon Sep 17 00:00:00 2001 From: tkling Date: Fri, 30 Oct 2015 16:27:21 -0400 Subject: [PATCH 6/8] handling systemKey not found and JSON parse errors --- lib/jumpcloud.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/jumpcloud.rb b/lib/jumpcloud.rb index 747abf7..c56cd01 100755 --- a/lib/jumpcloud.rb +++ b/lib/jumpcloud.rb @@ -28,10 +28,13 @@ def self.parse_config 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"] + key.nil? ? fail('systemKey not found in configuration!') : key end def self.create_signature(verb, date, system_key) @@ -94,7 +97,6 @@ def self.get_system_data 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) JSON.parse(response.body) end From 70411e9963c0978c93bb26c24cc3a40a9b6ac4d6 Mon Sep 17 00:00:00 2001 From: tkling Date: Fri, 30 Oct 2015 16:49:47 -0400 Subject: [PATCH 7/8] 0.4.0 => 0.4.1 --- jumpcloud.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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"] From c307f0a03b483083246dc9f371beab3340621230 Mon Sep 17 00:00:00 2001 From: tkling Date: Fri, 30 Oct 2015 16:50:08 -0400 Subject: [PATCH 8/8] not using ternary operator --- lib/jumpcloud.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/jumpcloud.rb b/lib/jumpcloud.rb index c56cd01..f7276d5 100755 --- a/lib/jumpcloud.rb +++ b/lib/jumpcloud.rb @@ -34,7 +34,8 @@ def self.parse_config def self.get_key_from_config key = parse_config["systemKey"] - key.nil? ? fail('systemKey not found in configuration!') : key + fail('systemKey not found in configuration!') if key.nil? + key end def self.create_signature(verb, date, system_key)