Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion dev-tools/mage/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"log"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/josephspurrier/goversioninfo"
Expand All @@ -46,6 +47,39 @@ type BuildArgs struct {
WinMetadata bool // Add resource metadata to Windows binaries (like add the version number to the .exe properties).
}

// buildTagRE is a regexp to match strings like "-tags=abcd"
// but does not match "-tags= "
var buildTagRE = regexp.MustCompile(`-tags=([\S]+)?`)

// ParseBuildTags returns the ExtraFlags param where all flags that are go build tags are joined by a comma.
//
// For example if given -someflag=val1 -tags=buildtag1 -tags=buildtag2
// It will return -someflag=val1 -tags=buildtag1,buildtag2
func (b BuildArgs) ParseBuildTags() []string {
flags := make([]string, 0)
if len(b.ExtraFlags) == 0 {
return flags
}

buildTags := make([]string, 0)
for _, flag := range b.ExtraFlags {
if buildTagRE.MatchString(flag) {
arr := buildTagRE.FindStringSubmatch(flag)
if len(arr) != 2 || arr[1] == "" {
log.Printf("Parsing buildargs.ExtraFlags found strange flag %q ignoring value", flag)
continue
}
buildTags = append(buildTags, arr[1])
} else {
flags = append(flags, flag)
}
}
if len(buildTags) > 0 {
flags = append(flags, "-tags="+strings.Join(buildTags, ","))
}
return flags
}

// DefaultBuildArgs returns the default BuildArgs for use in builds.
func DefaultBuildArgs() BuildArgs {
args := BuildArgs{
Expand Down Expand Up @@ -74,6 +108,10 @@ func DefaultBuildArgs() BuildArgs {
// Remove all file system paths from the compiled executable, to improve build reproducibility
args.ExtraFlags = append(args.ExtraFlags, "-trimpath")
}
if FIPSBuild {
args.ExtraFlags = append(args.ExtraFlags, "-tags=requirefips")
args.CGO = true
}

return args
}
Expand Down Expand Up @@ -175,6 +213,10 @@ func Build(params BuildArgs) error {
if params.CGO {
cgoEnabled = "1"
}
if FIPSBuild {
cgoEnabled = "1"
env["GOEXPERIMENT"] = "systemcrypto"
}
env["CGO_ENABLED"] = cgoEnabled

// Spec
Expand All @@ -186,7 +228,7 @@ func Build(params BuildArgs) error {
if params.BuildMode != "" {
args = append(args, "-buildmode", params.BuildMode)
}
args = append(args, params.ExtraFlags...)
args = append(args, params.ParseBuildTags()...)

// ldflags
ldflags := params.LDFlags
Expand Down
72 changes: 72 additions & 0 deletions dev-tools/mage/build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package mage

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_BuildArgs_ParseBuildTags(t *testing.T) {
tests := []struct {
name string
input []string
expect []string
}{{
name: "no flags",
input: nil,
expect: []string{},
}, {
name: "multiple flags with no tags",
input: []string{"-a", "-b", "-key=value"},
expect: []string{"-a", "-b", "-key=value"},
}, {
name: "one build tag",
input: []string{"-tags=example"},
expect: []string{"-tags=example"},
}, {
name: "multiple build tags",
input: []string{"-tags=example", "-tags=test"},
expect: []string{"-tags=example,test"},
}, {
name: "joined build tags",
input: []string{"-tags=example,test"},
expect: []string{"-tags=example,test"},
}, {
name: "multiple build tags with other flags",
input: []string{"-tags=example", "-tags=test", "-key=value", "-a"},
expect: []string{"-key=value", "-a", "-tags=example,test"},
}, {
name: "incorrectly formatted tag",
input: []string{"-tags= example"},
expect: []string{},
}, {
name: "incorrectly formatted tag with valid tag",
input: []string{"-tags= example", "-tags=test"},
expect: []string{"-tags=test"},
}}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
args := BuildArgs{ExtraFlags: tc.input}
flags := args.ParseBuildTags()
assert.EqualValues(t, tc.expect, flags)
})
}
}
6 changes: 6 additions & 0 deletions dev-tools/mage/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"sync"
"text/template"
"time"
"unicode"

