Class: LogStash::Filters::Empow::ClassificationCenterClient

Inherits:
Object
  • Object
show all
Includes:
Util::Loggable
Defined in:
lib/logstash/filters/center-client.rb

Instance Method Summary collapse

Constructor Details

#initialize(username, password, aws_client_id, url_base) ⇒ ClassificationCenterClient

Returns a new instance of ClassificationCenterClient.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/logstash/filters/center-client.rb', line 15

def initialize(username, password, aws_client_id, url_base)
  @logger = self.logger

  @token = nil
  @url_base = url_base
  
  aws_region = 'us-east-2'

  @cognito_client = LogStash::Filters::Empow::CognitoClient.new(username, password, aws_region, aws_client_id)

  @last_authenticate_minute = 0
end

Instance Method Details

#authenticateObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/logstash/filters/center-client.rb', line 29

def authenticate
  # fixme: should check token expiration and throttle connections on failure
  
  @token = nil

  @logger.debug("reconnecting to the classfication center")

  current_minute = (Time.now.to_i / 60)
  if @last_authenticate_minute < current_minute
    @last_authenticate_minute = current_minute
    @last_minute_failed_login_count = 0
    @last_authentication_error = ''
  end

  # avoid too many authentication requests
  if @last_minute_failed_login_count < 3
    begin
      @token = @cognito_client.authenticate
    rescue Aws::CognitoIdentityProvider::Errors::NotAuthorizedException, Aws::CognitoIdentityProvider::Errors::UserNotFoundException, Aws::CognitoIdentityProvider::Errors::UserNotConfirmedException => e
      @logger.warn("unable to authenticate with classification center", :error => e)
      @last_authentication_error = e.to_s
      inc_unsuccessful_logins()
    rescue StandardError => e
      @logger.warn("unable to authenticate with classification center", :error => e.class.name)
      @last_authentication_error = e.class.name.to_s
      inc_unsuccessful_logins()
    end
  end

  return (!@token.nil?)
end

#classify(requests) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/logstash/filters/center-client.rb', line 66

def classify(requests)
  authenticate if @token.nil? # try connecting if not already connected

  res = nil

  begin
    res = classify_online(requests)

  rescue RestClient::Unauthorized, RestClient::Forbidden, RestClient::UpgradeRequired => err
    @logger.debug("reconnecting to the empow cloud", :error => err)

    if !authenticate
      return unauthorized_bulk_response(@last_authentication_error, requests)
    end
    
    begin
      res = classify_online(requests)
    rescue StandardError => e
      @logger.debug("encountered an unexpected error on the 2nd attempt", :error => e, :backtrace => e.backtrace)

      error_message = rescue_http_error_result(e)

      return bulk_error(error_message, requests)
    end

  rescue StandardError => e
    @logger.error("encountered an unexpected error while querying the center", :error => e)

    error_message = rescue_http_error_result(e)

    return bulk_error(error_message, requests)
  end

  if res.nil? || res.strip.length == 0
    return bulk_error("no content", requests)
  end

  parsed_json = nil

  begin
    parsed_json = JSON.parse(res)
  rescue StandardError => e
    @logger.error("unable to parse json", :json => res)
    return bulk_error("invalid request", requests)
  end

  return successful_response(requests, parsed_json)
end