tang-common-test-functions.in 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/bin/bash -ex
  2. # vim: set ts=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  3. #
  4. # Copyright (c) 2020 Red Hat, Inc.
  5. # Author: Sergio Correia <scorreia@redhat.com>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. SOCAT="@SOCAT@"
  21. TANGD_KEYGEN="@TANGD_KEYGEN@"
  22. TANGD_UPDATE="@TANGD_UPDATE@"
  23. TANGD="@TANGD@"
  24. tang_error() {
  25. echo "${1}" >&2
  26. exit 1
  27. }
  28. tang_skip() {
  29. echo "${1}" >&2
  30. exit 77
  31. }
  32. tang_sanity_check() {
  33. [ -n "${SOCAT}" ] && [ -n "${TANGD_KEYGEN}" ] && \
  34. [ -n "${TANGD}" ] && return 0
  35. tang_skip "tang is not enabled/supported. Check if you have met all the requirements"
  36. }
  37. # Creates a tang adv to be used in the tests.
  38. tang_create_adv() {
  39. local basedir="${1}"
  40. local adv="${2:-/dev/stdout}"
  41. local SIG="${basedir}/sig.jwk"
  42. jose jwk gen --input='{"alg":"ES512"}' --output="${SIG}"
  43. local EXC="${basedir}/exc.jwk"
  44. jose jwk gen --input='{"alg":"ECMR"}' --output="${EXC}"
  45. local TEMPLATE='{"protected":{"cty":"jwk-set+json"}}'
  46. jose jwk pub --set --input="${SIG}" --input="${EXC}" \
  47. | jose jws sig --detached=- --signature="${TEMPLATE}" \
  48. --key="${SIG}" --output="${adv}"
  49. }
  50. # Get a random port to be used with a test tang server.
  51. tang_new_random_port() {
  52. tang_sanity_check
  53. shuf -i 1024-65535 -n 1
  54. }
  55. # Removes tang rotated keys from the test server.
  56. tang_remove_rotated_keys() {
  57. tang_sanity_check
  58. local basedir="${1}"
  59. [ -z "${basedir}" ] && \
  60. tang_error "tang_remove_rotated_keys: please specify 'basedir'"
  61. local db="${basedir}/db"
  62. mkdir -p "${db}"
  63. pushd "${db}"
  64. find . -name ".*.jwk" -exec rm -f {} \;
  65. popd
  66. [ -n "${TANGD_UPDATE}" ] && "${TANGD_UPDATE}" "${db}" "${basedir}/cache"
  67. return 0
  68. }
  69. # Creates new keys for the test tang server.
  70. tang_new_keys() {
  71. tang_sanity_check
  72. local basedir="${1}"
  73. local rotate="${2:-}"
  74. local sig_name="${3:-}"
  75. local exc_name="${4:-}"
  76. [ -z "${basedir}" ] && tang_error "tang_new_keys: please specify 'basedir'"
  77. local db="${basedir}/db"
  78. mkdir -p "${db}"
  79. if [ -n "${rotate}" ]; then
  80. pushd "${db}"
  81. local k
  82. k=$(find . -name "*.jwk" | wc -l)
  83. if [ "${k}" -gt 0 ]; then
  84. for k in *.jwk; do
  85. mv -f -- "${k}" ".${k}"
  86. done
  87. fi
  88. popd
  89. fi
  90. "${TANGD_KEYGEN}" "${db}" ${sig_name} ${exc_name}
  91. [ -n "${TANGD_UPDATE}" ] && "${TANGD_UPDATE}" "${db}" "${basedir}/cache"
  92. return 0
  93. }
  94. # Wait for the tang server to be operational.
  95. tang_wait_until_ready() {
  96. tang_sanity_check
  97. local port="${1}"
  98. [ -z "${port}" ] && \
  99. tang_error "tang_wait_until_ready: please specify 'port'"
  100. local max_timeout_in_s=5
  101. local start elapsed
  102. start="${SECONDS}"
  103. while ! curl --output /dev/null --silent --fail \
  104. "http://localhost:${port}/adv"; do
  105. elapsed=$((SECONDS - start))
  106. if [ "${elapsed}" -gt "${max_timeout_in_s}" ]; then
  107. tang_error "Timeout (${max_timeout_in_s}s) waiting for tang server"
  108. fi
  109. sleep 0.1
  110. echo -n . >&2
  111. done
  112. }
  113. # Start a test tang server.
  114. tang_run() {
  115. tang_sanity_check
  116. local basedir="${1}"
  117. local port="${2}"
  118. local sig_name="${3:-}"
  119. local exc_name="${4:-}"
  120. [ -z "${basedir}" ] && tang_error "tang_run: please specify 'basedir'"
  121. [ -z "${port}" ] && tang_error "tang_run: please specify 'port'"
  122. if ! tang_new_keys "${basedir}" "" "${sig_name}" "${exc_name}"; then
  123. tang_error "Error creating new keys for tang server"
  124. fi
  125. local KEYS="${basedir}/cache"
  126. [ -z "${TANGD_UPDATE}" ] && KEYS="${basedir}/db"
  127. local pid pidfile
  128. pidfile="${basedir}/tang.pid"
  129. "${SOCAT}" -v -v TCP-LISTEN:${port},reuseaddr,fork \
  130. exec:"${TANGD} ${KEYS}" &
  131. pid=$!
  132. echo "${pid}" > "${pidfile}"
  133. tang_wait_until_ready "${port}"
  134. }
  135. # Stop tang server.
  136. tang_stop() {
  137. tang_sanity_check
  138. local basedir="${1}"
  139. [ -z "${basedir}" ] && tang_error "tang_stop: please specify 'basedir'"
  140. local pidfile="${basedir}/tang.pid"
  141. [ -f "${pidfile}" ] || return 0
  142. local pid
  143. pid=$(<"${pidfile}")
  144. kill -9 "${pid}" 2>/dev/null || :
  145. }
  146. # Get tang advertisement.
  147. tang_get_adv() {
  148. tang_sanity_check
  149. local port="${1}"
  150. local adv="${2:-/dev/stdout}"
  151. [ -z "${port}" ] && tang_error "tang_get_adv: please specify 'port'"
  152. curl -L -o "${adv}" "http://localhost:${port}/adv"
  153. }
  154. run_test_server() {
  155. local port="${1}"
  156. local response="${2}"
  157. [ -z "${SOCAT}" ] && tang_skip "run_test_server: socat is not available"
  158. [ -z "${port}" ] && tang_error "run_test_server: please specify 'port'"
  159. [ -z "${response}" ] && tang_error "run_test_server: please specify 'response'"
  160. "${SOCAT}" -v -v TCP-LISTEN:${port},reuseaddr SYSTEM:"cat ${response}" &
  161. sleep 1
  162. }