Check failure on line 48 in dev-tools/mage/common.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

could not import unicode (-: could not load export data: internal error in importing "unicode" (unsupported version: 2); please report an issue) (typecheck)

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
Expand Down Expand Up @@ -511,6 +511,12 @@
return err
}
case tar.TypeReg:
// create containing folder if it doesn't exist yet
targetContainingDir := filepath.Dir(filepath.FromSlash(path))
if mkDirErr := os.MkdirAll(targetContainingDir, 0755); mkDirErr != nil {
return fmt.Errorf("creating container directory for file %s: %w", header.Name, mkDirErr)
}

writer, err := os.Create(path)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions dev-tools/mage/crossbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ func CrossBuildImage(platform string) (string, error) {
if err != nil {
return "", err
}
if FIPSBuild {
tagSuffix += "-fips"
}

return BeatsCrossBuildImage + ":" + goVersion + "-" + tagSuffix, nil
}
Expand Down Expand Up @@ -331,6 +334,7 @@ func (b GolangCrossBuilder) Build() error {
"--env", "MAGEFILE_VERBOSE="+verbose,
"--env", "MAGEFILE_TIMEOUT="+EnvOr("MAGEFILE_TIMEOUT", ""),
"--env", fmt.Sprintf("SNAPSHOT=%v", Snapshot),
"--env", fmt.Sprintf("FIPS=%v", FIPSBuild),
"-v", repoInfo.RootDir+":"+mountPoint,
"-w", workDir,
)
Expand Down
3 changes: 3 additions & 0 deletions dev-tools/mage/dockerbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ func (b *dockerBuilder) dockerBuild() (string, error) {
if b.Snapshot {
tag = tag + "-SNAPSHOT"
}
if b.FIPS {
tag = tag + "-fips"
}
if repository, _ := b.ExtraVars["repository"]; repository != "" {
tag = fmt.Sprintf("%s/%s", repository, tag)
}
Expand Down
4 changes: 2 additions & 2 deletions dev-tools/mage/gotest.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ func GoTest(ctx context.Context, params GoTestArgs) error {
}
}
if len(params.Tags) > 0 {
params := strings.Join(params.Tags, " ")
params := strings.Join(params.Tags, ",")
if params != "" {
testArgs = append(testArgs, "-tags", params)
testArgs = append(testArgs, "-tags="+params)
}
}
if params.CoverageProfileFile != "" {
Expand Down
11 changes: 8 additions & 3 deletions dev-tools/mage/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func Package() error {
spec.OS = target.GOOS()
spec.Arch = packageArch
spec.Snapshot = Snapshot
spec.FIPS = FIPSBuild
spec.evalContext = map[string]interface{}{
"GOOS": target.GOOS(),
"GOARCH": target.GOARCH(),
Expand Down Expand Up @@ -246,11 +247,11 @@ type packageBuilder struct {
}

func (b packageBuilder) Build() error {
fmt.Printf(">> package: Building %v type=%v for platform=%v\n", b.Spec.Name, b.Type, b.Platform.Name)
fmt.Printf(">> package: Building %v type=%v for platform=%v fips=%v\n", b.Spec.Name, b.Type, b.Platform.Name, b.Spec.FIPS)
log.Printf("Package spec: %+v", b.Spec)
if err := b.Type.Build(b.Spec); err != nil {
return fmt.Errorf("failed building %v type=%v for platform=%v: %w",
b.Spec.Name, b.Type, b.Platform.Name, err)
return fmt.Errorf("failed building %v type=%v for platform=%v fips=%v: %w",
b.Spec.Name, b.Type, b.Platform.Name, b.Spec.FIPS, err)
}
return nil
}
Expand Down Expand Up @@ -344,6 +345,10 @@ func TestPackages(options ...TestPackagesOption) error {
args = append(args, "-root-owner")
}

if FIPSBuild {
args = append(args, "-fips")
}

args = append(args, "-files", MustExpand("{{.PWD}}/build/distributions/*"))

if out, err := goTest(args...); err != nil {
Expand Down
7 changes: 4 additions & 3 deletions dev-tools/mage/pkgtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const (
packageStagingDir = "build/package"

// defaultBinaryName specifies the output file for zip and tar.gz.
defaultBinaryName = "{{.Name}}-{{.Version}}{{if .Snapshot}}-SNAPSHOT{{end}}{{if .OS}}-{{.OS}}{{end}}{{if .Arch}}-{{.Arch}}{{end}}"
defaultBinaryName = "{{.Name}}{{if .FIPS}}-fips{{end}}-{{.Version}}{{if .Snapshot}}-SNAPSHOT{{end}}{{if .OS}}-{{.OS}}{{end}}{{if .Arch}}-{{.Arch}}{{end}}"
)

// PackageType defines the file format of the package (e.g. zip, rpm, etc).
Expand Down Expand Up @@ -79,6 +79,7 @@ type PackageSpec struct {
Arch string `yaml:"arch,omitempty"`
Vendor string `yaml:"vendor,omitempty"`
Snapshot bool `yaml:"snapshot"`
FIPS bool `yaml:"fips"`
Version string `yaml:"version,omitempty"`
License string `yaml:"license,omitempty"`
URL string `yaml:"url,omitempty"`
Expand Down Expand Up @@ -528,7 +529,7 @@ func (s PackageSpec) rootDir() string {

// NOTE: This uses .BeatName instead of .Name because we wanted the internal
// directory to not include "-oss".
return s.MustExpand("{{.BeatName}}-{{.Version}}{{if .Snapshot}}-SNAPSHOT{{end}}{{if .OS}}-{{.OS}}{{end}}{{if .Arch}}-{{.Arch}}{{end}}")
return s.MustExpand("{{.BeatName}}{{if .FIPS}}-fips{{end}}-{{.Version}}{{if .Snapshot}}-SNAPSHOT{{end}}{{if .OS}}-{{.OS}}{{end}}{{if .Arch}}-{{.Arch}}{{end}}")
}

// PackageZip packages a zip file.
Expand Down Expand Up @@ -727,7 +728,7 @@ func runFPM(spec PackageSpec, packageType PackageType) error {
}
defer os.Remove(inputTar)

outputFile, err := spec.Expand("{{.Name}}-{{.Version}}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.Arch}}")
outputFile, err := spec.Expand("{{.Name}}-{{.Version}}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.Arch}}{{if .FIPS}}-fips{{end}}")
if err != nil {
return err
}
Expand Down
11 changes: 9 additions & 2 deletions dev-tools/mage/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@

BeatProjectType ProjectType

Snapshot bool
DevBuild bool
Snapshot bool
DevBuild bool
FIPSBuild bool

versionQualified bool
versionQualifier string
Expand Down Expand Up @@ -128,6 +129,11 @@
panic(fmt.Errorf("failed to parse DEV env value: %w", err))
}

FIPSBuild, err = strconv.ParseBool(EnvOr("FIPS", "false"))
if err != nil {
panic(fmt.Errorf("failed to parse FIPS env value: %w", err))
}

versionQualifier, versionQualified = os.LookupEnv("VERSION_QUALIFIER")
}

Expand Down Expand Up @@ -179,6 +185,7 @@
"BeatUser": BeatUser,
"Snapshot": Snapshot,
"DEV": DevBuild,
"FIPS": FIPSBuild,
"Qualifier": versionQualifier,
"CI": CI,
}
Expand Down Expand Up @@ -452,7 +459,7 @@

panic(fmt.Errorf("magefile must call devtools.SetBuildVariableSources() "+
"because it is not an elastic beat (repo=%+v)", repo.RootImportPath))
}

Check failure on line 462 in dev-tools/mage/settings.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

missing return (typecheck)

// BuildVariableSources is used to explicitly define what files contain build
// variables and how to parse the values from that file. This removes ambiguity
Expand Down
Loading
Loading