Skip to content

Commit 6a2700b

Browse files
authored
Bash hardening and code consistency improvements (#248)
1 parent c6b3399 commit 6a2700b

11 files changed

Lines changed: 209 additions & 222 deletions

File tree

bin/compile

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
11
#!/usr/bin/env bash
2-
# bin/compile <build-dir> <cache-dir> <env-dir>
32

4-
set -e
3+
set -euo pipefail
54

6-
BP_DIR=$(
7-
cd "$(dirname "${0}")/"..
8-
pwd
9-
)
5+
BUILD_DIR="${1}"
6+
CACHE_DIR="${2}"
7+
ENV_DIR="${3}"
108

11-
BUILD_DIR=$1
12-
CACHE_DIR=$2
13-
ENV_DIR=$3
9+
BUILDPACK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)"
1410

15-
source "${BP_DIR}/lib/util.sh"
16-
source "${BP_DIR}/lib/common.sh"
17-
source "${BP_DIR}/lib/maven.sh"
18-
source "${BP_DIR}/lib/metrics.sh"
11+
source "${BUILDPACK_DIR}/lib/util.sh"
12+
source "${BUILDPACK_DIR}/lib/common.sh"
13+
source "${BUILDPACK_DIR}/lib/maven.sh"
14+
source "${BUILDPACK_DIR}/lib/metrics.sh"
1915

20-
# Initialise the buildpack metadata store.
21-
# This is used to track state across builds (for cache invalidation and messaging when build
22-
# configuration changes) and also so that `bin/report` can generate the build report.
2316
metrics::init "${CACHE_DIR}" "java"
2417
metrics::setup
2518

26-
export_env_dir "${ENV_DIR}" "." "JAVA_OPTS|JAVA_TOOL_OPTIONS"
19+
util::export_env_dir "${ENV_DIR}" "." "JAVA_OPTS|JAVA_TOOL_OPTIONS"
2720

28-
install_jdk "${BUILD_DIR}" "${CACHE_DIR}"
21+
common::install_jdk "${BUILD_DIR}" "${CACHE_DIR}"
2922

30-
run_mvn "compile" "${BUILD_DIR}" "${CACHE_DIR}"
31-
remove_mvn "${BUILD_DIR}" "${CACHE_DIR}"
23+
maven::run_mvn "compile" "${BUILD_DIR}" "${CACHE_DIR}"
24+
maven::remove_mvn "${BUILD_DIR}" "${CACHE_DIR}"

bin/detect

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
#!/usr/bin/env bash
22

3-
if [ -f "${1:?}/pom.xml" ] ||
4-
[ -f "${1:?}/pom.atom" ] ||
5-
[ -f "${1:?}/pom.clj" ] ||
6-
[ -f "${1:?}/pom.groovy" ] ||
7-
[ -f "${1:?}/pom.rb" ] ||
8-
[ -f "${1:?}/pom.scala" ] ||
9-
[ -f "${1:?}/pom.yaml" ] ||
10-
[ -f "${1:?}/pom.yml" ]; then
3+
set -euo pipefail
4+
5+
BUILD_DIR="${1}"
6+
7+
if [[ -f "${BUILD_DIR}/pom.xml" ]] ||
8+
[[ -f "${BUILD_DIR}/pom.atom" ]] ||
9+
[[ -f "${BUILD_DIR}/pom.clj" ]] ||
10+
[[ -f "${BUILD_DIR}/pom.groovy" ]] ||
11+
[[ -f "${BUILD_DIR}/pom.rb" ]] ||
12+
[[ -f "${BUILD_DIR}/pom.scala" ]] ||
13+
[[ -f "${BUILD_DIR}/pom.yaml" ]] ||
14+
[[ -f "${BUILD_DIR}/pom.yml" ]]; then
1115
echo "Java"
1216
exit 0
1317
else

bin/release

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,39 @@
11
#!/usr/bin/env bash
2-
# bin/release <build-dir>
32

4-
set -e
3+
set -euo pipefail
54

6-
BP_DIR=$(
7-
cd "$(dirname "${0}")"/..
8-
pwd
9-
)
10-
BUILD_DIR=$1
5+
BUILD_DIR="${1}"
116

12-
source "${BP_DIR}/lib/frameworks.sh"
7+
BUILDPACK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)"
8+
9+
source "${BUILDPACK_DIR}/lib/frameworks.sh"
1310

1411
echo "---"
1512

16-
if has_postgres "${BUILD_DIR}"; then
13+
if frameworks::has_postgres "${BUILD_DIR}"; then
1714
cat <<EOF
1815
addons:
1916
- heroku-postgresql
2017
EOF
2118
fi
2219

