aboutsummaryrefslogtreecommitdiff
path: root/tests/rep-auth.test
blob: b2331aae7c31e1f94df0ad25559c9c89915274a9 (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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# file      : tests/rep-auth.test
# copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
# license   : MIT; see accompanying LICENSE file

.include common.test auth.test config.test remote.test

# There is no rep-auth command, and this testscript contains tests for various
# authentication scenarios throughout different stages of repositories
# preparation and consumption. Note that by that reason usage of $* is
# meaningless.
#

# Source repository:
#
# rep-auth
# |-- expired
# |   |-- foo-1.tar.gz
# |   |-- packages
# |   |-- repositories
# |   `-- signature
# `-- unsigned
#     |-- foo-1.tar.gz
#     `-- repositories

# Prepare repositories used by tests if running in the local mode.
#
+if ($remote != true)
  rc = $rep_create 2>!

  # Create the 'unsigned1' repository.
  #
  cp -r $src/unsigned $out/unsigned1
  $rc $out/unsigned1 &$out/unsigned1/packages

  # Create the 'unsigned2' repository. This is a copy of the just created
  # 'unsigned1' repository.
  #
  cp -r $out/unsigned1 $out/unsigned2

  # Create the 'signed' repository.
  #
  cp -r $src/unsigned $out/signed
  cat <<<$cert_manifest >+$out/signed/repositories
  $rc --key $key $out/signed &$out/signed/packages &$out/signed/signature

  # Create the 'name-mismatch' repository. Note that its certificate name
  # mismatches the repository location.
  #
  cp -r $src/unsigned $out/name-mismatch

  echo 'certificate: \'                    >+$out/name-mismatch/repositories
  cat  <<<$src_base/auth/mismatch-cert.pem >+$out/name-mismatch/repositories
  echo '\'                                 >+$out/name-mismatch/repositories

  $rc --key $key $out/name-mismatch &$out/name-mismatch/packages  \
                                    &$out/name-mismatch/signature

  # Create the 'expired' repository. This repository is "pre-created" and its
  # certificate is expired by now. So we just copy it from the source
  # directory.
  #
  cp -r $src/expired $out/expired

  # Create the 'sha256sum-mismatch' repository. This is a copy of the just
  # created 'signed' repository that has the sha256sum manifest value tampered.
  #
  cp -r $out/signed $out/sha256sum-mismatch

  v = 'd374c59b36fdbdbd0d4468665061d94fda9c6c687863dfe72b0bcc34ff9d5fb4'
  sed -i -e "s/^\(sha256sum: \).*\$/\\1$v/" $out/sha256sum-mismatch/signature

  # Create the 'signature-mismatch' repository. This is a copy of the just
  # created 'signed' repository that has the signature manifest value tampered.
  #
  cp -r $out/signed $out/signature-mismatch

  # Here we tamper the last signature line (the one of 76 chars length, without
  # spaces and terminated with '=').
  #
  v = 'mnBAsS529NUdNIQy8EB4si/UK26ICaMywbLeHDVvWOB+AsqZ5rj8VjGDamLbmUrDr3ru7BU1gJU='
  sed -i -e "s%^[^ ]{75}=\$%$v%" $out/signature-mismatch/signature
end

pkg_status += -d cfg
rep_add    += -d cfg 2>!
rep_fetch  += -d cfg

# Check if rep-fetch command was successfull or not.
#
fetched     = $pkg_status foo >'available 1 sys:?'
not_fetched = $pkg_status foo >'unknown'

sc = " " # Space character to append to here-document line when required.

: no-auth
:
: Test that local repositories do not require authentication by default.
:
{
  r = 1/signed
  +mkdir 1/
  +cp -r $src/unsigned $r
  +cat <<<$cert_manifest >+$r/repositories
  +$rep_create --key $key $r &$r/packages &$r/signature 2>!

  : rep-fetch
  :
  {
    $clone_root_cfg && $rep_add ../$r;

    $rep_fetch 2>>/~%EOE%
      %fetching .+/no-auth/signed%
      1 package(s) in 1 repository(s)
      EOE
  }

  : rep-info
  :
  $clone_root_cfg;
  $rep_info --cert-name ../$r >'name:build2.org'
}

: signed
:
{
  : rep-fetch
  :
  {
    +$clone_root_cfg && $rep_add $rep/signed
    rep_fetch += --auth all &?cfg/.bpkg/certs/***

    : no-auth
    :
    {
      $clone_cfg;

      $rep_fetch 2>>"EOE" != 0;
        fetching build2.org/rep-auth/signed
        warning: authenticity of the certificate for repository build2.org/rep-auth/signed cannot be established
        certificate is for build2.org, "Code Synthesis" <info@build2.org>
        certificate SHA256 fingerprint:
        $cert_fp
        trust this certificate? [y/n]$sc
        error: unable to read y/n answer from STDOUT
        EOE

      $not_fetched
    }

    : trust-fp
    :
    {
      $clone_cfg;

      $rep_fetch --trust $cert_fp 2>>EOE;
        fetching build2.org/rep-auth/signed
        1 package(s) in 1 repository(s)
        EOE

      $fetched
    }

    : trust-fp-no
    :
    {
      $clone_cfg;

      $rep_fetch --trust-no --trust $cert_fp 2>>EOE;
        fetching build2.org/rep-auth/signed
        1 package(s) in 1 repository(s)
        EOE

      $fetched
    }

    : trust-yes
    :
    {
      $clone_cfg;

      $rep_fetch --trust-yes 2>>EOE;
        fetching build2.org/rep-auth/signed
        1 package(s) in 1 repository(s)
        EOE

      $fetched
    }

    : trust-no
    :
    {
      $clone_cfg;

      $rep_fetch --trust-no 2>>EOE != 0;
        fetching build2.org/rep-auth/signed
        error: authenticity of the certificate for repository build2.org/rep-auth/signed cannot be established
        EOE

      $not_fetched
    }

    : trust-yes-no
    :
    {
      $clone_cfg;

      $rep_fetch --trust-yes --trust-no 2>>EOE != 0;
        fetching build2.org/rep-auth/signed
        error: --trust-yes and --trust-no are mutually exclusive
        EOE

      $not_fetched
    }

    : already-trusted
    :
    {
      $clone_cfg;

      $rep_fetch --trust-yes 2>>EOE;
        fetching build2.org/rep-auth/signed
        1 package(s) in 1 repository(s)
        EOE

      $rep_fetch 2>>EOE;
        fetching build2.org/rep-auth/signed
        1 package(s) in 1 repository(s)
        EOE

      $fetched;

      $rep_fetch --trust-no 2>>EOE;
        fetching build2.org/rep-auth/signed
        1 package(s) in 1 repository(s)
        EOE

      $fetched
    }
  }

  : rep-info
  :
  {
    rep_info += --cert-name --auth all $rep/signed

    : no-auth
    :
    $rep_info 2>>"EOE" != 0
      warning: authenticity of the certificate for repository build2.org/rep-auth/signed cannot be established
      certificate is for build2.org, "Code Synthesis" <info@build2.org>
      certificate SHA256 fingerprint:
      $cert_fp
      trust this certificate? [y/n]$sc
      error: unable to read y/n answer from STDOUT
      EOE

    : trust-fp
    :
    $rep_info --trust $cert_fp >'name:build2.org'

    : trust-yes
    :
    $rep_info --trust-yes >'name:build2.org'

    : trust-no
    :
    $rep_info --trust-no 2>>EOE != 0
      error: authenticity of the certificate for repository build2.org/rep-auth/signed cannot be established
      EOE

    : already-trusted
    :
    {
      $clone_root_cfg;
      rep_info += -d cfg;

      $rep_info --trust "$cert_fp" &cfg/.bpkg/certs/*** >'name:build2.org';
      $rep_info                                         >'name:build2.org'
    }
  }
}

: unsigned
:
{
  : rep-fetch
  :
  {
    +$clone_root_cfg && $rep_add $rep/unsigned1
    rep_fetch += --auth all

    : no-auth
    :
    {
      $clone_cfg;

      $rep_fetch 2>>~%EOE% != 0;
        fetching build2.org/rep-auth/unsigned1
        warning: repository build2.org/rep-auth/unsigned1 is unsigned
        %continue without authenticating repositories at .+\? \[y/n\] %
        error: unable to read y/n answer from STDOUT
        EOE

      $not_fetched
    }

    : trust-yes
    :
    {
      $clone_cfg;

      $rep_fetch --trust-yes 2>>EOE;
        fetching build2.org/rep-auth/unsigned1
        1 package(s) in 1 repository(s)
        EOE

      $fetched
    }

    : trust-no
    :
    {
      $clone_cfg;

      $rep_fetch --trust-no 2>>EOE != 0;
        fetching build2.org/rep-auth/unsigned1
        error: repository build2.org/rep-auth/unsigned1 is unsigned
        EOE

      $not_fetched
    }

    : already-trusted
    :
    {
      $clone_cfg;

      $rep_fetch --trust-yes 2>>EOE;
        fetching build2.org/rep-auth/unsigned1
        1 package(s) in 1 repository(s)
        EOE

      $rep_fetch 2>>EOE;
        fetching build2.org/rep-auth/unsigned1
        1 package(s) in 1 repository(s)
        EOE

      $fetched;

      $rep_fetch --trust-no 2>>EOE;
        fetching build2.org/rep-auth/unsigned1
        1 package(s) in 1 repository(s)
        EOE

      $fetched;

      $rep_add $rep/unsigned2;

      $rep_fetch 2>>EOE;
        fetching build2.org/rep-auth/unsigned1
        fetching build2.org/rep-auth/unsigned2
        1 package(s) in 2 repository(s)
        EOE

      $fetched
    }
  }

  : rep-info
  :
  {
    rep_info += --name --auth all $rep/unsigned1

    : no-auth
    :
    $rep_info 2>>~%EOE% != 0
      warning: repository build2.org/rep-auth/unsigned1 is unsigned
      %continue without authenticating repositories at .+\? \[y/n\] %
      error: unable to read y/n answer from STDOUT
      EOE

    : trust-yes
    :
    $rep_info --trust-yes >"build2.org/rep-auth/unsigned1 ($rep/unsigned1)"

    : trust-no
    :
    $rep_info --trust-no 2>>EOE != 0
      error: repository build2.org/rep-auth/unsigned1 is unsigned
      EOE

    : already-trusted
    :
    {
      $clone_root_cfg;
      rep_info += -d cfg;

      $rep_info --trust-yes >"build2.org/rep-auth/unsigned1 ($rep/unsigned1)";
      $rep_info             >"build2.org/rep-auth/unsigned1 ($rep/unsigned1)"
    }
  }
}

: faulty
:
{
  rep_info += --auth all --trust-yes

  : name-mismatch
  :
  $rep_info $rep/name-mismatch 2>>EOE != 0
    error: certificate name mismatch for repository build2.org/rep-auth/name-mismatch
      info: certificate name is build2.org/mismatched/name
    EOE

  : expired
  :
  $rep_info $rep/expired 2>>EOE != 0
    error: certificate for repository build2.org/rep-auth/expired has expired
    EOE

  : sha256sum-mismatch
  :
  $rep_info $rep/sha256sum-mismatch 2>>EOE != 0
    error: packages manifest file checksum mismatch for build2.org/rep-auth/sha256sum-mismatch
      info: try again
    EOE

  : signature-mismatch
  :
  $rep_info $rep/signature-mismatch 2>>~%EOE% != 0
    %.*
    %error: unable to authenticate repository build2.org/rep-auth/signature-mismatch%
    EOE

  : create-rep
  :
  {
    : no-email
    :
    {
      cp -r $src/unsigned rep;

      echo 'certificate: \'                   >+rep/repositories;
      cat  <<<$src_base/auth/noemail-cert.pem >+rep/repositories;
      echo '\'                                >+rep/repositories;

      $rep_create --key $key rep &rep/packages 2>>/EOE != 0
        adding foo 1
        error: invalid certificate for rep/: no email
        EOE
    }

    : expired
    :
    {
      cp -r $src/unsigned rep;

      echo 'certificate: \'                   >+rep/repositories;
      cat  <<<$src_base/auth/expired-cert.pem >+rep/repositories;
      echo '\'                                >+rep/repositories;

      $rep_create --key $key rep &rep/packages 2>>/EOE != 0
        adding foo 1
        error: certificate for repository rep/ has expired
        EOE
    }
  }
}