Skip to content

Commit 511af71

Browse files
author
dominik kuehne
committed
Merge branch 'MartinFechner-fixup-github-webhook'
2 parents 943fb0e + 7cc4beb commit 511af71

File tree

8 files changed

+51
-20
lines changed

8 files changed

+51
-20
lines changed

README.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ Synchronizes your data collection with GitHub and GitLab.
2020
- the data xar containing the target collection must be installed prior to using Tuttle
2121
- Authtoken for git repository to use
2222

23-
## Current restrictions
24-
25-
In version 1.1.1 not implemented:
26-
- webhooks are not fully implemented.
27-
2823
## Building and Installation
2924

3025
Tuttle uses Gulp as its build tool which itself builds on NPM.
@@ -94,8 +89,21 @@ Gitserver type: 'github' or 'gitlab'
9489
#### ref
9590
Define the working branch of the git repository
9691

97-
#### hookuser & hookpasswd (future use not implemented yet)
98-
tba
92+
#### hookuser & hookpasswd
93+
94+
##### Create API-Keys for Github / Gitlab
95+
96+
At this stage of development, the API keys must be generated via the API endpoint '/git/apikey' or for a specific collection '/git/{collection}/apikey'.
97+
98+
In the configuration "tuttle.xml" the "hookuser" is used to define the dbuser which executes the update.
99+
100+
Example configuration for GitHub:
101+
* 'Payload URL': https://existdb:8443/exist/apps/tuttle/git/hook
102+
* 'Content type': application/json
103+
104+
Example configuration for GitLab:
105+
* 'URL' : https://46.23.86.66:8443/exist/apps/tuttle/git/hook
106+
99107

