naughty-from.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/env bash
  2. set -Eeuo pipefail
  3. : "${BASHBREW_CACHE:=$HOME/.cache/bashbrew}"
  4. export BASHBREW_CACHE BASHBREW_ARCH=
  5. if [ ! -d "$BASHBREW_CACHE/git" ]; then
  6. # initialize the "bashbrew cache"
  7. bashbrew --arch amd64 from --uniq --apply-constraints hello-world:linux > /dev/null
  8. fi
  9. if [ "$#" -eq 0 ]; then
  10. set -- '--all'
  11. fi
  12. _is_naughty() {
  13. local from="$1"; shift
  14. case "$BASHBREW_ARCH=$from" in
  15. # a few images that no longer exist (and are thus not permissible)
  16. # https://techcommunity.microsoft.com/t5/Containers/Removing-the-latest-Tag-An-Update-on-MCR/ba-p/393045
  17. *=mcr.microsoft.com/windows/*:latest) return 0 ;;
  18. # 20H2 is not *technically* EOL until 05/10/2022, but its use is discouraged here given the existence of ltsc2022
  19. # 2004 is not *technically* EOL until 12/14/2021, but its use is discouraged here given the existence of ltsc2022
  20. # https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/base-image-lifecycle
  21. # "05/11/2021"
  22. *=mcr.microsoft.com/windows/*:1909*) return 0 ;;
  23. # https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/base-image-lifecycle
  24. # "12/08/2020"
  25. *=mcr.microsoft.com/windows/*:1903*) return 0 ;;
  26. # https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/base-image-lifecycle
  27. # "11/12/2019"
  28. *=mcr.microsoft.com/windows/*:1803*) return 0 ;;
  29. # https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/base-image-lifecycle
  30. # "04/09/2019"
  31. *=mcr.microsoft.com/windows/*:1709*) return 0 ;;
  32. # https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/base-image-lifecycle
  33. # "10/09/2018"
  34. *=mcr.microsoft.com/windows/nanoserver:sac2016) return 0 ;;
  35. # a few explicitly permissible exceptions to Santa's naughty list
  36. *=scratch \
  37. | amd64=docker.elastic.co/elasticsearch/elasticsearch:* \
  38. | amd64=docker.elastic.co/kibana/kibana:* \
  39. | amd64=docker.elastic.co/logstash/logstash:* \
  40. | arm64v8=docker.elastic.co/elasticsearch/elasticsearch:* \
  41. | arm64v8=docker.elastic.co/kibana/kibana:* \
  42. | arm64v8=docker.elastic.co/logstash/logstash:* \
  43. | windows-*=mcr.microsoft.com/windows/nanoserver:* \
  44. | windows-*=mcr.microsoft.com/windows/servercore:* \
  45. ) return 1 ;;
  46. # "x/y" and not an approved exception
  47. */*) return 0 ;;
  48. esac
  49. # must be some other official image AND support our current architecture
  50. local archSupported
  51. if archSupported="$(bashbrew cat --format '{{ .TagEntry.HasArchitecture arch | ternary arch "" }}' "$from")" && [ -n "$archSupported" ]; then
  52. return 1
  53. fi
  54. return 0
  55. }
  56. _arches() {
  57. bashbrew cat --format '
  58. {{- range .TagEntries -}}
  59. {{- .Architectures | join "\n" -}}
  60. {{- "\n" -}}
  61. {{- end -}}
  62. ' "$@" | sort -u
  63. }
  64. _froms() {
  65. bashbrew cat --format '
  66. {{- range .TagEntries -}}
  67. {{- $.DockerFroms . | join "\n" -}}
  68. {{- "\n" -}}
  69. {{- end -}}
  70. ' "$@" | sort -u
  71. }
  72. declare -A naughtyFromsArches=(
  73. #[img:tag=from:tag]='arch arch ...'
  74. )
  75. naughtyFroms=()
  76. declare -A allNaughty=(
  77. #[img:tag]=1
  78. )
  79. tags="$(bashbrew --namespace '' list --uniq "$@" | sort -u)"
  80. for img in $tags; do
  81. arches="$(_arches "$img")"
  82. hasNice= # do we have _any_ arches that aren't naughty? (so we can make the message better if not)
  83. for BASHBREW_ARCH in $arches; do
  84. export BASHBREW_ARCH
  85. froms="$(_froms "$img")"
  86. [ -n "$froms" ] # rough sanity check
  87. for from in $froms; do
  88. if _is_naughty "$from"; then
  89. if [ -z "${naughtyFromsArches["$img=$from"]:-}" ]; then
  90. naughtyFroms+=( "$img=$from" )
  91. else
  92. naughtyFromsArches["$img=$from"]+=', '
  93. fi
  94. naughtyFromsArches["$img=$from"]+="$BASHBREW_ARCH"
  95. else
  96. hasNice=1
  97. fi
  98. done
  99. done
  100. if [ -z "$hasNice" ]; then
  101. allNaughty["$img"]=1
  102. fi
  103. done
  104. for naughtyFrom in "${naughtyFroms[@]:-}"; do
  105. [ -n "$naughtyFrom" ] || continue # https://mywiki.wooledge.org/BashFAQ/112#BashFAQ.2F112.line-8 (empty array + "set -u" + bash 4.3 == sad day)
  106. img="${naughtyFrom%%=*}"
  107. from="${naughtyFrom#$img=}"
  108. if [ -n "${allNaughty["$img"]:-}" ]; then
  109. echo " - $img (FROM $from) -- completely unsupported base!"
  110. else
  111. arches="${naughtyFromsArches[$naughtyFrom]}"
  112. echo " - $img (FROM $from) [$arches]"
  113. fi
  114. done