|
| 1 | +#! /usr/bin/env bash |
| 2 | +# this script builds a go binary after applying openssl fips patches one by one. |
| 3 | +# |
| 4 | +export GOLANG_VER=1.22.2 |
| 5 | +export GOLANG_REL=1 |
| 6 | + |
| 7 | +# variables to store tar file names |
| 8 | +GO_TAR_NAME=go$GOLANG_VER |
| 9 | +PATCH_TAR_NAME=go$GOLANG_VER-$GOLANG_REL-openssl-fips |
| 10 | +GO_SRC_TAR=$GO_TAR_NAME.tar.gz |
| 11 | +PATCH_TAR=$PATCH_TAR_NAME.tar.gz |
| 12 | + |
| 13 | +# after untar, the dir names are stored in PATCH_DIR and SRC_DIR |
| 14 | +SRC_DIR=go-$GO_TAR_NAME |
| 15 | +PATCH_DIR=go-$PATCH_TAR_NAME |
| 16 | +TOP_DIR=/tmp/golang-fips/build2 |
| 17 | +# If the go src dir ends up in a weird state where in patches are failing to apply, you might want to remove the $TOP_DIR (rm -rf /tmp/golang-fips/build2) to start with a clean slate. |
| 18 | +PATCH_PATH=$TOP_DIR/$PATCH_DIR/patches |
| 19 | + |
| 20 | +# this file stores state of last applied patch, that way patches are not reapplied |
| 21 | +PATCH_STATE=$TOP_DIR/state |
| 22 | +set -x |
| 23 | +[[ -d $TOP_DIR ]] || mkdir -p $TOP_DIR |
| 24 | +cd $TOP_DIR |
| 25 | + |
| 26 | +[[ -f $GO_SRC_TAR ]] || wget -q --show-progress https://github.com/golang/go/archive/refs/tags/$GO_SRC_TAR |
| 27 | +[[ -f $PATCH_TAR ]] || wget -q --show-progress https://github.com/golang-fips/go/archive/refs/tags/$PATCH_TAR |
| 28 | +[[ -d $SRC_DIR ]] || tar -xf $GO_SRC_TAR |
| 29 | +[[ -d $PATCH_DIR ]] || tar -xf $PATCH_TAR |
| 30 | + |
| 31 | +cd $SRC_DIR |
| 32 | +{ set +x; } 2>/dev/null |
| 33 | +if [[ -f $PATCH_STATE && "$(cat $PATCH_STATE)" == "complete" ]]; then |
| 34 | + echo "patches already applied. skipping" |
| 35 | +else |
| 36 | + for patch in $PATCH_PATH/*.patch; do |
| 37 | + patch -p1 < "${patch}" |
| 38 | + [[ $? -eq 0 ]] || { echo "incomplete" > $PATCH_STATE; break; exit 1; } |
| 39 | + done |
| 40 | +fi |
| 41 | + |
| 42 | +echo "complete" > $PATCH_STATE |
| 43 | +# patches have been supplied successfully, simply build like above. |
| 44 | +set -x |
| 45 | +cd src |
| 46 | +export GO_BUILDTAGS+="goexperiment.strictfipsruntime,!no_openssl" |
| 47 | +export CGO_ENABLED=1 |
| 48 | +./make.bash --no-clean |
| 49 | +{ set +x; } 2>/dev/null |
0 commit comments