23-
if [ ! -f "${BUILD_DIR}/Procfile" ] && [ -d "${BUILD_DIR}/target" ]; then
24-
if is_quarkus "${BUILD_DIR}"; then
20+
if [[ ! -f "${BUILD_DIR}/Procfile" ]] && [[ -d "${BUILD_DIR}/target" ]]; then
21+
if frameworks::is_quarkus "${BUILD_DIR}"; then
2522
echo "default_process_types:"
2623
echo " web: java -Dquarkus.http.port=\$PORT \$JAVA_OPTS -jar target/quarkus-app/quarkus-run.jar"
2724
else
2825
cd "${BUILD_DIR}"
2926
# shellcheck disable=SC2044
30-
for jarFile in $(find target -maxdepth 1 -name "*.jar" -type f -exec ls -S {} +); do
31-
if is_spring_boot "${BUILD_DIR}"; then
27+
for jar_file in $(find target -maxdepth 1 -name "*.jar" -type f -exec ls -S {} +); do
28+
if frameworks::is_spring_boot "${BUILD_DIR}"; then
3229
echo "default_process_types:"
33-
echo " web: java -Dserver.port=\$PORT \$JAVA_OPTS -jar ${jarFile}"
34-
elif is_wildfly_swarm "${BUILD_DIR}"; then
30+
echo " web: java -Dserver.port=\$PORT \$JAVA_OPTS -jar ${jar_file}"
31+
elif frameworks::is_wildfly_swarm "${BUILD_DIR}"; then
3532
echo "default_process_types:"
36-
echo " web: java -Dswarm.http.port=\$PORT \$JAVA_OPTS -jar ${jarFile}"
37-
elif is_micronaut "${BUILD_DIR}"; then
33+
echo " web: java -Dswarm.http.port=\$PORT \$JAVA_OPTS -jar ${jar_file}"
34+
elif frameworks::is_micronaut "${BUILD_DIR}"; then
3835
echo "default_process_types:"
39-
echo " web: java -Dmicronaut.server.port=\$PORT \$JAVA_OPTS -jar ${jarFile}"
36+
echo " web: java -Dmicronaut.server.port=\$PORT \$JAVA_OPTS -jar ${jar_file}"
4037
fi
4138
break
4239
done

bin/report

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env bash
2-
# Usage: bin/report <build-dir> <cache-dir> <env-dir>
32

43
# Produces a build report containing metadata about the build, that's consumed by the build system.
54
# This script is run for both successful and failing builds, so it should not assume the build ran
@@ -20,9 +19,10 @@
2019
set -euo pipefail
2120
shopt -s inherit_errexit
2221

22+
# BUILD_DIR="${1}"
2323
CACHE_DIR="${2}"
24+
# ENV_DIR="${3}"
2425

25-
# The absolute path to the root of the buildpack.
2626
BUILDPACK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)"
2727

2828
# The build system doesn't source the `export` script before running this script, so we have to do

bin/test

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
#!/usr/bin/env bash
2-
# bin/test <build-dir> <env-dir> <artifact-dir>
32

4-
# fail fast
5-
set -e
3+
set -euo pipefail
64

7-
BP_DIR=$(
8-
cd "$(dirname "${0}")"/..
9-
pwd
10-
)
11-
BUILD_DIR=$1
12-
ENV_DIR=$2
5+
BUILD_DIR="${1}"
6+
ENV_DIR="${2}"
7+
# ARTIFACT_DIR="${3}"
138

14-
source "${BP_DIR}/lib/util.sh"
15-
source "${BP_DIR}/lib/common.sh"
16-
source "${BP_DIR}/lib/maven.sh"
9+
BUILDPACK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)"
1710

18-
export_env_dir "${ENV_DIR}" "." "JAVA_OPTS|JAVA_TOOL_OPTIONS"
11+
source "${BUILDPACK_DIR}/lib/util.sh"
12+
source "${BUILDPACK_DIR}/lib/common.sh"
13+
source "${BUILDPACK_DIR}/lib/maven.sh"
14+
15+
util::export_env_dir "${ENV_DIR}" "." "JAVA_OPTS|JAVA_TOOL_OPTIONS"
1916

2017
cd "${BUILD_DIR}"
2118

22-
mvn_settings_opt="$(_mvn_settings_opt "${BUILD_DIR}" "${BUILD_DIR}")"
19+
mvn_settings_opt="$(maven::mvn_settings_opt "${BUILD_DIR}" "${BUILD_DIR}")"
2320

