fork 0k-charms
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.

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