Skip to content

Commit 7831c4b

Browse files
author
Mike Trinkala
authored
Merge pull request #222 from mozilla-services/dev
Sprint 2018-0
2 parents 14ecde1 + de886c9 commit 7831c4b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1408
-65
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ env:
1313
- DOCKER_IMAGE=debian:stretch
1414
- DOCKER_IMAGE=fedora:latest
1515
- DOCKER_IMAGE=ubuntu:12.04 CMAKE_URL=auto
16-
- DOCKER_IMAGE=ubuntu:latest # LTS
16+
- DOCKER_IMAGE=ubuntu:latest CMAKE_URL=auto # LTS
1717
- DOCKER_IMAGE=ubuntu:devel
1818

1919
script:

CMakeLists.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
# License, v. 2.0. If a copy of the MPL was not distributed with this
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

5-
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
5+
cmake_minimum_required(VERSION 3.6 FATAL_ERROR)
66
project(luasandbox-extensions LANGUAGES NONE)
77
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
88

99
include(GNUInstallDirs)
1010
if(WIN32)
1111
set(CMAKE_INSTALL_LIBDIR ${CMAKE_INSTALL_BINDIR})
1212
endif()
13-
set(CMAKE_INSTALL_PREFIX "")
1413
set(CMAKE_SHARED_LIBRARY_PREFIX "")
1514
set(PACKAGE_PREFIX luasandbox)
1615
set(CPACK_DEBIAN_FILE_NAME "DEB-DEFAULT")

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ decoupling the module and businesss logic maintenance and deployment.
1212

1313
### Prerequisites
1414
* C compiler (GCC 4.7+, Visual Studio 2013)
15-
* CMake (3.5+) - http://cmake.org/cmake/resources/software.html
15+
* CMake (3.6+) - http://cmake.org/cmake/resources/software.html
1616
* Git http://git-scm.com/download
1717
* luasandbox (1.2+) https://github.com/mozilla-services/lua_sandbox
1818
* Module specific (i.e. if buiding the ssl module openssl will be required)

cmake/CTestCustom.in.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
set(CTEST_CUSTOM_PRE_TEST "${CMAKE_MAKE_PROGRAM} install DESTDIR=install")
2+
set(CTEST_CUSTOM_PRE_TEST "${CMAKE_COMMAND} -DCMAKE_INSTALL_PREFIX=install -P cmake_install.cmake")

geoip/CMakeLists.txt

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

55
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
6-
project(geoip LANGUAGES NONE)
6+
project(geoip LANGUAGES C)
77

