aboutsummaryrefslogtreecommitdiff
path: root/brep/handler/submit/submit-pub.in
blob: ea12a297d9e58662b73e8ad4e36d332c3de58f72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/usr/bin/env bash

# file      : brep/handler/submit/submit-pub.in
# license   : MIT; see accompanying LICENSE file

# Package submission handler with direct repository publishing.
#
# The overall idea behind this handler is to directly add the package to a
# private/trusted (unsigned) pkg repository with a simple structure (no
# sections). Upon successful execution of this handler no additional steps are
# required.
#
# Specifically, the handler performs the following steps:
#
# - Lock the repository directory for the duraton of the package submission.
#
# - Check for the package duplicate.
#
# - Create the new repository as a hardlink-copy of the current one.
#
# - Remove any package revisions, if present.
#
# - Validate and add the package archive to the new repository (with project
#   subdirectory).
#
# - Re-generate the new repository without signing.
#
# - Verify that the new repository is loadable into the brep package database.
#
# - Atomically switch the repository symlink to refer to the new repository.
#
# - Release the lock and remove the old repository.
#
# The repository argument (<repo>) should be an absolute path to a symbolic
# link to the pkg repository directory, with the archive and manifest files
# residing in its 1/ subdirectory. The base name of the <repo> path is used
# as a base for new repository directories.
#
# Unless the handler is called for testing, the loader program's absolute path
# and options should be specified so that the handler can verify that the
# package is loadable into the brep package database (this makes sure the
# package dependencies are resolvable, etc).
#
# Notes:
#
# - Filesystem entries that exist or are created in the data directory:
#
#   <pkg>-<ver>.tar.gz   saved by brep (could be other archives in the future)
#   request.manifest     created by brep
#   package.manifest     extracted by the handler
#   loadtab              created by the handler
#   result.manifest      saved by brep
#
# Options:
#
# --user <name>
#
#   Re-execute itself under the specified user.
#
#   Note that the repository can also be modified manually (e.g., to remove
#   packages). This option is normally specified to make sure that all the
#   repository filesystem entries belong to a single user, which, in
#   particular, can simplify their permissions handling (avoid extra ACLs,
#   etc).
#
#   Note that if this option is specified, then current user (normally the
#   user under which Apache2 is running) must be allowed to execute sudo
#   without a password, which is only recommended in private/trusted
#   environments.
#
# --result-url <url>
#
#   Result URL base for the response. If specified, the handler appends the
#   <package>/<version> to this value and includes the resulting URL in the
#   response message.
#
usage="usage: $0 [<options>] [<loader-path> <loader-options>] <repo> <dir>"

# Diagnostics.
#
verbose= #true

# The repository lock timeout (seconds).
#
rep_lock_timeout=60

trap "{ exit 1; }" ERR
set -o errtrace     # Trap in functions and subshells.
set -o pipefail     # Fail if any pipeline command fails.
shopt -s lastpipe   # Execute last pipeline command in the current shell.
shopt -s nullglob   # Expand no-match globs to nothing rather than themselves.

@import brep/handler/handler@
@import brep/handler/submit/submit@

# Parse the command line options and, while at it, compose the arguments array
# for potential re-execution under a different user.
#
user=
result_url=

scr_exe="$(realpath "${BASH_SOURCE[0]}")"
scr_dir="$(dirname "$scr_exe")"

args=("$scr_exe")

while [ "$#" -gt 0 ]; do
  case $1 in
    --user)
      shift
      user="$1"
      shift
      ;;
    --result-url)
      args+=("$1")
      shift
      result_url="${1%/}"
      args+=("$1")
      shift
      ;;
    *)
      break; # The end of options is encountered.
      ;;
  esac
done

loader_args=() # The loader path and options.

# Assume all the remaining arguments except for the last two (repository
# symlink and data directory) as the loader program path and arguments.
#
while [ "$#" -gt 2 ]; do
  loader_args+=("$1")
  args+=("$1")
  shift
