naughty-from.sh 3.8 KB

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