Skip to content

Commit c5278d2

Browse files
authored
Merge pull request #67 from joewiz/fix-repo-group-permissions
Fix repo group permissions
2 parents 3b2f036 + 40fe860 commit c5278d2

File tree

7 files changed

+88
-122
lines changed

7 files changed

+88
-122
lines changed

modules/config.xqm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,14 @@ declare function config:repo-descriptor() as element(repo:meta) {
7171
};
7272

7373
(:~
74-
: Returns the user and group from the repo.xml descriptor.
74+
: Returns the permissions information from the repo.xml descriptor.
7575
:)
7676
declare function config:repo-permissions() as map(*) {
7777
config:repo-descriptor()/repo:permissions !
7878
map {
7979
"user": ./@user/string(),
80-
"group": ./@group/string()
80+
"group": ./@group/string(),
81+
"mode": ./@mode/string()
8182
}
8283
};
8384

modules/log.xqm

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ xquery version "3.1";
2727
module namespace log="http://exist-db.org/xquery/app/log";
2828

2929
import module namespace config="http://exist-db.org/xquery/apps/config" at "config.xqm";
30+
import module namespace scanrepo="http://exist-db.org/xquery/admin/scanrepo" at "scan.xqm";
3031

3132
(:~
3233
: Append entries to the structured application event log
@@ -36,21 +37,21 @@ import module namespace config="http://exist-db.org/xquery/apps/config" at "conf
3637
:)
3738
declare function log:event($event as element(event)) as empty-sequence() {
3839
let $today := current-date()
39-
let $log-collection := log:collection($today)
40+
let $log-collection-name := log:collection($today)
41+
let $log-collection := $config:logs-col || "/" || $log-collection-name
4042
let $log-document-name := log:document-name($today)
41-
let $log-document-path :=
42-
($config:logs-col, $log-collection, $log-document-name)
43-
=> string-join("/")
44-
let $_ :=
45-
if (doc-available($log-document-path)) then
46-
update insert $event into doc($log-document-path)/public-repo-log
47-
else (
48-
log:mkcol($config:logs-col, $log-collection),
49-
xmldb:store(
50-
$config:logs-col || "/" || $log-collection,
51-
$log-document-name,
52-
element public-repo-log { $event })
53-
)
43+
let $log-document := $log-collection || "/" || $log-document-name
44+
let $store-log :=
45+
if (doc-available($log-document)) then
46+
update insert $event into doc($log-document)/public-repo-log
47+
else
48+
(
49+
if (xmldb:collection-available($log-collection)) then
50+
()
51+
else
52+
log:mkcol($config:logs-col, $log-collection-name),
53+
scanrepo:store($log-collection, $log-document-name, element public-repo-log { $event })
54+
)
5455
return
5556
()
5657
};
@@ -76,7 +77,11 @@ function log:mkcol-recursive($collection as xs:string, $components as xs:string*
7677
if (exists($components)) then
7778
let $newColl := concat($collection, "/", $components[1])
7879
return (
79-
xmldb:create-collection($collection, $components[1]),
80+
xmldb:create-collection($collection, $components[1]) !
81+
(
82+
sm:chgrp(xs:anyURI(.), config:repo-permissions()?mode),
83+
.
84+
),
8085
log:mkcol-recursive($newColl, subsequence($components, 2))
8186
)
8287
else

modules/publish-package.xq

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ declare function local:log-put-package-event($filename as xs:string) as empty-se
3232
};
3333