done

if [ "$#" -ne 2 ]; then
  error "$usage"
fi

# pkg repository symlink.
#
repo="${1%/}"
shift

if [ -z "$repo" ]; then
  error "$usage"
fi

# Submission data directory.
#
data_dir="${1%/}"
shift

if [ -z "$data_dir" ]; then
  error "$usage"
fi

# Re-execute itself under a different user, if requested.
#
if [ -n "$user" ]; then
  args+=("$repo" "$data_dir")

  # Compose the arguments string to pass to the su program, quoting empty
  # arguments as well as those that contain spaces. Note that here, for
  # simplicity, we assume that the arguments may not contain '"'.
  #
  as=
  for a in "${args[@]}"; do
    if [ -z "$a" -o -z "${a##* *}" ]; then
      a="\"$a\""
    fi
    if [ -n "$as" ]; then
      a=" $a"
    fi
    as="$as$a"
  done

  run exec sudo --non-interactive su -l "$user" -c "$as"
fi

# Check path presence (do it after user switch for permissions).
#
if [ ! -L "$repo" ]; then
  error "'$repo' does not exist or is not a symlink"
fi

if [ ! -d "$data_dir" ]; then
  error "'$data_dir' does not exist or is not a directory"
fi

reference="$(basename "$data_dir")"

# Parse the submission request manifest and obtain the archive path as well as
# the simulate value.
#
manifest_parser_start "$data_dir/request.manifest"

archive=
simulate=

while IFS=: read -ru "$manifest_parser_ofd" -d '' n v; do
  case "$n" in
    archive)  archive="$v"  ;;
    simulate) simulate="$v" ;;
  esac
done

manifest_parser_finish

if [ -z "$archive" ]; then
  error "archive manifest value expected"
fi

if [ -n "$simulate" -a "$simulate" != "success" ]; then
  exit_with_manifest 400 "unrecognized simulation outcome '$simulate'"
fi

m="$data_dir/package.manifest"
extract_package_manifest "$data_dir/$archive" "$m"

# Parse the package manifest and obtain the package name, version, and
# project.
#
manifest_parser_start "$m"

name=
version=
project=

while IFS=: read -ru "$manifest_parser_ofd" -d '' n v; do
  case "$n" in
    name)    name="$v"    ;;
    version) version="$v" ;;
    project) project="$v" ;;
  esac
done

manifest_parser_finish

if [ -z "$name" ]; then
  error "name manifest value expected"
fi

if [ -z "$version" ]; then
  error "version manifest value expected"
fi

if [ -z "$project" ]; then
  project="$name"
fi

if [ -n "$result_url" ]; then
  message_suffix=": $result_url/$name/$version"
else
  message_suffix=": $name/$version"
fi

# Open the reading file descriptor and lock the repository. Fail if unable to
# lock before timeout.
#
l="$repo.lock"
run touch "$l"
trace "+ exec {lfd}<$l"
exec {lfd}<"$l"

# Note that on the locking failure we don't suggest the user to try again,
# since the client program may suggest to re-try later for all server errors
# (as bdep-publish(1) does).
#
if ! run flock -w "$rep_lock_timeout" "$lfd"; then
  exit_with_manifest 503 "submission service is busy"
fi

repo_old="$(realpath "$repo")"                              # Old repo path.
repo_name="$(basename "$repo")-$(date "+%Y%m%d-%H%M%S-%N")" # New repo name.
repo_new="$(dirname "$repo_old")/$repo_name"                # New repo path.
repo_link="$repo_new.link"                                  # New repo symlink.

# On exit, remove the new repository symlink and directory, unless the link
# doesn't exist or the directory removal is canceled (for example, the new
# repository is made current).
#
function exit_trap ()
{
  if [ -L "$repo_link" ]; then
    run rm -r -f "$repo_link"
  fi

  if [ -n "$repo_new" -a -d "$repo_new" ]; then
    run rm -r -f "$repo_new"
  fi
}

