<commit_before>module Zensu
  module Server
    class Keymaster < RPC::Responder
      include RPC::SSL
      include Persistence

      #TODO add loop to check for updated key

      def generate_response(request)
        if valid_certificate?(request.cert)
          result = { 
            'cert' => Zensu.settings.ssl.certificate, 
            'cipher' => Zensu.settings.ssl.cipher,
            'shared_key' => public_encrypt(request.cert, shared_key) 
          }
          response = RPC::Response.new(result, nil, request.id)
        else
          response = RPC::Response.new(nil, "Invalid certificate", request.id)
        end
        response.tap do |r|
          r.plaintext = true
        end
      end

      #TODO this shouldn't happen here, it's probably not thread safe.
      def shared_key
        persister.setnx(shared_key_key, generate_shared_key(cipher.key_len))
        @shared_key = persister.get(shared_key_key)
        Zensu.settings.ssl.shared_key = @shared_key<commit_msg>more consistent error format from keymaster<commit_after>module Zensu
  module Server
    class Keymaster < RPC::Responder
      include RPC::SSL
      include Persistence

      #TODO add loop to check for updated key

      def generate_response(request)
        if valid_certificate?(request.cert)
          result = { 
            'cert' => Zensu.settings.ssl.certificate, 
            'cipher' => Zensu.settings.ssl.cipher,
            'shared_key' => public_encrypt(request.cert, shared_key) 
          }
          response = RPC::Response.new(result, nil, request.id)
        else
          response = RPC::Response.new(nil, "unauthorized", request.id)
        end
        response.tap do |r|
          r.plaintext = true
        end
      end

      #TODO this shouldn't happen here, it's probably not thread safe.
      def shared_key
        persister.setnx(shared_key_key, generate_shared_key(cipher.key_len))
        @shared_key = persister.get(shared_key_key)
        Zensu.settings.ssl.shared_key = @shared_key
{'fraction_non_alphanumeric': 0.08790123456790123, 'fraction_numerical': 0.0, 'mean_word_length': 2.3158756137479544, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1528704', 'n_tokens_mistral': 614, 'n_tokens_neox': 591, 'n_words': 137}