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.

333 lines
7.5 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. export state_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  110. trap "rm -rf \"$state_tmpdir\"" EXIT
  111. ##
  112. ## cert-provider
  113. ##
  114. try "
  115. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  116. export CONFIGSTORE='\$CONFIGSTORE'
  117. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  118. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  119. RELATIONS=()
  120. apache_vhost_create publish_dir '
  121. domain: www.example.com
  122. ssl:
  123. foo: |
  124. a
  125. b
  126. '
  127. " "unknown cert key"
  128. is errlvl 1
  129. is err reg 'Error: .*cert-provider.*'
  130. try "
  131. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  132. export CONFIGSTORE='\$CONFIGSTORE'
  133. export DATASTORE='\$DATASTORE'
  134. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  135. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  136. CFG='
  137. domain: www.example.com
  138. ssl:
  139. foo: 12
  140. '
  141. ADDITION='
  142. apache-custom-rules:
  143. - |
  144. ## Auto-redirection from http to https
  145. RewriteEngine On
  146. RewriteCond %{HTTPS} off
  147. RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]'
  148. MERGE_YAML_STR=\"
  149. 3e417c2db15450f3: |
  150. \$(echo \"\$CFG\" | prefix ' ')
  151. \$(echo \"\$ADDITION\" | prefix ' ')
  152. \"
  153. RELATIONS=(cert-provider foo a True)
  154. apache_vhost_create publish_dir \"\$CFG\"
  155. " "known cert key"
  156. is errlvl 0
  157. is err part "\
  158. relation-set apache-custom-rules:
  159. | - |
  160. | ## Auto-redirection from http to https
  161. | RewriteEngine On
  162. | RewriteCond %{HTTPS} off
  163. | RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]
  164. |"
  165. is out reg 'Calling: compose .*foo: options: <merge_yaml_str\(.a., .12., )>.*run --rm --service-ports foo.*'
  166. is out part 'config-add
  167. | services:
  168. | $MASTER_TARGET_SERVICE_NAME:
  169. | volumes:
  170. | - $DATASTORE/foo/etc/letsencrypt:/etc/letsencrypt:ro'
  171. is out part '
  172. | ## Auto-redirection from http to https
  173. | RewriteEngine On
  174. | RewriteCond %{HTTPS} off
  175. | RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]
  176. '
  177. try "
  178. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  179. export CONFIGSTORE='\$CONFIGSTORE'
  180. export DATASTORE='\$DATASTORE'
  181. export SERVICE_NAME='\$SERVICE_NAME'
  182. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  183. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  184. RELATIONS=(cert-provider foo a True)
  185. CFG='
  186. domain: www.example.com
  187. server-aliases:
  188. ssl:
  189. foo: 12
  190. '
  191. ADDITION='
  192. apache-custom-rules:
  193. - |
  194. ## Auto-redirection from http to https
  195. RewriteEngine On
  196. RewriteCond %{HTTPS} off
  197. RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]'
  198. MERGE_YAML_STR=\"
  199. d186e0ae74640f6d: |
  200. \$(echo \"\$CFG\" | prefix ' ')
  201. \$(echo \"\$ADDITION\" | prefix ' ')
  202. \"
  203. apache_vhost_create publish_dir \"\$CFG\"
  204. " "known cert key - empty server-aliases"
  205. is errlvl 0
  206. is err part "\
  207. relation-set apache-custom-rules:
  208. | - |
  209. | ## Auto-redirection from http to https
  210. | RewriteEngine On
  211. | RewriteCond %{HTTPS} off
  212. | RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]
  213. |"
  214. is out reg 'Calling: compose .*foo: options: <merge_yaml_str\(.a., .12., )>.*run --rm --service-ports foo crt create www.example.com\s*
  215. ' RTRIM
  216. is out part 'config-add
  217. | services:
  218. | $MASTER_TARGET_SERVICE_NAME:
  219. | volumes:
  220. | - $DATASTORE/foo/etc/letsencrypt:/etc/letsencrypt:ro'
  221. is out part 'file_put $SERVICE_CONFIGSTORE/etc/apache2/sites-enabled/www.example.com.conf
  222. | <VirtualHost *:80>
  223. |
  224. | ServerAdmin contact@www.example.com
  225. | ServerName www.example.com
  226. |
  227. | ServerSignature Off' RTRIM
  228. is out part '
  229. | ## Auto-redirection from http to https
  230. | RewriteEngine On
  231. | RewriteCond %{HTTPS} off
  232. | RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]
  233. '
  234. try "
  235. export SERVICE_CONFIGSTORE='\$SERVICE_CONFIGSTORE'
  236. export CONFIGSTORE='\$CONFIGSTORE'
  237. export DATASTORE='\$DATASTORE'
  238. export SERVICE_NAME='\$SERVICE_NAME'
  239. export BASE_SERVICE_NAME='\$BASE_SERVICE_NAME'
  240. export MASTER_TARGET_SERVICE_NAME='\$MASTER_TARGET_SERVICE_NAME'
  241. RELATIONS=(cert-provider foo a True)
  242. CFG='
  243. domain: www.example.com
  244. server-aliases:
  245. - example.fr
  246. - example.de
  247. ssl:
  248. foo: 12
  249. '
  250. ADDITION='
  251. apache-custom-rules:
  252. - |
  253. ## Auto-redirection from http to https
  254. RewriteEngine On
  255. RewriteCond %{HTTPS} off
  256. RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]'
  257. MERGE_YAML_STR=\"
  258. fcab3acadc661133: |
  259. \$(echo \"\$CFG\" | prefix ' ')
  260. \$(echo \"\$ADDITION\" | prefix ' ')
  261. \"
  262. apache_vhost_create publish_dir \"\$CFG\"
  263. " "known cert key - multiple server-aliases"
  264. is errlvl 0
  265. is err part "\
  266. relation-set apache-custom-rules:
  267. | - |
  268. | ## Auto-redirection from http to https
  269. | RewriteEngine On
  270. | RewriteCond %{HTTPS} off
  271. | RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]
  272. |"
  273. 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+
  274. '
  275. is out part 'config-add
  276. | services:
  277. | $MASTER_TARGET_SERVICE_NAME:
  278. | volumes:
  279. | - $DATASTORE/foo/etc/letsencrypt:/etc/letsencrypt:ro'
  280. is out part 'file_put $SERVICE_CONFIGSTORE/etc/apache2/sites-enabled/www.example.com.conf
  281. | <VirtualHost *:80>
  282. |
  283. | ServerAdmin contact@www.example.com
  284. | ServerName www.example.com
  285. | ServerAlias example.fr
  286. | ServerAlias example.de
  287. |
  288. | ServerSignature Off' RTRIM
  289. is out part '
  290. | ## Auto-redirection from http to https
  291. | RewriteEngine On
  292. | RewriteCond %{HTTPS} off
  293. | RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]
  294. '