Skip to content

Commit

Permalink
changed from concating strings to string.format
Browse files Browse the repository at this point in the history
  • Loading branch information
ajvb committed Dec 17, 2018
1 parent 5d5ff90 commit e03fcba
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions lib/resty/iprepd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function _M.new(options)

local cache, err = lrucache.new(cache_buffer_count)
if not cache then
fatal_error('failed to create the cache: ' .. (err or 'unknown'))
fatal_error(string.format('failed to create the cache: %s', (err or 'unknown')))
end

local statsd_client = nil
Expand All @@ -45,7 +45,7 @@ function _M.new(options)
timeout = options.timeout or 10,
threshold = iprepd_threshold,
api_key_hdr = {
['Authorization'] = 'APIKey ' .. iprepd_api_key,
['Authorization'] = string.format('APIKey %s', iprepd_api_key),
},
cache = cache,
cache_ttl = options.cache_ttl or 30,
Expand All @@ -64,20 +64,20 @@ function _M.new(options)
end

function _M.check(self, ip)
self:debug_log("Checking " .. ip)
self:debug_log(string.format("Checking %s", ip))
ngx.req.set_header('X-Foxsec-IP-Reputation-Below-Threshold', 'false')
ngx.req.set_header('X-Foxsec-Block', 'false')
if self.whitelist then
if iputils.ip_in_cidrs(ip, self.whitelist) then
self:debug_log(ip .. " in whitelist")
self:debug_log(string.format("%s in whitelist", ip))
return
end
end


local reputation = self:get_reputation(ip)
if reputation then
self:debug_log("Got reputation of " .. reputation .. " for " .. ip)
self:debug_log(string.format("Got reputation of %d for %s", reputation, ip))
ngx.req.set_header('X-Foxsec-IP-Reputation', tostring(reputation))
if reputation <= self.threshold then
ngx.req.set_header('X-Foxsec-IP-Reputation-Below-Threshold', 'true')
Expand All @@ -87,25 +87,20 @@ function _M.check(self, ip)
end

if self.dont_block == 1 then
ngx.log(ngx.ERR, ip .. ' is below threshold with a reputation of ' .. reputation)
ngx.log(ngx.ERR, string.format("%s is below threshold with a reputation of %d", ip, reputation))
else
ngx.log(ngx.ERR, ip .. ' rejected with a reputation of ' .. reputation)
ngx.log(ngx.ERR, string.format("%s rejected with a reputation of %d", ip, reputation))
if self.statsd then
self.statsd.incr("iprepd.status.rejected")
end
ngx.exit(ngx.HTTP_FORBIDDEN)
end
else
self:debug_log(ip .. " accepted")
if self.statsd then
self.statsd.incr("iprepd.status.accepted")
end
end

return
return
end
end

self:debug_log(ip .. " accepted")
self:debug_log(string.format("%s accepted", ip))
if self.statsd then
self.statsd.incr("iprepd.status.accepted")
end
Expand All @@ -117,15 +112,15 @@ function _M.get_reputation(self, ip)
if not reputation then
local httpc = http.new()
httpc:set_timeout(self.timeout)
local resp, err = httpc:request_uri(self.url .. '/' .. ip, {
local resp, err = httpc:request_uri(string.format("%s/%s", self.url, ip), {
method = "GET",
headers = self.api_key_hdr,
})
if err then
if self.statsd then
self.statsd.incr("iprepd.err." .. err)
end
ngx.log(ngx.ERR, 'Error with request to iprepd: ' .. err)
ngx.log(ngx.ERR, string.format("Error with request to iprepd: %s", err))
return nil
end

Expand All @@ -138,13 +133,13 @@ function _M.get_reputation(self, ip)
elseif resp.status == 404 then
reputation = 100
else
ngx.log(ngx.ERR, 'iprepd responded with a ' .. resp.status .. ' http status code')
ngx.log(ngx.ERR, string.format("iprepd responded with a %d http status code", resp.status))
if self.statsd then
self.statsd.incr("iprepd.err." .. resp.status)
end
if self.cache_errors == 1 then
reputation = 100
self:debug_log('cache_errors is enabled, setting reputation of ' .. ip .. ' to 100 within the cache')
self:debug_log(string.format("cache_errors is enabled, setting reputation of %s to 100 within the cache", ip))
end
end
end
Expand Down Expand Up @@ -174,7 +169,7 @@ end

function _M.debug_log(self, msg)
if self.verbose == 1 then
ngx.log(ngx.ERR, "[verbose] " .. msg)
ngx.log(ngx.ERR, string.format("[verbose] %s", msg))
end
end

Expand Down

0 comments on commit e03fcba

Please sign in to comment.