Skip to content

Commit

Permalink
should works with cloud run !
Browse files Browse the repository at this point in the history
  • Loading branch information
panaC committed Jun 23, 2022
1 parent 3d0c7f6 commit 6b71e4c
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ RUN opm get openresty/lua-resty-mysql

RUN opm list

COPY conf.d /etc/nginx/conf.d
COPY nginx.conf /usr/local/openresty/nginx/conf/nginx.conf
COPY conf.d/nginx.conf /etc/nginx/conf.d/nginx.conf

COPY lua /lua

RUN rm -f /etc/nginx/conf.d/default.conf

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

Expand Down
13 changes: 13 additions & 0 deletions body.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

const info = {
os_version: 'my os version',
locale: 'fr',
timestamp: new Date().toISOString(),
fresh: true,
type: "poll", // poll or error emun
current_version: '1.2.3',
prev_version: '0.1.2',
};

process.stdout.write(JSON.stringify(info));

9 changes: 9 additions & 0 deletions curl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

#curl -v http://localhost:8080
#curl -H "Authorization: EDRLAB " -v http://localhost:8080
#curl -H "Authorization: EDRLAB rocks" -v http://localhost:8080

res=`node hmac.js`
hmac=`echo $res | cut -d ' ' -f1`
data=`echo $res | cut -d ' ' -f2-`
echo "curl -H \"Authorization: EDRLAB $hmac\" -d '$data' -v http://localhost:8080"
20 changes: 20 additions & 0 deletions db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

# create database telemetry;

# use telemetry;

create table if not exists logs (
id int not null auto_increment,
ts timestamp DEFAULT CURRENT_TIMESTAMP,
os_version varchar(512) not null,
locale varchar(8) not null,
os_ts timestamp not null,
fresh_install boolean not null,
entry_type enum('poll', 'error') not null,
current_version varchar(64) not null,
prev_version varchar(64) not null,
new_install boolean not null,
error text,
primary key(id)
);

16 changes: 16 additions & 0 deletions docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@


docker stop resty && docker rm resty
docker build -f Dockerfile -t resty:latest .

docker image inspect resty:latest | head

docker run \
--env SECRET_KEY="hello world" \
--env DB_HOST="192.168.65.2" \
--env DB_NAME="telemetry" \
--env DB_USER="root" \
--env DB_PASS="hello" \
--name resty \
--publish 8080:80 \
resty:latest
19 changes: 19 additions & 0 deletions hmac.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { createHmac } =require("crypto");
const { spawn } = require('node:child_process');


const bat = spawn('node', ['body.js']);

bat.stdout.on('data', (data) => {
process.stdout.write(`${telemetryHmac(data.toString())} ${data.toString()}`);
});

const telemetryHmac = (body) => {

const key = "hello world";
// find the key from fs or env-var // cf Daniel to hide it

const hmac = createHmac("sha1", key);
hmac.update(body, "utf8");
return hmac.digest("hex"); // length always 40
};
2 changes: 1 addition & 1 deletion lua/script.template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ print("Timestamp OK");

local ok, err, errcode, sqlstate = db:connect{
host = DB_HOST,--"192.168.65.2",
port = DB_PORT or 3306,
port = tonumber(DB_PORT) or 3306,
database = DB_NAME or "telemetry",
user = DB_USER or "root",
password = DB_PASS or "hello",
Expand Down
88 changes: 88 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# nginx.conf -- docker-openresty
#
# This file is installed to:
# `/usr/local/openresty/nginx/conf/nginx.conf`
# and is the file loaded by nginx at startup,
# unless the user specifies otherwise.
#
# It tracks the upstream OpenResty's `nginx.conf`, but removes the `server`
# section and adds this directive:
# `include /etc/nginx/conf.d/*.conf;`
#
# The `docker-openresty` file `nginx.vh.default.conf` is copied to
# `/etc/nginx/conf.d/default.conf`. It contains the `server section
# of the upstream `nginx.conf`.
#
# See https://github.com/openresty/docker-openresty/blob/master/README.md#nginx-config-files
#

#user nobody;
#worker_processes 1;

# Enables the use of JIT for regular expressions to speed-up their processing.
pcre_jit on;

#error_log logs/error.log;
error_log /dev/stdout notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

# Enables or disables the use of underscores in client request header fields.
# When the use of underscores is disabled, request header fields whose names contain underscores are marked as invalid and become subject to the ignore_invalid_headers directive.
# underscores_in_headers off;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

# Log in JSON Format
log_format nginxlog_json escape=json '{ "timestamp": "$time_iso8601", '
'"remote_addr": "$remote_addr", '
'"body_bytes_sent": $body_bytes_sent, '
'"request_time": $request_time, '
'"response_status": $status, '
'"request": "$request", '
'"request_method": "$request_method", '
'"host": "$host",'
'"upstream_addr": "$upstream_addr",'
'"http_x_forwarded_for": "$http_x_forwarded_for",'
'"http_referrer": "$http_referer", '
'"http_user_agent": "$http_user_agent", '
'"http_version": "$server_protocol", '
'"nginx_access": true }';
access_log /dev/stdout nginxlog_json;

# See Move default writable paths to a dedicated directory (#119)
# https://github.com/openresty/docker-openresty/issues/119
client_body_temp_path /var/run/openresty/nginx-client-body;
#proxy_temp_path /var/run/openresty/nginx-proxy;
#fastcgi_temp_path /var/run/openresty/nginx-fastcgi;
#uwsgi_temp_path /var/run/openresty/nginx-uwsgi;
#scgi_temp_path /var/run/openresty/nginx-scgi;

#sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

include /etc/nginx/conf.d/nginx.conf;

# Don't reveal OpenResty version to clients.
server_tokens off;
}
1 change: 1 addition & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 6b71e4c

Please sign in to comment.