summaryrefslogtreecommitdiffstats
path: root/test/modules/md/test_310_conf_store.py
blob: d56790bb1fbd5c73656591b1cffe70ea52794c11 (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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
# test mod_md basic configurations
import time

import pytest
import os

from .md_conf import MDConf
from .md_env import MDTestEnv

SEC_PER_DAY = 24 * 60 * 60
MS_PER_DAY = SEC_PER_DAY * 1000
NS_PER_DAY = MS_PER_DAY * 1000


@pytest.mark.skipif(condition=not MDTestEnv.has_a2md(), reason="no a2md available")
@pytest.mark.skipif(condition=not MDTestEnv.has_acme_server(),
                    reason="no ACME test server configured")
class TestConf:

    @pytest.fixture(autouse=True, scope='class')
    def _class_scope(self, env, acme):
        acme.start(config='default')
        env.check_acme()

    @pytest.fixture(autouse=True, scope='function')
    def _method_scope(self, env, request):
        env.clear_store()
        self.test_domain = env.get_request_domain(request)

    # test case: no md definitions in config
    def test_md_310_001(self, env):
        MDConf(env, text="").install()
        assert env.apache_restart() == 0
        r = env.a2md(["list"])
        assert 0 == len(r.json["output"])

    # test case: add md definitions on empty store
    @pytest.mark.parametrize("confline,dns_lists,md_count", [
        ("MDomain testdomain.org www.testdomain.org mail.testdomain.org", 
            [["testdomain.org", "www.testdomain.org", "mail.testdomain.org"]], 1),
        ("""MDomain testdomain.org www.testdomain.org mail.testdomain.org
            MDomain testdomain2.org www.testdomain2.org mail.testdomain2.org""", 
            [["testdomain.org", "www.testdomain.org", "mail.testdomain.org"],
             ["testdomain2.org", "www.testdomain2.org", "mail.testdomain2.org"]], 2)
    ])
    def test_md_310_100(self, env, confline, dns_lists, md_count):
        MDConf(env, text=confline).install()
        assert env.apache_restart() == 0
        for i in range(0, len(dns_lists)):
            env.check_md(dns_lists[i], state=1)
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: add managed domains as separate steps
    def test_md_310_101(self, env):
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        env.check_md(["testdomain.org", "www.testdomain.org", "mail.testdomain.org"], state=1)
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            MDomain testdomain2.org www.testdomain2.org mail.testdomain2.org
            """).install()
        assert env.apache_restart() == 0
        env.check_md(["testdomain.org", "www.testdomain.org", "mail.testdomain.org"], state=1)
        env.check_md(["testdomain2.org", "www.testdomain2.org", "mail.testdomain2.org"], state=1)
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: add dns to existing md
    def test_md_310_102(self, env):
        assert env.a2md(["add", "testdomain.org", "www.testdomain.org"]).exit_code == 0
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        env.check_md(["testdomain.org", "www.testdomain.org", "mail.testdomain.org"], state=1)
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: add new md definition with acme url, acme protocol, acme agreement
    def test_md_310_103(self, env):
        MDConf(env, text="""
            MDCertificateAuthority http://acme.test.org:4000/directory
            MDCertificateProtocol ACME
            MDCertificateAgreement http://acme.test.org:4000/terms/v1

            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """, local_ca=False).install()
        assert env.apache_restart() == 0
        name = "testdomain.org"
        env.check_md([name, "www.testdomain.org", "mail.testdomain.org"], state=1,
                     ca="http://acme.test.org:4000/directory", protocol="ACME",
                     agreement="http://acme.test.org:4000/terms/v1")
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: add to existing md: acme url, acme protocol
    def test_md_310_104(self, env):
        name = "testdomain.org"
        MDConf(env, local_ca=False, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        env.check_md([name, "www.testdomain.org", "mail.testdomain.org"], state=1,
                     ca="https://acme-v02.api.letsencrypt.org/directory", protocol="ACME")
        MDConf(env, local_ca=False, text="""
            MDCertificateAuthority http://acme.test.org:4000/directory
            MDCertificateProtocol ACME
            MDCertificateAgreement http://acme.test.org:4000/terms/v1

            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        env.check_md([name, "www.testdomain.org", "mail.testdomain.org"], state=1,
                     ca="http://acme.test.org:4000/directory", protocol="ACME",
                     agreement="http://acme.test.org:4000/terms/v1")
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: add new md definition with server admin
    def test_md_310_105(self, env):
        MDConf(env, admin="admin@testdomain.org", text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        name = "testdomain.org"
        env.check_md([name, "www.testdomain.org", "mail.testdomain.org"], state=1,
                     contacts=["mailto:admin@testdomain.org"])
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: add to existing md: server admin
    def test_md_310_106(self, env):
        name = "testdomain.org"
        assert env.a2md(["add", name, "www.testdomain.org", "mail.testdomain.org"]).exit_code == 0
        MDConf(env, admin="admin@testdomain.org", text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        env.check_md([name, "www.testdomain.org", "mail.testdomain.org"], state=1,
                     contacts=["mailto:admin@testdomain.org"])
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: assign separate contact info based on VirtualHost
    def test_md_310_107(self, env):
        MDConf(env, admin="", text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            MDomain testdomain2.org www.testdomain2.org mail.testdomain2.org

            <VirtualHost *:12346>
                ServerName testdomain.org
                ServerAlias www.testdomain.org
                ServerAdmin mailto:admin@testdomain.org
            </VirtualHost>

            <VirtualHost *:12346>
                ServerName testdomain2.org
                ServerAlias www.testdomain2.org
                ServerAdmin mailto:admin@testdomain2.org
            </VirtualHost>
            """).install()
        assert env.apache_restart() == 0
        name1 = "testdomain.org"
        name2 = "testdomain2.org"
        env.check_md([name1, "www." + name1, "mail." + name1], state=1, contacts=["mailto:admin@" + name1])
        env.check_md([name2, "www." + name2, "mail." + name2], state=1, contacts=["mailto:admin@" + name2])

    # test case: normalize names - lowercase
    def test_md_310_108(self, env):
        MDConf(env, text="""
            MDomain testdomain.org WWW.testdomain.org MAIL.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        env.check_md(["testdomain.org", "www.testdomain.org", "mail.testdomain.org"], state=1)
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: default drive mode - auto
    def test_md_310_109(self, env):
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['renew-mode'] == 1
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: drive mode manual
    def test_md_310_110(self, env):
        MDConf(env, text="""
            MDRenewMode manual
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['renew-mode'] == 0
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: drive mode auto
    def test_md_310_111(self, env):
        MDConf(env, text="""
            MDRenewMode auto
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['renew-mode'] == 1
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: drive mode always
    def test_md_310_112(self, env):
        MDConf(env, text="""
            MDRenewMode always
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['renew-mode'] == 2

    # test case: renew window - 14 days
    def test_md_310_113a(self, env):
        MDConf(env, text="""
            MDRenewWindow 14d
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['renew-window'] == '14d'
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: renew window - 10 percent
    def test_md_310_113b(self, env):
        MDConf(env, text="""
            MDRenewWindow 10%
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['renew-window'] == '10%'
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: ca challenge type - http-01
    def test_md_310_114(self, env):
        MDConf(env, text="""
            MDCAChallenges http-01
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['ca']['challenges'] == ['http-01']
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: ca challenge type - http-01
    def test_md_310_115(self, env):
        MDConf(env, text="""
            MDCAChallenges tls-alpn-01
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['ca']['challenges'] == ['tls-alpn-01']
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: ca challenge type - all
    def test_md_310_116(self, env):
        MDConf(env, text="""
            MDCAChallenges http-01 tls-alpn-01
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['ca']['challenges'] == ['http-01', 'tls-alpn-01']
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: automatically collect md names from vhost config
    def test_md_310_117(self, env):
        conf = MDConf(env, text="""
            MDMember auto
            MDomain testdomain.org
            """)
        conf.add_vhost(port=12346, domains=[
            "testdomain.org", "test.testdomain.org", "mail.testdomain.org",
        ], with_ssl=True)
        conf.install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['domains'] == \
               ['testdomain.org', 'test.testdomain.org', 'mail.testdomain.org']

    # add renew window to existing md
    def test_md_310_118(self, env):
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        MDConf(env, text="""
            MDRenewWindow 14d
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        stat = env.get_md_status("testdomain.org")
        assert stat['renew-window'] == '14d'
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: set RSA key length 2048
    def test_md_310_119(self, env):
        MDConf(env, text="""
            MDPrivateKeys RSA 2048
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['privkey'] == {
            "type": "RSA",
            "bits": 2048
        }
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: set RSA key length 4096
    def test_md_310_120(self, env):
        MDConf(env, text="""
            MDPrivateKeys RSA 4096
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['privkey'] == {
            "type": "RSA",
            "bits": 4096
        }
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: require HTTPS
    def test_md_310_121(self, env):
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            MDRequireHttps temporary
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['require-https'] == "temporary"
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045",  # No VirtualHost matches Managed Domain
                "AH10105"   # no domain match
            ]
        )

    # test case: require OCSP stapling
    def test_md_310_122(self, env):
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            MDMustStaple on
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['must-staple'] is True
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: remove managed domain from config
    def test_md_310_200(self, env):
        dns_list = ["testdomain.org", "www.testdomain.org", "mail.testdomain.org"]
        env.a2md(["add"] + dns_list)
        env.check_md(dns_list, state=1)
        conf = MDConf(env,)
        conf.install()
        assert env.apache_restart() == 0
        # check: md stays in store
        env.check_md(dns_list, state=1)

    # test case: remove alias DNS from managed domain
    def test_md_310_201(self, env):
        dns_list = ["testdomain.org", "test.testdomain.org", "www.testdomain.org", "mail.testdomain.org"]
        env.a2md(["add"] + dns_list)
        env.check_md(dns_list, state=1)
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        # check: DNS has been removed from md in store
        env.check_md(["testdomain.org", "www.testdomain.org", "mail.testdomain.org"], state=1)
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: remove primary name from managed domain
    def test_md_310_202(self, env):
        dns_list = ["name.testdomain.org", "testdomain.org", "www.testdomain.org", "mail.testdomain.org"]
        env.a2md(["add"] + dns_list)
        env.check_md(dns_list, state=1)
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        # check: md overwrite previous name and changes name
        env.check_md(["testdomain.org", "www.testdomain.org", "mail.testdomain.org"],
                     md="testdomain.org", state=1)
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: remove one md, keep another
    def test_md_310_203(self, env):
        dns_list1 = ["greenbytes2.de", "www.greenbytes2.de", "mail.greenbytes2.de"]
        dns_list2 = ["testdomain.org", "www.testdomain.org", "mail.testdomain.org"]
        env.a2md(["add"] + dns_list1)
        env.a2md(["add"] + dns_list2)
        env.check_md(dns_list1, state=1)
        env.check_md(dns_list2, state=1)
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        # all mds stay in store
        env.check_md(dns_list1, state=1)
        env.check_md(dns_list2, state=1)
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: remove ca info from md, should switch over to new defaults
    def test_md_310_204(self, env):
        name = "testdomain.org"
        MDConf(env, local_ca=False, text="""
            MDCertificateAuthority http://acme.test.org:4000/directory
            MDCertificateProtocol ACME
            MDCertificateAgreement http://acme.test.org:4000/terms/v1

            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        # setup: sync with ca info removed
        MDConf(env, local_ca=False, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        env.check_md([name, "www.testdomain.org", "mail.testdomain.org"], state=1,
                     ca="https://acme-v02.api.letsencrypt.org/directory", protocol="ACME")
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: remove server admin from md
    def test_md_310_205(self, env):
        name = "testdomain.org"
        MDConf(env, admin="admin@testdomain.org", text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        # setup: sync with admin info removed
        MDConf(env, admin="", text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        # check: md stays the same with previous admin info
        env.check_md([name, "www.testdomain.org", "mail.testdomain.org"], state=1,
                     contacts=["mailto:admin@testdomain.org"])
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: remove renew window from conf -> fallback to default
    def test_md_310_206(self, env):
        MDConf(env, text="""
            MDRenewWindow 14d
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['renew-window'] == '14d'
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        # check: renew window not set
        assert env.a2md(["list"]).json['output'][0]['renew-window'] == '33%'
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: remove drive mode from conf -> fallback to default (auto)
    @pytest.mark.parametrize("renew_mode,exp_code", [
        ("manual", 0), 
        ("auto", 1), 
        ("always", 2)
    ])
    def test_md_310_207(self, env, renew_mode, exp_code):
        MDConf(env, text="""
            MDRenewMode %s
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """ % renew_mode).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['renew-mode'] == exp_code
        #
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['renew-mode'] == 1
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: remove challenges from conf -> fallback to default (not set)
    def test_md_310_208(self, env):
        MDConf(env, text="""
            MDCAChallenges http-01
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['ca']['challenges'] == ['http-01']
        #
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert 'challenges' not in env.a2md(["list"]).json['output'][0]['ca']
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: specify RSA key
    @pytest.mark.parametrize("key_size", ["2048", "4096"])
    def test_md_310_209(self, env, key_size):
        MDConf(env, text="""
            MDPrivateKeys RSA %s
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """ % key_size).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['privkey']['type'] == "RSA"
        #
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert "privkey" not in env.a2md(["list"]).json['output'][0]
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: require HTTPS
    @pytest.mark.parametrize("mode", ["temporary", "permanent"])
    def test_md_310_210(self, env, mode):
        MDConf(env, text="""
            <MDomainSet testdomain.org>
                MDMember www.testdomain.org mail.testdomain.org
                MDRequireHttps %s
            </MDomainSet>
            """ % mode).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['require-https'] == mode, \
            "Unexpected HTTPS require mode in store. config: {}".format(mode)
        #
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert "require-https" not in env.a2md(["list"]).json['output'][0], \
            "HTTPS require still persisted in store. config: {}".format(mode)
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045",  # No VirtualHost matches Managed Domain
                "AH10105",  # MDomain does not match any vhost
            ]
        )

    # test case: require OCSP stapling
    def test_md_310_211(self, env):
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            MDMustStaple on
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['must-staple'] is True
        #
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['must-staple'] is False
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: reorder DNS names in md definition
    def test_md_310_300(self, env):
        dns_list = ["testdomain.org", "mail.testdomain.org", "www.testdomain.org"]
        env.a2md(["add"] + dns_list)
        env.check_md(dns_list, state=1)
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        # check: dns list changes
        env.check_md(["testdomain.org", "www.testdomain.org", "mail.testdomain.org"], state=1)
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: move DNS from one md to another
    def test_md_310_301(self, env):
        env.a2md(["add", "testdomain.org", "www.testdomain.org", "mail.testdomain.org", "mail.testdomain2.org"])
        env.a2md(["add", "testdomain2.org", "www.testdomain2.org"])
        env.check_md(["testdomain.org", "www.testdomain.org",
                      "mail.testdomain.org", "mail.testdomain2.org"], state=1)
        env.check_md(["testdomain2.org", "www.testdomain2.org"], state=1)        
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            MDomain testdomain2.org www.testdomain2.org mail.testdomain2.org
            """).install()
        assert env.apache_restart() == 0
        env.check_md(["testdomain.org", "www.testdomain.org", "mail.testdomain.org"], state=1)
        env.check_md(["testdomain2.org", "www.testdomain2.org", "mail.testdomain2.org"], state=1)
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: change ca info
    def test_md_310_302(self, env):
        name = "testdomain.org"
        MDConf(env, local_ca=False, text="""
            MDCertificateAuthority http://acme.test.org:4000/directory
            MDCertificateProtocol ACME
            MDCertificateAgreement http://acme.test.org:4000/terms/v1

            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        # setup: sync with changed ca info
        MDConf(env, local_ca=False, admin="webmaster@testdomain.org",
                  text="""
            MDCertificateAuthority http://somewhere.com:6666/directory
            MDCertificateProtocol ACME
            MDCertificateAgreement http://somewhere.com:6666/terms/v1

            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        # check: md stays the same with previous ca info
        env.check_md([name, "www.testdomain.org", "mail.testdomain.org"], state=1,
                     ca="http://somewhere.com:6666/directory", protocol="ACME",
                     agreement="http://somewhere.com:6666/terms/v1")
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: change server admin
    def test_md_310_303(self, env):
        name = "testdomain.org"
        MDConf(env, admin="admin@testdomain.org", text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        # setup: sync with changed admin info
        MDConf(env, local_ca=False, admin="webmaster@testdomain.org", text="""
            MDCertificateAuthority http://somewhere.com:6666/directory
            MDCertificateProtocol ACME
            MDCertificateAgreement http://somewhere.com:6666/terms/v1

            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        # check: md stays the same with previous admin info
        env.check_md([name, "www.testdomain.org", "mail.testdomain.org"], state=1,
                     contacts=["mailto:webmaster@testdomain.org"])
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: change drive mode - manual -> auto -> always
    def test_md_310_304(self, env):
        MDConf(env, text="""
            MDRenewMode manual
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['renew-mode'] == 0
        # test case: drive mode auto
        MDConf(env, text="""
            MDRenewMode auto
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['renew-mode'] == 1
        # test case: drive mode always
        MDConf(env, text="""
            MDRenewMode always
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['renew-mode'] == 2
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: change config value for renew window, use various syntax alternatives
    def test_md_310_305(self, env):
        MDConf(env, text="""
            MDRenewWindow 14d
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        md = env.a2md(["list"]).json['output'][0]
        assert md['renew-window'] == '14d'
        MDConf(env, text="""
            MDRenewWindow 10
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        md = env.a2md(["list"]).json['output'][0]
        assert md['renew-window'] == '10d'
        MDConf(env, text="""
            MDRenewWindow 10%
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        md = env.a2md(["list"]).json['output'][0]
        assert md['renew-window'] == '10%'
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: change challenge types - http -> tls-sni -> all
    def test_md_310_306(self, env):
        MDConf(env, text="""
            MDCAChallenges http-01
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['ca']['challenges'] == ['http-01']
        # test case: drive mode auto
        MDConf(env, text="""
            MDCAChallenges tls-alpn-01
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['ca']['challenges'] == ['tls-alpn-01']
        # test case: drive mode always
        MDConf(env, text="""
            MDCAChallenges http-01 tls-alpn-01
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['ca']['challenges'] == ['http-01', 'tls-alpn-01']
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case:  RSA key length: 4096 -> 2048 -> 4096
    def test_md_310_307(self, env):
        MDConf(env, text="""
            MDPrivateKeys RSA 4096
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['privkey'] == {
            "type": "RSA",
            "bits": 4096
        }
        MDConf(env, text="""
            MDPrivateKeys RSA 2048
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['privkey'] == {
            "type": "RSA",
            "bits": 2048
        }
        MDConf(env, text="""
            MDPrivateKeys RSA 4096
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['privkey'] == {
            "type": "RSA",
            "bits": 4096
        }
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: change HTTPS require settings on existing md
    def test_md_310_308(self, env):
        # setup: nothing set
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert "require-https" not in env.a2md(["list"]).json['output'][0]
        # test case: temporary redirect
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            MDRequireHttps temporary
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['require-https'] == "temporary"
        # test case: permanent redirect
        MDConf(env, text="""
            <MDomainSet testdomain.org>
                MDMember www.testdomain.org mail.testdomain.org
                MDRequireHttps permanent
            </MDomainSet>
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['require-https'] == "permanent"
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045",  # No VirtualHost matches Managed Domain
                "AH10105",  # MDomain matches no vhost
            ]
        )

    # test case: change OCSP stapling settings on existing md
    def test_md_310_309(self, env):
        # setup: nothing set
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['must-staple'] is False
        # test case: OCSP stapling on
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            MDMustStaple on
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['must-staple'] is True
        # test case: OCSP stapling off
        MDConf(env, text="""
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            MDMustStaple off
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'][0]['must-staple'] is False
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: change renew window parameter
    @pytest.mark.parametrize("window", [
        "0%", "33d", "40%"
    ])
    def test_md_310_310(self, env, window):
        # non-default renewal setting
        domain = self.test_domain
        conf = MDConf(env, admin="admin@" + domain)
        conf.start_md([domain])
        conf.add_drive_mode("manual")
        conf.add_renew_window(window)
        conf.end_md()
        conf.add_vhost(domains=domain)
        conf.install()
        assert env.apache_restart() == 0
        stat = env.get_md_status(domain)
        assert stat["renew-window"] == window

    # test case: add dns name on existing valid md
    def test_md_310_400(self, env):
        # setup: create complete md in store
        domain = self.test_domain
        name = "www." + domain
        assert env.a2md(["add", name, "test1." + domain]).exit_code == 0
        assert env.a2md(["update", name, "contacts", "admin@" + name]).exit_code == 0
        assert env.a2md(["update", name, "agreement", env.acme_tos]).exit_code == 0
        MDConf(env).install()
        assert env.apache_restart() == 0

        # setup: drive it
        r = env.a2md(["-v", "drive", name])
        assert r.exit_code == 0, "drive not successful: {0}".format(r.stderr)
        assert env.a2md(["list", name]).json['output'][0]['state'] == env.MD_S_COMPLETE

        # remove one domain -> status stays COMPLETE
        assert env.a2md(["update", name, "domains", name]).exit_code == 0
        assert env.a2md(["list", name]).json['output'][0]['state'] == env.MD_S_COMPLETE
        
        # add other domain -> status INCOMPLETE
        assert env.a2md(["update", name, "domains", name, "test2." + domain]).exit_code == 0
        assert env.a2md(["list", name]).json['output'][0]['state'] == env.MD_S_INCOMPLETE

    # test case: change ca info
    def test_md_310_401(self, env):
        # setup: create complete md in store
        domain = self.test_domain
        name = "www." + domain
        assert env.a2md(["add", name]).exit_code == 0
        assert env.a2md(["update", name, "contacts", "admin@" + name]).exit_code == 0
        assert env.a2md(["update", name, "agreement", env.acme_tos]).exit_code == 0
        assert env.apache_restart() == 0
        # setup: drive it
        assert env.a2md(["drive", name]).exit_code == 0
        assert env.a2md(["list", name]).json['output'][0]['state'] == env.MD_S_COMPLETE
        # setup: change CA URL
        assert env.a2md(["update", name, "ca", env.acme_url]).exit_code == 0
        # check: state stays COMPLETE
        assert env.a2md(["list", name]).json['output'][0]['state'] == env.MD_S_COMPLETE

    # test case: change the store dir
    def test_md_310_500(self, env):
        MDConf(env, text="""
            MDStoreDir md-other
            MDomain testdomain.org www.testdomain.org mail.testdomain.org
            """).install()
        assert env.apache_restart() == 0
        assert env.a2md(["list"]).json['output'] == []
        env.set_store_dir("md-other")
        env.check_md(["testdomain.org", "www.testdomain.org", "mail.testdomain.org"], state=1)
        env.clear_store()
        env.set_store_dir_default()
        env.httpd_error_log.ignore_recent(
            lognos = [
                "AH10045"   # No VirtualHost matches Managed Domain
            ]
        )

    # test case: place an unexpected file into the store, check startup survival, see #218
    def test_md_310_501(self, env):
        # setup: create complete md in store
        domain = self.test_domain
        conf = MDConf(env, admin="admin@" + domain)
        conf.start_md([domain])
        conf.end_md()
        conf.add_vhost(domains=[domain])
        conf.install()
        assert env.apache_restart() == 0
        # add a file at top level
        assert env.await_completion([domain])
        fpath = os.path.join(env.store_domains(), "wrong.com")
        with open(fpath, 'w') as fd:
            fd.write("this does not belong here\n")
        assert env.apache_restart() == 0

    # test case: add external account binding
    def test_md_310_601(self, env):
        domain = self.test_domain
        # directly set
        conf = MDConf(env, admin="admin@" + domain)
        conf.start_md([domain])
        conf.add_drive_mode("manual")
        conf.add("MDExternalAccountBinding k123 hash123")
        conf.end_md()
        conf.add_vhost(domains=domain)
        conf.install()
        assert env.apache_restart() == 0
        stat = env.get_md_status(domain)
        assert stat["eab"] == {'kid': 'k123', 'hmac': '***'}
        # eab inherited
        conf = MDConf(env, admin="admin@" + domain)
        conf.add("MDExternalAccountBinding k456 hash456")
        conf.start_md([domain])
        conf.add_drive_mode("manual")
        conf.end_md()
        conf.add_vhost(domains=domain)
        conf.install()
        assert env.apache_restart() == 0
        stat = env.get_md_status(domain)
        assert stat["eab"] == {'kid': 'k456', 'hmac': '***'}
        # override eab inherited
        conf = MDConf(env, admin="admin@" + domain)
        conf.add("MDExternalAccountBinding k456 hash456")
        conf.start_md([domain])
        conf.add_drive_mode("manual")
        conf.add("MDExternalAccountBinding none")
        conf.end_md()
        conf.add_vhost(domains=domain)
        conf.install()
        assert env.apache_restart() == 0
        stat = env.get_md_status(domain)
        assert "eab" not in stat