1605797689.v15-2-g3dedc5d.tests-move-tang-tests-to-src-pins-tang.patch 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. Subject: Tests: move tang tests to src/pins/tang
  2. Origin: v15-2-g3dedc5d <https://github.com/latchset/clevis/commit/v15-2-g3dedc5d>
  3. Upstream-Author: Sergio Correia <scorreia@redhat.com>
  4. Date: Thu Nov 19 11:54:49 2020 -0300
  5. So that we can organize them better.
  6. --- a/src/pins/tang/meson.build
  7. +++ b/src/pins/tang/meson.build
  8. @@ -1,33 +1,3 @@
  9. -actv = find_program(
  10. - 'systemd-socket-activate',
  11. - 'systemd-activate',
  12. - join_paths('/', 'usr', 'lib', 'systemd', 'systemd-activate'),
  13. - required: false
  14. -)
  15. -kgen = find_program(
  16. - join_paths(libexecdir, 'tangd-keygen'),
  17. - join_paths(get_option('prefix'), get_option('libdir'), 'tangd-keygen'),
  18. - join_paths(get_option('prefix'), get_option('libexecdir'), 'tangd-keygen'),
  19. - join_paths('/', 'usr', get_option('libdir'), 'tangd-keygen'),
  20. - join_paths('/', 'usr', get_option('libexecdir'), 'tangd-keygen'),
  21. - required: false
  22. -)
  23. -updt = find_program(
  24. - join_paths(libexecdir, 'tangd-update'),
  25. - join_paths(get_option('prefix'), get_option('libdir'), 'tangd-update'),
  26. - join_paths(get_option('prefix'), get_option('libexecdir'), 'tangd-update'),
  27. - join_paths('/', 'usr', get_option('libdir'), 'tangd-update'),
  28. - join_paths('/', 'usr', get_option('libexecdir'), 'tangd-update'),
  29. - required: false
  30. -)
  31. -tang = find_program(
  32. - join_paths(libexecdir, 'tangd'),
  33. - join_paths(get_option('prefix'), get_option('libdir'), 'tangd'),
  34. - join_paths(get_option('prefix'), get_option('libexecdir'), 'tangd'),
  35. - join_paths('/', 'usr', get_option('libdir'), 'tangd'),
  36. - join_paths('/', 'usr', get_option('libexecdir'), 'tangd'),
  37. - required: false
  38. -)
  39. curl = find_program('curl', required: false)
  40. if curl.found()
  41. @@ -35,22 +5,7 @@
  42. bins += join_paths(meson.current_source_dir(), 'clevis-encrypt-tang')
  43. mans += join_paths(meson.current_source_dir(), 'clevis-encrypt-tang.1')
  44. - if actv.found() and kgen.found() and updt.found() and tang.found()
  45. - env = environment()
  46. - env.set('SD_ACTIVATE', actv.path())
  47. - env.set('TANGD_KEYGEN', kgen.path())
  48. - env.set('TANGD_UPDATE', updt.path())
  49. - env.set('TANGD', tang.path())
  50. - env.prepend('PATH',
  51. - join_paths(meson.source_root(), 'src'),
  52. - meson.current_source_dir(),
  53. - separator: ':'
  54. - )
  55. -
  56. - test('pin-tang', find_program('./pin-tang'), env: env)
  57. - else
  58. - warning('Will not run tang tests due to missing dependencies!')
  59. - endif
  60. + subdir('tests')
  61. else
  62. warning('Will not install tang pin due to missing dependencies!')
  63. endif
  64. --- a/src/pins/tang/pin-tang
  65. +++ /dev/null
  66. @@ -1,65 +0,0 @@
  67. -#!/bin/bash -x
  68. -# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  69. -#
  70. -# Copyright (c) 2016 Red Hat, Inc.
  71. -# Author: Nathaniel McCallum <npmccallum@redhat.com>
  72. -#
  73. -# This program is free software: you can redistribute it and/or modify
  74. -# it under the terms of the GNU General Public License as published by
  75. -# the Free Software Foundation, either version 3 of the License, or
  76. -# (at your option) any later version.
  77. -#
  78. -# This program is distributed in the hope that it will be useful,
  79. -# but WITHOUT ANY WARRANTY; without even the implied warranty of
  80. -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  81. -# GNU General Public License for more details.
  82. -#
  83. -# You should have received a copy of the GNU General Public License
  84. -# along with this program. If not, see <http://www.gnu.org/licenses/>.
  85. -#
  86. -
  87. -function on_exit() {
  88. - if [ "$PID" ]; then kill $PID; wait $PID || true; fi
  89. - [ -d "$TMP" ] && rm -rf "$TMP"
  90. -}
  91. -
  92. -trap 'on_exit' EXIT
  93. -trap 'exit' ERR
  94. -
  95. -TMP="$(mktemp -d)"
  96. -mkdir -p "$TMP"/db
  97. -mkdir -p "$TMP"/cache
  98. -
  99. -# Generate the server keys
  100. -"${TANGD_KEYGEN}" "$TMP"/db sig exc
  101. -"${TANGD_UPDATE}" "$TMP"/db "$TMP"/cache
  102. -
  103. -# Start the server
  104. -port="$(shuf -i 1024-65536 -n 1)"
  105. -
  106. -inetd='--inetd'
  107. -[ "${SD_ACTIVATE##*/}" = "systemd-activate" ] && inetd=
  108. -
  109. -"$SD_ACTIVATE" $inetd -l 127.0.0.1:"$port" -a "$TANGD" "$TMP"/cache &
  110. -PID=$!
  111. -sleep 0.25
  112. -
  113. -thp="$(jose jwk thp -i "$TMP/db/sig.jwk")"
  114. -adv="$TMP/cache/default.jws"
  115. -url="http://localhost:${port}"
  116. -
  117. -cfg="$(printf '{"url":"%s","adv":"%s"}' "$url" "$adv")"
  118. -enc="$(echo -n "hi" | clevis encrypt tang "$cfg")"
  119. -dec="$(echo -n "$enc" | clevis decrypt)"
  120. -test "$dec" == "hi"
  121. -
  122. -cfg="$(printf '{"url":"%s","thp":"%s"}' "$url" "$thp")"
  123. -enc="$(echo -n "hi" | clevis encrypt tang "$cfg")"
  124. -dec="$(echo -n "$enc" | clevis decrypt)"
  125. -test "$dec" == "hi"
  126. -
  127. -kill -9 $PID
  128. -! wait $PID
  129. -unset PID
  130. -
  131. -! echo "$enc" | clevis decrypt
  132. --- /dev/null
  133. +++ b/src/pins/tang/tests/meson.build
  134. @@ -0,0 +1,64 @@
  135. +kgen = find_program(
  136. + join_paths(libexecdir, 'tangd-keygen'),
  137. + join_paths(get_option('prefix'), get_option('libdir'), 'tangd-keygen'),
  138. + join_paths(get_option('prefix'), get_option('libexecdir'), 'tangd-keygen'),
  139. + join_paths('/', 'usr', get_option('libdir'), 'tangd-keygen'),
  140. + join_paths('/', 'usr', get_option('libexecdir'), 'tangd-keygen'),
  141. + required: false
  142. +)
  143. +updt = find_program(
  144. + join_paths(libexecdir, 'tangd-update'),
  145. + join_paths(get_option('prefix'), get_option('libdir'), 'tangd-update'),
  146. + join_paths(get_option('prefix'), get_option('libexecdir'), 'tangd-update'),
  147. + join_paths('/', 'usr', get_option('libdir'), 'tangd-update'),
  148. + join_paths('/', 'usr', get_option('libexecdir'), 'tangd-update'),
  149. + required: false
  150. +)
  151. +tang = find_program(
  152. + join_paths(libexecdir, 'tangd'),
  153. + join_paths(get_option('prefix'), get_option('libdir'), 'tangd'),
  154. + join_paths(get_option('prefix'), get_option('libexecdir'), 'tangd'),
  155. + join_paths('/', 'usr', get_option('libdir'), 'tangd'),
  156. + join_paths('/', 'usr', get_option('libexecdir'), 'tangd'),
  157. + required: false
  158. +)
  159. +
  160. +socat = find_program('socat', required: false)
  161. +
  162. +tang_data = configuration_data()
  163. +tang_data.set('SOCAT', '')
  164. +tang_data.set('TANGD_KEYGEN', '')
  165. +tang_data.set('TANGD_UPDATE', '')
  166. +tang_data.set('TANGD', '')
  167. +
  168. +if socat.found()
  169. + tang_data.set('SOCAT', socat.path())
  170. +endif
  171. +
  172. +if kgen.found()
  173. + tang_data.set('TANGD_KEYGEN', kgen.path())
  174. +endif
  175. +
  176. +if tang.found()
  177. + tang_data.set('TANGD', tang.path())
  178. +endif
  179. +
  180. +if updt.found()
  181. + tang_data.set('TANGD_UPDATE', updt.path())
  182. +endif
  183. +
  184. +tang_tests_common = configure_file(
  185. + input: 'tang-common-test-functions.in',
  186. + output: 'tang-common-test-functions',
  187. + configuration: tang_data
  188. +)
  189. +
  190. +env = environment()
  191. +env.prepend('PATH',
  192. + join_paths(meson.source_root(), 'src'),
  193. + join_paths(meson.source_root(), 'src', 'pins', 'tang'),
  194. + join_paths(meson.build_root(), 'src', 'pins', 'tang', 'tests'),
  195. + separator: ':'
  196. +)
  197. +
  198. +test('pin-tang', find_program('pin-tang'), env: env)
  199. --- /dev/null
  200. +++ b/src/pins/tang/tests/pin-tang
  201. @@ -0,0 +1,55 @@
  202. +#!/bin/bash -xe
  203. +# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  204. +#
  205. +# Copyright (c) 2016 Red Hat, Inc.
  206. +# Author: Nathaniel McCallum <npmccallum@redhat.com>
  207. +#
  208. +# This program is free software: you can redistribute it and/or modify
  209. +# it under the terms of the GNU General Public License as published by
  210. +# the Free Software Foundation, either version 3 of the License, or
  211. +# (at your option) any later version.
  212. +#
  213. +# This program is distributed in the hope that it will be useful,
  214. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  215. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  216. +# GNU General Public License for more details.
  217. +#
  218. +# You should have received a copy of the GNU General Public License
  219. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  220. +#
  221. +
  222. +. tang-common-test-functions
  223. +
  224. +on_exit() {
  225. + exit_status=$?
  226. + tang_stop "${TMP}"
  227. + [ -d "$TMP" ] && rm -rf "$TMP"
  228. + exit "${exit_status}"
  229. +}
  230. +
  231. +trap 'on_exit' EXIT
  232. +
  233. +TMP="$(mktemp -d)"
  234. +
  235. +port=$(tang_new_random_port)
  236. +
  237. +tang_run "${TMP}" "${port}" sig exc
  238. +
  239. +thp="$(jose jwk thp -i "$TMP/db/sig.jwk")"
  240. +adv="${TMP}/adv.jws"
  241. +tang_get_adv "${port}" "${adv}"
  242. +url="http://localhost:${port}"
  243. +
  244. +cfg="$(printf '{"url":"%s","adv":"%s"}' "$url" "$adv")"
  245. +enc="$(echo -n "hi" | clevis encrypt tang "$cfg")"
  246. +dec="$(echo -n "$enc" | clevis decrypt)"
  247. +test "$dec" == "hi"
  248. +
  249. +cfg="$(printf '{"url":"%s","thp":"%s"}' "$url" "$thp")"
  250. +enc="$(echo -n "hi" | clevis encrypt tang "$cfg")"
  251. +dec="$(echo -n "$enc" | clevis decrypt)"
  252. +test "$dec" == "hi"
  253. +
  254. +tang_stop "${TMP}"
  255. +
  256. +! echo "$enc" | clevis decrypt
  257. --- /dev/null
  258. +++ b/src/pins/tang/tests/tang-common-test-functions.in
  259. @@ -0,0 +1,188 @@
  260. +#!/bin/bash -ex
  261. +# vim: set ts=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  262. +#
  263. +# Copyright (c) 2020 Red Hat, Inc.
  264. +# Author: Sergio Correia <scorreia@redhat.com>
  265. +#
  266. +# This program is free software: you can redistribute it and/or modify
  267. +# it under the terms of the GNU General Public License as published by
  268. +# the Free Software Foundation, either version 3 of the License, or
  269. +# (at your option) any later version.
  270. +#
  271. +# This program is distributed in the hope that it will be useful,
  272. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  273. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  274. +# GNU General Public License for more details.
  275. +#
  276. +# You should have received a copy of the GNU General Public License
  277. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  278. +#
  279. +
  280. +SOCAT="@SOCAT@"
  281. +TANGD_KEYGEN="@TANGD_KEYGEN@"
  282. +TANGD_UPDATE="@TANGD_UPDATE@"
  283. +TANGD="@TANGD@"
  284. +
  285. +tang_error() {
  286. + echo "${1}" >&2
  287. + exit 1
  288. +}
  289. +
  290. +tang_skip() {
  291. + echo "${1}" >&2
  292. + exit 77
  293. +}
  294. +
  295. +tang_sanity_check() {
  296. + [ -n "${SOCAT}" ] && [ -n "${TANGD_KEYGEN}" ] && \
  297. + [ -n "${TANGD}" ] && return 0
  298. + tang_skip "tang is not enabled/supported. Check if you have met all the requirements"
  299. +}
  300. +
  301. +# Creates a tang adv to be used in the tests.
  302. +tang_create_adv() {
  303. + local basedir="${1}"
  304. + local adv="${2:-/dev/stdout}"
  305. +
  306. + local SIG="${basedir}/sig.jwk"
  307. + jose jwk gen --input='{"alg":"ES512"}' --output="${SIG}"
  308. +
  309. + local EXC="${basedir}/exc.jwk"
  310. + jose jwk gen --input='{"alg":"ECMR"}' --output="${EXC}"
  311. +
  312. + local TEMPLATE='{"protected":{"cty":"jwk-set+json"}}'
  313. + jose jwk pub --set --input="${SIG}" --input="${EXC}" \
  314. + | jose jws sig --detached=- --signature="${TEMPLATE}" \
  315. + --key="${SIG}" --output="${adv}"
  316. +}
  317. +
  318. +# Get a random port to be used with a test tang server.
  319. +tang_new_random_port() {
  320. + tang_sanity_check
  321. + shuf -i 1024-65535 -n 1
  322. +}
  323. +
  324. +# Removes tang rotated keys from the test server.
  325. +tang_remove_rotated_keys() {
  326. + tang_sanity_check
  327. + local basedir="${1}"
  328. +
  329. + [ -z "${basedir}" ] && \
  330. + tang_error "tang_remove_rotated_keys: please specify 'basedir'"
  331. +
  332. + local db="${basedir}/db"
  333. +
  334. + mkdir -p "${db}"
  335. + pushd "${db}"
  336. + find . -name ".*.jwk" -exec rm -f {} \;
  337. + popd
  338. +
  339. + [ -n "${TANGD_UPDATE}" ] && "${TANGD_UPDATE}" "${db}" "${basedir}/cache"
  340. + return 0
  341. +}
  342. +
  343. +# Creates new keys for the test tang server.
  344. +tang_new_keys() {
  345. + tang_sanity_check
  346. + local basedir="${1}"
  347. + local rotate="${2:-}"
  348. + local sig_name="${3:-}"
  349. + local exc_name="${4:-}"
  350. +
  351. + [ -z "${basedir}" ] && tang_error "tang_new_keys: please specify 'basedir'"
  352. +
  353. + local db="${basedir}/db"
  354. + mkdir -p "${db}"
  355. +
  356. + if [ -n "${rotate}" ]; then
  357. + pushd "${db}"
  358. + local k
  359. + k=$(find . -name "*.jwk" | wc -l)
  360. + if [ "${k}" -gt 0 ]; then
  361. + for k in *.jwk; do
  362. + mv -f -- "${k}" ".${k}"
  363. + done
  364. + fi
  365. + popd
  366. + fi
  367. +
  368. + "${TANGD_KEYGEN}" "${db}" ${sig_name} ${exc_name}
  369. + [ -n "${TANGD_UPDATE}" ] && "${TANGD_UPDATE}" "${db}" "${basedir}/cache"
  370. +
  371. + return 0
  372. +}
  373. +
  374. +# Wait for the tang server to be operational.
  375. +tang_wait_until_ready() {
  376. + tang_sanity_check
  377. + local port="${1}"
  378. +
  379. + [ -z "${port}" ] && \
  380. + tang_error "tang_wait_until_ready: please specify 'port'"
  381. +
  382. + local max_timeout_in_s=5
  383. + local start elapsed
  384. + start="${SECONDS}"
  385. + while ! curl --output /dev/null --silent --fail \
  386. + "http://localhost:${port}/adv"; do
  387. + elapsed=$((SECONDS - start))
  388. + if [ "${elapsed}" -gt "${max_timeout_in_s}" ]; then
  389. + tang_error "Timeout (${max_timeout_in_s}s) waiting for tang server"
  390. + fi
  391. + sleep 0.1
  392. + echo -n . >&2
  393. + done
  394. +}
  395. +
  396. +# Start a test tang server.
  397. +tang_run() {
  398. + tang_sanity_check
  399. + local basedir="${1}"
  400. + local port="${2}"
  401. + local sig_name="${3:-}"
  402. + local exc_name="${4:-}"
  403. +
  404. + [ -z "${basedir}" ] && tang_error "tang_run: please specify 'basedir'"
  405. + [ -z "${port}" ] && tang_error "tang_run: please specify 'port'"
  406. +
  407. + if ! tang_new_keys "${basedir}" "" "${sig_name}" "${exc_name}"; then
  408. + tang_error "Error creating new keys for tang server"
  409. + fi
  410. +
  411. + local KEYS="${basedir}/cache"
  412. + [ -z "${TANGD_UPDATE}" ] && KEYS="${basedir}/db"
  413. +
  414. + local pid pidfile
  415. + pidfile="${basedir}/tang.pid"
  416. +
  417. + "${SOCAT}" -v -v TCP-LISTEN:${port},reuseaddr,fork \
  418. + exec:"${TANGD} ${KEYS}" &
  419. +
  420. + pid=$!
  421. + echo "${pid}" > "${pidfile}"
  422. + tang_wait_until_ready "${port}"
  423. +}
  424. +
  425. +# Stop tang server.
  426. +tang_stop() {
  427. + tang_sanity_check
  428. + local basedir="${1}"
  429. + [ -z "${basedir}" ] && tang_error "tang_stop: please specify 'basedir'"
  430. +
  431. + local pidfile="${basedir}/tang.pid"
  432. + [ -f "${pidfile}" ] || return 0
  433. +
  434. + local pid
  435. + pid=$(<"${pidfile}")
  436. + kill -9 "${pid}" 2>/dev/null || :
  437. +}
  438. +
  439. +# Get tang advertisement.
  440. +tang_get_adv() {
  441. + tang_sanity_check
  442. + local port="${1}"
  443. + local adv="${2:-/dev/stdout}"
  444. +
  445. + [ -z "${port}" ] && tang_error "tang_get_adv: please specify 'port'"
  446. + curl -L -o "${adv}" "http://localhost:${port}/adv"
  447. +}