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
33 changes: 33 additions & 0 deletions pkg/containers/detection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package containers

import "os"

// IsRunningInContainer checks whether the current process is being executed inside one of the containers we check for
func IsRunningInContainer() bool {
return IsDocker() || IsPodman() || IsCryoSpark()
}

// IsDocker returns true if we are running in a docker instance
func IsDocker() bool {
return pathExists("/.dockerenv") ||
pathExists("/.dockerinit") ||
pathExists("/run/.containerenv") ||
pathExists("/var/run/.containerenv")
}

// IsPodman returns true if we are running in a podman instance
func IsPodman() bool {
return os.Getenv("container") == "oci" || os.Getenv("container") == "podman"
}

// IsCryoSpark returns true if we are running in a cryospark instance
func IsCryoSpark() bool {
_, hostnameSet := os.LookupEnv("CRYOSPARC_MASTER_HOSTNAME")
_, userSet := os.LookupEnv("CRYOSPARC_USER")
return hostnameSet || userSet || pathExists("/opt/cryosparc") || pathExists("/app/cryosparc")
}

func pathExists(filepath string) bool {
_, err := os.Stat(filepath)
return err == nil
}
18 changes: 18 additions & 0 deletions pkg/containers/detection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package containers

import (
"os"
"testing"

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

// Assert that container detection is running correctly by checking if it returns true in CI. Expected to return
// false when run locally.
func TestContainerDetection(t *testing.T) {
if _, ok := os.LookupEnv("GITHUB_ACTIONS"); ok {
assert.True(t, IsRunningInContainer())
} else {
assert.False(t, IsRunningInContainer())
}
}
3 changes: 2 additions & 1 deletion roxctl/central/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stackrox/rox/pkg/backup"
"github.com/stackrox/rox/pkg/buildinfo"
"github.com/stackrox/rox/pkg/certgen"
"github.com/stackrox/rox/pkg/containers"
"github.com/stackrox/rox/pkg/env"
"github.com/stackrox/rox/pkg/errox"
"github.com/stackrox/rox/pkg/features"
Expand Down Expand Up @@ -277,7 +278,7 @@ func OutputZip(logger logger.Logger, io io2.IO, config renderer.Config) error {
}

var outputPath string
if roxctl.InMainImage() {
if roxctl.InMainImage() || !containers.IsRunningInContainer() {
bytes, err := wrapper.Zip()
if err != nil {
return errors.Wrap(err, "error generating zip file")
Expand Down