You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

328 lines
7.4 KiB

  1. #!/bin/bash
  2. exname=$(basename $0)
  3. prefix_cmd="
  4. . /etc/shlib
  5. include common
  6. include parse
  7. . ../lib/common
  8. depends compose
  9. "
  10. ##
  11. ## Mocks
  12. ##
  13. relation-get() {
  14. local key="$1"
  15. echo "$CFG" | shyaml get-value "$key" 2>/dev/null
  16. }
  17. export -f relation-get
  18. relation-set() {
  19. local key="$1" value="$2"
  20. echo "relation-set $key:" >&2
  21. echo "$value" | prefix " | " >&2
  22. }
  23. export -f relation-set
  24. cfg-get-value() {
  25. local key="$1"
  26. shyaml get-value "$key" 2>/dev/null
  27. }
  28. export -f cfg-get-value
  29. get_service_relations() {
  30. printf "%s\0" "${RELATIONS[@]}"
  31. }
  32. export -f get_service_relations
  33. merge_yaml_str() {
  34. local arg_hash="$(H "$@" | cut -c -16)"
  35. local i
  36. echo "Calling: merge_yaml_str" >&2
  37. ((i=0))
  38. for arg in "$@"; do
  39. echo " arg$((i++)):"
  40. echo "$arg" | prefix " | "
  41. done >&2
  42. echo " H> $arg_hash" >&2
  43. while read-0 h res; do
  44. if [[ "$arg_hash" == "$h" ]]; then
  45. echo "Mock hash matched, returning:" >&2
  46. echo "$res" | prefix " | " >&2
  47. echo "$res"
  48. return 0
  49. fi
  50. done < <(e "$MERGE_YAML_STR" | shyaml key-values-0)
  51. printf "<merge_yaml_str("
  52. printf "'%s', " "$@"
  53. printf ")>"
  54. }
  55. export -f merge_yaml_str
  56. compose() {
  57. printf "Calling: compose "
  58. printf "%s " "$*"
  59. echo
  60. }
  61. export -f compose
  62. yaml_key_val_str() {
  63. printf "%s: %s" "$1" "$2"
  64. }
  65. export -f yaml_key_val_str
  66. yaml_get_interpret() {
  67. shyaml get-value
  68. }
  69. export -f yaml_get_interpret
  70. file_put() {
  71. echo "file_put $1"
  72. cat - | prefix " | "
  73. }
  74. export -f file_put
  75. docker() {
  76. echo "docker" "$@"
  77. echo stdin:
  78. cat - | prefix " | "
  79. }
  80. export -f docker
  81. config-add() {
  82. echo "config-add"
  83. echo "$1" | prefix " | "
  84. }
  85. export -f config-add
  86. mkdir() {
  87. echo "called: $FUNCNAME $@" >&2
  88. }
  89. export -f mkdir
  90. setfacl() {
  91. echo "called: $FUNCNAME $@" >&2
  92. }
  93. export -f setfacl
  94. chgrp() {
  95. echo "called: $FUNCNAME $@" >&2
  96. }
  97. export -f chgrp
  98. chmod() {
  99. echo "called: $FUNCNAME $@" >&2
  100. }
  101. export -f chmod
  102. cached_cmd_on_base_image() {
  103. echo "called: $FUNCNAME $@" >&2
  104. echo "stdout:" >&2
  105. echo "<GID>" | prefix " | " >&2
  106. echo "<GID>"
  107. }
  108. export -f cached_cmd_on_base_image
  109. ##
  110. ## cert-provider
  111. ##
  112. try "
  113. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  114. export CONFIGSTORE='\$CONFIGSTORE'
  115. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  116. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  117. RELATIONS=()
  118. apache_vhost_create publish_dir '
  119. domain: www.example.com
  120. ssl:
  121. foo: |
  122. a
  123. b
  124. '
  125. " "unknown cert key"
  126. is errlvl 1
  127. is err reg 'Error: .*cert-provider.*'
  128. try "
  129. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  130. export CONFIGSTORE='\$CONFIGSTORE'
  131. export DATASTORE='\$DATASTORE'
  132. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  133. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  134. CFG='
  135. domain: www.example.com
  136. ssl:
  137. foo: 12
  138. '
  139. ADDITION='
  140. apache-custom-rules:
  141. - |
  142. ## Auto-redirection from http to https
  143. RewriteEngine On
  144. RewriteCond %{HTTPS} off
  145. RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]'
  146. MERGE_YAML_STR=\"
  147. 3e417c2db15450f3: |
  148. \$(echo \"\$CFG\" | prefix ' ')
  149. \$(echo \"\$ADDITION\" | prefix ' ')
  150. \"
  151. RELATIONS=(cert-provider foo a True)
  152. apache_vhost_create publish_dir \"\$CFG\"
  153. " "known cert key"
  154. is errlvl 0
  155. is err part "\
  156. relation-set apache-custom-rules:
  157. | - |
  158. | ## Auto-redirection from http to https
  159. | RewriteEngine On
  160. | RewriteCond %{HTTPS} off
  161. | RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]
  162. |"
  163. is out reg 'Calling: compose .*foo: options: <merge_yaml_str\(.a., .12., )>.*run --rm --service-ports foo.*'
  164. is out part 'config-add
  165. | services:
  166. | $MASTER_TARGET_SERVICE_NAME:
  167. | volumes:
  168. | - $DATASTORE/foo/etc/letsencrypt:/etc/letsencrypt:ro'
  169. is out part '
  170. | ## Auto-redirection from http to https
  171. | RewriteEngine On
  172. | RewriteCond %{HTTPS} off
  173. | RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]
  174. '
  175. try "
  176. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  177. export CONFIGSTORE='\$CONFIGSTORE'
  178. export DATASTORE='\$DATASTORE'
  179. export SERVICE_NAME='\$SERVICE_NAME'
  180. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  181. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  182. RELATIONS=(cert-provider foo a True)
  183. CFG='
  184. domain: www.example.com
  185. server-aliases:
  186. ssl:
  187. foo: 12
  188. '
  189. ADDITION='
  190. apache-custom-rules:
  191. - |
  192. ## Auto-redirection from http to https
  193. RewriteEngine On
  194. RewriteCond %{HTTPS} off
  195. RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]'
  196. MERGE_YAML_STR=\"
  197. d186e0ae74640f6d: |
  198. \$(echo \"\$CFG\" | prefix ' ')
  199. \$(echo \"\$ADDITION\" | prefix ' ')
  200. \"
  201. apache_vhost_create publish_dir \"\$CFG\"
  202. " "known cert key - empty server-aliases"
  203. is errlvl 0
  204. is err part "\
  205. relation-set apache-custom-rules:
  206. | - |
  207. | ## Auto-redirection from http to https
  208. | RewriteEngine On
  209. | RewriteCond %{HTTPS} off
  210. | RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]
  211. |"
  212. is out reg 'Calling: compose .*foo: options: <merge_yaml_str\(.a., .12., )>.*run --rm --service-ports foo crt create www.example.com\s*
  213. ' RTRIM
  214. is out part 'config-add
  215. | services:
  216. | $MASTER_TARGET_SERVICE_NAME:
  217. | volumes:
  218. | - $DATASTORE/foo/etc/letsencrypt:/etc/letsencrypt:ro'
  219. is out part 'file_put $SERVICE_CONFIGSTORE/etc/apache2/sites-enabled/www.example.com.conf
  220. | <VirtualHost *:80>
  221. |
  222. | ServerAdmin contact@www.example.com
  223. | ServerName www.example.com
  224. |
  225. | ServerSignature Off' RTRIM
  226. is out part '
  227. | ## Auto-redirection from http to https
  228. | RewriteEngine On
  229. | RewriteCond %{HTTPS} off
  230. | RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]
  231. '
  232. try "
  233. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  234. export CONFIGSTORE='\$CONFIGSTORE'
  235. export DATASTORE='\$DATASTORE'
  236. export SERVICE_NAME='\$SERVICE_NAME'
  237. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  238. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  239. RELATIONS=(cert-provider foo a True)
  240. CFG='
  241. domain: www.example.com
  242. server-aliases:
  243. - example.fr
  244. - example.de
  245. ssl:
  246. foo: 12
  247. '
  248. ADDITION='
  249. apache-custom-rules:
  250. - |
  251. ## Auto-redirection from http to https
  252. RewriteEngine On
  253. RewriteCond %{HTTPS} off
  254. RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]'
  255. MERGE_YAML_STR=\"
  256. fcab3acadc661133: |
  257. \$(echo \"\$CFG\" | prefix ' ')
  258. \$(echo \"\$ADDITION\" | prefix ' ')
  259. \"
  260. apache_vhost_create publish_dir \"\$CFG\"
  261. " "known cert key - multiple server-aliases"
  262. is errlvl 0
  263. is err part "\
  264. relation-set apache-custom-rules:
  265. | - |
  266. | ## Auto-redirection from http to https
  267. | RewriteEngine On
  268. | RewriteCond %{HTTPS} off
  269. | RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]
  270. |"
  271. is out reg 'Calling: compose .*foo: options: <merge_yaml_str\(.a., .12., )>.*run --rm --service-ports foo crt create www.example.com example.fr example.de\s+
  272. '
  273. is out part 'config-add
  274. | services:
  275. | $MASTER_TARGET_SERVICE_NAME:
  276. | volumes:
  277. | - $DATASTORE/foo/etc/letsencrypt:/etc/letsencrypt:ro'
  278. is out part 'file_put $SERVICE_CONFIGSTORE/etc/apache2/sites-enabled/www.example.com.conf
  279. | <VirtualHost *:80>
  280. |
  281. | ServerAdmin contact@www.example.com
  282. | ServerName www.example.com
  283. | ServerAlias example.fr
  284. | ServerAlias example.de
  285. | ServerSignature Off' RTRIM
  286. is out part '
  287. | ## Auto-redirection from http to https
  288. | RewriteEngine On
  289. | RewriteCond %{HTTPS} off
  290. | RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]
  291. '