#!/usr/bin/env bash set -e # change dir to where this script is running CURDIR=${PWD} SCRIPT=$(readlink -f "$0") SDIR=$(dirname "$SCRIPT") cd $SDIR if [ -z "$VOUCH_ROOT" ]; then export VOUCH_ROOT=${GOPATH}/src/github.com/vouch/vouch-proxy/ fi IMAGE=quay.io/vouch/vouch-proxy:latest ALPINE=quay.io/vouch/vouch-proxy:alpine-latest GOIMAGE=golang:1.16 NAME=vouch-proxy HTTPPORT=9090 GODOC_PORT=5050 run () { go run main.go } build () { local VERSION=$(git describe --always --long) local DT=$(date -u +"%Y-%m-%dT%H:%M:%SZ") # ISO-8601 local FQDN=$(_hostname) local SEMVER=$(git tag --list --sort="v:refname" | tail -n -1) local BRANCH=$(git rev-parse --abbrev-ref HEAD) local UNAME=$(uname) go build -v -ldflags=" -X main.version=${VERSION} -X main.uname=${UNAME} -X main.builddt=${DT} -X main.host=${FQDN} -X main.semver=${SEMVER} -X main.branch=${BRANCH}" . } _hostname() { local FQDN local HOSTNAME_CMD case $(uname) in FreeBSD) HOSTNAME_CMD="hostname";; *) HOSTNAME_CMD="hostname --fqdn" esac FQDN=$($HOSTNAME_CMD) if [ -z "$FQDN" ]; then >&2 echo "error: Could determine the fully qualified domain name using command $HOSTNAME_CMD" return 1 fi echo $FQDN return 0; } install () { cp ./vouch-proxy ${GOPATH}/bin/vouch-proxy } gogo () { docker run --rm -i -t -v /var/run/docker.sock:/var/run/docker.sock -v ${SDIR}/go:/go --name gogo $GOIMAGE $* } dbuild () { docker build -f Dockerfile -t $IMAGE . } dbuildalpine () { docker build -f Dockerfile.alpine -t $ALPINE . } gobuildstatic () { export CGO_ENABLED=0 export GOOS=linux build } drun () { if [ "$(docker ps | grep $NAME)" ]; then docker stop $NAME docker rm $NAME fi WITHCERTS="" if [ -d "${SDIR}/certs" ] && [ -z $(find ${SDIR}/certs -type d -empty) ]; then WITHCERTS="-v ${SDIR}/certs:/certs" fi CMD="docker run --rm -i -t -p ${HTTPPORT}:${HTTPPORT} --name $NAME -v ${SDIR}/config:/config $WITHCERTS $IMAGE $* " echo $CMD $CMD } drunalpine () { IMAGE=$ALPINE drun $* } watch () { CMD=$@; if [ -z "$CMD" ]; then CMD="go run main.go" fi clear echo -e "starting watcher for:\n\t$CMD" # TODO: add *.tmpl and *.css # find . -type f -name '*.css' | entr -cr $CMD find . -name '*.go' | entr -cr $CMD } goget () { # install all the things go get -u -v ./... go mod tidy } REDACT="" bug_report() { set +e # CONFIG=$1; shift; CONFIG=config/config.yml REDACT=$* if [ -z "$REDACT" ]; then cat <&1 | _redact } _redact_exit () { echo -e "\n\n-------------------------\n" echo -e "redact your nginx config with:\n" echo -e "\tcat nginx.conf | sed 's/yourdomain.com/DOMAIN.COM/g'\n" echo -e "Please upload configs and logs to a gist and open an issue on GitHub at https://github.com/vouch/vouch-proxy/issues\n" } _redact() { SECRET_FIELDS=("client_id client_secret secret ClientSecret ClientID") while IFS= read -r LINE; do for i in $SECRET_FIELDS; do LINE=$(echo "$LINE" | sed -r "s/${i}..[[:graph:]]*\>/${i}: XXXXXXXXXXX/g") done # j=$(expr $j + 1) for i in $REDACT; do r=$i r=$(echo "$r" | sed "s/[[:alnum:]]/+/g") # LINE=$(echo "$LINE" | sed "s/${i}/+++++++/g") LINE=$(echo "$LINE" | sed "s/${i}/${r}/g") done echo "${LINE}" done } coverage() { mkdir -p .cover && go test -v -coverprofile=.cover/cover.out ./... } coveragereport() { go tool cover -html=.cover/cover.out -o .cover/coverage.html } test() { if [ -z "$VOUCH_CONFIG" ]; then export VOUCH_CONFIG="$SDIR/config/testing/test_config.yml" fi TEST_PRIVATE_KEY_FILE="$SDIR/config/testing/rsa.key" TEST_PUBLIC_KEY_FILE="$SDIR/config/testing/rsa.pub" if [[ ! -f "$TEST_PRIVATE_KEY_FILE" ]]; then openssl genrsa -out "$TEST_PRIVATE_KEY_FILE" 4096 fi if [[ ! -f "$TEST_PUBLIC_KEY_FILE" ]]; then openssl rsa -in "$TEST_PRIVATE_KEY_FILE" -pubout > "$TEST_PUBLIC_KEY_FILE" fi go get -t ./... # test all the things if [ -n "$*" ]; then # go test -v -race $EXTRA_TEST_ARGS $* go test -race $EXTRA_TEST_ARGS $* else # go test -v -race $EXTRA_TEST_ARGS ./... go test -race $EXTRA_TEST_ARGS ./... fi } test_logging() { build declare -a levels=(error warn info debug) echo "testing loglevel set from command line" levelcount=0 for ll in ${levels[*]}; do # test that we can see the current level and no level below this level declare -a shouldnotfind=() for (( i=0; i<${#levels[@]}; i++ )); do if (( $i > $levelcount )); then shouldnotfind+=(${levels[$i]}) fi done linesread=0 IFS=$'\n';for line in $(./vouch-proxy -logtest -loglevel ${ll} -config ./config/testing/test_config.yml); do let "linesread+=1" # echo "$linesread $line" # first line is log info if (( $linesread > 1 )); then for nono in ${shouldnotfind[*]} ; do if echo $line | grep $nono; then echo "error: line should not contain '$nono' when loglevel is '$ll'" echo "$linesread $line" exit 1; fi done fi done let "levelcount+=1" done echo "passed" echo "testing loglevel set from config file" levelcount=0 for ll in ${levels[*]}; do # test that we can see the current level and no level below this level declare -a shouldnotfind=() for (( i=0; i<${#levels[@]}; i++ )); do if (( $i > $levelcount )); then shouldnotfind+=(${levels[$i]}) fi done linesread=0 IFS=$'\n';for line in $(./vouch-proxy -logtest -config ./config/testing/logging_${ll}.yml); do let "linesread+=1" # the first four messages are log and info when starting from the command line if (( $linesread > 4 )); then # echo "$linesread $line" for nono in ${shouldnotfind[*]} ; do # echo "testing $nono" if echo $line | grep $nono; then echo "error: line should not contain '$nono' when loglevel is '$ll'" echo "$linesread $line" exit 1; fi done fi done let "levelcount+=1" done echo "passed" exit 0 } stats () { echo -n "lines of code: " find . -name '*.go' | xargs wc -l | grep total echo -n "number of go files: " find . -name '*.go' | wc -l echo -n "number of covered packages: " covered=$(coverage | grep ok | wc -l) echo $covered echo -n "number of packages not covered: " coverage | grep -v ok | wc -l echo -n "average of coverage for all covered packages: " sumcoverage=$(coverage | grep ok | awk '{print $5}' | sed 's/%//' | paste -sd+ - | bc) # echo " sumcoverage: $sumcoverage " perl -le "print $sumcoverage/$covered, '%'" exit 0; } license() { local FILE=$1; if [ ! -f "${FILE}" ]; then echo "need filename"; exit 1; fi FOUND=$(_has_license $FILE) if [ -z "${FOUND}" ]; then local YEAR=$(git log -1 --format="%ai" -- $FILE | cut -d- -f1); _print_license $YEAR > ${FILE}_licensed cat $FILE >> ${FILE}_licensed mv ${FILE}_licensed $FILE echo "added license to the header of $FILE" fi # and then format the codebase gofmt } _print_license() { local YEAR=$1; if [ -z "$YEAR" ]; then YEAR=$(date +%Y) fi cat <