295 lines
5.3 KiB

  1. #!/bin/bash
  2. exname=$(basename $0)
  3. prefix_cmd="
  4. . /etc/shlib
  5. include common
  6. include parse
  7. . ../lib/common
  8. valid_existing_cert() {
  9. local i
  10. echo \"Calling valid_existing_cert\" >&2
  11. ((i=0))
  12. for arg in \"\$@\"; do
  13. echo \" arg\$((i++)):\"
  14. echo \"\$arg\" | prefix \" | \"
  15. done >&2
  16. return \"\$VALID_EXISTING_CERT\"
  17. }
  18. export -f valid_existing_cert
  19. crt() {
  20. local i
  21. echo \"Calling crt\" >&2
  22. ((i=0))
  23. for arg in \"\$@\"; do
  24. echo \" arg\$((i++)):\"
  25. echo \"\$arg\" | prefix \" | \"
  26. done >&2
  27. return \$CRT
  28. }
  29. export -f crt
  30. letsencrypt_set_renew_before_expiry() {
  31. local i
  32. echo \"Calling letsencrypt_set_renew_before_expiry\" >&2
  33. ((i=0))
  34. for arg in \"\$@\"; do
  35. echo \" arg\$((i++)):\"
  36. echo \"\$arg\" | prefix \" | \"
  37. done >&2
  38. [ \"\$LETSENCRYPT_SET_RENEW_BEFORE_EXPIRY\" == \"yes\" ]
  39. }
  40. export -f letsencrypt_set_renew_before_expiry
  41. letsencrypt_cert_delete() {
  42. local i
  43. echo \"Calling letsencrypt_cert_delete\" >&2
  44. ((i=0))
  45. for arg in \"\$@\"; do
  46. echo \" arg\$((i++)):\"
  47. echo \"\$arg\" | prefix \" | \"
  48. done >&2
  49. [ \"\$LETSENCRYPT_CERT_DELETE\" == \"yes\" ]
  50. }
  51. export -f letsencrypt_cert_delete
  52. "
  53. ##
  54. ## Mocks
  55. ##
  56. get_compose_service_def() {
  57. local i
  58. echo "Calling: get_compose_service_def" >&2
  59. ((i=0))
  60. for arg in "$@"; do
  61. echo " arg$((i++)):"
  62. echo "$arg" | prefix " | "
  63. done >&2
  64. echo "$GET_COMPOSE_SERVICE_DEF"
  65. }
  66. export -f get_compose_service_def
  67. try "
  68. exname=\"crt create\"
  69. SERVICE_NAME='\$SERVICE_NAME'
  70. GET_COMPOSE_SERVICE_DEF=
  71. VALID_EXISTING_CERT=1
  72. crt_create
  73. "
  74. is err 'Error: At least one domain should be provided as argument.
  75. usage:
  76. crt create [-h|--help]
  77. crt create MAIN_DOMAIN [ALT_DOMAINS...]' RTRIM
  78. is errlvl 1
  79. try "
  80. exname=\"crt create\"
  81. SERVICE_NAME='\$SERVICE_NAME'
  82. GET_COMPOSE_SERVICE_DEF=
  83. VALID_EXISTING_CERT=1
  84. crt_create --help
  85. "
  86. is err ''
  87. is out 'usage:
  88. crt create [-h|--help]
  89. crt create MAIN_DOMAIN [ALT_DOMAINS...]' RTRIM
  90. is errlvl 0
  91. try "
  92. CRT=0
  93. exname=\"crt create\"
  94. SERVICE_NAME='\$SERVICE_NAME'
  95. GET_COMPOSE_SERVICE_DEF=
  96. VALID_EXISTING_CERT=1
  97. LETSENCRYPT_SET_RENEW_BEFORE_EXPIRY=yes
  98. crt_create www.example.com
  99. " "invalid cert"
  100. is err 'Calling: get_compose_service_def
  101. arg0:
  102. | $SERVICE_NAME
  103. Calling valid_existing_cert
  104. arg0:
  105. | 30
  106. arg1:
  107. | www.example.com
  108. Calling crt
  109. arg0:
  110. |
  111. arg1:
  112. | create
  113. arg2:
  114. | www.example.com
  115. Calling letsencrypt_set_renew_before_expiry
  116. arg0:
  117. | www.example.com
  118. arg1:
  119. | 30' RTRIM
  120. is out '' RTRIM
  121. is errlvl 0
  122. try "
  123. exname=\"crt create\"
  124. SERVICE_NAME='\$SERVICE_NAME'
  125. GET_COMPOSE_SERVICE_DEF=
  126. VALID_EXISTING_CERT=0
  127. LETSENCRYPT_SET_RENEW_BEFORE_EXPIRY=yes
  128. crt_create www.example.com
  129. " "valid cert"
  130. is err 'Calling: get_compose_service_def
  131. arg0:
  132. | $SERVICE_NAME
  133. Calling valid_existing_cert
  134. arg0:
  135. | 30
  136. arg1:
  137. | www.example.com
  138. II A valid cert already exists for domain www.example.com.' RTRIM
  139. is out '' RTRIM
  140. is errlvl 0
  141. try "
  142. exname=\"crt create\"
  143. SERVICE_NAME='\$SERVICE_NAME'
  144. GET_COMPOSE_SERVICE_DEF=
  145. VALID_EXISTING_CERT=0
  146. LETSENCRYPT_SET_RENEW_BEFORE_EXPIRY=yes
  147. crt_create www.example.com -f
  148. " "valid cert but force"
  149. is err 'Calling: get_compose_service_def
  150. arg0:
  151. | $SERVICE_NAME
  152. Calling valid_existing_cert
  153. arg0:
  154. | 30
  155. arg1:
  156. | www.example.com
  157. Calling crt
  158. arg0:
  159. |
  160. arg1:
  161. | create
  162. arg2:
  163. | www.example.com
  164. Calling letsencrypt_set_renew_before_expiry
  165. arg0:
  166. | www.example.com
  167. arg1:
  168. | 30' RTRIM
  169. is out '' RTRIM
  170. is errlvl 0
  171. try "
  172. exname=\"crt create\"
  173. SERVICE_NAME='\$SERVICE_NAME'
  174. LETSENCRYPT_SET_RENEW_BEFORE_EXPIRY=yes
  175. GET_COMPOSE_SERVICE_DEF='
  176. a: 1
  177. options:
  178. foo: bar'
  179. VALID_EXISTING_CERT=1
  180. crt_create www.example.com
  181. " "not valid, cfg is passed correctly"
  182. is err reg 'Calling crt
  183. arg0:
  184. . foo: bar
  185. arg1:
  186. . create
  187. arg2:
  188. . www.example.com' RTRIM
  189. is out '' RTRIM
  190. is errlvl 0
  191. try "
  192. exname=\"crt create\"
  193. SERVICE_NAME='\$SERVICE_NAME'
  194. GET_COMPOSE_SERVICE_DEF='
  195. a: 1
  196. options:
  197. foo: bar'
  198. VALID_EXISTING_CERT=2
  199. LETSENCRYPT_SET_RENEW_BEFORE_EXPIRY=yes
  200. LETSENCRYPT_CERT_DELETE=yes
  201. crt_create www.example.com
  202. " "not valid, already existing diff domain"
  203. is err 'Calling: get_compose_service_def
  204. arg0:
  205. | $SERVICE_NAME
  206. Calling valid_existing_cert
  207. arg0:
  208. | 30
  209. arg1:
  210. | www.example.com
  211. Error: Domain mismatch detected, lets delete previous cert.
  212. Calling letsencrypt_cert_delete
  213. arg0:
  214. | www.example.com
  215. Error: Previous cert for www.example.com deleted.
  216. Calling crt
  217. arg0:
  218. | foo: bar
  219. arg1:
  220. | create
  221. arg2:
  222. | www.example.com
  223. Calling letsencrypt_set_renew_before_expiry
  224. arg0:
  225. | www.example.com
  226. arg1:
  227. | 30' RTRIM
  228. is out '' RTRIM
  229. is errlvl 0
  230. try "
  231. exname=\"crt create\"
  232. SERVICE_NAME='\$SERVICE_NAME'
  233. LETSENCRYPT_SET_RENEW_BEFORE_EXPIRY=yes
  234. GET_COMPOSE_SERVICE_DEF='
  235. a: 1
  236. options:
  237. foo: bar
  238. renew-before-expiry: 15
  239. '
  240. VALID_EXISTING_CERT=1
  241. crt_create www.example.com
  242. " "not valid, renew-before-expiry is used"
  243. is err reg 'Calling valid_existing_cert
  244. arg0:
  245. . 15
  246. arg1:
  247. . www.example.com
  248. ' RTRIM
  249. is out '' RTRIM
  250. is errlvl 0
  251. try "
  252. crt() { return 1; }
  253. exname=\"crt create\"
  254. SERVICE_NAME='\$SERVICE_NAME'
  255. GET_COMPOSE_SERVICE_DEF=''
  256. VALID_EXISTING_CERT=1
  257. crt_create www.example.com
  258. " "valid cert but force"
  259. is err part "Error: Certificate creation/renew failed for domain 'www.example.com'." RTRIM
  260. is out '' RTRIM
  261. is errlvl 1