24-
if has_maven_wrapper "${BUILD_DIR}"; then
21+
if maven::has_maven_wrapper "${BUILD_DIR}"; then
2522
# shellcheck disable=SC2086
2623
./mvnw -B ${mvn_settings_opt} "${MAVEN_HEROKU_CI_GOAL:-test}"
2724
else

bin/test-compile

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
#!/usr/bin/env bash
2-
# bin/test-compile <build-dir> <cache-dir> <env-dir>
32

4-
# fail fast
5-
set -e
3+
set -euo pipefail
64

7-
BP_DIR=$(
8-
cd "$(dirname "${0}")"/..
9-
pwd
10-
)
11-
BUILD_DIR=$1
12-
CACHE_DIR=$2
13-
ENV_DIR=$3
5+
BUILD_DIR="${1}"
6+
CACHE_DIR="${2}"
7+
ENV_DIR="${3}"
148

15-
source "${BP_DIR}/lib/util.sh"
16-
source "${BP_DIR}/lib/common.sh"
17-
source "${BP_DIR}/lib/maven.sh"
9+
BUILDPACK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)"
1810

19-
export_env_dir "${ENV_DIR}" "." "JAVA_OPTS|JAVA_TOOL_OPTIONS"
11+
source "${BUILDPACK_DIR}/lib/util.sh"
12+
source "${BUILDPACK_DIR}/lib/common.sh"
13+
source "${BUILDPACK_DIR}/lib/maven.sh"
2014

21-
install_jdk "${BUILD_DIR}"
15+
util::export_env_dir "${ENV_DIR}" "." "JAVA_OPTS|JAVA_TOOL_OPTIONS"
16+
17+
common::install_jdk "${BUILD_DIR}" "${CACHE_DIR}"
2218

2319
cd "${BUILD_DIR}"
2420

25-
cache_copy ".m2" "${CACHE_DIR}" "${BUILD_DIR}"
26-
run_mvn "test-compile" "${BUILD_DIR}" "${BUILD_DIR}"
27-
write_mvn_profile "${BUILD_DIR}"
28-
cache_copy ".m2" "${BUILD_DIR}" "${CACHE_DIR}"
21+
common::cache_copy ".m2" "${CACHE_DIR}" "${BUILD_DIR}"
22+
maven::run_mvn "test-compile" "${BUILD_DIR}" "${BUILD_DIR}"
23+
maven::write_mvn_profile "${BUILD_DIR}"
24+
common::cache_copy ".m2" "${BUILD_DIR}" "${CACHE_DIR}"

lib/common.sh

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
#!/usr/bin/env bash
22

3+
# This is technically redundant, since all consumers of this lib will have enabled these,
4+
# however, it helps Shellcheck realise the options under which these functions will run.
5+
set -euo pipefail
6+
37
export DEFAULT_MAVEN_VERSION="3.9.4"
48

