Skip to content

Commit

Permalink
Check native environment before starting (#25186)
Browse files Browse the repository at this point in the history
Running Beats/Agent build for 32bits sometimes leads to problems on
64bit platforms. Some system metrics do have different sizing on 32 and
64bit architectures, which can trip up Agent/Beats wrongly interpreting
the given data.

On Windows/Linux it is possible to run 32bit binaries. Now a check will
be run to fail on startup if the architecture does not match the
binaries architecture.

(cherry picked from commit 7253026)
  • Loading branch information
Steffen Siering authored and mergify-bot committed Apr 21, 2021
1 parent f73073c commit 97d3fa9
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 0 deletions.
48 changes: 48 additions & 0 deletions libbeat/cmd/platformcheck/platformcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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.

// +build linux windows

package platformcheck

import (
"fmt"
"math/bits"
"strings"

"github.com/shirou/gopsutil/host"
)

func CheckNativePlatformCompat() error {
const compiledArchBits = bits.UintSize // 32 if the binary was compiled for 32 bit architecture.

if compiledArchBits > 32 {
// We assume that 64bit binaries can only be run on 64bit systems
return nil
}

arch, err := host.KernelArch()
if err != nil {
return err
}

if strings.Contains(arch, "64") {
return fmt.Errorf("trying to run %vBit binary on 64Bit system", compiledArchBits)
}

return nil
}
24 changes: 24 additions & 0 deletions libbeat/cmd/platformcheck/platformcheck_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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.

// +build !linux,!windows

package platformcheck

func CheckNativePlatformCompat() error {
return nil
}
65 changes: 65 additions & 0 deletions libbeat/cmd/platformcheck/platformcheck_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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 platformcheck

import (
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"

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

func TestCheckPlatformCompat(t *testing.T) {
if !(runtime.GOARCH == "amd64" && (runtime.GOOS == "linux" ||
runtime.GOOS == "windows")) {
t.Skip("Test not support on current platform")
}

// compile test helper
tmp := t.TempDir()
helper := filepath.Join(tmp, "helper")

cmd := exec.Command("go", "test", "-c", "-o", helper)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = append(os.Environ(), "GOARCH=386")
require.NoError(t, cmd.Run(), "failed to compile test helper")

// run test helper
cmd = exec.Command(helper, "-test.v", "-test.run", "TestHelper")
cmd.Env = []string{"GO_USE_HELPER=1"}
output, err := cmd.Output()
if err != nil {
t.Logf("32bit binary tester failed.\n Output: %s", output)
}
}

func TestHelper(t *testing.T) {
if os.Getenv("GO_USE_HELPER") != "1" {
t.Log("ignore helper")
return
}

err := CheckNativePlatformCompat()
if err.Error() != "trying to run 32Bit binary on 64Bit system" {
t.Error("expected the native platform check to fail")
}
}
6 changes: 6 additions & 0 deletions libbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/cmd/instance"
"github.com/elastic/beats/v7/libbeat/cmd/platformcheck"
)

func init() {
Expand Down Expand Up @@ -56,6 +57,11 @@ type BeatsRootCmd struct {
// run command, which will be called if no args are given (for backwards compatibility),
// and beat settings
func GenRootCmdWithSettings(beatCreator beat.Creator, settings instance.Settings) *BeatsRootCmd {
if err := platformcheck.CheckNativePlatformCompat(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to initialize: %v\n", err)
os.Exit(1)
}

if settings.IndexPrefix == "" {
settings.IndexPrefix = settings.Name
}
Expand Down
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

- Docker container is not run as root by default. {pull}21213[21213]
- Read Fleet connection information from `fleet.*` instead of `fleet.kibana.*`. {pull}24713[24713]
- Beats build for 32Bit Windows or Linux system will refuse to run on a 64bit system. {pull}25186[25186]

==== Bugfixes
- Fix rename *ConfigChange to *PolicyChange to align on changes in the UI. {pull}20779[20779]
Expand Down
6 changes: 6 additions & 0 deletions x-pack/elastic-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ import (
"os"
"time"

"github.com/elastic/beats/v7/libbeat/cmd/platformcheck"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/cmd"
)

// Setups and Runs agent.
func main() {
if err := platformcheck.CheckNativePlatformCompat(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to initialize: %v\n", err)
os.Exit(1)
}

rand.Seed(time.Now().UnixNano())
command := cmd.NewCommand()
if err := command.Execute(); err != nil {
Expand Down

0 comments on commit 97d3fa9

Please sign in to comment.