naughty-from.sh 3.9 KB

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