5-
install_maven() {
6-
local installDir=$1
7-
local buildDir=$2
8-
mavenHome=$installDir/.maven
9+
common::install_maven() {
10+
local install_dir="${1}"
11+
local build_dir="${2}"
12+
local maven_home="${install_dir}/.maven"
913

10-
definedMavenVersion=$(detect_maven_version "${buildDir}")
14+
local defined_maven_version
15+
defined_maven_version=$(common::detect_maven_version "${build_dir}")
1116

12-
mavenVersion=${definedMavenVersion:-$DEFAULT_MAVEN_VERSION}
17+
local maven_version="${defined_maven_version:-${DEFAULT_MAVEN_VERSION}}"
1318

14-
status_pending "Installing Maven ${mavenVersion}"
15-
local mavenUrl="https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/${mavenVersion}/apache-maven-${mavenVersion}-bin.tar.gz"
16-
if is_supported_maven_version "${mavenVersion}" "${mavenUrl}"; then
17-
download_maven "${mavenUrl}" "${mavenHome}"
19+
status_pending "Installing Maven ${maven_version}"
20+
local maven_url="https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/${maven_version}/apache-maven-${maven_version}-bin.tar.gz"
21+
if common::is_supported_maven_version "${maven_version}" "${maven_url}"; then
22+
common::download_maven "${maven_url}" "${maven_home}"
1823
status_done
1924
else
2025
error_return "Error, you have defined an unsupported Maven version in the system.properties file.
@@ -23,34 +28,35 @@ The default supported version is ${DEFAULT_MAVEN_VERSION}"
2328
fi
2429
}
2530

26-
download_maven() {
27-
local mavenUrl=$1
28-
local installDir=$2
31+
common::download_maven() {
32+
local maven_url="${1}"
33+
local install_dir="${2}"
2934

30-
rm -rf "${installDir}"
31-
mkdir -p "${installDir}"
32-
curl --fail --retry 3 --retry-connrefused --connect-timeout 5 --silent --max-time 60 --location "${mavenUrl}" | tar -xzm --strip-components 1 -C "${installDir}"
33-
chmod +x "${installDir}/bin/mvn"
35+
rm -rf "${install_dir}"
36+
mkdir -p "${install_dir}"
37+
curl --fail --retry 3 --retry-connrefused --connect-timeout 5 --silent --max-time 60 --location "${maven_url}" | tar -xzm --strip-components 1 -C "${install_dir}"
38+
chmod +x "${install_dir}/bin/mvn"
3439
}
3540

36-
is_supported_maven_version() {
37-
local mavenVersion=${1}
38-
local mavenUrl=${2:?}
39-
if [ "$mavenVersion" = "$DEFAULT_MAVEN_VERSION" ]; then
41+
common::is_supported_maven_version() {
42+
local maven_version="${1}"
43+
local maven_url="${2:?}"
44+
if [[ "${maven_version}" = "${DEFAULT_MAVEN_VERSION}" ]]; then
4045
return 0
41-
elif curl -I --retry 3 --retry-connrefused --connect-timeout 5 --fail --silent --max-time 5 --location "${mavenUrl}" >/dev/null; then
46+
elif curl -I --retry 3 --retry-connrefused --connect-timeout 5 --fail --silent --max-time 5 --location "${maven_url}" >/dev/null; then
4247
return 0
4348
else
4449
return 1
4550
fi
4651
}
4752

48-
detect_maven_version() {
49-
local baseDir=${1}
50-
if [ -f "${baseDir}/system.properties" ]; then
51-
mavenVersion=$(get_app_system_value "${baseDir}/system.properties" "maven.version")
52-
if [ -n "$mavenVersion" ]; then
53-
echo "${mavenVersion}"
53+
common::detect_maven_version() {
54+
local base_dir="${1}"
55+
if [[ -f "${base_dir}/system.properties" ]]; then
56+
local maven_version
57+
maven_version=$(common::get_app_system_value "${base_dir}/system.properties" "maven.version")
58+
if [[ -n "${maven_version}" ]]; then
59+
echo "${maven_version}"
5460
else
5561
echo ""
5662
fi
@@ -59,34 +65,34 @@ detect_maven_version() {
5965
fi
6066
}
6167

62-
get_app_system_value() {
63-
local file=${1?"No file specified"}
64-
local key=${2?"No key specified"}
68+
common::get_app_system_value() {
69+
local file="${1?No file specified}"
70+
local key="${2?No key specified}"
6571

6672
# escape for regex
6773
local escaped_key
6874
# shellcheck disable=SC2001
6975
escaped_key="$(echo "${key}" | sed "s/\./\\\./g")"
7076

71-
[ -f "${file}" ] &&
77+
[[ -f "${file}" ]] &&
7278
grep -E "^${escaped_key}[[:space:]=]+" "${file}" |
7379
sed -E -e "s/${escaped_key}([\ \t]*=[\ \t]*|[\ \t]+)([A-Za-z0-9\.-]*).*/\2/g"
7480
}
7581

76-
cache_copy() {
77-
rel_dir=$1
78-
from_dir=$2
79-
to_dir=$3
82+
common::cache_copy() {
83+
local rel_dir="${1}"
84+
local from_dir="${2}"
85+
local to_dir="${3}"
8086
rm -rf "${to_dir:?}/${rel_dir:?}"
81-
if [ -d "${from_dir}/${rel_dir}" ]; then
87+
if [[ -d "${from_dir}/${rel_dir}" ]]; then
8288
mkdir -p "${to_dir}/${rel_dir}"
8389
cp -pr "${from_dir}/${rel_dir}"/. "${to_dir}/${rel_dir}"
8490
fi
8591
}
8692

87-
install_jdk() {
88-
local install_dir=${1}
89-
local cache_dir=${2}
93+
common::install_jdk() {
94+
local install_dir="${1}"
95+
local cache_dir="${2}"
9096

9197
JVM_COMMON_BUILDPACK=${JVM_COMMON_BUILDPACK:-https://buildpack-registry.s3.us-east-1.amazonaws.com/buildpacks/heroku/jvm.tgz}
9298
mkdir -p /tmp/jvm-common

0 commit comments

Comments
 (0)