source/code/plugins/agent_maintenance_script.rb
# Renew certificates for agent/workspace connection
Renew certificates for agent/workspace connection
Renew certificates for agent / workspace connection
def renew_certs
      if @load_config_return_code != 0
        log_error("Error loading configuration from #{@omsadmin_conf_path}")
        return @load_config_return_code
      elsif @WORKSPACE_ID.nil? or @AGENT_GUID.nil? or @WORKSPACE_ID.empty? or @AGENT_GUID.empty?
        log_error("Missing required field from configuration file: #{@omsadmin_conf_path}")
        return MISSING_CONFIG
      elsif @CERTIFICATE_UPDATE_ENDPOINT.nil? or @CERTIFICATE_UPDATE_ENDPOINT.empty?
        log_error("Missing CERTIFICATE_UPDATE_ENDPOINT from configuration")
        return MISSING_CONFIG
      elsif !file_exists_nonempty(@cert_path) or !file_exists_nonempty(@key_path)
        log_error("No certificates exist; cannot renew certificates")
        return MISSING_CERTS
      end
      log_info("Renewing the certificates")
      cert_old = OpenSSL::X509::Certificate.new(File.open(@cert_path))
      key_old = OpenSSL::PKey::RSA.new(File.open(@key_path))
      generated = generate_certs(@WORKSPACE_ID, @AGENT_GUID)
      if generated != 0
        return generated
      end
      renew_certs_req = AgentRenewCertificateRequest.new
      renew_certs_req.NewCertificate = get_cert_server(@cert_path)
      renew_certs_xml = "<?xml version=\"1.0\"?>\n"
      renew_certs_xml.concat(Gyoku.xml({ "CertificateUpdateRequest" => {:content! => obj_to_hash(renew_certs_req), \
:'@xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", :'@xmlns:xsd' => "http://www.w3.org/2001/XMLSchema", \
:@xmlns => "http://schemas.microsoft.com/WorkloadMonitoring/HealthServiceProtocol/2014/09/"}}))
      req,http = form_post_request_and_http(headers = {}, @CERTIFICATE_UPDATE_ENDPOINT,
                     renew_certs_xml, cert_old, key_old)
      log_info("Generated renew certificates request:\n#{req.body}") if @verbose
      begin
        res = nil
        res = http.start { |http_each| http.request(req) }
      rescue => e
        log_error("Error renewing certificate: #{e.message}")
        restore_old_certs(cert_old, key_old)
        return ERROR_SENDING_HTTP
      end
      if !res.nil?
        log_info("Renew certificates response code: #{res.code}") if @verbose
        if res.code == "200"
          hb_return = heartbeat
          if hb_return == 0
            log_info("Certificates successfully renewed")
          else
            log_error("Error renewing certificate. Restoring old certs.")
            restore_old_certs(cert_old, key_old)
            return hb_return
          end
        else
          log_error("Error renewing certificate. HTTP code #{res.code}")
          restore_old_certs(cert_old, key_old)
          return HTTP_NON_200
        end
      else
        log_error("Error renewing certificate. No HTTP code")
        return ERROR_SENDING_HTTP
      end
      return 0
    end
def renew_certs if @load_config_return_code != 0 log_error ( "Error loading configuration from #{@omsadmin_conf_path}" ) return @load_config_return_code elsif @WORKSPACE_ID . nil? or @AGENT_GUID . nil? or @WORKSPACE_ID . empty? or @AGENT_GUID . empty? log_error ( "Missing required field from configuration file: #{@omsadmin_conf_path}" ) return MISSING_CONFIG elsif @CERTIFICATE_UPDATE_ENDPOINT . nil? or @CERTIFICATE_UPDATE_ENDPOINT . empty? log_error ( "Missing CERTIFICATE_UPDATE_ENDPOINT from configuration" ) return MISSING_CONFIG elsif ! file_exists_nonempty ( @cert_path ) or ! file_exists_nonempty ( @key_path ) log_error ( "No certificates exist; cannot renew certificates" ) return MISSING_CERTS end log_info ( "Renewing the certificates" ) cert_old = OpenSSL :: X509 :: Certificate . new ( File . open ( @cert_path ) ) key_old = OpenSSL :: PKey :: RSA . new ( File . open ( @key_path ) ) generated = generate_certs ( @WORKSPACE_ID , @AGENT_GUID ) if generated != 0 return generated end renew_certs_req = AgentRenewCertificateRequest . new renew_certs_req . NewCertificate = get_cert_server ( @cert_path ) renew_certs_xml = "<?xml version=\"1.0\"?>\n" renew_certs_xml . concat ( Gyoku . xml ( { "CertificateUpdateRequest" => { :content! => obj_to_hash ( renew_certs_req ) , :' @xmlns:xsi ' => "http://www.w3.org/2001/XMLSchema-instance" , :' @xmlns:xsd ' => "http://www.w3.org/2001/XMLSchema" , :@xmlns => "http://schemas.microsoft.com/WorkloadMonitoring/HealthServiceProtocol/2014/09/" } } ) ) req , http = form_post_request_and_http ( headers = { } , @CERTIFICATE_UPDATE_ENDPOINT , renew_certs_xml , cert_old , key_old ) log_info ( "Generated renew certificates request:\n#{req.body}" ) if @verbose begin res = nil res = http . start { | http_each | http . request ( req ) } rescue => e log_error ( "Error renewing certificate: #{e.message}" ) restore_old_certs ( cert_old , key_old ) return ERROR_SENDING_HTTP end if ! res . nil? log_info ( "Renew certificates response code: #{res.code}" ) if @verbose if res . code == "200" hb_return = heartbeat if hb_return == 0 log_info ( "Certificates successfully renewed" ) else log_error ( "Error renewing certificate. Restoring old certs." ) restore_old_certs ( cert_old , key_old ) return hb_return end else log_error ( "Error renewing certificate. HTTP code #{res.code}" ) restore_old_certs ( cert_old , key_old ) return HTTP_NON_200 end else log_error ( "Error renewing certificate. No HTTP code" ) return ERROR_SENDING_HTTP end return 0 end
Renew certificates for agent/workspace connection
Renew certificates for agent / workspace connection
# Check necessary inputs # Save old certs # Form POST request # Submit request # Do one heartbeat for the server to acknowledge the change