...

Text file src/github.com/cert-manager/issuer-lib/make/_shared/tools/util/checkhash.sh

Documentation: github.com/cert-manager/issuer-lib/make/_shared/tools/util

     1#!/usr/bin/env bash
     2
     3# Copyright 2023 The cert-manager Authors.
     4#
     5# Licensed under the Apache License, Version 2.0 (the "License");
     6# you may not use this file except in compliance with the License.
     7# You may obtain a copy of the License at
     8#
     9#     http://www.apache.org/licenses/LICENSE-2.0
    10#
    11# Unless required by applicable law or agreed to in writing, software
    12# distributed under the License is distributed on an "AS IS" BASIS,
    13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14# See the License for the specific language governing permissions and
    15# limitations under the License.
    16
    17set -o errexit
    18set -o nounset
    19set -o pipefail
    20
    21SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
    22
    23# This script takes the hash of its first argument and verifies it against the
    24# hex hash given in its second argument
    25
    26function usage_and_exit() {
    27	echo "usage: $0 <path-to-target> <expected-hash>"
    28	echo "or: LEARN_FILE=<path-to-learn-file> $0 <path-to-target> <old-hash>"
    29	exit 1
    30}
    31
    32HASH_TARGET=${1:-}
    33EXPECTED_HASH=${2:-}
    34
    35if [[ -z $HASH_TARGET ]]; then
    36	usage_and_exit
    37fi
    38
    39if [[ -z $EXPECTED_HASH ]]; then
    40	usage_and_exit
    41fi
    42
    43SHASUM=$("${SCRIPT_DIR}/hash.sh" "$HASH_TARGET")
    44
    45if [[ "$SHASUM" == "$EXPECTED_HASH" ]]; then
    46	exit 0
    47fi
    48
    49# When running 'make learn-sha-tools', we don't want this script to fail.
    50# Instead we log what sha values are wrong, so the make.mk file can be updated.
    51
    52if [ "${LEARN_FILE:-}" != "" ]; then
    53	echo "s/$EXPECTED_HASH/$SHASUM/g" >> "${LEARN_FILE:-}"
    54	exit 0
    55fi
    56
    57echo "invalid checksum for \"$HASH_TARGET\": wanted \"$EXPECTED_HASH\" but got \"$SHASUM\""
    58exit 1

View as plain text