aboutsummaryrefslogtreecommitdiff
path: root/tests/rep-auth.test
blob: 1fb7d9782f02258f9c12595749b995061e5deec1 (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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# 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 'self-match' repository. Note that its certificate name is
  # the '*build2.org' wildcard (matches build2.org and any single-level
  # subdomain).
  #
  cp -r $src/unsigned $out/self-match

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

  $rc --key $key $out/self-match &$out/self-match/packages \
                                 &$out/self-match/signature

  # Create the 'self-any-match' repository. Note that its certificate name is
  # the '**build2.org' wildcard (matches build2.org and any subdomain).
  #
  cp -r $src/unsigned $out/self-any-match

  echo 'certificate: \'                    >+$out/self-any-match/repositories
  cat  <<<$src_base/auth/self-any-cert.pem >+$out/self-any-match/repositories
  echo '\'                                 >+$out/self-any-match/repositories

  $rc --key $key $out/self-any-match &$out/self-any-match/packages \
                                     &$out/self-any-match/signature

  # Create the 'subdomain-match' repository. Note that its certificate name is
  # the '*.build2.org' wildcard (matches any single-level subdomain of
  # build2.org).
  #
  cp -r $src/unsigned $out/subdomain-match

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

  $rc --key $key $out/subdomain-match &$out/subdomain-match/packages \
                                      &$out/subdomain-match/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 pkg:build2.org/rep-auth/signed
        warning: authenticity of the certificate for repository pkg: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 pkg: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 pkg: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 pkg: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 pkg:build2.org/rep-auth/signed
        error: authenticity of the certificate for repository pkg: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 pkg: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 pkg:build2.org/rep-auth/signed
        1 package(s) in 1 repository(s)
        EOE

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

      $fetched;

      $rep_fetch --trust-no 2>>EOE;
        fetching pkg: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 pkg: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 pkg: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/** >>EOO;
        name:build2.org
        EOO

      $rep_info >'name:build2.org'
    }
  }

  : subdomain-wildcard
  :
  {
    rep_info += --auth all --trust-yes --cert-name

    : self
    :
    {
      : exact
      :
      $rep_info $rep/self-match >'name:*build2.org'

      : subdomain
      :
      if ($remote != true)
      {
        : first-level
        :
        {
          r = $canonicalize([dir_path] $~/pkg/1/a.build2.org/);
          mkdir -p $r;
          cp -r $rep/self-match $r;

          $rep_info $r/self-match >'name:*build2.org'
        }

        : second-level
        :
        {
          r = $canonicalize([dir_path] $~/pkg/1/b.a.build2.org/);
          mkdir -p $r;
          cp -r $rep/self-match $r;

          $rep_info $r/self-match 2>>EOE != 0
            error: certificate name mismatch for repository pkg:b.a.build2.org/self-match
              info: certificate name is *build2.org
            EOE
        }
      }
    }

    : self-any
    :
    {
      : exact
      :
      $rep_info $rep/self-any-match >'name:**build2.org'

      : subdomain
      :
      if ($remote != true)
      {
        : first-level
        :
        {
          r = $canonicalize([dir_path] $~/pkg/1/a.build2.org/);
          mkdir -p $r;
          cp -r $rep/self-any-match $r;

          $rep_info $r/self-any-match >'name:**build2.org'
        }

        : second-level
        :
        {
          r = $canonicalize([dir_path] $~/pkg/1/b.a.build2.org/);
          mkdir -p $r;
          cp -r $rep/self-any-match $r;

          $rep_info $r/self-any-match >'name:**build2.org'
        }
      }
    }

    : subdomain
    :
    {
      : exact
      :
      $rep_info $rep/subdomain-match 2>>EOE != 0
        error: certificate name mismatch for repository pkg:build2.org/rep-auth/subdomain-match
          info: certificate name is *.build2.org
        EOE

      : subdomain
      :
      if ($remote != true)
      {
        : first-level
        :
        {
          r = $canonicalize([dir_path] $~/pkg/1/a.build2.org/);
          mkdir -p $r;
          cp -r $rep/subdomain-match $r;

          $rep_info $r/subdomain-match >'name:*.build2.org'
        }

        : second-level
        :
        {
          r = $canonicalize([dir_path] $~/pkg/1/b.a.build2.org/);
          mkdir -p $r;
          cp -r $rep/subdomain-match $r;

          $rep_info $r/subdomain-match 2>>EOE != 0
            error: certificate name mismatch for repository pkg:b.a.build2.org/subdomain-match
              info: certificate name is *.build2.org
            EOE
        }
      }
    }
  }
}

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

    : no-auth
    :
    {
      $clone_cfg;

      $rep_fetch 2>>~%EOE% != 0;
        fetching pkg:build2.org/rep-auth/unsigned1
        warning: repository pkg: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 pkg: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 pkg:build2.org/rep-auth/unsigned1
        error: repository pkg:build2.org/rep-auth/unsigned1 is unsigned
        EOE

      $not_fetched
    }

    : already-trusted
    :
    {
      $clone_cfg;

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

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

      $fetched;

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

      $fetched;

      $rep_add $rep/unsigned2;

      $rep_fetch 2>>EOE;
        fetching pkg:build2.org/rep-auth/unsigned1
        fetching pkg: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 pkg: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 >>"EOO"
      pkg:build2.org/rep-auth/unsigned1 ($rep/unsigned1)
      EOO

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

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

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

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

  : name-mismatch
  :
  $rep_info $rep/name-mismatch 2>>EOE != 0
    error: certificate name mismatch for repository pkg: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 pkg: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 pkg: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 pkg: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
    }
  }
}