Class: CloudflareAccess

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/inputs/cloudflare_logs.rb

Constant Summary collapse

DEFAULT_FIELDS =
%w(CacheCacheStatus CacheResponseBytes CacheResponseStatus ClientASN ClientCountry ClientDeviceType ClientIP ClientIPClass ClientRequestBytes ClientRequestHost ClientRequestMethod ClientRequestProtocol ClientRequestReferer ClientRequestURI ClientRequestUserAgent ClientSSLCipher ClientSSLProtocol ClientSrcPort EdgeColoID EdgeEndTimestamp EdgePathingStatus EdgeResponseBytes EdgeResponseCompressionRatio EdgeResponseStatus EdgeStartTimestamp OriginIP OriginResponseBytes OriginResponseHTTPExpires OriginResponseHTTPLastModified OriginResponseStatus OriginResponseTime RayID WAFAction WAFRuleID ZoneID).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments) ⇒ CloudflareAccess

Returns a new instance of CloudflareAccess.



14
15
16
17
18
19
20
21
# File 'lib/logstash/inputs/cloudflare_logs.rb', line 14

def initialize(arguments)
  @auth_email = arguments[:auth_email]
  @auth_key = arguments[:auth_key]
  @domain = arguments[:domain]
  @fields = arguments[:fields] || DEFAULT_FIELDS
  @metadata_file = arguments[:metadata_file]
  @logger = arguments[:logger]
end

Instance Attribute Details

#auth_emailObject

Returns the value of attribute auth_email.



13
14
15
# File 'lib/logstash/inputs/cloudflare_logs.rb', line 13

def auth_email
  @auth_email
end

#auth_keyObject

Returns the value of attribute auth_key.



13
14
15
# File 'lib/logstash/inputs/cloudflare_logs.rb', line 13

def auth_key
  @auth_key
end

#domainObject

Returns the value of attribute domain.



13
14
15
# File 'lib/logstash/inputs/cloudflare_logs.rb', line 13

def domain
  @domain
end

#fieldsObject

Returns the value of attribute fields.



13
14
15
# File 'lib/logstash/inputs/cloudflare_logs.rb', line 13

def fields
  @fields
end

#loggerObject

Returns the value of attribute logger.



13
14
15
# File 'lib/logstash/inputs/cloudflare_logs.rb', line 13

def logger
  @logger
end

#metadata_fileObject

Returns the value of attribute metadata_file.



13
14
15
# File 'lib/logstash/inputs/cloudflare_logs.rb', line 13

def 
  @metadata_file
end

Instance Method Details

#end_timeObject



36
37
38
# File 'lib/logstash/inputs/cloudflare_logs.rb', line 36

def end_time
  @end_time ||= (Time.now - (5 * 60)).to_datetime.rfc3339.to_s.gsub(/[-+]00:00/, 'Z')
end

#logsObject



56
57
58
59
# File 'lib/logstash/inputs/cloudflare_logs.rb', line 56

def logs
  results = RestClient.get("https://api.cloudflare.com/client/v4/zones/#{domain}/logs/received?start=#{start_time}&end=#{end_time}&fields=#{fields.join(',')}", 'X-Auth-Email' => auth_email, 'X-Auth-Key' => auth_key)
  results.body.split("\n").collect { |raw_log| JSON.parse(raw_log) }
end

#start_timeObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/logstash/inputs/cloudflare_logs.rb', line 23

def start_time
  if File.exist?()
    begin
      start_time = Time.parse(JSON.parse(File.read())['start_time']).to_datetime.rfc3339.to_s.gsub(/[-+]00:00/, 'Z')
    rescue JSON::ParserError
      start_time = (Time.now - (15 * 60)).to_datetime.rfc3339.to_s.gsub(/[-+]00:00/, 'Z')
    end
  else
    start_time = (Time.now - (15 * 60)).to_datetime.rfc3339.to_s.gsub(/[-+]00:00/, 'Z')
  end
  start_time
end

#update_metadata_file(key, value) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/logstash/inputs/cloudflare_logs.rb', line 40

def (key, value)
  key = key.to_s
   = {}

  if File.exist?()
    begin
       = JSON.parse(File.read())
    rescue JSON::ParserError
    end
  end

  [key] = value

  File.open(, 'w+') { |file| file.write(.to_json) }
end