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.

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