Skip to content

Commit

Permalink
Merge pull request #163 from FedeDP/new/go_embed_templates
Browse files Browse the repository at this point in the history
new(pkg): use go:embed for templates, removing them from source code and putting them in their own subfolder
  • Loading branch information
dwindsor authored Jul 11, 2022
2 parents d72a1ad + 5efb0e4 commit 025a738
Show file tree
Hide file tree
Showing 17 changed files with 413 additions and 401 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,17 @@ func (v archLinux) Script(c Config) (string, error) {
```

Essentially, the `Script` function that you are implementing will need to return a string containing
a `bash` script that will be executed by driverkit at build time.
a `bash` script that will be executed by driverkit at build time.

Under `pkg/driverbuilder/builder/templates` folder, you can find all the template scripts for the supported builders.
Adding a new template there and using `go:embed` to include it in your builder, allows leaner code
without mixing up templates and builder logic.
For example:

```go
//go:embed templates/archlinux.sh
var archlinuxTemplate string
```

Depending on how the distro works, the script will need to fetch the kernel headers for it at the specific kernel version specified
in the `Config` struct at `c.Build.KernelVersion`.
Expand Down
49 changes: 4 additions & 45 deletions pkg/driverbuilder/builder/amazonlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"compress/bzip2"
"compress/gzip"
_ "embed"
"fmt"
"io"
"io/ioutil"
Expand All @@ -22,6 +23,9 @@ import (
logger "github.com/sirupsen/logrus"
)

//go:embed templates/amazonlinux.sh
var amazonlinuxTemplate string

type amazonBuilder interface {
Builder
repos() []string
Expand Down Expand Up @@ -54,51 +58,6 @@ func init() {
BuilderByTarget[TargetTypeAmazonLinux] = &amazonlinux{}
}

const amazonlinuxTemplate = `
#!/bin/bash
set -xeuo pipefail
rm -Rf {{ .DriverBuildDir }}
mkdir {{ .DriverBuildDir }}
rm -Rf /tmp/module-download
mkdir -p /tmp/module-download
curl --silent -SL {{ .ModuleDownloadURL }} | tar -xzf - -C /tmp/module-download
mv /tmp/module-download/*/driver/* {{ .DriverBuildDir }}
cp /driverkit/module-Makefile {{ .DriverBuildDir }}/Makefile
bash /driverkit/fill-driver-config.sh {{ .DriverBuildDir }}
# Fetch the kernel
mkdir /tmp/kernel-download
cd /tmp/kernel-download
{{ range $url := .KernelDownloadURLs }}
curl --silent -o kernel.rpm -SL {{ $url }}
rpm2cpio kernel.rpm | cpio --extract --make-directories
rm -rf kernel.rpm
{{ end }}
rm -Rf /tmp/kernel
mkdir -p /tmp/kernel
mv usr/src/kernels/*/* /tmp/kernel
{{ if .BuildModule }}
# Build the kernel module
cd {{ .DriverBuildDir }}
make KERNELDIR=/tmp/kernel CC=/usr/bin/gcc LD=/usr/bin/ld.bfd CROSS_COMPILE=""
mv {{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
{{ end }}
{{ if .BuildProbe }}
# Build the eBPF probe
cd {{ .DriverBuildDir }}/bpf
make LLC=/usr/bin/llc-{{ .LLVMVersion }} CLANG=/usr/bin/clang-{{ .LLVMVersion }} CC=/usr/bin/gcc KERNELDIR=/tmp/kernel
ls -l probe.o
{{ end }}
`

type amazonlinuxTemplateData struct {
DriverBuildDir string
ModuleDownloadURL string
Expand Down
49 changes: 4 additions & 45 deletions pkg/driverbuilder/builder/centos.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package builder

import (
"bytes"
_ "embed"
"fmt"
"text/template"

"github.com/falcosecurity/driverkit/pkg/kernelrelease"
)

//go:embed templates/centos.sh
var centosTemplate string

// TargetTypeCentos identifies the Centos target.
const TargetTypeCentos Type = "centos"

Expand Down Expand Up @@ -179,51 +183,6 @@ type centosTemplateData struct {
BuildProbe bool
}

const centosTemplate = `
#!/bin/bash
set -xeuo pipefail
rm -Rf {{ .DriverBuildDir }}
mkdir {{ .DriverBuildDir }}
rm -Rf /tmp/module-download
mkdir -p /tmp/module-download
curl --silent -SL {{ .ModuleDownloadURL }} | tar -xzf - -C /tmp/module-download
mv /tmp/module-download/*/driver/* {{ .DriverBuildDir }}
cp /driverkit/module-Makefile {{ .DriverBuildDir }}/Makefile
bash /driverkit/fill-driver-config.sh {{ .DriverBuildDir }}
# Fetch the kernel
mkdir /tmp/kernel-download
cd /tmp/kernel-download
curl --silent -o kernel-devel.rpm -SL {{ .KernelDownloadURL }}
rpm2cpio kernel-devel.rpm | cpio --extract --make-directories
rm -Rf /tmp/kernel
mkdir -p /tmp/kernel
mv usr/src/kernels/*/* /tmp/kernel
# Change current gcc
ln -sf /usr/bin/gcc-{{ .GCCVersion }} /usr/bin/gcc
{{ if .BuildModule }}
# Build the module
cd {{ .DriverBuildDir }}
make KERNELDIR=/tmp/kernel
mv {{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
strip -g {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
{{ end }}
{{ if .BuildProbe }}
# Build the eBPF probe
cd {{ .DriverBuildDir }}/bpf
make LLC=/usr/bin/llc-7 CLANG=/usr/bin/clang-7 CC=/usr/bin/gcc KERNELDIR=/tmp/kernel
ls -l probe.o
{{ end }}
`

func centosGccVersionFromKernelRelease(kr kernelrelease.KernelRelease) string {
switch kr.Version {
case 3:
Expand Down
57 changes: 4 additions & 53 deletions pkg/driverbuilder/builder/debian.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package builder

import (
"bytes"
_ "embed"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -12,6 +13,9 @@ import (
"github.com/falcosecurity/driverkit/pkg/kernelrelease"
)

//go:embed templates/debian.sh
var debianTemplate string

// TargetTypeDebian identifies the Debian target.
const TargetTypeDebian Type = "debian"

Expand Down Expand Up @@ -102,59 +106,6 @@ type debianTemplateData struct {
LLVMVersion string
}

const debianTemplate = `
#!/bin/bash
set -xeuo pipefail
rm -Rf {{ .DriverBuildDir }}
mkdir {{ .DriverBuildDir }}
rm -Rf /tmp/module-download
mkdir -p /tmp/module-download
curl --silent -SL {{ .ModuleDownloadURL }} | tar -xzf - -C /tmp/module-download
mv /tmp/module-download/*/driver/* {{ .DriverBuildDir }}
cp /driverkit/module-Makefile {{ .DriverBuildDir }}/Makefile
bash /driverkit/fill-driver-config.sh {{ .DriverBuildDir }}
# Fetch the kernel
mkdir /tmp/kernel-download
cd /tmp/kernel-download
{{ range $url := .KernelDownloadURLS }}
curl --silent -o kernel.deb -SL {{ $url }}
ar x kernel.deb
tar -xvf data.tar.xz
{{ end }}
ls -la /tmp/kernel-download
cd /tmp/kernel-download/
cp -r usr/* /usr
cp -r lib/* /lib
cd /usr/src
sourcedir=$(find . -type d -name "linux-headers-*%s" | head -n 1 | xargs readlink -f)
ls -la $sourcedir
{{ if .BuildModule }}
# Build the module
cd {{ .DriverBuildDir }}
make CC=/usr/bin/gcc-8 KERNELDIR=$sourcedir
mv {{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
strip -g {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
{{ end }}
{{ if .BuildProbe }}
# Build the eBPF probe
cd {{ .DriverBuildDir }}/bpf
make LLC=/usr/bin/llc-{{ .LLVMVersion }} CLANG=/usr/bin/clang-{{ .LLVMVersion }} CC=/usr/bin/gcc-8 KERNELDIR=$sourcedir
ls -l probe.o
{{ end }}
`

func debianHeadersURLFromRelease(kr kernelrelease.KernelRelease) ([]string, error) {
baseURLS := []string{
"http://security-cdn.debian.org/pool/main/l/linux/",
Expand Down
55 changes: 4 additions & 51 deletions pkg/driverbuilder/builder/flatcar.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package builder

import (
"bytes"
_ "embed"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -11,6 +12,9 @@ import (
"github.com/falcosecurity/driverkit/pkg/kernelrelease"
)

//go:embed templates/flatcar.sh
var flatcarTemplate string

// TargetTypeFlatcar identifies the Flatcar target.
const TargetTypeFlatcar Type = "flatcar"

Expand Down Expand Up @@ -169,57 +173,6 @@ type flatcarTemplateData struct {
BuildProbe bool
}

const flatcarTemplate = `
#!/bin/bash
set -xeuo pipefail
rm -Rf {{ .DriverBuildDir }}
mkdir {{ .DriverBuildDir }}
rm -Rf /tmp/module-download
mkdir -p /tmp/module-download
curl --silent -SL {{ .ModuleDownloadURL }} | tar -xzf - -C /tmp/module-download
mv /tmp/module-download/*/driver/* {{ .DriverBuildDir }}
cp /driverkit/module-Makefile {{ .DriverBuildDir }}/Makefile
bash /driverkit/fill-driver-config.sh {{ .DriverBuildDir }}
# Fetch the kernel
mkdir /tmp/kernel-download
cd /tmp/kernel-download
curl --silent -SL {{ .KernelDownloadURL }} | tar -Jxf - -C /tmp/kernel-download
rm -Rf /tmp/kernel
mkdir -p /tmp/kernel
mv /tmp/kernel-download/*/* /tmp/kernel
# Change current gcc
ln -sf /usr/bin/gcc-{{ .GCCVersion }} /usr/bin/gcc
curl --silent -o /tmp/kernel.config -SL {{ .KernelConfigURL }}
cd /tmp/kernel
sed -i -e 's|^\(EXTRAVERSION =\).*|\1 -flatcar|' Makefile
make KCONFIG_CONFIG=/tmp/kernel.config oldconfig
make KCONFIG_CONFIG=/tmp/kernel.config modules_prepare
{{ if .BuildModule }}
# Build the module
cd {{ .DriverBuildDir }}
make KERNELDIR=/tmp/kernel
mv {{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
strip -g {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
{{ end }}
{{ if .BuildProbe }}
# Build the eBPF probe
cd {{ .DriverBuildDir }}/bpf
make LLC=/usr/bin/llc-12 CLANG=/usr/bin/clang-12 CC=/usr/bin/gcc KERNELDIR=/tmp/kernel
ls -l probe.o
{{ end }}
`

func flatcarGccVersion(gccVersion string) string {
// reuse kernelrelease version parsing for gcc
gv := kernelrelease.FromString(gccVersion)
Expand Down
50 changes: 5 additions & 45 deletions pkg/driverbuilder/builder/redhat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package builder

import (
"bytes"
_ "embed"
"github.com/falcosecurity/driverkit/pkg/kernelrelease"
"text/template"
)

//go:embed templates/redhat.sh
var redhatTemplate string

// TargetTypeRedhat identifies the redhat target.
const TargetTypeRedhat Type = "redhat"

Expand All @@ -27,57 +31,13 @@ type redhatTemplateData struct {
BuildProbe bool
}

const redhatTemplate = `
#!/bin/bash
set -xeuo pipefail
rm -Rf {{ .DriverBuildDir }}
mkdir {{ .DriverBuildDir }}
rm -Rf /tmp/module-download
mkdir -p /tmp/module-download
curl --silent -SL {{ .ModuleDownloadURL }} | tar -xzf - -C /tmp/module-download
mv /tmp/module-download/*/driver/* {{ .DriverBuildDir }}
cp /driverkit/module-Makefile {{ .DriverBuildDir }}/Makefile
bash /driverkit/fill-driver-config.sh {{ .DriverBuildDir }}
# Fetch the kernel
rm -Rf /tmp/kernel-download
mkdir /tmp/kernel-download
cd /tmp/kernel-download
yum install -y --downloadonly --downloaddir=/tmp/kernel-download kernel-devel-0:{{ .KernelPackage }}
rpm2cpio kernel-devel-{{ .KernelPackage }}.rpm | cpio --extract --make-directories
rm -Rf /tmp/kernel
mkdir -p /tmp/kernel
mv usr/src/kernels/*/* /tmp/kernel
{{ if .BuildModule }}
# Build the module
cd {{ .DriverBuildDir }}
make KERNELDIR=/tmp/kernel
mv {{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
strip -g {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
{{ end }}
{{ if .BuildProbe }}
# Build the eBPF probe
cd {{ .DriverBuildDir }}/bpf
make LLC=/usr/bin/llc CLANG=/usr/bin/clang CC=/usr/bin/gcc KERNELDIR=/tmp/kernel
ls -l probe.o
{{ end }}
`

func (v redhat) Script(cfg Config, kr kernelrelease.KernelRelease) (string, error) {
t := template.New(string(TargetTypeRedhat))
parsed, err := t.Parse(redhatTemplate)
if err != nil {
return "", err
}

td := redhatTemplateData{
DriverBuildDir: DriverDirectory,
KernelPackage: kr.Fullversion + kr.FullExtraversion,
Expand Down
Loading

0 comments on commit 025a738

Please sign in to comment.