Last active
October 10, 2023 14:42
-
-
Save ITJamie/4937de9139c682c02c34ff2d17051d58 to your computer and use it in GitHub Desktop.
Ruby - Logicmonitor Rest API - auth / signing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rest-client' #rubygem | |
require 'date' | |
require 'openssl' | |
#warning. the code from below has come from a class. Ive cleaned it up but there may be some errors. This is a GIST after all | |
# Instance variables | |
@LM_accountname = 'lM_accountname' | |
@LM_access_id = 'lM_access_id' | |
@LM_access_key = 'lM_access_key' | |
@URL = "https://#{@LM_accountname}.logicmonitor.com/santaba/rest" | |
RestClient.add_before_execution_proc do |request, params| | |
##Lets check to see if the URL is a logicmonitor rest URL. If so we should sign the request! | |
if request.uri.to_s.include? ".logicmonitor.com/santaba/rest" | |
if defined?params[:payload] | |
#puts params[:payload] | |
datastring=params[:payload] | |
else | |
datastring='' | |
end | |
#puts params[:payload] | |
urlwithoutparams=URI.parse(request.path).path | |
pathsplit=urlwithoutparams.split('rest') #need to split the request path so that we have something like /service/services | |
#puts pathsplit | |
resourcepath=pathsplit[1] #see above comment | |
puts "RestClient-Request: Using Logicmonitor Rest API Signing Code for `#{request.method}` request on endpoint: `#{resourcepath}` of instance: `#{@LM_accountname}`" | |
timestamp = DateTime.now.strftime('%Q') #epoch date timestamp. | |
requestVars = request.method + "#{timestamp}#{datastring}#{resourcepath}" | |
signature = Base64.strict_encode64( | |
OpenSSL::HMAC.hexdigest( | |
OpenSSL::Digest.new('sha256'), | |
@LM_access_key, | |
requestVars | |
) | |
) | |
# The signature is added as HTTP header to the request | |
#puts "RestClient-Request: Domain contains '.logicmonitor.com/santaba/rest', Signing request with HMAC-SHA256 LMv1 Auth" | |
request['Authorization'] = "LMv1 #{@LM_access_id}:#{signature}:#{timestamp}" | |
end | |
end | |
######################### | |
######################### | |
######################### | |
######################### | |
temp_url = @URL + '/service/services' | |
response = RestClient.get(temp_url) | |
# Printing API server response | |
puts "\nServer response: #{response}" | |
######################### | |
puts "Rest Post API Example with LogicMonitor" | |
temp_url = @URL + '/service/services' | |
payload = '{"name":"newPingService","type":"ping","stopMonitoring":false,"disableAlerting":true,"pollingInterval":3,"host":"www.google.com"}' | |
# Executing HTTP request | |
response = RestClient.post( temp_url, payload, :content_type => :json,) | |
# Printing API server response | |
puts "\nServer response: #{response}" | |
######################### | |
# now you can do requests easily. EG | |
collectorid="1" | |
downloadLocation = "/tmp/" | |
puts 'attempting to download logicmonitor agent. This may take some time. Collector ID: '+collectorID | |
temp_url = @URL + '/setting/collectors/'+collectorID+'/installers/Linux64' | |
response = RestClient.get( temp_url) | |
if response.code == 200 | |
puts "Collector Retrieved, Saving file." | |
filelocation = downloadLocation+'/collectorInstaller.bin' | |
File.open(filelocation, 'w') { |file| file.write(response.body) } | |
puts "Installer file should now exist: "+filelocation.to_s | |
else | |
errormessage="Abort: Failure: Critical: Unable to get collector installer from LogicMonitor. API Error Code: "+response.code.to_s | |
abort(errormessage) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment