From 6ee392e55eccba4342e8c7e4075605de271b1b44 Mon Sep 17 00:00:00 2001 From: Dominik Meyer Date: Tue, 5 Mar 2024 17:47:07 +0100 Subject: [PATCH] ADD: initial commit --- .drone.yml | 106 +++++++++++++++++++++++++ Containerfile | 18 +++++ entrypoint.sh | 5 ++ lib/Webhook2Ntfy.pm | 32 ++++++++ lib/Webhook2Ntfy/Controller/Webhook.pm | 105 ++++++++++++++++++++++++ public/index.html | 11 +++ script/webhook2_ntfy | 11 +++ scripts/setupEnvironment.sh | 3 + t/basic.t | 9 +++ templates/layouts/default.html.ep | 5 ++ templates/webhook/call.html.ep | 3 + webhook2_ntfy.yml | 3 + 12 files changed, 311 insertions(+) create mode 100644 .drone.yml create mode 100644 Containerfile create mode 100644 entrypoint.sh create mode 100644 lib/Webhook2Ntfy.pm create mode 100644 lib/Webhook2Ntfy/Controller/Webhook.pm create mode 100644 public/index.html create mode 100755 script/webhook2_ntfy create mode 100755 scripts/setupEnvironment.sh create mode 100644 t/basic.t create mode 100644 templates/layouts/default.html.ep create mode 100644 templates/webhook/call.html.ep create mode 100644 webhook2_ntfy.yml diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..50c6ba0 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,106 @@ +kind: pipeline +type: kubernetes +name: build-amd64 +platform: + arch: amd64 +node_selector: + kubernetes.io/arch: amd64 + +volumes: +- name: fedhq-ca-crt + config_map: + name: fedhq-ca-crt + default_mode: 420 # same as 644 in octal, or u+w,a+r + optional: false + +steps: + - name: build amd64 + image: quay.io/buildah/stable + privileged: true + volumes: + - name: fedhq-ca-crt + path: /etc/ssl/certs2/ + commands: + - scripts/setupEnvironment.sh + - buildah bud -t "registry.cloud.federationhq.de/webhook2ntfy:latest-amd64" --arch amd64 . + - buildah push --all registry.cloud.federationhq.de/webhook2ntfy:latest-amd64 + +--- + +kind: pipeline +type: kubernetes +name: build-arm64 +platform: + arch: arm64 +node_selector: + kubernetes.io/arch: arm64 + +volumes: +- name: fedhq-ca-crt + config_map: + name: fedhq-ca-crt + default_mode: 420 # same as 644 in octal, or u+w,a+r + optional: false + +steps: + - name: build arm64 + image: quay.io/buildah/stable + privileged: true + volumes: + - name: fedhq-ca-crt + path: /etc/ssl/certs2/ + commands: + - scripts/setupEnvironment.sh + - buildah bud -t "registry.cloud.federationhq.de/webhook2ntfy:latest-arm64" --arch arm64 . + - buildah push --all registry.cloud.federationhq.de/webhook2ntfy:latest-arm64 +--- +kind: pipeline +type: kubernetes +name: push +node_selector: + kubernetes.io/arch: amd64 + +volumes: +- name: fedhq-ca-crt + config_map: + name: fedhq-ca-crt + default_mode: 420 # same as 644 in octal, or u+w,a+r + optional: false + +steps: + - name: push + image: quay.io/buildah/stable + privileged: true + environment: + USERNAME: + from_secret: username + PASSWORD: + from_secret: password + volumes: + - name: fedhq-ca-crt + path: /etc/ssl/certs2/ + commands: + - scripts/setupEnvironment.sh + - buildah manifest create webhook2ntfy:latest registry.cloud.federationhq.de/webhook2ntfy:latest-arm64 registry.cloud.federationhq.de/webhook2ntfy:latest-amd64 + - buildah manifest push --all webhook2ntfy:latest docker://registry.cloud.federationhq.de/webhook2ntfy:latest + #- buildah login -u $${USERNAME} -p $${PASSWORD} registry.hub.docker.com + #- buildah manifest push --all imapfilter:latest docker://registry.hub.docker.com/byterazor/imapfilter:latest + - buildah manifest rm imapfilter:latest +depends_on: + - build-amd64 + - build-arm64 + + +--- +kind: secret +name: username +get: + path: docker + name: username + +--- +kind: secret +name: password +get: + path: docker + name: secret \ No newline at end of file diff --git a/Containerfile b/Containerfile new file mode 100644 index 0000000..74ca636 --- /dev/null +++ b/Containerfile @@ -0,0 +1,18 @@ +FROM alpine + +RUN apk update && apk add --no-cache tini bash ca-certificates openssl perl perl-mojolicious + +RUN mkdir -p /opt/webhook2_ntfy/ +ADD . /opt/webhook2_ntfy/ +RUN chmod a+x /opt/webhook2_ntfy/script/webhook2_ntfy +WORKDIR /opt/webhook2_ntfy + +RUN addgroup webhook2ntfy && adduser -D -G webhook2ntfy webhook2ntfy + +ADD entrypoint.sh /entrypoint.sh +RUN chmod a+x /entrypoint.sh + +USER webhook2ntfy + + +ENTRYPOINT ["/sbin/tini", "--", "/entrypoint.sh"] \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..e9cadee --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +cd /opt/webhook2_ntfy + +script/webhook2_ntfy daemon -l http://*:3000 \ No newline at end of file diff --git a/lib/Webhook2Ntfy.pm b/lib/Webhook2Ntfy.pm new file mode 100644 index 0000000..e27f782 --- /dev/null +++ b/lib/Webhook2Ntfy.pm @@ -0,0 +1,32 @@ +package Webhook2Ntfy; +use Mojo::Base 'Mojolicious', -signatures; + +# This method will run once at server start +sub startup ($self) { + + # Configure the application + $self->secrets($ENV{SECRETS}); + + my $webhook = $ENV{WEBHOOK_PATH}; + + if (!$webhook || length($webhook) == 0 ) + { + $self->log->fatal("no webhook provided in WEBHOOK_PATH"); + exit(-1); + } + elsif (length($webhook) < 20) + { + $self->log->fatal("please provide a more complex webhook path in WEBHOOK_PATH"); + exit(-1); + } + + $self->log->info("using webhook path: " . $webhook); + + # Router + my $r = $self->routes; + + # Normal route to controller + $r->post($webhook)->to('Webhook#call'); +} + +1; diff --git a/lib/Webhook2Ntfy/Controller/Webhook.pm b/lib/Webhook2Ntfy/Controller/Webhook.pm new file mode 100644 index 0000000..cabfc9b --- /dev/null +++ b/lib/Webhook2Ntfy/Controller/Webhook.pm @@ -0,0 +1,105 @@ +package Webhook2Ntfy::Controller::Webhook; +use Mojo::Base 'Mojolicious::Controller', -signatures; +use JSON; +use Data::Dumper; +use URI::Escape; +use Encode qw/encode decode/; +use MIME::Base64; +use LWP::UserAgent; +use HTTP::Request::Common qw{ POST }; + +sub publish +{ + my $self = shift; + my $msg = shift; + my $ntfyServer = $ENV{NTFY_SERVER}; + my $ntfyToken = $self->prepareAuthToken($ENV{NTFY_TOKEN}); + + my $ua = LWP::UserAgent->new(); + my $url = $ntfyServer . "/?auth=$ntfyToken"; + + my $response = $ua->post( $url, Markdown=>'yes', Content => to_json($msg)); + +} + +sub prepareAuthToken +{ + my $self = shift; + my $token = shift; + + my $auth=encode_base64("".":".$token, ""); + my $authString = encode_base64("Basic " . $auth, ""); + $authString =~ s/=+$//; + return $authString; +} + +sub redmine2ntfy +{ + + my $self = shift; + my $data = shift; + + $data->{text} =~ /^<(.*)\|(.+)>\+\-\+(.+)\+<(.*)\|(.*)>\+.*\+by\+(.*)$/; + + my $projectUrl=$1; + my $projectName=$2; + my $entity = $3; + my $entityUrl=$4; + my $entityName=$5; + my $author=$6; + $author =~ s/\+/ /g; + + my $status; + if ($entity eq "Issue") + { + $entityName=~s/\+/ /g; + } + + my $title = "$entity update for $projectName"; + my $text = "There has been an $entity update for project [$projectName]($projectUrl) by $author on [$entityName]($entityUrl)"; + + my $message = { + topic => $data->{channel}, + title => $title, + message => $text + + + }; + +} + +sub toNtfyMessage +{ + my $self = shift; + my $data = shift; + + + if ($ENV{IS_REDMINE}) + { + return $self->redmine2ntfy($data); + } + + my $message = { + topic => $data->{channel}, + message => $data->{text} + }; + + return $message; +} + + +# This action will render a template +sub call { + my $self = shift; + my $body = $self->req->body; + $body=uri_unescape($body); + $body=substr($body,8,length($body)); + my $data=from_json($body); + + $self->publish($self->toNtfyMessage($data)); + + # Render template "example/welcome.html.ep" with message + $self->render(msg => 'accepted'); +} + +1; diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..e74bb5f --- /dev/null +++ b/public/index.html @@ -0,0 +1,11 @@ + + + + Welcome to the Mojolicious real-time web framework! + + +

Welcome to the Mojolicious real-time web framework!

+ This is the static document "public/index.html", + click here to get back to the start. + + diff --git a/script/webhook2_ntfy b/script/webhook2_ntfy new file mode 100755 index 0000000..f2392f6 --- /dev/null +++ b/script/webhook2_ntfy @@ -0,0 +1,11 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use Mojo::File qw(curfile); +use lib curfile->dirname->sibling('lib')->to_string; +use Mojolicious::Commands; + +# Start command line interface for application +Mojolicious::Commands->start_app('Webhook2Ntfy'); diff --git a/scripts/setupEnvironment.sh b/scripts/setupEnvironment.sh new file mode 100755 index 0000000..2761ce0 --- /dev/null +++ b/scripts/setupEnvironment.sh @@ -0,0 +1,3 @@ +#!/bin/bash +cp /etc/ssl/certs2/federationHQ-CA.pem /etc/pki/ca-trust/source/anchors/ +update-ca-trust diff --git a/t/basic.t b/t/basic.t new file mode 100644 index 0000000..b2c687b --- /dev/null +++ b/t/basic.t @@ -0,0 +1,9 @@ +use Mojo::Base -strict; + +use Test::More; +use Test::Mojo; + +my $t = Test::Mojo->new('Webhook2Ntfy'); +$t->get_ok('/')->status_is(200)->content_like(qr/Mojolicious/i); + +done_testing(); diff --git a/templates/layouts/default.html.ep b/templates/layouts/default.html.ep new file mode 100644 index 0000000..599c556 --- /dev/null +++ b/templates/layouts/default.html.ep @@ -0,0 +1,5 @@ + + + <%= title %> + <%= content %> + diff --git a/templates/webhook/call.html.ep b/templates/webhook/call.html.ep new file mode 100644 index 0000000..3a4b902 --- /dev/null +++ b/templates/webhook/call.html.ep @@ -0,0 +1,3 @@ +% layout 'default'; +% title 'Message Accepted'; +

<%= $msg %>

diff --git a/webhook2_ntfy.yml b/webhook2_ntfy.yml new file mode 100644 index 0000000..80ed2e2 --- /dev/null +++ b/webhook2_ntfy.yml @@ -0,0 +1,3 @@ +--- +secrets: + - 6d0aaa833652976c0d076e7567195a8d258f57e3