100108
## Dashboard
101109

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tuttle",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "tuttle - a Git-integration for eXist-db",
55
"scripts": {
66
"test": "gulp install && mocha --exit",

src/expath-pkg.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
<package
33
xmlns="http://expath.org/ns/pkg"
44
name="http://e-editiones.org/tuttle"
5-
abbrev="tuttle" version="1.1.1" spec="1.0">
5+
abbrev="tuttle" version="1.1.2" spec="1.0">
66
<title>Tuttle - Git for eXist-db</title>
77
<dependency processor="http://exist-db.org" semver-min="5.3.0"/>
88
<dependency package="http://e-editiones.org/roaster" semver-min="1.0.0"/>
99
<dependency package="http://exist-db.org/apps/shared" semver-min="0.9.1"/>
10-
<!-- <dependency package="http://expath.org/ns/crypto" semver-min="5.0.0"/>-->
10+
<dependency package="http://expath.org/ns/crypto" semver-min="5.0.0"/>
1111
</package>

src/modules/api.xql

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,16 @@ declare function api:incremental($request as map(*)) {
335335
: APIKey generation for webhooks
336336
:)
337337
declare function api:api-keygen($request as map(*)) {
338-
let $git-collection := config:default-collection()
338+
let $git-collection :=
339+
if (not(exists($request?parameters?collection)))
340+
then config:default-collection()
341+
else xmldb:decode-uri($request?parameters?collection)
339342
let $config := config:collections($git-collection)
340-
let $collection := config:default-collection()
341343

342344
return
343345
if (exists($config)) then (
344346
let $apikey := app:random-key(42)
345-
let $write-apikey := app:write-apikey($collection, $apikey)
347+
let $write-apikey := app:write-apikey($git-collection, $apikey)
346348
return
347349
map {
348350
"APIKey" : $apikey
@@ -362,12 +364,12 @@ declare function api:hook($request as map(*)) {
362364

363365
return
364366
if (exists($config)) then (
365-
let $apikey := doc(config:apikeys())//apikeys/collection[name = $collection]/key/text()
367+
let $apikey := doc(config:apikeys())//apikeys/collection[name = $git-collection]/key/text()
366368
return
367369
if ($apikey) then (
368370
let $apikey-header :=
369371
if ($config?vcs = "github" ) then
370-
if (github:check-signature($git-collection, request:get-header("X-Hub-Signature"), request:get-data())) then
372+
if (github:check-signature($git-collection, request:get-header("X-Hub-Signature-256"), util:binary-to-string(request:get-data()))) then
371373
$apikey
372374
else ()
373375
else
@@ -377,7 +379,7 @@ declare function api:hook($request as map(*)) {
377379
let $collection-path := config:prefix() || "/" || $git-collection
378380
let $lockfile := $collection-path || "/" || config:lock()
379381
let $collection-destination-sha := $collection-path || "/gitsha.xml"
380-
let $login := xmldb:login($collection-path, $config:hookuser, $config:hookpasswd)
382+
let $login := xmldb:login($collection-path, $config?hookuser, $config?hookpasswd)
381383

382384
return
383385
if (not(exists(doc($lockfile)))) then (

src/modules/app.xql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ declare function app:write-apikey($collection as xs:string, $apikey as xs:string
9393
return update insert $add into doc(config:apikeys())//apikeys
9494
else
9595
let $add := <apikeys><collection><name>{$collection}</name><key>{$apikey}</key></collection></apikeys>
96-
return xmldb:store($collection-prefix, $apikey-resource, $add)
96+
let $store := xmldb:store($collection-prefix, $apikey-resource, $add)
97+
let $chmod := sm:chmod(config:apikeys(), "rw-r-----")
98+
return $store
9799
}
98100
catch * {
99101
map {

src/modules/github.xql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module namespace github="http://exist-db.org/apps/tuttle/github";
44

55
import module namespace http="http://expath.org/ns/http-client";
66
import module namespace compression="http://exist-db.org/xquery/compression";
7-
(:import module namespace crypto="http://expath.org/ns/crypto";:)
7+
import module namespace crypto="http://expath.org/ns/crypto";
88

99
import module namespace app="http://exist-db.org/apps/tuttle/app" at "app.xql";
1010
import module namespace config="http://exist-db.org/apps/tuttle/config" at "config.xql";
@@ -218,8 +218,8 @@ declare function github:get-url($config as map(*)) {
218218
:)
219219
declare function github:check-signature($collection as xs:string, $signature as xs:string, $payload as xs:string) as xs:boolean {
220220
let $private-key := xs:string(doc(config:apikeys())//apikeys/collection[name = $collection]/key/text())
221-
(: let $expected-signature := crypto:hmac($payload, $private-key, "HmacSha256", "base64"):)
222-
let $expected-signature := ""
221+
let $expected-signature := "sha256="||crypto:hmac($payload, $private-key, "HmacSha256", "hex")
222+
(: let $expected-signature := "":)
223223

224224
return
225225
if ($signature = $expected-signature) then

src/post-install.xql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
xquery version "3.1";
2+
3+
import module namespace api="http://exist-db.org/apps/tuttle/api" at "/db/apps/tuttle/modules/api.xql";
4+
5+
declare namespace sm="http://exist-db.org/xquery/securitymanager";
6+
declare namespace appconf="http://www.bbaw.de/telota/software/ediarum/web/appconf";
7+
8+
(: The following external variables are set by the repo:deploy function :)
9+
10+
(: file path pointing to the exist installation directory :)
11+
declare variable $home external;
12+
(: path to the directory containing the unpacked .xar package :)
13+
declare variable $dir external;
14+
(: the target collection into which the app is deployed :)
15+
declare variable $target external;
16+
17+
sm:chmod(xs:anyURI($target||"/modules/api.xql"), "rwxr-sr-x"),
18+
sm:chmod(xs:anyURI($target||"/data/tuttle.xml"), "rw-r-----")

src/repo.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
<copyright>true</copyright>
1010
<type>application</type>
1111
<target>tuttle</target>
12+
<finish>post-install.xql</finish>
1213
</meta>

0 commit comments

Comments
 (0)