naughty-from.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 \
  18. | *=microsoft/*:latest \
  19. ) return 0 ;;
  20. # https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/base-image-lifecycle
  21. # "04/09/2019"
  22. *=mcr.microsoft.com/windows/*:1709* \
  23. | *=microsoft/*:1709* \
  24. ) return 0 ;;
  25. # https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/base-image-lifecycle
  26. # "10/09/2018"
  27. *=mcr.microsoft.com/windows/nanoserver:sac2016 \
  28. | *=microsoft/nanoserver:sac2016 \
  29. ) return 0 ;;
  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. | windows-*=mcr.microsoft.com/windows/nanoserver:* \
  36. | windows-*=mcr.microsoft.com/windows/servercore:* \
  37. | windows-*=microsoft/nanoserver:* \
  38. | windows-*=microsoft/windowsservercore:* \
  39. ) return 1 ;;
  40. # "x/y" and not an approved exception
  41. */*) return 0 ;;
  42. esac
  43. # must be some other official image AND support our current architecture
  44. local archSupported
  45. if archSupported="$(bashbrew cat --format '{{ .TagEntry.HasArchitecture arch | ternary arch "" }}' "$from")" && [ -n "$archSupported" ]; then
  46. return 1
  47. fi
  48. return 0
  49. }
  50. _arches() {
  51. bashbrew cat --format '
  52. {{- range .TagEntries -}}
  53. {{- .Architectures | join "\n" -}}
  54. {{- "\n" -}}
  55. {{- end -}}
  56. ' "$@" | sort -u
  57. }
  58. _froms() {
  59. bashbrew cat --format '
  60. {{- range .TagEntries -}}
  61. {{- $.DockerFroms . | join "\n" -}}
  62. {{- "\n" -}}
  63. {{- end -}}
  64. ' "$@" | sort -u
  65. }
  66. declare -A naughtyFromsArches=(
  67. #[img:tag=from:tag]='arch arch ...'
  68. )
  69. naughtyFroms=()
  70. declare -A allNaughty=(
  71. #[img:tag]=1
  72. )
  73. tags="$(bashbrew list --uniq "$@" | sort -u)"
  74. for img in $tags; do
  75. arches="$(_arches "$img")"
  76. hasNice= # do we have _any_ arches that aren't naughty? (so we can make the message better if not)
  77. for BASHBREW_ARCH in $arches; do
  78. export BASHBREW_ARCH
  79. if ! froms="$(_froms "$img" 2>/dev/null)"; then
  80. # if we can't fetch the tags from their real locations, let's try the warehouse
  81. refsList="$(
  82. bashbrew list --uniq "$img" \
  83. | sed \
  84. -e 's!:!/!' \
  85. -e "s!^!refs/tags/$BASHBREW_ARCH/!" \
  86. -e 's!$!:!'
  87. )"
  88. [ -n "$refsList" ]
  89. git -C "$BASHBREW_CACHE/git" \
  90. fetch --no-tags --quiet \
  91. https://github.com/docker-library/commit-warehouse.git \
  92. $refsList
  93. froms="$(_froms "$img")"
  94. fi
  95. [ -n "$froms" ] # rough sanity check
  96. for from in $froms; do
  97. if _is_naughty "$from"; then
  98. if [ -z "${naughtyFromsArches["$img=$from"]:-}" ]; then
  99. naughtyFroms+=( "$img=$from" )
  100. else
  101. naughtyFromsArches["$img=$from"]+=', '
  102. fi
  103. naughtyFromsArches["$img=$from"]+="$BASHBREW_ARCH"
  104. else
  105. hasNice=1
  106. fi
  107. done
  108. done
  109. if [ -z "$hasNice" ]; then
  110. allNaughty["$img"]=1
  111. fi
  112. done
  113. for naughtyFrom in "${naughtyFroms[@]:-}"; do
  114. [ -n "$naughtyFrom" ] || continue # https://mywiki.wooledge.org/BashFAQ/112#BashFAQ.2F112.line-8 (empty array + "set -u" + bash 4.3 == sad day)
  115. img="${naughtyFrom%%=*}"
  116. from="${naughtyFrom#$img=}"
  117. if [ -n "${allNaughty["$img"]:-}" ]; then
  118. echo " - $img (FROM $from) -- completely unsupported base!"
  119. else
  120. arches="${naughtyFromsArches[$naughtyFrom]}"
  121. echo " - $img (FROM $from) [$arches]"
  122. fi
  123. done