mirror of
https://github.com/caddyserver/caddy.git
synced 2026-05-26 16:52:40 -04:00
Compare commits
680 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ad3a83fb91 | |||
| 53c4d788d4 | |||
| d6bc9e0b5c | |||
| 54d1923ccb | |||
| c0f76e9ed4 | |||
| f259ed52bb | |||
| 8bac134f26 | |||
| 412dcc07d3 | |||
| 660c59b6f3 | |||
| 58e05cab15 | |||
| 10f85558ea | |||
| 98468af8b6 | |||
| 25f10511e7 | |||
| b6e96fa3c5 | |||
| 56013934a4 | |||
| 0b6f764356 | |||
| 050d6e0aeb | |||
| 0bcd02d5f6 | |||
| c82fe91104 | |||
| f9b42c3772 | |||
| aaf6794b31 | |||
| 1498132ea3 | |||
| 7f9b1f43c9 | |||
| 5e729c1e85 | |||
| 0a14f97e49 | |||
| 9864b138fb | |||
| 3d18bc56b9 | |||
| 886ba84baa | |||
| a9267791c4 | |||
| ef0aaca0d6 | |||
| 6891f7f421 | |||
| 499ad6d182 | |||
| 8e6bc36084 | |||
| 58970cae92 | |||
| 9e760e2e0c | |||
| 4b4e99bdb2 | |||
| 57d27c1b58 | |||
| 693e9b5283 | |||
| b687d7b967 | |||
| f7be0ee101 | |||
| f6900fcf53 | |||
| ec86a2f7a3 | |||
| e7fbee8c82 | |||
| e84e19a04e | |||
| 4a223f5203 | |||
| af7321511c | |||
| 0be3d99543 | |||
| 3017b245c9 | |||
| 2e4c09155a | |||
| dcc98da4d2 | |||
| 3ab648382d | |||
| 40b193fb79 | |||
| d543ad1ffd | |||
| a8bb4a665a | |||
| 3a1e0dbf47 | |||
| 77a77c0219 | |||
| db62942d63 | |||
| dadd4b59b0 | |||
| d230b33007 | |||
| 0d13173071 | |||
| c3a82f53d5 | |||
| 30b6d1f47a | |||
| bc15b4b0e7 | |||
| e2535233bb | |||
| 00234c8ac2 | |||
| 6512832f9f | |||
| 3e3bb00265 | |||
| e4ce40f8ff | |||
| afca242111 | |||
| 7d229665ed | |||
| 22d8edb984 | |||
| 734acc776a | |||
| b4f1a71397 | |||
| d06d0e79f8 | |||
| a58f240d3e | |||
| 4b75f3e2f0 | |||
| b8dbecb841 | |||
| 134b805644 | |||
| c9b5e7f77b | |||
| 79cbe7bfd0 | |||
| 55b4c12e04 | |||
| 2196c92c0e | |||
| c2327161f7 | |||
| c5fffb4ac2 | |||
| dc4d147388 | |||
| 93c99f6734 | |||
| 4e9fbee1e2 | |||
| a9c7e94a38 | |||
| 3d616e8c6d | |||
| b82e22b459 | |||
| bf6a1b7538 | |||
| c7d6c4cbb9 | |||
| d0b608af31 | |||
| d9b1d46325 | |||
| c8f2834b51 | |||
| ab0455922a | |||
| c50094fc9d | |||
| d058dee11d | |||
| 09ba9e994e | |||
| be82cc7aca | |||
| 2bb8550a4c | |||
| a72acd21b0 | |||
| a6199cf814 | |||
| ceef70dbc5 | |||
| f5e104944e | |||
| 6b385a36f9 | |||
| 9b7cdfa2f2 | |||
| 78e381b29f | |||
| de490c7cad | |||
| bbad6931e3 | |||
| 5bd96a6ac2 | |||
| ac14b64e08 | |||
| 15c95e9d5b | |||
| bc447e307f | |||
| 87a1f228b4 | |||
| acbee94708 | |||
| 7ea5b2a818 | |||
| 186fdba916 | |||
| 7778912d4e | |||
| c921e08296 | |||
| ddbb234d91 | |||
| 0de51593a6 | |||
| 26d633baf8 | |||
| ff137d17d0 | |||
| 57a708d189 | |||
| 32aad90938 | |||
| 40b54434f3 | |||
| 1d0425b26f | |||
| 7557d1d922 | |||
| ff74a0aa09 | |||
| 599c81d753 | |||
| 741b0502ee | |||
| 7ca5921a87 | |||
| da4a759bad | |||
| 042abeb431 | |||
| eb891d4683 | |||
| 44e5e9e43f | |||
| bf380d00ab | |||
| 94035c1797 | |||
| b3f7ce34b4 | |||
| a79b4055e5 | |||
| 5a07156894 | |||
| bcb7a19cd3 | |||
| 6e6ce2be6b | |||
| 1b7ff5d76c | |||
| 93a7a45e7e | |||
| 1a7a78a1f2 | |||
| 1feb65952a | |||
| 66de438a98 | |||
| 850e1605df | |||
| af1ac9cd2e | |||
| 64a3218f5c | |||
| c634bbe9cc | |||
| 4b9849c792 | |||
| 80d7a356b3 | |||
| b4bfa29be2 | |||
| 6cadb60fa2 | |||
| 2e46c2ac1d | |||
| 249adc1c87 | |||
| e9dde23024 | |||
| 3fe2c73dd0 | |||
| 5333c3528b | |||
| 180ae0cc48 | |||
| a1c41210d3 | |||
| ecac03cdcb | |||
| c04d24cafa | |||
| 81ee34e962 | |||
| 78b5356f2b | |||
| 6f9b6ad78e | |||
| 4906b9357a | |||
| e90d751732 | |||
| dce81e85d5 | |||
| a1b417c832 | |||
| 5bf0adad87 | |||
| 8e5aafa5cd | |||
| c133153447 | |||
| ec14ccdd40 | |||
| f55b123d63 | |||
| 0eb0b60f47 | |||
| 5e5af50e64 | |||
| 9ee68c1bd5 | |||
| 789efa5dee | |||
| 8887adb027 | |||
| bcac2beee7 | |||
| 1e10f6f725 | |||
| c8b5a81607 | |||
| eead337324 | |||
| 7d5047c1f1 | |||
| 7f364c777a | |||
| b47af6ef04 | |||
| e81369e220 | |||
| e7457b43e4 | |||
| f376a38b25 | |||
| 749e55c738 | |||
| 24fda7514d | |||
| 3385856966 | |||
| f73f55dba7 | |||
| 012d235314 | |||
| 997e41deae | |||
| 0ffb2229b0 | |||
| a21d5a001f | |||
| a2119c09e9 | |||
| 062657d0d8 | |||
| b092061591 | |||
| 64f8b557b1 | |||
| 95c035060f | |||
| c4790d7f9d | |||
| 837cdc566d | |||
| be5f77e84d | |||
| cbb045a121 | |||
| c48fadc4a7 | |||
| 059fc32f00 | |||
| e2d964ea30 | |||
| 501da21f20 | |||
| 3336faf254 | |||
| 16f752125f | |||
| 0a5f7a677f | |||
| d3a0259944 | |||
| 5fda9610f9 | |||
| 3f2c3ecf85 | |||
| 907e2d8d3a | |||
| 33c70f418f | |||
| 2ebfda1ae9 | |||
| 2392478bd3 | |||
| a437206643 | |||
| a779e1b383 | |||
| 46ab93be51 | |||
| e0fc46a911 | |||
| 9f6393c64c | |||
| 105dac8c2a | |||
| 4ebf100f09 | |||
| f43fd6f388 | |||
| 84b906a248 | |||
| 403732c433 | |||
| f6d5ec2fd6 | |||
| 19a55d6aeb | |||
| bfbc459c0a | |||
| f70a7578fa | |||
| 51f125bd44 | |||
| d74913f871 | |||
| ce5a45db45 | |||
| e0a6a1efff | |||
| c1cd192ee7 | |||
| a056fcd7ba | |||
| 9e333c39da | |||
| 8a974a4f8f | |||
| 6bc87ea2ff | |||
| 1b1e625c20 | |||
| a10910f398 | |||
| ab32440b21 | |||
| e6c29ce081 | |||
| 68c5c71659 | |||
| 569ecdbd02 | |||
| c131339c5c | |||
| b6f51254ea | |||
| 124ba1ba71 | |||
| 1c6c7714a3 | |||
| 46d99aba85 | |||
| 9e16e80f3c | |||
| d882211080 | |||
| 42e140b1b2 | |||
| 4245ceb67d | |||
| 0bdb8aa82d | |||
| 191dc86f9e | |||
| 81e5318021 | |||
| b3d35a4995 | |||
| 2de7e14e1c | |||
| 885a9aaf48 | |||
| 69c914483d | |||
| 9d4ed3a323 | |||
| fbd6560976 | |||
| 238914d70b | |||
| e8ae80adca | |||
| 32c284b54a | |||
| 7c68809f4e | |||
| 6d25261c22 | |||
| 8848df9c5d | |||
| 89aa3a5ef3 | |||
| 05656a60b3 | |||
| 1e92258dd6 | |||
| 76913b19ff | |||
| 4c2da18841 | |||
| f9b54454a1 | |||
| 658772ff24 | |||
| 323ffd2076 | |||
| 2a8109468c | |||
| 94b712009a | |||
| 7b500e74b4 | |||
| ecd5eeab38 | |||
| b4cef492cc | |||
| e3c369d452 | |||
| c052162203 | |||
| 7f26a6b3e5 | |||
| b82db994f3 | |||
| aef8d4decc | |||
| 37718560c1 | |||
| 2aefe15686 | |||
| dbe164d98a | |||
| bc22102478 | |||
| f5db41ce1d | |||
| 77764714ad | |||
| 61642b766b | |||
| 3cf443f0fe | |||
| d4b2f1bcee | |||
| a17c3b568d | |||
| 74f5d66c48 | |||
| efe84497d7 | |||
| e4a22de9d1 | |||
| e6f6d3a476 | |||
| ef7f15f3a4 | |||
| 6e0e3e1537 | |||
| 53ececda21 | |||
| 637fd8f67b | |||
| 956f01163d | |||
| ff6ca577ec | |||
| 9017557169 | |||
| 3a1e81dbf6 | |||
| a8d45277ca | |||
| 1e218e1d2e | |||
| 4d0474e3b8 | |||
| d789596bc0 | |||
| 96bb365929 | |||
| 00e12aa918 | |||
| 2250920e1d | |||
| 42b7134ffa | |||
| 3903642aa7 | |||
| 03b5debd95 | |||
| 3f6283b385 | |||
| 45fb7202ac | |||
| 66783eb4d9 | |||
| 1455d6bb69 | |||
| 3401f91dbe | |||
| eb3955a960 | |||
| d21e88ae3a | |||
| a0a7c60cb9 | |||
| 7da9241fd7 | |||
| e68dbe9cf8 | |||
| bd357bf005 | |||
| aac1ccf12d | |||
| f35a7fa466 | |||
| 75f797debd | |||
| 1c8ea00828 | |||
| d63d5ae1ce | |||
| a6bc58153b | |||
| 911c8a371a | |||
| 87fbc0783a | |||
| f1c36680fc | |||
| a87f757fcc | |||
| 0018b9be0d | |||
| a48c6205b7 | |||
| 28a4159933 | |||
| 0d7fe36007 | |||
| f137b82227 | |||
| 2a127ac3d1 | |||
| 802f80c382 | |||
| 51f35ba03f | |||
| ad8d01cb66 | |||
| 5bf0a55df4 | |||
| ec309c6d52 | |||
| ce5a0934a8 | |||
| b54fa41239 | |||
| 427bbe99d0 | |||
| a8fdc0a998 | |||
| f6bb02b303 | |||
| 6722ae3a83 | |||
| edb362aa96 | |||
| 5376e5113e | |||
| ec3ac840cf | |||
| fbd00e4b53 | |||
| bafb562991 | |||
| ed678235a4 | |||
| cc63c5805e | |||
| 51e3fdba77 | |||
| 5ef76ff3e6 | |||
| 653a0d3f6b | |||
| 0aefa7b047 | |||
| 8c291298c9 | |||
| bf50d7010a | |||
| 8ec90f1c40 | |||
| 90284e8017 | |||
| 2772ede43c | |||
| c986110678 | |||
| 55e49ff5c8 | |||
| e2940c8c03 | |||
| bef80cd806 | |||
| e2c5c28597 | |||
| ab80ff4fd2 | |||
| 3366384d93 | |||
| 1ac6351705 | |||
| 160d199999 | |||
| d68cff8eb6 | |||
| 8f6f9865d4 | |||
| 58e83a811b | |||
| f0c0f38ba5 | |||
| 59071ea15d | |||
| 14f50d9dfb | |||
| 0bf2046da7 | |||
| 88a38bd00d | |||
| 4f64105fbb | |||
| 09432ba64d | |||
| ef54483249 | |||
| c2b91dbd65 | |||
| 8b6fdc04da | |||
| f0216967dc | |||
| b1bec8c899 | |||
| 3c9256a1be | |||
| 7846bc1e06 | |||
| 144b65cf99 | |||
| c8557dc00b | |||
| 1b453dd4fb | |||
| ebc278ec98 | |||
| 79f3af9927 | |||
| d8bcf5be4e | |||
| 38a83ca6f8 | |||
| 2b90cdba52 | |||
| 635f075f18 | |||
| e384f07a3c | |||
| 132525de3b | |||
| deedf8abb0 | |||
| 63bda6a0dc | |||
| b8a799df9f | |||
| a748151666 | |||
| c898a37f40 | |||
| 31fbcd7401 | |||
| 7e719157d9 | |||
| 6e9ac248dd | |||
| 5643dc3fb9 | |||
| 3d0e046238 | |||
| bac82073d0 | |||
| e7a5a3850f | |||
| aca7ef0d4c | |||
| 792fca40f1 | |||
| 9157051f45 | |||
| 4cff36d731 | |||
| a26f70a12b | |||
| 4afcdc49d1 | |||
| 7d7434c9ce | |||
| 53aa60afff | |||
| b0f8fc7aae | |||
| 03d853e2ec | |||
| 63afffc2e3 | |||
| 2d5498ee6f | |||
| 0a7721dcfe | |||
| c5197f5999 | |||
| 06ba006f9b | |||
| c6dec30535 | |||
| 3cfefeb0f7 | |||
| 4a641f6c6f | |||
| bd17eb205d | |||
| 1e480b818b | |||
| 96058538f0 | |||
| 6e0849d4c2 | |||
| b0d5c2c8ae | |||
| 12cc69ab7a | |||
| 349457cc1b | |||
| 6ea6f3ebe0 | |||
| 1438e4dbc8 | |||
| 4fc570711e | |||
| 99b8f44486 | |||
| 670b723e38 | |||
| 13781e67ab | |||
| 7a3d9d81fe | |||
| 95af4262a8 | |||
| 3db60e6cba | |||
| 7c28ecb5f4 | |||
| 9e28f60aab | |||
| b4f49e2962 | |||
| dd26875ffc | |||
| eda9a1b377 | |||
| 860cc6adfe | |||
| 8d038ca515 | |||
| 937ec34201 | |||
| 966d5e6b42 | |||
| b66099379d | |||
| c9fdff9976 | |||
| db4f1c0277 | |||
| b6e96d6f4a | |||
| b6686a54d8 | |||
| 97caf368ee | |||
| 385adf5d87 | |||
| c7efb0307d | |||
| e34d9f1244 | |||
| ef8a372a1c | |||
| 0fc47e8357 | |||
| 25d2b4bf29 | |||
| 023d702f30 | |||
| 6722426f1a | |||
| 3b9eae70c9 | |||
| aa9c3eb732 | |||
| fdfdc03339 | |||
| dadfe1933b | |||
| 85152679ce | |||
| a33e4b5426 | |||
| f197cec7f3 | |||
| be6daa5fd4 | |||
| fe27f9cf0c | |||
| b1d456d8ab | |||
| d16ede358a | |||
| c82c231ba7 | |||
| 3ee663dee1 | |||
| 8ec51bbede | |||
| bc453fa6ae | |||
| e3324aa6de | |||
| d55d50b3b3 | |||
| b95b87381a | |||
| b01bb275b3 | |||
| 309c1fec62 | |||
| b88e2b6a49 | |||
| 4217217bad | |||
| 1c5969b576 | |||
| 0ee4378227 | |||
| 9859ab8148 | |||
| 00e6b77fe4 | |||
| d4f249741e | |||
| 04f50a9759 | |||
| 4cd7ae35b3 | |||
| 24f34780b6 | |||
| 724b74d981 | |||
| 4940325844 | |||
| 744d04c258 | |||
| ecbc1f85c5 | |||
| 997ef522bc | |||
| 0279a57ac4 | |||
| c94f5bb7dd | |||
| 0afbab8667 | |||
| fc65320e9c | |||
| e385be9225 | |||
| 66863aad3b | |||
| c42bfaf31e | |||
| e2f913bb7f | |||
| 65a09524c3 | |||
| c6d6a775a1 | |||
| 4accf737a6 | |||
| ff19bddac5 | |||
| 584eba94a4 | |||
| 904f149e5b | |||
| 8b80a3201f | |||
| 68529e2f9e | |||
| 399eff415c | |||
| c054a818a1 | |||
| af5c148ed1 | |||
| 514eef33fe | |||
| 3860b235d0 | |||
| 6f73a358f4 | |||
| 6a14e2c2a8 | |||
| 2bc30bb780 | |||
| 28d870c193 | |||
| fb9d874fa9 | |||
| 6cea1f239d | |||
| 2ae8c11927 | |||
| e9b1d7dcb4 | |||
| bd9d796e6e | |||
| 246a31aacd | |||
| 0665a86eb7 | |||
| 3fdaf50785 | |||
| 19cc2bd3c3 | |||
| 705de11bef | |||
| 8a0fff58aa | |||
| 6f0f159ba5 | |||
| 6eafd4e82f | |||
| eda54c22a6 | |||
| 2c71fb116b | |||
| 724613a1be | |||
| 735c86658d | |||
| a2dae1d43f | |||
| efc0cc5e85 | |||
| 0bf2565c37 | |||
| 7bfe5b6c95 | |||
| 2a5599e2ad | |||
| c35820012b | |||
| 2d0f8831f8 | |||
| d7dbf85525 | |||
| 77f233a484 | |||
| ddd690de4c | |||
| 6004d3f779 | |||
| caca55e582 | |||
| c9049bdc24 | |||
| 21c00a3cd2 | |||
| 61b7002d26 | |||
| b1480eb52f | |||
| 5bc4777be9 | |||
| 3af15c0725 | |||
| 6db3615547 | |||
| 32cafbb630 | |||
| 003403ecbc | |||
| 5b48f784ae | |||
| d84a5d8427 | |||
| 7da32f493a | |||
| cb0d9838cb | |||
| d81a69ef16 | |||
| 99dcc10f31 | |||
| fa4cdde7d8 | |||
| d55c3b31eb | |||
| 6d03fb48f9 | |||
| b3bff13f7d | |||
| 7211101c52 | |||
| 90dba172cb | |||
| 4b10ae5ce6 | |||
| 1dfb11486e | |||
| 11a132d48b | |||
| 9dafa63933 | |||
| 21c1da101c | |||
| 7a99835dab | |||
| 7b0962ba4d | |||
| 2d1f7b9da8 | |||
| a285fe4129 | |||
| 97e61c16a3 | |||
| 83551edf3e | |||
| e18c373064 | |||
| 9a7756c6e4 | |||
| fdf2a77feb | |||
| a496308f6e | |||
| d5d7fb5954 | |||
| 996af0915d | |||
| 6c051cd27d | |||
| 9415feca7c | |||
| 881b826fb5 | |||
| 538ddb8587 | |||
| 69b5643130 | |||
| e5bbed1046 | |||
| 294910c68c | |||
| 8c5d00b2bc | |||
| aa20878887 | |||
| c1e5c09294 | |||
| ffc125d6f5 | |||
| 22055c5e0f | |||
| dfe802aed3 | |||
| 7a365af5df | |||
| 0cbf467b3f | |||
| bb67e19d7b | |||
| 1dc4ec2d77 | |||
| 452d4726f7 | |||
| 2a8a198568 | |||
| cc8fb488d3 | |||
| fae064262d | |||
| 9ee01dceac | |||
| 812278acd8 | |||
| c47ddbeffb | |||
| 483e31b978 | |||
| 41a682ddde | |||
| 7243454a96 | |||
| 3fb2c394d1 | |||
| 21de227fe9 | |||
| 62c9f2cf3e | |||
| bde3823b76 | |||
| 4df56c77e3 | |||
| cee5589b98 | |||
| 90c7b4b0a1 | |||
| aef560c7fc | |||
| 44536a7594 | |||
| ea7e4b4024 | |||
| ef6e53bb5f | |||
| 35e1d92d58 | |||
| dc9f4f13fc | |||
| 4c55d26f11 | |||
| d534162556 | |||
| 5bde8d705b | |||
| 7960b4259d | |||
| 2c91688f39 | |||
| 513e0240fd | |||
| bf8c3c25c1 | |||
| c8da8ca673 | |||
| 43fba378d6 | |||
| cd9317e5df | |||
| 8dbc5f70a5 | |||
| 07c6076ea0 | |||
| 28ab0bfb13 | |||
| 1c17e6c6bb | |||
| b814c0af9c | |||
| 9e5d9e2530 | |||
| 9408dacc27 | |||
| 12cfc19487 | |||
| afecd90a6c | |||
| 2f59467ac3 | |||
| 184e8e9f71 | |||
| 1e8c9764df | |||
| 41c7bd27b4 | |||
| 96d6d277a4 | |||
| 26e559662d | |||
| 52305618df |
@@ -0,0 +1,5 @@
|
|||||||
|
[*]
|
||||||
|
end_of_line = lf
|
||||||
|
|
||||||
|
[caddytest/integration/caddyfile_adapt/*.txt]
|
||||||
|
indent_style = tab
|
||||||
+17
-14
@@ -23,13 +23,13 @@ Other menu items:
|
|||||||
|
|
||||||
### Contributing code
|
### Contributing code
|
||||||
|
|
||||||
You can have a huge impact on the project by helping with its code. To contribute code to Caddy, open a [pull request](https://github.com/caddyserver/caddy/pulls) (PR). If you're new to our community, that's okay: **we gladly welcome pull requests from anyone, regardless of your native language or coding experience.** You can get familiar with Caddy's code base by using [code search at Sourcegraph](https://sourcegraph.com/github.com/caddyserver/caddy/-/search).
|
You can have a huge impact on the project by helping with its code. To contribute code to Caddy, first submit or comment in an issue to discuss your contribution, then open a [pull request](https://github.com/caddyserver/caddy/pulls) (PR). If you're new to our community, that's okay: **we gladly welcome pull requests from anyone, regardless of your native language or coding experience.** You can get familiar with Caddy's code base by using [code search at Sourcegraph](https://sourcegraph.com/github.com/caddyserver/caddy).
|
||||||
|
|
||||||
We hold contributions to a high standard for quality :bowtie:, so don't be surprised if we ask for revisions—even if it seems small or insignificant. Please don't take it personally. :blue_heart: If your change is on the right track, we can guide you to make it mergable.
|
We hold contributions to a high standard for quality :bowtie:, so don't be surprised if we ask for revisions—even if it seems small or insignificant. Please don't take it personally. :blue_heart: If your change is on the right track, we can guide you to make it mergable.
|
||||||
|
|
||||||
Here are some of the expectations we have of contributors:
|
Here are some of the expectations we have of contributors:
|
||||||
|
|
||||||
- **Open an issue to propose your change first.** This way we can avoid confusion, coordinate what everyone is working on, and ensure that any changes are in-line with the project's goals and the best interests of its users. We can also discuss the best possible implementation. If there's already an issue about it, comment on the existing issue to claim it.
|
- **Open an issue to propose your change first.** This way we can avoid confusion, coordinate what everyone is working on, and ensure that any changes are in-line with the project's goals and the best interests of its users. We can also discuss the best possible implementation. If there's already an issue about it, comment on the existing issue to claim it. A lot of valuable time can be saved by discussing a proposal first.
|
||||||
|
|
||||||
- **Keep pull requests small.** Smaller PRs are more likely to be merged because they are easier to review! We might ask you to break up large PRs into smaller ones. [An example of what we want to avoid.](https://twitter.com/iamdevloper/status/397664295875805184)
|
- **Keep pull requests small.** Smaller PRs are more likely to be merged because they are easier to review! We might ask you to break up large PRs into smaller ones. [An example of what we want to avoid.](https://twitter.com/iamdevloper/status/397664295875805184)
|
||||||
|
|
||||||
@@ -45,16 +45,18 @@ Here are some of the expectations we have of contributors:
|
|||||||
|
|
||||||
- **Use comments properly.** We expect good godoc comments for package-level functions, types, and values. Comments are also useful whenever the purpose for a line of code is not obvious.
|
- **Use comments properly.** We expect good godoc comments for package-level functions, types, and values. Comments are also useful whenever the purpose for a line of code is not obvious.
|
||||||
|
|
||||||
|
- **Pull requests may still get closed.** The longer a PR stays open and idle, the more likely it is to be closed. If we haven't reviewed it in a while, it probably means the change is not a priority. Please don't take this personally, we're trying to balance a lot of tasks! If nobody else has commented or reacted to the PR, it likely means your change is useful only to you. The reality is this happens quite a bit. We don't tend to accept PRs that aren't generally helpful. For these reasons or others, the PR may get closed even after a review. We are not obligated to accept all proposed changes, even if the best justification we can give is something vague like, "It doesn't sit right." Sometimes PRs are just the wrong thing or the wrong time. Because it is open source, you can always build your own modified version of Caddy with a change you need, even if we reject it in the official repo.
|
||||||
|
|
||||||
We often grant [collaborator status](#collaborator-instructions) to contributors who author one or more significant, high-quality PRs that are merged into the code base!
|
We often grant [collaborator status](#collaborator-instructions) to contributors who author one or more significant, high-quality PRs that are merged into the code base!
|
||||||
|
|
||||||
|
|
||||||
#### HOW TO MAKE A PULL REQUEST TO CADDY
|
#### HOW TO MAKE A PULL REQUEST TO CADDY
|
||||||
|
|
||||||
Contributing to Go projects on GitHub is fun and easy. We recommend the following workflow:
|
Contributing to Go projects on GitHub is fun and easy. After you have proposed your change in an issue, we recommend the following workflow:
|
||||||
|
|
||||||
1. [Fork this repo](https://github.com/caddyserver/caddy). This makes a copy of the code you can write to.
|
1. [Fork this repo](https://github.com/caddyserver/caddy). This makes a copy of the code you can write to.
|
||||||
|
|
||||||
2. If you don't already have this repo (caddyserver/caddy.git) repo on your computer, get it with `go get github.com/caddyserver/caddy/v2`.
|
2. If you don't already have this repo (caddyserver/caddy.git) repo on your computer, clone it down: `git clone https://github.com/caddyserver/caddy.git`
|
||||||
|
|
||||||
3. Tell git that it can push the caddyserver/caddy.git repo to your fork by adding a remote: `git remote add myfork https://github.com/<your-username>/caddy.git`
|
3. Tell git that it can push the caddyserver/caddy.git repo to your fork by adding a remote: `git remote add myfork https://github.com/<your-username>/caddy.git`
|
||||||
|
|
||||||
@@ -85,9 +87,9 @@ Many people on the forums could benefit from your experience and expertise, too.
|
|||||||
|
|
||||||
Like every software, Caddy has its flaws. If you find one, [search the issues](https://github.com/caddyserver/caddy/issues) to see if it has already been reported. If not, [open a new issue](https://github.com/caddyserver/caddy/issues/new) and describe the bug, and somebody will look into it! (This repository is only for Caddy and its standard modules.)
|
Like every software, Caddy has its flaws. If you find one, [search the issues](https://github.com/caddyserver/caddy/issues) to see if it has already been reported. If not, [open a new issue](https://github.com/caddyserver/caddy/issues/new) and describe the bug, and somebody will look into it! (This repository is only for Caddy and its standard modules.)
|
||||||
|
|
||||||
**You can help stop bugs in their tracks!** Speed up the patch by identifying the bug in the code. This can sometimes be done by adding `fmt.Println()` statements (or similar) in relevant code paths to narrow down where the problem may be. It's a good way to [introduce yourself to the Go language](https://tour.golang.org), too.
|
**You can help us fix bugs!** Speed up the patch by identifying the bug in the code. This can sometimes be done by adding `fmt.Println()` statements (or similar) in relevant code paths to narrow down where the problem may be. It's a good way to [introduce yourself to the Go language](https://tour.golang.org), too.
|
||||||
|
|
||||||
Please follow the issue template so we have all the needed information. Unredacted—yes, actual values matter. We need to be able to repeat the bug using your instructions. Please simplify the issue as much as possible. The burden is on you to convince us that it is actually a bug in Caddy. This is easiest to do when you write clear, concise instructions so we can reproduce the behavior (even if it seems obvious). The more detailed and specific you are, the faster we will be able to help you!
|
We may reply with an issue template. Please follow the template so we have all the needed information. Unredacted—yes, actual values matter. We need to be able to repeat the bug using your instructions. Please simplify the issue as much as possible. If you don't, we might close your report. The burden is on you to make it easily reproducible and to convince us that it is actually a bug in Caddy. This is easiest to do when you write clear, concise instructions so we can reproduce the behavior (even if it seems obvious). The more detailed and specific you are, the faster we will be able to help you!
|
||||||
|
|
||||||
We suggest reading [How to Report Bugs Effectively](http://www.chiark.greenend.org.uk/~sgtatham/bugs.html).
|
We suggest reading [How to Report Bugs Effectively](http://www.chiark.greenend.org.uk/~sgtatham/bugs.html).
|
||||||
|
|
||||||
@@ -98,11 +100,12 @@ Please be kind. :smile: Remember that Caddy comes at no cost to you, and you're
|
|||||||
Maintainers---or more generally, developers---need three things to act on bugs:
|
Maintainers---or more generally, developers---need three things to act on bugs:
|
||||||
|
|
||||||
1. To agree or be convinced that it's a bug (reporter's responsibility).
|
1. To agree or be convinced that it's a bug (reporter's responsibility).
|
||||||
- A bug is undesired or surprising behavior which violates documentation or the spec.
|
- A bug is unintentional, undesired, or surprising behavior which violates documentation or relevant spec. It might be either a mistake in the documentation or a bug in the code.
|
||||||
|
- This project usually does not work around bugs in other software, systems, and dependencies; instead, we recommend that those bugs are fixed at their source. This sometimes means we close issues or reject PRs that attempt to fix, workaround, or hide bugs in other projects.
|
||||||
|
|
||||||
2. To be able to understand what is happening (mostly reporter's responsibility).
|
2. To be able to understand what is happening (mostly reporter's responsibility).
|
||||||
- If the reporter can provide satisfactory instructions such that a developer can reproduce the bug, the developer will likely be able to understand the bug, write a test case, and implement a fix.
|
- If the reporter can provide satisfactory instructions such that a developer can reproduce the bug, the developer will likely be able to understand the bug, write a test case, and implement a fix. This is the least amount of work for everyone and path to the fastest resolution.
|
||||||
- Otherwise, the burden is on the reporter to test possible solutions. This is discouraged because it loosens the feedback loop, slows down debugging efforts, obscures the true nature of the problem from the developers, and is unlikely to result in new test cases.
|
- Otherwise, the burden is on the reporter to test possible solutions. This is less preferable because it loosens the feedback loop, slows down debugging efforts, obscures the true nature of the problem from the developers, and is unlikely to result in new test cases.
|
||||||
|
|
||||||
3. A solution, or ideas toward a solution (mostly maintainer's responsibility).
|
3. A solution, or ideas toward a solution (mostly maintainer's responsibility).
|
||||||
- Sometimes the best solution is a documentation change.
|
- Sometimes the best solution is a documentation change.
|
||||||
@@ -112,7 +115,7 @@ Maintainers---or more generally, developers---need three things to act on bugs:
|
|||||||
|
|
||||||
Thus, at the very least, the reporter is expected to:
|
Thus, at the very least, the reporter is expected to:
|
||||||
|
|
||||||
1. Convince the reader that it's a bug (if it's not obvious).
|
1. Convince the reader that it's a bug in Caddy (if it's not obvious).
|
||||||
2. Reduce the problem down to the minimum specific steps required to reproduce it.
|
2. Reduce the problem down to the minimum specific steps required to reproduce it.
|
||||||
|
|
||||||
The maintainer is usually able to do the rest; but of course the reporter may invest additional effort to speed up the process.
|
The maintainer is usually able to do the rest; but of course the reporter may invest additional effort to speed up the process.
|
||||||
@@ -123,7 +126,7 @@ The maintainer is usually able to do the rest; but of course the reporter may in
|
|||||||
|
|
||||||
First, [search to see if your feature has already been requested](https://github.com/caddyserver/caddy/issues). If it has, you can add a :+1: reaction to vote for it. If your feature idea is new, open an issue to request the feature. Please describe your idea thoroughly so that we know how to implement it! Really vague requests may not be helpful or actionable and, without clarification, will have to be closed.
|
First, [search to see if your feature has already been requested](https://github.com/caddyserver/caddy/issues). If it has, you can add a :+1: reaction to vote for it. If your feature idea is new, open an issue to request the feature. Please describe your idea thoroughly so that we know how to implement it! Really vague requests may not be helpful or actionable and, without clarification, will have to be closed.
|
||||||
|
|
||||||
While we really do value your requests and implement many of them, not all features are a good fit for Caddy. Most of those [make good modules](#writing-a-caddy-module), which can be made by anyone! But if a feature is not in the best interest of the Caddy project or its users in general, we may politely decline to implement it into Caddy core.
|
While we really do value your requests and implement many of them, not all features are a good fit for Caddy. Most of those [make good modules](#writing-a-caddy-module), which can be made by anyone! But if a feature is not in the best interest of the Caddy project or its users in general, we may politely decline to implement it into Caddy core. Additionally, some features are bad ideas altogether (for either obvious or non-obvious reasons) which may be rejected. We'll try to explain why we reject a feature, but sometimes the best we can do is, "It's not a good fit for the project."
|
||||||
|
|
||||||
|
|
||||||
### Improving documentation
|
### Improving documentation
|
||||||
@@ -132,11 +135,11 @@ Caddy's documentation is available at [https://caddyserver.com/docs](https://cad
|
|||||||
|
|
||||||
Note that third-party module documentation is not hosted by the Caddy website, other than basic usage examples. They are managed by the individual module authors, and you will have to contact them to change their documentation.
|
Note that third-party module documentation is not hosted by the Caddy website, other than basic usage examples. They are managed by the individual module authors, and you will have to contact them to change their documentation.
|
||||||
|
|
||||||
|
Our documentation is scoped to the Caddy project only: it is not for describing how other software or systems work, even if they relate to Caddy or web servers. That kind of content [can be found in our community wiki](https://caddy.community/c/wiki/13), however.
|
||||||
|
|
||||||
## Collaborator Instructions
|
## Collaborator Instructions
|
||||||
|
|
||||||
Collaborators have push rights to the repository. We grant this permission after one or more successful, high-quality PRs are merged! We thank them for their help.The expectations we have of collaborators are:
|
Collaborators have push rights to the repository. We grant this permission after one or more successful, high-quality PRs are merged! We thank them for their help. The expectations we have of collaborators are:
|
||||||
|
|
||||||
- **Help review pull requests.** Be meticulous, but also kind. We love our contributors, but we critique the contribution to make it better. Multiple, thorough reviews make for the best contributions! Here are some questions to consider:
|
- **Help review pull requests.** Be meticulous, but also kind. We love our contributors, but we critique the contribution to make it better. Multiple, thorough reviews make for the best contributions! Here are some questions to consider:
|
||||||
- Can the change be made more elegant?
|
- Can the change be made more elegant?
|
||||||
@@ -167,7 +170,7 @@ Collaborators have push rights to the repository. We grant this permission after
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Values
|
## Values (WIP)
|
||||||
|
|
||||||
- A person is always more important than code. People don't like being handled "efficiently". But we can still process issues and pull requests efficiently while being kind, patient, and considerate.
|
- A person is always more important than code. People don't like being handled "efficiently". But we can still process issues and pull requests efficiently while being kind, patient, and considerate.
|
||||||
|
|
||||||
|
|||||||
+39
-7
@@ -2,26 +2,58 @@
|
|||||||
|
|
||||||
The Caddy project would like to make sure that it stays on top of all practically-exploitable vulnerabilities.
|
The Caddy project would like to make sure that it stays on top of all practically-exploitable vulnerabilities.
|
||||||
|
|
||||||
Some security problems are more the result of interplay between different components of the Web, rather than a vulnerability in the web server itself. Please report only vulnerabilities in the web server itself, as we cannot coerce the rest of the Web to be fixed (for example, we do not consider IP spoofing or BGP hijacks a vulnerability in the Caddy web server).
|
|
||||||
|
|
||||||
Please note that we consider publicly-registered domain names to be public information. This necessary in order to maintain the integrity of certificate transparency, public DNS, and other public trust systems.
|
|
||||||
|
|
||||||
## Supported Versions
|
## Supported Versions
|
||||||
|
|
||||||
| Version | Supported |
|
| Version | Supported |
|
||||||
| ------- | ------------------ |
|
| ------- | ------------------ |
|
||||||
| 2.x | :white_check_mark: |
|
| 2.x | :white_check_mark: |
|
||||||
| 1.x | :white_check_mark: (deprecating soon) |
|
| 1.x | :x: |
|
||||||
| < 1.x | :x: |
|
| < 1.x | :x: |
|
||||||
|
|
||||||
|
|
||||||
|
## Acceptable Scope
|
||||||
|
|
||||||
|
A security report must demonstrate a security bug in the source code from this repository.
|
||||||
|
|
||||||
|
Some security problems are the result of interplay between different components of the Web, rather than a vulnerability in the web server itself. Please only report vulnerabilities in the web server itself, as we cannot coerce the rest of the Web to be fixed (for example, we do not consider IP spoofing, BGP hijacks, or missing/misconfigured HTTP headers a vulnerability in the Caddy web server).
|
||||||
|
|
||||||
|
Vulnerabilities caused by misconfigurations are out of scope. Yes, it is entirely possible to craft and use a configuration that is unsafe, just like with every other web server; we recommend against doing that.
|
||||||
|
|
||||||
|
We do not accept reports if the steps imply or require a compromised system or third-party software, as we cannot control those. We expect that users secure their own systems and keep all their software patched. For example, if untrusted users are able to upload/write/host arbitrary files in the web root directory, it is NOT a security bug in Caddy if those files get served to clients; however, it _would_ be a valid report if a bug in Caddy's source code unintentionally gave unauthorized users the ability to upload unsafe files or delete files without relying on an unpatched system or piece of software.
|
||||||
|
|
||||||
|
Client-side exploits are out of scope. In other words, it is not a bug in Caddy if the web browser does something unsafe, even if the downloaded content was served by Caddy. (Those kinds of exploits can generally be mitigated by proper configuration of HTTP headers.) As a general rule, the content served by Caddy is not considered in scope because content is configurable by the site owner or the associated web application.
|
||||||
|
|
||||||
|
Security bugs in code dependencies are out of scope. Instead, if a dependency has patched a relevant security bug, please feel free to open a public issue or pull request to update that dependency in our code.
|
||||||
|
|
||||||
|
|
||||||
## Reporting a Vulnerability
|
## Reporting a Vulnerability
|
||||||
|
|
||||||
Please email Matt Holt (the author) directly: Matthew dot Holt at Gmail.
|
We get a lot of difficult reports that turn out to be invalid. Clear, obvious reports tend to be the most credible (but are also rare).
|
||||||
|
|
||||||
We'll need enough information to verify the bug and make a patch. It will speed things up if you suggest a working patch, such as a code diff, and explain why and how it works. Reports that are not actionable, do not contain enough information, are too pushy/demanding, or are not able to convince us that it is a viable and practical attack on the web server itself may be deferred to a later time or possibly ignored, resources permitting. Priority will be given to credible, responsible reports that are constructive, specific, and actionable. Thank you for understanding.
|
First please ensure your report falls within the accepted scope of security bugs (above).
|
||||||
|
|
||||||
|
We'll need enough information to verify the bug and make a patch. To speed things up, please include:
|
||||||
|
|
||||||
|
- Most minimal possible config (without redactions!)
|
||||||
|
- Command(s)
|
||||||
|
- Precise HTTP requests (`curl -v` and its output please)
|
||||||
|
- Full log output (please enable debug mode)
|
||||||
|
- Specific minimal steps to reproduce the issue from scratch
|
||||||
|
- A working patch
|
||||||
|
|
||||||
|
Please DO NOT use containers, VMs, cloud instances or services, or any other complex infrastructure in your steps. Always prefer `curl` instead of web browsers.
|
||||||
|
|
||||||
|
We consider publicly-registered domain names to be public information. This necessary in order to maintain the integrity of certificate transparency, public DNS, and other public trust systems. Do not redact domain names from your reports. The actual content of your domain name affects Caddy's behavior, so we need the exact domain name(s) to reproduce with, or your report will be ignored.
|
||||||
|
|
||||||
|
It will speed things up if you suggest a working patch, such as a code diff, and explain why and how it works. Reports that are not actionable, do not contain enough information, are too pushy/demanding, or are not able to convince us that it is a viable and practical attack on the web server itself may be deferred to a later time or possibly ignored, depending on available resources. Priority will be given to credible, responsible reports that are constructive, specific, and actionable. (We get a lot of invalid reports.) Thank you for understanding.
|
||||||
|
|
||||||
|
When you are ready, please email Matt Holt (the author) directly: matt at dyanim dot com.
|
||||||
|
|
||||||
|
Please don't encrypt the email body. It only makes the process more complicated.
|
||||||
|
|
||||||
Please also understand that due to our nature as an open source project, we do not have a budget to award security bounties. We can only thank you.
|
Please also understand that due to our nature as an open source project, we do not have a budget to award security bounties. We can only thank you.
|
||||||
|
|
||||||
If your report is valid and a patch is released, we will not reveal your identity by default. If you wish to be credited, please give us the name to use.
|
If your report is valid and a patch is released, we will not reveal your identity by default. If you wish to be credited, please give us the name to use and/or your GitHub username. If you don't provide this we can't credit you.
|
||||||
|
|
||||||
Thanks for responsibly helping Caddy—and thousands of websites—be more secure!
|
Thanks for responsibly helping Caddy—and thousands of websites—be more secure!
|
||||||
|
|||||||
+67
-20
@@ -1,14 +1,16 @@
|
|||||||
# Used as inspiration: https://github.com/mvdan/github-actions-golang
|
# Used as inspiration: https://github.com/mvdan/github-actions-golang
|
||||||
|
|
||||||
name: Cross-Platform
|
name: Tests
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
|
- 2.*
|
||||||
pull_request:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
|
- 2.*
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
@@ -17,12 +19,20 @@ jobs:
|
|||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
os: [ ubuntu-latest, macos-latest, windows-latest ]
|
os: [ ubuntu-latest, macos-latest, windows-latest ]
|
||||||
go-version: [ 1.14.x ]
|
go: [ '1.17', '1.18' ]
|
||||||
|
|
||||||
|
include:
|
||||||
|
# Set the minimum Go patch version for the given Go minor
|
||||||
|
# Usable via ${{ matrix.GO_SEMVER }}
|
||||||
|
- go: '1.17'
|
||||||
|
GO_SEMVER: '~1.17.9'
|
||||||
|
|
||||||
|
- go: '1.18'
|
||||||
|
GO_SEMVER: '~1.18.1'
|
||||||
|
|
||||||
# Set some variables per OS, usable via ${{ matrix.VAR }}
|
# Set some variables per OS, usable via ${{ matrix.VAR }}
|
||||||
# CADDY_BIN_PATH: the path to the compiled Caddy binary, for artifact publishing
|
# CADDY_BIN_PATH: the path to the compiled Caddy binary, for artifact publishing
|
||||||
# SUCCESS: the typical value for $? per OS (Windows/pwsh returns 'True')
|
# SUCCESS: the typical value for $? per OS (Windows/pwsh returns 'True')
|
||||||
include:
|
|
||||||
- os: ubuntu-latest
|
- os: ubuntu-latest
|
||||||
CADDY_BIN_PATH: ./cmd/caddy/caddy
|
CADDY_BIN_PATH: ./cmd/caddy/caddy
|
||||||
SUCCESS: 0
|
SUCCESS: 0
|
||||||
@@ -39,12 +49,13 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Install Go
|
- name: Install Go
|
||||||
uses: actions/setup-go@v1
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: ${{ matrix.go-version }}
|
go-version: ${{ matrix.GO_SEMVER }}
|
||||||
|
check-latest: true
|
||||||
|
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
# These tools would be useful if we later decide to reinvestigate
|
# These tools would be useful if we later decide to reinvestigate
|
||||||
# publishing test/coverage reports to some tool for easier consumption
|
# publishing test/coverage reports to some tool for easier consumption
|
||||||
@@ -64,17 +75,26 @@ jobs:
|
|||||||
go env
|
go env
|
||||||
printf "\n\nSystem environment:\n\n"
|
printf "\n\nSystem environment:\n\n"
|
||||||
env
|
env
|
||||||
|
printf "Git version: $(git version)\n\n"
|
||||||
# Calculate the short SHA1 hash of the git commit
|
# Calculate the short SHA1 hash of the git commit
|
||||||
echo "::set-output name=short_sha::$(git rev-parse --short HEAD)"
|
echo "::set-output name=short_sha::$(git rev-parse --short HEAD)"
|
||||||
echo "::set-output name=go_cache::$(go env GOCACHE)"
|
|
||||||
|
|
||||||
- name: Cache the build cache
|
- name: Cache the build cache
|
||||||
uses: actions/cache@v1
|
uses: actions/cache@v2
|
||||||
with:
|
with:
|
||||||
path: ${{ steps.vars.outputs.go_cache }}
|
# In order:
|
||||||
key: ${{ runner.os }}-go-ci-${{ hashFiles('**/go.sum') }}
|
# * Module download cache
|
||||||
|
# * Build cache (Linux)
|
||||||
|
# * Build cache (Mac)
|
||||||
|
# * Build cache (Windows)
|
||||||
|
path: |
|
||||||
|
~/go/pkg/mod
|
||||||
|
~/.cache/go-build
|
||||||
|
~/Library/Caches/go-build
|
||||||
|
~\AppData\Local\go-build
|
||||||
|
key: ${{ runner.os }}-${{ matrix.go }}-go-ci-${{ hashFiles('**/go.sum') }}
|
||||||
restore-keys: |
|
restore-keys: |
|
||||||
${{ runner.os }}-go-ci
|
${{ runner.os }}-${{ matrix.go }}-go-ci
|
||||||
|
|
||||||
- name: Get dependencies
|
- name: Get dependencies
|
||||||
run: |
|
run: |
|
||||||
@@ -91,7 +111,7 @@ jobs:
|
|||||||
- name: Publish Build Artifact
|
- name: Publish Build Artifact
|
||||||
uses: actions/upload-artifact@v1
|
uses: actions/upload-artifact@v1
|
||||||
with:
|
with:
|
||||||
name: caddy_v2_${{ runner.os }}_${{ steps.vars.outputs.short_sha }}
|
name: caddy_${{ runner.os }}_go${{ matrix.go }}_${{ steps.vars.outputs.short_sha }}
|
||||||
path: ${{ matrix.CADDY_BIN_PATH }}
|
path: ${{ matrix.CADDY_BIN_PATH }}
|
||||||
|
|
||||||
# Commented bits below were useful to allow the job to continue
|
# Commented bits below were useful to allow the job to continue
|
||||||
@@ -120,16 +140,43 @@ jobs:
|
|||||||
# echo "step_test ${{ steps.step_test.outputs.status }}\n"
|
# echo "step_test ${{ steps.step_test.outputs.status }}\n"
|
||||||
# exit 1
|
# exit 1
|
||||||
|
|
||||||
# From https://github.com/reviewdog/action-golangci-lint
|
s390x-test:
|
||||||
golangci-lint:
|
name: test (s390x on IBM Z)
|
||||||
name: runner / golangci-lint
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
if: github.event.pull_request.head.repo.full_name == github.repository
|
||||||
|
continue-on-error: true # August 2020: s390x VM is down due to weather and power issues
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code into the Go module directory
|
- name: Checkout code into the Go module directory
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
|
- name: Run Tests
|
||||||
|
run: |
|
||||||
|
mkdir -p ~/.ssh && echo -e "${SSH_KEY//_/\\n}" > ~/.ssh/id_ecdsa && chmod og-rwx ~/.ssh/id_ecdsa
|
||||||
|
|
||||||
- name: Run golangci-lint
|
# short sha is enough?
|
||||||
uses: reviewdog/action-golangci-lint@v1
|
short_sha=$(git rev-parse --short HEAD)
|
||||||
# uses: docker://reviewdog/action-golangci-lint:v1 # pre-build docker image
|
|
||||||
|
# The environment is fresh, so there's no point in keeping accepting and adding the key.
|
||||||
|
rsync -arz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress --delete --exclude '.git' . caddy-ci@ci-s390x.caddyserver.com:/var/tmp/"$short_sha"
|
||||||
|
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -t caddy-ci@ci-s390x.caddyserver.com "cd /var/tmp/$short_sha; go version; go env; printf "\n\n";CGO_ENABLED=0 go test -v ./..."
|
||||||
|
test_result=$?
|
||||||
|
|
||||||
|
# There's no need leaving the files around
|
||||||
|
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null caddy-ci@ci-s390x.caddyserver.com "rm -rf /var/tmp/'$short_sha'"
|
||||||
|
|
||||||
|
echo "Test exit code: $test_result"
|
||||||
|
exit $test_result
|
||||||
|
env:
|
||||||
|
SSH_KEY: ${{ secrets.S390X_SSH_KEY }}
|
||||||
|
|
||||||
|
goreleaser-check:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: checkout
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- uses: goreleaser/goreleaser-action@v2
|
||||||
with:
|
with:
|
||||||
github_token: ${{ secrets.github_token }}
|
version: latest
|
||||||
|
args: check
|
||||||
|
env:
|
||||||
|
TAG: ${{ steps.vars.outputs.version_tag }}
|
||||||
|
|||||||
@@ -0,0 +1,74 @@
|
|||||||
|
name: Cross-Build
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- 2.*
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- 2.*
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
cross-build-test:
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
goos: ['android', 'linux', 'solaris', 'illumos', 'dragonfly', 'freebsd', 'openbsd', 'plan9', 'windows', 'darwin', 'netbsd']
|
||||||
|
go: [ '1.18' ]
|
||||||
|
|
||||||
|
include:
|
||||||
|
# Set the minimum Go patch version for the given Go minor
|
||||||
|
# Usable via ${{ matrix.GO_SEMVER }}
|
||||||
|
- go: '1.18'
|
||||||
|
GO_SEMVER: '~1.18.1'
|
||||||
|
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
continue-on-error: true
|
||||||
|
steps:
|
||||||
|
- name: Install Go
|
||||||
|
uses: actions/setup-go@v3
|
||||||
|
with:
|
||||||
|
go-version: ${{ matrix.GO_SEMVER }}
|
||||||
|
check-latest: true
|
||||||
|
|
||||||
|
- name: Print Go version and environment
|
||||||
|
id: vars
|
||||||
|
run: |
|
||||||
|
printf "Using go at: $(which go)\n"
|
||||||
|
printf "Go version: $(go version)\n"
|
||||||
|
printf "\n\nGo environment:\n\n"
|
||||||
|
go env
|
||||||
|
printf "\n\nSystem environment:\n\n"
|
||||||
|
env
|
||||||
|
|
||||||
|
- name: Cache the build cache
|
||||||
|
uses: actions/cache@v2
|
||||||
|
with:
|
||||||
|
# In order:
|
||||||
|
# * Module download cache
|
||||||
|
# * Build cache (Linux)
|
||||||
|
path: |
|
||||||
|
~/go/pkg/mod
|
||||||
|
~/.cache/go-build
|
||||||
|
key: cross-build-go${{ matrix.go }}-${{ matrix.goos }}-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: |
|
||||||
|
cross-build-go${{ matrix.go }}-${{ matrix.goos }}
|
||||||
|
|
||||||
|
- name: Checkout code into the Go module directory
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Run Build
|
||||||
|
env:
|
||||||
|
CGO_ENABLED: 0
|
||||||
|
GOOS: ${{ matrix.goos }}
|
||||||
|
shell: bash
|
||||||
|
continue-on-error: true
|
||||||
|
working-directory: ./cmd/caddy
|
||||||
|
run: |
|
||||||
|
GOOS=$GOOS go build -trimpath -o caddy-"$GOOS"-amd64 2> /dev/null
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo "::warning ::$GOOS Build Failed"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
name: Fuzzing
|
|
||||||
|
|
||||||
on:
|
|
||||||
# Daily midnight fuzzing
|
|
||||||
schedule:
|
|
||||||
- cron: '0 0 * * *'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
fuzzing:
|
|
||||||
name: Fuzzing
|
|
||||||
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
os: [ ubuntu-latest ]
|
|
||||||
go-version: [ 1.14.x ]
|
|
||||||
runs-on: ${{ matrix.os }}
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Install Go
|
|
||||||
uses: actions/setup-go@v1
|
|
||||||
with:
|
|
||||||
go-version: ${{ matrix.go-version }}
|
|
||||||
|
|
||||||
- name: Checkout code
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
|
|
||||||
- name: Download go-fuzz tools and the Fuzzit CLI, move Fuzzit CLI to GOBIN
|
|
||||||
# If we decide we need to prevent this from running on forks, we can use this line:
|
|
||||||
# if: github.repository == 'caddyserver/caddy'
|
|
||||||
run: |
|
|
||||||
|
|
||||||
go get -v github.com/dvyukov/go-fuzz/go-fuzz github.com/dvyukov/go-fuzz/go-fuzz-build
|
|
||||||
wget -q -O fuzzit https://github.com/fuzzitdev/fuzzit/releases/download/v2.4.77/fuzzit_Linux_x86_64
|
|
||||||
chmod a+x fuzzit
|
|
||||||
mv fuzzit $(go env GOPATH)/bin
|
|
||||||
echo "::add-path::$(go env GOPATH)/bin"
|
|
||||||
|
|
||||||
- name: Generate fuzzers & submit them to Fuzzit
|
|
||||||
continue-on-error: true
|
|
||||||
env:
|
|
||||||
FUZZIT_API_KEY: ${{ secrets.FUZZIT_API_KEY }}
|
|
||||||
SYSTEM_PULLREQUEST_SOURCEBRANCH: ${{ github.ref }}
|
|
||||||
BUILD_SOURCEVERSION: ${{ github.sha }}
|
|
||||||
run: |
|
|
||||||
# debug
|
|
||||||
echo "PR Source Branch: $SYSTEM_PULLREQUEST_SOURCEBRANCH"
|
|
||||||
echo "Source version: $BUILD_SOURCEVERSION"
|
|
||||||
|
|
||||||
declare -A fuzzers_funcs=(\
|
|
||||||
["./caddyconfig/httpcaddyfile/addresses_fuzz.go"]="FuzzParseAddress" \
|
|
||||||
["./listeners_fuzz.go"]="FuzzParseNetworkAddress" \
|
|
||||||
["./replacer_fuzz.go"]="FuzzReplacer" \
|
|
||||||
)
|
|
||||||
|
|
||||||
declare -A fuzzers_targets=(\
|
|
||||||
["./caddyconfig/httpcaddyfile/addresses_fuzz.go"]="parse-address" \
|
|
||||||
["./listeners_fuzz.go"]="parse-network-address" \
|
|
||||||
["./replacer_fuzz.go"]="replacer" \
|
|
||||||
)
|
|
||||||
|
|
||||||
fuzz_type="fuzzing"
|
|
||||||
|
|
||||||
for f in $(find . -name \*_fuzz.go); do
|
|
||||||
FUZZER_DIRECTORY=$(dirname "$f")
|
|
||||||
|
|
||||||
echo "go-fuzz-build func ${fuzzers_funcs[$f]} residing in $f"
|
|
||||||
|
|
||||||
go-fuzz-build -func "${fuzzers_funcs[$f]}" -o "$FUZZER_DIRECTORY/${fuzzers_targets[$f]}.zip" "$FUZZER_DIRECTORY"
|
|
||||||
|
|
||||||
fuzzit create job --engine go-fuzz caddyserver/"${fuzzers_targets[$f]}" "$FUZZER_DIRECTORY"/"${fuzzers_targets[$f]}.zip" --api-key "${FUZZIT_API_KEY}" --type "${fuzz_type}" --branch "${SYSTEM_PULLREQUEST_SOURCEBRANCH}" --revision "${BUILD_SOURCEVERSION}"
|
|
||||||
|
|
||||||
echo "Completed $f"
|
|
||||||
done
|
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
name: Lint
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- 2.*
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- 2.*
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
# From https://github.com/golangci/golangci-lint-action
|
||||||
|
golangci:
|
||||||
|
name: lint
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- uses: actions/setup-go@v3
|
||||||
|
with:
|
||||||
|
go-version: '~1.17.9'
|
||||||
|
check-latest: true
|
||||||
|
|
||||||
|
- name: golangci-lint
|
||||||
|
uses: golangci/golangci-lint-action@v3
|
||||||
|
with:
|
||||||
|
version: v1.44
|
||||||
|
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
||||||
|
# only-new-issues: true
|
||||||
+114
-13
@@ -11,21 +11,38 @@ jobs:
|
|||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
os: [ ubuntu-latest ]
|
os: [ ubuntu-latest ]
|
||||||
go-version: [ 1.14.x ]
|
go: [ '1.18' ]
|
||||||
|
|
||||||
|
include:
|
||||||
|
# Set the minimum Go patch version for the given Go minor
|
||||||
|
# Usable via ${{ matrix.GO_SEMVER }}
|
||||||
|
- go: '1.18'
|
||||||
|
GO_SEMVER: '~1.18.1'
|
||||||
|
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Install Go
|
- name: Install Go
|
||||||
uses: actions/setup-go@v1
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: ${{ matrix.go-version }}
|
go-version: ${{ matrix.GO_SEMVER }}
|
||||||
|
check-latest: true
|
||||||
|
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v3
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
# So GoReleaser can generate the changelog properly
|
# Force fetch upstream tags -- because 65 minutes
|
||||||
- name: Unshallowify the repo clone
|
# tl;dr: actions/checkout@v3 runs this line:
|
||||||
run: git fetch --prune --unshallow
|
# git -c protocol.version=2 fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin +ebc278ec98bb24f2852b61fde2a9bf2e3d83818b:refs/tags/
|
||||||
|
# which makes its own local lightweight tag, losing all the annotations in the process. Our earlier script ran:
|
||||||
|
# git fetch --prune --unshallow
|
||||||
|
# which doesn't overwrite that tag because that would be destructive.
|
||||||
|
# Credit to @francislavoie for the investigation.
|
||||||
|
# https://github.com/actions/checkout/issues/290#issuecomment-680260080
|
||||||
|
- name: Force fetch upstream tags
|
||||||
|
run: git fetch --tags --force
|
||||||
|
|
||||||
# https://github.community/t5/GitHub-Actions/How-to-get-just-the-tag-name/m-p/32167/highlight/true#M1027
|
# https://github.community/t5/GitHub-Actions/How-to-get-just-the-tag-name/m-p/32167/highlight/true#M1027
|
||||||
- name: Print Go version and environment
|
- name: Print Go version and environment
|
||||||
@@ -39,19 +56,53 @@ jobs:
|
|||||||
env
|
env
|
||||||
echo "::set-output name=version_tag::${GITHUB_REF/refs\/tags\//}"
|
echo "::set-output name=version_tag::${GITHUB_REF/refs\/tags\//}"
|
||||||
echo "::set-output name=short_sha::$(git rev-parse --short HEAD)"
|
echo "::set-output name=short_sha::$(git rev-parse --short HEAD)"
|
||||||
echo "::set-output name=go_cache::$(go env GOCACHE)"
|
|
||||||
|
# Add "pip install" CLI tools to PATH
|
||||||
|
echo ~/.local/bin >> $GITHUB_PATH
|
||||||
|
|
||||||
|
# Parse semver
|
||||||
|
TAG=${GITHUB_REF/refs\/tags\//}
|
||||||
|
SEMVER_RE='[^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)\([0-9A-Za-z\.-]*\)'
|
||||||
|
TAG_MAJOR=`echo ${TAG#v} | sed -e "s#$SEMVER_RE#\1#"`
|
||||||
|
TAG_MINOR=`echo ${TAG#v} | sed -e "s#$SEMVER_RE#\2#"`
|
||||||
|
TAG_PATCH=`echo ${TAG#v} | sed -e "s#$SEMVER_RE#\3#"`
|
||||||
|
TAG_SPECIAL=`echo ${TAG#v} | sed -e "s#$SEMVER_RE#\4#"`
|
||||||
|
echo "::set-output name=tag_major::${TAG_MAJOR}"
|
||||||
|
echo "::set-output name=tag_minor::${TAG_MINOR}"
|
||||||
|
echo "::set-output name=tag_patch::${TAG_PATCH}"
|
||||||
|
echo "::set-output name=tag_special::${TAG_SPECIAL}"
|
||||||
|
|
||||||
|
# Cloudsmith CLI tooling for pushing releases
|
||||||
|
# See https://help.cloudsmith.io/docs/cli
|
||||||
|
- name: Install Cloudsmith CLI
|
||||||
|
run: pip install --upgrade cloudsmith-cli
|
||||||
|
|
||||||
|
- name: Validate commits and tag signatures
|
||||||
|
run: |
|
||||||
|
|
||||||
|
# Import Matt Holt's key
|
||||||
|
curl 'https://github.com/mholt.gpg' | gpg --import
|
||||||
|
|
||||||
|
echo "Verifying the tag: ${{ steps.vars.outputs.version_tag }}"
|
||||||
|
# tags are only accepted if signed by Matt's key
|
||||||
|
git verify-tag "${{ steps.vars.outputs.version_tag }}" || exit 1
|
||||||
|
|
||||||
- name: Cache the build cache
|
- name: Cache the build cache
|
||||||
uses: actions/cache@v1
|
uses: actions/cache@v2
|
||||||
with:
|
with:
|
||||||
path: ${{ steps.vars.outputs.go_cache }}
|
# In order:
|
||||||
key: ${{ runner.os }}-go-release-${{ hashFiles('**/go.sum') }}
|
# * Module download cache
|
||||||
|
# * Build cache (Linux)
|
||||||
|
path: |
|
||||||
|
~/go/pkg/mod
|
||||||
|
~/.cache/go-build
|
||||||
|
key: ${{ runner.os }}-go${{ matrix.go }}-release-${{ hashFiles('**/go.sum') }}
|
||||||
restore-keys: |
|
restore-keys: |
|
||||||
${{ runner.os }}-go-release
|
${{ runner.os }}-go${{ matrix.go }}-release
|
||||||
|
|
||||||
# GoReleaser will take care of publishing those artifacts into the release
|
# GoReleaser will take care of publishing those artifacts into the release
|
||||||
- name: Run GoReleaser
|
- name: Run GoReleaser
|
||||||
uses: goreleaser/goreleaser-action@v1
|
uses: goreleaser/goreleaser-action@v2
|
||||||
with:
|
with:
|
||||||
version: latest
|
version: latest
|
||||||
args: release --rm-dist
|
args: release --rm-dist
|
||||||
@@ -59,10 +110,60 @@ jobs:
|
|||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
TAG: ${{ steps.vars.outputs.version_tag }}
|
TAG: ${{ steps.vars.outputs.version_tag }}
|
||||||
|
|
||||||
|
# Only publish on non-special tags (e.g. non-beta)
|
||||||
|
# We will continue to push to Gemfury for the foreseeable future, although
|
||||||
|
# Cloudsmith is probably better, to not break things for existing users of Gemfury.
|
||||||
|
# See https://gemfury.com/caddy/deb:caddy
|
||||||
- name: Publish .deb to Gemfury
|
- name: Publish .deb to Gemfury
|
||||||
|
if: ${{ steps.vars.outputs.tag_special == '' }}
|
||||||
env:
|
env:
|
||||||
GEMFURY_PUSH_TOKEN: ${{ secrets.GEMFURY_PUSH_TOKEN }}
|
GEMFURY_PUSH_TOKEN: ${{ secrets.GEMFURY_PUSH_TOKEN }}
|
||||||
run: |
|
run: |
|
||||||
for filename in dist/*.deb; do
|
for filename in dist/*.deb; do
|
||||||
|
# armv6 and armv7 are both "armhf" so we can skip the duplicate
|
||||||
|
if [[ "$filename" == *"armv6"* ]]; then
|
||||||
|
echo "Skipping $filename"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
curl -F package=@"$filename" https://${GEMFURY_PUSH_TOKEN}:@push.fury.io/caddy/
|
curl -F package=@"$filename" https://${GEMFURY_PUSH_TOKEN}:@push.fury.io/caddy/
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# Publish only special tags (unstable/beta/rc) to the "testing" repo
|
||||||
|
# See https://cloudsmith.io/~caddy/repos/testing/
|
||||||
|
- name: Publish .deb to Cloudsmith (special tags)
|
||||||
|
if: ${{ steps.vars.outputs.tag_special != '' }}
|
||||||
|
env:
|
||||||
|
CLOUDSMITH_API_KEY: ${{ secrets.CLOUDSMITH_API_KEY }}
|
||||||
|
run: |
|
||||||
|
for filename in dist/*.deb; do
|
||||||
|
# armv6 and armv7 are both "armhf" so we can skip the duplicate
|
||||||
|
if [[ "$filename" == *"armv6"* ]]; then
|
||||||
|
echo "Skipping $filename"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Pushing $filename to 'testing'"
|
||||||
|
cloudsmith push deb caddy/testing/any-distro/any-version $filename
|
||||||
|
done
|
||||||
|
|
||||||
|
# Publish stable tags to Cloudsmith to both repos, "stable" and "testing"
|
||||||
|
# See https://cloudsmith.io/~caddy/repos/stable/
|
||||||
|
- name: Publish .deb to Cloudsmith (stable tags)
|
||||||
|
if: ${{ steps.vars.outputs.tag_special == '' }}
|
||||||
|
env:
|
||||||
|
CLOUDSMITH_API_KEY: ${{ secrets.CLOUDSMITH_API_KEY }}
|
||||||
|
run: |
|
||||||
|
for filename in dist/*.deb; do
|
||||||
|
# armv6 and armv7 are both "armhf" so we can skip the duplicate
|
||||||
|
if [[ "$filename" == *"armv6"* ]]; then
|
||||||
|
echo "Skipping $filename"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Pushing $filename to 'stable'"
|
||||||
|
cloudsmith push deb caddy/stable/any-distro/any-version $filename
|
||||||
|
|
||||||
|
echo "Pushing $filename to 'testing'"
|
||||||
|
cloudsmith push deb caddy/testing/any-distro/any-version $filename
|
||||||
|
done
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
name: Release Published
|
||||||
|
|
||||||
|
# Event payload: https://developer.github.com/webhooks/event-payloads/#release
|
||||||
|
on:
|
||||||
|
release:
|
||||||
|
types: [published]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
release:
|
||||||
|
name: Release Published
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [ ubuntu-latest ]
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
|
||||||
|
# See https://github.com/peter-evans/repository-dispatch
|
||||||
|
- name: Trigger event on caddyserver/dist
|
||||||
|
uses: peter-evans/repository-dispatch@v1
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.REPO_DISPATCH_TOKEN }}
|
||||||
|
repository: caddyserver/dist
|
||||||
|
event-type: release-tagged
|
||||||
|
client-payload: '{"tag": "${{ github.event.release.tag_name }}"}'
|
||||||
|
|
||||||
|
- name: Trigger event on caddyserver/caddy-docker
|
||||||
|
uses: peter-evans/repository-dispatch@v1
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.REPO_DISPATCH_TOKEN }}
|
||||||
|
repository: caddyserver/caddy-docker
|
||||||
|
event-type: release-tagged
|
||||||
|
client-payload: '{"tag": "${{ github.event.release.tag_name }}"}'
|
||||||
|
|
||||||
+6
-1
@@ -1,13 +1,14 @@
|
|||||||
_gitignore/
|
_gitignore/
|
||||||
*.log
|
*.log
|
||||||
Caddyfile
|
Caddyfile
|
||||||
|
Caddyfile.*
|
||||||
!caddyfile/
|
!caddyfile/
|
||||||
|
|
||||||
# artifacts from pprof tooling
|
# artifacts from pprof tooling
|
||||||
*.prof
|
*.prof
|
||||||
*.test
|
*.test
|
||||||
|
|
||||||
# build artifacts
|
# build artifacts and helpers
|
||||||
cmd/caddy/caddy
|
cmd/caddy/caddy
|
||||||
cmd/caddy/caddy.exe
|
cmd/caddy/caddy.exe
|
||||||
|
|
||||||
@@ -21,3 +22,7 @@ vendor
|
|||||||
dist
|
dist
|
||||||
caddy-build
|
caddy-build
|
||||||
caddy-dist
|
caddy-dist
|
||||||
|
|
||||||
|
# IDE files
|
||||||
|
.idea/
|
||||||
|
.vscode/
|
||||||
|
|||||||
+52
-5
@@ -1,21 +1,68 @@
|
|||||||
linters-settings:
|
linters-settings:
|
||||||
errcheck:
|
errcheck:
|
||||||
ignore: fmt:.*,io/ioutil:^Read.*,github.com/caddyserver/caddy/v2/caddyconfig:RegisterAdapter,github.com/caddyserver/caddy/v2:RegisterModule
|
ignore: fmt:.*,go.uber.org/zap/zapcore:^Add.*
|
||||||
ignoretests: true
|
ignoretests: true
|
||||||
misspell:
|
|
||||||
locale: US
|
|
||||||
|
|
||||||
linters:
|
linters:
|
||||||
|
disable-all: true
|
||||||
enable:
|
enable:
|
||||||
- bodyclose
|
- bodyclose
|
||||||
- prealloc
|
- deadcode
|
||||||
- unconvert
|
|
||||||
- errcheck
|
- errcheck
|
||||||
- gofmt
|
- gofmt
|
||||||
- goimports
|
- goimports
|
||||||
- gosec
|
- gosec
|
||||||
|
- gosimple
|
||||||
|
- govet
|
||||||
- ineffassign
|
- ineffassign
|
||||||
- misspell
|
- misspell
|
||||||
|
- prealloc
|
||||||
|
- staticcheck
|
||||||
|
- structcheck
|
||||||
|
- typecheck
|
||||||
|
- unconvert
|
||||||
|
- unused
|
||||||
|
- varcheck
|
||||||
|
# these are implicitly disabled:
|
||||||
|
# - asciicheck
|
||||||
|
# - depguard
|
||||||
|
# - dogsled
|
||||||
|
# - dupl
|
||||||
|
# - exhaustive
|
||||||
|
# - exportloopref
|
||||||
|
# - funlen
|
||||||
|
# - gci
|
||||||
|
# - gochecknoglobals
|
||||||
|
# - gochecknoinits
|
||||||
|
# - gocognit
|
||||||
|
# - goconst
|
||||||
|
# - gocritic
|
||||||
|
# - gocyclo
|
||||||
|
# - godot
|
||||||
|
# - godox
|
||||||
|
# - goerr113
|
||||||
|
# - gofumpt
|
||||||
|
# - goheader
|
||||||
|
# - golint
|
||||||
|
# - gomnd
|
||||||
|
# - gomodguard
|
||||||
|
# - goprintffuncname
|
||||||
|
# - interfacer
|
||||||
|
# - lll
|
||||||
|
# - maligned
|
||||||
|
# - nakedret
|
||||||
|
# - nestif
|
||||||
|
# - nlreturn
|
||||||
|
# - noctx
|
||||||
|
# - nolintlint
|
||||||
|
# - rowserrcheck
|
||||||
|
# - scopelint
|
||||||
|
# - sqlclosecheck
|
||||||
|
# - stylecheck
|
||||||
|
# - testpackage
|
||||||
|
# - unparam
|
||||||
|
# - whitespace
|
||||||
|
# - wsl
|
||||||
|
|
||||||
run:
|
run:
|
||||||
# default concurrency is a available CPU number.
|
# default concurrency is a available CPU number.
|
||||||
|
|||||||
+47
-12
@@ -1,12 +1,18 @@
|
|||||||
before:
|
before:
|
||||||
hooks:
|
hooks:
|
||||||
|
# The build is done in this particular way to build Caddy in a designated directory named in .gitignore.
|
||||||
|
# This is so we can run goreleaser on tag without Git complaining of being dirty. The main.go in cmd/caddy directory
|
||||||
|
# cannot be built within that directory due to changes necessary for the build causing Git to be dirty, which
|
||||||
|
# subsequently causes gorleaser to refuse running.
|
||||||
- mkdir -p caddy-build
|
- mkdir -p caddy-build
|
||||||
- cp cmd/caddy/main.go caddy-build/main.go
|
- cp cmd/caddy/main.go caddy-build/main.go
|
||||||
- cp ./go.mod caddy-build/go.mod
|
- /bin/sh -c 'cd ./caddy-build && go mod init caddy'
|
||||||
- sed -i.bkp 's|github.com/caddyserver/caddy/v2|caddy|g' ./caddy-build/go.mod
|
|
||||||
# GoReleaser doesn't seem to offer {{.Tag}} at this stage, so we have to embed it into the env
|
# GoReleaser doesn't seem to offer {{.Tag}} at this stage, so we have to embed it into the env
|
||||||
# so we run: TAG=$(git describe --abbrev=0) goreleaser release --rm-dist --skip-publish --skip-validate
|
# so we run: TAG=$(git describe --abbrev=0) goreleaser release --rm-dist --skip-publish --skip-validate
|
||||||
- go mod edit -require=github.com/caddyserver/caddy/v2@{{.Env.TAG}} ./caddy-build/go.mod
|
- go mod edit -require=github.com/caddyserver/caddy/v2@{{.Env.TAG}} ./caddy-build/go.mod
|
||||||
|
# as of Go 1.16, `go` commands no longer automatically change go.{mod,sum}. We now have to explicitly
|
||||||
|
# run `go mod tidy`. The `/bin/sh -c '...'` is because goreleaser can't find cd in PATH without shell invocation.
|
||||||
|
- /bin/sh -c 'cd ./caddy-build && go mod tidy'
|
||||||
- git clone --depth 1 https://github.com/caddyserver/dist caddy-dist
|
- git clone --depth 1 https://github.com/caddyserver/dist caddy-dist
|
||||||
- go mod download
|
- go mod download
|
||||||
|
|
||||||
@@ -26,12 +32,30 @@ builds:
|
|||||||
- amd64
|
- amd64
|
||||||
- arm
|
- arm
|
||||||
- arm64
|
- arm64
|
||||||
|
- s390x
|
||||||
|
- ppc64le
|
||||||
goarm:
|
goarm:
|
||||||
- 6
|
- "5"
|
||||||
- 7
|
- "6"
|
||||||
|
- "7"
|
||||||
ignore:
|
ignore:
|
||||||
- goos: darwin
|
- goos: darwin
|
||||||
goarch: arm
|
goarch: arm
|
||||||
|
- goos: darwin
|
||||||
|
goarch: ppc64le
|
||||||
|
- goos: darwin
|
||||||
|
goarch: s390x
|
||||||
|
- goos: windows
|
||||||
|
goarch: ppc64le
|
||||||
|
- goos: windows
|
||||||
|
goarch: s390x
|
||||||
|
- goos: freebsd
|
||||||
|
goarch: ppc64le
|
||||||
|
- goos: freebsd
|
||||||
|
goarch: s390x
|
||||||
|
- goos: freebsd
|
||||||
|
goarch: arm
|
||||||
|
goarm: "5"
|
||||||
flags:
|
flags:
|
||||||
- -trimpath
|
- -trimpath
|
||||||
ldflags:
|
ldflags:
|
||||||
@@ -50,11 +74,11 @@ nfpms:
|
|||||||
- id: default
|
- id: default
|
||||||
package_name: caddy
|
package_name: caddy
|
||||||
|
|
||||||
vendor: Light Code Labs
|
vendor: Dyanim
|
||||||
homepage: https://caddyserver.com
|
homepage: https://caddyserver.com
|
||||||
maintainer: Matthew Holt <mholt@users.noreply.github.com>
|
maintainer: Matthew Holt <mholt@users.noreply.github.com>
|
||||||
description: |
|
description: |
|
||||||
Powerful, enterprise-ready, open source web server with automatic HTTPS written in Go
|
Caddy - Powerful, enterprise-ready, open source web server with automatic HTTPS written in Go
|
||||||
license: Apache 2.0
|
license: Apache 2.0
|
||||||
|
|
||||||
formats:
|
formats:
|
||||||
@@ -62,12 +86,22 @@ nfpms:
|
|||||||
# - rpm
|
# - rpm
|
||||||
|
|
||||||
bindir: /usr/bin
|
bindir: /usr/bin
|
||||||
files:
|
contents:
|
||||||
./caddy-dist/init/caddy.service: /lib/systemd/system/caddy.service
|
- src: ./caddy-dist/init/caddy.service
|
||||||
./caddy-dist/init/caddy-api.service: /lib/systemd/system/caddy-api.service
|
dst: /lib/systemd/system/caddy.service
|
||||||
./caddy-dist/welcome/index.html: /usr/share/caddy/index.html
|
|
||||||
config_files:
|
- src: ./caddy-dist/init/caddy-api.service
|
||||||
./caddy-dist/config/Caddyfile: /etc/caddy/Caddyfile
|
dst: /lib/systemd/system/caddy-api.service
|
||||||
|
|
||||||
|
- src: ./caddy-dist/welcome/index.html
|
||||||
|
dst: /usr/share/caddy/index.html
|
||||||
|
|
||||||
|
- src: ./caddy-dist/scripts/completions/bash-completion
|
||||||
|
dst: /etc/bash_completion.d/caddy
|
||||||
|
|
||||||
|
- src: ./caddy-dist/config/Caddyfile
|
||||||
|
dst: /etc/caddy/Caddyfile
|
||||||
|
type: config
|
||||||
|
|
||||||
scripts:
|
scripts:
|
||||||
postinstall: ./caddy-dist/scripts/postinstall.sh
|
postinstall: ./caddy-dist/scripts/postinstall.sh
|
||||||
@@ -89,5 +123,6 @@ changelog:
|
|||||||
- '^chore:'
|
- '^chore:'
|
||||||
- '^ci:'
|
- '^ci:'
|
||||||
- '^docs?:'
|
- '^docs?:'
|
||||||
|
- '^readme:'
|
||||||
- '^tests?:'
|
- '^tests?:'
|
||||||
- '^\w+\s+' # a hack to remove commit messages without colons thus don't correspond to a package
|
- '^\w+\s+' # a hack to remove commit messages without colons thus don't correspond to a package
|
||||||
|
|||||||
-24
@@ -1,24 +0,0 @@
|
|||||||
branches:
|
|
||||||
only:
|
|
||||||
- master
|
|
||||||
language: go
|
|
||||||
go:
|
|
||||||
- 1.14.x
|
|
||||||
dist: bionic
|
|
||||||
arch:
|
|
||||||
- ppc64le
|
|
||||||
- s390x
|
|
||||||
env:
|
|
||||||
- GO111MODULE=on CGO_ENABLED=0
|
|
||||||
cache:
|
|
||||||
directories:
|
|
||||||
- $GOPATH/pkg/mod
|
|
||||||
- /home/travis/.cache/go-build
|
|
||||||
git:
|
|
||||||
depth: 1
|
|
||||||
|
|
||||||
install:
|
|
||||||
- go get -v -t -d ./...
|
|
||||||
script:
|
|
||||||
- go test -v -short ./...
|
|
||||||
- go build -trimpath -ldflags="-w -s" -v -o cmd/caddy/caddy cmd/caddy/main.go
|
|
||||||
@@ -1,21 +1,25 @@
|
|||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://caddyserver.com"><img src="https://user-images.githubusercontent.com/1128849/36338535-05fb646a-136f-11e8-987b-e6901e717d5a.png" alt="Caddy" width="450"></a>
|
<a href="https://caddyserver.com"><img src="https://user-images.githubusercontent.com/1128849/36338535-05fb646a-136f-11e8-987b-e6901e717d5a.png" alt="Caddy" width="450"></a>
|
||||||
|
<br>
|
||||||
|
<h3 align="center">a <a href="https://zerossl.com"><img src="https://caddyserver.com/resources/images/zerossl-logo.svg" height="28" valign="middle"></a> project</h3>
|
||||||
</p>
|
</p>
|
||||||
|
<hr>
|
||||||
<h3 align="center">Every site on HTTPS</h3>
|
<h3 align="center">Every site on HTTPS</h3>
|
||||||
<p align="center">Caddy is an extensible server platform that uses TLS by default.</p>
|
<p align="center">Caddy is an extensible server platform that uses TLS by default.</p>
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://github.com/caddyserver/caddy/actions?query=workflow%3ACross-Platform"><img src="https://github.com/caddyserver/caddy/workflows/Cross-Platform/badge.svg"></a>
|
<a href="https://github.com/caddyserver/caddy/actions?query=workflow%3ACross-Platform"><img src="https://github.com/caddyserver/caddy/workflows/Cross-Platform/badge.svg"></a>
|
||||||
<a href="https://pkg.go.dev/github.com/caddyserver/caddy/v2"><img src="https://img.shields.io/badge/godoc-reference-blue.svg"></a>
|
<a href="https://pkg.go.dev/github.com/caddyserver/caddy/v2"><img src="https://img.shields.io/badge/godoc-reference-%23007d9c.svg"></a>
|
||||||
<a href="https://app.fuzzit.dev/orgs/caddyserver-gh/dashboard"><img src="https://app.fuzzit.dev/badge?org_id=caddyserver-gh"></a>
|
|
||||||
<br>
|
<br>
|
||||||
<a href="https://twitter.com/caddyserver" title="@caddyserver on Twitter"><img src="https://img.shields.io/badge/twitter-@caddyserver-55acee.svg" alt="@caddyserver on Twitter"></a>
|
<a href="https://twitter.com/caddyserver" title="@caddyserver on Twitter"><img src="https://img.shields.io/badge/twitter-@caddyserver-55acee.svg" alt="@caddyserver on Twitter"></a>
|
||||||
<a href="https://caddy.community" title="Caddy Forum"><img src="https://img.shields.io/badge/community-forum-ff69b4.svg" alt="Caddy Forum"></a>
|
<a href="https://caddy.community" title="Caddy Forum"><img src="https://img.shields.io/badge/community-forum-ff69b4.svg" alt="Caddy Forum"></a>
|
||||||
|
<br>
|
||||||
<a href="https://sourcegraph.com/github.com/caddyserver/caddy?badge" title="Caddy on Sourcegraph"><img src="https://sourcegraph.com/github.com/caddyserver/caddy/-/badge.svg" alt="Caddy on Sourcegraph"></a>
|
<a href="https://sourcegraph.com/github.com/caddyserver/caddy?badge" title="Caddy on Sourcegraph"><img src="https://sourcegraph.com/github.com/caddyserver/caddy/-/badge.svg" alt="Caddy on Sourcegraph"></a>
|
||||||
|
<a href="https://cloudsmith.io/~caddy/repos/"><img src="https://img.shields.io/badge/OSS%20hosting%20by-cloudsmith-blue?logo=cloudsmith" alt="Cloudsmith"></a>
|
||||||
</p>
|
</p>
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://github.com/caddyserver/caddy/releases">Download</a> ·
|
<a href="https://github.com/caddyserver/caddy/releases">Releases</a> ·
|
||||||
<a href="https://caddyserver.com/docs/">Documentation</a> ·
|
<a href="https://caddyserver.com/docs/">Documentation</a> ·
|
||||||
<a href="https://caddy.community">Community</a>
|
<a href="https://caddy.community">Get Help</a>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
|
||||||
@@ -23,10 +27,11 @@
|
|||||||
### Menu
|
### Menu
|
||||||
|
|
||||||
- [Features](#features)
|
- [Features](#features)
|
||||||
|
- [Install](#install)
|
||||||
- [Build from source](#build-from-source)
|
- [Build from source](#build-from-source)
|
||||||
- [For development](#for-development)
|
- [For development](#for-development)
|
||||||
- [With version information and/or plugins](#with-version-information-andor-plugins)
|
- [With version information and/or plugins](#with-version-information-andor-plugins)
|
||||||
- [Getting started](#getting-started)
|
- [Quick start](#quick-start)
|
||||||
- [Overview](#overview)
|
- [Overview](#overview)
|
||||||
- [Full documentation](#full-documentation)
|
- [Full documentation](#full-documentation)
|
||||||
- [Getting help](#getting-help)
|
- [Getting help](#getting-help)
|
||||||
@@ -39,49 +44,71 @@
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
|
||||||
## Features
|
## [Features](https://caddyserver.com/v2)
|
||||||
|
|
||||||
- **Easy configuration** with the [Caddyfile](https://caddyserver.com/docs/caddyfile)
|
- **Easy configuration** with the [Caddyfile](https://caddyserver.com/docs/caddyfile)
|
||||||
- **Powerful configuration** with its [native JSON config](https://caddyserver.com/docs/json/)
|
- **Powerful configuration** with its [native JSON config](https://caddyserver.com/docs/json/)
|
||||||
- **Dynamic configuration** with the [JSON API](https://caddyserver.com/docs/api)
|
- **Dynamic configuration** with the [JSON API](https://caddyserver.com/docs/api)
|
||||||
- [**Config adapters**](https://caddyserver.com/docs/config-adapters) if you don't like JSON
|
- [**Config adapters**](https://caddyserver.com/docs/config-adapters) if you don't like JSON
|
||||||
- **Automatic HTTPS** by default
|
- **Automatic HTTPS** by default
|
||||||
- [Let's Encrypt](https://letsencrypt.org) for public sites
|
- [ZeroSSL](https://zerossl.com) and [Let's Encrypt](https://letsencrypt.org) for public names
|
||||||
- Fully-managed local CA for internal names & IPs
|
- Fully-managed local CA for internal names & IPs
|
||||||
- Can coordinate with other Caddy instances in a cluster
|
- Can coordinate with other Caddy instances in a cluster
|
||||||
|
- Multi-issuer fallback
|
||||||
- **Stays up when other servers go down** due to TLS/OCSP/certificate-related issues
|
- **Stays up when other servers go down** due to TLS/OCSP/certificate-related issues
|
||||||
|
- **Production-ready** after serving trillions of requests and managing millions of TLS certificates
|
||||||
|
- **Scales to tens of thousands of sites** ... and probably more
|
||||||
- **HTTP/1.1, HTTP/2, and experimental HTTP/3** support
|
- **HTTP/1.1, HTTP/2, and experimental HTTP/3** support
|
||||||
- **Highly extensible** [modular architecture](https://caddyserver.com/docs/architecture) lets Caddy do anything without bloat
|
- **Highly extensible** [modular architecture](https://caddyserver.com/docs/architecture) lets Caddy do anything without bloat
|
||||||
- **Runs anywhere** with **no external dependencies** (not even libc)
|
- **Runs anywhere** with **no external dependencies** (not even libc)
|
||||||
- Written in Go, a language with higher **memory safety guarantees** than other servers
|
- Written in Go, a language with higher **memory safety guarantees** than other servers
|
||||||
- Actually **fun to use**
|
- Actually **fun to use**
|
||||||
- So, so much more to discover
|
- So, so much more to [discover](https://caddyserver.com/v2)
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
The simplest, cross-platform way is to download from [GitHub Releases](https://github.com/caddyserver/caddy/releases) and place the executable file in your PATH.
|
||||||
|
|
||||||
|
For other install options, see https://caddyserver.com/docs/install.
|
||||||
|
|
||||||
## Build from source
|
## Build from source
|
||||||
|
|
||||||
Requirements:
|
Requirements:
|
||||||
|
|
||||||
- [Go 1.14 or newer](https://golang.org/dl/)
|
- [Go 1.17 or newer](https://golang.org/dl/)
|
||||||
- Do NOT disable [Go modules](https://github.com/golang/go/wiki/Modules) (`export GO111MODULE=on`)
|
|
||||||
|
|
||||||
### For development
|
### For development
|
||||||
|
|
||||||
|
_**Note:** These steps [will not embed proper version information](https://github.com/golang/go/issues/29228). For that, please follow the instructions in the next section._
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ git clone "https://github.com/caddyserver/caddy.git"
|
$ git clone "https://github.com/caddyserver/caddy.git"
|
||||||
$ cd caddy/cmd/caddy/
|
$ cd caddy/cmd/caddy/
|
||||||
$ go build
|
$ go build
|
||||||
```
|
```
|
||||||
|
|
||||||
_**Note:** These steps [will not embed proper version information](https://github.com/golang/go/issues/29228). For that, please follow the instructions below._
|
When you run Caddy, it may try to bind to low ports unless otherwise specified in your config. If your OS requires elevated privileges for this, you will need to give your new binary permission to do so. On Linux, this can be done easily with: `sudo setcap cap_net_bind_service=+ep ./caddy`
|
||||||
|
|
||||||
|
If you prefer to use `go run` which only creates temporary binaries, you can still do this with the included `setcap.sh` like so:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ go run -exec ./setcap.sh main.go
|
||||||
|
```
|
||||||
|
|
||||||
|
If you don't want to type your password for `setcap`, use `sudo visudo` to edit your sudoers file and allow your user account to run that command without a password, for example:
|
||||||
|
|
||||||
|
```
|
||||||
|
username ALL=(ALL:ALL) NOPASSWD: /usr/sbin/setcap
|
||||||
|
```
|
||||||
|
|
||||||
|
replacing `username` with your actual username. Please be careful and only do this if you know what you are doing! We are only qualified to document how to use Caddy, not Go tooling or your computer, and we are providing these instructions for convenience only; please learn how to use your own computer at your own risk and make any needful adjustments.
|
||||||
|
|
||||||
### With version information and/or plugins
|
### With version information and/or plugins
|
||||||
|
|
||||||
Using [our builder tool](https://github.com/caddyserver/xcaddy)...
|
Using [our builder tool, `xcaddy`](https://github.com/caddyserver/xcaddy)...
|
||||||
|
|
||||||
```
|
```
|
||||||
$ xcaddy build <caddy_version>
|
$ xcaddy build
|
||||||
```
|
```
|
||||||
|
|
||||||
...the following steps are automated:
|
...the following steps are automated:
|
||||||
@@ -90,8 +117,9 @@ $ xcaddy build <caddy_version>
|
|||||||
2. Change into it: `cd caddy`
|
2. Change into it: `cd caddy`
|
||||||
3. Copy [Caddy's main.go](https://github.com/caddyserver/caddy/blob/master/cmd/caddy/main.go) into the empty folder. Add imports for any custom plugins you want to add.
|
3. Copy [Caddy's main.go](https://github.com/caddyserver/caddy/blob/master/cmd/caddy/main.go) into the empty folder. Add imports for any custom plugins you want to add.
|
||||||
4. Initialize a Go module: `go mod init caddy`
|
4. Initialize a Go module: `go mod init caddy`
|
||||||
5. Pin Caddy version: `go get github.com/caddyserver/caddy/v2@TAG` replacing `TAG` with a git tag or commit. You can also pin any plugin versions similarly.
|
5. (Optional) Pin Caddy version: `go get github.com/caddyserver/caddy/v2@version` replacing `version` with a git tag, commit, or branch name.
|
||||||
6. Compile: `go build`
|
6. (Optional) Add plugins by adding their import: `_ "import/path/here"`
|
||||||
|
7. Compile: `go build`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -100,7 +128,7 @@ $ xcaddy build <caddy_version>
|
|||||||
|
|
||||||
The [Caddy website](https://caddyserver.com/docs/) has documentation that includes tutorials, quick-start guides, reference, and more.
|
The [Caddy website](https://caddyserver.com/docs/) has documentation that includes tutorials, quick-start guides, reference, and more.
|
||||||
|
|
||||||
**We recommend that all users do our [Getting Started](https://caddyserver.com/docs/getting-started) guide to become familiar with using Caddy.**
|
**We recommend that all users -- regardless of experience level -- do our [Getting Started](https://caddyserver.com/docs/getting-started) guide to become familiar with using Caddy.**
|
||||||
|
|
||||||
If you've only got a minute, [the website has several quick-start tutorials](https://caddyserver.com/docs/quick-starts) to choose from! However, after finishing a quick-start tutorial, please read more documentation to understand how the software works. 🙂
|
If you've only got a minute, [the website has several quick-start tutorials](https://caddyserver.com/docs/quick-starts) to choose from! However, after finishing a quick-start tutorial, please read more documentation to understand how the software works. 🙂
|
||||||
|
|
||||||
@@ -119,7 +147,7 @@ The primary way to configure Caddy is through [its API](https://caddyserver.com/
|
|||||||
|
|
||||||
Caddy exposes an unprecedented level of control compared to any web server in existence. In Caddy, you are usually setting the actual values of the initialized types in memory that power everything from your HTTP handlers and TLS handshakes to your storage medium. Caddy is also ridiculously extensible, with a powerful plugin system that makes vast improvements over other web servers.
|
Caddy exposes an unprecedented level of control compared to any web server in existence. In Caddy, you are usually setting the actual values of the initialized types in memory that power everything from your HTTP handlers and TLS handshakes to your storage medium. Caddy is also ridiculously extensible, with a powerful plugin system that makes vast improvements over other web servers.
|
||||||
|
|
||||||
To wield the power of this design, you need to know how the config document is structured. Please see the [our documentation site](https://caddyserver.com/docs/) for details about [Caddy's config structure](https://caddyserver.com/docs/json/).
|
To wield the power of this design, you need to know how the config document is structured. Please see [our documentation site](https://caddyserver.com/docs/) for details about [Caddy's config structure](https://caddyserver.com/docs/json/).
|
||||||
|
|
||||||
Nearly all of Caddy's configuration is contained in a single config document, rather than being scattered across CLI flags and env variables and a configuration file as with other web servers. This makes managing your server config more straightforward and reduces hidden variables/factors.
|
Nearly all of Caddy's configuration is contained in a single config document, rather than being scattered across CLI flags and env variables and a configuration file as with other web servers. This makes managing your server config more straightforward and reduces hidden variables/factors.
|
||||||
|
|
||||||
@@ -138,6 +166,8 @@ The docs are also open source. You can contribute to them here: https://github.c
|
|||||||
|
|
||||||
- We **strongly recommend** that all professionals or companies using Caddy get a support contract through [Ardan Labs](https://www.ardanlabs.com/my/contact-us?dd=caddy) before help is needed.
|
- We **strongly recommend** that all professionals or companies using Caddy get a support contract through [Ardan Labs](https://www.ardanlabs.com/my/contact-us?dd=caddy) before help is needed.
|
||||||
|
|
||||||
|
- A [sponsorship](https://github.com/sponsors/mholt) goes a long way! If Caddy is benefitting your company, please consider a sponsorship! This not only helps fund full-time work to ensure the longevity of the project, it's also a great look for your company to your customers and potential customers!
|
||||||
|
|
||||||
- Individuals can exchange help for free on our community forum at https://caddy.community. Remember that people give help out of their spare time and good will. The best way to get help is to give it first!
|
- Individuals can exchange help for free on our community forum at https://caddy.community. Remember that people give help out of their spare time and good will. The best way to get help is to give it first!
|
||||||
|
|
||||||
Please use our [issue tracker](https://github.com/caddyserver/caddy/issues) only for bug reports and feature requests, i.e. actionable development items (support questions will usually be referred to the forums).
|
Please use our [issue tracker](https://github.com/caddyserver/caddy/issues) only for bug reports and feature requests, i.e. actionable development items (support questions will usually be referred to the forums).
|
||||||
@@ -146,7 +176,13 @@ Please use our [issue tracker](https://github.com/caddyserver/caddy/issues) only
|
|||||||
|
|
||||||
## About
|
## About
|
||||||
|
|
||||||
**The name "Caddy" is trademarked.** The name of the software is "Caddy", not "Caddy Server" or "CaddyServer". Please call it "Caddy" or, if you wish to clarify, "the Caddy web server". Caddy is a registered trademark of Light Code Labs, LLC.
|
Matthew Holt began developing Caddy in 2014 while studying computer science at Brigham Young University. (The name "Caddy" was chosen because this software helps with the tedious, mundane tasks of serving the Web, and is also a single place for multiple things to be organized together.) It soon became the first web server to use HTTPS automatically and by default, and now has hundreds of contributors and has served trillions of HTTPS requests.
|
||||||
|
|
||||||
|
**The name "Caddy" is trademarked.** The name of the software is "Caddy", not "Caddy Server" or "CaddyServer". Please call it "Caddy" or, if you wish to clarify, "the Caddy web server". Caddy is a registered trademark of Stack Holdings GmbH.
|
||||||
|
|
||||||
- _Project on Twitter: [@caddyserver](https://twitter.com/caddyserver)_
|
- _Project on Twitter: [@caddyserver](https://twitter.com/caddyserver)_
|
||||||
- _Author on Twitter: [@mholt6](https://twitter.com/mholt6)_
|
- _Author on Twitter: [@mholt6](https://twitter.com/mholt6)_
|
||||||
|
|
||||||
|
Caddy is a project of [ZeroSSL](https://zerossl.com), a Stack Holdings company.
|
||||||
|
|
||||||
|
Debian package repository hosting is graciously provided by [Cloudsmith](https://cloudsmith.com). Cloudsmith is the only fully hosted, cloud-native, universal package management solution, that enables your organization to create, store and share packages in any format, to any place, with total confidence.
|
||||||
+87
-20
@@ -16,10 +16,31 @@ package caddy
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var testCfg = []byte(`{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"myserver": {
|
||||||
|
"listen": ["tcp/localhost:8080-8084"],
|
||||||
|
"read_timeout": "30s"
|
||||||
|
},
|
||||||
|
"yourserver": {
|
||||||
|
"listen": ["127.0.0.1:5000"],
|
||||||
|
"read_header_timeout": "15s"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`)
|
||||||
|
|
||||||
func TestUnsyncedConfigAccess(t *testing.T) {
|
func TestUnsyncedConfigAccess(t *testing.T) {
|
||||||
// each test is performed in sequence, so
|
// each test is performed in sequence, so
|
||||||
// each change builds on the previous ones;
|
// each change builds on the previous ones;
|
||||||
@@ -108,25 +129,71 @@ func TestUnsyncedConfigAccess(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkLoad(b *testing.B) {
|
// TestLoadConcurrent exercises Load under concurrent conditions
|
||||||
for i := 0; i < b.N; i++ {
|
// and is most useful under test with `-race` enabled.
|
||||||
cfg := []byte(`{
|
func TestLoadConcurrent(t *testing.T) {
|
||||||
"apps": {
|
var wg sync.WaitGroup
|
||||||
"http": {
|
|
||||||
"servers": {
|
for i := 0; i < 100; i++ {
|
||||||
"myserver": {
|
wg.Add(1)
|
||||||
"listen": ["tcp/localhost:8080-8084"],
|
go func() {
|
||||||
"read_timeout": "30s"
|
_ = Load(testCfg, true)
|
||||||
},
|
wg.Done()
|
||||||
"yourserver": {
|
}()
|
||||||
"listen": ["127.0.0.1:5000"],
|
}
|
||||||
"read_header_timeout": "15s"
|
wg.Wait()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
type fooModule struct {
|
||||||
}
|
IntField int
|
||||||
}
|
StrField string
|
||||||
`)
|
}
|
||||||
Load(cfg, true)
|
|
||||||
|
func (fooModule) CaddyModule() ModuleInfo {
|
||||||
|
return ModuleInfo{
|
||||||
|
ID: "foo",
|
||||||
|
New: func() Module { return new(fooModule) },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func (fooModule) Start() error { return nil }
|
||||||
|
func (fooModule) Stop() error { return nil }
|
||||||
|
|
||||||
|
func TestETags(t *testing.T) {
|
||||||
|
RegisterModule(fooModule{})
|
||||||
|
|
||||||
|
if err := Load([]byte(`{"apps": {"foo": {"strField": "abc", "intField": 0}}}`), true); err != nil {
|
||||||
|
t.Fatalf("loading: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
const key = "/" + rawConfigKey + "/apps/foo"
|
||||||
|
|
||||||
|
// try update the config with the wrong etag
|
||||||
|
err := changeConfig(http.MethodPost, key, []byte(`{"strField": "abc", "intField": 1}}`), fmt.Sprintf(`"/%s not_an_etag"`, rawConfigKey), false)
|
||||||
|
if apiErr, ok := err.(APIError); !ok || apiErr.HTTPStatus != http.StatusPreconditionFailed {
|
||||||
|
t.Fatalf("expected precondition failed; got %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the etag
|
||||||
|
hash := etagHasher()
|
||||||
|
if err := readConfig(key, hash); err != nil {
|
||||||
|
t.Fatalf("reading: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// do the same update with the correct key
|
||||||
|
err = changeConfig(http.MethodPost, key, []byte(`{"strField": "abc", "intField": 1}`), makeEtag(key, hash), false)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("expected update to work; got %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// now try another update. The hash should no longer match and we should get precondition failed
|
||||||
|
err = changeConfig(http.MethodPost, key, []byte(`{"strField": "abc", "intField": 2}`), makeEtag(key, hash), false)
|
||||||
|
if apiErr, ok := err.(APIError); !ok || apiErr.HTTPStatus != http.StatusPreconditionFailed {
|
||||||
|
t.Fatalf("expected precondition failed; got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkLoad(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
Load(testCfg, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,10 +17,11 @@ package caddy
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@@ -32,7 +33,9 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/caddyserver/caddy/v2/notify"
|
||||||
"github.com/caddyserver/certmagic"
|
"github.com/caddyserver/certmagic"
|
||||||
|
"github.com/google/uuid"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -99,16 +102,36 @@ func Run(cfg *Config) error {
|
|||||||
// if it is different from the current config or
|
// if it is different from the current config or
|
||||||
// forceReload is true.
|
// forceReload is true.
|
||||||
func Load(cfgJSON []byte, forceReload bool) error {
|
func Load(cfgJSON []byte, forceReload bool) error {
|
||||||
return changeConfig(http.MethodPost, "/"+rawConfigKey, cfgJSON, forceReload)
|
if err := notify.NotifyReloading(); err != nil {
|
||||||
|
Log().Error("unable to notify reloading to service manager", zap.Error(err))
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if err := notify.NotifyReadiness(); err != nil {
|
||||||
|
Log().Error("unable to notify readiness to service manager", zap.Error(err))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
err := changeConfig(http.MethodPost, "/"+rawConfigKey, cfgJSON, "", forceReload)
|
||||||
|
if errors.Is(err, errSameConfig) {
|
||||||
|
err = nil // not really an error
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// changeConfig changes the current config (rawCfg) according to the
|
// changeConfig changes the current config (rawCfg) according to the
|
||||||
// method, traversed via the given path, and uses the given input as
|
// method, traversed via the given path, and uses the given input as
|
||||||
// the new value (if applicable; i.e. "DELETE" doesn't have an input).
|
// the new value (if applicable; i.e. "DELETE" doesn't have an input).
|
||||||
// If the resulting config is the same as the previous, no reload will
|
// If the resulting config is the same as the previous, no reload will
|
||||||
// occur unless forceReload is true. This function is safe for
|
// occur unless forceReload is true. If the config is unchanged and not
|
||||||
|
// forcefully reloaded, then errConfigUnchanged This function is safe for
|
||||||
// concurrent use.
|
// concurrent use.
|
||||||
func changeConfig(method, path string, input []byte, forceReload bool) error {
|
// The ifMatchHeader can optionally be given a string of the format:
|
||||||
|
// "<path> <hash>"
|
||||||
|
// where <path> is the absolute path in the config and <hash> is the expected hash of
|
||||||
|
// the config at that path. If the hash in the ifMatchHeader doesn't match
|
||||||
|
// the hash of the config, then an APIError with status 412 will be returned.
|
||||||
|
func changeConfig(method, path string, input []byte, ifMatchHeader string, forceReload bool) error {
|
||||||
switch method {
|
switch method {
|
||||||
case http.MethodGet,
|
case http.MethodGet,
|
||||||
http.MethodHead,
|
http.MethodHead,
|
||||||
@@ -121,6 +144,40 @@ func changeConfig(method, path string, input []byte, forceReload bool) error {
|
|||||||
currentCfgMu.Lock()
|
currentCfgMu.Lock()
|
||||||
defer currentCfgMu.Unlock()
|
defer currentCfgMu.Unlock()
|
||||||
|
|
||||||
|
if ifMatchHeader != "" {
|
||||||
|
// expect the first and last character to be quotes
|
||||||
|
if len(ifMatchHeader) < 2 || ifMatchHeader[0] != '"' || ifMatchHeader[len(ifMatchHeader)-1] != '"' {
|
||||||
|
return APIError{
|
||||||
|
HTTPStatus: http.StatusBadRequest,
|
||||||
|
Err: fmt.Errorf("malformed If-Match header; expect quoted string"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// read out the parts
|
||||||
|
parts := strings.Fields(ifMatchHeader[1 : len(ifMatchHeader)-1])
|
||||||
|
if len(parts) != 2 {
|
||||||
|
return APIError{
|
||||||
|
HTTPStatus: http.StatusBadRequest,
|
||||||
|
Err: fmt.Errorf("malformed If-Match header; expect format \"<path> <hash>\""),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the current hash of the config
|
||||||
|
// at the given path
|
||||||
|
hash := etagHasher()
|
||||||
|
err := unsyncedConfigAccess(http.MethodGet, parts[0], nil, hash)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if hex.EncodeToString(hash.Sum(nil)) != parts[1] {
|
||||||
|
return APIError{
|
||||||
|
HTTPStatus: http.StatusPreconditionFailed,
|
||||||
|
Err: fmt.Errorf("If-Match header did not match current config hash"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err := unsyncedConfigAccess(method, path, input, nil)
|
err := unsyncedConfigAccess(method, path, input, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -130,15 +187,15 @@ func changeConfig(method, path string, input []byte, forceReload bool) error {
|
|||||||
newCfg, err := json.Marshal(rawCfg[rawConfigKey])
|
newCfg, err := json.Marshal(rawCfg[rawConfigKey])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return APIError{
|
return APIError{
|
||||||
Code: http.StatusBadRequest,
|
HTTPStatus: http.StatusBadRequest,
|
||||||
Err: fmt.Errorf("encoding new config: %v", err),
|
Err: fmt.Errorf("encoding new config: %v", err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if nothing changed, no need to do a whole reload unless the client forces it
|
// if nothing changed, no need to do a whole reload unless the client forces it
|
||||||
if !forceReload && bytes.Equal(rawCfgJSON, newCfg) {
|
if !forceReload && bytes.Equal(rawCfgJSON, newCfg) {
|
||||||
Log().Named("admin.api").Info("config is unchanged")
|
Log().Info("config is unchanged")
|
||||||
return nil
|
return errSameConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
// find any IDs in this config and index them
|
// find any IDs in this config and index them
|
||||||
@@ -146,14 +203,14 @@ func changeConfig(method, path string, input []byte, forceReload bool) error {
|
|||||||
err = indexConfigObjects(rawCfg[rawConfigKey], "/"+rawConfigKey, idx)
|
err = indexConfigObjects(rawCfg[rawConfigKey], "/"+rawConfigKey, idx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return APIError{
|
return APIError{
|
||||||
Code: http.StatusInternalServerError,
|
HTTPStatus: http.StatusInternalServerError,
|
||||||
Err: fmt.Errorf("indexing config: %v", err),
|
Err: fmt.Errorf("indexing config: %v", err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// load this new config; if it fails, we need to revert to
|
// load this new config; if it fails, we need to revert to
|
||||||
// our old representation of caddy's actual config
|
// our old representation of caddy's actual config
|
||||||
err = unsyncedDecodeAndRun(newCfg)
|
err = unsyncedDecodeAndRun(newCfg, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if len(rawCfgJSON) > 0 {
|
if len(rawCfgJSON) > 0 {
|
||||||
// restore old config state to keep it consistent
|
// restore old config state to keep it consistent
|
||||||
@@ -233,8 +290,10 @@ func indexConfigObjects(ptr interface{}, configPath string, index map[string]str
|
|||||||
// it as the new config, replacing any other current config.
|
// it as the new config, replacing any other current config.
|
||||||
// It does NOT update the raw config state, as this is a
|
// It does NOT update the raw config state, as this is a
|
||||||
// lower-level function; most callers will want to use Load
|
// lower-level function; most callers will want to use Load
|
||||||
// instead. A write lock on currentCfgMu is required!
|
// instead. A write lock on currentCfgMu is required! If
|
||||||
func unsyncedDecodeAndRun(cfgJSON []byte) error {
|
// allowPersist is false, it will not be persisted to disk,
|
||||||
|
// even if it is configured to.
|
||||||
|
func unsyncedDecodeAndRun(cfgJSON []byte, allowPersist bool) error {
|
||||||
// remove any @id fields from the JSON, which would cause
|
// remove any @id fields from the JSON, which would cause
|
||||||
// loading to break since the field wouldn't be recognized
|
// loading to break since the field wouldn't be recognized
|
||||||
strippedCfgJSON := RemoveMetaFields(cfgJSON)
|
strippedCfgJSON := RemoveMetaFields(cfgJSON)
|
||||||
@@ -245,6 +304,20 @@ func unsyncedDecodeAndRun(cfgJSON []byte) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// prevent recursive config loads; that is a user error, and
|
||||||
|
// although frequent config loads should be safe, we cannot
|
||||||
|
// guarantee that in the presence of third party plugins, nor
|
||||||
|
// do we want this error to go unnoticed (we assume it was a
|
||||||
|
// pulled config if we're not allowed to persist it)
|
||||||
|
if !allowPersist &&
|
||||||
|
newCfg != nil &&
|
||||||
|
newCfg.Admin != nil &&
|
||||||
|
newCfg.Admin.Config != nil &&
|
||||||
|
newCfg.Admin.Config.LoadRaw != nil &&
|
||||||
|
newCfg.Admin.Config.LoadDelay <= 0 {
|
||||||
|
return fmt.Errorf("recursive config loading detected: pulled configs cannot pull other configs without positive load_delay")
|
||||||
|
}
|
||||||
|
|
||||||
// run the new config and start all its apps
|
// run the new config and start all its apps
|
||||||
err = run(newCfg, true)
|
err = run(newCfg, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -259,7 +332,8 @@ func unsyncedDecodeAndRun(cfgJSON []byte) error {
|
|||||||
unsyncedStop(oldCfg)
|
unsyncedStop(oldCfg)
|
||||||
|
|
||||||
// autosave a non-nil config, if not disabled
|
// autosave a non-nil config, if not disabled
|
||||||
if newCfg != nil &&
|
if allowPersist &&
|
||||||
|
newCfg != nil &&
|
||||||
(newCfg.Admin == nil ||
|
(newCfg.Admin == nil ||
|
||||||
newCfg.Admin.Config == nil ||
|
newCfg.Admin.Config == nil ||
|
||||||
newCfg.Admin.Config.Persist == nil ||
|
newCfg.Admin.Config.Persist == nil ||
|
||||||
@@ -271,9 +345,9 @@ func unsyncedDecodeAndRun(cfgJSON []byte) error {
|
|||||||
zap.String("dir", dir),
|
zap.String("dir", dir),
|
||||||
zap.Error(err))
|
zap.Error(err))
|
||||||
} else {
|
} else {
|
||||||
err := ioutil.WriteFile(ConfigAutosavePath, cfgJSON, 0600)
|
err := os.WriteFile(ConfigAutosavePath, cfgJSON, 0600)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
Log().Info("autosaved config", zap.String("file", ConfigAutosavePath))
|
Log().Info("autosaved config (load with --resume flag)", zap.String("file", ConfigAutosavePath))
|
||||||
} else {
|
} else {
|
||||||
Log().Error("unable to autosave config",
|
Log().Error("unable to autosave config",
|
||||||
zap.String("file", ConfigAutosavePath),
|
zap.String("file", ConfigAutosavePath),
|
||||||
@@ -309,21 +383,10 @@ func run(newCfg *Config, start bool) error {
|
|||||||
// been set by a short assignment
|
// been set by a short assignment
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
// start the admin endpoint (and stop any prior one)
|
|
||||||
if start {
|
|
||||||
err = replaceAdmin(newCfg)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("starting caddy administration endpoint: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if newCfg == nil {
|
if newCfg == nil {
|
||||||
return nil
|
newCfg = new(Config)
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepare the new config for use
|
|
||||||
newCfg.apps = make(map[string]App)
|
|
||||||
|
|
||||||
// create a context within which to load
|
// create a context within which to load
|
||||||
// modules - essentially our new config's
|
// modules - essentially our new config's
|
||||||
// execution environment; be sure that
|
// execution environment; be sure that
|
||||||
@@ -357,6 +420,17 @@ func run(newCfg *Config, start bool) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// start the admin endpoint (and stop any prior one)
|
||||||
|
if start {
|
||||||
|
err = replaceLocalAdminServer(newCfg)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("starting caddy administration endpoint: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// prepare the new config for use
|
||||||
|
newCfg.apps = make(map[string]App)
|
||||||
|
|
||||||
// set up global storage and make it CertMagic's default storage, too
|
// set up global storage and make it CertMagic's default storage, too
|
||||||
err = func() error {
|
err = func() error {
|
||||||
if newCfg.StorageRaw != nil {
|
if newCfg.StorageRaw != nil {
|
||||||
@@ -399,9 +473,16 @@ func run(newCfg *Config, start bool) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Provision any admin routers which may need to access
|
||||||
|
// some of the other apps at runtime
|
||||||
|
err = newCfg.Admin.provisionAdminRouters(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Start
|
// Start
|
||||||
return func() error {
|
err = func() error {
|
||||||
var started []string
|
started := make([]string, 0, len(newCfg.apps))
|
||||||
for name, a := range newCfg.apps {
|
for name, a := range newCfg.apps {
|
||||||
err := a.Start()
|
err := a.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -420,6 +501,108 @@ func run(newCfg *Config, start bool) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}()
|
}()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// now that the user's config is running, finish setting up anything else,
|
||||||
|
// such as remote admin endpoint, config loader, etc.
|
||||||
|
return finishSettingUp(ctx, newCfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// finishSettingUp should be run after all apps have successfully started.
|
||||||
|
func finishSettingUp(ctx Context, cfg *Config) error {
|
||||||
|
// establish this server's identity (only after apps are loaded
|
||||||
|
// so that cert management of this endpoint doesn't prevent user's
|
||||||
|
// servers from starting which likely also use HTTP/HTTPS ports;
|
||||||
|
// but before remote management which may depend on these creds)
|
||||||
|
err := manageIdentity(ctx, cfg)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("provisioning remote admin endpoint: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// replace any remote admin endpoint
|
||||||
|
err = replaceRemoteAdminServer(ctx, cfg)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("provisioning remote admin endpoint: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// if dynamic config is requested, set that up and run it
|
||||||
|
if cfg != nil && cfg.Admin != nil && cfg.Admin.Config != nil && cfg.Admin.Config.LoadRaw != nil {
|
||||||
|
val, err := ctx.LoadModule(cfg.Admin.Config, "LoadRaw")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("loading config loader module: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger := Log().Named("config_loader").With(
|
||||||
|
zap.String("module", val.(Module).CaddyModule().ID.Name()),
|
||||||
|
zap.Int("load_delay", int(cfg.Admin.Config.LoadDelay)))
|
||||||
|
|
||||||
|
runLoadedConfig := func(config []byte) error {
|
||||||
|
logger.Info("applying dynamically-loaded config")
|
||||||
|
err := changeConfig(http.MethodPost, "/"+rawConfigKey, config, "", false)
|
||||||
|
if errors.Is(err, errSameConfig) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("failed to run dynamically-loaded config", zap.Error(err))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
logger.Info("successfully applied dynamically-loaded config")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Admin.Config.LoadDelay > 0 {
|
||||||
|
go func() {
|
||||||
|
// the loop is here to iterate ONLY if there is an error, a no-op config load,
|
||||||
|
// or an unchanged config; in which case we simply wait the delay and try again
|
||||||
|
for {
|
||||||
|
timer := time.NewTimer(time.Duration(cfg.Admin.Config.LoadDelay))
|
||||||
|
select {
|
||||||
|
case <-timer.C:
|
||||||
|
loadedConfig, err := val.(ConfigLoader).LoadConfig(ctx)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("failed loading dynamic config; will retry", zap.Error(err))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if loadedConfig == nil {
|
||||||
|
logger.Info("dynamically-loaded config was nil; will retry")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
err = runLoadedConfig(loadedConfig)
|
||||||
|
if errors.Is(err, errSameConfig) {
|
||||||
|
logger.Info("dynamically-loaded config was unchanged; will retry")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
case <-ctx.Done():
|
||||||
|
if !timer.Stop() {
|
||||||
|
<-timer.C
|
||||||
|
}
|
||||||
|
logger.Info("stopping dynamic config loading")
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
} else {
|
||||||
|
// if no LoadDelay is provided, will load config synchronously
|
||||||
|
loadedConfig, err := val.(ConfigLoader).LoadConfig(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("loading dynamic config from %T: %v", val, err)
|
||||||
|
}
|
||||||
|
// do this in a goroutine so current config can finish being loaded; otherwise deadlock
|
||||||
|
go func() { _ = runLoadedConfig(loadedConfig) }()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConfigLoader is a type that can load a Caddy config. If
|
||||||
|
// the return value is non-nil, it must be valid Caddy JSON;
|
||||||
|
// if nil or with non-nil error, it is considered to be a
|
||||||
|
// no-op load and may be retried later.
|
||||||
|
type ConfigLoader interface {
|
||||||
|
LoadConfig(Context) ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop stops running the current configuration.
|
// Stop stops running the current configuration.
|
||||||
@@ -462,17 +645,6 @@ func unsyncedStop(cfg *Config) {
|
|||||||
cfg.cancelFunc()
|
cfg.cancelFunc()
|
||||||
}
|
}
|
||||||
|
|
||||||
// stopAndCleanup calls stop and cleans up anything
|
|
||||||
// else that is expedient. This should only be used
|
|
||||||
// when stopping and not replacing with a new config.
|
|
||||||
func stopAndCleanup() error {
|
|
||||||
if err := Stop(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
certmagic.CleanUpOwnLocks()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate loads, provisions, and validates
|
// Validate loads, provisions, and validates
|
||||||
// cfg, but does not start running it.
|
// cfg, but does not start running it.
|
||||||
func Validate(cfg *Config) error {
|
func Validate(cfg *Config) error {
|
||||||
@@ -483,10 +655,76 @@ func Validate(cfg *Config) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// exitProcess exits the process as gracefully as possible,
|
||||||
|
// but it always exits, even if there are errors doing so.
|
||||||
|
// It stops all apps, cleans up external locks, removes any
|
||||||
|
// PID file, and shuts down admin endpoint(s) in a goroutine.
|
||||||
|
// Errors are logged along the way, and an appropriate exit
|
||||||
|
// code is emitted.
|
||||||
|
func exitProcess(ctx context.Context, logger *zap.Logger) {
|
||||||
|
if logger == nil {
|
||||||
|
logger = Log()
|
||||||
|
}
|
||||||
|
logger.Warn("exiting; byeee!! 👋")
|
||||||
|
|
||||||
|
exitCode := ExitCodeSuccess
|
||||||
|
|
||||||
|
// stop all apps
|
||||||
|
if err := Stop(); err != nil {
|
||||||
|
logger.Error("failed to stop apps", zap.Error(err))
|
||||||
|
exitCode = ExitCodeFailedQuit
|
||||||
|
}
|
||||||
|
|
||||||
|
// clean up certmagic locks
|
||||||
|
certmagic.CleanUpOwnLocks(ctx, logger)
|
||||||
|
|
||||||
|
// remove pidfile
|
||||||
|
if pidfile != "" {
|
||||||
|
err := os.Remove(pidfile)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("cleaning up PID file:",
|
||||||
|
zap.String("pidfile", pidfile),
|
||||||
|
zap.Error(err))
|
||||||
|
exitCode = ExitCodeFailedQuit
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// shut down admin endpoint(s) in goroutines so that
|
||||||
|
// if this function was called from an admin handler,
|
||||||
|
// it has a chance to return gracefully
|
||||||
|
// use goroutine so that we can finish responding to API request
|
||||||
|
go func() {
|
||||||
|
defer func() {
|
||||||
|
logger = logger.With(zap.Int("exit_code", exitCode))
|
||||||
|
if exitCode == ExitCodeSuccess {
|
||||||
|
logger.Info("shutdown complete")
|
||||||
|
} else {
|
||||||
|
logger.Error("unclean shutdown")
|
||||||
|
}
|
||||||
|
os.Exit(exitCode)
|
||||||
|
}()
|
||||||
|
|
||||||
|
if remoteAdminServer != nil {
|
||||||
|
err := stopAdminServer(remoteAdminServer)
|
||||||
|
if err != nil {
|
||||||
|
exitCode = ExitCodeFailedQuit
|
||||||
|
logger.Error("failed to stop remote admin server gracefully", zap.Error(err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if localAdminServer != nil {
|
||||||
|
err := stopAdminServer(localAdminServer)
|
||||||
|
if err != nil {
|
||||||
|
exitCode = ExitCodeFailedQuit
|
||||||
|
logger.Error("failed to stop local admin server gracefully", zap.Error(err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
// Duration can be an integer or a string. An integer is
|
// Duration can be an integer or a string. An integer is
|
||||||
// interpreted as nanoseconds. If a string, it is a Go
|
// interpreted as nanoseconds. If a string, it is a Go
|
||||||
// time.Duration value such as `300ms`, `1.5h`, or `2h45m`;
|
// time.Duration value such as `300ms`, `1.5h`, or `2h45m`;
|
||||||
// valid units are `ns`, `us`/`µs`, `ms`, `s`, `m`, and `h`.
|
// valid units are `ns`, `us`/`µs`, `ms`, `s`, `m`, `h`, and `d`.
|
||||||
type Duration time.Duration
|
type Duration time.Duration
|
||||||
|
|
||||||
// UnmarshalJSON satisfies json.Unmarshaler.
|
// UnmarshalJSON satisfies json.Unmarshaler.
|
||||||
@@ -497,7 +735,7 @@ func (d *Duration) UnmarshalJSON(b []byte) error {
|
|||||||
var dur time.Duration
|
var dur time.Duration
|
||||||
var err error
|
var err error
|
||||||
if b[0] == byte('"') && b[len(b)-1] == byte('"') {
|
if b[0] == byte('"') && b[len(b)-1] == byte('"') {
|
||||||
dur, err = time.ParseDuration(strings.Trim(string(b), `"`))
|
dur, err = ParseDuration(strings.Trim(string(b), `"`))
|
||||||
} else {
|
} else {
|
||||||
err = json.Unmarshal(b, &dur)
|
err = json.Unmarshal(b, &dur)
|
||||||
}
|
}
|
||||||
@@ -505,6 +743,54 @@ func (d *Duration) UnmarshalJSON(b []byte) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseDuration parses a duration string, adding
|
||||||
|
// support for the "d" unit meaning number of days,
|
||||||
|
// where a day is assumed to be 24h.
|
||||||
|
func ParseDuration(s string) (time.Duration, error) {
|
||||||
|
var inNumber bool
|
||||||
|
var numStart int
|
||||||
|
for i := 0; i < len(s); i++ {
|
||||||
|
ch := s[i]
|
||||||
|
if ch == 'd' {
|
||||||
|
daysStr := s[numStart:i]
|
||||||
|
days, err := strconv.ParseFloat(daysStr, 64)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
hours := days * 24.0
|
||||||
|
hoursStr := strconv.FormatFloat(hours, 'f', -1, 64)
|
||||||
|
s = s[:numStart] + hoursStr + "h" + s[i+1:]
|
||||||
|
i--
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !inNumber {
|
||||||
|
numStart = i
|
||||||
|
}
|
||||||
|
inNumber = (ch >= '0' && ch <= '9') || ch == '.' || ch == '-' || ch == '+'
|
||||||
|
}
|
||||||
|
return time.ParseDuration(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// InstanceID returns the UUID for this instance, and generates one if it
|
||||||
|
// does not already exist. The UUID is stored in the local data directory,
|
||||||
|
// regardless of storage configuration, since each instance is intended to
|
||||||
|
// have its own unique ID.
|
||||||
|
func InstanceID() (uuid.UUID, error) {
|
||||||
|
uuidFilePath := filepath.Join(AppDataDir(), "instance.uuid")
|
||||||
|
uuidFileBytes, err := os.ReadFile(uuidFilePath)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
uuid, err := uuid.NewRandom()
|
||||||
|
if err != nil {
|
||||||
|
return uuid, err
|
||||||
|
}
|
||||||
|
err = os.WriteFile(uuidFilePath, []byte(uuid.String()), 0600)
|
||||||
|
return uuid, err
|
||||||
|
} else if err != nil {
|
||||||
|
return [16]byte{}, err
|
||||||
|
}
|
||||||
|
return uuid.ParseBytes(uuidFileBytes)
|
||||||
|
}
|
||||||
|
|
||||||
// GoModule returns the build info of this Caddy
|
// GoModule returns the build info of this Caddy
|
||||||
// build from debug.BuildInfo (requires Go modules).
|
// build from debug.BuildInfo (requires Go modules).
|
||||||
// If no version information is available, a non-nil
|
// If no version information is available, a non-nil
|
||||||
@@ -566,5 +852,10 @@ var (
|
|||||||
rawCfgIndex map[string]string
|
rawCfgIndex map[string]string
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// errSameConfig is returned if the new config is the same
|
||||||
|
// as the old one. This isn't usually an actual, actionable
|
||||||
|
// error; it's mostly a sentinel value.
|
||||||
|
var errSameConfig = errors.New("config is unchanged")
|
||||||
|
|
||||||
// ImportPath is the package import path for Caddy core.
|
// ImportPath is the package import path for Caddy core.
|
||||||
const ImportPath = "github.com/caddyserver/caddy/v2"
|
const ImportPath = "github.com/caddyserver/caddy/v2"
|
||||||
|
|||||||
@@ -0,0 +1,74 @@
|
|||||||
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package caddy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseDuration(t *testing.T) {
|
||||||
|
const day = 24 * time.Hour
|
||||||
|
for i, tc := range []struct {
|
||||||
|
input string
|
||||||
|
expect time.Duration
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
input: "3h",
|
||||||
|
expect: 3 * time.Hour,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "1d",
|
||||||
|
expect: day,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "1d30m",
|
||||||
|
expect: day + 30*time.Minute,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "1m2d",
|
||||||
|
expect: time.Minute + day*2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "1m2d30s",
|
||||||
|
expect: time.Minute + day*2 + 30*time.Second,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "1d2d",
|
||||||
|
expect: 3 * day,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "1.5d",
|
||||||
|
expect: time.Duration(1.5 * float64(day)),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "4m1.25d",
|
||||||
|
expect: 4*time.Minute + time.Duration(1.25*float64(day)),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "-1.25d12h",
|
||||||
|
expect: time.Duration(-1.25*float64(day)) - 12*time.Hour,
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
actual, err := ParseDuration(tc.input)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Test %d ('%s'): Got error: %v", i, tc.input, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if actual != tc.expect {
|
||||||
|
t.Errorf("Test %d ('%s'): Expected=%s Actual=%s", i, tc.input, tc.expect, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,6 +15,7 @@
|
|||||||
package caddyfile
|
package caddyfile
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
@@ -51,15 +52,46 @@ func (a Adapter) Adapt(body []byte, options map[string]interface{}) ([]byte, []c
|
|||||||
return nil, warnings, err
|
return nil, warnings, err
|
||||||
}
|
}
|
||||||
|
|
||||||
marshalFunc := json.Marshal
|
// lint check: see if input was properly formatted; sometimes messy files files parse
|
||||||
if options["pretty"] == "true" {
|
// successfully but result in logical errors (the Caddyfile is a bad format, I'm sorry)
|
||||||
marshalFunc = caddyconfig.JSONIndent
|
if warning, different := formattingDifference(filename, body); different {
|
||||||
|
warnings = append(warnings, warning)
|
||||||
}
|
}
|
||||||
result, err := marshalFunc(cfg)
|
|
||||||
|
result, err := json.Marshal(cfg)
|
||||||
|
|
||||||
return result, warnings, err
|
return result, warnings, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// formattingDifference returns a warning and true if the formatted version
|
||||||
|
// is any different from the input; empty warning and false otherwise.
|
||||||
|
// TODO: also perform this check on imported files
|
||||||
|
func formattingDifference(filename string, body []byte) (caddyconfig.Warning, bool) {
|
||||||
|
// replace windows-style newlines to normalize comparison
|
||||||
|
normalizedBody := bytes.Replace(body, []byte("\r\n"), []byte("\n"), -1)
|
||||||
|
|
||||||
|
formatted := Format(normalizedBody)
|
||||||
|
if bytes.Equal(formatted, normalizedBody) {
|
||||||
|
return caddyconfig.Warning{}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// find where the difference is
|
||||||
|
line := 1
|
||||||
|
for i, ch := range normalizedBody {
|
||||||
|
if i >= len(formatted) || ch != formatted[i] {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if ch == '\n' {
|
||||||
|
line++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return caddyconfig.Warning{
|
||||||
|
File: filename,
|
||||||
|
Line: line,
|
||||||
|
Message: "Caddyfile input is not formatted; run the 'caddy fmt' command to fix inconsistencies",
|
||||||
|
}, true
|
||||||
|
}
|
||||||
|
|
||||||
// Unmarshaler is a type that can unmarshal
|
// Unmarshaler is a type that can unmarshal
|
||||||
// Caddyfile tokens to set itself up for a
|
// Caddyfile tokens to set itself up for a
|
||||||
// JSON encoding. The goal of an unmarshaler
|
// JSON encoding. The goal of an unmarshaler
|
||||||
@@ -87,5 +119,31 @@ type ServerType interface {
|
|||||||
Setup([]ServerBlock, map[string]interface{}) (*caddy.Config, []caddyconfig.Warning, error)
|
Setup([]ServerBlock, map[string]interface{}) (*caddy.Config, []caddyconfig.Warning, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalModule instantiates a module with the given ID and invokes
|
||||||
|
// UnmarshalCaddyfile on the new value using the immediate next segment
|
||||||
|
// of d as input. In other words, d's next token should be the first
|
||||||
|
// token of the module's Caddyfile input.
|
||||||
|
//
|
||||||
|
// This function is used when the next segment of Caddyfile tokens
|
||||||
|
// belongs to another Caddy module. The returned value is often
|
||||||
|
// type-asserted to the module's associated type for practical use
|
||||||
|
// when setting up a config.
|
||||||
|
func UnmarshalModule(d *Dispenser, moduleID string) (Unmarshaler, error) {
|
||||||
|
mod, err := caddy.GetModule(moduleID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, d.Errf("getting module named '%s': %v", moduleID, err)
|
||||||
|
}
|
||||||
|
inst := mod.New()
|
||||||
|
unm, ok := inst.(Unmarshaler)
|
||||||
|
if !ok {
|
||||||
|
return nil, d.Errf("module %s is not a Caddyfile unmarshaler; is %T", mod.ID, inst)
|
||||||
|
}
|
||||||
|
err = unm.UnmarshalCaddyfile(d.NewFromNextSegment())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return unm, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Interface guard
|
// Interface guard
|
||||||
var _ caddyconfig.Adapter = (*Adapter)(nil)
|
var _ caddyconfig.Adapter = (*Adapter)(nil)
|
||||||
|
|||||||
Executable → Regular
+138
-5
@@ -17,6 +17,9 @@ package caddyfile
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -37,6 +40,16 @@ func NewDispenser(tokens []Token) *Dispenser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewTestDispenser parses input into tokens and creates a new
|
||||||
|
// Dispenser for test purposes only; any errors are fatal.
|
||||||
|
func NewTestDispenser(input string) *Dispenser {
|
||||||
|
tokens, err := allTokens("Testfile", []byte(input))
|
||||||
|
if err != nil && err != io.EOF {
|
||||||
|
log.Fatalf("getting all tokens from input: %v", err)
|
||||||
|
}
|
||||||
|
return NewDispenser(tokens)
|
||||||
|
}
|
||||||
|
|
||||||
// Next loads the next token. Returns true if a token
|
// Next loads the next token. Returns true if a token
|
||||||
// was loaded; false otherwise. If false, all tokens
|
// was loaded; false otherwise. If false, all tokens
|
||||||
// have been consumed.
|
// have been consumed.
|
||||||
@@ -189,6 +202,43 @@ func (d *Dispenser) Val() string {
|
|||||||
return d.tokens[d.cursor].Text
|
return d.tokens[d.cursor].Text
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ValRaw gets the raw text of the current token (including quotes).
|
||||||
|
// If there is no token loaded, it returns empty string.
|
||||||
|
func (d *Dispenser) ValRaw() string {
|
||||||
|
if d.cursor < 0 || d.cursor >= len(d.tokens) {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
quote := d.tokens[d.cursor].wasQuoted
|
||||||
|
if quote > 0 {
|
||||||
|
return string(quote) + d.tokens[d.cursor].Text + string(quote) // string literal
|
||||||
|
}
|
||||||
|
return d.tokens[d.cursor].Text
|
||||||
|
}
|
||||||
|
|
||||||
|
// ScalarVal gets value of the current token, converted to the closest
|
||||||
|
// scalar type. If there is no token loaded, it returns nil.
|
||||||
|
func (d *Dispenser) ScalarVal() interface{} {
|
||||||
|
if d.cursor < 0 || d.cursor >= len(d.tokens) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
quote := d.tokens[d.cursor].wasQuoted
|
||||||
|
text := d.tokens[d.cursor].Text
|
||||||
|
|
||||||
|
if quote > 0 {
|
||||||
|
return text // string literal
|
||||||
|
}
|
||||||
|
if num, err := strconv.Atoi(text); err == nil {
|
||||||
|
return num
|
||||||
|
}
|
||||||
|
if num, err := strconv.ParseFloat(text, 64); err == nil {
|
||||||
|
return num
|
||||||
|
}
|
||||||
|
if bool, err := strconv.ParseBool(text); err == nil {
|
||||||
|
return bool
|
||||||
|
}
|
||||||
|
return text
|
||||||
|
}
|
||||||
|
|
||||||
// Line gets the line number of the current token.
|
// Line gets the line number of the current token.
|
||||||
// If there is no token loaded, it returns 0.
|
// If there is no token loaded, it returns 0.
|
||||||
func (d *Dispenser) Line() int {
|
func (d *Dispenser) Line() int {
|
||||||
@@ -237,6 +287,19 @@ func (d *Dispenser) AllArgs(targets ...*string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CountRemainingArgs counts the amount of remaining arguments
|
||||||
|
// (tokens on the same line) without consuming the tokens.
|
||||||
|
func (d *Dispenser) CountRemainingArgs() int {
|
||||||
|
count := 0
|
||||||
|
for d.NextArg() {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
for i := 0; i < count; i++ {
|
||||||
|
d.Prev()
|
||||||
|
}
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
|
||||||
// RemainingArgs loads any more arguments (tokens on the same line)
|
// RemainingArgs loads any more arguments (tokens on the same line)
|
||||||
// into a slice and returns them. Open curly brace tokens also indicate
|
// into a slice and returns them. Open curly brace tokens also indicate
|
||||||
// the end of arguments, and the curly brace is not included in
|
// the end of arguments, and the curly brace is not included in
|
||||||
@@ -249,6 +312,18 @@ func (d *Dispenser) RemainingArgs() []string {
|
|||||||
return args
|
return args
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemainingArgsRaw loads any more arguments (tokens on the same line,
|
||||||
|
// retaining quotes) into a slice and returns them. Open curly brace
|
||||||
|
// tokens also indicate the end of arguments, and the curly brace is
|
||||||
|
// not included in the return value nor is it loaded.
|
||||||
|
func (d *Dispenser) RemainingArgsRaw() []string {
|
||||||
|
var args []string
|
||||||
|
for d.NextArg() {
|
||||||
|
args = append(args, d.ValRaw())
|
||||||
|
}
|
||||||
|
return args
|
||||||
|
}
|
||||||
|
|
||||||
// NewFromNextSegment returns a new dispenser with a copy of
|
// NewFromNextSegment returns a new dispenser with a copy of
|
||||||
// the tokens from the current token until the end of the
|
// the tokens from the current token until the end of the
|
||||||
// "directive" whether that be to the end of the line or
|
// "directive" whether that be to the end of the line or
|
||||||
@@ -333,13 +408,17 @@ func (d *Dispenser) EOFErr() error {
|
|||||||
|
|
||||||
// Err generates a custom parse-time error with a message of msg.
|
// Err generates a custom parse-time error with a message of msg.
|
||||||
func (d *Dispenser) Err(msg string) error {
|
func (d *Dispenser) Err(msg string) error {
|
||||||
msg = fmt.Sprintf("%s:%d - Error during parsing: %s", d.File(), d.Line(), msg)
|
return d.Errf(msg)
|
||||||
return errors.New(msg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errf is like Err, but for formatted error messages
|
// Errf is like Err, but for formatted error messages
|
||||||
func (d *Dispenser) Errf(format string, args ...interface{}) error {
|
func (d *Dispenser) Errf(format string, args ...interface{}) error {
|
||||||
return d.Err(fmt.Sprintf(format, args...))
|
return d.WrapErr(fmt.Errorf(format, args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// WrapErr takes an existing error and adds the Caddyfile file and line number.
|
||||||
|
func (d *Dispenser) WrapErr(err error) error {
|
||||||
|
return fmt.Errorf("%s:%d - Error during parsing: %w", d.File(), d.Line(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete deletes the current token and returns the updated slice
|
// Delete deletes the current token and returns the updated slice
|
||||||
@@ -379,6 +458,60 @@ func (d *Dispenser) isNewLine() bool {
|
|||||||
if d.cursor > len(d.tokens)-1 {
|
if d.cursor > len(d.tokens)-1 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return d.tokens[d.cursor-1].File != d.tokens[d.cursor].File ||
|
|
||||||
d.tokens[d.cursor-1].Line+d.numLineBreaks(d.cursor-1) < d.tokens[d.cursor].Line
|
prev := d.tokens[d.cursor-1]
|
||||||
|
curr := d.tokens[d.cursor]
|
||||||
|
|
||||||
|
// If the previous token is from a different file,
|
||||||
|
// we can assume it's from a different line
|
||||||
|
if prev.File != curr.File {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// The previous token may contain line breaks if
|
||||||
|
// it was quoted and spanned multiple lines. e.g:
|
||||||
|
//
|
||||||
|
// dir "foo
|
||||||
|
// bar
|
||||||
|
// baz"
|
||||||
|
prevLineBreaks := d.numLineBreaks(d.cursor - 1)
|
||||||
|
|
||||||
|
// If the previous token (incl line breaks) ends
|
||||||
|
// on a line earlier than the current token,
|
||||||
|
// then the current token is on a new line
|
||||||
|
return prev.Line+prevLineBreaks < curr.Line
|
||||||
|
}
|
||||||
|
|
||||||
|
// isNextOnNewLine determines whether the current token is on a different
|
||||||
|
// line (higher line number) than the next token. It handles imported
|
||||||
|
// tokens correctly. If there isn't a next token, it returns true.
|
||||||
|
func (d *Dispenser) isNextOnNewLine() bool {
|
||||||
|
if d.cursor < 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if d.cursor >= len(d.tokens)-1 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
curr := d.tokens[d.cursor]
|
||||||
|
next := d.tokens[d.cursor+1]
|
||||||
|
|
||||||
|
// If the next token is from a different file,
|
||||||
|
// we can assume it's from a different line
|
||||||
|
if curr.File != next.File {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// The current token may contain line breaks if
|
||||||
|
// it was quoted and spanned multiple lines. e.g:
|
||||||
|
//
|
||||||
|
// dir "foo
|
||||||
|
// bar
|
||||||
|
// baz"
|
||||||
|
currLineBreaks := d.numLineBreaks(d.cursor)
|
||||||
|
|
||||||
|
// If the current token (incl line breaks) ends
|
||||||
|
// on a line earlier than the next token,
|
||||||
|
// then the next token is on a new line
|
||||||
|
return curr.Line+currLineBreaks < next.Line
|
||||||
}
|
}
|
||||||
|
|||||||
Executable → Regular
+5
-10
@@ -15,8 +15,7 @@
|
|||||||
package caddyfile
|
package caddyfile
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"errors"
|
||||||
"log"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -305,14 +304,10 @@ func TestDispenser_ArgErr_Err(t *testing.T) {
|
|||||||
if !strings.Contains(err.Error(), "foobar") {
|
if !strings.Contains(err.Error(), "foobar") {
|
||||||
t.Errorf("Expected error message with custom message in it ('foobar'); got '%v'", err)
|
t.Errorf("Expected error message with custom message in it ('foobar'); got '%v'", err)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// NewTestDispenser parses input into tokens and creates a new
|
var ErrBarIsFull = errors.New("bar is full")
|
||||||
// Disenser for test purposes only; any errors are fatal.
|
bookingError := d.Errf("unable to reserve: %w", ErrBarIsFull)
|
||||||
func NewTestDispenser(input string) *Dispenser {
|
if !errors.Is(bookingError, ErrBarIsFull) {
|
||||||
tokens, err := allTokens("Testfile", []byte(input))
|
t.Errorf("Errf(): should be able to unwrap the error chain")
|
||||||
if err != nil && err != io.EOF {
|
|
||||||
log.Fatalf("getting all tokens from input: %v", err)
|
|
||||||
}
|
}
|
||||||
return NewDispenser(tokens)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,6 +78,9 @@ func Format(input []byte) []byte {
|
|||||||
if comment {
|
if comment {
|
||||||
if ch == '\n' {
|
if ch == '\n' {
|
||||||
comment = false
|
comment = false
|
||||||
|
space = true
|
||||||
|
nextLine()
|
||||||
|
continue
|
||||||
} else {
|
} else {
|
||||||
write(ch)
|
write(ch)
|
||||||
continue
|
continue
|
||||||
@@ -131,9 +134,6 @@ func Format(input []byte) []byte {
|
|||||||
//////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////
|
||||||
|
|
||||||
if ch == '#' {
|
if ch == '#' {
|
||||||
if !spacePrior && !beginningOfLine {
|
|
||||||
write(' ')
|
|
||||||
}
|
|
||||||
comment = true
|
comment = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
//go:build gofuzz
|
||||||
|
// +build gofuzz
|
||||||
|
|
||||||
|
package caddyfile
|
||||||
|
|
||||||
|
import "bytes"
|
||||||
|
|
||||||
|
func FuzzFormat(input []byte) int {
|
||||||
|
formatted := Format(input)
|
||||||
|
if bytes.Equal(formatted, Format(formatted)) {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
@@ -179,6 +179,11 @@ d {
|
|||||||
{$F}
|
{$F}
|
||||||
}`,
|
}`,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "env var placeholders with port",
|
||||||
|
input: `:{$PORT}`,
|
||||||
|
expect: `:{$PORT}`,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
description: "comments",
|
description: "comments",
|
||||||
input: `#a "\n"
|
input: `#a "\n"
|
||||||
@@ -201,7 +206,7 @@ c
|
|||||||
}
|
}
|
||||||
|
|
||||||
d {
|
d {
|
||||||
e #f
|
e#f
|
||||||
# g
|
# g
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,7 +234,7 @@ bar"
|
|||||||
j {
|
j {
|
||||||
"\"k\" l m"
|
"\"k\" l m"
|
||||||
}`,
|
}`,
|
||||||
expect: `"a \"b\" " #c
|
expect: `"a \"b\" "#c
|
||||||
d
|
d
|
||||||
|
|
||||||
e {
|
e {
|
||||||
@@ -305,6 +310,60 @@ bar "{\"key\":34}"`,
|
|||||||
|
|
||||||
baz`,
|
baz`,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "hash within string is not a comment",
|
||||||
|
input: `redir / /some/#/path`,
|
||||||
|
expect: `redir / /some/#/path`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "brace does not fold into comment above",
|
||||||
|
input: `# comment
|
||||||
|
{
|
||||||
|
foo
|
||||||
|
}`,
|
||||||
|
expect: `# comment
|
||||||
|
{
|
||||||
|
foo
|
||||||
|
}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "matthewpi/vscode-caddyfile-support#13",
|
||||||
|
input: `{
|
||||||
|
email {$ACMEEMAIL}
|
||||||
|
#debug
|
||||||
|
}
|
||||||
|
|
||||||
|
block {
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
expect: `{
|
||||||
|
email {$ACMEEMAIL}
|
||||||
|
#debug
|
||||||
|
}
|
||||||
|
|
||||||
|
block {
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "matthewpi/vscode-caddyfile-support#13 - bad formatting",
|
||||||
|
input: `{
|
||||||
|
email {$ACMEEMAIL}
|
||||||
|
#debug
|
||||||
|
}
|
||||||
|
|
||||||
|
block {
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
expect: `{
|
||||||
|
email {$ACMEEMAIL}
|
||||||
|
#debug
|
||||||
|
}
|
||||||
|
|
||||||
|
block {
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
},
|
||||||
} {
|
} {
|
||||||
// the formatter should output a trailing newline,
|
// the formatter should output a trailing newline,
|
||||||
// even if the tests aren't written to expect that
|
// even if the tests aren't written to expect that
|
||||||
|
|||||||
@@ -0,0 +1,127 @@
|
|||||||
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package caddyfile
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type adjacency map[string][]string
|
||||||
|
|
||||||
|
type importGraph struct {
|
||||||
|
nodes map[string]bool
|
||||||
|
edges adjacency
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *importGraph) addNode(name string) {
|
||||||
|
if i.nodes == nil {
|
||||||
|
i.nodes = make(map[string]bool)
|
||||||
|
}
|
||||||
|
if _, exists := i.nodes[name]; exists {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
i.nodes[name] = true
|
||||||
|
}
|
||||||
|
func (i *importGraph) addNodes(names []string) {
|
||||||
|
for _, name := range names {
|
||||||
|
i.addNode(name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *importGraph) removeNode(name string) {
|
||||||
|
delete(i.nodes, name)
|
||||||
|
}
|
||||||
|
func (i *importGraph) removeNodes(names []string) {
|
||||||
|
for _, name := range names {
|
||||||
|
i.removeNode(name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *importGraph) addEdge(from, to string) error {
|
||||||
|
if !i.exists(from) || !i.exists(to) {
|
||||||
|
return fmt.Errorf("one of the nodes does not exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
if i.willCycle(to, from) {
|
||||||
|
return fmt.Errorf("a cycle of imports exists between %s and %s", from, to)
|
||||||
|
}
|
||||||
|
|
||||||
|
if i.areConnected(from, to) {
|
||||||
|
// if connected, there's nothing to do
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if i.nodes == nil {
|
||||||
|
i.nodes = make(map[string]bool)
|
||||||
|
}
|
||||||
|
if i.edges == nil {
|
||||||
|
i.edges = make(adjacency)
|
||||||
|
}
|
||||||
|
|
||||||
|
i.edges[from] = append(i.edges[from], to)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (i *importGraph) addEdges(from string, tos []string) error {
|
||||||
|
for _, to := range tos {
|
||||||
|
err := i.addEdge(from, to)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *importGraph) areConnected(from, to string) bool {
|
||||||
|
al, ok := i.edges[from]
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, v := range al {
|
||||||
|
if v == to {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *importGraph) willCycle(from, to string) bool {
|
||||||
|
collector := make(map[string]bool)
|
||||||
|
|
||||||
|
var visit func(string)
|
||||||
|
visit = func(start string) {
|
||||||
|
if !collector[start] {
|
||||||
|
collector[start] = true
|
||||||
|
for _, v := range i.edges[start] {
|
||||||
|
visit(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range i.edges[from] {
|
||||||
|
visit(v)
|
||||||
|
}
|
||||||
|
for k := range collector {
|
||||||
|
if to == k {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *importGraph) exists(key string) bool {
|
||||||
|
_, exists := i.nodes[key]
|
||||||
|
return exists
|
||||||
|
}
|
||||||
Executable → Regular
+44
-14
@@ -1,4 +1,4 @@
|
|||||||
// Copyright 2015 Light Code Labs, LLC
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// you may not use this file except in compliance with the License.
|
// you may not use this file except in compliance with the License.
|
||||||
@@ -16,6 +16,7 @@ package caddyfile
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"unicode"
|
"unicode"
|
||||||
)
|
)
|
||||||
@@ -34,9 +35,12 @@ type (
|
|||||||
|
|
||||||
// Token represents a single parsable unit.
|
// Token represents a single parsable unit.
|
||||||
Token struct {
|
Token struct {
|
||||||
File string
|
File string
|
||||||
Line int
|
Line int
|
||||||
Text string
|
Text string
|
||||||
|
wasQuoted rune // enclosing quote character, if any
|
||||||
|
inSnippet bool
|
||||||
|
snippetName string
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -73,10 +77,11 @@ func (l *lexer) load(input io.Reader) error {
|
|||||||
// a token was loaded; false otherwise.
|
// a token was loaded; false otherwise.
|
||||||
func (l *lexer) next() bool {
|
func (l *lexer) next() bool {
|
||||||
var val []rune
|
var val []rune
|
||||||
var comment, quoted, escaped bool
|
var comment, quoted, btQuoted, escaped bool
|
||||||
|
|
||||||
makeToken := func() bool {
|
makeToken := func(quoted rune) bool {
|
||||||
l.token.Text = string(val)
|
l.token.Text = string(val)
|
||||||
|
l.token.wasQuoted = quoted
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +89,7 @@ func (l *lexer) next() bool {
|
|||||||
ch, _, err := l.reader.ReadRune()
|
ch, _, err := l.reader.ReadRune()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if len(val) > 0 {
|
if len(val) > 0 {
|
||||||
return makeToken()
|
return makeToken(0)
|
||||||
}
|
}
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
return false
|
return false
|
||||||
@@ -92,13 +97,13 @@ func (l *lexer) next() bool {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !escaped && ch == '\\' {
|
if !escaped && !btQuoted && ch == '\\' {
|
||||||
escaped = true
|
escaped = true
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if quoted {
|
if quoted || btQuoted {
|
||||||
if escaped {
|
if quoted && escaped {
|
||||||
// all is literal in quoted area,
|
// all is literal in quoted area,
|
||||||
// so only escape quotes
|
// so only escape quotes
|
||||||
if ch != '"' {
|
if ch != '"' {
|
||||||
@@ -106,8 +111,11 @@ func (l *lexer) next() bool {
|
|||||||
}
|
}
|
||||||
escaped = false
|
escaped = false
|
||||||
} else {
|
} else {
|
||||||
if ch == '"' {
|
if quoted && ch == '"' {
|
||||||
return makeToken()
|
return makeToken('"')
|
||||||
|
}
|
||||||
|
if btQuoted && ch == '`' {
|
||||||
|
return makeToken('`')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ch == '\n' {
|
if ch == '\n' {
|
||||||
@@ -133,12 +141,12 @@ func (l *lexer) next() bool {
|
|||||||
comment = false
|
comment = false
|
||||||
}
|
}
|
||||||
if len(val) > 0 {
|
if len(val) > 0 {
|
||||||
return makeToken()
|
return makeToken(0)
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if ch == '#' {
|
if ch == '#' && len(val) == 0 {
|
||||||
comment = true
|
comment = true
|
||||||
}
|
}
|
||||||
if comment {
|
if comment {
|
||||||
@@ -151,6 +159,10 @@ func (l *lexer) next() bool {
|
|||||||
quoted = true
|
quoted = true
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if ch == '`' {
|
||||||
|
btQuoted = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if escaped {
|
if escaped {
|
||||||
@@ -161,3 +173,21 @@ func (l *lexer) next() bool {
|
|||||||
val = append(val, ch)
|
val = append(val, ch)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tokenize takes bytes as input and lexes it into
|
||||||
|
// a list of tokens that can be parsed as a Caddyfile.
|
||||||
|
// Also takes a filename to fill the token's File as
|
||||||
|
// the source of the tokens, which is important to
|
||||||
|
// determine relative paths for `import` directives.
|
||||||
|
func Tokenize(input []byte, filename string) ([]Token, error) {
|
||||||
|
l := lexer{}
|
||||||
|
if err := l.load(bytes.NewReader(input)); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var tokens []Token
|
||||||
|
for l.next() {
|
||||||
|
l.token.File = filename
|
||||||
|
tokens = append(tokens, l.token)
|
||||||
|
}
|
||||||
|
return tokens, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
//go:build gofuzz
|
||||||
|
// +build gofuzz
|
||||||
|
|
||||||
|
package caddyfile
|
||||||
|
|
||||||
|
func FuzzTokenize(input []byte) int {
|
||||||
|
tokens, err := Tokenize(input, "Caddyfile")
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
if len(tokens) == 0 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return 1
|
||||||
|
}
|
||||||
Executable → Regular
+83
-41
@@ -1,4 +1,4 @@
|
|||||||
// Copyright 2015 Light Code Labs, LLC
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// you may not use this file except in compliance with the License.
|
// you may not use this file except in compliance with the License.
|
||||||
@@ -15,37 +15,35 @@
|
|||||||
package caddyfile
|
package caddyfile
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type lexerTestCase struct {
|
type lexerTestCase struct {
|
||||||
input string
|
input []byte
|
||||||
expected []Token
|
expected []Token
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLexer(t *testing.T) {
|
func TestLexer(t *testing.T) {
|
||||||
testCases := []lexerTestCase{
|
testCases := []lexerTestCase{
|
||||||
{
|
{
|
||||||
input: `host:123`,
|
input: []byte(`host:123`),
|
||||||
expected: []Token{
|
expected: []Token{
|
||||||
{Line: 1, Text: "host:123"},
|
{Line: 1, Text: "host:123"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: `host:123
|
input: []byte(`host:123
|
||||||
|
|
||||||
directive`,
|
directive`),
|
||||||
expected: []Token{
|
expected: []Token{
|
||||||
{Line: 1, Text: "host:123"},
|
{Line: 1, Text: "host:123"},
|
||||||
{Line: 3, Text: "directive"},
|
{Line: 3, Text: "directive"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: `host:123 {
|
input: []byte(`host:123 {
|
||||||
directive
|
directive
|
||||||
}`,
|
}`),
|
||||||
expected: []Token{
|
expected: []Token{
|
||||||
{Line: 1, Text: "host:123"},
|
{Line: 1, Text: "host:123"},
|
||||||
{Line: 1, Text: "{"},
|
{Line: 1, Text: "{"},
|
||||||
@@ -54,7 +52,7 @@ func TestLexer(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: `host:123 { directive }`,
|
input: []byte(`host:123 { directive }`),
|
||||||
expected: []Token{
|
expected: []Token{
|
||||||
{Line: 1, Text: "host:123"},
|
{Line: 1, Text: "host:123"},
|
||||||
{Line: 1, Text: "{"},
|
{Line: 1, Text: "{"},
|
||||||
@@ -63,12 +61,12 @@ func TestLexer(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: `host:123 {
|
input: []byte(`host:123 {
|
||||||
#comment
|
#comment
|
||||||
directive
|
directive
|
||||||
# comment
|
# comment
|
||||||
foobar # another comment
|
foobar # another comment
|
||||||
}`,
|
}`),
|
||||||
expected: []Token{
|
expected: []Token{
|
||||||
{Line: 1, Text: "host:123"},
|
{Line: 1, Text: "host:123"},
|
||||||
{Line: 1, Text: "{"},
|
{Line: 1, Text: "{"},
|
||||||
@@ -78,8 +76,28 @@ func TestLexer(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: `a "quoted value" b
|
input: []byte(`host:123 {
|
||||||
foobar`,
|
# hash inside string is not a comment
|
||||||
|
redir / /some/#/path
|
||||||
|
}`),
|
||||||
|
expected: []Token{
|
||||||
|
{Line: 1, Text: "host:123"},
|
||||||
|
{Line: 1, Text: "{"},
|
||||||
|
{Line: 3, Text: "redir"},
|
||||||
|
{Line: 3, Text: "/"},
|
||||||
|
{Line: 3, Text: "/some/#/path"},
|
||||||
|
{Line: 4, Text: "}"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: []byte("# comment at beginning of file\n# comment at beginning of line\nhost:123"),
|
||||||
|
expected: []Token{
|
||||||
|
{Line: 3, Text: "host:123"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: []byte(`a "quoted value" b
|
||||||
|
foobar`),
|
||||||
expected: []Token{
|
expected: []Token{
|
||||||
{Line: 1, Text: "a"},
|
{Line: 1, Text: "a"},
|
||||||
{Line: 1, Text: "quoted value"},
|
{Line: 1, Text: "quoted value"},
|
||||||
@@ -88,7 +106,7 @@ func TestLexer(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: `A "quoted \"value\" inside" B`,
|
input: []byte(`A "quoted \"value\" inside" B`),
|
||||||
expected: []Token{
|
expected: []Token{
|
||||||
{Line: 1, Text: "A"},
|
{Line: 1, Text: "A"},
|
||||||
{Line: 1, Text: `quoted "value" inside`},
|
{Line: 1, Text: `quoted "value" inside`},
|
||||||
@@ -96,7 +114,7 @@ func TestLexer(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "An escaped \"newline\\\ninside\" quotes",
|
input: []byte("An escaped \"newline\\\ninside\" quotes"),
|
||||||
expected: []Token{
|
expected: []Token{
|
||||||
{Line: 1, Text: "An"},
|
{Line: 1, Text: "An"},
|
||||||
{Line: 1, Text: "escaped"},
|
{Line: 1, Text: "escaped"},
|
||||||
@@ -105,7 +123,7 @@ func TestLexer(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "An escaped newline\\\noutside quotes",
|
input: []byte("An escaped newline\\\noutside quotes"),
|
||||||
expected: []Token{
|
expected: []Token{
|
||||||
{Line: 1, Text: "An"},
|
{Line: 1, Text: "An"},
|
||||||
{Line: 1, Text: "escaped"},
|
{Line: 1, Text: "escaped"},
|
||||||
@@ -115,7 +133,7 @@ func TestLexer(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "line1\\\nescaped\nline2\nline3",
|
input: []byte("line1\\\nescaped\nline2\nline3"),
|
||||||
expected: []Token{
|
expected: []Token{
|
||||||
{Line: 1, Text: "line1"},
|
{Line: 1, Text: "line1"},
|
||||||
{Line: 1, Text: "escaped"},
|
{Line: 1, Text: "escaped"},
|
||||||
@@ -124,7 +142,7 @@ func TestLexer(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "line1\\\nescaped1\\\nescaped2\nline4\nline5",
|
input: []byte("line1\\\nescaped1\\\nescaped2\nline4\nline5"),
|
||||||
expected: []Token{
|
expected: []Token{
|
||||||
{Line: 1, Text: "line1"},
|
{Line: 1, Text: "line1"},
|
||||||
{Line: 1, Text: "escaped1"},
|
{Line: 1, Text: "escaped1"},
|
||||||
@@ -134,34 +152,34 @@ func TestLexer(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: `"unescapable\ in quotes"`,
|
input: []byte(`"unescapable\ in quotes"`),
|
||||||
expected: []Token{
|
expected: []Token{
|
||||||
{Line: 1, Text: `unescapable\ in quotes`},
|
{Line: 1, Text: `unescapable\ in quotes`},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: `"don't\escape"`,
|
input: []byte(`"don't\escape"`),
|
||||||
expected: []Token{
|
expected: []Token{
|
||||||
{Line: 1, Text: `don't\escape`},
|
{Line: 1, Text: `don't\escape`},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: `"don't\\escape"`,
|
input: []byte(`"don't\\escape"`),
|
||||||
expected: []Token{
|
expected: []Token{
|
||||||
{Line: 1, Text: `don't\\escape`},
|
{Line: 1, Text: `don't\\escape`},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: `un\escapable`,
|
input: []byte(`un\escapable`),
|
||||||
expected: []Token{
|
expected: []Token{
|
||||||
{Line: 1, Text: `un\escapable`},
|
{Line: 1, Text: `un\escapable`},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: `A "quoted value with line
|
input: []byte(`A "quoted value with line
|
||||||
break inside" {
|
break inside" {
|
||||||
foobar
|
foobar
|
||||||
}`,
|
}`),
|
||||||
expected: []Token{
|
expected: []Token{
|
||||||
{Line: 1, Text: "A"},
|
{Line: 1, Text: "A"},
|
||||||
{Line: 1, Text: "quoted value with line\n\t\t\t\t\tbreak inside"},
|
{Line: 1, Text: "quoted value with line\n\t\t\t\t\tbreak inside"},
|
||||||
@@ -171,13 +189,13 @@ func TestLexer(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: `"C:\php\php-cgi.exe"`,
|
input: []byte(`"C:\php\php-cgi.exe"`),
|
||||||
expected: []Token{
|
expected: []Token{
|
||||||
{Line: 1, Text: `C:\php\php-cgi.exe`},
|
{Line: 1, Text: `C:\php\php-cgi.exe`},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: `empty "" string`,
|
input: []byte(`empty "" string`),
|
||||||
expected: []Token{
|
expected: []Token{
|
||||||
{Line: 1, Text: `empty`},
|
{Line: 1, Text: `empty`},
|
||||||
{Line: 1, Text: ``},
|
{Line: 1, Text: ``},
|
||||||
@@ -185,7 +203,7 @@ func TestLexer(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "skip those\r\nCR characters",
|
input: []byte("skip those\r\nCR characters"),
|
||||||
expected: []Token{
|
expected: []Token{
|
||||||
{Line: 1, Text: "skip"},
|
{Line: 1, Text: "skip"},
|
||||||
{Line: 1, Text: "those"},
|
{Line: 1, Text: "those"},
|
||||||
@@ -194,30 +212,54 @@ func TestLexer(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "\xEF\xBB\xBF:8080", // test with leading byte order mark
|
input: []byte("\xEF\xBB\xBF:8080"), // test with leading byte order mark
|
||||||
expected: []Token{
|
expected: []Token{
|
||||||
{Line: 1, Text: ":8080"},
|
{Line: 1, Text: ":8080"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
input: []byte("simple `backtick quoted` string"),
|
||||||
|
expected: []Token{
|
||||||
|
{Line: 1, Text: `simple`},
|
||||||
|
{Line: 1, Text: `backtick quoted`},
|
||||||
|
{Line: 1, Text: `string`},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: []byte("multiline `backtick\nquoted\n` string"),
|
||||||
|
expected: []Token{
|
||||||
|
{Line: 1, Text: `multiline`},
|
||||||
|
{Line: 1, Text: "backtick\nquoted\n"},
|
||||||
|
{Line: 3, Text: `string`},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: []byte("nested `\"quotes inside\" backticks` string"),
|
||||||
|
expected: []Token{
|
||||||
|
{Line: 1, Text: `nested`},
|
||||||
|
{Line: 1, Text: `"quotes inside" backticks`},
|
||||||
|
{Line: 1, Text: `string`},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: []byte("reverse-nested \"`backticks` inside\" quotes"),
|
||||||
|
expected: []Token{
|
||||||
|
{Line: 1, Text: `reverse-nested`},
|
||||||
|
{Line: 1, Text: "`backticks` inside"},
|
||||||
|
{Line: 1, Text: `quotes`},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, testCase := range testCases {
|
for i, testCase := range testCases {
|
||||||
actual := tokenize(testCase.input)
|
actual, err := Tokenize(testCase.input, "")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%v", err)
|
||||||
|
}
|
||||||
lexerCompare(t, i, testCase.expected, actual)
|
lexerCompare(t, i, testCase.expected, actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func tokenize(input string) (tokens []Token) {
|
|
||||||
l := lexer{}
|
|
||||||
if err := l.load(strings.NewReader(input)); err != nil {
|
|
||||||
log.Printf("[ERROR] load failed: %v", err)
|
|
||||||
}
|
|
||||||
for l.next() {
|
|
||||||
tokens = append(tokens, l.token)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func lexerCompare(t *testing.T, n int, expected, actual []Token) {
|
func lexerCompare(t *testing.T, n int, expected, actual []Token) {
|
||||||
if len(expected) != len(actual) {
|
if len(expected) != len(actual) {
|
||||||
t.Errorf("Test case %d: expected %d token(s) but got %d", n, len(expected), len(actual))
|
t.Errorf("Test case %d: expected %d token(s) but got %d", n, len(expected), len(actual))
|
||||||
|
|||||||
Executable → Regular
+137
-43
@@ -1,4 +1,4 @@
|
|||||||
// Copyright 2015 Light Code Labs, LLC
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// you may not use this file except in compliance with the License.
|
// you may not use this file except in compliance with the License.
|
||||||
@@ -16,11 +16,15 @@ package caddyfile
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io/ioutil"
|
"fmt"
|
||||||
"log"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/caddyserver/caddy/v2"
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Parse parses the input just enough to group tokens, in
|
// Parse parses the input just enough to group tokens, in
|
||||||
@@ -33,15 +37,43 @@ import (
|
|||||||
// Environment variables in {$ENVIRONMENT_VARIABLE} notation
|
// Environment variables in {$ENVIRONMENT_VARIABLE} notation
|
||||||
// will be replaced before parsing begins.
|
// will be replaced before parsing begins.
|
||||||
func Parse(filename string, input []byte) ([]ServerBlock, error) {
|
func Parse(filename string, input []byte) ([]ServerBlock, error) {
|
||||||
tokens, err := allTokens(filename, input)
|
// unfortunately, we must copy the input because parsing must
|
||||||
|
// remain a read-only operation, but we have to expand environment
|
||||||
|
// variables before we parse, which changes the underlying array (#4422)
|
||||||
|
inputCopy := make([]byte, len(input))
|
||||||
|
copy(inputCopy, input)
|
||||||
|
|
||||||
|
tokens, err := allTokens(filename, inputCopy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
p := parser{Dispenser: NewDispenser(tokens)}
|
p := parser{
|
||||||
|
Dispenser: NewDispenser(tokens),
|
||||||
|
importGraph: importGraph{
|
||||||
|
nodes: make(map[string]bool),
|
||||||
|
edges: make(adjacency),
|
||||||
|
},
|
||||||
|
}
|
||||||
return p.parseAll()
|
return p.parseAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allTokens lexes the entire input, but does not parse it.
|
||||||
|
// It returns all the tokens from the input, unstructured
|
||||||
|
// and in order. It may mutate input as it expands env vars.
|
||||||
|
func allTokens(filename string, input []byte) ([]Token, error) {
|
||||||
|
inputCopy, err := replaceEnvVars(input)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
tokens, err := Tokenize(inputCopy, filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return tokens, nil
|
||||||
|
}
|
||||||
|
|
||||||
// replaceEnvVars replaces all occurrences of environment variables.
|
// replaceEnvVars replaces all occurrences of environment variables.
|
||||||
|
// It mutates the underlying array and returns the updated slice.
|
||||||
func replaceEnvVars(input []byte) ([]byte, error) {
|
func replaceEnvVars(input []byte) ([]byte, error) {
|
||||||
var offset int
|
var offset int
|
||||||
for {
|
for {
|
||||||
@@ -57,52 +89,42 @@ func replaceEnvVars(input []byte) ([]byte, error) {
|
|||||||
end += begin + len(spanOpen) // make end relative to input, not begin
|
end += begin + len(spanOpen) // make end relative to input, not begin
|
||||||
|
|
||||||
// get the name; if there is no name, skip it
|
// get the name; if there is no name, skip it
|
||||||
envVarName := input[begin+len(spanOpen) : end]
|
envString := input[begin+len(spanOpen) : end]
|
||||||
if len(envVarName) == 0 {
|
if len(envString) == 0 {
|
||||||
offset = end + len(spanClose)
|
offset = end + len(spanClose)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// split the string into a key and an optional default
|
||||||
|
envParts := strings.SplitN(string(envString), envVarDefaultDelimiter, 2)
|
||||||
|
|
||||||
|
// do a lookup for the env var, replace with the default if not found
|
||||||
|
envVarValue, found := os.LookupEnv(envParts[0])
|
||||||
|
if !found && len(envParts) == 2 {
|
||||||
|
envVarValue = envParts[1]
|
||||||
|
}
|
||||||
|
|
||||||
// get the value of the environment variable
|
// get the value of the environment variable
|
||||||
envVarValue := []byte(os.ExpandEnv(os.Getenv(string(envVarName))))
|
// note that this causes one-level deep chaining
|
||||||
|
envVarBytes := []byte(envVarValue)
|
||||||
|
|
||||||
// splice in the value
|
// splice in the value
|
||||||
input = append(input[:begin],
|
input = append(input[:begin],
|
||||||
append(envVarValue, input[end+len(spanClose):]...)...)
|
append(envVarBytes, input[end+len(spanClose):]...)...)
|
||||||
|
|
||||||
// continue at the end of the replacement
|
// continue at the end of the replacement
|
||||||
offset = begin + len(envVarValue)
|
offset = begin + len(envVarBytes)
|
||||||
}
|
}
|
||||||
return input, nil
|
return input, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// allTokens lexes the entire input, but does not parse it.
|
|
||||||
// It returns all the tokens from the input, unstructured
|
|
||||||
// and in order.
|
|
||||||
func allTokens(filename string, input []byte) ([]Token, error) {
|
|
||||||
input, err := replaceEnvVars(input)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
l := new(lexer)
|
|
||||||
err = l.load(bytes.NewReader(input))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var tokens []Token
|
|
||||||
for l.next() {
|
|
||||||
l.token.File = filename
|
|
||||||
tokens = append(tokens, l.token)
|
|
||||||
}
|
|
||||||
return tokens, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type parser struct {
|
type parser struct {
|
||||||
*Dispenser
|
*Dispenser
|
||||||
block ServerBlock // current server block being parsed
|
block ServerBlock // current server block being parsed
|
||||||
eof bool // if we encounter a valid EOF in a hard place
|
eof bool // if we encounter a valid EOF in a hard place
|
||||||
definedSnippets map[string][]Token
|
definedSnippets map[string][]Token
|
||||||
nesting int
|
nesting int
|
||||||
|
importGraph importGraph
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *parser) parseAll() ([]ServerBlock, error) {
|
func (p *parser) parseAll() ([]ServerBlock, error) {
|
||||||
@@ -158,6 +180,15 @@ func (p *parser) begin() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// Just as we need to track which file the token comes from, we need to
|
||||||
|
// keep track of which snippets do the tokens come from. This is helpful
|
||||||
|
// in tracking import cycles across files/snippets by namespacing them. Without
|
||||||
|
// this we end up with false-positives in cycle-detection.
|
||||||
|
for k, v := range tokens {
|
||||||
|
v.inSnippet = true
|
||||||
|
v.snippetName = name
|
||||||
|
tokens[k] = v
|
||||||
|
}
|
||||||
p.definedSnippets[name] = tokens
|
p.definedSnippets[name] = tokens
|
||||||
// empty block keys so we don't save this block as a real server.
|
// empty block keys so we don't save this block as a real server.
|
||||||
p.block.Keys = nil
|
p.block.Keys = nil
|
||||||
@@ -187,9 +218,20 @@ func (p *parser) addresses() error {
|
|||||||
if expectingAnother {
|
if expectingAnother {
|
||||||
return p.Errf("Expected another address but had '%s' - check for extra comma", tkn)
|
return p.Errf("Expected another address but had '%s' - check for extra comma", tkn)
|
||||||
}
|
}
|
||||||
|
// Mark this server block as being defined with braces.
|
||||||
|
// This is used to provide a better error message when
|
||||||
|
// the user may have tried to define two server blocks
|
||||||
|
// without having used braces, which are required in
|
||||||
|
// that case.
|
||||||
|
p.block.HasBraces = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Users commonly forget to place a space between the address and the '{'
|
||||||
|
if strings.HasSuffix(tkn, "{") {
|
||||||
|
return p.Errf("Site addresses cannot end with a curly brace: '%s' - put a space between the token and the brace", tkn)
|
||||||
|
}
|
||||||
|
|
||||||
if tkn != "" { // empty token possible if user typed ""
|
if tkn != "" { // empty token possible if user typed ""
|
||||||
// Trailing comma indicates another address will follow, which
|
// Trailing comma indicates another address will follow, which
|
||||||
// may possibly be on the next line
|
// may possibly be on the next line
|
||||||
@@ -200,6 +242,13 @@ func (p *parser) addresses() error {
|
|||||||
expectingAnother = false // but we may still see another one on this line
|
expectingAnother = false // but we may still see another one on this line
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If there's a comma here, it's probably because they didn't use a space
|
||||||
|
// between their two domains, e.g. "foo.com,bar.com", which would not be
|
||||||
|
// parsed as two separate site addresses.
|
||||||
|
if strings.Contains(tkn, ",") {
|
||||||
|
return p.Errf("Site addresses cannot contain a comma ',': '%s' - put a space after the comma to separate site addresses", tkn)
|
||||||
|
}
|
||||||
|
|
||||||
p.block.Keys = append(p.block.Keys, tkn)
|
p.block.Keys = append(p.block.Keys, tkn)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -292,17 +341,30 @@ func (p *parser) doImport() error {
|
|||||||
if importPattern == "" {
|
if importPattern == "" {
|
||||||
return p.Err("Import requires a non-empty filepath")
|
return p.Err("Import requires a non-empty filepath")
|
||||||
}
|
}
|
||||||
if p.NextArg() {
|
|
||||||
return p.Err("Import takes only one argument (glob pattern or file)")
|
// grab remaining args as placeholder replacements
|
||||||
|
args := p.RemainingArgs()
|
||||||
|
|
||||||
|
// add args to the replacer
|
||||||
|
repl := caddy.NewEmptyReplacer()
|
||||||
|
for index, arg := range args {
|
||||||
|
repl.Set("args."+strconv.Itoa(index), arg)
|
||||||
}
|
}
|
||||||
// splice out the import directive and its argument (2 tokens total)
|
|
||||||
tokensBefore := p.tokens[:p.cursor-1]
|
// splice out the import directive and its arguments
|
||||||
|
// (2 tokens, plus the length of args)
|
||||||
|
tokensBefore := p.tokens[:p.cursor-1-len(args)]
|
||||||
tokensAfter := p.tokens[p.cursor+1:]
|
tokensAfter := p.tokens[p.cursor+1:]
|
||||||
var importedTokens []Token
|
var importedTokens []Token
|
||||||
|
var nodes []string
|
||||||
|
|
||||||
// first check snippets. That is a simple, non-recursive replacement
|
// first check snippets. That is a simple, non-recursive replacement
|
||||||
if p.definedSnippets != nil && p.definedSnippets[importPattern] != nil {
|
if p.definedSnippets != nil && p.definedSnippets[importPattern] != nil {
|
||||||
importedTokens = p.definedSnippets[importPattern]
|
importedTokens = p.definedSnippets[importPattern]
|
||||||
|
if len(importedTokens) > 0 {
|
||||||
|
// just grab the first one
|
||||||
|
nodes = append(nodes, fmt.Sprintf("%s:%s", importedTokens[0].File, importedTokens[0].snippetName))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// make path relative to the file of the _token_ being processed rather
|
// make path relative to the file of the _token_ being processed rather
|
||||||
// than current working directory (issue #867) and then use glob to get
|
// than current working directory (issue #867) and then use glob to get
|
||||||
@@ -331,14 +393,13 @@ func (p *parser) doImport() error {
|
|||||||
}
|
}
|
||||||
if len(matches) == 0 {
|
if len(matches) == 0 {
|
||||||
if strings.ContainsAny(globPattern, "*?[]") {
|
if strings.ContainsAny(globPattern, "*?[]") {
|
||||||
log.Printf("[WARNING] No files matching import glob pattern: %s", importPattern)
|
caddy.Log().Warn("No files matching import glob pattern", zap.String("pattern", importPattern))
|
||||||
} else {
|
} else {
|
||||||
return p.Errf("File to import not found: %s", importPattern)
|
return p.Errf("File to import not found: %s", importPattern)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// collect all the imported tokens
|
// collect all the imported tokens
|
||||||
|
|
||||||
for _, importFile := range matches {
|
for _, importFile := range matches {
|
||||||
newTokens, err := p.doSingleImport(importFile)
|
newTokens, err := p.doSingleImport(importFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -346,12 +407,34 @@ func (p *parser) doImport() error {
|
|||||||
}
|
}
|
||||||
importedTokens = append(importedTokens, newTokens...)
|
importedTokens = append(importedTokens, newTokens...)
|
||||||
}
|
}
|
||||||
|
nodes = matches
|
||||||
|
}
|
||||||
|
|
||||||
|
nodeName := p.File()
|
||||||
|
if p.Token().inSnippet {
|
||||||
|
nodeName += fmt.Sprintf(":%s", p.Token().snippetName)
|
||||||
|
}
|
||||||
|
p.importGraph.addNode(nodeName)
|
||||||
|
p.importGraph.addNodes(nodes)
|
||||||
|
if err := p.importGraph.addEdges(nodeName, nodes); err != nil {
|
||||||
|
p.importGraph.removeNodes(nodes)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// copy the tokens so we don't overwrite p.definedSnippets
|
||||||
|
tokensCopy := make([]Token, len(importedTokens))
|
||||||
|
copy(tokensCopy, importedTokens)
|
||||||
|
|
||||||
|
// run the argument replacer on the tokens
|
||||||
|
for index, token := range tokensCopy {
|
||||||
|
token.Text = repl.ReplaceKnown(token.Text, "")
|
||||||
|
tokensCopy[index] = token
|
||||||
}
|
}
|
||||||
|
|
||||||
// splice the imported tokens in the place of the import statement
|
// splice the imported tokens in the place of the import statement
|
||||||
// and rewind cursor so Next() will land on first imported token
|
// and rewind cursor so Next() will land on first imported token
|
||||||
p.tokens = append(tokensBefore, append(importedTokens, tokensAfter...)...)
|
p.tokens = append(tokensBefore, append(tokensCopy, tokensAfter...)...)
|
||||||
p.cursor--
|
p.cursor -= len(args) + 1
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -371,7 +454,7 @@ func (p *parser) doSingleImport(importFile string) ([]Token, error) {
|
|||||||
return nil, p.Errf("Could not import %s: is a directory", importFile)
|
return nil, p.Errf("Could not import %s: is a directory", importFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
input, err := ioutil.ReadAll(file)
|
input, err := io.ReadAll(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, p.Errf("Could not read imported file %s: %v", importFile, err)
|
return nil, p.Errf("Could not read imported file %s: %v", importFile, err)
|
||||||
}
|
}
|
||||||
@@ -411,6 +494,13 @@ func (p *parser) directive() error {
|
|||||||
for p.Next() {
|
for p.Next() {
|
||||||
if p.Val() == "{" {
|
if p.Val() == "{" {
|
||||||
p.nesting++
|
p.nesting++
|
||||||
|
if !p.isNextOnNewLine() && p.Token().wasQuoted == 0 {
|
||||||
|
return p.Err("Unexpected next token after '{' on same line")
|
||||||
|
}
|
||||||
|
} else if p.Val() == "{}" {
|
||||||
|
if p.isNextOnNewLine() && p.Token().wasQuoted == 0 {
|
||||||
|
return p.Err("Unexpected '{}' at end of line")
|
||||||
|
}
|
||||||
} else if p.isNewLine() && p.nesting == 0 {
|
} else if p.isNewLine() && p.nesting == 0 {
|
||||||
p.cursor-- // read too far
|
p.cursor-- // read too far
|
||||||
break
|
break
|
||||||
@@ -501,8 +591,9 @@ func (p *parser) snippetTokens() ([]Token, error) {
|
|||||||
// head of the server block with tokens, which are
|
// head of the server block with tokens, which are
|
||||||
// grouped by segments.
|
// grouped by segments.
|
||||||
type ServerBlock struct {
|
type ServerBlock struct {
|
||||||
Keys []string
|
HasBraces bool
|
||||||
Segments []Segment
|
Keys []string
|
||||||
|
Segments []Segment
|
||||||
}
|
}
|
||||||
|
|
||||||
// DispenseDirective returns a dispenser that contains
|
// DispenseDirective returns a dispenser that contains
|
||||||
@@ -533,4 +624,7 @@ func (s Segment) Directive() string {
|
|||||||
|
|
||||||
// spanOpen and spanClose are used to bound spans that
|
// spanOpen and spanClose are used to bound spans that
|
||||||
// contain the name of an environment variable.
|
// contain the name of an environment variable.
|
||||||
var spanOpen, spanClose = []byte{'{', '$'}, []byte{'}'}
|
var (
|
||||||
|
spanOpen, spanClose = []byte{'{', '$'}, []byte{'}'}
|
||||||
|
envVarDefaultDelimiter = ":"
|
||||||
|
)
|
||||||
|
|||||||
Executable → Regular
+69
-9
@@ -1,4 +1,4 @@
|
|||||||
// Copyright 2015 Light Code Labs, LLC
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// you may not use this file except in compliance with the License.
|
// you may not use this file except in compliance with the License.
|
||||||
@@ -16,7 +16,6 @@ package caddyfile
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -160,6 +159,10 @@ func TestParseOneAndImport(t *testing.T) {
|
|||||||
"localhost",
|
"localhost",
|
||||||
}, []int{}},
|
}, []int{}},
|
||||||
|
|
||||||
|
{`localhost{
|
||||||
|
dir1
|
||||||
|
}`, true, []string{}, []int{}},
|
||||||
|
|
||||||
{`localhost
|
{`localhost
|
||||||
dir1 {
|
dir1 {
|
||||||
nested {
|
nested {
|
||||||
@@ -182,14 +185,31 @@ func TestParseOneAndImport(t *testing.T) {
|
|||||||
"host1",
|
"host1",
|
||||||
}, []int{1, 2}},
|
}, []int{1, 2}},
|
||||||
|
|
||||||
{`import testdata/import_test1.txt testdata/import_test2.txt`, true, []string{}, []int{}},
|
|
||||||
|
|
||||||
{`import testdata/not_found.txt`, true, []string{}, []int{}},
|
{`import testdata/not_found.txt`, true, []string{}, []int{}},
|
||||||
|
|
||||||
{`""`, false, []string{}, []int{}},
|
{`""`, false, []string{}, []int{}},
|
||||||
|
|
||||||
{``, false, []string{}, []int{}},
|
{``, false, []string{}, []int{}},
|
||||||
|
|
||||||
|
// Unexpected next token after '{' on same line
|
||||||
|
{`localhost
|
||||||
|
dir1 { a b }`, true, []string{"localhost"}, []int{}},
|
||||||
|
// Workaround with quotes
|
||||||
|
{`localhost
|
||||||
|
dir1 "{" a b "}"`, false, []string{"localhost"}, []int{5}},
|
||||||
|
|
||||||
|
// Unexpected '{}' at end of line
|
||||||
|
{`localhost
|
||||||
|
dir1 {}`, true, []string{"localhost"}, []int{}},
|
||||||
|
// Workaround with quotes
|
||||||
|
{`localhost
|
||||||
|
dir1 "{}"`, false, []string{"localhost"}, []int{2}},
|
||||||
|
|
||||||
|
// import with args
|
||||||
|
{`import testdata/import_args0.txt a`, false, []string{"a"}, []int{}},
|
||||||
|
{`import testdata/import_args1.txt a b`, false, []string{"a", "b"}, []int{}},
|
||||||
|
{`import testdata/import_args*.txt a b`, false, []string{"a"}, []int{2}},
|
||||||
|
|
||||||
// test cases found by fuzzing!
|
// test cases found by fuzzing!
|
||||||
{`import }{$"`, true, []string{}, []int{}},
|
{`import }{$"`, true, []string{}, []int{}},
|
||||||
{`import /*/*.txt`, true, []string{}, []int{}},
|
{`import /*/*.txt`, true, []string{}, []int{}},
|
||||||
@@ -210,6 +230,7 @@ func TestParseOneAndImport(t *testing.T) {
|
|||||||
t.Errorf("Test %d: Expected no error, but got: %v", i, err)
|
t.Errorf("Test %d: Expected no error, but got: %v", i, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// t.Logf("%+v\n", result)
|
||||||
if len(result.Keys) != len(test.keys) {
|
if len(result.Keys) != len(test.keys) {
|
||||||
t.Errorf("Test %d: Expected %d keys, got %d",
|
t.Errorf("Test %d: Expected %d keys, got %d",
|
||||||
i, len(test.keys), len(result.Keys))
|
i, len(test.keys), len(result.Keys))
|
||||||
@@ -272,7 +293,7 @@ func TestRecursiveImport(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// test relative recursive import
|
// test relative recursive import
|
||||||
err = ioutil.WriteFile(recursiveFile1, []byte(
|
err = os.WriteFile(recursiveFile1, []byte(
|
||||||
`localhost
|
`localhost
|
||||||
dir1
|
dir1
|
||||||
import recursive_import_test2`), 0644)
|
import recursive_import_test2`), 0644)
|
||||||
@@ -281,7 +302,7 @@ func TestRecursiveImport(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer os.Remove(recursiveFile1)
|
defer os.Remove(recursiveFile1)
|
||||||
|
|
||||||
err = ioutil.WriteFile(recursiveFile2, []byte("dir2 1"), 0644)
|
err = os.WriteFile(recursiveFile2, []byte("dir2 1"), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -306,7 +327,7 @@ func TestRecursiveImport(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// test absolute recursive import
|
// test absolute recursive import
|
||||||
err = ioutil.WriteFile(recursiveFile1, []byte(
|
err = os.WriteFile(recursiveFile1, []byte(
|
||||||
`localhost
|
`localhost
|
||||||
dir1
|
dir1
|
||||||
import `+recursiveFile2), 0644)
|
import `+recursiveFile2), 0644)
|
||||||
@@ -362,7 +383,7 @@ func TestDirectiveImport(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ioutil.WriteFile(directiveFile, []byte(`prop1 1
|
err = os.WriteFile(directiveFile, []byte(`prop1 1
|
||||||
prop2 2`), 0644)
|
prop2 2`), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -440,6 +461,28 @@ func TestParseAll(t *testing.T) {
|
|||||||
|
|
||||||
{`import notfound/*`, false, [][]string{}}, // glob needn't error with no matches
|
{`import notfound/*`, false, [][]string{}}, // glob needn't error with no matches
|
||||||
{`import notfound/file.conf`, true, [][]string{}}, // but a specific file should
|
{`import notfound/file.conf`, true, [][]string{}}, // but a specific file should
|
||||||
|
|
||||||
|
// recursive self-import
|
||||||
|
{`import testdata/import_recursive0.txt`, true, [][]string{}},
|
||||||
|
{`import testdata/import_recursive3.txt
|
||||||
|
import testdata/import_recursive1.txt`, true, [][]string{}},
|
||||||
|
|
||||||
|
// cyclic imports
|
||||||
|
{`(A) {
|
||||||
|
import A
|
||||||
|
}
|
||||||
|
:80
|
||||||
|
import A
|
||||||
|
`, true, [][]string{}},
|
||||||
|
{`(A) {
|
||||||
|
import B
|
||||||
|
}
|
||||||
|
(B) {
|
||||||
|
import A
|
||||||
|
}
|
||||||
|
:80
|
||||||
|
import A
|
||||||
|
`, true, [][]string{}},
|
||||||
} {
|
} {
|
||||||
p := testParser(test.input)
|
p := testParser(test.input)
|
||||||
blocks, err := p.parseAll()
|
blocks, err := p.parseAll()
|
||||||
@@ -474,6 +517,7 @@ func TestParseAll(t *testing.T) {
|
|||||||
|
|
||||||
func TestEnvironmentReplacement(t *testing.T) {
|
func TestEnvironmentReplacement(t *testing.T) {
|
||||||
os.Setenv("FOOBAR", "foobar")
|
os.Setenv("FOOBAR", "foobar")
|
||||||
|
os.Setenv("CHAINED", "$FOOBAR")
|
||||||
|
|
||||||
for i, test := range []struct {
|
for i, test := range []struct {
|
||||||
input string
|
input string
|
||||||
@@ -519,6 +563,22 @@ func TestEnvironmentReplacement(t *testing.T) {
|
|||||||
input: "{$FOOBAR}{$FOOBAR}",
|
input: "{$FOOBAR}{$FOOBAR}",
|
||||||
expect: "foobarfoobar",
|
expect: "foobarfoobar",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
input: "{$CHAINED}",
|
||||||
|
expect: "$FOOBAR", // should not chain env expands
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "{$FOO:default}",
|
||||||
|
expect: "default",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "foo{$BAR:bar}baz",
|
||||||
|
expect: "foobarbaz",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "foo{$BAR:$FOOBAR}baz",
|
||||||
|
expect: "foo$FOOBARbaz", // should not chain env expands
|
||||||
|
},
|
||||||
{
|
{
|
||||||
input: "{$FOOBAR",
|
input: "{$FOOBAR",
|
||||||
expect: "{$FOOBAR",
|
expect: "{$FOOBAR",
|
||||||
@@ -586,7 +646,7 @@ func TestSnippets(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func writeStringToTempFileOrDie(t *testing.T, str string) (pathToFile string) {
|
func writeStringToTempFileOrDie(t *testing.T, str string) (pathToFile string) {
|
||||||
file, err := ioutil.TempFile("", t.Name())
|
file, err := os.CreateTemp("", t.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err) // get a stack trace so we know where this was called from.
|
panic(err) // get a stack trace so we know where this was called from.
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
{args.0}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
{args.0} {args.1}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
import import_recursive0.txt
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
import import_recursive2.txt
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
import import_recursive3.txt
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
import import_recursive1.txt
|
||||||
@@ -35,6 +35,14 @@ type Warning struct {
|
|||||||
Message string `json:"message,omitempty"`
|
Message string `json:"message,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w Warning) String() string {
|
||||||
|
var directive string
|
||||||
|
if w.Directive != "" {
|
||||||
|
directive = fmt.Sprintf(" (%s)", w.Directive)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s:%d%s: %s", w.File, w.Line, directive, w.Message)
|
||||||
|
}
|
||||||
|
|
||||||
// JSON encodes val as JSON, returning it as a json.RawMessage. Any
|
// JSON encodes val as JSON, returning it as a json.RawMessage. Any
|
||||||
// marshaling errors (which are highly unlikely with correct code)
|
// marshaling errors (which are highly unlikely with correct code)
|
||||||
// are converted to warnings. This is convenient when filling config
|
// are converted to warnings. This is convenient when filling config
|
||||||
@@ -51,14 +59,13 @@ func JSON(val interface{}, warnings *[]Warning) json.RawMessage {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// JSONModuleObject is like JSON, except it marshals val into a JSON object
|
// JSONModuleObject is like JSON(), except it marshals val into a JSON object
|
||||||
// and then adds a key to that object named fieldName with the value fieldVal.
|
// with an added key named fieldName with the value fieldVal. This is useful
|
||||||
// This is useful for JSON-encoding module values where the module name has to
|
// for encoding module values where the module name has to be described within
|
||||||
// be described within the object by a certain key; for example,
|
// the object by a certain key; for example, `"handler": "file_server"` for a
|
||||||
// "responder": "file_server" for a file server HTTP responder. The val must
|
// file server HTTP handler (fieldName="handler" and fieldVal="file_server").
|
||||||
// encode into a map[string]interface{} (i.e. it must be a struct or map),
|
// The val parameter must encode into a map[string]interface{} (i.e. it must be
|
||||||
// and any errors are converted into warnings, so this can be conveniently
|
// a struct or map). Any errors are converted into warnings.
|
||||||
// used when filling a struct. For correct code, there should be no errors.
|
|
||||||
func JSONModuleObject(val interface{}, fieldName, fieldVal string, warnings *[]Warning) json.RawMessage {
|
func JSONModuleObject(val interface{}, fieldName, fieldVal string, warnings *[]Warning) json.RawMessage {
|
||||||
// encode to a JSON object first
|
// encode to a JSON object first
|
||||||
enc, err := json.Marshal(val)
|
enc, err := json.Marshal(val)
|
||||||
@@ -94,12 +101,6 @@ func JSONModuleObject(val interface{}, fieldName, fieldVal string, warnings *[]W
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// JSONIndent is used to JSON-marshal the final resulting Caddy
|
|
||||||
// configuration in a consistent, human-readable way.
|
|
||||||
func JSONIndent(val interface{}) ([]byte, error) {
|
|
||||||
return json.MarshalIndent(val, "", "\t")
|
|
||||||
}
|
|
||||||
|
|
||||||
// RegisterAdapter registers a config adapter with the given name.
|
// RegisterAdapter registers a config adapter with the given name.
|
||||||
// This should usually be done at init-time. It panics if the
|
// This should usually be done at init-time. It panics if the
|
||||||
// adapter cannot be registered successfully.
|
// adapter cannot be registered successfully.
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
@@ -101,12 +102,20 @@ func (st *ServerType) mapAddressToServerBlocks(originalServerBlocks []serverBloc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// make a slice of the map keys so we can iterate in sorted order
|
||||||
|
addrs := make([]string, 0, len(addrToKeys))
|
||||||
|
for k := range addrToKeys {
|
||||||
|
addrs = append(addrs, k)
|
||||||
|
}
|
||||||
|
sort.Strings(addrs)
|
||||||
|
|
||||||
// now that we know which addresses serve which keys of this
|
// now that we know which addresses serve which keys of this
|
||||||
// server block, we iterate that mapping and create a list of
|
// server block, we iterate that mapping and create a list of
|
||||||
// new server blocks for each address where the keys of the
|
// new server blocks for each address where the keys of the
|
||||||
// server block are only the ones which use the address; but
|
// server block are only the ones which use the address; but
|
||||||
// the contents (tokens) are of course the same
|
// the contents (tokens) are of course the same
|
||||||
for addr, keys := range addrToKeys {
|
for _, addr := range addrs {
|
||||||
|
keys := addrToKeys[addr]
|
||||||
// parse keys so that we only have to do it once
|
// parse keys so that we only have to do it once
|
||||||
parsedKeys := make([]Address, 0, len(keys))
|
parsedKeys := make([]Address, 0, len(keys))
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
@@ -160,9 +169,17 @@ func (st *ServerType) consolidateAddrMappings(addrToServerBlocks map[string][]se
|
|||||||
delete(addrToServerBlocks, otherAddr)
|
delete(addrToServerBlocks, otherAddr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
sort.Strings(a.addresses)
|
||||||
|
|
||||||
sbaddrs = append(sbaddrs, a)
|
sbaddrs = append(sbaddrs, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sort them by their first address (we know there will always be at least one)
|
||||||
|
// to avoid problems with non-deterministic ordering (makes tests flaky)
|
||||||
|
sort.Slice(sbaddrs, func(i, j int) bool {
|
||||||
|
return sbaddrs[i].addresses[0] < sbaddrs[j].addresses[0]
|
||||||
|
})
|
||||||
|
|
||||||
return sbaddrs
|
return sbaddrs
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,12 +217,16 @@ func (st *ServerType) listenerAddrsForServerBlockKey(sblock serverBlock, key str
|
|||||||
}
|
}
|
||||||
|
|
||||||
// the bind directive specifies hosts, but is optional
|
// the bind directive specifies hosts, but is optional
|
||||||
lnHosts := make([]string, 0, len(sblock.pile))
|
lnHosts := make([]string, 0, len(sblock.pile["bind"]))
|
||||||
for _, cfgVal := range sblock.pile["bind"] {
|
for _, cfgVal := range sblock.pile["bind"] {
|
||||||
lnHosts = append(lnHosts, cfgVal.Value.([]string)...)
|
lnHosts = append(lnHosts, cfgVal.Value.([]string)...)
|
||||||
}
|
}
|
||||||
if len(lnHosts) == 0 {
|
if len(lnHosts) == 0 {
|
||||||
lnHosts = []string{""}
|
if defaultBind, ok := options["default_bind"].([]string); ok {
|
||||||
|
lnHosts = defaultBind
|
||||||
|
} else {
|
||||||
|
lnHosts = []string{""}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// use a map to prevent duplication
|
// use a map to prevent duplication
|
||||||
@@ -215,7 +236,7 @@ func (st *ServerType) listenerAddrsForServerBlockKey(sblock serverBlock, key str
|
|||||||
if err == nil && addr.IsUnixNetwork() {
|
if err == nil && addr.IsUnixNetwork() {
|
||||||
listeners[host] = struct{}{}
|
listeners[host] = struct{}{}
|
||||||
} else {
|
} else {
|
||||||
listeners[net.JoinHostPort(host, lnPort)] = struct{}{}
|
listeners[host+":"+lnPort] = struct{}{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,6 +245,7 @@ func (st *ServerType) listenerAddrsForServerBlockKey(sblock serverBlock, key str
|
|||||||
for lnStr := range listeners {
|
for lnStr := range listeners {
|
||||||
listenersList = append(listenersList, lnStr)
|
listenersList = append(listenersList, lnStr)
|
||||||
}
|
}
|
||||||
|
sort.Strings(listenersList)
|
||||||
|
|
||||||
return listenersList, nil
|
return listenersList, nil
|
||||||
}
|
}
|
||||||
@@ -329,7 +351,9 @@ func (a Address) Normalize() Address {
|
|||||||
// ensure host is normalized if it's an IP address
|
// ensure host is normalized if it's an IP address
|
||||||
host := strings.TrimSpace(a.Host)
|
host := strings.TrimSpace(a.Host)
|
||||||
if ip := net.ParseIP(host); ip != nil {
|
if ip := net.ParseIP(host); ip != nil {
|
||||||
host = ip.String()
|
if ipv6 := ip.To16(); ipv6 != nil && ipv6.DefaultMask() == nil {
|
||||||
|
host = ipv6.String()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Address{
|
return Address{
|
||||||
@@ -341,28 +365,6 @@ func (a Address) Normalize() Address {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Key returns a string form of a, much like String() does, but this
|
|
||||||
// method doesn't add anything default that wasn't in the original.
|
|
||||||
func (a Address) Key() string {
|
|
||||||
res := ""
|
|
||||||
if a.Scheme != "" {
|
|
||||||
res += a.Scheme + "://"
|
|
||||||
}
|
|
||||||
if a.Host != "" {
|
|
||||||
res += a.Host
|
|
||||||
}
|
|
||||||
// insert port only if the original has its own explicit port
|
|
||||||
if a.Port != "" &&
|
|
||||||
len(a.Original) >= len(res) &&
|
|
||||||
strings.HasPrefix(a.Original[len(res):], ":"+a.Port) {
|
|
||||||
res += ":" + a.Port
|
|
||||||
}
|
|
||||||
if a.Path != "" {
|
|
||||||
res += a.Path
|
|
||||||
}
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
// lowerExceptPlaceholders lowercases s except within
|
// lowerExceptPlaceholders lowercases s except within
|
||||||
// placeholders (substrings in non-escaped '{ }' spans).
|
// placeholders (substrings in non-escaped '{ }' spans).
|
||||||
// See https://github.com/caddyserver/caddy/issues/3264
|
// See https://github.com/caddyserver/caddy/issues/3264
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
//go:build gofuzz
|
||||||
// +build gofuzz
|
// +build gofuzz
|
||||||
|
|
||||||
package httpcaddyfile
|
package httpcaddyfile
|
||||||
|
|||||||
@@ -106,67 +106,128 @@ func TestAddressString(t *testing.T) {
|
|||||||
func TestKeyNormalization(t *testing.T) {
|
func TestKeyNormalization(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
input string
|
input string
|
||||||
expect string
|
expect Address
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
input: "example.com",
|
input: "example.com",
|
||||||
expect: "example.com",
|
expect: Address{
|
||||||
|
Host: "example.com",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "http://host:1234/path",
|
input: "http://host:1234/path",
|
||||||
expect: "http://host:1234/path",
|
expect: Address{
|
||||||
|
Scheme: "http",
|
||||||
|
Host: "host",
|
||||||
|
Port: "1234",
|
||||||
|
Path: "/path",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "HTTP://A/ABCDEF",
|
input: "HTTP://A/ABCDEF",
|
||||||
expect: "http://a/ABCDEF",
|
expect: Address{
|
||||||
|
Scheme: "http",
|
||||||
|
Host: "a",
|
||||||
|
Path: "/ABCDEF",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "A/ABCDEF",
|
input: "A/ABCDEF",
|
||||||
expect: "a/ABCDEF",
|
expect: Address{
|
||||||
|
Host: "a",
|
||||||
|
Path: "/ABCDEF",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "A:2015/Path",
|
input: "A:2015/Path",
|
||||||
expect: "a:2015/Path",
|
expect: Address{
|
||||||
|
Host: "a",
|
||||||
|
Port: "2015",
|
||||||
|
Path: "/Path",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "sub.{env.MY_DOMAIN}",
|
input: "sub.{env.MY_DOMAIN}",
|
||||||
expect: "sub.{env.MY_DOMAIN}",
|
expect: Address{
|
||||||
|
Host: "sub.{env.MY_DOMAIN}",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "sub.ExAmPle",
|
input: "sub.ExAmPle",
|
||||||
expect: "sub.example",
|
expect: Address{
|
||||||
|
Host: "sub.example",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "sub.\\{env.MY_DOMAIN\\}",
|
input: "sub.\\{env.MY_DOMAIN\\}",
|
||||||
expect: "sub.\\{env.my_domain\\}",
|
expect: Address{
|
||||||
|
Host: "sub.\\{env.my_domain\\}",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "sub.{env.MY_DOMAIN}.com",
|
input: "sub.{env.MY_DOMAIN}.com",
|
||||||
expect: "sub.{env.MY_DOMAIN}.com",
|
expect: Address{
|
||||||
|
Host: "sub.{env.MY_DOMAIN}.com",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: ":80",
|
input: ":80",
|
||||||
expect: ":80",
|
expect: Address{
|
||||||
|
Port: "80",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: ":443",
|
input: ":443",
|
||||||
expect: ":443",
|
expect: Address{
|
||||||
|
Port: "443",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: ":1234",
|
input: ":1234",
|
||||||
expect: ":1234",
|
expect: Address{
|
||||||
|
Port: "1234",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "",
|
input: "",
|
||||||
expect: "",
|
expect: Address{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: ":",
|
input: ":",
|
||||||
expect: "",
|
expect: Address{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "[::]",
|
input: "[::]",
|
||||||
expect: "::",
|
expect: Address{
|
||||||
|
Host: "::",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "127.0.0.1",
|
||||||
|
expect: Address{
|
||||||
|
Host: "127.0.0.1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:1234",
|
||||||
|
expect: Address{
|
||||||
|
Host: "2001:db8:85a3:8d3:1319:8a2e:370:7348",
|
||||||
|
Port: "1234",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// IPv4 address in IPv6 form (#4381)
|
||||||
|
input: "[::ffff:cff4:e77d]:1234",
|
||||||
|
expect: Address{
|
||||||
|
Host: "::ffff:cff4:e77d",
|
||||||
|
Port: "1234",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "::ffff:cff4:e77d",
|
||||||
|
expect: Address{
|
||||||
|
Host: "::ffff:cff4:e77d",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for i, tc := range testCases {
|
for i, tc := range testCases {
|
||||||
@@ -175,9 +236,18 @@ func TestKeyNormalization(t *testing.T) {
|
|||||||
t.Errorf("Test %d: Parsing address '%s': %v", i, tc.input, err)
|
t.Errorf("Test %d: Parsing address '%s': %v", i, tc.input, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if actual := addr.Normalize().Key(); actual != tc.expect {
|
actual := addr.Normalize()
|
||||||
t.Errorf("Test %d: Input '%s': Expected '%s' but got '%s'", i, tc.input, tc.expect, actual)
|
if actual.Scheme != tc.expect.Scheme {
|
||||||
|
t.Errorf("Test %d: Input '%s': Expected Scheme='%s' but got Scheme='%s'", i, tc.input, tc.expect.Scheme, actual.Scheme)
|
||||||
|
}
|
||||||
|
if actual.Host != tc.expect.Host {
|
||||||
|
t.Errorf("Test %d: Input '%s': Expected Host='%s' but got Host='%s'", i, tc.input, tc.expect.Host, actual.Host)
|
||||||
|
}
|
||||||
|
if actual.Port != tc.expect.Port {
|
||||||
|
t.Errorf("Test %d: Input '%s': Expected Port='%s' but got Port='%s'", i, tc.input, tc.expect.Port, actual.Port)
|
||||||
|
}
|
||||||
|
if actual.Path != tc.expect.Path {
|
||||||
|
t.Errorf("Test %d: Input '%s': Expected Path='%s' but got Path='%s'", i, tc.input, tc.expect.Path, actual.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,10 +15,14 @@
|
|||||||
package httpcaddyfile
|
package httpcaddyfile
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/pem"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html"
|
"html"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/caddyserver/caddy/v2"
|
"github.com/caddyserver/caddy/v2"
|
||||||
@@ -26,6 +30,8 @@ import (
|
|||||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||||
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
||||||
|
"github.com/caddyserver/certmagic"
|
||||||
|
"github.com/mholt/acmez/acme"
|
||||||
"go.uber.org/zap/zapcore"
|
"go.uber.org/zap/zapcore"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -33,8 +39,11 @@ func init() {
|
|||||||
RegisterDirective("bind", parseBind)
|
RegisterDirective("bind", parseBind)
|
||||||
RegisterDirective("tls", parseTLS)
|
RegisterDirective("tls", parseTLS)
|
||||||
RegisterHandlerDirective("root", parseRoot)
|
RegisterHandlerDirective("root", parseRoot)
|
||||||
|
RegisterHandlerDirective("vars", parseVars)
|
||||||
RegisterHandlerDirective("redir", parseRedir)
|
RegisterHandlerDirective("redir", parseRedir)
|
||||||
RegisterHandlerDirective("respond", parseRespond)
|
RegisterHandlerDirective("respond", parseRespond)
|
||||||
|
RegisterHandlerDirective("abort", parseAbort)
|
||||||
|
RegisterHandlerDirective("error", parseError)
|
||||||
RegisterHandlerDirective("route", parseRoute)
|
RegisterHandlerDirective("route", parseRoute)
|
||||||
RegisterHandlerDirective("handle", parseHandle)
|
RegisterHandlerDirective("handle", parseHandle)
|
||||||
RegisterDirective("handle_errors", parseHandleErrors)
|
RegisterDirective("handle_errors", parseHandleErrors)
|
||||||
@@ -59,12 +68,22 @@ func parseBind(h Helper) ([]ConfigValue, error) {
|
|||||||
// protocols <min> [<max>]
|
// protocols <min> [<max>]
|
||||||
// ciphers <cipher_suites...>
|
// ciphers <cipher_suites...>
|
||||||
// curves <curves...>
|
// curves <curves...>
|
||||||
|
// client_auth {
|
||||||
|
// mode [request|require|verify_if_given|require_and_verify]
|
||||||
|
// trusted_ca_cert <base64_der>
|
||||||
|
// trusted_ca_cert_file <filename>
|
||||||
|
// trusted_leaf_cert <base64_der>
|
||||||
|
// trusted_leaf_cert_file <filename>
|
||||||
|
// }
|
||||||
// alpn <values...>
|
// alpn <values...>
|
||||||
// load <paths...>
|
// load <paths...>
|
||||||
// ca <acme_ca_endpoint>
|
// ca <acme_ca_endpoint>
|
||||||
// ca_root <pem_file>
|
// ca_root <pem_file>
|
||||||
// dns <provider_name>
|
// dns <provider_name> [...]
|
||||||
// on_demand
|
// on_demand
|
||||||
|
// eab <key_id> <mac_key>
|
||||||
|
// issuer <module_name> [...]
|
||||||
|
// get_certificate <module_name> [...]
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
func parseTLS(h Helper) ([]ConfigValue, error) {
|
func parseTLS(h Helper) ([]ConfigValue, error) {
|
||||||
@@ -73,7 +92,10 @@ func parseTLS(h Helper) ([]ConfigValue, error) {
|
|||||||
var folderLoader caddytls.FolderLoader
|
var folderLoader caddytls.FolderLoader
|
||||||
var certSelector caddytls.CustomCertSelectionPolicy
|
var certSelector caddytls.CustomCertSelectionPolicy
|
||||||
var acmeIssuer *caddytls.ACMEIssuer
|
var acmeIssuer *caddytls.ACMEIssuer
|
||||||
|
var keyType string
|
||||||
var internalIssuer *caddytls.InternalIssuer
|
var internalIssuer *caddytls.InternalIssuer
|
||||||
|
var issuers []certmagic.Issuer
|
||||||
|
var certManagers []certmagic.Manager
|
||||||
var onDemand bool
|
var onDemand bool
|
||||||
|
|
||||||
for h.Next() {
|
for h.Next() {
|
||||||
@@ -107,10 +129,10 @@ func parseTLS(h Helper) ([]ConfigValue, error) {
|
|||||||
// must load each cert only once; otherwise, they each get a
|
// must load each cert only once; otherwise, they each get a
|
||||||
// different tag... since a cert loaded twice has the same
|
// different tag... since a cert loaded twice has the same
|
||||||
// bytes, it will overwrite the first one in the cache, and
|
// bytes, it will overwrite the first one in the cache, and
|
||||||
// only the last cert (and its tag) will survive, so a any conn
|
// only the last cert (and its tag) will survive, so any conn
|
||||||
// policy that is looking for any tag but the last one to be
|
// policy that is looking for any tag other than the last one
|
||||||
// loaded won't find it, and TLS handshakes will fail (see end)
|
// to be loaded won't find it, and TLS handshakes will fail
|
||||||
// of issue #3004)
|
// (see end of issue #3004)
|
||||||
//
|
//
|
||||||
// tlsCertTags maps certificate filenames to their tag.
|
// tlsCertTags maps certificate filenames to their tag.
|
||||||
// This is used to remember which tag is used for each
|
// This is used to remember which tag is used for each
|
||||||
@@ -143,7 +165,7 @@ func parseTLS(h Helper) ([]ConfigValue, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var hasBlock bool
|
var hasBlock bool
|
||||||
for h.NextBlock(0) {
|
for nesting := h.Nesting(); h.NextBlock(nesting); {
|
||||||
hasBlock = true
|
hasBlock = true
|
||||||
|
|
||||||
switch h.Val() {
|
switch h.Val() {
|
||||||
@@ -181,6 +203,57 @@ func parseTLS(h Helper) ([]ConfigValue, error) {
|
|||||||
cp.Curves = append(cp.Curves, h.Val())
|
cp.Curves = append(cp.Curves, h.Val())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case "client_auth":
|
||||||
|
cp.ClientAuthentication = &caddytls.ClientAuthentication{}
|
||||||
|
for nesting := h.Nesting(); h.NextBlock(nesting); {
|
||||||
|
subdir := h.Val()
|
||||||
|
switch subdir {
|
||||||
|
case "mode":
|
||||||
|
if !h.Args(&cp.ClientAuthentication.Mode) {
|
||||||
|
return nil, h.ArgErr()
|
||||||
|
}
|
||||||
|
if h.NextArg() {
|
||||||
|
return nil, h.ArgErr()
|
||||||
|
}
|
||||||
|
|
||||||
|
case "trusted_ca_cert",
|
||||||
|
"trusted_leaf_cert":
|
||||||
|
if !h.NextArg() {
|
||||||
|
return nil, h.ArgErr()
|
||||||
|
}
|
||||||
|
if subdir == "trusted_ca_cert" {
|
||||||
|
cp.ClientAuthentication.TrustedCACerts = append(cp.ClientAuthentication.TrustedCACerts, h.Val())
|
||||||
|
} else {
|
||||||
|
cp.ClientAuthentication.TrustedLeafCerts = append(cp.ClientAuthentication.TrustedLeafCerts, h.Val())
|
||||||
|
}
|
||||||
|
|
||||||
|
case "trusted_ca_cert_file",
|
||||||
|
"trusted_leaf_cert_file":
|
||||||
|
if !h.NextArg() {
|
||||||
|
return nil, h.ArgErr()
|
||||||
|
}
|
||||||
|
filename := h.Val()
|
||||||
|
certDataPEM, err := os.ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
block, _ := pem.Decode(certDataPEM)
|
||||||
|
if block == nil || block.Type != "CERTIFICATE" {
|
||||||
|
return nil, h.Errf("no CERTIFICATE pem block found in %s", h.Val())
|
||||||
|
}
|
||||||
|
if subdir == "trusted_ca_cert_file" {
|
||||||
|
cp.ClientAuthentication.TrustedCACerts = append(cp.ClientAuthentication.TrustedCACerts,
|
||||||
|
base64.StdEncoding.EncodeToString(block.Bytes))
|
||||||
|
} else {
|
||||||
|
cp.ClientAuthentication.TrustedLeafCerts = append(cp.ClientAuthentication.TrustedLeafCerts,
|
||||||
|
base64.StdEncoding.EncodeToString(block.Bytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
return nil, h.Errf("unknown subdirective for client_auth: %s", subdir)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
case "alpn":
|
case "alpn":
|
||||||
args := h.RemainingArgs()
|
args := h.RemainingArgs()
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
@@ -201,6 +274,58 @@ func parseTLS(h Helper) ([]ConfigValue, error) {
|
|||||||
}
|
}
|
||||||
acmeIssuer.CA = arg[0]
|
acmeIssuer.CA = arg[0]
|
||||||
|
|
||||||
|
case "key_type":
|
||||||
|
arg := h.RemainingArgs()
|
||||||
|
if len(arg) != 1 {
|
||||||
|
return nil, h.ArgErr()
|
||||||
|
}
|
||||||
|
keyType = arg[0]
|
||||||
|
|
||||||
|
case "eab":
|
||||||
|
arg := h.RemainingArgs()
|
||||||
|
if len(arg) != 2 {
|
||||||
|
return nil, h.ArgErr()
|
||||||
|
}
|
||||||
|
if acmeIssuer == nil {
|
||||||
|
acmeIssuer = new(caddytls.ACMEIssuer)
|
||||||
|
}
|
||||||
|
acmeIssuer.ExternalAccount = &acme.EAB{
|
||||||
|
KeyID: arg[0],
|
||||||
|
MACKey: arg[1],
|
||||||
|
}
|
||||||
|
|
||||||
|
case "issuer":
|
||||||
|
if !h.NextArg() {
|
||||||
|
return nil, h.ArgErr()
|
||||||
|
}
|
||||||
|
modName := h.Val()
|
||||||
|
modID := "tls.issuance." + modName
|
||||||
|
unm, err := caddyfile.UnmarshalModule(h.Dispenser, modID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
issuer, ok := unm.(certmagic.Issuer)
|
||||||
|
if !ok {
|
||||||
|
return nil, h.Errf("module %s (%T) is not a certmagic.Issuer", modID, unm)
|
||||||
|
}
|
||||||
|
issuers = append(issuers, issuer)
|
||||||
|
|
||||||
|
case "get_certificate":
|
||||||
|
if !h.NextArg() {
|
||||||
|
return nil, h.ArgErr()
|
||||||
|
}
|
||||||
|
modName := h.Val()
|
||||||
|
modID := "tls.get_certificate." + modName
|
||||||
|
unm, err := caddyfile.UnmarshalModule(h.Dispenser, modID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
certManager, ok := unm.(certmagic.Manager)
|
||||||
|
if !ok {
|
||||||
|
return nil, h.Errf("module %s (%T) is not a certmagic.CertificateManager", modID, unm)
|
||||||
|
}
|
||||||
|
certManagers = append(certManagers, certManager)
|
||||||
|
|
||||||
case "dns":
|
case "dns":
|
||||||
if !h.NextArg() {
|
if !h.NextArg() {
|
||||||
return nil, h.ArgErr()
|
return nil, h.ArgErr()
|
||||||
@@ -211,20 +336,48 @@ func parseTLS(h Helper) ([]ConfigValue, error) {
|
|||||||
}
|
}
|
||||||
if acmeIssuer.Challenges == nil {
|
if acmeIssuer.Challenges == nil {
|
||||||
acmeIssuer.Challenges = new(caddytls.ChallengesConfig)
|
acmeIssuer.Challenges = new(caddytls.ChallengesConfig)
|
||||||
|
}
|
||||||
|
if acmeIssuer.Challenges.DNS == nil {
|
||||||
acmeIssuer.Challenges.DNS = new(caddytls.DNSChallengeConfig)
|
acmeIssuer.Challenges.DNS = new(caddytls.DNSChallengeConfig)
|
||||||
}
|
}
|
||||||
dnsProvModule, err := caddy.GetModule("dns.providers." + provName)
|
modID := "dns.providers." + provName
|
||||||
|
unm, err := caddyfile.UnmarshalModule(h.Dispenser, modID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, h.Errf("getting DNS provider module named '%s': %v", provName, err)
|
return nil, err
|
||||||
}
|
}
|
||||||
dnsProvModuleInstance := dnsProvModule.New()
|
acmeIssuer.Challenges.DNS.ProviderRaw = caddyconfig.JSONModuleObject(unm, "name", provName, h.warnings)
|
||||||
if unm, ok := dnsProvModuleInstance.(caddyfile.Unmarshaler); ok {
|
|
||||||
err = unm.UnmarshalCaddyfile(h.NewFromNextSegment())
|
case "resolvers":
|
||||||
if err != nil {
|
args := h.RemainingArgs()
|
||||||
return nil, err
|
if len(args) == 0 {
|
||||||
}
|
return nil, h.ArgErr()
|
||||||
}
|
}
|
||||||
acmeIssuer.Challenges.DNS.ProviderRaw = caddyconfig.JSONModuleObject(dnsProvModuleInstance, "name", provName, h.warnings)
|
if acmeIssuer == nil {
|
||||||
|
acmeIssuer = new(caddytls.ACMEIssuer)
|
||||||
|
}
|
||||||
|
if acmeIssuer.Challenges == nil {
|
||||||
|
acmeIssuer.Challenges = new(caddytls.ChallengesConfig)
|
||||||
|
}
|
||||||
|
if acmeIssuer.Challenges.DNS == nil {
|
||||||
|
acmeIssuer.Challenges.DNS = new(caddytls.DNSChallengeConfig)
|
||||||
|
}
|
||||||
|
acmeIssuer.Challenges.DNS.Resolvers = args
|
||||||
|
|
||||||
|
case "dns_challenge_override_domain":
|
||||||
|
arg := h.RemainingArgs()
|
||||||
|
if len(arg) != 1 {
|
||||||
|
return nil, h.ArgErr()
|
||||||
|
}
|
||||||
|
if acmeIssuer == nil {
|
||||||
|
acmeIssuer = new(caddytls.ACMEIssuer)
|
||||||
|
}
|
||||||
|
if acmeIssuer.Challenges == nil {
|
||||||
|
acmeIssuer.Challenges = new(caddytls.ChallengesConfig)
|
||||||
|
}
|
||||||
|
if acmeIssuer.Challenges.DNS == nil {
|
||||||
|
acmeIssuer.Challenges.DNS = new(caddytls.DNSChallengeConfig)
|
||||||
|
}
|
||||||
|
acmeIssuer.Challenges.DNS.OverrideDomain = arg[0]
|
||||||
|
|
||||||
case "ca_root":
|
case "ca_root":
|
||||||
arg := h.RemainingArgs()
|
arg := h.RemainingArgs()
|
||||||
@@ -254,50 +407,80 @@ func parseTLS(h Helper) ([]ConfigValue, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// begin building the final config values
|
// begin building the final config values
|
||||||
var configVals []ConfigValue
|
configVals := []ConfigValue{}
|
||||||
|
|
||||||
// certificate loaders
|
// certificate loaders
|
||||||
if len(fileLoader) > 0 {
|
if len(fileLoader) > 0 {
|
||||||
configVals = append(configVals, ConfigValue{
|
configVals = append(configVals, ConfigValue{
|
||||||
Class: "tls.certificate_loader",
|
Class: "tls.cert_loader",
|
||||||
Value: fileLoader,
|
Value: fileLoader,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if len(folderLoader) > 0 {
|
if len(folderLoader) > 0 {
|
||||||
configVals = append(configVals, ConfigValue{
|
configVals = append(configVals, ConfigValue{
|
||||||
Class: "tls.certificate_loader",
|
Class: "tls.cert_loader",
|
||||||
Value: folderLoader,
|
Value: folderLoader,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// issuer
|
// some tls subdirectives are shortcuts that implicitly configure issuers, and the
|
||||||
if acmeIssuer != nil && internalIssuer != nil {
|
// user can also configure issuers explicitly using the issuer subdirective; the
|
||||||
// the logic to support this would be complex
|
// logic to support both would likely be complex, or at least unintuitive
|
||||||
return nil, h.Err("cannot use both ACME and internal issuers in same server block")
|
if len(issuers) > 0 && (acmeIssuer != nil || internalIssuer != nil) {
|
||||||
|
return nil, h.Err("cannot mix issuer subdirective (explicit issuers) with other issuer-specific subdirectives (implicit issuers)")
|
||||||
}
|
}
|
||||||
if acmeIssuer != nil {
|
if acmeIssuer != nil && internalIssuer != nil {
|
||||||
// fill in global defaults, if configured
|
return nil, h.Err("cannot create both ACME and internal certificate issuers")
|
||||||
if email := h.Option("email"); email != nil && acmeIssuer.Email == "" {
|
}
|
||||||
acmeIssuer.Email = email.(string)
|
|
||||||
}
|
// now we should either have: explicitly-created issuers, or an implicitly-created
|
||||||
if acmeCA := h.Option("acme_ca"); acmeCA != nil && acmeIssuer.CA == "" {
|
// ACME or internal issuer, or no issuers at all
|
||||||
acmeIssuer.CA = acmeCA.(string)
|
switch {
|
||||||
}
|
case len(issuers) > 0:
|
||||||
if caPemFile := h.Option("acme_ca_root"); caPemFile != nil {
|
for _, issuer := range issuers {
|
||||||
acmeIssuer.TrustedRootsPEMFiles = append(acmeIssuer.TrustedRootsPEMFiles, caPemFile.(string))
|
configVals = append(configVals, ConfigValue{
|
||||||
|
Class: "tls.cert_issuer",
|
||||||
|
Value: issuer,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
configVals = append(configVals, ConfigValue{
|
case acmeIssuer != nil:
|
||||||
Class: "tls.cert_issuer",
|
// implicit ACME issuers (from various subdirectives) - use defaults; there might be more than one
|
||||||
Value: acmeIssuer,
|
defaultIssuers := caddytls.DefaultIssuers()
|
||||||
})
|
|
||||||
} else if internalIssuer != nil {
|
// if a CA endpoint was set, override multiple implicit issuers since it's a specific one
|
||||||
|
if acmeIssuer.CA != "" {
|
||||||
|
defaultIssuers = []certmagic.Issuer{acmeIssuer}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, issuer := range defaultIssuers {
|
||||||
|
switch iss := issuer.(type) {
|
||||||
|
case *caddytls.ACMEIssuer:
|
||||||
|
issuer = acmeIssuer
|
||||||
|
case *caddytls.ZeroSSLIssuer:
|
||||||
|
iss.ACMEIssuer = acmeIssuer
|
||||||
|
}
|
||||||
|
configVals = append(configVals, ConfigValue{
|
||||||
|
Class: "tls.cert_issuer",
|
||||||
|
Value: issuer,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
case internalIssuer != nil:
|
||||||
configVals = append(configVals, ConfigValue{
|
configVals = append(configVals, ConfigValue{
|
||||||
Class: "tls.cert_issuer",
|
Class: "tls.cert_issuer",
|
||||||
Value: internalIssuer,
|
Value: internalIssuer,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// certificate key type
|
||||||
|
if keyType != "" {
|
||||||
|
configVals = append(configVals, ConfigValue{
|
||||||
|
Class: "tls.key_type",
|
||||||
|
Value: keyType,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// on-demand TLS
|
// on-demand TLS
|
||||||
if onDemand {
|
if onDemand {
|
||||||
configVals = append(configVals, ConfigValue{
|
configVals = append(configVals, ConfigValue{
|
||||||
@@ -305,6 +488,12 @@ func parseTLS(h Helper) ([]ConfigValue, error) {
|
|||||||
Value: true,
|
Value: true,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
for _, certManager := range certManagers {
|
||||||
|
configVals = append(configVals, ConfigValue{
|
||||||
|
Class: "tls.cert_manager",
|
||||||
|
Value: certManager,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// custom certificate selection
|
// custom certificate selection
|
||||||
if len(certSelector.AnyTag) > 0 {
|
if len(certSelector.AnyTag) > 0 {
|
||||||
@@ -342,6 +531,13 @@ func parseRoot(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
|||||||
return caddyhttp.VarsMiddleware{"root": root}, nil
|
return caddyhttp.VarsMiddleware{"root": root}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseVars parses the vars directive. See its UnmarshalCaddyfile method for syntax.
|
||||||
|
func parseVars(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||||
|
v := new(caddyhttp.VarsMiddleware)
|
||||||
|
err := v.UnmarshalCaddyfile(h.Dispenser)
|
||||||
|
return v, err
|
||||||
|
}
|
||||||
|
|
||||||
// parseRedir parses the redir directive. Syntax:
|
// parseRedir parses the redir directive. Syntax:
|
||||||
//
|
//
|
||||||
// redir [<matcher>] <to> [<code>]
|
// redir [<matcher>] <to> [<code>]
|
||||||
@@ -360,14 +556,14 @@ func parseRedir(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
|||||||
if h.NextArg() {
|
if h.NextArg() {
|
||||||
code = h.Val()
|
code = h.Val()
|
||||||
}
|
}
|
||||||
if code == "permanent" {
|
|
||||||
code = "301"
|
|
||||||
}
|
|
||||||
if code == "temporary" || code == "" {
|
|
||||||
code = "302"
|
|
||||||
}
|
|
||||||
var body string
|
var body string
|
||||||
if code == "html" {
|
switch code {
|
||||||
|
case "permanent":
|
||||||
|
code = "301"
|
||||||
|
case "temporary", "":
|
||||||
|
code = "302"
|
||||||
|
case "html":
|
||||||
// Script tag comes first since that will better imitate a redirect in the browser's
|
// Script tag comes first since that will better imitate a redirect in the browser's
|
||||||
// history, but the meta tag is a fallback for most non-JS clients.
|
// history, but the meta tag is a fallback for most non-JS clients.
|
||||||
const metaRedir = `<!DOCTYPE html>
|
const metaRedir = `<!DOCTYPE html>
|
||||||
@@ -382,6 +578,27 @@ func parseRedir(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
|||||||
`
|
`
|
||||||
safeTo := html.EscapeString(to)
|
safeTo := html.EscapeString(to)
|
||||||
body = fmt.Sprintf(metaRedir, safeTo, safeTo, safeTo, safeTo)
|
body = fmt.Sprintf(metaRedir, safeTo, safeTo, safeTo, safeTo)
|
||||||
|
code = "302"
|
||||||
|
default:
|
||||||
|
// Allow placeholders for the code
|
||||||
|
if strings.HasPrefix(code, "{") {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// Try to validate as an integer otherwise
|
||||||
|
codeInt, err := strconv.Atoi(code)
|
||||||
|
if err != nil {
|
||||||
|
return nil, h.Errf("Not a supported redir code type or not valid integer: '%s'", code)
|
||||||
|
}
|
||||||
|
// Sometimes, a 401 with Location header is desirable because
|
||||||
|
// requests made with XHR will "eat" the 3xx redirect; so if
|
||||||
|
// the intent was to redirect to an auth page, a 3xx won't
|
||||||
|
// work. Responding with 401 allows JS code to read the
|
||||||
|
// Location header and do a window.location redirect manually.
|
||||||
|
// see https://stackoverflow.com/a/2573589/846934
|
||||||
|
// see https://github.com/oauth2-proxy/oauth2-proxy/issues/1522
|
||||||
|
if codeInt < 300 || (codeInt > 399 && codeInt != 401) {
|
||||||
|
return nil, h.Errf("Redir code not in the 3xx range or 401: '%v'", codeInt)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return caddyhttp.StaticResponse{
|
return caddyhttp.StaticResponse{
|
||||||
@@ -401,40 +618,46 @@ func parseRespond(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
|||||||
return sr, nil
|
return sr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseAbort parses the abort directive.
|
||||||
|
func parseAbort(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||||
|
h.Next() // consume directive
|
||||||
|
for h.Next() || h.NextBlock(0) {
|
||||||
|
return nil, h.ArgErr()
|
||||||
|
}
|
||||||
|
return &caddyhttp.StaticResponse{Abort: true}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseError parses the error directive.
|
||||||
|
func parseError(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||||
|
se := new(caddyhttp.StaticError)
|
||||||
|
err := se.UnmarshalCaddyfile(h.Dispenser)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return se, nil
|
||||||
|
}
|
||||||
|
|
||||||
// parseRoute parses the route directive.
|
// parseRoute parses the route directive.
|
||||||
func parseRoute(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
func parseRoute(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||||
sr := new(caddyhttp.Subroute)
|
sr := new(caddyhttp.Subroute)
|
||||||
|
|
||||||
for h.Next() {
|
allResults, err := parseSegmentAsConfig(h)
|
||||||
for nesting := h.Nesting(); h.NextBlock(nesting); {
|
if err != nil {
|
||||||
dir := h.Val()
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
dirFunc, ok := registeredDirectives[dir]
|
for _, result := range allResults {
|
||||||
if !ok {
|
switch handler := result.Value.(type) {
|
||||||
return nil, h.Errf("unrecognized directive: %s", dir)
|
case caddyhttp.Route:
|
||||||
}
|
sr.Routes = append(sr.Routes, handler)
|
||||||
|
case caddyhttp.Subroute:
|
||||||
subHelper := h
|
// directives which return a literal subroute instead of a route
|
||||||
subHelper.Dispenser = h.NewFromNextSegment()
|
// means they intend to keep those handlers together without
|
||||||
|
// them being reordered; we're doing that anyway since we're in
|
||||||
results, err := dirFunc(subHelper)
|
// the route directive, so just append its handlers
|
||||||
if err != nil {
|
sr.Routes = append(sr.Routes, handler.Routes...)
|
||||||
return nil, h.Errf("parsing caddyfile tokens for '%s': %v", dir, err)
|
default:
|
||||||
}
|
return nil, h.Errf("%s directive returned something other than an HTTP route or subroute: %#v (only handler directives can be used in routes)", result.directive, result.Value)
|
||||||
for _, result := range results {
|
|
||||||
switch handler := result.Value.(type) {
|
|
||||||
case caddyhttp.Route:
|
|
||||||
sr.Routes = append(sr.Routes, handler)
|
|
||||||
case caddyhttp.Subroute:
|
|
||||||
// directives which return a literal subroute instead of a route
|
|
||||||
// means they intend to keep those handlers together without
|
|
||||||
// them being reordered; we're doing that anyway since we're in
|
|
||||||
// the route directive, so just append its handlers
|
|
||||||
sr.Routes = append(sr.Routes, handler.Routes...)
|
|
||||||
default:
|
|
||||||
return nil, h.Errf("%s directive returned something other than an HTTP route or subroute: %#v (only handler directives can be used in routes)", dir, result.Value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -442,11 +665,11 @@ func parseRoute(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseHandle(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
func parseHandle(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||||
return parseSegmentAsSubroute(h)
|
return ParseSegmentAsSubroute(h)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseHandleErrors(h Helper) ([]ConfigValue, error) {
|
func parseHandleErrors(h Helper) ([]ConfigValue, error) {
|
||||||
subroute, err := parseSegmentAsSubroute(h)
|
subroute, err := ParseSegmentAsSubroute(h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -467,8 +690,52 @@ func parseHandleErrors(h Helper) ([]ConfigValue, error) {
|
|||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
func parseLog(h Helper) ([]ConfigValue, error) {
|
func parseLog(h Helper) ([]ConfigValue, error) {
|
||||||
|
return parseLogHelper(h, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseLogHelper is used both for the parseLog directive within Server Blocks,
|
||||||
|
// as well as the global "log" option for configuring loggers at the global
|
||||||
|
// level. The parseAsGlobalOption parameter is used to distinguish any differing logic
|
||||||
|
// between the two.
|
||||||
|
func parseLogHelper(h Helper, globalLogNames map[string]struct{}) ([]ConfigValue, error) {
|
||||||
|
// When the globalLogNames parameter is passed in, we make
|
||||||
|
// modifications to the parsing behavior.
|
||||||
|
parseAsGlobalOption := globalLogNames != nil
|
||||||
|
|
||||||
var configValues []ConfigValue
|
var configValues []ConfigValue
|
||||||
for h.Next() {
|
for h.Next() {
|
||||||
|
// Logic below expects that a name is always present when a
|
||||||
|
// global option is being parsed.
|
||||||
|
var globalLogName string
|
||||||
|
if parseAsGlobalOption {
|
||||||
|
if h.NextArg() {
|
||||||
|
globalLogName = h.Val()
|
||||||
|
|
||||||
|
// Only a single argument is supported.
|
||||||
|
if h.NextArg() {
|
||||||
|
return nil, h.ArgErr()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// If there is no log name specified, we
|
||||||
|
// reference the default logger. See the
|
||||||
|
// setupNewDefault function in the logging
|
||||||
|
// package for where this is configured.
|
||||||
|
globalLogName = "default"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify this name is unused.
|
||||||
|
_, used := globalLogNames[globalLogName]
|
||||||
|
if used {
|
||||||
|
return nil, h.Err("duplicate global log option for: " + globalLogName)
|
||||||
|
}
|
||||||
|
globalLogNames[globalLogName] = struct{}{}
|
||||||
|
} else {
|
||||||
|
// No arguments are supported for the server block log directive
|
||||||
|
if h.NextArg() {
|
||||||
|
return nil, h.ArgErr()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cl := new(caddy.CustomLog)
|
cl := new(caddy.CustomLog)
|
||||||
|
|
||||||
for h.NextBlock(0) {
|
for h.NextBlock(0) {
|
||||||
@@ -492,21 +759,15 @@ func parseLog(h Helper) ([]ConfigValue, error) {
|
|||||||
case "discard":
|
case "discard":
|
||||||
wo = caddy.DiscardWriter{}
|
wo = caddy.DiscardWriter{}
|
||||||
default:
|
default:
|
||||||
mod, err := caddy.GetModule("caddy.logging.writers." + moduleName)
|
modID := "caddy.logging.writers." + moduleName
|
||||||
if err != nil {
|
unm, err := caddyfile.UnmarshalModule(h.Dispenser, modID)
|
||||||
return nil, h.Errf("getting log writer module named '%s': %v", moduleName, err)
|
|
||||||
}
|
|
||||||
unm, ok := mod.New().(caddyfile.Unmarshaler)
|
|
||||||
if !ok {
|
|
||||||
return nil, h.Errf("log writer module '%s' is not a Caddyfile unmarshaler", mod)
|
|
||||||
}
|
|
||||||
err = unm.UnmarshalCaddyfile(h.NewFromNextSegment())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
var ok bool
|
||||||
wo, ok = unm.(caddy.WriterOpener)
|
wo, ok = unm.(caddy.WriterOpener)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, h.Errf("module %s is not a WriterOpener", mod)
|
return nil, h.Errf("module %s (%T) is not a WriterOpener", modID, unm)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cl.WriterRaw = caddyconfig.JSONModuleObject(wo, "output", moduleName, h.warnings)
|
cl.WriterRaw = caddyconfig.JSONModuleObject(wo, "output", moduleName, h.warnings)
|
||||||
@@ -516,21 +777,14 @@ func parseLog(h Helper) ([]ConfigValue, error) {
|
|||||||
return nil, h.ArgErr()
|
return nil, h.ArgErr()
|
||||||
}
|
}
|
||||||
moduleName := h.Val()
|
moduleName := h.Val()
|
||||||
mod, err := caddy.GetModule("caddy.logging.encoders." + moduleName)
|
moduleID := "caddy.logging.encoders." + moduleName
|
||||||
if err != nil {
|
unm, err := caddyfile.UnmarshalModule(h.Dispenser, moduleID)
|
||||||
return nil, h.Errf("getting log encoder module named '%s': %v", moduleName, err)
|
|
||||||
}
|
|
||||||
unm, ok := mod.New().(caddyfile.Unmarshaler)
|
|
||||||
if !ok {
|
|
||||||
return nil, h.Errf("log encoder module '%s' is not a Caddyfile unmarshaler", mod)
|
|
||||||
}
|
|
||||||
err = unm.UnmarshalCaddyfile(h.NewFromNextSegment())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
enc, ok := unm.(zapcore.Encoder)
|
enc, ok := unm.(zapcore.Encoder)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, h.Errf("module %s is not a zapcore.Encoder", mod)
|
return nil, h.Errf("module %s (%T) is not a zapcore.Encoder", moduleID, unm)
|
||||||
}
|
}
|
||||||
cl.EncoderRaw = caddyconfig.JSONModuleObject(enc, "format", moduleName, h.warnings)
|
cl.EncoderRaw = caddyconfig.JSONModuleObject(enc, "format", moduleName, h.warnings)
|
||||||
|
|
||||||
@@ -543,22 +797,48 @@ func parseLog(h Helper) ([]ConfigValue, error) {
|
|||||||
return nil, h.ArgErr()
|
return nil, h.ArgErr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case "include":
|
||||||
|
// This configuration is only allowed in the global options
|
||||||
|
if !parseAsGlobalOption {
|
||||||
|
return nil, h.ArgErr()
|
||||||
|
}
|
||||||
|
for h.NextArg() {
|
||||||
|
cl.Include = append(cl.Include, h.Val())
|
||||||
|
}
|
||||||
|
|
||||||
|
case "exclude":
|
||||||
|
// This configuration is only allowed in the global options
|
||||||
|
if !parseAsGlobalOption {
|
||||||
|
return nil, h.ArgErr()
|
||||||
|
}
|
||||||
|
for h.NextArg() {
|
||||||
|
cl.Exclude = append(cl.Exclude, h.Val())
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return nil, h.Errf("unrecognized subdirective: %s", h.Val())
|
return nil, h.Errf("unrecognized subdirective: %s", h.Val())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var val namedCustomLog
|
var val namedCustomLog
|
||||||
|
// Skip handling of empty logging configs
|
||||||
if !reflect.DeepEqual(cl, new(caddy.CustomLog)) {
|
if !reflect.DeepEqual(cl, new(caddy.CustomLog)) {
|
||||||
logCounter, ok := h.State["logCounter"].(int)
|
if parseAsGlobalOption {
|
||||||
if !ok {
|
// Use indicated name for global log options
|
||||||
logCounter = 0
|
val.name = globalLogName
|
||||||
|
val.log = cl
|
||||||
|
} else {
|
||||||
|
// Construct a log name for server log streams
|
||||||
|
logCounter, ok := h.State["logCounter"].(int)
|
||||||
|
if !ok {
|
||||||
|
logCounter = 0
|
||||||
|
}
|
||||||
|
val.name = fmt.Sprintf("log%d", logCounter)
|
||||||
|
cl.Include = []string{"http.log.access." + val.name}
|
||||||
|
val.log = cl
|
||||||
|
logCounter++
|
||||||
|
h.State["logCounter"] = logCounter
|
||||||
}
|
}
|
||||||
val.name = fmt.Sprintf("log%d", logCounter)
|
|
||||||
cl.Include = []string{"http.log.access." + val.name}
|
|
||||||
val.log = cl
|
|
||||||
logCounter++
|
|
||||||
h.State["logCounter"] = logCounter
|
|
||||||
}
|
}
|
||||||
configValues = append(configValues, ConfigValue{
|
configValues = append(configValues, ConfigValue{
|
||||||
Class: "custom_log",
|
Class: "custom_log",
|
||||||
|
|||||||
@@ -0,0 +1,215 @@
|
|||||||
|
package httpcaddyfile
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||||
|
_ "github.com/caddyserver/caddy/v2/modules/logging"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLogDirectiveSyntax(t *testing.T) {
|
||||||
|
for i, tc := range []struct {
|
||||||
|
input string
|
||||||
|
output string
|
||||||
|
expectError bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
log
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
output: `{"apps":{"http":{"servers":{"srv0":{"listen":[":8080"],"logs":{}}}}}}`,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
log {
|
||||||
|
output file foo.log
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
output: `{"logging":{"logs":{"default":{"exclude":["http.log.access.log0"]},"log0":{"writer":{"filename":"foo.log","output":"file"},"include":["http.log.access.log0"]}}},"apps":{"http":{"servers":{"srv0":{"listen":[":8080"],"logs":{"default_logger_name":"log0"}}}}}}`,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
log {
|
||||||
|
format filter {
|
||||||
|
wrap console
|
||||||
|
fields {
|
||||||
|
request>remote_ip ip_mask {
|
||||||
|
ipv4 24
|
||||||
|
ipv6 32
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
output: `{"logging":{"logs":{"default":{"exclude":["http.log.access.log0"]},"log0":{"encoder":{"fields":{"request\u003eremote_ip":{"filter":"ip_mask","ipv4_cidr":24,"ipv6_cidr":32}},"format":"filter","wrap":{"format":"console"}},"include":["http.log.access.log0"]}}},"apps":{"http":{"servers":{"srv0":{"listen":[":8080"],"logs":{"default_logger_name":"log0"}}}}}}`,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
log invalid {
|
||||||
|
output file foo.log
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
expectError: true,
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
|
||||||
|
adapter := caddyfile.Adapter{
|
||||||
|
ServerType: ServerType{},
|
||||||
|
}
|
||||||
|
|
||||||
|
out, _, err := adapter.Adapt([]byte(tc.input), nil)
|
||||||
|
|
||||||
|
if err != nil != tc.expectError {
|
||||||
|
t.Errorf("Test %d error expectation failed Expected: %v, got %s", i, tc.expectError, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if string(out) != tc.output {
|
||||||
|
t.Errorf("Test %d error output mismatch Expected: %s, got %s", i, tc.output, out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRedirDirectiveSyntax(t *testing.T) {
|
||||||
|
for i, tc := range []struct {
|
||||||
|
input string
|
||||||
|
expectError bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
redir :8081
|
||||||
|
}`,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
redir * :8081
|
||||||
|
}`,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
redir /api/* :8081 300
|
||||||
|
}`,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
redir :8081 300
|
||||||
|
}`,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
redir /api/* :8081 399
|
||||||
|
}`,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
redir :8081 399
|
||||||
|
}`,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
redir /old.html /new.html
|
||||||
|
}`,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
redir /old.html /new.html temporary
|
||||||
|
}`,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
redir https://example.com{uri} permanent
|
||||||
|
}`,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
redir /old.html /new.html permanent
|
||||||
|
}`,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
redir /old.html /new.html html
|
||||||
|
}`,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// this is now allowed so a Location header
|
||||||
|
// can be written and consumed by JS
|
||||||
|
// in the case of XHR requests
|
||||||
|
input: `:8080 {
|
||||||
|
redir * :8081 401
|
||||||
|
}`,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
redir * :8081 402
|
||||||
|
}`,
|
||||||
|
expectError: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
redir * :8081 {http.reverse_proxy.status_code}
|
||||||
|
}`,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
redir /old.html /new.html htlm
|
||||||
|
}`,
|
||||||
|
expectError: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
redir * :8081 200
|
||||||
|
}`,
|
||||||
|
expectError: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
redir * :8081 temp
|
||||||
|
}`,
|
||||||
|
expectError: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
redir * :8081 perm
|
||||||
|
}`,
|
||||||
|
expectError: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `:8080 {
|
||||||
|
redir * :8081 php
|
||||||
|
}`,
|
||||||
|
expectError: true,
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
|
||||||
|
adapter := caddyfile.Adapter{
|
||||||
|
ServerType: ServerType{},
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _, err := adapter.Adapt([]byte(tc.input), nil)
|
||||||
|
|
||||||
|
if err != nil != tc.expectError {
|
||||||
|
t.Errorf("Test %d error expectation failed Expected: %v, got %s", i, tc.expectError, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -37,32 +37,47 @@ import (
|
|||||||
// The header directive goes second so that headers
|
// The header directive goes second so that headers
|
||||||
// can be manipulated before doing redirects.
|
// can be manipulated before doing redirects.
|
||||||
var directiveOrder = []string{
|
var directiveOrder = []string{
|
||||||
|
"tracing",
|
||||||
|
|
||||||
|
"map",
|
||||||
|
"vars",
|
||||||
"root",
|
"root",
|
||||||
|
|
||||||
"header",
|
"header",
|
||||||
|
"copy_response_headers", // only in reverse_proxy's handle_response
|
||||||
|
"request_body",
|
||||||
|
|
||||||
"redir",
|
"redir",
|
||||||
"rewrite",
|
|
||||||
|
|
||||||
// URI manipulation
|
// incoming request manipulation
|
||||||
|
"method",
|
||||||
|
"rewrite",
|
||||||
"uri",
|
"uri",
|
||||||
"try_files",
|
"try_files",
|
||||||
|
|
||||||
// middleware handlers; some wrap responses
|
// middleware handlers; some wrap responses
|
||||||
"basicauth",
|
"basicauth",
|
||||||
|
"forward_auth",
|
||||||
"request_header",
|
"request_header",
|
||||||
"encode",
|
"encode",
|
||||||
|
"push",
|
||||||
"templates",
|
"templates",
|
||||||
|
|
||||||
// special routing directives
|
// special routing & dispatching directives
|
||||||
"handle",
|
"handle",
|
||||||
|
"handle_path",
|
||||||
"route",
|
"route",
|
||||||
|
|
||||||
// handlers that typically respond to requests
|
// handlers that typically respond to requests
|
||||||
|
"abort",
|
||||||
|
"error",
|
||||||
|
"copy_response", // only in reverse_proxy's handle_response
|
||||||
"respond",
|
"respond",
|
||||||
|
"metrics",
|
||||||
"reverse_proxy",
|
"reverse_proxy",
|
||||||
"php_fastcgi",
|
"php_fastcgi",
|
||||||
"file_server",
|
"file_server",
|
||||||
|
"acme_server",
|
||||||
}
|
}
|
||||||
|
|
||||||
// directiveIsOrdered returns true if dir is
|
// directiveIsOrdered returns true if dir is
|
||||||
@@ -97,20 +112,11 @@ func RegisterHandlerDirective(dir string, setupFunc UnmarshalHandlerFunc) {
|
|||||||
return nil, h.ArgErr()
|
return nil, h.ArgErr()
|
||||||
}
|
}
|
||||||
|
|
||||||
matcherSet, ok, err := h.MatcherToken()
|
matcherSet, err := h.ExtractMatcherSet()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if ok {
|
|
||||||
// strip matcher token; we don't need to
|
|
||||||
// use the return value here because a
|
|
||||||
// new dispenser should have been made
|
|
||||||
// solely for this directive's tokens,
|
|
||||||
// with no other uses of same slice
|
|
||||||
h.Dispenser.Delete()
|
|
||||||
}
|
|
||||||
|
|
||||||
h.Dispenser.Reset() // pretend this lookahead never happened
|
|
||||||
val, err := setupFunc(h)
|
val, err := setupFunc(h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -120,6 +126,17 @@ func RegisterHandlerDirective(dir string, setupFunc UnmarshalHandlerFunc) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RegisterGlobalOption registers a unique global option opt with
|
||||||
|
// an associated unmarshaling (setup) function. When the global
|
||||||
|
// option opt is encountered in a Caddyfile, setupFunc will be
|
||||||
|
// called to unmarshal its tokens.
|
||||||
|
func RegisterGlobalOption(opt string, setupFunc UnmarshalGlobalFunc) {
|
||||||
|
if _, ok := registeredGlobalOptions[opt]; ok {
|
||||||
|
panic("global option " + opt + " already registered")
|
||||||
|
}
|
||||||
|
registeredGlobalOptions[opt] = setupFunc
|
||||||
|
}
|
||||||
|
|
||||||
// Helper is a type which helps setup a value from
|
// Helper is a type which helps setup a value from
|
||||||
// Caddyfile tokens.
|
// Caddyfile tokens.
|
||||||
type Helper struct {
|
type Helper struct {
|
||||||
@@ -184,7 +201,12 @@ func (h Helper) ExtractMatcherSet() (caddy.ModuleMap, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if hasMatcher {
|
if hasMatcher {
|
||||||
h.Dispenser.Delete() // strip matcher token
|
// strip matcher token; we don't need to
|
||||||
|
// use the return value here because a
|
||||||
|
// new dispenser should have been made
|
||||||
|
// solely for this directive's tokens,
|
||||||
|
// with no other uses of same slice
|
||||||
|
h.Dispenser.Delete()
|
||||||
}
|
}
|
||||||
h.Dispenser.Reset() // pretend this lookahead never happened
|
h.Dispenser.Reset() // pretend this lookahead never happened
|
||||||
return matcherSet, nil
|
return matcherSet, nil
|
||||||
@@ -250,6 +272,94 @@ func (h Helper) NewBindAddresses(addrs []string) []ConfigValue {
|
|||||||
return []ConfigValue{{Class: "bind", Value: addrs}}
|
return []ConfigValue{{Class: "bind", Value: addrs}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithDispenser returns a new instance based on d. All others Helper
|
||||||
|
// fields are copied, so typically maps are shared with this new instance.
|
||||||
|
func (h Helper) WithDispenser(d *caddyfile.Dispenser) Helper {
|
||||||
|
h.Dispenser = d
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseSegmentAsSubroute parses the segment such that its subdirectives
|
||||||
|
// are themselves treated as directives, from which a subroute is built
|
||||||
|
// and returned.
|
||||||
|
func ParseSegmentAsSubroute(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||||
|
allResults, err := parseSegmentAsConfig(h)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return buildSubroute(allResults, h.groupCounter)
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseSegmentAsConfig parses the segment such that its subdirectives
|
||||||
|
// are themselves treated as directives, including named matcher definitions,
|
||||||
|
// and the raw Config structs are returned.
|
||||||
|
func parseSegmentAsConfig(h Helper) ([]ConfigValue, error) {
|
||||||
|
var allResults []ConfigValue
|
||||||
|
|
||||||
|
for h.Next() {
|
||||||
|
// don't allow non-matcher args on the first line
|
||||||
|
if h.NextArg() {
|
||||||
|
return nil, h.ArgErr()
|
||||||
|
}
|
||||||
|
|
||||||
|
// slice the linear list of tokens into top-level segments
|
||||||
|
var segments []caddyfile.Segment
|
||||||
|
for nesting := h.Nesting(); h.NextBlock(nesting); {
|
||||||
|
segments = append(segments, h.NextSegment())
|
||||||
|
}
|
||||||
|
|
||||||
|
// copy existing matcher definitions so we can augment
|
||||||
|
// new ones that are defined only in this scope
|
||||||
|
matcherDefs := make(map[string]caddy.ModuleMap, len(h.matcherDefs))
|
||||||
|
for key, val := range h.matcherDefs {
|
||||||
|
matcherDefs[key] = val
|
||||||
|
}
|
||||||
|
|
||||||
|
// find and extract any embedded matcher definitions in this scope
|
||||||
|
for i := 0; i < len(segments); i++ {
|
||||||
|
seg := segments[i]
|
||||||
|
if strings.HasPrefix(seg.Directive(), matcherPrefix) {
|
||||||
|
// parse, then add the matcher to matcherDefs
|
||||||
|
err := parseMatcherDefinitions(caddyfile.NewDispenser(seg), matcherDefs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// remove the matcher segment (consumed), then step back the loop
|
||||||
|
segments = append(segments[:i], segments[i+1:]...)
|
||||||
|
i--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// with matchers ready to go, evaluate each directive's segment
|
||||||
|
for _, seg := range segments {
|
||||||
|
dir := seg.Directive()
|
||||||
|
dirFunc, ok := registeredDirectives[dir]
|
||||||
|
if !ok {
|
||||||
|
return nil, h.Errf("unrecognized directive: %s - are you sure your Caddyfile structure (nesting and braces) is correct?", dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
subHelper := h
|
||||||
|
subHelper.Dispenser = caddyfile.NewDispenser(seg)
|
||||||
|
subHelper.matcherDefs = matcherDefs
|
||||||
|
|
||||||
|
results, err := dirFunc(subHelper)
|
||||||
|
if err != nil {
|
||||||
|
return nil, h.Errf("parsing caddyfile tokens for '%s': %v", dir, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
dir = normalizeDirectiveName(dir)
|
||||||
|
|
||||||
|
for _, result := range results {
|
||||||
|
result.directive = dir
|
||||||
|
allResults = append(allResults, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return allResults, nil
|
||||||
|
}
|
||||||
|
|
||||||
// ConfigValue represents a value to be added to the final
|
// ConfigValue represents a value to be added to the final
|
||||||
// configuration, or a value to be consulted when building
|
// configuration, or a value to be consulted when building
|
||||||
// the final configuration.
|
// the final configuration.
|
||||||
@@ -276,113 +386,71 @@ func sortRoutes(routes []ConfigValue) {
|
|||||||
dirPositions[dir] = i
|
dirPositions[dir] = i
|
||||||
}
|
}
|
||||||
|
|
||||||
// while we are sorting, we will need to decode a route's path matcher
|
|
||||||
// in order to sub-sort by path length; we can amortize this operation
|
|
||||||
// for efficiency by storing the decoded matchers in a slice
|
|
||||||
decodedMatchers := make([]caddyhttp.MatchPath, len(routes))
|
|
||||||
|
|
||||||
sort.SliceStable(routes, func(i, j int) bool {
|
sort.SliceStable(routes, func(i, j int) bool {
|
||||||
|
// if the directives are different, just use the established directive order
|
||||||
iDir, jDir := routes[i].directive, routes[j].directive
|
iDir, jDir := routes[i].directive, routes[j].directive
|
||||||
if iDir == jDir {
|
if iDir != jDir {
|
||||||
// directives are the same; sub-sort by path matcher length
|
return dirPositions[iDir] < dirPositions[jDir]
|
||||||
// if there's only one matcher set and one path (common case)
|
}
|
||||||
iRoute, ok := routes[i].Value.(caddyhttp.Route)
|
|
||||||
if !ok {
|
// directives are the same; sub-sort by path matcher length if there's
|
||||||
return false
|
// only one matcher set and one path (this is a very common case and
|
||||||
}
|
// usually -- but not always -- helpful/expected, oh well; user can
|
||||||
jRoute, ok := routes[j].Value.(caddyhttp.Route)
|
// always take manual control of order using handler or route blocks)
|
||||||
if !ok {
|
iRoute, ok := routes[i].Value.(caddyhttp.Route)
|
||||||
return false
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
jRoute, ok := routes[j].Value.(caddyhttp.Route)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// decode the path matchers, if there is just one of them
|
||||||
|
var iPM, jPM caddyhttp.MatchPath
|
||||||
|
if len(iRoute.MatcherSetsRaw) == 1 {
|
||||||
|
_ = json.Unmarshal(iRoute.MatcherSetsRaw[0]["path"], &iPM)
|
||||||
|
}
|
||||||
|
if len(jRoute.MatcherSetsRaw) == 1 {
|
||||||
|
_ = json.Unmarshal(jRoute.MatcherSetsRaw[0]["path"], &jPM)
|
||||||
|
}
|
||||||
|
|
||||||
|
// sort by longer path (more specific) first; missing path
|
||||||
|
// matchers or multi-matchers are treated as zero-length paths
|
||||||
|
var iPathLen, jPathLen int
|
||||||
|
if len(iPM) > 0 {
|
||||||
|
iPathLen = len(iPM[0])
|
||||||
|
}
|
||||||
|
if len(jPM) > 0 {
|
||||||
|
jPathLen = len(jPM[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
// some directives involve setting values which can overwrite
|
||||||
|
// eachother, so it makes most sense to reverse the order so
|
||||||
|
// that the lease specific matcher is first; everything else
|
||||||
|
// has most-specific matcher first
|
||||||
|
if iDir == "vars" {
|
||||||
|
// if both directives have no path matcher, use whichever one
|
||||||
|
// has no matcher first.
|
||||||
|
if iPathLen == 0 && jPathLen == 0 {
|
||||||
|
return len(iRoute.MatcherSetsRaw) == 0 && len(jRoute.MatcherSetsRaw) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// use already-decoded matcher, or decode if it's the first time seeing it
|
// sort with the least-specific (shortest) path first
|
||||||
iPM, jPM := decodedMatchers[i], decodedMatchers[j]
|
return iPathLen < jPathLen
|
||||||
if iPM == nil && len(iRoute.MatcherSetsRaw) == 1 {
|
} else {
|
||||||
var pathMatcher caddyhttp.MatchPath
|
// if both directives have no path matcher, use whichever one
|
||||||
_ = json.Unmarshal(iRoute.MatcherSetsRaw[0]["path"], &pathMatcher)
|
// has any kind of matcher defined first.
|
||||||
decodedMatchers[i] = pathMatcher
|
if iPathLen == 0 && jPathLen == 0 {
|
||||||
iPM = pathMatcher
|
return len(iRoute.MatcherSetsRaw) > 0 && len(jRoute.MatcherSetsRaw) == 0
|
||||||
}
|
|
||||||
if jPM == nil && len(jRoute.MatcherSetsRaw) == 1 {
|
|
||||||
var pathMatcher caddyhttp.MatchPath
|
|
||||||
_ = json.Unmarshal(jRoute.MatcherSetsRaw[0]["path"], &pathMatcher)
|
|
||||||
decodedMatchers[j] = pathMatcher
|
|
||||||
jPM = pathMatcher
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// sort by longer path (more specific) first; missing
|
// sort with the most-specific (longest) path first
|
||||||
// path matchers are treated as zero-length paths
|
|
||||||
var iPathLen, jPathLen int
|
|
||||||
if iPM != nil {
|
|
||||||
iPathLen = len(iPM[0])
|
|
||||||
}
|
|
||||||
if jPM != nil {
|
|
||||||
jPathLen = len(jPM[0])
|
|
||||||
}
|
|
||||||
return iPathLen > jPathLen
|
return iPathLen > jPathLen
|
||||||
}
|
}
|
||||||
|
|
||||||
return dirPositions[iDir] < dirPositions[jDir]
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseSegmentAsSubroute parses the segment such that its subdirectives
|
|
||||||
// are themselves treated as directives, from which a subroute is built
|
|
||||||
// and returned.
|
|
||||||
func parseSegmentAsSubroute(h Helper) (caddyhttp.MiddlewareHandler, error) {
|
|
||||||
var allResults []ConfigValue
|
|
||||||
|
|
||||||
for h.Next() {
|
|
||||||
// slice the linear list of tokens into top-level segments
|
|
||||||
var segments []caddyfile.Segment
|
|
||||||
for nesting := h.Nesting(); h.NextBlock(nesting); {
|
|
||||||
segments = append(segments, h.NextSegment())
|
|
||||||
}
|
|
||||||
|
|
||||||
// copy existing matcher definitions so we can augment
|
|
||||||
// new ones that are defined only in this scope
|
|
||||||
matcherDefs := make(map[string]caddy.ModuleMap, len(h.matcherDefs))
|
|
||||||
for key, val := range h.matcherDefs {
|
|
||||||
matcherDefs[key] = val
|
|
||||||
}
|
|
||||||
|
|
||||||
// find and extract any embedded matcher definitions in this scope
|
|
||||||
for i, seg := range segments {
|
|
||||||
if strings.HasPrefix(seg.Directive(), matcherPrefix) {
|
|
||||||
err := parseMatcherDefinitions(caddyfile.NewDispenser(seg), matcherDefs)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
segments = append(segments[:i], segments[i+1:]...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// with matchers ready to go, evaluate each directive's segment
|
|
||||||
for _, seg := range segments {
|
|
||||||
dir := seg.Directive()
|
|
||||||
dirFunc, ok := registeredDirectives[dir]
|
|
||||||
if !ok {
|
|
||||||
return nil, h.Errf("unrecognized directive: %s", dir)
|
|
||||||
}
|
|
||||||
|
|
||||||
subHelper := h
|
|
||||||
subHelper.Dispenser = caddyfile.NewDispenser(seg)
|
|
||||||
subHelper.matcherDefs = matcherDefs
|
|
||||||
|
|
||||||
results, err := dirFunc(subHelper)
|
|
||||||
if err != nil {
|
|
||||||
return nil, h.Errf("parsing caddyfile tokens for '%s': %v", dir, err)
|
|
||||||
}
|
|
||||||
for _, result := range results {
|
|
||||||
result.directive = dir
|
|
||||||
allResults = append(allResults, result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return buildSubroute(allResults, h.groupCounter)
|
|
||||||
}
|
|
||||||
|
|
||||||
// serverBlock pairs a Caddyfile server block with
|
// serverBlock pairs a Caddyfile server block with
|
||||||
// a "pile" of config values, keyed by class name,
|
// a "pile" of config values, keyed by class name,
|
||||||
// as well as its parsed keys for convenience.
|
// as well as its parsed keys for convenience.
|
||||||
@@ -435,6 +503,27 @@ func (sb serverBlock) hostsFromKeys(loggerMode bool) []string {
|
|||||||
return sblockHosts
|
return sblockHosts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sb serverBlock) hostsFromKeysNotHTTP(httpPort string) []string {
|
||||||
|
// ensure each entry in our list is unique
|
||||||
|
hostMap := make(map[string]struct{})
|
||||||
|
for _, addr := range sb.keys {
|
||||||
|
if addr.Host == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if addr.Scheme != "http" && addr.Port != httpPort {
|
||||||
|
hostMap[addr.Host] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// convert map to slice
|
||||||
|
sblockHosts := make([]string, 0, len(hostMap))
|
||||||
|
for host := range hostMap {
|
||||||
|
sblockHosts = append(sblockHosts, host)
|
||||||
|
}
|
||||||
|
|
||||||
|
return sblockHosts
|
||||||
|
}
|
||||||
|
|
||||||
// hasHostCatchAllKey returns true if sb has a key that
|
// hasHostCatchAllKey returns true if sb has a key that
|
||||||
// omits a host portion, i.e. it "catches all" hosts.
|
// omits a host portion, i.e. it "catches all" hosts.
|
||||||
func (sb serverBlock) hasHostCatchAllKey() bool {
|
func (sb serverBlock) hasHostCatchAllKey() bool {
|
||||||
@@ -446,6 +535,17 @@ func (sb serverBlock) hasHostCatchAllKey() bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isAllHTTP returns true if all sb keys explicitly specify
|
||||||
|
// the http:// scheme
|
||||||
|
func (sb serverBlock) isAllHTTP() bool {
|
||||||
|
for _, addr := range sb.keys {
|
||||||
|
if addr.Scheme != "http" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// UnmarshalFunc is a function which can unmarshal Caddyfile
|
// UnmarshalFunc is a function which can unmarshal Caddyfile
|
||||||
// tokens into zero or more config values using a Helper type.
|
// tokens into zero or more config values using a Helper type.
|
||||||
@@ -462,6 +562,14 @@ type (
|
|||||||
// for you. These are passed to a call to
|
// for you. These are passed to a call to
|
||||||
// RegisterHandlerDirective.
|
// RegisterHandlerDirective.
|
||||||
UnmarshalHandlerFunc func(h Helper) (caddyhttp.MiddlewareHandler, error)
|
UnmarshalHandlerFunc func(h Helper) (caddyhttp.MiddlewareHandler, error)
|
||||||
|
|
||||||
|
// UnmarshalGlobalFunc is a function which can unmarshal Caddyfile
|
||||||
|
// tokens from a global option. It is passed the tokens to parse and
|
||||||
|
// existing value from the previous instance of this global option
|
||||||
|
// (if any). It returns the value to associate with this global option.
|
||||||
|
UnmarshalGlobalFunc func(d *caddyfile.Dispenser, existingVal interface{}) (interface{}, error)
|
||||||
)
|
)
|
||||||
|
|
||||||
var registeredDirectives = make(map[string]UnmarshalFunc)
|
var registeredDirectives = make(map[string]UnmarshalFunc)
|
||||||
|
|
||||||
|
var registeredGlobalOptions = make(map[string]UnmarshalGlobalFunc)
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -26,13 +27,26 @@ import (
|
|||||||
"github.com/caddyserver/caddy/v2/caddyconfig"
|
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||||
|
"github.com/caddyserver/caddy/v2/modules/caddypki"
|
||||||
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
caddyconfig.RegisterAdapter("caddyfile", caddyfile.Adapter{ServerType: ServerType{}})
|
caddyconfig.RegisterAdapter("caddyfile", caddyfile.Adapter{ServerType: ServerType{}})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// App represents the configuration for a non-standard
|
||||||
|
// Caddy app module (e.g. third-party plugin) which was
|
||||||
|
// parsed from a global options block.
|
||||||
|
type App struct {
|
||||||
|
// The JSON key for the app being configured
|
||||||
|
Name string
|
||||||
|
|
||||||
|
// The raw app config as JSON
|
||||||
|
Value json.RawMessage
|
||||||
|
}
|
||||||
|
|
||||||
// ServerType can set up a config from an HTTP Caddyfile.
|
// ServerType can set up a config from an HTTP Caddyfile.
|
||||||
type ServerType struct {
|
type ServerType struct {
|
||||||
}
|
}
|
||||||
@@ -53,6 +67,9 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
|
|||||||
originalServerBlocks := make([]serverBlock, 0, len(inputServerBlocks))
|
originalServerBlocks := make([]serverBlock, 0, len(inputServerBlocks))
|
||||||
for i, sblock := range inputServerBlocks {
|
for i, sblock := range inputServerBlocks {
|
||||||
for j, k := range sblock.Keys {
|
for j, k := range sblock.Keys {
|
||||||
|
if j == 0 && strings.HasPrefix(k, "@") {
|
||||||
|
return nil, warnings, fmt.Errorf("cannot define a matcher outside of a site block: '%s'", k)
|
||||||
|
}
|
||||||
if _, ok := sbKeys[k]; ok {
|
if _, ok := sbKeys[k]; ok {
|
||||||
return nil, warnings, fmt.Errorf("duplicate site address not allowed: '%s' in %v (site block %d, key %d)", k, sblock.Keys, i, j)
|
return nil, warnings, fmt.Errorf("duplicate site address not allowed: '%s' in %v (site block %d, key %d)", k, sblock.Keys, i, j)
|
||||||
}
|
}
|
||||||
@@ -71,34 +88,37 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
|
|||||||
return nil, warnings, err
|
return nil, warnings, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// replace shorthand placeholders (which are convenient
|
||||||
|
// when writing a Caddyfile) with their actual placeholder
|
||||||
|
// identifiers or variable names
|
||||||
|
replacer := strings.NewReplacer(placeholderShorthands()...)
|
||||||
|
|
||||||
|
// these are placeholders that allow a user-defined final
|
||||||
|
// parameters, but we still want to provide a shorthand
|
||||||
|
// for those, so we use a regexp to replace
|
||||||
|
regexpReplacements := []struct {
|
||||||
|
search *regexp.Regexp
|
||||||
|
replace string
|
||||||
|
}{
|
||||||
|
{regexp.MustCompile(`{query\.([\w-]*)}`), "{http.request.uri.query.$1}"},
|
||||||
|
{regexp.MustCompile(`{labels\.([\w-]*)}`), "{http.request.host.labels.$1}"},
|
||||||
|
{regexp.MustCompile(`{header\.([\w-]*)}`), "{http.request.header.$1}"},
|
||||||
|
{regexp.MustCompile(`{path\.([\w-]*)}`), "{http.request.uri.path.$1}"},
|
||||||
|
{regexp.MustCompile(`{re\.([\w-]*)\.([\w-]*)}`), "{http.regexp.$1.$2}"},
|
||||||
|
{regexp.MustCompile(`{vars\.([\w-]*)}`), "{http.vars.$1}"},
|
||||||
|
{regexp.MustCompile(`{rp\.([\w-\.]*)}`), "{http.reverse_proxy.$1}"},
|
||||||
|
{regexp.MustCompile(`{err\.([\w-\.]*)}`), "{http.error.$1}"},
|
||||||
|
}
|
||||||
|
|
||||||
for _, sb := range originalServerBlocks {
|
for _, sb := range originalServerBlocks {
|
||||||
// replace shorthand placeholders (which are
|
|
||||||
// convenient when writing a Caddyfile) with
|
|
||||||
// their actual placeholder identifiers or
|
|
||||||
// variable names
|
|
||||||
replacer := strings.NewReplacer(
|
|
||||||
"{dir}", "{http.request.uri.path.dir}",
|
|
||||||
"{file}", "{http.request.uri.path.file}",
|
|
||||||
"{host}", "{http.request.host}",
|
|
||||||
"{hostport}", "{http.request.hostport}",
|
|
||||||
"{method}", "{http.request.method}",
|
|
||||||
"{path}", "{http.request.uri.path}",
|
|
||||||
"{query}", "{http.request.uri.query}",
|
|
||||||
"{remote}", "{http.request.remote}",
|
|
||||||
"{remote_host}", "{http.request.remote.host}",
|
|
||||||
"{remote_port}", "{http.request.remote.port}",
|
|
||||||
"{scheme}", "{http.request.scheme}",
|
|
||||||
"{uri}", "{http.request.uri}",
|
|
||||||
"{tls_cipher}", "{http.request.tls.cipher_suite}",
|
|
||||||
"{tls_version}", "{http.request.tls.version}",
|
|
||||||
"{tls_client_fingerprint}", "{http.request.tls.client.fingerprint}",
|
|
||||||
"{tls_client_issuer}", "{http.request.tls.client.issuer}",
|
|
||||||
"{tls_client_serial}", "{http.request.tls.client.serial}",
|
|
||||||
"{tls_client_subject}", "{http.request.tls.client.subject}",
|
|
||||||
)
|
|
||||||
for _, segment := range sb.block.Segments {
|
for _, segment := range sb.block.Segments {
|
||||||
for i := 0; i < len(segment); i++ {
|
for i := 0; i < len(segment); i++ {
|
||||||
|
// simple string replacements
|
||||||
segment[i].Text = replacer.Replace(segment[i].Text)
|
segment[i].Text = replacer.Replace(segment[i].Text)
|
||||||
|
// complex regexp replacements
|
||||||
|
for _, r := range regexpReplacements {
|
||||||
|
segment[i].Text = r.search.ReplaceAllString(segment[i].Text, r.replace)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +150,11 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
|
|||||||
dirFunc, ok := registeredDirectives[dir]
|
dirFunc, ok := registeredDirectives[dir]
|
||||||
if !ok {
|
if !ok {
|
||||||
tkn := segment[0]
|
tkn := segment[0]
|
||||||
return nil, warnings, fmt.Errorf("%s:%d: unrecognized directive: %s", tkn.File, tkn.Line, dir)
|
message := "%s:%d: unrecognized directive: %s"
|
||||||
|
if !sb.block.HasBraces {
|
||||||
|
message += "\nDid you mean to define a second site? If so, you must use curly braces around each site to separate their configurations."
|
||||||
|
}
|
||||||
|
return nil, warnings, fmt.Errorf(message, tkn.File, tkn.Line, dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
h := Helper{
|
h := Helper{
|
||||||
@@ -147,6 +171,9 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, warnings, fmt.Errorf("parsing caddyfile tokens for '%s': %v", dir, err)
|
return nil, warnings, fmt.Errorf("parsing caddyfile tokens for '%s': %v", dir, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dir = normalizeDirectiveName(dir)
|
||||||
|
|
||||||
for _, result := range results {
|
for _, result := range results {
|
||||||
result.directive = dir
|
result.directive = dir
|
||||||
sb.pile[result.Class] = append(sb.pile[result.Class], result)
|
sb.pile[result.Class] = append(sb.pile[result.Class], result)
|
||||||
@@ -172,9 +199,10 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
|
|||||||
|
|
||||||
// now that each server is configured, make the HTTP app
|
// now that each server is configured, make the HTTP app
|
||||||
httpApp := caddyhttp.App{
|
httpApp := caddyhttp.App{
|
||||||
HTTPPort: tryInt(options["http_port"], &warnings),
|
HTTPPort: tryInt(options["http_port"], &warnings),
|
||||||
HTTPSPort: tryInt(options["https_port"], &warnings),
|
HTTPSPort: tryInt(options["https_port"], &warnings),
|
||||||
Servers: servers,
|
GracePeriod: tryDuration(options["grace_period"], &warnings),
|
||||||
|
Servers: servers,
|
||||||
}
|
}
|
||||||
|
|
||||||
// then make the TLS app
|
// then make the TLS app
|
||||||
@@ -183,31 +211,32 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
|
|||||||
return nil, warnings, err
|
return nil, warnings, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// if experimental HTTP/3 is enabled, enable it on each server
|
// then make the PKI app
|
||||||
if enableH3, ok := options["experimental_http3"].(bool); ok && enableH3 {
|
pkiApp, warnings, err := st.buildPKIApp(pairings, options, warnings)
|
||||||
for _, srv := range httpApp.Servers {
|
if err != nil {
|
||||||
srv.ExperimentalHTTP3 = true
|
return nil, warnings, err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// extract any custom logs, and enforce configured levels
|
// extract any custom logs, and enforce configured levels
|
||||||
var customLogs []namedCustomLog
|
var customLogs []namedCustomLog
|
||||||
var hasDefaultLog bool
|
var hasDefaultLog bool
|
||||||
for _, p := range pairings {
|
addCustomLog := func(ncl namedCustomLog) {
|
||||||
for _, sb := range p.serverBlocks {
|
if ncl.name == "" {
|
||||||
for _, clVal := range sb.pile["custom_log"] {
|
return
|
||||||
ncl := clVal.Value.(namedCustomLog)
|
}
|
||||||
if ncl.name == "" {
|
if ncl.name == "default" {
|
||||||
continue
|
hasDefaultLog = true
|
||||||
}
|
}
|
||||||
if ncl.name == "default" {
|
if _, ok := options["debug"]; ok && ncl.log.Level == "" {
|
||||||
hasDefaultLog = true
|
ncl.log.Level = "DEBUG"
|
||||||
}
|
}
|
||||||
if _, ok := options["debug"]; ok && ncl.log.Level == "" {
|
customLogs = append(customLogs, ncl)
|
||||||
ncl.log.Level = "DEBUG"
|
}
|
||||||
}
|
|
||||||
customLogs = append(customLogs, ncl)
|
// Apply global log options, when set
|
||||||
}
|
if options["log"] != nil {
|
||||||
|
for _, logValue := range options["log"].([]ConfigValue) {
|
||||||
|
addCustomLog(logValue.Value.(namedCustomLog))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,26 +251,45 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply server-specific log options
|
||||||
|
for _, p := range pairings {
|
||||||
|
for _, sb := range p.serverBlocks {
|
||||||
|
for _, clVal := range sb.pile["custom_log"] {
|
||||||
|
addCustomLog(clVal.Value.(namedCustomLog))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// annnd the top-level config, then we're done!
|
// annnd the top-level config, then we're done!
|
||||||
cfg := &caddy.Config{AppsRaw: make(caddy.ModuleMap)}
|
cfg := &caddy.Config{AppsRaw: make(caddy.ModuleMap)}
|
||||||
|
|
||||||
|
// loop through the configured options, and if any of
|
||||||
|
// them are an httpcaddyfile App, then we insert them
|
||||||
|
// into the config as raw Caddy apps
|
||||||
|
for _, opt := range options {
|
||||||
|
if app, ok := opt.(App); ok {
|
||||||
|
cfg.AppsRaw[app.Name] = app.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// insert the standard Caddy apps into the config
|
||||||
if len(httpApp.Servers) > 0 {
|
if len(httpApp.Servers) > 0 {
|
||||||
cfg.AppsRaw["http"] = caddyconfig.JSON(httpApp, &warnings)
|
cfg.AppsRaw["http"] = caddyconfig.JSON(httpApp, &warnings)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(tlsApp, &caddytls.TLS{CertificatesRaw: make(caddy.ModuleMap)}) {
|
if !reflect.DeepEqual(tlsApp, &caddytls.TLS{CertificatesRaw: make(caddy.ModuleMap)}) {
|
||||||
cfg.AppsRaw["tls"] = caddyconfig.JSON(tlsApp, &warnings)
|
cfg.AppsRaw["tls"] = caddyconfig.JSON(tlsApp, &warnings)
|
||||||
}
|
}
|
||||||
|
if !reflect.DeepEqual(pkiApp, &caddypki.PKI{CAs: make(map[string]*caddypki.CA)}) {
|
||||||
|
cfg.AppsRaw["pki"] = caddyconfig.JSON(pkiApp, &warnings)
|
||||||
|
}
|
||||||
if storageCvtr, ok := options["storage"].(caddy.StorageConverter); ok {
|
if storageCvtr, ok := options["storage"].(caddy.StorageConverter); ok {
|
||||||
cfg.StorageRaw = caddyconfig.JSONModuleObject(storageCvtr,
|
cfg.StorageRaw = caddyconfig.JSONModuleObject(storageCvtr,
|
||||||
"module",
|
"module",
|
||||||
storageCvtr.(caddy.Module).CaddyModule().ID.Name(),
|
storageCvtr.(caddy.Module).CaddyModule().ID.Name(),
|
||||||
&warnings)
|
&warnings)
|
||||||
}
|
}
|
||||||
if adminConfig, ok := options["admin"].(string); ok && adminConfig != "" {
|
if adminConfig, ok := options["admin"].(*caddy.AdminConfig); ok && adminConfig != nil {
|
||||||
if adminConfig == "off" {
|
cfg.Admin = adminConfig
|
||||||
cfg.Admin = &caddy.AdminConfig{Disabled: true}
|
|
||||||
} else {
|
|
||||||
cfg.Admin = &caddy.AdminConfig{Listen: adminConfig}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if len(customLogs) > 0 {
|
if len(customLogs) > 0 {
|
||||||
if cfg.Logging == nil {
|
if cfg.Logging == nil {
|
||||||
@@ -256,7 +304,7 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
|
|||||||
// most users seem to prefer not writing access logs
|
// most users seem to prefer not writing access logs
|
||||||
// to the default log when they are directed to a
|
// to the default log when they are directed to a
|
||||||
// file or have any other special customization
|
// file or have any other special customization
|
||||||
if len(ncl.log.Include) > 0 {
|
if ncl.name != "default" && len(ncl.log.Include) > 0 {
|
||||||
defaultLog, ok := cfg.Logging.Logs["default"]
|
defaultLog, ok := cfg.Logging.Logs["default"]
|
||||||
if !ok {
|
if !ok {
|
||||||
defaultLog = new(caddy.CustomLog)
|
defaultLog = new(caddy.CustomLog)
|
||||||
@@ -280,44 +328,68 @@ func (ServerType) evaluateGlobalOptionsBlock(serverBlocks []serverBlock, options
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, segment := range serverBlocks[0].block.Segments {
|
for _, segment := range serverBlocks[0].block.Segments {
|
||||||
dir := segment.Directive()
|
opt := segment.Directive()
|
||||||
var val interface{}
|
var val interface{}
|
||||||
var err error
|
var err error
|
||||||
disp := caddyfile.NewDispenser(segment)
|
disp := caddyfile.NewDispenser(segment)
|
||||||
switch dir {
|
|
||||||
case "debug":
|
optFunc, ok := registeredGlobalOptions[opt]
|
||||||
val = true
|
if !ok {
|
||||||
case "http_port":
|
tkn := segment[0]
|
||||||
val, err = parseOptHTTPPort(disp)
|
return nil, fmt.Errorf("%s:%d: unrecognized global option: %s", tkn.File, tkn.Line, opt)
|
||||||
case "https_port":
|
|
||||||
val, err = parseOptHTTPSPort(disp)
|
|
||||||
case "default_sni":
|
|
||||||
val, err = parseOptSingleString(disp)
|
|
||||||
case "order":
|
|
||||||
val, err = parseOptOrder(disp)
|
|
||||||
case "experimental_http3":
|
|
||||||
val, err = parseOptExperimentalHTTP3(disp)
|
|
||||||
case "storage":
|
|
||||||
val, err = parseOptStorage(disp)
|
|
||||||
case "acme_ca", "acme_dns", "acme_ca_root":
|
|
||||||
val, err = parseOptSingleString(disp)
|
|
||||||
case "email":
|
|
||||||
val, err = parseOptSingleString(disp)
|
|
||||||
case "admin":
|
|
||||||
val, err = parseOptAdmin(disp)
|
|
||||||
case "on_demand_tls":
|
|
||||||
val, err = parseOptOnDemand(disp)
|
|
||||||
case "local_certs":
|
|
||||||
val = true
|
|
||||||
case "key_type":
|
|
||||||
val, err = parseOptSingleString(disp)
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("unrecognized parameter name: %s", dir)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val, err = optFunc(disp, options[opt])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s: %v", dir, err)
|
return nil, fmt.Errorf("parsing caddyfile tokens for '%s': %v", opt, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// As a special case, fold multiple "servers" options together
|
||||||
|
// in an array instead of overwriting a possible existing value
|
||||||
|
if opt == "servers" {
|
||||||
|
existingOpts, ok := options[opt].([]serverOptions)
|
||||||
|
if !ok {
|
||||||
|
existingOpts = []serverOptions{}
|
||||||
|
}
|
||||||
|
serverOpts, ok := val.(serverOptions)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("unexpected type from 'servers' global options: %T", val)
|
||||||
|
}
|
||||||
|
options[opt] = append(existingOpts, serverOpts)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Additionally, fold multiple "log" options together into an
|
||||||
|
// array so that multiple loggers can be configured.
|
||||||
|
if opt == "log" {
|
||||||
|
existingOpts, ok := options[opt].([]ConfigValue)
|
||||||
|
if !ok {
|
||||||
|
existingOpts = []ConfigValue{}
|
||||||
|
}
|
||||||
|
logOpts, ok := val.([]ConfigValue)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("unexpected type from 'log' global options: %T", val)
|
||||||
|
}
|
||||||
|
options[opt] = append(existingOpts, logOpts...)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
options[opt] = val
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we got "servers" options, we'll sort them by their listener address
|
||||||
|
if serverOpts, ok := options["servers"].([]serverOptions); ok {
|
||||||
|
sort.Slice(serverOpts, func(i, j int) bool {
|
||||||
|
return len(serverOpts[i].ListenerAddress) > len(serverOpts[j].ListenerAddress)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Reject the config if there are duplicate listener address
|
||||||
|
seen := make(map[string]bool)
|
||||||
|
for _, entry := range serverOpts {
|
||||||
|
if _, alreadySeen := seen[entry.ListenerAddress]; alreadySeen {
|
||||||
|
return nil, fmt.Errorf("cannot have 'servers' global options with duplicate listener addresses: %s", entry.ListenerAddress)
|
||||||
|
}
|
||||||
|
seen[entry.ListenerAddress] = true
|
||||||
}
|
}
|
||||||
options[dir] = val
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return serverBlocks[1:], nil
|
return serverBlocks[1:], nil
|
||||||
@@ -342,12 +414,42 @@ func (st *ServerType) serversFromPairings(
|
|||||||
if hsp, ok := options["https_port"].(int); ok {
|
if hsp, ok := options["https_port"].(int); ok {
|
||||||
httpsPort = strconv.Itoa(hsp)
|
httpsPort = strconv.Itoa(hsp)
|
||||||
}
|
}
|
||||||
|
autoHTTPS := "on"
|
||||||
|
if ah, ok := options["auto_https"].(string); ok {
|
||||||
|
autoHTTPS = ah
|
||||||
|
}
|
||||||
|
|
||||||
for i, p := range pairings {
|
for i, p := range pairings {
|
||||||
srv := &caddyhttp.Server{
|
srv := &caddyhttp.Server{
|
||||||
Listen: p.addresses,
|
Listen: p.addresses,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handle the auto_https global option
|
||||||
|
if autoHTTPS != "on" {
|
||||||
|
srv.AutoHTTPS = new(caddyhttp.AutoHTTPSConfig)
|
||||||
|
switch autoHTTPS {
|
||||||
|
case "off":
|
||||||
|
srv.AutoHTTPS.Disabled = true
|
||||||
|
case "disable_redirects":
|
||||||
|
srv.AutoHTTPS.DisableRedir = true
|
||||||
|
case "disable_certs":
|
||||||
|
srv.AutoHTTPS.DisableCerts = true
|
||||||
|
case "ignore_loaded_certs":
|
||||||
|
srv.AutoHTTPS.IgnoreLoadedCerts = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Using paths in site addresses is deprecated
|
||||||
|
// See ParseAddress() where parsing should later reject paths
|
||||||
|
// See https://github.com/caddyserver/caddy/pull/4728 for a full explanation
|
||||||
|
for _, sblock := range p.serverBlocks {
|
||||||
|
for _, addr := range sblock.keys {
|
||||||
|
if addr.Path != "" {
|
||||||
|
caddy.Log().Named("caddyfile").Warn("Using a path in a site address is deprecated; please use the 'handle' directive instead", zap.String("address", addr.String()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// sort server blocks by their keys; this is important because
|
// sort server blocks by their keys; this is important because
|
||||||
// only the first matching site should be evaluated, and we should
|
// only the first matching site should be evaluated, and we should
|
||||||
// attempt to match most specific site first (host and path), in
|
// attempt to match most specific site first (host and path), in
|
||||||
@@ -358,7 +460,11 @@ func (st *ServerType) serversFromPairings(
|
|||||||
// but I don't expect many blocks will have THAT many keys...
|
// but I don't expect many blocks will have THAT many keys...
|
||||||
var iLongestPath, jLongestPath string
|
var iLongestPath, jLongestPath string
|
||||||
var iLongestHost, jLongestHost string
|
var iLongestHost, jLongestHost string
|
||||||
|
var iWildcardHost, jWildcardHost bool
|
||||||
for _, addr := range p.serverBlocks[i].keys {
|
for _, addr := range p.serverBlocks[i].keys {
|
||||||
|
if strings.Contains(addr.Host, "*") || addr.Host == "" {
|
||||||
|
iWildcardHost = true
|
||||||
|
}
|
||||||
if specificity(addr.Host) > specificity(iLongestHost) {
|
if specificity(addr.Host) > specificity(iLongestHost) {
|
||||||
iLongestHost = addr.Host
|
iLongestHost = addr.Host
|
||||||
}
|
}
|
||||||
@@ -367,6 +473,9 @@ func (st *ServerType) serversFromPairings(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, addr := range p.serverBlocks[j].keys {
|
for _, addr := range p.serverBlocks[j].keys {
|
||||||
|
if strings.Contains(addr.Host, "*") || addr.Host == "" {
|
||||||
|
jWildcardHost = true
|
||||||
|
}
|
||||||
if specificity(addr.Host) > specificity(jLongestHost) {
|
if specificity(addr.Host) > specificity(jLongestHost) {
|
||||||
jLongestHost = addr.Host
|
jLongestHost = addr.Host
|
||||||
}
|
}
|
||||||
@@ -374,6 +483,20 @@ func (st *ServerType) serversFromPairings(
|
|||||||
jLongestPath = addr.Path
|
jLongestPath = addr.Path
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// catch-all blocks (blocks with no hostname) should always go
|
||||||
|
// last, even after blocks with wildcard hosts
|
||||||
|
if specificity(iLongestHost) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if specificity(jLongestHost) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if iWildcardHost != jWildcardHost {
|
||||||
|
// site blocks that have a key with a wildcard in the hostname
|
||||||
|
// must always be less specific than blocks without one; see
|
||||||
|
// https://github.com/caddyserver/caddy/issues/3410
|
||||||
|
return jWildcardHost && !iWildcardHost
|
||||||
|
}
|
||||||
if specificity(iLongestHost) == specificity(jLongestHost) {
|
if specificity(iLongestHost) == specificity(jLongestHost) {
|
||||||
return len(iLongestPath) > len(jLongestPath)
|
return len(iLongestPath) > len(jLongestPath)
|
||||||
}
|
}
|
||||||
@@ -381,7 +504,26 @@ func (st *ServerType) serversFromPairings(
|
|||||||
})
|
})
|
||||||
|
|
||||||
var hasCatchAllTLSConnPolicy, addressQualifiesForTLS bool
|
var hasCatchAllTLSConnPolicy, addressQualifiesForTLS bool
|
||||||
autoHTTPSWillAddConnPolicy := true
|
autoHTTPSWillAddConnPolicy := autoHTTPS != "off"
|
||||||
|
|
||||||
|
// if a catch-all server block (one which accepts all hostnames) exists in this pairing,
|
||||||
|
// we need to know that so that we can configure logs properly (see #3878)
|
||||||
|
var catchAllSblockExists bool
|
||||||
|
for _, sblock := range p.serverBlocks {
|
||||||
|
if len(sblock.hostsFromKeys(false)) == 0 {
|
||||||
|
catchAllSblockExists = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if needed, the ServerLogConfig is initialized beforehand so
|
||||||
|
// that all server blocks can populate it with data, even when not
|
||||||
|
// coming with a log directive
|
||||||
|
for _, sblock := range p.serverBlocks {
|
||||||
|
if len(sblock.pile["custom_log"]) != 0 {
|
||||||
|
srv.Logs = new(caddyhttp.ServerLogConfig)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// create a subroute for each site in the server block
|
// create a subroute for each site in the server block
|
||||||
for _, sblock := range p.serverBlocks {
|
for _, sblock := range p.serverBlocks {
|
||||||
@@ -392,6 +534,13 @@ func (st *ServerType) serversFromPairings(
|
|||||||
|
|
||||||
hosts := sblock.hostsFromKeys(false)
|
hosts := sblock.hostsFromKeys(false)
|
||||||
|
|
||||||
|
// emit warnings if user put unspecified IP addresses; they probably want the bind directive
|
||||||
|
for _, h := range hosts {
|
||||||
|
if h == "0.0.0.0" || h == "::" {
|
||||||
|
caddy.Log().Named("caddyfile").Warn("Site block has an unspecified IP address which only matches requests having that Host header; you probably want the 'bind' directive to configure the socket", zap.String("address", h))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// tls: connection policies
|
// tls: connection policies
|
||||||
if cpVals, ok := sblock.pile["tls.connection_policy"]; ok {
|
if cpVals, ok := sblock.pile["tls.connection_policy"]; ok {
|
||||||
// tls connection policies
|
// tls connection policies
|
||||||
@@ -413,24 +562,31 @@ func (st *ServerType) serversFromPairings(
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
cp.DefaultSNI = defaultSNI
|
cp.DefaultSNI = defaultSNI
|
||||||
hasCatchAllTLSConnPolicy = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
srv.TLSConnPolicies = append(srv.TLSConnPolicies, cp)
|
// only append this policy if it actually changes something
|
||||||
|
if !cp.SettingsEmpty() {
|
||||||
|
srv.TLSConnPolicies = append(srv.TLSConnPolicies, cp)
|
||||||
|
hasCatchAllTLSConnPolicy = len(hosts) == 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, addr := range sblock.keys {
|
for _, addr := range sblock.keys {
|
||||||
// exclude any hosts that were defined explicitly with "http://"
|
// if server only uses HTTP port, auto-HTTPS will not apply
|
||||||
// in the key from automated cert management (issue #2998)
|
if listenersUseAnyPortOtherThan(srv.Listen, httpPort) {
|
||||||
if addr.Scheme == "http" && addr.Host != "" {
|
// exclude any hosts that were defined explicitly with "http://"
|
||||||
if srv.AutoHTTPS == nil {
|
// in the key from automated cert management (issue #2998)
|
||||||
srv.AutoHTTPS = new(caddyhttp.AutoHTTPSConfig)
|
if addr.Scheme == "http" && addr.Host != "" {
|
||||||
}
|
if srv.AutoHTTPS == nil {
|
||||||
if !sliceContains(srv.AutoHTTPS.Skip, addr.Host) {
|
srv.AutoHTTPS = new(caddyhttp.AutoHTTPSConfig)
|
||||||
srv.AutoHTTPS.Skip = append(srv.AutoHTTPS.Skip, addr.Host)
|
}
|
||||||
|
if !sliceContains(srv.AutoHTTPS.Skip, addr.Host) {
|
||||||
|
srv.AutoHTTPS.Skip = append(srv.AutoHTTPS.Skip, addr.Host)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// we'll need to remember if the address qualifies for auto-HTTPS, so we
|
// we'll need to remember if the address qualifies for auto-HTTPS, so we
|
||||||
// can add a TLS conn policy if necessary
|
// can add a TLS conn policy if necessary
|
||||||
if addr.Scheme == "https" ||
|
if addr.Scheme == "https" ||
|
||||||
@@ -443,6 +599,20 @@ func (st *ServerType) serversFromPairings(
|
|||||||
(addr.Port == httpsPort || (addr.Port != httpPort && addr.Host != ""))
|
(addr.Port == httpsPort || (addr.Port != httpPort && addr.Host != ""))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Look for any config values that provide listener wrappers on the server block
|
||||||
|
for _, listenerConfig := range sblock.pile["listener_wrapper"] {
|
||||||
|
listenerWrapper, ok := listenerConfig.Value.(caddy.ListenerWrapper)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("config for a listener wrapper did not provide a value that implements caddy.ListenerWrapper")
|
||||||
|
}
|
||||||
|
jsonListenerWrapper := caddyconfig.JSONModuleObject(
|
||||||
|
listenerWrapper,
|
||||||
|
"wrapper",
|
||||||
|
listenerWrapper.(caddy.Module).CaddyModule().ID.Name(),
|
||||||
|
warnings)
|
||||||
|
srv.ListenerWrappersRaw = append(srv.ListenerWrappersRaw, jsonListenerWrapper)
|
||||||
|
}
|
||||||
|
|
||||||
// set up each handler directive, making sure to honor directive order
|
// set up each handler directive, making sure to honor directive order
|
||||||
dirRoutes := sblock.pile["route"]
|
dirRoutes := sblock.pile["route"]
|
||||||
siteSubroute, err := buildSubroute(dirRoutes, groupCounter)
|
siteSubroute, err := buildSubroute(dirRoutes, groupCounter)
|
||||||
@@ -469,9 +639,6 @@ func (st *ServerType) serversFromPairings(
|
|||||||
sblockLogHosts := sblock.hostsFromKeys(true)
|
sblockLogHosts := sblock.hostsFromKeys(true)
|
||||||
for _, cval := range sblock.pile["custom_log"] {
|
for _, cval := range sblock.pile["custom_log"] {
|
||||||
ncl := cval.Value.(namedCustomLog)
|
ncl := cval.Value.(namedCustomLog)
|
||||||
if srv.Logs == nil {
|
|
||||||
srv.Logs = new(caddyhttp.ServerLogConfig)
|
|
||||||
}
|
|
||||||
if sblock.hasHostCatchAllKey() {
|
if sblock.hasHostCatchAllKey() {
|
||||||
// all requests for hosts not able to be listed should use
|
// all requests for hosts not able to be listed should use
|
||||||
// this log because it's a catch-all-hosts server block
|
// this log because it's a catch-all-hosts server block
|
||||||
@@ -479,13 +646,13 @@ func (st *ServerType) serversFromPairings(
|
|||||||
} else {
|
} else {
|
||||||
// map each host to the user's desired logger name
|
// map each host to the user's desired logger name
|
||||||
for _, h := range sblockLogHosts {
|
for _, h := range sblockLogHosts {
|
||||||
// if the custom logger name is non-empty, add it to
|
// if the custom logger name is non-empty, add it to the map;
|
||||||
// the map; otherwise, only map to an empty logger
|
// otherwise, only map to an empty logger name if this or
|
||||||
// name if the server block has a catch-all host (in
|
// another site block on this server has a catch-all host (in
|
||||||
// which case only requests with mapped hostnames will
|
// which case only requests with mapped hostnames will be
|
||||||
// be access-logged, so it'll be necessary to add them
|
// access-logged, so it'll be necessary to add them to the
|
||||||
// to the map even if they use default logger)
|
// map even if they use default logger)
|
||||||
if ncl.name != "" || len(hosts) == 0 {
|
if ncl.name != "" || catchAllSblockExists {
|
||||||
if srv.Logs.LoggerNames == nil {
|
if srv.Logs.LoggerNames == nil {
|
||||||
srv.Logs.LoggerNames = make(map[string]string)
|
srv.Logs.LoggerNames = make(map[string]string)
|
||||||
}
|
}
|
||||||
@@ -542,6 +709,11 @@ func (st *ServerType) serversFromPairings(
|
|||||||
servers[fmt.Sprintf("srv%d", i)] = srv
|
servers[fmt.Sprintf("srv%d", i)] = srv
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err := applyServerOptions(servers, options, warnings)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return servers, nil
|
return servers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -603,9 +775,15 @@ func detectConflictingSchemes(srv *caddyhttp.Server, serverBlocks []serverBlock,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// consolidateConnPolicies removes empty TLS connection policies and combines
|
// consolidateConnPolicies sorts any catch-all policy to the end, removes empty TLS connection
|
||||||
// equivalent ones for a cleaner overall output.
|
// policies, and combines equivalent ones for a cleaner overall output.
|
||||||
func consolidateConnPolicies(cps caddytls.ConnectionPolicies) (caddytls.ConnectionPolicies, error) {
|
func consolidateConnPolicies(cps caddytls.ConnectionPolicies) (caddytls.ConnectionPolicies, error) {
|
||||||
|
// catch-all policies (those without any matcher) should be at the
|
||||||
|
// end, otherwise it nullifies any more specific policies
|
||||||
|
sort.SliceStable(cps, func(i, j int) bool {
|
||||||
|
return cps[j].MatchersRaw == nil && cps[i].MatchersRaw != nil
|
||||||
|
})
|
||||||
|
|
||||||
for i := 0; i < len(cps); i++ {
|
for i := 0; i < len(cps); i++ {
|
||||||
// compare it to the others
|
// compare it to the others
|
||||||
for j := 0; j < len(cps); j++ {
|
for j := 0; j < len(cps); j++ {
|
||||||
@@ -798,7 +976,18 @@ func buildSubroute(routes []ConfigValue, groupCounter counter) (*caddyhttp.Subro
|
|||||||
// root directives would overwrite previously-matched ones; they should not cascade
|
// root directives would overwrite previously-matched ones; they should not cascade
|
||||||
"root": {},
|
"root": {},
|
||||||
}
|
}
|
||||||
for meDir, info := range mutuallyExclusiveDirs {
|
|
||||||
|
// we need to deterministically loop over each of these directives
|
||||||
|
// in order to keep the group numbers consistent
|
||||||
|
keys := make([]string, 0, len(mutuallyExclusiveDirs))
|
||||||
|
for k := range mutuallyExclusiveDirs {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
|
||||||
|
for _, meDir := range keys {
|
||||||
|
info := mutuallyExclusiveDirs[meDir]
|
||||||
|
|
||||||
// see how many instances of the directive there are
|
// see how many instances of the directive there are
|
||||||
for _, r := range routes {
|
for _, r := range routes {
|
||||||
if r.directive == meDir {
|
if r.directive == meDir {
|
||||||
@@ -859,6 +1048,19 @@ func buildSubroute(routes []ConfigValue, groupCounter counter) (*caddyhttp.Subro
|
|||||||
return subroute, nil
|
return subroute, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// normalizeDirectiveName ensures directives that should be sorted
|
||||||
|
// at the same level are named the same before sorting happens.
|
||||||
|
func normalizeDirectiveName(directive string) string {
|
||||||
|
// As a special case, we want "handle_path" to be sorted
|
||||||
|
// at the same level as "handle", so we force them to use
|
||||||
|
// the same directive name after their parsing is complete.
|
||||||
|
// See https://github.com/caddyserver/caddy/issues/3675#issuecomment-678042377
|
||||||
|
if directive == "handle_path" {
|
||||||
|
directive = "handle"
|
||||||
|
}
|
||||||
|
return directive
|
||||||
|
}
|
||||||
|
|
||||||
// consolidateRoutes combines routes with the same properties
|
// consolidateRoutes combines routes with the same properties
|
||||||
// (same matchers, same Terminal and Group settings) for a
|
// (same matchers, same Terminal and Group settings) for a
|
||||||
// cleaner overall output.
|
// cleaner overall output.
|
||||||
@@ -1001,7 +1203,7 @@ func parseMatcherDefinitions(d *caddyfile.Dispenser, matchers map[string]caddy.M
|
|||||||
// handle more than one segment); otherwise, we'd overwrite other
|
// handle more than one segment); otherwise, we'd overwrite other
|
||||||
// instances of the matcher in this set
|
// instances of the matcher in this set
|
||||||
tokensByMatcherName := make(map[string][]caddyfile.Token)
|
tokensByMatcherName := make(map[string][]caddyfile.Token)
|
||||||
for nesting := d.Nesting(); d.NextBlock(nesting); {
|
for nesting := d.Nesting(); d.NextArg() || d.NextBlock(nesting); {
|
||||||
matcherName := d.Val()
|
matcherName := d.Val()
|
||||||
tokensByMatcherName[matcherName] = append(tokensByMatcherName[matcherName], d.NextSegment()...)
|
tokensByMatcherName[matcherName] = append(tokensByMatcherName[matcherName], d.NextSegment()...)
|
||||||
}
|
}
|
||||||
@@ -1040,6 +1242,58 @@ func encodeMatcherSet(matchers map[string]caddyhttp.RequestMatcher) (caddy.Modul
|
|||||||
return msEncoded, nil
|
return msEncoded, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// placeholderShorthands returns a slice of old-new string pairs,
|
||||||
|
// where the left of the pair is a placeholder shorthand that may
|
||||||
|
// be used in the Caddyfile, and the right is the replacement.
|
||||||
|
func placeholderShorthands() []string {
|
||||||
|
return []string{
|
||||||
|
"{dir}", "{http.request.uri.path.dir}",
|
||||||
|
"{file}", "{http.request.uri.path.file}",
|
||||||
|
"{host}", "{http.request.host}",
|
||||||
|
"{hostport}", "{http.request.hostport}",
|
||||||
|
"{port}", "{http.request.port}",
|
||||||
|
"{method}", "{http.request.method}",
|
||||||
|
"{path}", "{http.request.uri.path}",
|
||||||
|
"{query}", "{http.request.uri.query}",
|
||||||
|
"{remote}", "{http.request.remote}",
|
||||||
|
"{remote_host}", "{http.request.remote.host}",
|
||||||
|
"{remote_port}", "{http.request.remote.port}",
|
||||||
|
"{scheme}", "{http.request.scheme}",
|
||||||
|
"{uri}", "{http.request.uri}",
|
||||||
|
"{tls_cipher}", "{http.request.tls.cipher_suite}",
|
||||||
|
"{tls_version}", "{http.request.tls.version}",
|
||||||
|
"{tls_client_fingerprint}", "{http.request.tls.client.fingerprint}",
|
||||||
|
"{tls_client_issuer}", "{http.request.tls.client.issuer}",
|
||||||
|
"{tls_client_serial}", "{http.request.tls.client.serial}",
|
||||||
|
"{tls_client_subject}", "{http.request.tls.client.subject}",
|
||||||
|
"{tls_client_certificate_pem}", "{http.request.tls.client.certificate_pem}",
|
||||||
|
"{tls_client_certificate_der_base64}", "{http.request.tls.client.certificate_der_base64}",
|
||||||
|
"{upstream_hostport}", "{http.reverse_proxy.upstream.hostport}",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WasReplacedPlaceholderShorthand checks if a token string was
|
||||||
|
// likely a replaced shorthand of the known Caddyfile placeholder
|
||||||
|
// replacement outputs. Useful to prevent some user-defined map
|
||||||
|
// output destinations from overlapping with one of the
|
||||||
|
// predefined shorthands.
|
||||||
|
func WasReplacedPlaceholderShorthand(token string) string {
|
||||||
|
prev := ""
|
||||||
|
for i, item := range placeholderShorthands() {
|
||||||
|
// only look at every 2nd item, which is the replacement
|
||||||
|
if i%2 == 0 {
|
||||||
|
prev = item
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.Trim(token, "{}") == strings.Trim(item, "{}") {
|
||||||
|
// we return the original shorthand so it
|
||||||
|
// can be used for an error message
|
||||||
|
return prev
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
// tryInt tries to convert val to an integer. If it fails,
|
// tryInt tries to convert val to an integer. If it fails,
|
||||||
// it downgrades the error to a warning and returns 0.
|
// it downgrades the error to a warning and returns 0.
|
||||||
func tryInt(val interface{}, warnings *[]caddyconfig.Warning) int {
|
func tryInt(val interface{}, warnings *[]caddyconfig.Warning) int {
|
||||||
@@ -1058,6 +1312,14 @@ func tryString(val interface{}, warnings *[]caddyconfig.Warning) string {
|
|||||||
return stringVal
|
return stringVal
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func tryDuration(val interface{}, warnings *[]caddyconfig.Warning) caddy.Duration {
|
||||||
|
durationVal, ok := val.(caddy.Duration)
|
||||||
|
if val != nil && !ok && warnings != nil {
|
||||||
|
*warnings = append(*warnings, caddyconfig.Warning{Message: "not a duration type"})
|
||||||
|
}
|
||||||
|
return durationVal
|
||||||
|
}
|
||||||
|
|
||||||
// sliceContains returns true if needle is in haystack.
|
// sliceContains returns true if needle is in haystack.
|
||||||
func sliceContains(haystack []string, needle string) bool {
|
func sliceContains(haystack []string, needle string) bool {
|
||||||
for _, s := range haystack {
|
for _, s := range haystack {
|
||||||
@@ -1068,6 +1330,26 @@ func sliceContains(haystack []string, needle string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// listenersUseAnyPortOtherThan returns true if there are any
|
||||||
|
// listeners in addresses that use a port which is not otherPort.
|
||||||
|
// Mostly borrowed from unexported method in caddyhttp package.
|
||||||
|
func listenersUseAnyPortOtherThan(addresses []string, otherPort string) bool {
|
||||||
|
otherPortInt, err := strconv.Atoi(otherPort)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, lnAddr := range addresses {
|
||||||
|
laddrs, err := caddy.ParseNetworkAddress(lnAddr)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if uint(otherPortInt) > laddrs.EndPort || uint(otherPortInt) < laddrs.StartPort {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// specificity returns len(s) minus any wildcards (*) and
|
// specificity returns len(s) minus any wildcards (*) and
|
||||||
// placeholders ({...}). Basically, it's a length count
|
// placeholders ({...}). Basically, it's a length count
|
||||||
// that penalizes the use of wildcards and placeholders.
|
// that penalizes the use of wildcards and placeholders.
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
func TestMatcherSyntax(t *testing.T) {
|
func TestMatcherSyntax(t *testing.T) {
|
||||||
for i, tc := range []struct {
|
for i, tc := range []struct {
|
||||||
input string
|
input string
|
||||||
expectWarn bool
|
|
||||||
expectError bool
|
expectError bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@@ -18,7 +17,6 @@ func TestMatcherSyntax(t *testing.T) {
|
|||||||
query showdebug=1
|
query showdebug=1
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
expectWarn: false,
|
|
||||||
expectError: false,
|
expectError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -27,7 +25,6 @@ func TestMatcherSyntax(t *testing.T) {
|
|||||||
query bad format
|
query bad format
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
expectWarn: false,
|
|
||||||
expectError: true,
|
expectError: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -38,7 +35,6 @@ func TestMatcherSyntax(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
expectWarn: false,
|
|
||||||
expectError: false,
|
expectError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -47,21 +43,29 @@ func TestMatcherSyntax(t *testing.T) {
|
|||||||
not path /somepath*
|
not path /somepath*
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
expectWarn: false,
|
|
||||||
expectError: false,
|
expectError: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
input: `http://localhost
|
||||||
|
@debug not path /somepath*
|
||||||
|
`,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `@matcher {
|
||||||
|
path /matcher-not-allowed/outside-of-site-block/*
|
||||||
|
}
|
||||||
|
http://localhost
|
||||||
|
`,
|
||||||
|
expectError: true,
|
||||||
|
},
|
||||||
} {
|
} {
|
||||||
|
|
||||||
adapter := caddyfile.Adapter{
|
adapter := caddyfile.Adapter{
|
||||||
ServerType: ServerType{},
|
ServerType: ServerType{},
|
||||||
}
|
}
|
||||||
|
|
||||||
_, warnings, err := adapter.Adapt([]byte(tc.input), nil)
|
_, _, err := adapter.Adapt([]byte(tc.input), nil)
|
||||||
|
|
||||||
if len(warnings) > 0 != tc.expectWarn {
|
|
||||||
t.Errorf("Test %d warning expectation failed Expected: %v, got %v", i, tc.expectWarn, warnings)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil != tc.expectError {
|
if err != nil != tc.expectError {
|
||||||
t.Errorf("Test %d error expectation failed Expected: %v, got %s", i, tc.expectError, err)
|
t.Errorf("Test %d error expectation failed Expected: %v, got %s", i, tc.expectError, err)
|
||||||
@@ -103,7 +107,6 @@ func TestSpecificity(t *testing.T) {
|
|||||||
func TestGlobalOptions(t *testing.T) {
|
func TestGlobalOptions(t *testing.T) {
|
||||||
for i, tc := range []struct {
|
for i, tc := range []struct {
|
||||||
input string
|
input string
|
||||||
expectWarn bool
|
|
||||||
expectError bool
|
expectError bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@@ -113,7 +116,6 @@ func TestGlobalOptions(t *testing.T) {
|
|||||||
}
|
}
|
||||||
:80
|
:80
|
||||||
`,
|
`,
|
||||||
expectWarn: false,
|
|
||||||
expectError: false,
|
expectError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -123,7 +125,6 @@ func TestGlobalOptions(t *testing.T) {
|
|||||||
}
|
}
|
||||||
:80
|
:80
|
||||||
`,
|
`,
|
||||||
expectWarn: false,
|
|
||||||
expectError: false,
|
expectError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -133,7 +134,6 @@ func TestGlobalOptions(t *testing.T) {
|
|||||||
}
|
}
|
||||||
:80
|
:80
|
||||||
`,
|
`,
|
||||||
expectWarn: false,
|
|
||||||
expectError: false,
|
expectError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -145,7 +145,54 @@ func TestGlobalOptions(t *testing.T) {
|
|||||||
}
|
}
|
||||||
:80
|
:80
|
||||||
`,
|
`,
|
||||||
expectWarn: false,
|
expectError: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `
|
||||||
|
{
|
||||||
|
admin {
|
||||||
|
enforce_origin
|
||||||
|
origins 192.168.1.1:2020 127.0.0.1:2020
|
||||||
|
}
|
||||||
|
}
|
||||||
|
:80
|
||||||
|
`,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `
|
||||||
|
{
|
||||||
|
admin 127.0.0.1:2020 {
|
||||||
|
enforce_origin
|
||||||
|
origins 192.168.1.1:2020 127.0.0.1:2020
|
||||||
|
}
|
||||||
|
}
|
||||||
|
:80
|
||||||
|
`,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `
|
||||||
|
{
|
||||||
|
admin 192.168.1.1:2020 127.0.0.1:2020 {
|
||||||
|
enforce_origin
|
||||||
|
origins 192.168.1.1:2020 127.0.0.1:2020
|
||||||
|
}
|
||||||
|
}
|
||||||
|
:80
|
||||||
|
`,
|
||||||
|
expectError: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `
|
||||||
|
{
|
||||||
|
admin off {
|
||||||
|
enforce_origin
|
||||||
|
origins 192.168.1.1:2020 127.0.0.1:2020
|
||||||
|
}
|
||||||
|
}
|
||||||
|
:80
|
||||||
|
`,
|
||||||
expectError: true,
|
expectError: true,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
@@ -154,12 +201,7 @@ func TestGlobalOptions(t *testing.T) {
|
|||||||
ServerType: ServerType{},
|
ServerType: ServerType{},
|
||||||
}
|
}
|
||||||
|
|
||||||
_, warnings, err := adapter.Adapt([]byte(tc.input), nil)
|
_, _, err := adapter.Adapt([]byte(tc.input), nil)
|
||||||
|
|
||||||
if len(warnings) > 0 != tc.expectWarn {
|
|
||||||
t.Errorf("Test %d warning expectation failed Expected: %v, got %v", i, tc.expectWarn, warnings)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil != tc.expectError {
|
if err != nil != tc.expectError {
|
||||||
t.Errorf("Test %d error expectation failed Expected: %v, got %s", i, tc.expectError, err)
|
t.Errorf("Test %d error expectation failed Expected: %v, got %s", i, tc.expectError, err)
|
||||||
|
|||||||
@@ -16,14 +16,47 @@ package httpcaddyfile
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/caddyserver/caddy/v2"
|
"github.com/caddyserver/caddy/v2"
|
||||||
|
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||||
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
||||||
|
"github.com/caddyserver/certmagic"
|
||||||
|
"github.com/mholt/acmez/acme"
|
||||||
)
|
)
|
||||||
|
|
||||||
func parseOptHTTPPort(d *caddyfile.Dispenser) (int, error) {
|
func init() {
|
||||||
|
RegisterGlobalOption("debug", parseOptTrue)
|
||||||
|
RegisterGlobalOption("http_port", parseOptHTTPPort)
|
||||||
|
RegisterGlobalOption("https_port", parseOptHTTPSPort)
|
||||||
|
RegisterGlobalOption("default_bind", parseOptStringList)
|
||||||
|
RegisterGlobalOption("grace_period", parseOptDuration)
|
||||||
|
RegisterGlobalOption("default_sni", parseOptSingleString)
|
||||||
|
RegisterGlobalOption("order", parseOptOrder)
|
||||||
|
RegisterGlobalOption("storage", parseOptStorage)
|
||||||
|
RegisterGlobalOption("storage_clean_interval", parseOptDuration)
|
||||||
|
RegisterGlobalOption("renew_interval", parseOptDuration)
|
||||||
|
RegisterGlobalOption("acme_ca", parseOptSingleString)
|
||||||
|
RegisterGlobalOption("acme_ca_root", parseOptSingleString)
|
||||||
|
RegisterGlobalOption("acme_dns", parseOptACMEDNS)
|
||||||
|
RegisterGlobalOption("acme_eab", parseOptACMEEAB)
|
||||||
|
RegisterGlobalOption("cert_issuer", parseOptCertIssuer)
|
||||||
|
RegisterGlobalOption("skip_install_trust", parseOptTrue)
|
||||||
|
RegisterGlobalOption("email", parseOptSingleString)
|
||||||
|
RegisterGlobalOption("admin", parseOptAdmin)
|
||||||
|
RegisterGlobalOption("on_demand_tls", parseOptOnDemand)
|
||||||
|
RegisterGlobalOption("local_certs", parseOptTrue)
|
||||||
|
RegisterGlobalOption("key_type", parseOptSingleString)
|
||||||
|
RegisterGlobalOption("auto_https", parseOptAutoHTTPS)
|
||||||
|
RegisterGlobalOption("servers", parseServerOptions)
|
||||||
|
RegisterGlobalOption("ocsp_stapling", parseOCSPStaplingOptions)
|
||||||
|
RegisterGlobalOption("log", parseLogOptions)
|
||||||
|
RegisterGlobalOption("preferred_chains", parseOptPreferredChains)
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseOptTrue(d *caddyfile.Dispenser, _ interface{}) (interface{}, error) { return true, nil }
|
||||||
|
|
||||||
|
func parseOptHTTPPort(d *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
|
||||||
var httpPort int
|
var httpPort int
|
||||||
for d.Next() {
|
for d.Next() {
|
||||||
var httpPortStr string
|
var httpPortStr string
|
||||||
@@ -39,7 +72,7 @@ func parseOptHTTPPort(d *caddyfile.Dispenser) (int, error) {
|
|||||||
return httpPort, nil
|
return httpPort, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseOptHTTPSPort(d *caddyfile.Dispenser) (int, error) {
|
func parseOptHTTPSPort(d *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
|
||||||
var httpsPort int
|
var httpsPort int
|
||||||
for d.Next() {
|
for d.Next() {
|
||||||
var httpsPortStr string
|
var httpsPortStr string
|
||||||
@@ -55,11 +88,7 @@ func parseOptHTTPSPort(d *caddyfile.Dispenser) (int, error) {
|
|||||||
return httpsPort, nil
|
return httpsPort, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseOptExperimentalHTTP3(d *caddyfile.Dispenser) (bool, error) {
|
func parseOptOrder(d *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseOptOrder(d *caddyfile.Dispenser) ([]string, error) {
|
|
||||||
newOrder := directiveOrder
|
newOrder := directiveOrder
|
||||||
|
|
||||||
for d.Next() {
|
for d.Next() {
|
||||||
@@ -135,34 +164,110 @@ func parseOptOrder(d *caddyfile.Dispenser) ([]string, error) {
|
|||||||
return newOrder, nil
|
return newOrder, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseOptStorage(d *caddyfile.Dispenser) (caddy.StorageConverter, error) {
|
func parseOptStorage(d *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
|
||||||
if !d.Next() { // consume option name
|
if !d.Next() { // consume option name
|
||||||
return nil, d.ArgErr()
|
return nil, d.ArgErr()
|
||||||
}
|
}
|
||||||
if !d.Next() { // get storage module name
|
if !d.Next() { // get storage module name
|
||||||
return nil, d.ArgErr()
|
return nil, d.ArgErr()
|
||||||
}
|
}
|
||||||
modName := d.Val()
|
modID := "caddy.storage." + d.Val()
|
||||||
mod, err := caddy.GetModule("caddy.storage." + modName)
|
unm, err := caddyfile.UnmarshalModule(d, modID)
|
||||||
if err != nil {
|
|
||||||
return nil, d.Errf("getting storage module '%s': %v", modName, err)
|
|
||||||
}
|
|
||||||
unm, ok := mod.New().(caddyfile.Unmarshaler)
|
|
||||||
if !ok {
|
|
||||||
return nil, d.Errf("storage module '%s' is not a Caddyfile unmarshaler", mod.ID)
|
|
||||||
}
|
|
||||||
err = unm.UnmarshalCaddyfile(d.NewFromNextSegment())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
storage, ok := unm.(caddy.StorageConverter)
|
storage, ok := unm.(caddy.StorageConverter)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, d.Errf("module %s is not a StorageConverter", mod.ID)
|
return nil, d.Errf("module %s is not a caddy.StorageConverter", modID)
|
||||||
}
|
}
|
||||||
return storage, nil
|
return storage, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseOptSingleString(d *caddyfile.Dispenser) (string, error) {
|
func parseOptDuration(d *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
|
||||||
|
if !d.Next() { // consume option name
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
if !d.Next() { // get duration value
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
dur, err := caddy.ParseDuration(d.Val())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return caddy.Duration(dur), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseOptACMEDNS(d *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
|
||||||
|
if !d.Next() { // consume option name
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
if !d.Next() { // get DNS module name
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
modID := "dns.providers." + d.Val()
|
||||||
|
unm, err := caddyfile.UnmarshalModule(d, modID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
prov, ok := unm.(certmagic.ACMEDNSProvider)
|
||||||
|
if !ok {
|
||||||
|
return nil, d.Errf("module %s (%T) is not a certmagic.ACMEDNSProvider", modID, unm)
|
||||||
|
}
|
||||||
|
return prov, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseOptACMEEAB(d *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
|
||||||
|
eab := new(acme.EAB)
|
||||||
|
for d.Next() {
|
||||||
|
if d.NextArg() {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
for nesting := d.Nesting(); d.NextBlock(nesting); {
|
||||||
|
switch d.Val() {
|
||||||
|
case "key_id":
|
||||||
|
if !d.NextArg() {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
eab.KeyID = d.Val()
|
||||||
|
|
||||||
|
case "mac_key":
|
||||||
|
if !d.NextArg() {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
eab.MACKey = d.Val()
|
||||||
|
|
||||||
|
default:
|
||||||
|
return nil, d.Errf("unrecognized parameter '%s'", d.Val())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return eab, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseOptCertIssuer(d *caddyfile.Dispenser, existing interface{}) (interface{}, error) {
|
||||||
|
var issuers []certmagic.Issuer
|
||||||
|
if existing != nil {
|
||||||
|
issuers = existing.([]certmagic.Issuer)
|
||||||
|
}
|
||||||
|
for d.Next() { // consume option name
|
||||||
|
if !d.Next() { // get issuer module name
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
modID := "tls.issuance." + d.Val()
|
||||||
|
unm, err := caddyfile.UnmarshalModule(d, modID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
iss, ok := unm.(certmagic.Issuer)
|
||||||
|
if !ok {
|
||||||
|
return nil, d.Errf("module %s (%T) is not a certmagic.Issuer", modID, unm)
|
||||||
|
}
|
||||||
|
issuers = append(issuers, iss)
|
||||||
|
}
|
||||||
|
return issuers, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseOptSingleString(d *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
|
||||||
d.Next() // consume parameter name
|
d.Next() // consume parameter name
|
||||||
if !d.Next() {
|
if !d.Next() {
|
||||||
return "", d.ArgErr()
|
return "", d.ArgErr()
|
||||||
@@ -174,21 +279,52 @@ func parseOptSingleString(d *caddyfile.Dispenser) (string, error) {
|
|||||||
return val, nil
|
return val, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseOptAdmin(d *caddyfile.Dispenser) (string, error) {
|
func parseOptStringList(d *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
|
||||||
if d.Next() {
|
d.Next() // consume parameter name
|
||||||
var listenAddress string
|
val := d.RemainingArgs()
|
||||||
if !d.AllArgs(&listenAddress) {
|
if len(val) == 0 {
|
||||||
return "", d.ArgErr()
|
return "", d.ArgErr()
|
||||||
}
|
|
||||||
if listenAddress == "" {
|
|
||||||
listenAddress = caddy.DefaultAdminListen
|
|
||||||
}
|
|
||||||
return listenAddress, nil
|
|
||||||
}
|
}
|
||||||
return "", nil
|
return val, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseOptOnDemand(d *caddyfile.Dispenser) (*caddytls.OnDemandConfig, error) {
|
func parseOptAdmin(d *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
|
||||||
|
adminCfg := new(caddy.AdminConfig)
|
||||||
|
for d.Next() {
|
||||||
|
if d.NextArg() {
|
||||||
|
listenAddress := d.Val()
|
||||||
|
if listenAddress == "off" {
|
||||||
|
adminCfg.Disabled = true
|
||||||
|
if d.Next() { // Do not accept any remaining options including block
|
||||||
|
return nil, d.Err("No more option is allowed after turning off admin config")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
adminCfg.Listen = listenAddress
|
||||||
|
if d.NextArg() { // At most 1 arg is allowed
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for nesting := d.Nesting(); d.NextBlock(nesting); {
|
||||||
|
switch d.Val() {
|
||||||
|
case "enforce_origin":
|
||||||
|
adminCfg.EnforceOrigin = true
|
||||||
|
|
||||||
|
case "origins":
|
||||||
|
adminCfg.Origins = d.RemainingArgs()
|
||||||
|
|
||||||
|
default:
|
||||||
|
return nil, d.Errf("unrecognized parameter '%s'", d.Val())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if adminCfg.Listen == "" && !adminCfg.Disabled {
|
||||||
|
adminCfg.Listen = caddy.DefaultAdminListen
|
||||||
|
}
|
||||||
|
return adminCfg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseOptOnDemand(d *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
|
||||||
var ond *caddytls.OnDemandConfig
|
var ond *caddytls.OnDemandConfig
|
||||||
for d.Next() {
|
for d.Next() {
|
||||||
if d.NextArg() {
|
if d.NextArg() {
|
||||||
@@ -209,7 +345,7 @@ func parseOptOnDemand(d *caddyfile.Dispenser) (*caddytls.OnDemandConfig, error)
|
|||||||
if !d.NextArg() {
|
if !d.NextArg() {
|
||||||
return nil, d.ArgErr()
|
return nil, d.ArgErr()
|
||||||
}
|
}
|
||||||
dur, err := time.ParseDuration(d.Val())
|
dur, err := caddy.ParseDuration(d.Val())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -247,3 +383,89 @@ func parseOptOnDemand(d *caddyfile.Dispenser) (*caddytls.OnDemandConfig, error)
|
|||||||
}
|
}
|
||||||
return ond, nil
|
return ond, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseOptAutoHTTPS(d *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
|
||||||
|
d.Next() // consume parameter name
|
||||||
|
if !d.Next() {
|
||||||
|
return "", d.ArgErr()
|
||||||
|
}
|
||||||
|
val := d.Val()
|
||||||
|
if d.Next() {
|
||||||
|
return "", d.ArgErr()
|
||||||
|
}
|
||||||
|
if val != "off" && val != "disable_redirects" && val != "disable_certs" && val != "ignore_loaded_certs" {
|
||||||
|
return "", d.Errf("auto_https must be one of 'off', 'disable_redirects', 'disable_certs', or 'ignore_loaded_certs'")
|
||||||
|
}
|
||||||
|
return val, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseServerOptions(d *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
|
||||||
|
return unmarshalCaddyfileServerOptions(d)
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseOCSPStaplingOptions(d *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
|
||||||
|
d.Next() // consume option name
|
||||||
|
var val string
|
||||||
|
if !d.AllArgs(&val) {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
if val != "off" {
|
||||||
|
return nil, d.Errf("invalid argument '%s'", val)
|
||||||
|
}
|
||||||
|
return certmagic.OCSPConfig{
|
||||||
|
DisableStapling: val == "off",
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseLogOptions parses the global log option. Syntax:
|
||||||
|
//
|
||||||
|
// log [name] {
|
||||||
|
// output <writer_module> ...
|
||||||
|
// format <encoder_module> ...
|
||||||
|
// level <level>
|
||||||
|
// include <namespaces...>
|
||||||
|
// exclude <namespaces...>
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// When the name argument is unspecified, this directive modifies the default
|
||||||
|
// logger.
|
||||||
|
//
|
||||||
|
func parseLogOptions(d *caddyfile.Dispenser, existingVal interface{}) (interface{}, error) {
|
||||||
|
currentNames := make(map[string]struct{})
|
||||||
|
if existingVal != nil {
|
||||||
|
innerVals, ok := existingVal.([]ConfigValue)
|
||||||
|
if !ok {
|
||||||
|
return nil, d.Errf("existing log values of unexpected type: %T", existingVal)
|
||||||
|
}
|
||||||
|
for _, rawVal := range innerVals {
|
||||||
|
val, ok := rawVal.Value.(namedCustomLog)
|
||||||
|
if !ok {
|
||||||
|
return nil, d.Errf("existing log value of unexpected type: %T", existingVal)
|
||||||
|
}
|
||||||
|
currentNames[val.name] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var warnings []caddyconfig.Warning
|
||||||
|
// Call out the same parser that handles server-specific log configuration.
|
||||||
|
configValues, err := parseLogHelper(
|
||||||
|
Helper{
|
||||||
|
Dispenser: d,
|
||||||
|
warnings: &warnings,
|
||||||
|
},
|
||||||
|
currentNames,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(warnings) > 0 {
|
||||||
|
return nil, d.Errf("warnings found in parsing global log options: %+v", warnings)
|
||||||
|
}
|
||||||
|
|
||||||
|
return configValues, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseOptPreferredChains(d *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
|
||||||
|
d.Next()
|
||||||
|
return caddytls.ParseCaddyfilePreferredChainsOptions(d)
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package httpcaddyfile
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||||
|
_ "github.com/caddyserver/caddy/v2/modules/logging"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGlobalLogOptionSyntax(t *testing.T) {
|
||||||
|
for i, tc := range []struct {
|
||||||
|
input string
|
||||||
|
output string
|
||||||
|
expectError bool
|
||||||
|
}{
|
||||||
|
// NOTE: Additional test cases of successful Caddyfile parsing
|
||||||
|
// are present in: caddytest/integration/caddyfile_adapt/
|
||||||
|
{
|
||||||
|
input: `{
|
||||||
|
log default
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
output: `{}`,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `{
|
||||||
|
log example {
|
||||||
|
output file foo.log
|
||||||
|
}
|
||||||
|
log example {
|
||||||
|
format json
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
expectError: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: `{
|
||||||
|
log example /foo {
|
||||||
|
output file foo.log
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
expectError: true,
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
|
||||||
|
adapter := caddyfile.Adapter{
|
||||||
|
ServerType: ServerType{},
|
||||||
|
}
|
||||||
|
|
||||||
|
out, _, err := adapter.Adapt([]byte(tc.input), nil)
|
||||||
|
|
||||||
|
if err != nil != tc.expectError {
|
||||||
|
t.Errorf("Test %d error expectation failed Expected: %v, got %v", i, tc.expectError, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if string(out) != tc.output {
|
||||||
|
t.Errorf("Test %d error output mismatch Expected: %s, got %s", i, tc.output, out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,219 @@
|
|||||||
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package httpcaddyfile
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||||
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||||
|
"github.com/caddyserver/caddy/v2/modules/caddypki"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RegisterGlobalOption("pki", parsePKIApp)
|
||||||
|
}
|
||||||
|
|
||||||
|
// parsePKIApp parses the global log option. Syntax:
|
||||||
|
//
|
||||||
|
// pki {
|
||||||
|
// ca [<id>] {
|
||||||
|
// name <name>
|
||||||
|
// root_cn <name>
|
||||||
|
// intermediate_cn <name>
|
||||||
|
// root {
|
||||||
|
// cert <path>
|
||||||
|
// key <path>
|
||||||
|
// format <format>
|
||||||
|
// }
|
||||||
|
// intermediate {
|
||||||
|
// cert <path>
|
||||||
|
// key <path>
|
||||||
|
// format <format>
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// When the CA ID is unspecified, 'local' is assumed.
|
||||||
|
//
|
||||||
|
func parsePKIApp(d *caddyfile.Dispenser, existingVal interface{}) (interface{}, error) {
|
||||||
|
pki := &caddypki.PKI{CAs: make(map[string]*caddypki.CA)}
|
||||||
|
|
||||||
|
for d.Next() {
|
||||||
|
for nesting := d.Nesting(); d.NextBlock(nesting); {
|
||||||
|
switch d.Val() {
|
||||||
|
case "ca":
|
||||||
|
pkiCa := new(caddypki.CA)
|
||||||
|
if d.NextArg() {
|
||||||
|
pkiCa.ID = d.Val()
|
||||||
|
if d.NextArg() {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if pkiCa.ID == "" {
|
||||||
|
pkiCa.ID = caddypki.DefaultCAID
|
||||||
|
}
|
||||||
|
|
||||||
|
for nesting := d.Nesting(); d.NextBlock(nesting); {
|
||||||
|
switch d.Val() {
|
||||||
|
case "name":
|
||||||
|
if !d.NextArg() {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
pkiCa.Name = d.Val()
|
||||||
|
|
||||||
|
case "root_cn":
|
||||||
|
if !d.NextArg() {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
pkiCa.RootCommonName = d.Val()
|
||||||
|
|
||||||
|
case "intermediate_cn":
|
||||||
|
if !d.NextArg() {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
pkiCa.IntermediateCommonName = d.Val()
|
||||||
|
|
||||||
|
case "root":
|
||||||
|
if pkiCa.Root == nil {
|
||||||
|
pkiCa.Root = new(caddypki.KeyPair)
|
||||||
|
}
|
||||||
|
for nesting := d.Nesting(); d.NextBlock(nesting); {
|
||||||
|
switch d.Val() {
|
||||||
|
case "cert":
|
||||||
|
if !d.NextArg() {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
pkiCa.Root.Certificate = d.Val()
|
||||||
|
|
||||||
|
case "key":
|
||||||
|
if !d.NextArg() {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
pkiCa.Root.PrivateKey = d.Val()
|
||||||
|
|
||||||
|
case "format":
|
||||||
|
if !d.NextArg() {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
pkiCa.Root.Format = d.Val()
|
||||||
|
|
||||||
|
default:
|
||||||
|
return nil, d.Errf("unrecognized pki ca root option '%s'", d.Val())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case "intermediate":
|
||||||
|
if pkiCa.Intermediate == nil {
|
||||||
|
pkiCa.Intermediate = new(caddypki.KeyPair)
|
||||||
|
}
|
||||||
|
for nesting := d.Nesting(); d.NextBlock(nesting); {
|
||||||
|
switch d.Val() {
|
||||||
|
case "cert":
|
||||||
|
if !d.NextArg() {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
pkiCa.Intermediate.Certificate = d.Val()
|
||||||
|
|
||||||
|
case "key":
|
||||||
|
if !d.NextArg() {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
pkiCa.Intermediate.PrivateKey = d.Val()
|
||||||
|
|
||||||
|
case "format":
|
||||||
|
if !d.NextArg() {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
pkiCa.Intermediate.Format = d.Val()
|
||||||
|
|
||||||
|
default:
|
||||||
|
return nil, d.Errf("unrecognized pki ca intermediate option '%s'", d.Val())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
return nil, d.Errf("unrecognized pki ca option '%s'", d.Val())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pki.CAs[pkiCa.ID] = pkiCa
|
||||||
|
|
||||||
|
default:
|
||||||
|
return nil, d.Errf("unrecognized pki option '%s'", d.Val())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return pki, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (st ServerType) buildPKIApp(
|
||||||
|
pairings []sbAddrAssociation,
|
||||||
|
options map[string]interface{},
|
||||||
|
warnings []caddyconfig.Warning,
|
||||||
|
) (*caddypki.PKI, []caddyconfig.Warning, error) {
|
||||||
|
|
||||||
|
skipInstallTrust := false
|
||||||
|
if _, ok := options["skip_install_trust"]; ok {
|
||||||
|
skipInstallTrust = true
|
||||||
|
}
|
||||||
|
falseBool := false
|
||||||
|
|
||||||
|
// Load the PKI app configured via global options
|
||||||
|
var pkiApp *caddypki.PKI
|
||||||
|
unwrappedPki, ok := options["pki"].(*caddypki.PKI)
|
||||||
|
if ok {
|
||||||
|
pkiApp = unwrappedPki
|
||||||
|
} else {
|
||||||
|
pkiApp = &caddypki.PKI{CAs: make(map[string]*caddypki.CA)}
|
||||||
|
}
|
||||||
|
for _, ca := range pkiApp.CAs {
|
||||||
|
if skipInstallTrust {
|
||||||
|
ca.InstallTrust = &falseBool
|
||||||
|
}
|
||||||
|
pkiApp.CAs[ca.ID] = ca
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add in the CAs configured via directives
|
||||||
|
for _, p := range pairings {
|
||||||
|
for _, sblock := range p.serverBlocks {
|
||||||
|
// find all the CAs that were defined and add them to the app config
|
||||||
|
// i.e. from any "acme_server" directives
|
||||||
|
for _, caCfgValue := range sblock.pile["pki.ca"] {
|
||||||
|
ca := caCfgValue.Value.(*caddypki.CA)
|
||||||
|
if skipInstallTrust {
|
||||||
|
ca.InstallTrust = &falseBool
|
||||||
|
}
|
||||||
|
|
||||||
|
// the CA might already exist from global options, so
|
||||||
|
// don't overwrite it in that case
|
||||||
|
if _, ok := pkiApp.CAs[ca.ID]; !ok {
|
||||||
|
pkiApp.CAs[ca.ID] = ca
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if there was no CAs defined in any of the servers,
|
||||||
|
// and we were requested to not install trust, then
|
||||||
|
// add one for the default/local CA to do so
|
||||||
|
if len(pkiApp.CAs) == 0 && skipInstallTrust {
|
||||||
|
ca := new(caddypki.CA)
|
||||||
|
ca.ID = caddypki.DefaultCAID
|
||||||
|
ca.InstallTrust = &falseBool
|
||||||
|
pkiApp.CAs[ca.ID] = ca
|
||||||
|
}
|
||||||
|
|
||||||
|
return pkiApp, warnings, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,244 @@
|
|||||||
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package httpcaddyfile
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/caddyserver/caddy/v2"
|
||||||
|
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||||
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||||
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||||
|
"github.com/dustin/go-humanize"
|
||||||
|
)
|
||||||
|
|
||||||
|
// serverOptions collects server config overrides parsed from Caddyfile global options
|
||||||
|
type serverOptions struct {
|
||||||
|
// If set, will only apply these options to servers that contain a
|
||||||
|
// listener address that matches exactly. If empty, will apply to all
|
||||||
|
// servers that were not already matched by another serverOptions.
|
||||||
|
ListenerAddress string
|
||||||
|
|
||||||
|
// These will all map 1:1 to the caddyhttp.Server struct
|
||||||
|
ListenerWrappersRaw []json.RawMessage
|
||||||
|
ReadTimeout caddy.Duration
|
||||||
|
ReadHeaderTimeout caddy.Duration
|
||||||
|
WriteTimeout caddy.Duration
|
||||||
|
IdleTimeout caddy.Duration
|
||||||
|
MaxHeaderBytes int
|
||||||
|
AllowH2C bool
|
||||||
|
ExperimentalHTTP3 bool
|
||||||
|
StrictSNIHost *bool
|
||||||
|
ShouldLogCredentials bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func unmarshalCaddyfileServerOptions(d *caddyfile.Dispenser) (interface{}, error) {
|
||||||
|
serverOpts := serverOptions{}
|
||||||
|
for d.Next() {
|
||||||
|
if d.NextArg() {
|
||||||
|
serverOpts.ListenerAddress = d.Val()
|
||||||
|
if d.NextArg() {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for nesting := d.Nesting(); d.NextBlock(nesting); {
|
||||||
|
switch d.Val() {
|
||||||
|
case "listener_wrappers":
|
||||||
|
for nesting := d.Nesting(); d.NextBlock(nesting); {
|
||||||
|
modID := "caddy.listeners." + d.Val()
|
||||||
|
unm, err := caddyfile.UnmarshalModule(d, modID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
listenerWrapper, ok := unm.(caddy.ListenerWrapper)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("module %s (%T) is not a listener wrapper", modID, unm)
|
||||||
|
}
|
||||||
|
jsonListenerWrapper := caddyconfig.JSONModuleObject(
|
||||||
|
listenerWrapper,
|
||||||
|
"wrapper",
|
||||||
|
listenerWrapper.(caddy.Module).CaddyModule().ID.Name(),
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
serverOpts.ListenerWrappersRaw = append(serverOpts.ListenerWrappersRaw, jsonListenerWrapper)
|
||||||
|
}
|
||||||
|
|
||||||
|
case "timeouts":
|
||||||
|
for nesting := d.Nesting(); d.NextBlock(nesting); {
|
||||||
|
switch d.Val() {
|
||||||
|
case "read_body":
|
||||||
|
if !d.NextArg() {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
dur, err := caddy.ParseDuration(d.Val())
|
||||||
|
if err != nil {
|
||||||
|
return nil, d.Errf("parsing read_body timeout duration: %v", err)
|
||||||
|
}
|
||||||
|
serverOpts.ReadTimeout = caddy.Duration(dur)
|
||||||
|
|
||||||
|
case "read_header":
|
||||||
|
if !d.NextArg() {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
dur, err := caddy.ParseDuration(d.Val())
|
||||||
|
if err != nil {
|
||||||
|
return nil, d.Errf("parsing read_header timeout duration: %v", err)
|
||||||
|
}
|
||||||
|
serverOpts.ReadHeaderTimeout = caddy.Duration(dur)
|
||||||
|
|
||||||
|
case "write":
|
||||||
|
if !d.NextArg() {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
dur, err := caddy.ParseDuration(d.Val())
|
||||||
|
if err != nil {
|
||||||
|
return nil, d.Errf("parsing write timeout duration: %v", err)
|
||||||
|
}
|
||||||
|
serverOpts.WriteTimeout = caddy.Duration(dur)
|
||||||
|
|
||||||
|
case "idle":
|
||||||
|
if !d.NextArg() {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
dur, err := caddy.ParseDuration(d.Val())
|
||||||
|
if err != nil {
|
||||||
|
return nil, d.Errf("parsing idle timeout duration: %v", err)
|
||||||
|
}
|
||||||
|
serverOpts.IdleTimeout = caddy.Duration(dur)
|
||||||
|
|
||||||
|
default:
|
||||||
|
return nil, d.Errf("unrecognized timeouts option '%s'", d.Val())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case "max_header_size":
|
||||||
|
var sizeStr string
|
||||||
|
if !d.AllArgs(&sizeStr) {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
size, err := humanize.ParseBytes(sizeStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, d.Errf("parsing max_header_size: %v", err)
|
||||||
|
}
|
||||||
|
serverOpts.MaxHeaderBytes = int(size)
|
||||||
|
|
||||||
|
case "log_credentials":
|
||||||
|
if d.NextArg() {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
serverOpts.ShouldLogCredentials = true
|
||||||
|
|
||||||
|
case "protocol":
|
||||||
|
for nesting := d.Nesting(); d.NextBlock(nesting); {
|
||||||
|
switch d.Val() {
|
||||||
|
case "allow_h2c":
|
||||||
|
if d.NextArg() {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
serverOpts.AllowH2C = true
|
||||||
|
|
||||||
|
case "experimental_http3":
|
||||||
|
if d.NextArg() {
|
||||||
|
return nil, d.ArgErr()
|
||||||
|
}
|
||||||
|
serverOpts.ExperimentalHTTP3 = true
|
||||||
|
|
||||||
|
case "strict_sni_host":
|
||||||
|
if d.NextArg() && d.Val() != "insecure_off" && d.Val() != "on" {
|
||||||
|
return nil, d.Errf("strict_sni_host only supports 'on' or 'insecure_off', got '%s'", d.Val())
|
||||||
|
}
|
||||||
|
boolVal := true
|
||||||
|
if d.Val() == "insecure_off" {
|
||||||
|
boolVal = false
|
||||||
|
}
|
||||||
|
serverOpts.StrictSNIHost = &boolVal
|
||||||
|
|
||||||
|
default:
|
||||||
|
return nil, d.Errf("unrecognized protocol option '%s'", d.Val())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
return nil, d.Errf("unrecognized servers option '%s'", d.Val())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return serverOpts, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// applyServerOptions sets the server options on the appropriate servers
|
||||||
|
func applyServerOptions(
|
||||||
|
servers map[string]*caddyhttp.Server,
|
||||||
|
options map[string]interface{},
|
||||||
|
warnings *[]caddyconfig.Warning,
|
||||||
|
) error {
|
||||||
|
// If experimental HTTP/3 is enabled, enable it on each server.
|
||||||
|
// We already know there won't be a conflict with serverOptions because
|
||||||
|
// we validated earlier that "experimental_http3" cannot be set at the same
|
||||||
|
// time as "servers"
|
||||||
|
if enableH3, ok := options["experimental_http3"].(bool); ok && enableH3 {
|
||||||
|
*warnings = append(*warnings, caddyconfig.Warning{Message: "the 'experimental_http3' global option is deprecated, please use the 'servers > protocol > experimental_http3' option instead"})
|
||||||
|
for _, srv := range servers {
|
||||||
|
srv.ExperimentalHTTP3 = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
serverOpts, ok := options["servers"].([]serverOptions)
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, server := range servers {
|
||||||
|
// find the options that apply to this server
|
||||||
|
opts := func() *serverOptions {
|
||||||
|
for _, entry := range serverOpts {
|
||||||
|
if entry.ListenerAddress == "" {
|
||||||
|
return &entry
|
||||||
|
}
|
||||||
|
for _, listener := range server.Listen {
|
||||||
|
if entry.ListenerAddress == listener {
|
||||||
|
return &entry
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}()
|
||||||
|
|
||||||
|
// if none apply, then move to the next server
|
||||||
|
if opts == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// set all the options
|
||||||
|
server.ListenerWrappersRaw = opts.ListenerWrappersRaw
|
||||||
|
server.ReadTimeout = opts.ReadTimeout
|
||||||
|
server.ReadHeaderTimeout = opts.ReadHeaderTimeout
|
||||||
|
server.WriteTimeout = opts.WriteTimeout
|
||||||
|
server.IdleTimeout = opts.IdleTimeout
|
||||||
|
server.MaxHeaderBytes = opts.MaxHeaderBytes
|
||||||
|
server.AllowH2C = opts.AllowH2C
|
||||||
|
server.ExperimentalHTTP3 = opts.ExperimentalHTTP3
|
||||||
|
server.StrictSNIHost = opts.StrictSNIHost
|
||||||
|
if opts.ShouldLogCredentials {
|
||||||
|
if server.Logs == nil {
|
||||||
|
server.Logs = &caddyhttp.ServerLogConfig{}
|
||||||
|
}
|
||||||
|
server.Logs.ShouldLogCredentials = opts.ShouldLogCredentials
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
+397
-196
@@ -21,12 +21,14 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/caddyserver/caddy/v2"
|
"github.com/caddyserver/caddy/v2"
|
||||||
"github.com/caddyserver/caddy/v2/caddyconfig"
|
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||||
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
||||||
"github.com/caddyserver/certmagic"
|
"github.com/caddyserver/certmagic"
|
||||||
|
"github.com/mholt/acmez/acme"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (st ServerType) buildTLSApp(
|
func (st ServerType) buildTLSApp(
|
||||||
@@ -38,6 +40,10 @@ func (st ServerType) buildTLSApp(
|
|||||||
tlsApp := &caddytls.TLS{CertificatesRaw: make(caddy.ModuleMap)}
|
tlsApp := &caddytls.TLS{CertificatesRaw: make(caddy.ModuleMap)}
|
||||||
var certLoaders []caddytls.CertificateLoader
|
var certLoaders []caddytls.CertificateLoader
|
||||||
|
|
||||||
|
httpPort := strconv.Itoa(caddyhttp.DefaultHTTPPort)
|
||||||
|
if hp, ok := options["http_port"].(int); ok {
|
||||||
|
httpPort = strconv.Itoa(hp)
|
||||||
|
}
|
||||||
httpsPort := strconv.Itoa(caddyhttp.DefaultHTTPSPort)
|
httpsPort := strconv.Itoa(caddyhttp.DefaultHTTPSPort)
|
||||||
if hsp, ok := options["https_port"].(int); ok {
|
if hsp, ok := options["https_port"].(int); ok {
|
||||||
httpsPort = strconv.Itoa(hsp)
|
httpsPort = strconv.Itoa(hsp)
|
||||||
@@ -48,7 +54,7 @@ func (st ServerType) buildTLSApp(
|
|||||||
// a hostless key, so that they don't get forgotten/omitted
|
// a hostless key, so that they don't get forgotten/omitted
|
||||||
// by auto-HTTPS (since they won't appear in route matchers)
|
// by auto-HTTPS (since they won't appear in route matchers)
|
||||||
var serverBlocksWithTLSHostlessKey int
|
var serverBlocksWithTLSHostlessKey int
|
||||||
hostsSharedWithHostlessKey := make(map[string]struct{})
|
httpsHostsSharedWithHostlessKey := make(map[string]struct{})
|
||||||
for _, pair := range pairings {
|
for _, pair := range pairings {
|
||||||
for _, sb := range pair.serverBlocks {
|
for _, sb := range pair.serverBlocks {
|
||||||
for _, addr := range sb.keys {
|
for _, addr := range sb.keys {
|
||||||
@@ -64,8 +70,8 @@ func (st ServerType) buildTLSApp(
|
|||||||
if otherAddr.Original == addr.Original {
|
if otherAddr.Original == addr.Original {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if otherAddr.Host != "" {
|
if otherAddr.Host != "" && otherAddr.Scheme != "http" && otherAddr.Port != httpPort {
|
||||||
hostsSharedWithHostlessKey[otherAddr.Host] = struct{}{}
|
httpsHostsSharedWithHostlessKey[otherAddr.Host] = struct{}{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
@@ -74,166 +80,174 @@ func (st ServerType) buildTLSApp(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// a catch-all automation policy is used as a "default" for all subjects that
|
||||||
|
// don't have custom configuration explicitly associated with them; this
|
||||||
|
// is only to add if the global settings or defaults are non-empty
|
||||||
catchAllAP, err := newBaseAutomationPolicy(options, warnings, false)
|
catchAllAP, err := newBaseAutomationPolicy(options, warnings, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, warnings, err
|
return nil, warnings, err
|
||||||
}
|
}
|
||||||
|
if catchAllAP != nil {
|
||||||
|
if tlsApp.Automation == nil {
|
||||||
|
tlsApp.Automation = new(caddytls.AutomationConfig)
|
||||||
|
}
|
||||||
|
tlsApp.Automation.Policies = append(tlsApp.Automation.Policies, catchAllAP)
|
||||||
|
}
|
||||||
|
|
||||||
for _, p := range pairings {
|
for _, p := range pairings {
|
||||||
|
// avoid setting up TLS automation policies for a server that is HTTP-only
|
||||||
|
if !listenersUseAnyPortOtherThan(p.addresses, httpPort) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
for _, sblock := range p.serverBlocks {
|
for _, sblock := range p.serverBlocks {
|
||||||
|
// check the scheme of all the site addresses,
|
||||||
|
// skip building AP if they all had http://
|
||||||
|
if sblock.isAllHTTP() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
// get values that populate an automation policy for this block
|
// get values that populate an automation policy for this block
|
||||||
var ap *caddytls.AutomationPolicy
|
ap, err := newBaseAutomationPolicy(options, warnings, true)
|
||||||
|
if err != nil {
|
||||||
|
return nil, warnings, err
|
||||||
|
}
|
||||||
|
|
||||||
sblockHosts := sblock.hostsFromKeys(false)
|
sblockHosts := sblock.hostsFromKeys(false)
|
||||||
if len(sblockHosts) == 0 {
|
if len(sblockHosts) == 0 && catchAllAP != nil {
|
||||||
ap = catchAllAP
|
ap = catchAllAP
|
||||||
}
|
}
|
||||||
|
|
||||||
// on-demand tls
|
// on-demand tls
|
||||||
if _, ok := sblock.pile["tls.on_demand"]; ok {
|
if _, ok := sblock.pile["tls.on_demand"]; ok {
|
||||||
if ap == nil {
|
|
||||||
var err error
|
|
||||||
ap, err = newBaseAutomationPolicy(options, warnings, true)
|
|
||||||
if err != nil {
|
|
||||||
return nil, warnings, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ap.OnDemand = true
|
ap.OnDemand = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if keyTypeVals, ok := sblock.pile["tls.key_type"]; ok {
|
||||||
|
ap.KeyType = keyTypeVals[0].Value.(string)
|
||||||
|
}
|
||||||
|
|
||||||
// certificate issuers
|
// certificate issuers
|
||||||
if issuerVals, ok := sblock.pile["tls.cert_issuer"]; ok {
|
if issuerVals, ok := sblock.pile["tls.cert_issuer"]; ok {
|
||||||
|
var issuers []certmagic.Issuer
|
||||||
for _, issuerVal := range issuerVals {
|
for _, issuerVal := range issuerVals {
|
||||||
issuer := issuerVal.Value.(certmagic.Issuer)
|
issuers = append(issuers, issuerVal.Value.(certmagic.Issuer))
|
||||||
if ap == nil {
|
|
||||||
var err error
|
|
||||||
ap, err = newBaseAutomationPolicy(options, warnings, true)
|
|
||||||
if err != nil {
|
|
||||||
return nil, warnings, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ap == catchAllAP && !reflect.DeepEqual(ap.Issuer, issuer) {
|
|
||||||
return nil, warnings, fmt.Errorf("automation policy from site block is also default/catch-all policy because of key without hostname, and the two are in conflict: %#v != %#v", ap.Issuer, issuer)
|
|
||||||
}
|
|
||||||
ap.Issuer = issuer
|
|
||||||
}
|
}
|
||||||
|
if ap == catchAllAP && !reflect.DeepEqual(ap.Issuers, issuers) {
|
||||||
|
return nil, warnings, fmt.Errorf("automation policy from site block is also default/catch-all policy because of key without hostname, and the two are in conflict: %#v != %#v", ap.Issuers, issuers)
|
||||||
|
}
|
||||||
|
ap.Issuers = issuers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// certificate managers
|
||||||
|
if certManagerVals, ok := sblock.pile["tls.cert_manager"]; ok {
|
||||||
|
for _, certManager := range certManagerVals {
|
||||||
|
certGetterName := certManager.Value.(caddy.Module).CaddyModule().ID.Name()
|
||||||
|
ap.ManagersRaw = append(ap.ManagersRaw, caddyconfig.JSONModuleObject(certManager.Value, "via", certGetterName, &warnings))
|
||||||
|
}
|
||||||
|
}
|
||||||
// custom bind host
|
// custom bind host
|
||||||
for _, cfgVal := range sblock.pile["bind"] {
|
for _, cfgVal := range sblock.pile["bind"] {
|
||||||
// either an existing issuer is already configured (and thus, ap is not
|
for _, iss := range ap.Issuers {
|
||||||
// nil), or we need to configure an issuer, so we need ap to be non-nil
|
// if an issuer was already configured and it is NOT an ACME issuer,
|
||||||
if ap == nil {
|
// skip, since we intend to adjust only ACME issuers; ensure we
|
||||||
ap, err = newBaseAutomationPolicy(options, warnings, true)
|
// include any issuer that embeds/wraps an underlying ACME issuer
|
||||||
if err != nil {
|
var acmeIssuer *caddytls.ACMEIssuer
|
||||||
return nil, warnings, err
|
if acmeWrapper, ok := iss.(acmeCapable); ok {
|
||||||
|
acmeIssuer = acmeWrapper.GetACMEIssuer()
|
||||||
|
}
|
||||||
|
if acmeIssuer == nil {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// if an issuer was already configured and it is NOT an ACME
|
// proceed to configure the ACME issuer's bind host, without
|
||||||
// issuer, skip, since we intend to adjust only ACME issuers
|
// overwriting any existing settings
|
||||||
var acmeIssuer *caddytls.ACMEIssuer
|
if acmeIssuer.Challenges == nil {
|
||||||
if ap.Issuer != nil {
|
acmeIssuer.Challenges = new(caddytls.ChallengesConfig)
|
||||||
var ok bool
|
}
|
||||||
if acmeIssuer, ok = ap.Issuer.(*caddytls.ACMEIssuer); !ok {
|
if acmeIssuer.Challenges.BindHost == "" {
|
||||||
break
|
// only binding to one host is supported
|
||||||
|
var bindHost string
|
||||||
|
if bindHosts, ok := cfgVal.Value.([]string); ok && len(bindHosts) > 0 {
|
||||||
|
bindHost = bindHosts[0]
|
||||||
|
}
|
||||||
|
acmeIssuer.Challenges.BindHost = bindHost
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// proceed to configure the ACME issuer's bind host, without
|
|
||||||
// overwriting any existing settings
|
|
||||||
if acmeIssuer == nil {
|
|
||||||
acmeIssuer = new(caddytls.ACMEIssuer)
|
|
||||||
}
|
|
||||||
if acmeIssuer.Challenges == nil {
|
|
||||||
acmeIssuer.Challenges = new(caddytls.ChallengesConfig)
|
|
||||||
}
|
|
||||||
if acmeIssuer.Challenges.BindHost == "" {
|
|
||||||
// only binding to one host is supported
|
|
||||||
var bindHost string
|
|
||||||
if bindHosts, ok := cfgVal.Value.([]string); ok && len(bindHosts) > 0 {
|
|
||||||
bindHost = bindHosts[0]
|
|
||||||
}
|
|
||||||
acmeIssuer.Challenges.BindHost = bindHost
|
|
||||||
}
|
|
||||||
ap.Issuer = acmeIssuer // we'll encode it later
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ap != nil {
|
// first make sure this block is allowed to create an automation policy;
|
||||||
if ap.Issuer != nil {
|
// doing so is forbidden if it has a key with no host (i.e. ":443")
|
||||||
// encode issuer now that it's all set up
|
// and if there is a different server block that also has a key with no
|
||||||
issuerName := ap.Issuer.(caddy.Module).CaddyModule().ID.Name()
|
// host -- since a key with no host matches any host, we need its
|
||||||
ap.IssuerRaw = caddyconfig.JSONModuleObject(ap.Issuer, "module", issuerName, &warnings)
|
// associated automation policy to have an empty Subjects list, i.e. no
|
||||||
|
// host filter, which is indistinguishable between the two server blocks
|
||||||
|
// because automation is not done in the context of a particular server...
|
||||||
|
// this is an example of a poor mapping from Caddyfile to JSON but that's
|
||||||
|
// the least-leaky abstraction I could figure out
|
||||||
|
if len(sblockHosts) == 0 {
|
||||||
|
if serverBlocksWithTLSHostlessKey > 1 {
|
||||||
|
// this server block and at least one other has a key with no host,
|
||||||
|
// making the two indistinguishable; it is misleading to define such
|
||||||
|
// a policy within one server block since it actually will apply to
|
||||||
|
// others as well
|
||||||
|
return nil, warnings, fmt.Errorf("cannot make a TLS automation policy from a server block that has a host-less address when there are other TLS-enabled server block addresses lacking a host")
|
||||||
}
|
}
|
||||||
|
if catchAllAP == nil {
|
||||||
|
// this server block has a key with no hosts, but there is not yet
|
||||||
|
// a catch-all automation policy (probably because no global options
|
||||||
|
// were set), so this one becomes it
|
||||||
|
catchAllAP = ap
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// first make sure this block is allowed to create an automation policy;
|
// associate our new automation policy with this server block's hosts
|
||||||
// doing so is forbidden if it has a key with no host (i.e. ":443")
|
ap.Subjects = sblock.hostsFromKeysNotHTTP(httpPort)
|
||||||
// and if there is a different server block that also has a key with no
|
sort.Strings(ap.Subjects) // solely for deterministic test results
|
||||||
// host -- since a key with no host matches any host, we need its
|
|
||||||
// associated automation policy to have an empty Subjects list, i.e. no
|
// if a combination of public and internal names were given
|
||||||
// host filter, which is indistinguishable between the two server blocks
|
// for this same server block and no issuer was specified, we
|
||||||
// because automation is not done in the context of a particular server...
|
// need to separate them out in the automation policies so
|
||||||
// this is an example of a poor mapping from Caddyfile to JSON but that's
|
// that the internal names can use the internal issuer and
|
||||||
// the least-leaky abstraction I could figure out
|
// the other names can use the default/public/ACME issuer
|
||||||
if len(sblockHosts) == 0 {
|
var ap2 *caddytls.AutomationPolicy
|
||||||
if serverBlocksWithTLSHostlessKey > 1 {
|
if len(ap.Issuers) == 0 {
|
||||||
// this server block and at least one other has a key with no host,
|
var internal, external []string
|
||||||
// making the two indistinguishable; it is misleading to define such
|
for _, s := range ap.Subjects {
|
||||||
// a policy within one server block since it actually will apply to
|
if !certmagic.SubjectQualifiesForCert(s) {
|
||||||
// others as well
|
return nil, warnings, fmt.Errorf("subject does not qualify for certificate: '%s'", s)
|
||||||
return nil, warnings, fmt.Errorf("cannot make a TLS automation policy from a server block that has a host-less address when there are other TLS-enabled server block addresses lacking a host")
|
|
||||||
}
|
}
|
||||||
if catchAllAP == nil {
|
// we don't use certmagic.SubjectQualifiesForPublicCert() because of one nuance:
|
||||||
// this server block has a key with no hosts, but there is not yet
|
// names like *.*.tld that may not qualify for a public certificate are actually
|
||||||
// a catch-all automation policy (probably because no global options
|
// fine when used with OnDemand, since OnDemand (currently) does not obtain
|
||||||
// were set), so this one becomes it
|
// wildcards (if it ever does, there will be a separate config option to enable
|
||||||
catchAllAP = ap
|
// it that we would need to check here) since the hostname is known at handshake;
|
||||||
|
// and it is unexpected to switch to internal issuer when the user wants to get
|
||||||
|
// regular certificates on-demand for a class of certs like *.*.tld.
|
||||||
|
if subjectQualifiesForPublicCert(ap, s) {
|
||||||
|
external = append(external, s)
|
||||||
|
} else {
|
||||||
|
internal = append(internal, s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if len(external) > 0 && len(internal) > 0 {
|
||||||
// associate our new automation policy with this server block's hosts,
|
ap.Subjects = external
|
||||||
// unless, of course, the server block has a key with no hosts, in which
|
apCopy := *ap
|
||||||
// case its automation policy becomes or blends with the default/global
|
ap2 = &apCopy
|
||||||
// automation policy because, of necessity, it applies to all hostnames
|
ap2.Subjects = internal
|
||||||
// (i.e. it has no Subjects filter) -- in that case, we'll append it last
|
ap2.IssuersRaw = []json.RawMessage{caddyconfig.JSONModuleObject(caddytls.InternalIssuer{}, "module", "internal", &warnings)}
|
||||||
if ap != catchAllAP {
|
|
||||||
ap.Subjects = sblockHosts
|
|
||||||
|
|
||||||
// if a combination of public and internal names were given
|
|
||||||
// for this same server block and no issuer was specified, we
|
|
||||||
// need to separate them out in the automation policies so
|
|
||||||
// that the internal names can use the internal issuer and
|
|
||||||
// the other names can use the default/public/ACME issuer
|
|
||||||
var ap2 *caddytls.AutomationPolicy
|
|
||||||
if ap.Issuer == nil {
|
|
||||||
var internal, external []string
|
|
||||||
for _, s := range ap.Subjects {
|
|
||||||
if certmagic.SubjectQualifiesForPublicCert(s) {
|
|
||||||
external = append(external, s)
|
|
||||||
} else {
|
|
||||||
internal = append(internal, s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(external) > 0 && len(internal) > 0 {
|
|
||||||
ap.Subjects = external
|
|
||||||
apCopy := *ap
|
|
||||||
ap2 = &apCopy
|
|
||||||
ap2.Subjects = internal
|
|
||||||
ap2.IssuerRaw = caddyconfig.JSONModuleObject(caddytls.InternalIssuer{}, "module", "internal", &warnings)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if tlsApp.Automation == nil {
|
|
||||||
tlsApp.Automation = new(caddytls.AutomationConfig)
|
|
||||||
}
|
|
||||||
tlsApp.Automation.Policies = append(tlsApp.Automation.Policies, ap)
|
|
||||||
if ap2 != nil {
|
|
||||||
tlsApp.Automation.Policies = append(tlsApp.Automation.Policies, ap2)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if tlsApp.Automation == nil {
|
||||||
|
tlsApp.Automation = new(caddytls.AutomationConfig)
|
||||||
|
}
|
||||||
|
tlsApp.Automation.Policies = append(tlsApp.Automation.Policies, ap)
|
||||||
|
if ap2 != nil {
|
||||||
|
tlsApp.Automation.Policies = append(tlsApp.Automation.Policies, ap2)
|
||||||
|
}
|
||||||
|
|
||||||
// certificate loaders
|
// certificate loaders
|
||||||
if clVals, ok := sblock.pile["tls.certificate_loader"]; ok {
|
if clVals, ok := sblock.pile["tls.cert_loader"]; ok {
|
||||||
for _, clVal := range clVals {
|
for _, clVal := range clVals {
|
||||||
certLoaders = append(certLoaders, clVal.Value.(caddytls.CertificateLoader))
|
certLoaders = append(certLoaders, clVal.Value.(caddytls.CertificateLoader))
|
||||||
}
|
}
|
||||||
@@ -277,6 +291,27 @@ func (st ServerType) buildTLSApp(
|
|||||||
tlsApp.Automation.OnDemand = onDemand
|
tlsApp.Automation.OnDemand = onDemand
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set the storage clean interval if configured
|
||||||
|
if storageCleanInterval, ok := options["storage_clean_interval"].(caddy.Duration); ok {
|
||||||
|
if tlsApp.Automation == nil {
|
||||||
|
tlsApp.Automation = new(caddytls.AutomationConfig)
|
||||||
|
}
|
||||||
|
tlsApp.Automation.StorageCleanInterval = storageCleanInterval
|
||||||
|
}
|
||||||
|
|
||||||
|
// set the expired certificates renew interval if configured
|
||||||
|
if renewCheckInterval, ok := options["renew_interval"].(caddy.Duration); ok {
|
||||||
|
if tlsApp.Automation == nil {
|
||||||
|
tlsApp.Automation = new(caddytls.AutomationConfig)
|
||||||
|
}
|
||||||
|
tlsApp.Automation.RenewCheckInterval = renewCheckInterval
|
||||||
|
}
|
||||||
|
|
||||||
|
// set whether OCSP stapling should be disabled for manually-managed certificates
|
||||||
|
if ocspConfig, ok := options["ocsp_stapling"].(certmagic.OCSPConfig); ok {
|
||||||
|
tlsApp.DisableOCSPStapling = ocspConfig.DisableStapling
|
||||||
|
}
|
||||||
|
|
||||||
// if any hostnames appear on the same server block as a key with
|
// if any hostnames appear on the same server block as a key with
|
||||||
// no host, they will not be used with route matchers because the
|
// no host, they will not be used with route matchers because the
|
||||||
// hostless key matches all hosts, therefore, it wouldn't be
|
// hostless key matches all hosts, therefore, it wouldn't be
|
||||||
@@ -286,9 +321,9 @@ func (st ServerType) buildTLSApp(
|
|||||||
// get internal certificates by default rather than ACME
|
// get internal certificates by default rather than ACME
|
||||||
var al caddytls.AutomateLoader
|
var al caddytls.AutomateLoader
|
||||||
internalAP := &caddytls.AutomationPolicy{
|
internalAP := &caddytls.AutomationPolicy{
|
||||||
IssuerRaw: json.RawMessage(`{"module":"internal"}`),
|
IssuersRaw: []json.RawMessage{json.RawMessage(`{"module":"internal"}`)},
|
||||||
}
|
}
|
||||||
for h := range hostsSharedWithHostlessKey {
|
for h := range httpsHostsSharedWithHostlessKey {
|
||||||
al = append(al, h)
|
al = append(al, h)
|
||||||
if !certmagic.SubjectQualifiesForPublicCert(h) {
|
if !certmagic.SubjectQualifiesForPublicCert(h) {
|
||||||
internalAP.Subjects = append(internalAP.Subjects, h)
|
internalAP.Subjects = append(internalAP.Subjects, h)
|
||||||
@@ -304,23 +339,60 @@ func (st ServerType) buildTLSApp(
|
|||||||
tlsApp.Automation.Policies = append(tlsApp.Automation.Policies, internalAP)
|
tlsApp.Automation.Policies = append(tlsApp.Automation.Policies, internalAP)
|
||||||
}
|
}
|
||||||
|
|
||||||
// if there is a global/catch-all automation policy, ensure it goes last
|
// if there are any global options set for issuers (ACME ones in particular), make sure they
|
||||||
if catchAllAP != nil {
|
// take effect in every automation policy that does not have any issuers
|
||||||
// first, encode its issuer, if there is one
|
if tlsApp.Automation != nil {
|
||||||
if catchAllAP.Issuer != nil {
|
globalEmail := options["email"]
|
||||||
issuerName := catchAllAP.Issuer.(caddy.Module).CaddyModule().ID.Name()
|
globalACMECA := options["acme_ca"]
|
||||||
catchAllAP.IssuerRaw = caddyconfig.JSONModuleObject(catchAllAP.Issuer, "module", issuerName, &warnings)
|
globalACMECARoot := options["acme_ca_root"]
|
||||||
}
|
globalACMEDNS := options["acme_dns"]
|
||||||
|
globalACMEEAB := options["acme_eab"]
|
||||||
|
globalPreferredChains := options["preferred_chains"]
|
||||||
|
hasGlobalACMEDefaults := globalEmail != nil || globalACMECA != nil || globalACMECARoot != nil || globalACMEDNS != nil || globalACMEEAB != nil || globalPreferredChains != nil
|
||||||
|
if hasGlobalACMEDefaults {
|
||||||
|
for i := 0; i < len(tlsApp.Automation.Policies); i++ {
|
||||||
|
ap := tlsApp.Automation.Policies[i]
|
||||||
|
if len(ap.Issuers) == 0 && automationPolicyHasAllPublicNames(ap) {
|
||||||
|
// for public names, create default issuers which will later be filled in with configured global defaults
|
||||||
|
// (internal names will implicitly use the internal issuer at auto-https time)
|
||||||
|
ap.Issuers = caddytls.DefaultIssuers()
|
||||||
|
|
||||||
// then append it to the end of the policies list
|
// if a specific endpoint is configured, can't use multiple default issuers
|
||||||
if tlsApp.Automation == nil {
|
if globalACMECA != nil {
|
||||||
tlsApp.Automation = new(caddytls.AutomationConfig)
|
if strings.Contains(globalACMECA.(string), "zerossl") {
|
||||||
|
ap.Issuers = []certmagic.Issuer{&caddytls.ZeroSSLIssuer{ACMEIssuer: new(caddytls.ACMEIssuer)}}
|
||||||
|
} else {
|
||||||
|
ap.Issuers = []certmagic.Issuer{new(caddytls.ACMEIssuer)}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
tlsApp.Automation.Policies = append(tlsApp.Automation.Policies, catchAllAP)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// do a little verification & cleanup
|
// finalize and verify policies; do cleanup
|
||||||
if tlsApp.Automation != nil {
|
if tlsApp.Automation != nil {
|
||||||
|
for i, ap := range tlsApp.Automation.Policies {
|
||||||
|
// ensure all issuers have global defaults filled in
|
||||||
|
for j, issuer := range ap.Issuers {
|
||||||
|
err := fillInGlobalACMEDefaults(issuer, options)
|
||||||
|
if err != nil {
|
||||||
|
return nil, warnings, fmt.Errorf("filling in global issuer defaults for AP %d, issuer %d: %v", i, j, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// encode all issuer values we created, so they will be rendered in the output
|
||||||
|
if len(ap.Issuers) > 0 && ap.IssuersRaw == nil {
|
||||||
|
for _, iss := range ap.Issuers {
|
||||||
|
issuerName := iss.(caddy.Module).CaddyModule().ID.Name()
|
||||||
|
ap.IssuersRaw = append(ap.IssuersRaw, caddyconfig.JSONModuleObject(iss, "module", issuerName, &warnings))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// consolidate automation policies that are the exact same
|
||||||
|
tlsApp.Automation.Policies = consolidateAutomationPolicies(tlsApp.Automation.Policies)
|
||||||
|
|
||||||
// ensure automation policies don't overlap subjects (this should be
|
// ensure automation policies don't overlap subjects (this should be
|
||||||
// an error at provision-time as well, but catch it in the adapt phase
|
// an error at provision-time as well, but catch it in the adapt phase
|
||||||
// for convenience)
|
// for convenience)
|
||||||
@@ -334,27 +406,74 @@ func (st ServerType) buildTLSApp(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// consolidate automation policies that are the exact same
|
// if nothing remains, remove any excess values to clean up the resulting config
|
||||||
tlsApp.Automation.Policies = consolidateAutomationPolicies(tlsApp.Automation.Policies)
|
if len(tlsApp.Automation.Policies) == 0 {
|
||||||
|
tlsApp.Automation.Policies = nil
|
||||||
|
}
|
||||||
|
if reflect.DeepEqual(tlsApp.Automation, new(caddytls.AutomationConfig)) {
|
||||||
|
tlsApp.Automation = nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return tlsApp, warnings, nil
|
return tlsApp, warnings, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type acmeCapable interface{ GetACMEIssuer() *caddytls.ACMEIssuer }
|
||||||
|
|
||||||
|
func fillInGlobalACMEDefaults(issuer certmagic.Issuer, options map[string]interface{}) error {
|
||||||
|
acmeWrapper, ok := issuer.(acmeCapable)
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
acmeIssuer := acmeWrapper.GetACMEIssuer()
|
||||||
|
if acmeIssuer == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
globalEmail := options["email"]
|
||||||
|
globalACMECA := options["acme_ca"]
|
||||||
|
globalACMECARoot := options["acme_ca_root"]
|
||||||
|
globalACMEDNS := options["acme_dns"]
|
||||||
|
globalACMEEAB := options["acme_eab"]
|
||||||
|
globalPreferredChains := options["preferred_chains"]
|
||||||
|
|
||||||
|
if globalEmail != nil && acmeIssuer.Email == "" {
|
||||||
|
acmeIssuer.Email = globalEmail.(string)
|
||||||
|
}
|
||||||
|
if globalACMECA != nil && acmeIssuer.CA == "" {
|
||||||
|
acmeIssuer.CA = globalACMECA.(string)
|
||||||
|
}
|
||||||
|
if globalACMECARoot != nil && !sliceContains(acmeIssuer.TrustedRootsPEMFiles, globalACMECARoot.(string)) {
|
||||||
|
acmeIssuer.TrustedRootsPEMFiles = append(acmeIssuer.TrustedRootsPEMFiles, globalACMECARoot.(string))
|
||||||
|
}
|
||||||
|
if globalACMEDNS != nil && (acmeIssuer.Challenges == nil || acmeIssuer.Challenges.DNS == nil) {
|
||||||
|
acmeIssuer.Challenges = &caddytls.ChallengesConfig{
|
||||||
|
DNS: &caddytls.DNSChallengeConfig{
|
||||||
|
ProviderRaw: caddyconfig.JSONModuleObject(globalACMEDNS, "name", globalACMEDNS.(caddy.Module).CaddyModule().ID.Name(), nil),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if globalACMEEAB != nil && acmeIssuer.ExternalAccount == nil {
|
||||||
|
acmeIssuer.ExternalAccount = globalACMEEAB.(*acme.EAB)
|
||||||
|
}
|
||||||
|
if globalPreferredChains != nil && acmeIssuer.PreferredChains == nil {
|
||||||
|
acmeIssuer.PreferredChains = globalPreferredChains.(*caddytls.ChainPreference)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// newBaseAutomationPolicy returns a new TLS automation policy that gets
|
// newBaseAutomationPolicy returns a new TLS automation policy that gets
|
||||||
// its values from the global options map. It should be used as the base
|
// its values from the global options map. It should be used as the base
|
||||||
// for any other automation policies. A nil policy (and no error) will be
|
// for any other automation policies. A nil policy (and no error) will be
|
||||||
// returned if there are no default/global options. However, if always is
|
// returned if there are no default/global options. However, if always is
|
||||||
// true, a non-nil value will always be returned (unless there is an error).
|
// true, a non-nil value will always be returned (unless there is an error).
|
||||||
func newBaseAutomationPolicy(options map[string]interface{}, warnings []caddyconfig.Warning, always bool) (*caddytls.AutomationPolicy, error) {
|
func newBaseAutomationPolicy(options map[string]interface{}, warnings []caddyconfig.Warning, always bool) (*caddytls.AutomationPolicy, error) {
|
||||||
acmeCA, hasACMECA := options["acme_ca"]
|
issuers, hasIssuers := options["cert_issuer"]
|
||||||
acmeDNS, hasACMEDNS := options["acme_dns"]
|
_, hasLocalCerts := options["local_certs"]
|
||||||
acmeCARoot, hasACMECARoot := options["acme_ca_root"]
|
|
||||||
email, hasEmail := options["email"]
|
|
||||||
localCerts, hasLocalCerts := options["local_certs"]
|
|
||||||
keyType, hasKeyType := options["key_type"]
|
keyType, hasKeyType := options["key_type"]
|
||||||
|
ocspStapling, hasOCSPStapling := options["ocsp_stapling"]
|
||||||
|
|
||||||
hasGlobalAutomationOpts := hasACMECA || hasACMEDNS || hasACMECARoot || hasEmail || hasLocalCerts || hasKeyType
|
hasGlobalAutomationOpts := hasIssuers || hasLocalCerts || hasKeyType || hasOCSPStapling
|
||||||
|
|
||||||
// if there are no global options related to automation policies
|
// if there are no global options related to automation policies
|
||||||
// set, then we can just return right away
|
// set, then we can just return right away
|
||||||
@@ -366,40 +485,24 @@ func newBaseAutomationPolicy(options map[string]interface{}, warnings []caddycon
|
|||||||
}
|
}
|
||||||
|
|
||||||
ap := new(caddytls.AutomationPolicy)
|
ap := new(caddytls.AutomationPolicy)
|
||||||
|
if hasKeyType {
|
||||||
|
ap.KeyType = keyType.(string)
|
||||||
|
}
|
||||||
|
|
||||||
if localCerts != nil {
|
if hasIssuers && hasLocalCerts {
|
||||||
// internal issuer enabled trumps any ACME configurations; useful in testing
|
return nil, fmt.Errorf("global options are ambiguous: local_certs is confusing when combined with cert_issuer, because local_certs is also a specific kind of issuer")
|
||||||
ap.Issuer = new(caddytls.InternalIssuer) // we'll encode it later
|
}
|
||||||
} else {
|
|
||||||
if acmeCA == nil {
|
if hasIssuers {
|
||||||
acmeCA = ""
|
ap.Issuers = issuers.([]certmagic.Issuer)
|
||||||
}
|
} else if hasLocalCerts {
|
||||||
if email == nil {
|
ap.Issuers = []certmagic.Issuer{new(caddytls.InternalIssuer)}
|
||||||
email = ""
|
}
|
||||||
}
|
|
||||||
mgr := &caddytls.ACMEIssuer{
|
if hasOCSPStapling {
|
||||||
CA: acmeCA.(string),
|
ocspConfig := ocspStapling.(certmagic.OCSPConfig)
|
||||||
Email: email.(string),
|
ap.DisableOCSPStapling = ocspConfig.DisableStapling
|
||||||
}
|
ap.OCSPOverrides = ocspConfig.ResponderOverrides
|
||||||
if acmeDNS != nil {
|
|
||||||
provName := acmeDNS.(string)
|
|
||||||
dnsProvModule, err := caddy.GetModule("dns.providers." + provName)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("getting DNS provider module named '%s': %v", provName, err)
|
|
||||||
}
|
|
||||||
mgr.Challenges = &caddytls.ChallengesConfig{
|
|
||||||
DNS: &caddytls.DNSChallengeConfig{
|
|
||||||
ProviderRaw: caddyconfig.JSONModuleObject(dnsProvModule.New(), "name", provName, &warnings),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if acmeCARoot != nil {
|
|
||||||
mgr.TrustedRootsPEMFiles = []string{acmeCARoot.(string)}
|
|
||||||
}
|
|
||||||
if keyType != nil {
|
|
||||||
ap.KeyType = keyType.(string)
|
|
||||||
}
|
|
||||||
ap.Issuer = mgr // we'll encode it later
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ap, nil
|
return ap, nil
|
||||||
@@ -408,17 +511,50 @@ func newBaseAutomationPolicy(options map[string]interface{}, warnings []caddycon
|
|||||||
// consolidateAutomationPolicies combines automation policies that are the same,
|
// consolidateAutomationPolicies combines automation policies that are the same,
|
||||||
// for a cleaner overall output.
|
// for a cleaner overall output.
|
||||||
func consolidateAutomationPolicies(aps []*caddytls.AutomationPolicy) []*caddytls.AutomationPolicy {
|
func consolidateAutomationPolicies(aps []*caddytls.AutomationPolicy) []*caddytls.AutomationPolicy {
|
||||||
for i := 0; i < len(aps); i++ {
|
// sort from most specific to least specific; we depend on this ordering
|
||||||
for j := 0; j < len(aps); j++ {
|
sort.SliceStable(aps, func(i, j int) bool {
|
||||||
if j == i {
|
if automationPolicyIsSubset(aps[i], aps[j]) {
|
||||||
continue
|
return true
|
||||||
}
|
}
|
||||||
|
if automationPolicyIsSubset(aps[j], aps[i]) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return len(aps[i].Subjects) > len(aps[j].Subjects)
|
||||||
|
})
|
||||||
|
|
||||||
|
emptyAPCount := 0
|
||||||
|
origLenAPs := len(aps)
|
||||||
|
// compute the number of empty policies (disregarding subjects) - see #4128
|
||||||
|
emptyAP := new(caddytls.AutomationPolicy)
|
||||||
|
for i := 0; i < len(aps); i++ {
|
||||||
|
emptyAP.Subjects = aps[i].Subjects
|
||||||
|
if reflect.DeepEqual(aps[i], emptyAP) {
|
||||||
|
emptyAPCount++
|
||||||
|
if !automationPolicyHasAllPublicNames(aps[i]) {
|
||||||
|
// if this automation policy has internal names, we might as well remove it
|
||||||
|
// so auto-https can implicitly use the internal issuer
|
||||||
|
aps = append(aps[:i], aps[i+1:]...)
|
||||||
|
i--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If all policies are empty, we can return nil, as there is no need to set any policy
|
||||||
|
if emptyAPCount == origLenAPs {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove or combine duplicate policies
|
||||||
|
outer:
|
||||||
|
for i := 0; i < len(aps); i++ {
|
||||||
|
// compare only with next policies; we sorted by specificity so we must not delete earlier policies
|
||||||
|
for j := i + 1; j < len(aps); j++ {
|
||||||
// if they're exactly equal in every way, just keep one of them
|
// if they're exactly equal in every way, just keep one of them
|
||||||
if reflect.DeepEqual(aps[i], aps[j]) {
|
if reflect.DeepEqual(aps[i], aps[j]) {
|
||||||
aps = append(aps[:j], aps[j+1:]...)
|
aps = append(aps[:j], aps[j+1:]...)
|
||||||
|
// must re-evaluate current i against next j; can't skip it!
|
||||||
|
// even if i decrements to -1, will be incremented to 0 immediately
|
||||||
i--
|
i--
|
||||||
break
|
continue outer
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the policy is the same, we can keep just one, but we have
|
// if the policy is the same, we can keep just one, but we have
|
||||||
@@ -427,30 +563,95 @@ func consolidateAutomationPolicies(aps []*caddytls.AutomationPolicy) []*caddytls
|
|||||||
// otherwise the one without any subjects (a catch-all) would be
|
// otherwise the one without any subjects (a catch-all) would be
|
||||||
// eaten up by the one with subjects; and if both have subjects, we
|
// eaten up by the one with subjects; and if both have subjects, we
|
||||||
// need to combine their lists
|
// need to combine their lists
|
||||||
if bytes.Equal(aps[i].IssuerRaw, aps[j].IssuerRaw) &&
|
if reflect.DeepEqual(aps[i].IssuersRaw, aps[j].IssuersRaw) &&
|
||||||
bytes.Equal(aps[i].StorageRaw, aps[j].StorageRaw) &&
|
bytes.Equal(aps[i].StorageRaw, aps[j].StorageRaw) &&
|
||||||
aps[i].MustStaple == aps[j].MustStaple &&
|
aps[i].MustStaple == aps[j].MustStaple &&
|
||||||
aps[i].KeyType == aps[j].KeyType &&
|
aps[i].KeyType == aps[j].KeyType &&
|
||||||
aps[i].OnDemand == aps[j].OnDemand &&
|
aps[i].OnDemand == aps[j].OnDemand &&
|
||||||
aps[i].RenewalWindowRatio == aps[j].RenewalWindowRatio {
|
aps[i].RenewalWindowRatio == aps[j].RenewalWindowRatio {
|
||||||
if len(aps[i].Subjects) == 0 && len(aps[j].Subjects) > 0 {
|
if len(aps[i].Subjects) > 0 && len(aps[j].Subjects) == 0 {
|
||||||
aps = append(aps[:j], aps[j+1:]...)
|
// later policy (at j) has no subjects ("catch-all"), so we can
|
||||||
} else if len(aps[i].Subjects) > 0 && len(aps[j].Subjects) == 0 {
|
// remove the identical-but-more-specific policy that comes first
|
||||||
aps = append(aps[:i], aps[i+1:]...)
|
// AS LONG AS it is not shadowed by another policy before it; e.g.
|
||||||
|
// if policy i is for example.com, policy i+1 is '*.com', and policy
|
||||||
|
// j is catch-all, we cannot remove policy i because that would
|
||||||
|
// cause example.com to be served by the less specific policy for
|
||||||
|
// '*.com', which might be different (yes we've seen this happen)
|
||||||
|
if automationPolicyShadows(i, aps) >= j {
|
||||||
|
aps = append(aps[:i], aps[i+1:]...)
|
||||||
|
i--
|
||||||
|
continue outer
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
aps[i].Subjects = append(aps[i].Subjects, aps[j].Subjects...)
|
// avoid repeated subjects
|
||||||
|
for _, subj := range aps[j].Subjects {
|
||||||
|
if !sliceContains(aps[i].Subjects, subj) {
|
||||||
|
aps[i].Subjects = append(aps[i].Subjects, subj)
|
||||||
|
}
|
||||||
|
}
|
||||||
aps = append(aps[:j], aps[j+1:]...)
|
aps = append(aps[:j], aps[j+1:]...)
|
||||||
|
j--
|
||||||
}
|
}
|
||||||
i--
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensure any catch-all policies go last
|
|
||||||
sort.SliceStable(aps, func(i, j int) bool {
|
|
||||||
return len(aps[i].Subjects) > len(aps[j].Subjects)
|
|
||||||
})
|
|
||||||
|
|
||||||
return aps
|
return aps
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// automationPolicyIsSubset returns true if a's subjects are a subset
|
||||||
|
// of b's subjects.
|
||||||
|
func automationPolicyIsSubset(a, b *caddytls.AutomationPolicy) bool {
|
||||||
|
if len(b.Subjects) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if len(a.Subjects) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, aSubj := range a.Subjects {
|
||||||
|
var inSuperset bool
|
||||||
|
for _, bSubj := range b.Subjects {
|
||||||
|
if certmagic.MatchWildcard(aSubj, bSubj) {
|
||||||
|
inSuperset = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !inSuperset {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// automationPolicyShadows returns the index of a policy that aps[i] shadows;
|
||||||
|
// in other words, for all policies after position i, if that policy covers
|
||||||
|
// the same subjects but is less specific, that policy's position is returned,
|
||||||
|
// or -1 if no shadowing is found. For example, if policy i is for
|
||||||
|
// "foo.example.com" and policy i+2 is for "*.example.com", then i+2 will be
|
||||||
|
// returned, since that policy is shadowed by i, which is in front.
|
||||||
|
func automationPolicyShadows(i int, aps []*caddytls.AutomationPolicy) int {
|
||||||
|
for j := i + 1; j < len(aps); j++ {
|
||||||
|
if automationPolicyIsSubset(aps[i], aps[j]) {
|
||||||
|
return j
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
// subjectQualifiesForPublicCert is like certmagic.SubjectQualifiesForPublicCert() except
|
||||||
|
// that this allows domains with multiple wildcard levels like '*.*.example.com' to qualify
|
||||||
|
// if the automation policy has OnDemand enabled (i.e. this function is more lenient).
|
||||||
|
func subjectQualifiesForPublicCert(ap *caddytls.AutomationPolicy, subj string) bool {
|
||||||
|
return !certmagic.SubjectIsIP(subj) &&
|
||||||
|
!certmagic.SubjectIsInternal(subj) &&
|
||||||
|
(strings.Count(subj, "*.") < 2 || ap.OnDemand)
|
||||||
|
}
|
||||||
|
|
||||||
|
func automationPolicyHasAllPublicNames(ap *caddytls.AutomationPolicy) bool {
|
||||||
|
for _, subj := range ap.Subjects {
|
||||||
|
if !subjectQualifiesForPublicCert(ap, subj) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package httpcaddyfile
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAutomationPolicyIsSubset(t *testing.T) {
|
||||||
|
for i, test := range []struct {
|
||||||
|
a, b []string
|
||||||
|
expect bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
a: []string{"example.com"},
|
||||||
|
b: []string{},
|
||||||
|
expect: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
a: []string{},
|
||||||
|
b: []string{"example.com"},
|
||||||
|
expect: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
a: []string{"foo.example.com"},
|
||||||
|
b: []string{"*.example.com"},
|
||||||
|
expect: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
a: []string{"foo.example.com"},
|
||||||
|
b: []string{"foo.example.com"},
|
||||||
|
expect: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
a: []string{"foo.example.com"},
|
||||||
|
b: []string{"example.com"},
|
||||||
|
expect: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
a: []string{"example.com", "foo.example.com"},
|
||||||
|
b: []string{"*.com", "*.*.com"},
|
||||||
|
expect: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
a: []string{"example.com", "foo.example.com"},
|
||||||
|
b: []string{"*.com"},
|
||||||
|
expect: false,
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
apA := &caddytls.AutomationPolicy{Subjects: test.a}
|
||||||
|
apB := &caddytls.AutomationPolicy{Subjects: test.b}
|
||||||
|
if actual := automationPolicyIsSubset(apA, apB); actual != test.expect {
|
||||||
|
t.Errorf("Test %d: Expected %t but got %t (A: %v B: %v)", i, test.expect, actual, test.a, test.b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,173 @@
|
|||||||
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package caddyconfig
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"crypto/x509"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/caddyserver/caddy/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
caddy.RegisterModule(HTTPLoader{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTTPLoader can load Caddy configs over HTTP(S). It can adapt the config
|
||||||
|
// based on the Content-Type header of the HTTP response.
|
||||||
|
type HTTPLoader struct {
|
||||||
|
// The method for the request. Default: GET
|
||||||
|
Method string `json:"method,omitempty"`
|
||||||
|
|
||||||
|
// The URL of the request.
|
||||||
|
URL string `json:"url,omitempty"`
|
||||||
|
|
||||||
|
// HTTP headers to add to the request.
|
||||||
|
Headers http.Header `json:"header,omitempty"`
|
||||||
|
|
||||||
|
// Maximum time allowed for a complete connection and request.
|
||||||
|
Timeout caddy.Duration `json:"timeout,omitempty"`
|
||||||
|
|
||||||
|
TLS *struct {
|
||||||
|
// Present this instance's managed remote identity credentials to the server.
|
||||||
|
UseServerIdentity bool `json:"use_server_identity,omitempty"`
|
||||||
|
|
||||||
|
// PEM-encoded client certificate filename to present to the server.
|
||||||
|
ClientCertificateFile string `json:"client_certificate_file,omitempty"`
|
||||||
|
|
||||||
|
// PEM-encoded key to use with the client certificate.
|
||||||
|
ClientCertificateKeyFile string `json:"client_certificate_key_file,omitempty"`
|
||||||
|
|
||||||
|
// List of PEM-encoded CA certificate files to add to the same trust
|
||||||
|
// store as RootCAPool (or root_ca_pool in the JSON).
|
||||||
|
RootCAPEMFiles []string `json:"root_ca_pem_files,omitempty"`
|
||||||
|
} `json:"tls,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CaddyModule returns the Caddy module information.
|
||||||
|
func (HTTPLoader) CaddyModule() caddy.ModuleInfo {
|
||||||
|
return caddy.ModuleInfo{
|
||||||
|
ID: "caddy.config_loaders.http",
|
||||||
|
New: func() caddy.Module { return new(HTTPLoader) },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadConfig loads a Caddy config.
|
||||||
|
func (hl HTTPLoader) LoadConfig(ctx caddy.Context) ([]byte, error) {
|
||||||
|
repl := caddy.NewReplacer()
|
||||||
|
|
||||||
|
client, err := hl.makeClient(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
method := repl.ReplaceAll(hl.Method, "")
|
||||||
|
if method == "" {
|
||||||
|
method = http.MethodGet
|
||||||
|
}
|
||||||
|
|
||||||
|
url := repl.ReplaceAll(hl.URL, "")
|
||||||
|
req, err := http.NewRequest(method, url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for key, vals := range hl.Headers {
|
||||||
|
for _, val := range vals {
|
||||||
|
req.Header.Add(repl.ReplaceAll(key, ""), repl.ReplaceKnown(val, ""))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode >= 400 {
|
||||||
|
return nil, fmt.Errorf("server responded with HTTP %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
result, warnings, err := adaptByContentType(resp.Header.Get("Content-Type"), body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, warn := range warnings {
|
||||||
|
ctx.Logger(hl).Warn(warn.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hl HTTPLoader) makeClient(ctx caddy.Context) (*http.Client, error) {
|
||||||
|
client := &http.Client{
|
||||||
|
Timeout: time.Duration(hl.Timeout),
|
||||||
|
}
|
||||||
|
|
||||||
|
if hl.TLS != nil {
|
||||||
|
var tlsConfig *tls.Config
|
||||||
|
|
||||||
|
// client authentication
|
||||||
|
if hl.TLS.UseServerIdentity {
|
||||||
|
certs, err := ctx.IdentityCredentials(ctx.Logger(hl))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("getting server identity credentials: %v", err)
|
||||||
|
}
|
||||||
|
if tlsConfig == nil {
|
||||||
|
tlsConfig = new(tls.Config)
|
||||||
|
}
|
||||||
|
tlsConfig.Certificates = certs
|
||||||
|
} else if hl.TLS.ClientCertificateFile != "" && hl.TLS.ClientCertificateKeyFile != "" {
|
||||||
|
cert, err := tls.LoadX509KeyPair(hl.TLS.ClientCertificateFile, hl.TLS.ClientCertificateKeyFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if tlsConfig == nil {
|
||||||
|
tlsConfig = new(tls.Config)
|
||||||
|
}
|
||||||
|
tlsConfig.Certificates = []tls.Certificate{cert}
|
||||||
|
}
|
||||||
|
|
||||||
|
// trusted server certs
|
||||||
|
if len(hl.TLS.RootCAPEMFiles) > 0 {
|
||||||
|
rootPool := x509.NewCertPool()
|
||||||
|
for _, pemFile := range hl.TLS.RootCAPEMFiles {
|
||||||
|
pemData, err := os.ReadFile(pemFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed reading ca cert: %v", err)
|
||||||
|
}
|
||||||
|
rootPool.AppendCertsFromPEM(pemData)
|
||||||
|
}
|
||||||
|
if tlsConfig == nil {
|
||||||
|
tlsConfig = new(tls.Config)
|
||||||
|
}
|
||||||
|
tlsConfig.RootCAs = rootPool
|
||||||
|
}
|
||||||
|
|
||||||
|
client.Transport = &http.Transport{TLSClientConfig: tlsConfig}
|
||||||
|
}
|
||||||
|
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ caddy.ConfigLoader = (*HTTPLoader)(nil)
|
||||||
+100
-38
@@ -58,6 +58,10 @@ func (al adminLoad) Routes() []caddy.AdminRoute {
|
|||||||
Pattern: "/load",
|
Pattern: "/load",
|
||||||
Handler: caddy.AdminHandlerFunc(al.handleLoad),
|
Handler: caddy.AdminHandlerFunc(al.handleLoad),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Pattern: "/adapt",
|
||||||
|
Handler: caddy.AdminHandlerFunc(al.handleAdapt),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,8 +73,8 @@ func (al adminLoad) Routes() []caddy.AdminRoute {
|
|||||||
func (adminLoad) handleLoad(w http.ResponseWriter, r *http.Request) error {
|
func (adminLoad) handleLoad(w http.ResponseWriter, r *http.Request) error {
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
return caddy.APIError{
|
return caddy.APIError{
|
||||||
Code: http.StatusMethodNotAllowed,
|
HTTPStatus: http.StatusMethodNotAllowed,
|
||||||
Err: fmt.Errorf("method not allowed"),
|
Err: fmt.Errorf("method not allowed"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,8 +85,8 @@ func (adminLoad) handleLoad(w http.ResponseWriter, r *http.Request) error {
|
|||||||
_, err := io.Copy(buf, r.Body)
|
_, err := io.Copy(buf, r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return caddy.APIError{
|
return caddy.APIError{
|
||||||
Code: http.StatusBadRequest,
|
HTTPStatus: http.StatusBadRequest,
|
||||||
Err: fmt.Errorf("reading request body: %v", err),
|
Err: fmt.Errorf("reading request body: %v", err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
body := buf.Bytes()
|
body := buf.Bytes()
|
||||||
@@ -90,45 +94,21 @@ func (adminLoad) handleLoad(w http.ResponseWriter, r *http.Request) error {
|
|||||||
// if the config is formatted other than Caddy's native
|
// if the config is formatted other than Caddy's native
|
||||||
// JSON, we need to adapt it before loading it
|
// JSON, we need to adapt it before loading it
|
||||||
if ctHeader := r.Header.Get("Content-Type"); ctHeader != "" {
|
if ctHeader := r.Header.Get("Content-Type"); ctHeader != "" {
|
||||||
ct, _, err := mime.ParseMediaType(ctHeader)
|
result, warnings, err := adaptByContentType(ctHeader, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return caddy.APIError{
|
return caddy.APIError{
|
||||||
Code: http.StatusBadRequest,
|
HTTPStatus: http.StatusBadRequest,
|
||||||
Err: fmt.Errorf("invalid Content-Type: %v", err),
|
Err: err,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !strings.HasSuffix(ct, "/json") {
|
if len(warnings) > 0 {
|
||||||
slashIdx := strings.Index(ct, "/")
|
respBody, err := json.Marshal(warnings)
|
||||||
if slashIdx < 0 {
|
|
||||||
return caddy.APIError{
|
|
||||||
Code: http.StatusBadRequest,
|
|
||||||
Err: fmt.Errorf("malformed Content-Type"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
adapterName := ct[slashIdx+1:]
|
|
||||||
cfgAdapter := GetAdapter(adapterName)
|
|
||||||
if cfgAdapter == nil {
|
|
||||||
return caddy.APIError{
|
|
||||||
Code: http.StatusBadRequest,
|
|
||||||
Err: fmt.Errorf("unrecognized config adapter '%s'", adapterName),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result, warnings, err := cfgAdapter.Adapt(body, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return caddy.APIError{
|
caddy.Log().Named("admin.api.load").Error(err.Error())
|
||||||
Code: http.StatusBadRequest,
|
|
||||||
Err: fmt.Errorf("adapting config using %s adapter: %v", adapterName, err),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if len(warnings) > 0 {
|
_, _ = w.Write(respBody)
|
||||||
respBody, err := json.Marshal(warnings)
|
|
||||||
if err != nil {
|
|
||||||
caddy.Log().Named("admin.api.load").Error(err.Error())
|
|
||||||
}
|
|
||||||
_, _ = w.Write(respBody)
|
|
||||||
}
|
|
||||||
body = result
|
|
||||||
}
|
}
|
||||||
|
body = result
|
||||||
}
|
}
|
||||||
|
|
||||||
forceReload := r.Header.Get("Cache-Control") == "must-revalidate"
|
forceReload := r.Header.Get("Cache-Control") == "must-revalidate"
|
||||||
@@ -136,8 +116,8 @@ func (adminLoad) handleLoad(w http.ResponseWriter, r *http.Request) error {
|
|||||||
err = caddy.Load(body, forceReload)
|
err = caddy.Load(body, forceReload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return caddy.APIError{
|
return caddy.APIError{
|
||||||
Code: http.StatusBadRequest,
|
HTTPStatus: http.StatusBadRequest,
|
||||||
Err: fmt.Errorf("loading config: %v", err),
|
Err: fmt.Errorf("loading config: %v", err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,6 +126,88 @@ func (adminLoad) handleLoad(w http.ResponseWriter, r *http.Request) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handleAdapt adapts the given Caddy config to JSON and responds with the result.
|
||||||
|
func (adminLoad) handleAdapt(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
return caddy.APIError{
|
||||||
|
HTTPStatus: http.StatusMethodNotAllowed,
|
||||||
|
Err: fmt.Errorf("method not allowed"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := bufPool.Get().(*bytes.Buffer)
|
||||||
|
buf.Reset()
|
||||||
|
defer bufPool.Put(buf)
|
||||||
|
|
||||||
|
_, err := io.Copy(buf, r.Body)
|
||||||
|
if err != nil {
|
||||||
|
return caddy.APIError{
|
||||||
|
HTTPStatus: http.StatusBadRequest,
|
||||||
|
Err: fmt.Errorf("reading request body: %v", err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result, warnings, err := adaptByContentType(r.Header.Get("Content-Type"), buf.Bytes())
|
||||||
|
if err != nil {
|
||||||
|
return caddy.APIError{
|
||||||
|
HTTPStatus: http.StatusBadRequest,
|
||||||
|
Err: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
out := struct {
|
||||||
|
Warnings []Warning `json:"warnings,omitempty"`
|
||||||
|
Result json.RawMessage `json:"result"`
|
||||||
|
}{
|
||||||
|
Warnings: warnings,
|
||||||
|
Result: result,
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
return json.NewEncoder(w).Encode(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// adaptByContentType adapts body to Caddy JSON using the adapter specified by contentType.
|
||||||
|
// If contentType is empty or ends with "/json", the input will be returned, as a no-op.
|
||||||
|
func adaptByContentType(contentType string, body []byte) ([]byte, []Warning, error) {
|
||||||
|
// assume JSON as the default
|
||||||
|
if contentType == "" {
|
||||||
|
return body, nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ct, _, err := mime.ParseMediaType(contentType)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, caddy.APIError{
|
||||||
|
HTTPStatus: http.StatusBadRequest,
|
||||||
|
Err: fmt.Errorf("invalid Content-Type: %v", err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if already JSON, no need to adapt
|
||||||
|
if strings.HasSuffix(ct, "/json") {
|
||||||
|
return body, nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// adapter name should be suffix of MIME type
|
||||||
|
slashIdx := strings.Index(ct, "/")
|
||||||
|
if slashIdx < 0 {
|
||||||
|
return nil, nil, fmt.Errorf("malformed Content-Type")
|
||||||
|
}
|
||||||
|
|
||||||
|
adapterName := ct[slashIdx+1:]
|
||||||
|
cfgAdapter := GetAdapter(adapterName)
|
||||||
|
if cfgAdapter == nil {
|
||||||
|
return nil, nil, fmt.Errorf("unrecognized config adapter '%s'", adapterName)
|
||||||
|
}
|
||||||
|
|
||||||
|
result, warnings, err := cfgAdapter.Adapt(body, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, fmt.Errorf("adapting config using %s adapter: %v", adapterName, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, warnings, nil
|
||||||
|
}
|
||||||
|
|
||||||
var bufPool = sync.Pool{
|
var bufPool = sync.Pool{
|
||||||
New: func() interface{} {
|
New: func() interface{} {
|
||||||
return new(bytes.Buffer)
|
return new(bytes.Buffer)
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
-----BEGIN CERTIFICATE-----
|
||||||
|
MIIDSzCCAjOgAwIBAgIUfIRObjWNUA4jxQ/0x8BOCvE2Vw4wDQYJKoZIhvcNAQEL
|
||||||
|
BQAwFjEUMBIGA1UEAwwLRWFzeS1SU0EgQ0EwHhcNMTkwODI4MTYyNTU5WhcNMjkw
|
||||||
|
ODI1MTYyNTU5WjAWMRQwEgYDVQQDDAtFYXN5LVJTQSBDQTCCASIwDQYJKoZIhvcN
|
||||||
|
AQEBBQADggEPADCCAQoCggEBAK5m5elxhQfMp/3aVJ4JnpN9PUSz6LlP6LePAPFU
|
||||||
|
7gqohVVFVtDkChJAG3FNkNQNlieVTja/bgH9IcC6oKbROwdY1h0MvNV8AHHigvl0
|
||||||
|
3WuJD8g2ReVFXXwsnrPmKXCFzQyMI6TYk3m2gYrXsZOU1GLnfMRC3KAMRgE2F45t
|
||||||
|
wOs9hqG169YJ6mM2eQjzjCHWI6S2/iUYvYxRkCOlYUbLsMD/AhgAf1plzg6LPqNx
|
||||||
|
tdlwxZnA0ytgkmhK67HtzJu0+ovUCsMv0RwcMhsEo9T8nyFAGt9XLZ63X5WpBCTU
|
||||||
|
ApaAUhnG0XnerjmUWb6eUWw4zev54sEfY5F3x002iQaW6cECAwEAAaOBkDCBjTAd
|
||||||
|
BgNVHQ4EFgQU4CBUbZsS2GaNIkGRz/cBsD5ivjswUQYDVR0jBEowSIAU4CBUbZsS
|
||||||
|
2GaNIkGRz/cBsD5ivjuhGqQYMBYxFDASBgNVBAMMC0Vhc3ktUlNBIENBghR8hE5u
|
||||||
|
NY1QDiPFD/THwE4K8TZXDjAMBgNVHRMEBTADAQH/MAsGA1UdDwQEAwIBBjANBgkq
|
||||||
|
hkiG9w0BAQsFAAOCAQEAKB3V4HIzoiO/Ch6WMj9bLJ2FGbpkMrcb/Eq01hT5zcfK
|
||||||
|
D66lVS1MlK+cRL446Z2b2KDP1oFyVs+qmrmtdwrWgD+nfe2sBmmIHo9m9KygMkEO
|
||||||
|
fG3MghGTEcS+0cTKEcoHYWYyOqQh6jnedXY8Cdm4GM1hAc9MiL3/sqV8YCVSLNnk
|
||||||
|
oNysmr06/rZ0MCUZPGUtRmfd0heWhrfzAKw2HLgX+RAmpOE2MZqWcjvqKGyaRiaZ
|
||||||
|
ks4nJkP6521aC2Lgp0HhCz1j8/uQ5ldoDszCnu/iro0NAsNtudTMD+YoLQxLqdle
|
||||||
|
Ih6CW+illc2VdXwj7mn6J04yns9jfE2jRjW/yTLFuQ==
|
||||||
|
-----END CERTIFICATE-----
|
||||||
+106
-45
@@ -7,13 +7,14 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/cookiejar"
|
"net/http/cookiejar"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -55,8 +56,9 @@ var (
|
|||||||
|
|
||||||
// Tester represents an instance of a test client.
|
// Tester represents an instance of a test client.
|
||||||
type Tester struct {
|
type Tester struct {
|
||||||
Client *http.Client
|
Client *http.Client
|
||||||
t *testing.T
|
configLoaded bool
|
||||||
|
t *testing.T
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTester will create a new testing client with an attached cookie jar
|
// NewTester will create a new testing client with an attached cookie jar
|
||||||
@@ -73,7 +75,8 @@ func NewTester(t *testing.T) *Tester {
|
|||||||
Jar: jar,
|
Jar: jar,
|
||||||
Timeout: Default.TestRequestTimeout,
|
Timeout: Default.TestRequestTimeout,
|
||||||
},
|
},
|
||||||
t: t,
|
configLoaded: false,
|
||||||
|
t: t,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,6 +99,10 @@ func (tc *Tester) InitServer(rawConfig string, configType string) {
|
|||||||
tc.t.Logf("failed to load config: %s", err)
|
tc.t.Logf("failed to load config: %s", err)
|
||||||
tc.t.Fail()
|
tc.t.Fail()
|
||||||
}
|
}
|
||||||
|
if err := tc.ensureConfigRunning(rawConfig, configType); err != nil {
|
||||||
|
tc.t.Logf("failed ensurng config is running: %s", err)
|
||||||
|
tc.t.Fail()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitServer this will configure the server with a configurion of a specific
|
// InitServer this will configure the server with a configurion of a specific
|
||||||
@@ -114,17 +121,18 @@ func (tc *Tester) initServer(rawConfig string, configType string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tc.t.Cleanup(func() {
|
tc.t.Cleanup(func() {
|
||||||
if tc.t.Failed() {
|
if tc.t.Failed() && tc.configLoaded {
|
||||||
|
|
||||||
res, err := http.Get(fmt.Sprintf("http://localhost:%d/config/", Default.AdminPort))
|
res, err := http.Get(fmt.Sprintf("http://localhost:%d/config/", Default.AdminPort))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tc.t.Log("unable to read the current config")
|
tc.t.Log("unable to read the current config")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
body, err := ioutil.ReadAll(res.Body)
|
body, _ := io.ReadAll(res.Body)
|
||||||
|
|
||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
json.Indent(&out, body, "", " ")
|
_ = json.Indent(&out, body, "", " ")
|
||||||
tc.t.Logf("----------- failed with config -----------\n%s", out.String())
|
tc.t.Logf("----------- failed with config -----------\n%s", out.String())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -154,7 +162,7 @@ func (tc *Tester) initServer(rawConfig string, configType string) error {
|
|||||||
timeElapsed(start, "caddytest: config load time")
|
timeElapsed(start, "caddytest: config load time")
|
||||||
|
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
body, err := ioutil.ReadAll(res.Body)
|
body, err := io.ReadAll(res.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tc.t.Errorf("unable to read response. %s", err)
|
tc.t.Errorf("unable to read response. %s", err)
|
||||||
return err
|
return err
|
||||||
@@ -164,23 +172,61 @@ func (tc *Tester) initServer(rawConfig string, configType string) error {
|
|||||||
return configLoadError{Response: string(body)}
|
return configLoadError{Response: string(body)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tc.configLoaded = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var hasValidated bool
|
func (tc *Tester) ensureConfigRunning(rawConfig string, configType string) error {
|
||||||
var arePrerequisitesValid bool
|
expectedBytes := []byte(prependCaddyFilePath(rawConfig))
|
||||||
|
if configType != "json" {
|
||||||
func validateTestPrerequisites() error {
|
adapter := caddyconfig.GetAdapter(configType)
|
||||||
|
if adapter == nil {
|
||||||
if hasValidated {
|
return fmt.Errorf("adapter of config type is missing: %s", configType)
|
||||||
if !arePrerequisitesValid {
|
|
||||||
return errors.New("caddy integration prerequisites failed. see first error")
|
|
||||||
}
|
}
|
||||||
return nil
|
expectedBytes, _, _ = adapter.Adapt([]byte(rawConfig), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
hasValidated = true
|
var expected interface{}
|
||||||
arePrerequisitesValid = false
|
err := json.Unmarshal(expectedBytes, &expected)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
client := &http.Client{
|
||||||
|
Timeout: Default.LoadRequestTimeout,
|
||||||
|
}
|
||||||
|
|
||||||
|
fetchConfig := func(client *http.Client) interface{} {
|
||||||
|
resp, err := client.Get(fmt.Sprintf("http://localhost:%d/config/", Default.AdminPort))
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
actualBytes, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var actual interface{}
|
||||||
|
err = json.Unmarshal(actualBytes, &actual)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return actual
|
||||||
|
}
|
||||||
|
|
||||||
|
for retries := 4; retries > 0; retries-- {
|
||||||
|
if reflect.DeepEqual(expected, fetchConfig(client)) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
}
|
||||||
|
tc.t.Errorf("POSTed configuration isn't active")
|
||||||
|
return errors.New("EnsureConfigRunning: POSTed configuration isn't active")
|
||||||
|
}
|
||||||
|
|
||||||
|
// validateTestPrerequisites ensures the certificates are available in the
|
||||||
|
// designated path and Caddy sub-process is running.
|
||||||
|
func validateTestPrerequisites() error {
|
||||||
|
|
||||||
// check certificates are found
|
// check certificates are found
|
||||||
for _, certName := range Default.Certifcates {
|
for _, certName := range Default.Certifcates {
|
||||||
@@ -196,20 +242,14 @@ func validateTestPrerequisites() error {
|
|||||||
caddycmd.Main()
|
caddycmd.Main()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// wait for caddy to start
|
// wait for caddy to start serving the initial config
|
||||||
retries := 4
|
for retries := 4; retries > 0 && isCaddyAdminRunning() != nil; retries-- {
|
||||||
for ; retries > 0 && isCaddyAdminRunning() != nil; retries-- {
|
|
||||||
time.Sleep(10 * time.Millisecond)
|
time.Sleep(10 * time.Millisecond)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// assert that caddy is running
|
// one more time to return the error
|
||||||
if err := isCaddyAdminRunning(); err != nil {
|
return isCaddyAdminRunning()
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
arePrerequisitesValid = true
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func isCaddyAdminRunning() error {
|
func isCaddyAdminRunning() error {
|
||||||
@@ -217,10 +257,11 @@ func isCaddyAdminRunning() error {
|
|||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: Default.LoadRequestTimeout,
|
Timeout: Default.LoadRequestTimeout,
|
||||||
}
|
}
|
||||||
_, err := client.Get(fmt.Sprintf("http://localhost:%d/config/", Default.AdminPort))
|
resp, err := client.Get(fmt.Sprintf("http://localhost:%d/config/", Default.AdminPort))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("caddy integration test caddy server not running. Expected to be listening on localhost:2019")
|
return fmt.Errorf("caddy integration test caddy server not running. Expected to be listening on localhost:%d", Default.AdminPort)
|
||||||
}
|
}
|
||||||
|
resp.Body.Close()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -268,7 +309,7 @@ func CreateTestingTransport() *http.Transport {
|
|||||||
IdleConnTimeout: 90 * time.Second,
|
IdleConnTimeout: 90 * time.Second,
|
||||||
TLSHandshakeTimeout: 5 * time.Second,
|
TLSHandshakeTimeout: 5 * time.Second,
|
||||||
ExpectContinueTimeout: 1 * time.Second,
|
ExpectContinueTimeout: 1 * time.Second,
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, //nolint:gosec
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -309,35 +350,46 @@ func (tc *Tester) AssertRedirect(requestURI string, expectedToLocation string, e
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
tc.t.Errorf("requesting \"%s\" expected location: \"%s\" but got error: %s", requestURI, expectedToLocation, err)
|
tc.t.Errorf("requesting \"%s\" expected location: \"%s\" but got error: %s", requestURI, expectedToLocation, err)
|
||||||
}
|
}
|
||||||
|
if loc == nil && expectedToLocation != "" {
|
||||||
if expectedToLocation != loc.String() {
|
tc.t.Errorf("requesting \"%s\" expected a Location header, but didn't get one", requestURI)
|
||||||
tc.t.Errorf("requesting \"%s\" expected location: \"%s\" but got \"%s\"", requestURI, expectedToLocation, loc.String())
|
}
|
||||||
|
if loc != nil {
|
||||||
|
if expectedToLocation != loc.String() {
|
||||||
|
tc.t.Errorf("requesting \"%s\" expected location: \"%s\" but got \"%s\"", requestURI, expectedToLocation, loc.String())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp
|
return resp
|
||||||
}
|
}
|
||||||
|
|
||||||
// AssertAdapt adapts a config and then tests it against an expected result
|
// CompareAdapt adapts a config and then compares it against an expected result
|
||||||
func AssertAdapt(t *testing.T, rawConfig string, adapterName string, expectedResponse string) {
|
func CompareAdapt(t *testing.T, filename, rawConfig string, adapterName string, expectedResponse string) bool {
|
||||||
|
|
||||||
cfgAdapter := caddyconfig.GetAdapter(adapterName)
|
cfgAdapter := caddyconfig.GetAdapter(adapterName)
|
||||||
if cfgAdapter == nil {
|
if cfgAdapter == nil {
|
||||||
t.Errorf("unrecognized config adapter '%s'", adapterName)
|
t.Logf("unrecognized config adapter '%s'", adapterName)
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
options := make(map[string]interface{})
|
options := make(map[string]interface{})
|
||||||
options["pretty"] = "true"
|
|
||||||
|
|
||||||
result, warnings, err := cfgAdapter.Adapt([]byte(rawConfig), options)
|
result, warnings, err := cfgAdapter.Adapt([]byte(rawConfig), options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("adapting config using %s adapter: %v", adapterName, err)
|
t.Logf("adapting config using %s adapter: %v", adapterName, err)
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// prettify results to keep tests human-manageable
|
||||||
|
var prettyBuf bytes.Buffer
|
||||||
|
err = json.Indent(&prettyBuf, result, "", "\t")
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
result = prettyBuf.Bytes()
|
||||||
|
|
||||||
if len(warnings) > 0 {
|
if len(warnings) > 0 {
|
||||||
for _, w := range warnings {
|
for _, w := range warnings {
|
||||||
t.Logf("warning: directive: %s : %s", w.Directive, w.Message)
|
t.Logf("warning: %s:%d: %s: %s", filename, w.Line, w.Directive, w.Message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -365,6 +417,15 @@ func AssertAdapt(t *testing.T, rawConfig string, adapterName string, expectedRes
|
|||||||
fmt.Printf(" + %s\n", d.Payload)
|
fmt.Printf(" + %s\n", d.Payload)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// AssertAdapt adapts a config and then tests it against an expected result
|
||||||
|
func AssertAdapt(t *testing.T, rawConfig string, adapterName string, expectedResponse string) {
|
||||||
|
ok := CompareAdapt(t, "Caddyfile", rawConfig, adapterName, expectedResponse)
|
||||||
|
if !ok {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -410,14 +471,14 @@ func (tc *Tester) AssertResponse(req *http.Request, expectedStatusCode int, expe
|
|||||||
resp := tc.AssertResponseCode(req, expectedStatusCode)
|
resp := tc.AssertResponseCode(req, expectedStatusCode)
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
bytes, err := ioutil.ReadAll(resp.Body)
|
bytes, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tc.t.Fatalf("unable to read the response body %s", err)
|
tc.t.Fatalf("unable to read the response body %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
body := string(bytes)
|
body := string(bytes)
|
||||||
|
|
||||||
if !strings.Contains(body, expectedBody) {
|
if body != expectedBody {
|
||||||
tc.t.Errorf("requesting \"%s\" expected response body \"%s\" but got \"%s\"", req.RequestURI, expectedBody, body)
|
tc.t.Errorf("requesting \"%s\" expected response body \"%s\" but got \"%s\"", req.RequestURI, expectedBody, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,125 @@
|
|||||||
|
package integration
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/caddyserver/caddy/v2/caddytest"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAutoHTTPtoHTTPSRedirectsImplicitPort(t *testing.T) {
|
||||||
|
tester := caddytest.NewTester(t)
|
||||||
|
tester.InitServer(`
|
||||||
|
{
|
||||||
|
http_port 9080
|
||||||
|
https_port 9443
|
||||||
|
}
|
||||||
|
localhost
|
||||||
|
respond "Yahaha! You found me!"
|
||||||
|
`, "caddyfile")
|
||||||
|
|
||||||
|
tester.AssertRedirect("http://localhost:9080/", "https://localhost/", http.StatusPermanentRedirect)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAutoHTTPtoHTTPSRedirectsExplicitPortSameAsHTTPSPort(t *testing.T) {
|
||||||
|
tester := caddytest.NewTester(t)
|
||||||
|
tester.InitServer(`
|
||||||
|
{
|
||||||
|
http_port 9080
|
||||||
|
https_port 9443
|
||||||
|
}
|
||||||
|
localhost:9443
|
||||||
|
respond "Yahaha! You found me!"
|
||||||
|
`, "caddyfile")
|
||||||
|
|
||||||
|
tester.AssertRedirect("http://localhost:9080/", "https://localhost/", http.StatusPermanentRedirect)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAutoHTTPtoHTTPSRedirectsExplicitPortDifferentFromHTTPSPort(t *testing.T) {
|
||||||
|
tester := caddytest.NewTester(t)
|
||||||
|
tester.InitServer(`
|
||||||
|
{
|
||||||
|
http_port 9080
|
||||||
|
https_port 9443
|
||||||
|
}
|
||||||
|
localhost:1234
|
||||||
|
respond "Yahaha! You found me!"
|
||||||
|
`, "caddyfile")
|
||||||
|
|
||||||
|
tester.AssertRedirect("http://localhost:9080/", "https://localhost:1234/", http.StatusPermanentRedirect)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAutoHTTPRedirectsWithHTTPListenerFirstInAddresses(t *testing.T) {
|
||||||
|
tester := caddytest.NewTester(t)
|
||||||
|
tester.InitServer(`
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"http_port": 9080,
|
||||||
|
"https_port": 9443,
|
||||||
|
"servers": {
|
||||||
|
"ingress_server": {
|
||||||
|
"listen": [
|
||||||
|
":9080",
|
||||||
|
":9443"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": ["localhost"]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`, "json")
|
||||||
|
tester.AssertRedirect("http://localhost:9080/", "https://localhost/", http.StatusPermanentRedirect)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAutoHTTPRedirectsInsertedBeforeUserDefinedCatchAll(t *testing.T) {
|
||||||
|
tester := caddytest.NewTester(t)
|
||||||
|
tester.InitServer(`
|
||||||
|
{
|
||||||
|
http_port 9080
|
||||||
|
https_port 9443
|
||||||
|
local_certs
|
||||||
|
}
|
||||||
|
http://:9080 {
|
||||||
|
respond "Foo"
|
||||||
|
}
|
||||||
|
http://baz.localhost:9080 {
|
||||||
|
respond "Baz"
|
||||||
|
}
|
||||||
|
bar.localhost {
|
||||||
|
respond "Bar"
|
||||||
|
}
|
||||||
|
`, "caddyfile")
|
||||||
|
tester.AssertRedirect("http://bar.localhost:9080/", "https://bar.localhost/", http.StatusPermanentRedirect)
|
||||||
|
tester.AssertGetResponse("http://foo.localhost:9080/", 200, "Foo")
|
||||||
|
tester.AssertGetResponse("http://baz.localhost:9080/", 200, "Baz")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAutoHTTPRedirectsInsertedBeforeUserDefinedCatchAllWithNoExplicitHTTPSite(t *testing.T) {
|
||||||
|
tester := caddytest.NewTester(t)
|
||||||
|
tester.InitServer(`
|
||||||
|
{
|
||||||
|
http_port 9080
|
||||||
|
https_port 9443
|
||||||
|
local_certs
|
||||||
|
}
|
||||||
|
http://:9080 {
|
||||||
|
respond "Foo"
|
||||||
|
}
|
||||||
|
bar.localhost {
|
||||||
|
respond "Bar"
|
||||||
|
}
|
||||||
|
`, "caddyfile")
|
||||||
|
tester.AssertRedirect("http://bar.localhost:9080/", "https://bar.localhost/", http.StatusPermanentRedirect)
|
||||||
|
tester.AssertGetResponse("http://foo.localhost:9080/", 200, "Foo")
|
||||||
|
tester.AssertGetResponse("http://baz.localhost:9080/", 200, "Foo")
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
{
|
||||||
|
auto_https disable_redirects
|
||||||
|
}
|
||||||
|
|
||||||
|
localhost
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":443"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"localhost"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"automatic_https": {
|
||||||
|
"disable_redirects": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
{
|
||||||
|
auto_https ignore_loaded_certs
|
||||||
|
}
|
||||||
|
|
||||||
|
localhost
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":443"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"localhost"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"automatic_https": {
|
||||||
|
"ignore_loaded_certificates": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
{
|
||||||
|
auto_https off
|
||||||
|
}
|
||||||
|
|
||||||
|
localhost
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":443"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"localhost"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"tls_connection_policies": [
|
||||||
|
{}
|
||||||
|
],
|
||||||
|
"automatic_https": {
|
||||||
|
"disable": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
example.com {
|
||||||
|
bind tcp6/[::]
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
"tcp6/[::]:443"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"example.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
:80
|
||||||
|
|
||||||
|
# All the options
|
||||||
|
encode gzip zstd {
|
||||||
|
minimum_length 256
|
||||||
|
match {
|
||||||
|
status 2xx 4xx 500
|
||||||
|
header Content-Type text/*
|
||||||
|
header Content-Type application/json*
|
||||||
|
header Content-Type application/javascript*
|
||||||
|
header Content-Type application/xhtml+xml*
|
||||||
|
header Content-Type application/atom+xml*
|
||||||
|
header Content-Type application/rss+xml*
|
||||||
|
header Content-Type image/svg+xml*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Long way with a block for each encoding
|
||||||
|
encode {
|
||||||
|
zstd
|
||||||
|
gzip 5
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":80"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"encodings": {
|
||||||
|
"gzip": {},
|
||||||
|
"zstd": {}
|
||||||
|
},
|
||||||
|
"handler": "encode",
|
||||||
|
"match": {
|
||||||
|
"headers": {
|
||||||
|
"Content-Type": [
|
||||||
|
"text/*",
|
||||||
|
"application/json*",
|
||||||
|
"application/javascript*",
|
||||||
|
"application/xhtml+xml*",
|
||||||
|
"application/atom+xml*",
|
||||||
|
"application/rss+xml*",
|
||||||
|
"image/svg+xml*"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"status_code": [
|
||||||
|
2,
|
||||||
|
4,
|
||||||
|
500
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"minimum_length": 256,
|
||||||
|
"prefer": [
|
||||||
|
"gzip",
|
||||||
|
"zstd"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"encodings": {
|
||||||
|
"gzip": {
|
||||||
|
"level": 5
|
||||||
|
},
|
||||||
|
"zstd": {}
|
||||||
|
},
|
||||||
|
"handler": "encode",
|
||||||
|
"prefer": [
|
||||||
|
"zstd",
|
||||||
|
"gzip"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,138 @@
|
|||||||
|
example.com {
|
||||||
|
root * /srv
|
||||||
|
|
||||||
|
# Trigger errors for certain paths
|
||||||
|
error /private* "Unauthorized" 403
|
||||||
|
error /hidden* "Not found" 404
|
||||||
|
|
||||||
|
# Handle the error by serving an HTML page
|
||||||
|
handle_errors {
|
||||||
|
rewrite * /{http.error.status_code}.html
|
||||||
|
file_server
|
||||||
|
}
|
||||||
|
|
||||||
|
file_server
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":443"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"example.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "subroute",
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "vars",
|
||||||
|
"root": "/srv"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"error": "Unauthorized",
|
||||||
|
"handler": "error",
|
||||||
|
"status_code": 403
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"path": [
|
||||||
|
"/private*"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"error": "Not found",
|
||||||
|
"handler": "error",
|
||||||
|
"status_code": 404
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"path": [
|
||||||
|
"/hidden*"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "file_server",
|
||||||
|
"hide": [
|
||||||
|
"./Caddyfile"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"errors": {
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"example.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "subroute",
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"group": "group0",
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "rewrite",
|
||||||
|
"uri": "/{http.error.status_code}.html"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "file_server",
|
||||||
|
"hide": [
|
||||||
|
"./Caddyfile"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
example.com
|
||||||
|
|
||||||
|
@a expression {http.error.status_code} == 400
|
||||||
|
abort @a
|
||||||
|
|
||||||
|
@b expression {http.error.status_code} == "401"
|
||||||
|
abort @b
|
||||||
|
|
||||||
|
@c expression {http.error.status_code} == `402`
|
||||||
|
abort @c
|
||||||
|
|
||||||
|
@d expression "{http.error.status_code} == 403"
|
||||||
|
abort @d
|
||||||
|
|
||||||
|
@e expression `{http.error.status_code} == 404`
|
||||||
|
abort @e
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":443"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"example.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "subroute",
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"abort": true,
|
||||||
|
"handler": "static_response"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"expression": "{http.error.status_code} == 400"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"abort": true,
|
||||||
|
"handler": "static_response"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"expression": "{http.error.status_code} == \"401\""
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"abort": true,
|
||||||
|
"handler": "static_response"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"expression": "{http.error.status_code} == `402`"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"abort": true,
|
||||||
|
"handler": "static_response"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"expression": "{http.error.status_code} == 403"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"abort": true,
|
||||||
|
"handler": "static_response"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"expression": "{http.error.status_code} == 404"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
:80
|
||||||
|
|
||||||
|
file_server {
|
||||||
|
disable_canonical_uris
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":80"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"canonical_uris": false,
|
||||||
|
"handler": "file_server",
|
||||||
|
"hide": [
|
||||||
|
"./Caddyfile"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
:80
|
||||||
|
|
||||||
|
file_server {
|
||||||
|
pass_thru
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":80"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "file_server",
|
||||||
|
"hide": [
|
||||||
|
"./Caddyfile"
|
||||||
|
],
|
||||||
|
"pass_thru": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
:80
|
||||||
|
|
||||||
|
file_server {
|
||||||
|
precompressed zstd br gzip
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":80"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "file_server",
|
||||||
|
"hide": [
|
||||||
|
"./Caddyfile"
|
||||||
|
],
|
||||||
|
"precompressed": {
|
||||||
|
"br": {},
|
||||||
|
"gzip": {},
|
||||||
|
"zstd": {}
|
||||||
|
},
|
||||||
|
"precompressed_order": [
|
||||||
|
"zstd",
|
||||||
|
"br",
|
||||||
|
"gzip"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
localhost
|
||||||
|
|
||||||
|
root * /srv
|
||||||
|
|
||||||
|
handle /nope* {
|
||||||
|
file_server {
|
||||||
|
status 403
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handle /custom-status* {
|
||||||
|
file_server {
|
||||||
|
status {env.CUSTOM_STATUS}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":443"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"localhost"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "subroute",
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "vars",
|
||||||
|
"root": "/srv"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"group": "group2",
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "subroute",
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "file_server",
|
||||||
|
"hide": [
|
||||||
|
"./Caddyfile"
|
||||||
|
],
|
||||||
|
"status_code": "{env.CUSTOM_STATUS}"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"path": [
|
||||||
|
"/custom-status*"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"group": "group2",
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "subroute",
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "file_server",
|
||||||
|
"hide": [
|
||||||
|
"./Caddyfile"
|
||||||
|
],
|
||||||
|
"status_code": 403
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"path": [
|
||||||
|
"/nope*"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
app.example.com {
|
||||||
|
forward_auth authelia:9091 {
|
||||||
|
uri /api/verify?rd=https://authelia.example.com
|
||||||
|
copy_headers Remote-User Remote-Groups Remote-Name Remote-Email
|
||||||
|
}
|
||||||
|
|
||||||
|
reverse_proxy backend:8080
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":443"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"app.example.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "subroute",
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handle_response": [
|
||||||
|
{
|
||||||
|
"match": {
|
||||||
|
"status_code": [
|
||||||
|
2
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "headers",
|
||||||
|
"request": {
|
||||||
|
"set": {
|
||||||
|
"Remote-Email": [
|
||||||
|
"{http.reverse_proxy.header.Remote-Email}"
|
||||||
|
],
|
||||||
|
"Remote-Groups": [
|
||||||
|
"{http.reverse_proxy.header.Remote-Groups}"
|
||||||
|
],
|
||||||
|
"Remote-Name": [
|
||||||
|
"{http.reverse_proxy.header.Remote-Name}"
|
||||||
|
],
|
||||||
|
"Remote-User": [
|
||||||
|
"{http.reverse_proxy.header.Remote-User}"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handler": "reverse_proxy",
|
||||||
|
"headers": {
|
||||||
|
"request": {
|
||||||
|
"set": {
|
||||||
|
"X-Forwarded-Method": [
|
||||||
|
"{http.request.method}"
|
||||||
|
],
|
||||||
|
"X-Forwarded-Uri": [
|
||||||
|
"{http.request.uri}"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"rewrite": {
|
||||||
|
"method": "GET",
|
||||||
|
"uri": "/api/verify?rd=https://authelia.example.com"
|
||||||
|
},
|
||||||
|
"upstreams": [
|
||||||
|
{
|
||||||
|
"dial": "authelia:9091"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"handler": "reverse_proxy",
|
||||||
|
"upstreams": [
|
||||||
|
{
|
||||||
|
"dial": "backend:8080"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
:8881
|
||||||
|
|
||||||
|
forward_auth localhost:9000 {
|
||||||
|
uri /auth
|
||||||
|
copy_headers A>1 B C>3 {
|
||||||
|
D
|
||||||
|
E>5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":8881"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handle_response": [
|
||||||
|
{
|
||||||
|
"match": {
|
||||||
|
"status_code": [
|
||||||
|
2
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "headers",
|
||||||
|
"request": {
|
||||||
|
"set": {
|
||||||
|
"1": [
|
||||||
|
"{http.reverse_proxy.header.A}"
|
||||||
|
],
|
||||||
|
"3": [
|
||||||
|
"{http.reverse_proxy.header.C}"
|
||||||
|
],
|
||||||
|
"5": [
|
||||||
|
"{http.reverse_proxy.header.E}"
|
||||||
|
],
|
||||||
|
"B": [
|
||||||
|
"{http.reverse_proxy.header.B}"
|
||||||
|
],
|
||||||
|
"D": [
|
||||||
|
"{http.reverse_proxy.header.D}"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handler": "reverse_proxy",
|
||||||
|
"headers": {
|
||||||
|
"request": {
|
||||||
|
"set": {
|
||||||
|
"X-Forwarded-Method": [
|
||||||
|
"{http.request.method}"
|
||||||
|
],
|
||||||
|
"X-Forwarded-Uri": [
|
||||||
|
"{http.request.uri}"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"rewrite": {
|
||||||
|
"method": "GET",
|
||||||
|
"uri": "/auth"
|
||||||
|
},
|
||||||
|
"upstreams": [
|
||||||
|
{
|
||||||
|
"dial": "localhost:9000"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
{
|
||||||
|
debug
|
||||||
|
http_port 8080
|
||||||
|
https_port 8443
|
||||||
|
grace_period 5s
|
||||||
|
default_sni localhost
|
||||||
|
order root first
|
||||||
|
storage file_system {
|
||||||
|
root /data
|
||||||
|
}
|
||||||
|
acme_ca https://example.com
|
||||||
|
acme_ca_root /path/to/ca.crt
|
||||||
|
ocsp_stapling off
|
||||||
|
|
||||||
|
email test@example.com
|
||||||
|
admin off
|
||||||
|
on_demand_tls {
|
||||||
|
ask https://example.com
|
||||||
|
interval 30s
|
||||||
|
burst 20
|
||||||
|
}
|
||||||
|
local_certs
|
||||||
|
key_type ed25519
|
||||||
|
}
|
||||||
|
|
||||||
|
:80
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"admin": {
|
||||||
|
"disabled": true
|
||||||
|
},
|
||||||
|
"logging": {
|
||||||
|
"logs": {
|
||||||
|
"default": {
|
||||||
|
"level": "DEBUG"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"storage": {
|
||||||
|
"module": "file_system",
|
||||||
|
"root": "/data"
|
||||||
|
},
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"http_port": 8080,
|
||||||
|
"https_port": 8443,
|
||||||
|
"grace_period": 5000000000,
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":80"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"tls": {
|
||||||
|
"automation": {
|
||||||
|
"policies": [
|
||||||
|
{
|
||||||
|
"issuers": [
|
||||||
|
{
|
||||||
|
"module": "internal"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"key_type": "ed25519",
|
||||||
|
"disable_ocsp_stapling": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"on_demand": {
|
||||||
|
"rate_limit": {
|
||||||
|
"interval": 30000000000,
|
||||||
|
"burst": 20
|
||||||
|
},
|
||||||
|
"ask": "https://example.com"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"disable_ocsp_stapling": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
{
|
||||||
|
debug
|
||||||
|
http_port 8080
|
||||||
|
https_port 8443
|
||||||
|
default_sni localhost
|
||||||
|
order root first
|
||||||
|
storage file_system {
|
||||||
|
root /data
|
||||||
|
}
|
||||||
|
acme_ca https://example.com
|
||||||
|
acme_eab {
|
||||||
|
key_id 4K2scIVbBpNd-78scadB2g
|
||||||
|
mac_key abcdefghijklmnopqrstuvwx-abcdefghijklnopqrstuvwxyz12ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgh
|
||||||
|
}
|
||||||
|
acme_ca_root /path/to/ca.crt
|
||||||
|
email test@example.com
|
||||||
|
admin off
|
||||||
|
on_demand_tls {
|
||||||
|
ask https://example.com
|
||||||
|
interval 30s
|
||||||
|
burst 20
|
||||||
|
}
|
||||||
|
storage_clean_interval 7d
|
||||||
|
renew_interval 1d
|
||||||
|
|
||||||
|
key_type ed25519
|
||||||
|
}
|
||||||
|
|
||||||
|
:80
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"admin": {
|
||||||
|
"disabled": true
|
||||||
|
},
|
||||||
|
"logging": {
|
||||||
|
"logs": {
|
||||||
|
"default": {
|
||||||
|
"level": "DEBUG"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"storage": {
|
||||||
|
"module": "file_system",
|
||||||
|
"root": "/data"
|
||||||
|
},
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"http_port": 8080,
|
||||||
|
"https_port": 8443,
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":80"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"tls": {
|
||||||
|
"automation": {
|
||||||
|
"policies": [
|
||||||
|
{
|
||||||
|
"issuers": [
|
||||||
|
{
|
||||||
|
"ca": "https://example.com",
|
||||||
|
"email": "test@example.com",
|
||||||
|
"external_account": {
|
||||||
|
"key_id": "4K2scIVbBpNd-78scadB2g",
|
||||||
|
"mac_key": "abcdefghijklmnopqrstuvwx-abcdefghijklnopqrstuvwxyz12ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgh"
|
||||||
|
},
|
||||||
|
"module": "acme",
|
||||||
|
"trusted_roots_pem_files": [
|
||||||
|
"/path/to/ca.crt"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"key_type": "ed25519"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"on_demand": {
|
||||||
|
"rate_limit": {
|
||||||
|
"interval": 30000000000,
|
||||||
|
"burst": 20
|
||||||
|
},
|
||||||
|
"ask": "https://example.com"
|
||||||
|
},
|
||||||
|
"renew_interval": 86400000000000,
|
||||||
|
"storage_clean_interval": 604800000000000
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
{
|
||||||
|
debug
|
||||||
|
http_port 8080
|
||||||
|
https_port 8443
|
||||||
|
default_sni localhost
|
||||||
|
order root first
|
||||||
|
storage file_system {
|
||||||
|
root /data
|
||||||
|
}
|
||||||
|
acme_ca https://example.com
|
||||||
|
acme_ca_root /path/to/ca.crt
|
||||||
|
|
||||||
|
email test@example.com
|
||||||
|
admin {
|
||||||
|
origins localhost:2019 [::1]:2019 127.0.0.1:2019 192.168.10.128
|
||||||
|
}
|
||||||
|
on_demand_tls {
|
||||||
|
ask https://example.com
|
||||||
|
interval 30s
|
||||||
|
burst 20
|
||||||
|
}
|
||||||
|
local_certs
|
||||||
|
key_type ed25519
|
||||||
|
}
|
||||||
|
|
||||||
|
:80
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"admin": {
|
||||||
|
"listen": "localhost:2019",
|
||||||
|
"origins": [
|
||||||
|
"localhost:2019",
|
||||||
|
"[::1]:2019",
|
||||||
|
"127.0.0.1:2019",
|
||||||
|
"192.168.10.128"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"logging": {
|
||||||
|
"logs": {
|
||||||
|
"default": {
|
||||||
|
"level": "DEBUG"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"storage": {
|
||||||
|
"module": "file_system",
|
||||||
|
"root": "/data"
|
||||||
|
},
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"http_port": 8080,
|
||||||
|
"https_port": 8443,
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":80"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"tls": {
|
||||||
|
"automation": {
|
||||||
|
"policies": [
|
||||||
|
{
|
||||||
|
"issuers": [
|
||||||
|
{
|
||||||
|
"module": "internal"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"key_type": "ed25519"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"on_demand": {
|
||||||
|
"rate_limit": {
|
||||||
|
"interval": 30000000000,
|
||||||
|
"burst": 20
|
||||||
|
},
|
||||||
|
"ask": "https://example.com"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
{
|
||||||
|
debug
|
||||||
|
}
|
||||||
|
|
||||||
|
:8881 {
|
||||||
|
log {
|
||||||
|
format console
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"logging": {
|
||||||
|
"logs": {
|
||||||
|
"default": {
|
||||||
|
"level": "DEBUG",
|
||||||
|
"exclude": [
|
||||||
|
"http.log.access.log0"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"log0": {
|
||||||
|
"encoder": {
|
||||||
|
"format": "console"
|
||||||
|
},
|
||||||
|
"level": "DEBUG",
|
||||||
|
"include": [
|
||||||
|
"http.log.access.log0"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":8881"
|
||||||
|
],
|
||||||
|
"logs": {
|
||||||
|
"default_logger_name": "log0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
{
|
||||||
|
default_bind tcp4/0.0.0.0 tcp6/[::]
|
||||||
|
}
|
||||||
|
|
||||||
|
example.com {
|
||||||
|
}
|
||||||
|
|
||||||
|
example.org:12345 {
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
"tcp4/0.0.0.0:12345",
|
||||||
|
"tcp6/[::]:12345"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"example.org"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"srv1": {
|
||||||
|
"listen": [
|
||||||
|
"tcp4/0.0.0.0:443",
|
||||||
|
"tcp6/[::]:443"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"example.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
{
|
||||||
|
log {
|
||||||
|
output file caddy.log
|
||||||
|
include some-log-source
|
||||||
|
exclude admin.api admin2.api
|
||||||
|
}
|
||||||
|
log custom-logger {
|
||||||
|
output file caddy.log
|
||||||
|
level WARN
|
||||||
|
include custom-log-source
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
:8884 {
|
||||||
|
log {
|
||||||
|
format json
|
||||||
|
output file access.log
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"logging": {
|
||||||
|
"logs": {
|
||||||
|
"custom-logger": {
|
||||||
|
"writer": {
|
||||||
|
"filename": "caddy.log",
|
||||||
|
"output": "file"
|
||||||
|
},
|
||||||
|
"level": "WARN",
|
||||||
|
"include": [
|
||||||
|
"custom-log-source"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"default": {
|
||||||
|
"writer": {
|
||||||
|
"filename": "caddy.log",
|
||||||
|
"output": "file"
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"some-log-source"
|
||||||
|
],
|
||||||
|
"exclude": [
|
||||||
|
"admin.api",
|
||||||
|
"admin2.api",
|
||||||
|
"custom-log-source",
|
||||||
|
"http.log.access.log0"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"log0": {
|
||||||
|
"writer": {
|
||||||
|
"filename": "access.log",
|
||||||
|
"output": "file"
|
||||||
|
},
|
||||||
|
"encoder": {
|
||||||
|
"format": "json"
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"http.log.access.log0"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":8884"
|
||||||
|
],
|
||||||
|
"logs": {
|
||||||
|
"default_logger_name": "log0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
log {
|
||||||
|
output file foo.log
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"logging": {
|
||||||
|
"logs": {
|
||||||
|
"default": {
|
||||||
|
"writer": {
|
||||||
|
"filename": "foo.log",
|
||||||
|
"output": "file"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
{
|
||||||
|
log custom-logger {
|
||||||
|
format filter {
|
||||||
|
wrap console
|
||||||
|
fields {
|
||||||
|
request>remote_ip ip_mask {
|
||||||
|
ipv4 24
|
||||||
|
ipv6 32
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"logging": {
|
||||||
|
"logs": {
|
||||||
|
"custom-logger": {
|
||||||
|
"encoder": {
|
||||||
|
"fields": {
|
||||||
|
"request\u003eremote_ip": {
|
||||||
|
"filter": "ip_mask",
|
||||||
|
"ipv4_cidr": 24,
|
||||||
|
"ipv6_cidr": 32
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"format": "filter",
|
||||||
|
"wrap": {
|
||||||
|
"format": "console"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
log first {
|
||||||
|
output file foo.log
|
||||||
|
}
|
||||||
|
log second {
|
||||||
|
format json
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"logging": {
|
||||||
|
"logs": {
|
||||||
|
"first": {
|
||||||
|
"writer": {
|
||||||
|
"filename": "foo.log",
|
||||||
|
"output": "file"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"second": {
|
||||||
|
"encoder": {
|
||||||
|
"format": "json"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
{
|
||||||
|
preferred_chains smallest
|
||||||
|
}
|
||||||
|
|
||||||
|
example.com
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":443"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"example.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"tls": {
|
||||||
|
"automation": {
|
||||||
|
"policies": [
|
||||||
|
{
|
||||||
|
"subjects": [
|
||||||
|
"example.com"
|
||||||
|
],
|
||||||
|
"issuers": [
|
||||||
|
{
|
||||||
|
"module": "acme",
|
||||||
|
"preferred_chains": {
|
||||||
|
"smallest": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"module": "zerossl",
|
||||||
|
"preferred_chains": {
|
||||||
|
"smallest": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,168 @@
|
|||||||
|
{
|
||||||
|
skip_install_trust
|
||||||
|
pki {
|
||||||
|
ca {
|
||||||
|
name "Local"
|
||||||
|
root_cn "Custom Local Root Name"
|
||||||
|
intermediate_cn "Custom Local Intermediate Name"
|
||||||
|
root {
|
||||||
|
cert /path/to/cert.pem
|
||||||
|
key /path/to/key.pem
|
||||||
|
format pem_file
|
||||||
|
}
|
||||||
|
intermediate {
|
||||||
|
cert /path/to/cert.pem
|
||||||
|
key /path/to/key.pem
|
||||||
|
format pem_file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ca foo {
|
||||||
|
name "Foo"
|
||||||
|
root_cn "Custom Foo Root Name"
|
||||||
|
intermediate_cn "Custom Foo Intermediate Name"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
a.example.com {
|
||||||
|
tls internal
|
||||||
|
}
|
||||||
|
|
||||||
|
acme.example.com {
|
||||||
|
acme_server {
|
||||||
|
ca foo
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
acme-bar.example.com {
|
||||||
|
acme_server {
|
||||||
|
ca bar
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":443"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"acme-bar.example.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "subroute",
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"ca": "bar",
|
||||||
|
"handler": "acme_server"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"acme.example.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "subroute",
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"ca": "foo",
|
||||||
|
"handler": "acme_server"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"a.example.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"pki": {
|
||||||
|
"certificate_authorities": {
|
||||||
|
"bar": {
|
||||||
|
"install_trust": false
|
||||||
|
},
|
||||||
|
"foo": {
|
||||||
|
"name": "Foo",
|
||||||
|
"root_common_name": "Custom Foo Root Name",
|
||||||
|
"intermediate_common_name": "Custom Foo Intermediate Name",
|
||||||
|
"install_trust": false
|
||||||
|
},
|
||||||
|
"local": {
|
||||||
|
"name": "Local",
|
||||||
|
"root_common_name": "Custom Local Root Name",
|
||||||
|
"intermediate_common_name": "Custom Local Intermediate Name",
|
||||||
|
"install_trust": false,
|
||||||
|
"root": {
|
||||||
|
"certificate": "/path/to/cert.pem",
|
||||||
|
"private_key": "/path/to/key.pem",
|
||||||
|
"format": "pem_file"
|
||||||
|
},
|
||||||
|
"intermediate": {
|
||||||
|
"certificate": "/path/to/cert.pem",
|
||||||
|
"private_key": "/path/to/key.pem",
|
||||||
|
"format": "pem_file"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"tls": {
|
||||||
|
"automation": {
|
||||||
|
"policies": [
|
||||||
|
{
|
||||||
|
"subjects": [
|
||||||
|
"acme-bar.example.com",
|
||||||
|
"acme.example.com"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"subjects": [
|
||||||
|
"a.example.com"
|
||||||
|
],
|
||||||
|
"issuers": [
|
||||||
|
{
|
||||||
|
"module": "internal"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
{
|
||||||
|
servers {
|
||||||
|
timeouts {
|
||||||
|
idle 90s
|
||||||
|
}
|
||||||
|
protocol {
|
||||||
|
strict_sni_host insecure_off
|
||||||
|
}
|
||||||
|
}
|
||||||
|
servers :80 {
|
||||||
|
timeouts {
|
||||||
|
idle 60s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
servers :443 {
|
||||||
|
timeouts {
|
||||||
|
idle 30s
|
||||||
|
}
|
||||||
|
protocol {
|
||||||
|
strict_sni_host
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foo.com {
|
||||||
|
}
|
||||||
|
|
||||||
|
http://bar.com {
|
||||||
|
}
|
||||||
|
|
||||||
|
:8080 {
|
||||||
|
}
|
||||||
|
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":443"
|
||||||
|
],
|
||||||
|
"idle_timeout": 30000000000,
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"foo.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"strict_sni_host": true
|
||||||
|
},
|
||||||
|
"srv1": {
|
||||||
|
"listen": [
|
||||||
|
":80"
|
||||||
|
],
|
||||||
|
"idle_timeout": 60000000000,
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"bar.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"srv2": {
|
||||||
|
"listen": [
|
||||||
|
":8080"
|
||||||
|
],
|
||||||
|
"idle_timeout": 90000000000,
|
||||||
|
"strict_sni_host": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
{
|
||||||
|
servers {
|
||||||
|
listener_wrappers {
|
||||||
|
http_redirect
|
||||||
|
tls
|
||||||
|
}
|
||||||
|
timeouts {
|
||||||
|
read_body 30s
|
||||||
|
read_header 30s
|
||||||
|
write 30s
|
||||||
|
idle 30s
|
||||||
|
}
|
||||||
|
max_header_size 100MB
|
||||||
|
log_credentials
|
||||||
|
protocol {
|
||||||
|
allow_h2c
|
||||||
|
experimental_http3
|
||||||
|
strict_sni_host
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foo.com {
|
||||||
|
}
|
||||||
|
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":443"
|
||||||
|
],
|
||||||
|
"listener_wrappers": [
|
||||||
|
{
|
||||||
|
"wrapper": "http_redirect"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"wrapper": "tls"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"read_timeout": 30000000000,
|
||||||
|
"read_header_timeout": 30000000000,
|
||||||
|
"write_timeout": 30000000000,
|
||||||
|
"idle_timeout": 30000000000,
|
||||||
|
"max_header_bytes": 100000000,
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"foo.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"strict_sni_host": true,
|
||||||
|
"logs": {
|
||||||
|
"should_log_credentials": true
|
||||||
|
},
|
||||||
|
"experimental_http3": true,
|
||||||
|
"allow_h2c": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
:80
|
||||||
|
handle_path /api/v1/* {
|
||||||
|
respond "API v1"
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":80"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"path": [
|
||||||
|
"/api/v1/*"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "subroute",
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "rewrite",
|
||||||
|
"strip_path_prefix": "/api/v1"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"body": "API v1",
|
||||||
|
"handler": "static_response"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
:80 {
|
||||||
|
handle /api/* {
|
||||||
|
respond "api"
|
||||||
|
}
|
||||||
|
|
||||||
|
handle_path /static/* {
|
||||||
|
respond "static"
|
||||||
|
}
|
||||||
|
|
||||||
|
handle {
|
||||||
|
respond "handle"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":80"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"group": "group3",
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"path": [
|
||||||
|
"/static/*"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "subroute",
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "rewrite",
|
||||||
|
"strip_path_prefix": "/static"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"body": "static",
|
||||||
|
"handler": "static_response"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"group": "group3",
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"path": [
|
||||||
|
"/api/*"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "subroute",
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"body": "api",
|
||||||
|
"handler": "static_response"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"group": "group3",
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "subroute",
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"body": "handle",
|
||||||
|
"handler": "static_response"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,147 @@
|
|||||||
|
:80 {
|
||||||
|
header Denis "Ritchie"
|
||||||
|
header +Edsger "Dijkstra"
|
||||||
|
header ?John "von Neumann"
|
||||||
|
header -Wolfram
|
||||||
|
header {
|
||||||
|
Grace: "Hopper" # some users habitually suffix field names with a colon
|
||||||
|
+Ray "Solomonoff"
|
||||||
|
?Tim "Berners-Lee"
|
||||||
|
defer
|
||||||
|
}
|
||||||
|
@images path /images/*
|
||||||
|
header @images {
|
||||||
|
Cache-Control "public, max-age=3600, stale-while-revalidate=86400"
|
||||||
|
}
|
||||||
|
header {
|
||||||
|
+Link "Foo"
|
||||||
|
+Link "Bar"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":80"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"path": [
|
||||||
|
"/images/*"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "headers",
|
||||||
|
"response": {
|
||||||
|
"set": {
|
||||||
|
"Cache-Control": [
|
||||||
|
"public, max-age=3600, stale-while-revalidate=86400"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "headers",
|
||||||
|
"response": {
|
||||||
|
"set": {
|
||||||
|
"Denis": [
|
||||||
|
"Ritchie"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"handler": "headers",
|
||||||
|
"response": {
|
||||||
|
"add": {
|
||||||
|
"Edsger": [
|
||||||
|
"Dijkstra"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"handler": "headers",
|
||||||
|
"response": {
|
||||||
|
"require": {
|
||||||
|
"headers": {
|
||||||
|
"John": null
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"set": {
|
||||||
|
"John": [
|
||||||
|
"von Neumann"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"handler": "headers",
|
||||||
|
"response": {
|
||||||
|
"deferred": true,
|
||||||
|
"delete": [
|
||||||
|
"Wolfram"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"handler": "headers",
|
||||||
|
"response": {
|
||||||
|
"add": {
|
||||||
|
"Ray": [
|
||||||
|
"Solomonoff"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"deferred": true,
|
||||||
|
"set": {
|
||||||
|
"Grace": [
|
||||||
|
"Hopper"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"handler": "headers",
|
||||||
|
"response": {
|
||||||
|
"require": {
|
||||||
|
"headers": {
|
||||||
|
"Tim": null
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"set": {
|
||||||
|
"Tim": [
|
||||||
|
"Berners-Lee"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"handler": "headers",
|
||||||
|
"response": {
|
||||||
|
"add": {
|
||||||
|
"Link": [
|
||||||
|
"Foo",
|
||||||
|
"Bar"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
# https://github.com/caddyserver/caddy/issues/3977
|
||||||
|
http://* {
|
||||||
|
respond "Hello, world!"
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":80"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"*"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "subroute",
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"body": "Hello, world!",
|
||||||
|
"handler": "static_response"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
:80 {
|
||||||
|
respond /version 200 {
|
||||||
|
body "hello from localhost"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":80"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"path": [
|
||||||
|
"/version"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"body": "hello from localhost",
|
||||||
|
"handler": "static_response",
|
||||||
|
"status_code": 200
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
http://a.caddy.localhost {
|
||||||
|
respond /version 200 {
|
||||||
|
body "hello from localhost"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":80"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"a.caddy.localhost"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "subroute",
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"body": "hello from localhost",
|
||||||
|
"handler": "static_response",
|
||||||
|
"status_code": 200
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"path": [
|
||||||
|
"/version"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
# Issue #4113
|
||||||
|
:80, http://example.com {
|
||||||
|
respond "foo"
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":80"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"body": "foo",
|
||||||
|
"handler": "static_response"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
localhost:80 {
|
||||||
|
respond /version 200 {
|
||||||
|
body "hello from localhost"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":80"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"localhost"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "subroute",
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"body": "hello from localhost",
|
||||||
|
"handler": "static_response",
|
||||||
|
"status_code": 200
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"path": [
|
||||||
|
"/version"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
http://a.caddy.localhost:81 {
|
||||||
|
respond /version 200 {
|
||||||
|
body "hello from localhost"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":81"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"a.caddy.localhost"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "subroute",
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"body": "hello from localhost",
|
||||||
|
"handler": "static_response",
|
||||||
|
"status_code": 200
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"path": [
|
||||||
|
"/version"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"automatic_https": {
|
||||||
|
"skip": [
|
||||||
|
"a.caddy.localhost"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
a.caddy.localhost {
|
||||||
|
respond /version 200 {
|
||||||
|
body "hello from localhost"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":443"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"a.caddy.localhost"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "subroute",
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"body": "hello from localhost",
|
||||||
|
"handler": "static_response",
|
||||||
|
"status_code": 200
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"path": [
|
||||||
|
"/version"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
example.com
|
||||||
|
|
||||||
|
import testdata/import_respond.txt Groot Rocket
|
||||||
|
import testdata/import_respond.txt you "the confused man"
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":443"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"example.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"handler": "subroute",
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"body": "'I am Groot', hears Rocket",
|
||||||
|
"handler": "static_response"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"body": "'I am you', hears the confused man",
|
||||||
|
"handler": "static_response"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
(logging) {
|
||||||
|
log {
|
||||||
|
output file /var/log/caddy/{args.0}.access.log
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
a.example.com {
|
||||||
|
import logging a.example.com
|
||||||
|
}
|
||||||
|
|
||||||
|
b.example.com {
|
||||||
|
import logging b.example.com
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"logging": {
|
||||||
|
"logs": {
|
||||||
|
"default": {
|
||||||
|
"exclude": [
|
||||||
|
"http.log.access.log0",
|
||||||
|
"http.log.access.log1"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"log0": {
|
||||||
|
"writer": {
|
||||||
|
"filename": "/var/log/caddy/a.example.com.access.log",
|
||||||
|
"output": "file"
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"http.log.access.log0"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"log1": {
|
||||||
|
"writer": {
|
||||||
|
"filename": "/var/log/caddy/b.example.com.access.log",
|
||||||
|
"output": "file"
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"http.log.access.log1"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":443"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"a.example.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"match": [
|
||||||
|
{
|
||||||
|
"host": [
|
||||||
|
"b.example.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"terminal": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"logs": {
|
||||||
|
"logger_names": {
|
||||||
|
"a.example.com": "log0",
|
||||||
|
"b.example.com": "log1"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
(foo) {
|
||||||
|
respond {env.FOO}
|
||||||
|
}
|
||||||
|
|
||||||
|
:80 {
|
||||||
|
import foo
|
||||||
|
}
|
||||||
|
----------
|
||||||
|
{
|
||||||
|
"apps": {
|
||||||
|
"http": {
|
||||||
|
"servers": {
|
||||||
|
"srv0": {
|
||||||
|
"listen": [
|
||||||
|
":80"
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"handle": [
|
||||||
|
{
|
||||||
|
"body": "{env.FOO}",
|
||||||
|
"handler": "static_response"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user