trap exit_trap EXIT

# Check for the package duplicate (in all projects).
#
# Use <name>-<version>.* without .tar.gz in case we want to support more
# archive types later.
#
run pkg_find_archive "$name-$version.*" "$repo_old/1" | readarray -t p

if [ "${#p[@]}" -ne 0 ]; then
  n="${p[1]}"
  v="${p[2]}"

  trace "found: $n/$v in ${p[0]}"

  if [ "$n" == "$name" ]; then
    exit_with_manifest 422 "duplicate submission"
  else
    exit_with_manifest 422 "submission conflicts with $n/$v"
  fi
fi

# Copy the current repository using hardlinks.
#
# -r                 (recursive)
# -t                 (preserve timestamps)
# -O                 (omit dir timestamps)
# --link-dest        (hardlink files instead of copying)
#
# We also exclude the packages.manifest file that will be re-generated anyway.
#
run rsync -rtO --exclude 'packages.manifest' --link-dest="$repo_old" \
    "$repo_old/" "$repo_new"

# Remove the package version revision archives that may exist in the
# repository.
#
run pkg_find_archives "$name" "$version*" "$repo_new/1" | readarray -t arcs

for f in "${arcs[@]}"; do
  run rm "$f"
done

# Copy the archive rather than moving it since we may need it for
# troubleshooting. Note: the data and repository directories can be on
# different filesystems and so hardlinking could fail.
#
run mkdir -p "$repo_new/1/$project"
run cp "$data_dir/$archive" "$repo_new/1/$project"

# Create the new repository.
#
# Note that if bpkg-rep-create fails, we can't reliably distinguish if this is
# a user or internal error (broken package archive vs broken repository).
# Thus, we always treat is as a user error, providing the full error
# description in the response and assuming that the submitter can either fix
# the issue or report it to the repository maintainers. This again assumes
# private/trusted environment.
#
trace "+ bpkg rep-create '$repo_new/1' 2>&1"

if ! e="$(bpkg rep-create "$repo_new/1" 2>&1)"; then
  exit_with_manifest 400 "submitted archive is not a valid package
$e"
fi

# If requested, verify that the new repository is loadable into the package
# database and, as in the above case, treat the potential error as a user
# error.
#
if [ "${#loader_args[@]}" -ne 0 ]; then
  f="$data_dir/loadtab"
  echo "http://testrepo/1 private cache:$repo_new/1" >"$f"

  trace "+ ${loader_args[@]} '$f' 2>&1"

  if ! e="$("${loader_args[@]}" "$f" 2>&1)"; then

    # Sanitize the error message, removing the confusing lines.
    #
    e="$(run sed -re '/testrepo/d' <<<"$e")"
    exit_with_manifest 400 "unable to add package to repository
$e"
  fi
fi

# Finally, create the new repository symlink and replace the current symlink
# with it, unless we are simulating.
#
run ln -sf "$repo_name" "$repo_link"

if [ -z "$simulate" ]; then
  run mv -T "$repo_link" "$repo" # Switch the repository symlink atomically.

  # Now, when the repository link is switched, disable the new repository
  # removal.
  #
  # Note that we still can respond with an error status.  However, the
  # remaining operations are all cleanups and thus unlikely to fail.
  #
  repo_new=
fi

trace "+ exec {lfd}<&-"
exec {lfd}<&- # Close the file descriptor and unlock the repository.

# Remove the old repository, unless we are simulating.
#
# Note that if simulating, we leave the new repository directory/symlink
# removal to the exit trap (see above).
#
if [ -z "$simulate" ]; then
  run rm -r "$repo_old"

  what="published"
else
  what="simulated"
fi

run rm -r "$data_dir"

trace "package is $what$message_suffix"
exit_with_manifest 200 "package is published$message_suffix"