Skip to content

Commit be2f1c1

Browse files
committed
project docs, helper scripts, sample Dockerfile
1 parent 8092b81 commit be2f1c1

8 files changed

+156
-0
lines changed

docs/build.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Building a go binary that includes golang-fips patches
2+
3+
## Env variables
4+
5+
`GOEXPERIMENT=strictfipsruntime` helps ensure a fips compliant binary is built. Note that this is functionally equivalent of
6+
7+
```
8+
GO_BUILDTAGS+="goexperiment.strictfipsruntime"
9+
```
10+
11+
## Native build - Method 1
12+
13+
The easiest way to do is to simply clone this repo, apply patches and from the internal go/src directory run ./make.bash. See a more comprehensive script at `scripts/build1.sh`
14+
15+
```
16+
git clone https://github.com/golang-fips/go.git golang-fips
17+
cd golang-fips
18+
./scripts/full-initialize-repo.sh go$GOLANG_VER
19+
cd go/src
20+
./make.bash --no-clean
21+
```
22+
23+
## Native build - Method 2
24+
25+
Another way is to directly apply fips patches on downloaded go and golang-fips binaries. See full script at `scripts/build2.sh`
26+
27+
```
28+
wget https://github.com/golang/go/archive/refs/tags/go1.22.2.tar.gz
29+
wget https://github.com/golang-fips/go/archive/refs/tags/go1.22.2-1-openssl-fips.tar.gz
30+
tar -xf go1.22.2.tar.gz
31+
tar -xf go1.22.2-1-openssl-fips.tar.gz
32+
cd go-go1.22.2
33+
for patch in ../go-go1.22.2-1-openssl-fips/patches/*.patch; do
34+
patch -p1 < "${patch}"
35+
done
36+
cd src
37+
./make.bash --no-clean
38+
```
39+
40+
## Container Build
41+
42+
So far the two methods above described steps to build golang natively. To build golang inside a container, copy the sample Dockerfile located at scripts/Dockerfile.sample to a new directory, modify it to add distro specific variables and run docker/podman on it.
43+
44+
```
45+
mkdir build-context
46+
cp scripts/Dockerfile.sample scripts/Dockerfile
47+
# modify it to make it distro specific
48+
podman/docker build .
49+
```

docs/deps.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Dependencies

docs/openssl.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# OpenSSL considerations

docs/overview.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Overview here

docs/test-conf.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Test configuration

scripts/Dockerfile.sample

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
FROM <distro-specific-base-image>
2+
3+
ARG GOLANG_VER=1.21.9
4+
# turn on FIPS and CGO_ENABLED by default
5+
ARG FIPS=1
6+
ARG GODEBUG=0
7+
ARG CGO_ENABLED=1
8+
9+
# certain env need proxy to download from github
10+
ENV http_proxy=""
11+
ENV https_proxy=""
12+
ENV no_proxy=""
13+
14+
ENV GOLANG_FIPS=$FIPS
15+
ENV GODEBUG=$GODEBUG
16+
ENV CGO_ENABLED=1
17+
18+
# distro specific package manager
19+
# need bootstrap golang to build go, will be removed later
20+
RUN dnf/apt install -y git wget golang && \
21+
dnf/apt groupinstall -y "Development Tools"
22+
23+
RUN go version
24+
RUN echo "GOROOT_BOOTSTRAP = / by default so will use installed go as compiler"
25+
RUN git config --global user.email "<add email here>"
26+
RUN git config --global user.name "<add name here>"
27+
28+
RUN git clone https://github.com/golang-fips/go.git golang-fips && cd golang-fips && \
29+
GOLANG_VER_SHORT=${GOLANG_VER%.*} && \
30+
git fetch origin go${GOLANG_VER_SHORT}-fips-release && git checkout go${GOLANG_VER_SHORT}-fips-release && \
31+
./scripts/full-initialize-repo.sh go$GOLANG_VER
32+
33+
RUN cd golang-fips/go && \
34+
cd src && ./make.bash --no-clean
35+
36+
# rm bootstrap golang
37+
RUN dnf -y erase golang
38+
39+
ENV PATH=/golang-fips/go/bin:$PATH:/usr/local/go/bin
40+
ENV GOPATH=$HOME/go
41+
ENV PATH=$PATH:$GOPATH/bin
42+
43+
RUN go version

scripts/build1.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
# this script builds a go binary after applying openssl fips patches.
3+
4+
set -x
5+
export GOLANG_VER=1.22.2
6+
[[ -d go ]] || ./scripts/full-initialize-repo.sh go$GOLANG_VER
7+
cd go/src
8+
export GO_BUILDTAGS+="goexperiment.strictfipsruntime"
9+
export CGO_ENABLED=1
10+
./make.bash --no-clean
11+
{ set +x ; } 2>/dev/null

scripts/build2.sh

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

Comments
 (0)