3434
declare function local:upload-and-publish($xar-filename as xs:string, $xar-binary as xs:base64Binary) {
35-
let $path := xmldb:store($config:packages-col, $xar-filename, $xar-binary)
35+
let $path := scanrepo:store($config:packages-col, $xar-filename, $xar-binary)
3636
let $publish := scanrepo:publish-package($xar-filename)
3737
return
3838
map {

modules/scan.xqm

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ declare namespace xmldb="http://exist-db.org/xquery/xmldb";
1717

1818
declare namespace expath="http://expath.org/ns/pkg";
1919

20+
(:~
21+
: Helper function to store resources and set permissions for access by repo group
22+
:)
23+
declare function scanrepo:store($collection-uri as xs:string, $resource-name as xs:string, $contents as item()?) as xs:string {
24+
xmldb:store($collection-uri, $resource-name, $contents) !
25+
(
26+
sm:chgrp(., config:repo-permissions()?group),
27+
sm:chmod(., config:repo-permissions()?mode),
28+
.
29+
)
30+
};
31+
2032
(:~
2133
: Helper function to store a package's icon and transform its metadata into the format needed for raw-metadata
2234
:)
@@ -26,7 +38,7 @@ function scanrepo:handle-icon($path as xs:string, $data as item()?, $param as it
2638
let $pkgName := substring-before($param, ".xar")
2739
let $suffix := replace($path, "^.*\.([^\.]+)", "$1")
2840
let $name := concat($pkgName, ".", $suffix)
29-
let $stored := xmldb:store($config:icons-col, $name, $data)
41+
let $stored := scanrepo:store($config:icons-col, $name, $data)
3042
return
3143
element icon { $name }
3244
};
@@ -214,7 +226,7 @@ declare function scanrepo:rebuild-package-groups() as xs:string {
214226
$group
215227
}
216228
return
217-
xmldb:store($config:metadata-col, $config:package-groups-doc-name, $package-groups)
229+
scanrepo:store($config:metadata-col, $config:package-groups-doc-name, $package-groups)
218230
};
219231

220232
(:~
@@ -229,7 +241,7 @@ declare function scanrepo:rebuild-raw-packages() as xs:string {
229241
scanrepo:extract-raw-package($package-xar)
230242
}
231243
return
232-
xmldb:store($config:metadata-col, $config:raw-packages-doc-name, $raw-packages)
244+
scanrepo:store($config:metadata-col, $config:raw-packages-doc-name, $raw-packages)
233245
};
234246

235247
(:~

post-install.xq

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,60 @@ declare variable $dir external;
2323
(: the target collection into which the app is deployed :)
2424
declare variable $target external;
2525

26-
(: Until https://github.com/eXist-db/exist/issues/3734 is fixed, we hard code the default group name :)
27-
declare variable $repo-group :=
28-
(: config:repo-permissions()?group :)
29-
"repo"
30-
;
31-
declare variable $repo-user :=
32-
(: config:repo-permissions()?user :)
33-
"repo"
34-
;
26+
(: Configuration file for the logs collection :)
27+
declare variable $logs-xconf :=
28+
<collection xmlns="http://exist-db.org/collection-config/1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
29+
<index>
30+
<range>
31+
<create qname="type" type="xs:string"/>
32+
</range>
33+
</index>
34+
</collection>;
35+
36+
(: Helper function to recursively create a collection hierarchy :)
37+
declare function local:mkcol-recursive($collection as xs:string, $components as xs:string*) {
38+
if (exists($components)) then
39+
let $newColl := concat($collection, "/", $components[1])
40+
return (
41+
xmldb:create-collection($collection, $components[1]),
42+
local:mkcol-recursive($newColl, subsequence($components, 2))
43+
)
44+
else
45+
()
46+
};
47+
48+
(: Create a collection hierarchy :)
49+
declare function local:mkcol($collection as xs:string, $path as xs:string) {
50+
local:mkcol-recursive($collection, tokenize($path, "/"))
51+
};
3552

3653
(:~
3754
: Set user and group to be owner by values in repo.xml
3855
:)
3956
declare function local:set-data-collection-permissions($resource as xs:string) {
40-
if (sm:get-permissions(xs:anyURI($resource))/sm:permission/@group = $repo-group) then
57+
if (sm:get-permissions(xs:anyURI($resource))/sm:permission/@group = config:repo-permissions()?group) then
4158
()
4259
else
4360
(
44-
sm:chown($resource, $repo-user),
45-
sm:chgrp($resource, $repo-group),
46-
sm:chmod(xs:anyURI($resource), "rwxrwxr-x")
61+
sm:chown($resource, config:repo-permissions()?user),
62+
sm:chgrp($resource, config:repo-permissions()?group),
63+
sm:chmod(xs:anyURI($resource), config:repo-permissions()?mode)
4764
)
4865
};
4966

67+
(: Create the data collection hierarchy :)
68+
69+
xmldb:create-collection($config:app-data-parent-col, $config:app-data-col-name),
70+
for $col-name in ($config:icons-col-name, $config:metadata-col-name, $config:packages-col-name, $config:logs-col-name)
71+
return
72+
xmldb:create-collection($config:app-data-col, $col-name),
73+
74+
(: Create log indexes :)
75+
76+
local:mkcol("/db/system/config", $config:logs-col),
77+
xmldb:store("/db/system/config" || $config:logs-col, "collection.xconf", $logs-xconf),
78+
xmldb:reindex($config:logs-col),
79+
5080
(: Set user and group ownership on the package data collection hierarchy :)
5181

5282
for $col in ($config:app-data-col, xmldb:get-child-collections($config:app-data-col) ! ($config:app-data-col || "/" || .))
@@ -58,10 +88,8 @@ return
5888
if (doc-available($config:raw-packages-doc) and doc-available($config:package-groups-doc)) then
5989
()
6090
else
61-
(
62-
scanrepo:rebuild-all-package-metadata(),
63-
($config:raw-packages-doc, $config:package-groups-doc) ! local:set-data-collection-permissions(.)
64-
),
91+
scanrepo:rebuild-all-package-metadata(),
6592

66-
(: execute get-package.xq as repo group, so that it can write to logs :)
67-
sm:chmod(xs:anyURI($target || "/modules/get-package.xq"), "rwsrwxr-x")
93+
(: Ensure get-package.xq is run as "repo" group, so that it can write to logs :)
94+
95+
sm:chmod(xs:anyURI($target || "/modules/get-package.xq"), "g+s")

pre-install.xq

Lines changed: 0 additions & 80 deletions
This file was deleted.

repo.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
<copyright>true</copyright>
99
<type>application</type>
1010
<target>public-repo</target>
11-
<prepare>pre-install.xq</prepare>
11+
<prepare/>
1212
<finish>post-install.xq</finish>
13-
<permissions password="repo" user="repo" group="repo" mode="rw-rw-r--"/>
13+
<permissions password="repo" user="repo" group="repo" mode="rwxrwxr-x"/>
1414
<changelog>
1515
<change version="2.0.0">
1616
<ul xmlns="http://www.w3.org/1999/xhtml">

0 commit comments

Comments
 (0)