88
externalproject_add(
99
ep_geoip
1010
GIT_REPOSITORY https://github.com/agladysh/lua-geoip.git
1111
GIT_TAG 06548fd6dac30d4ba0ea5489c3cffe8c20b526c0
12-
CMAKE_ARGS ${EP_CMAKE_ARGS}
12+
CMAKE_ARGS ${EP_CMAKE_ARGS} -DPARENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}
1313
UPDATE_COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt.${PROJECT_NAME} <SOURCE_DIR>/CMakeLists.txt
1414
)
15-
externalproject_add_step(ep_geoip copy_cpack
15+
externalproject_add_step(ep_geoip copy_cpack
1616
COMMAND ${CMAKE_COMMAND} -E copy <BINARY_DIR>/${PROJECT_NAME}.cpack ${CMAKE_BINARY_DIR}
1717
DEPENDEES install)
18+
include(sandbox_ep_test)

geoip/CMakeLists.txt.geoip

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

55
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
6-
project(geoip VERSION 0.2.0 LANGUAGES C)
6+
project(geoip VERSION 0.2.1 LANGUAGES C)
77

88
set(CPACK_PACKAGE_NAME luasandbox-${PROJECT_NAME})
99
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Lua geoip Module")
@@ -85,3 +85,5 @@ install(TARGETS city DESTINATION ${INSTALL_IOMODULE_PATH}/geoip)
8585
add_library(country SHARED ${COUNTRY_SRC})
8686
target_link_libraries(country ${LUA_LIBRARIES} ${GEOIP_LIBRARY})
8787
install(TARGETS country DESTINATION ${INSTALL_IOMODULE_PATH}/geoip)
88+
89+
install(DIRECTORY ${PARENT_SOURCE_DIR}/io_modules/ DESTINATION ${INSTALL_IOMODULE_PATH} ${DPERMISSION})

geoip/io_modules/geoip/heka.lua

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
-- This Source Code Form is subject to the terms of the Mozilla Public
2+
-- License, v. 2.0. If a copy of the MPL was not distributed with this
3+
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
--[[
6+
# Heka Message Extensions with GeoIP information
7+
8+
This module is intended to be used with another IO module such as a decoder to
9+
extend the message with GeoIP information prior to injection.
10+
11+
## Functions
12+
13+
### add_geoip
14+
15+
Given a Heka message add the geoip entries based on the specified field name.
16+
The requested geoip entries are specified in the `lookup` configuration table.
17+
The table consists of the entry name and the suffix to add to the field name,
18+
an empty suffix will overwrite the original value. See: the `opts` variable in
19+
https://github.com/agladysh/lua-geoip/blob/master/src/city.c for the available
20+
options, common values include "city" and "country_code".
21+
22+
For example, if "remote_addr" is present in msg.Fields with a cfg of `lookup =
23+
{ city = "_city" }` on a successful lookup a new field will be added to the
24+
message named "remote_addr_city" with the resulting city value.
25+
26+
*Arguments*
27+
- msg (table) - original message
28+
- field_name (string) - field name in the message to lookup
29+
30+
*Return*
31+
- none - the message is modified in place or an error is thrown
32+
33+
## Configuration examples
34+
```lua
35+
geoip_heka = {
36+
city_db_file = "/path/to/geo/dat", -- path to GeoIP data
37+
lookup = { city = "_city", country_code = "_country" }, -- entries to lookup and their field suffix
38+
39+
test = false, -- true if being used in tests without GeoIP database
40+
}
41+
```
42+
--]]
43+
local module_name = ...
44+
local module_cfg = require "string".gsub(module_name, "%.", "_")
45+
local cfg = read_config(module_cfg) or error(module_name .. " configuration not found")
46+
assert(type(cfg.lookup) == "table" and next(cfg.lookup) ~= nil, "lookup configuration must be a table")
47+
48+
local geoip = require "geoip.city"
49+
local ostime = require "os".time
50+
local smatch = string.match
51+
local assert = assert
52+
local pairs = pairs
53+
local type = type
54+
55+
local M = {}
56+
setfenv(1, M) -- Remove external access to contain everything in the module.
57+
58+
local gtest = {}
59+
60+
-- In test mode, returns Mountain View, US for 192.168.1.2 otherwise nil, only
61+
-- supports country_code and city lookup values
62+
local test_return = {country_code = "US", city = "Mountain View"}
63+
function gtest:query_by_addr(v, lookup)
64+
if v == "192.168.1.2" then
65+
return test_return[lookup]
66+
end
67+
end
68+
69+
local ptime = 0
70+
local geodb = nil
71+
local function refresh_db()
72+
local ctime = ostime()
73+
if ptime + 3600 < ctime then
74+
if geodb then geodb:close() end
75+
geodb = assert(geoip.open(cfg.city_db_file))
76+
ptime = ctime
77+
end
78+
end
79+
80+
81+
local function validate_ip(ip)
82+
local sl = #ip
83+
return not (sl < 7 or sl > 15 or not smatch(ip, "^%d+%.%d+%.%d+%.%d+$"))
84+
end
85+
86+
87+
if cfg.test then
88+
geodb = gtest
89+
refresh_db = function () return end
90+
else
91+
refresh_db()
92+
end
93+
94+
function add_geoip(msg, field_name)
95+
if not msg.Fields then return end
96+
local value = msg.Fields[field_name]
97+
if type(value) ~= "string" or not validate_ip(value) then return end
98+
99+
refresh_db()
100+
101+
for k,v in pairs(cfg.lookup) do
102+
local ret = geodb:query_by_addr(value, k)
103+
if ret then
104+
msg.Fields[field_name .. v] = ret
105+
if cfg.remove_original_field then
106+
msg.Fields[field_name] = nil
107+
end
108+
end
109+
end
110+
end
111+
112+
113+
return M

geoip/test_sandbox.c

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2+
/* vim: set ts=2 et sw=2 tw=80: */
3+
/* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6+
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
#include <string.h>
10+
11+
#include <luasandbox/heka/sandbox.h>
12+
#include <luasandbox/test/mu_test.h>
13+
#include <luasandbox/test/sandbox.h>
14+
15+
#include "test_module.h"
16+
17+
char *e = NULL;
18+
19+
20+
static char* test_core()
21+
{
22+
lsb_lua_sandbox *sb = lsb_create(NULL, "test.lua", TEST_MODULE_PATH, NULL);
23+
mu_assert(sb, "lsb_create() received: NULL");
24+
lsb_err_value ret = lsb_init(sb, NULL);
25+
mu_assert(!ret, "lsb_init() received: %s %s", ret, lsb_get_error(sb));
26+
e = lsb_destroy(sb);
27+
mu_assert(!e, "lsb_destroy() received: %s", e);
28+
return NULL;
29+
}
30+
31+
32+
static char* all_tests()
33+
{
34+
mu_run_test(test_core);
35+
return NULL;
36+
}
37+
38+
39+
int main()
40+
{
41+
char *result = all_tests();
42+
if (result) {
43+
printf("%s\n", result);
44+
} else {
45+
printf("ALL TESTS PASSED\n");
46+
}
47+
printf("Tests run: %d\n", mu_tests_run);
48+
free(e);
49+
50+
return result != 0;
51+
}

geoip/tests/test.lua

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
-- This Source Code Form is subject to the terms of the Mozilla Public
2+
-- License, v. 2.0. If a copy of the MPL was not distributed with this
3+
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
local cfg ={
6+
geoip_heka = {
7+
test = true,
8+
lookup = {city = "_city", country_code = "_country"},
9+
}
10+
}
11+
12+
function read_config(k)
13+
return cfg[k]
14+
end
15+
16+
local string = require "string"
17+
local gh = require "geoip.heka"
18+
19+
local tests = {
20+
{{}, "remote_addr", nil},
21+
{{Fields = {}}, "remote_addr", {}},
22+
{{Fields = {remote_addr = "foobar"}}, "remote_addr", {remote_addr = "foobar"}},
23+
{{Fields = {remote_addr = "192.168.1.1"}}, "remote_addr", {remote_addr = "192.168.1.1"}},
24+
{{Fields = {remote_addr = "192.168.1.2"}}, "other_addr", {remote_addr = "192.168.1.2"}},
25+
{{Fields = {remote_addr = "192.168.1.2"}}, "remote_addr", {remote_addr = "192.168.1.2", remote_addr_city = "Mountain View", remote_addr_country = "US"}},
26+
{{Fields = {remote_addr = 7}}, "remote_addr", {remote_addr = 7}},
27+
}
28+
29+
local function verify_table(idx, t, expected)
30+
if t == nil and expected ~= nil then
31+
error(string.format("test: %d failed Fields not nil", idx))
32+
end
33+
if not t then return end
34+
35+
for k,r in pairs(t) do
36+
local e = expected[k]
37+
if e ~= r then
38+
error(string.format("test: %d field: %s expected: %s received: %s", idx, k, tostring(e), tostring(r)))
39+
end
40+
end
41+
end
42+
43+
for i,v in ipairs(tests) do
44+
gh.add_geoip(v[1], v[2])
45+
verify_table(i, v[1].Fields, v[3])
46+
end
47+
48+
cfg.geoip_heka.remove_original_field = true
49+
local msg = {Fields = {remote_addr = "192.168.1.2"}}
50+
gh.add_geoip(msg, "remote_addr")
51+
verify_table(99, msg.Fields, tests[6][3])
52+
assert(not msg.Fields.remote_addr)

heka/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

55
cmake_minimum_required(VERSION 3.0)
6-
project(heka VERSION 1.1.13 LANGUAGES C)
6+
project(heka VERSION 1.1.14 LANGUAGES C)
77
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Utility modules for Heka sandboxes")
88
set(CPACK_DEBIAN_PACKAGE_DEPENDS "${PACKAGE_PREFIX}-cjson (>= 2.1)")
99
string(REGEX REPLACE "[()]" "" CPACK_RPM_PACKAGE_REQUIRES ${CPACK_DEBIAN_PACKAGE_DEPENDS})

0 commit comments

Comments
 (0)