Skip to content

Commit

Permalink
🧹 simplify os connection names
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rock committed Feb 23, 2024
1 parent b9cf2b2 commit 7078a29
Show file tree
Hide file tree
Showing 23 changed files with 265 additions and 195 deletions.
28 changes: 14 additions & 14 deletions providers/os/connection/container/image_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,34 @@ import (
"go.mondoo.com/cnquery/v10/providers/os/id/containerid"
)

// NewContainerImageConnection uses a container image reference as input and creates a tar connection
func NewContainerImageConnection(id uint32, conf *inventory.Config, asset *inventory.Asset, img v1.Image) (*tar.TarConnection, error) {
// NewImageConnection uses a container image reference as input and creates a tar connection
func NewImageConnection(id uint32, conf *inventory.Config, asset *inventory.Asset, img v1.Image) (*tar.Connection, error) {
f, err := tar.RandomFile()
if err != nil {
return nil, err
}

conf.Options[tar.OPTION_FILE] = f.Name()

return tar.NewTarConnection(id, conf, asset,
return tar.NewConnection(id, conf, asset,
tar.WithFetchFn(func() (string, error) {
err = tar.StreamToTmpFile(mutate.Extract(img), f)
if err != nil {
os.Remove(f.Name())
_ = os.Remove(f.Name())
return "", err
}
log.Debug().Msg("tar> extracted image to temporary file")
return f.Name(), nil
}),
tar.WithCloseFn(func() {
log.Debug().Str("tar", f.Name()).Msg("tar> remove temporary tar file on connection close")
os.Remove(f.Name())
_ = os.Remove(f.Name())
}),
)
}

// NewContainerRegistryImage loads a container image from a remote registry
func NewContainerRegistryImage(id uint32, conf *inventory.Config, asset *inventory.Asset) (*tar.TarConnection, error) {
// NewRegistryImage loads a container image from a remote registry
func NewRegistryImage(id uint32, conf *inventory.Config, asset *inventory.Asset) (*tar.Connection, error) {
ref, err := name.ParseReference(conf.Host, name.WeakValidation)
if err != nil {
return nil, errors.New("invalid container registry reference: " + conf.Host)
Expand All @@ -65,7 +65,7 @@ func NewContainerRegistryImage(id uint32, conf *inventory.Config, asset *invento
conf.Options = map[string]string{}
}

conn, err := NewContainerImageConnection(id, conf, asset, img)
conn, err := NewImageConnection(id, conf, asset, img)
if err != nil {
return nil, err
}
Expand All @@ -81,9 +81,9 @@ func NewContainerRegistryImage(id uint32, conf *inventory.Config, asset *invento

repoName := ref.Context().Name()
imgDigest := hash.String()
name := repoName + "@" + containerid.ShortContainerImageID(imgDigest)
containerAssetName := repoName + "@" + containerid.ShortContainerImageID(imgDigest)
if asset.Name == "" {
asset.Name = name
asset.Name = containerAssetName
}
if len(asset.PlatformIds) == 0 {
asset.PlatformIds = []string{identifier}
Expand Down Expand Up @@ -111,7 +111,7 @@ func NewContainerRegistryImage(id uint32, conf *inventory.Config, asset *invento
return conn, err
}

func NewContainerFromTar(id uint32, conf *inventory.Config, asset *inventory.Asset) (*tar.TarConnection, error) {
func NewFromTar(id uint32, conf *inventory.Config, asset *inventory.Asset) (*tar.Connection, error) {
if conf == nil || len(conf.Options[tar.OPTION_FILE]) == 0 {
return nil, errors.New("tar provider requires a valid tar file")
}
Expand Down Expand Up @@ -141,19 +141,19 @@ func NewContainerFromTar(id uint32, conf *inventory.Config, asset *inventory.Ass
imageFilename = f.Name()
conf.Options[tar.OPTION_FILE] = imageFilename

c, err := tar.NewTarConnection(id, conf, asset,
c, err := tar.NewConnection(id, conf, asset,
tar.WithFetchFn(func() (string, error) {
err = tar.StreamToTmpFile(mutate.Extract(img), f)
if err != nil {
os.Remove(imageFilename)
_ = os.Remove(imageFilename)
return imageFilename, err
}
return imageFilename, nil
}),
tar.WithCloseFn(func() {
// remove temporary file on stream close
log.Debug().Str("tar", imageFilename).Msg("tar> remove temporary flattened image file on connection close")
os.Remove(imageFilename)
_ = os.Remove(imageFilename)
}),
)
if err != nil {
Expand Down
22 changes: 12 additions & 10 deletions providers/os/connection/container/image_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package container_test

import (
"go.mondoo.com/cnquery/v10/providers/os/connection/container"
"io"
"net/http"
"os"
Expand All @@ -18,6 +17,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mondoo.com/cnquery/v10/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v10/providers/os/connection/container"
"go.mondoo.com/cnquery/v10/providers/os/connection/tar"
)

Expand Down Expand Up @@ -64,22 +64,23 @@ func cacheCentos() error {

type dockerConnTest struct {
name string
conn *tar.TarConnection
conn *tar.Connection
testfile string
}

func TestImageConnections(t *testing.T) {
testConnections := []dockerConnTest{}
var testConnections []dockerConnTest

// create a connection to ta downloaded alpine image
err := cacheAlpine()
require.NoError(t, err, "should create tar without error")
alpineConn, err := container.NewContainerFromTar(0, &inventory.Config{
alpineConn, err := container.NewFromTar(0, &inventory.Config{
Type: "tar",
Options: map[string]string{
tar.OPTION_FILE: alpineContainerPath,
},
}, &inventory.Asset{})
require.NoError(t, err, "should create connection without error")
testConnections = append(testConnections, dockerConnTest{
name: "alpine",
conn: alpineConn,
Expand All @@ -89,20 +90,21 @@ func TestImageConnections(t *testing.T) {
// create a connection to ta downloaded centos image
err = cacheCentos()
require.NoError(t, err, "should create tar without error")
centosConn, err := container.NewContainerFromTar(0, &inventory.Config{
centosConn, err := container.NewFromTar(0, &inventory.Config{
Type: "tar",
Options: map[string]string{
tar.OPTION_FILE: centosContainerPath,
},
}, &inventory.Asset{})
require.NoError(t, err, "should create connection without error")
testConnections = append(testConnections, dockerConnTest{
name: "centos",
conn: centosConn,
testfile: "/etc/centos-release",
})

// create a connection to a remote alpine image
alpineRemoteConn, err := container.NewContainerRegistryImage(0, &inventory.Config{
alpineRemoteConn, err := container.NewRegistryImage(0, &inventory.Config{
Type: "docker-image",
Host: alpineImage,
}, &inventory.Asset{})
Expand Down Expand Up @@ -219,7 +221,7 @@ func TestTarSymlinkFile(t *testing.T) {
err := cacheAlpine()
require.NoError(t, err, "should create tar without error")

c, err := container.NewContainerFromTar(0, &inventory.Config{
c, err := container.NewFromTar(0, &inventory.Config{
Type: "tar",
Options: map[string]string{
tar.OPTION_FILE: alpineContainerPath,
Expand All @@ -237,11 +239,11 @@ func TestTarSymlinkFile(t *testing.T) {

stat, err := f.Stat()
assert.Equal(t, nil, err, "should stat without error")
assert.Equal(t, int64(796240), stat.Size(), "should read file size")
assert.True(t, stat.Size() > 0, "should read file size")

content, err := io.ReadAll(f)
assert.Equal(t, nil, err, "should execute without error")
assert.Equal(t, 796240, len(content), "should read the full content")
assert.True(t, len(content) > 0, "should read the full content")
}
}

Expand All @@ -251,7 +253,7 @@ func TestTarRelativeSymlinkFileCentos(t *testing.T) {
err := cacheCentos()
require.NoError(t, err, "should create tar without error")

c, err := container.NewContainerFromTar(0, &inventory.Config{
c, err := container.NewFromTar(0, &inventory.Config{
Type: "tar",
Options: map[string]string{
tar.OPTION_FILE: centosContainerPath,
Expand Down
56 changes: 28 additions & 28 deletions providers/os/connection/docker/container_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ import (
"go.mondoo.com/cnquery/v10/providers/os/connection/ssh/cat"
"go.mondoo.com/cnquery/v10/providers/os/connection/tar"
"go.mondoo.com/cnquery/v10/providers/os/id/containerid"
docker_discovery "go.mondoo.com/cnquery/v10/providers/os/resources/discovery/docker_engine"
dockerDiscovery "go.mondoo.com/cnquery/v10/providers/os/resources/discovery/docker_engine"
)

const (
DockerContainer shared.ConnectionType = "docker-container"
ContainerConnectionType shared.ConnectionType = "docker-container"
)

var _ shared.Connection = &DockerContainerConnection{}
var _ shared.Connection = &ContainerConnection{}

type DockerContainerConnection struct {
type ContainerConnection struct {
plugin.Connection
asset *inventory.Asset

Expand All @@ -52,7 +52,7 @@ type DockerContainerConnection struct {
runtime string
}

func NewDockerContainerConnection(id uint32, conf *inventory.Config, asset *inventory.Asset) (*DockerContainerConnection, error) {
func NewContainerConnection(id uint32, conf *inventory.Config, asset *inventory.Asset) (*ContainerConnection, error) {
// expect unix shell by default
dockerClient, err := GetDockerClient()
if err != nil {
Expand All @@ -69,7 +69,7 @@ func NewDockerContainerConnection(id uint32, conf *inventory.Config, asset *inve
return nil, errors.New("container " + data.ID + " is not running")
}

conn := &DockerContainerConnection{
conn := &ContainerConnection{
Connection: plugin.NewConnection(id, asset),
asset: asset,
Client: dockerClient,
Expand Down Expand Up @@ -105,27 +105,27 @@ func GetDockerClient() (*client.Client, error) {
return cli, nil
}

func (c *DockerContainerConnection) Name() string {
return string(DockerContainer)
func (c *ContainerConnection) Name() string {
return string(ContainerConnectionType)
}

func (c *DockerContainerConnection) Type() shared.ConnectionType {
return DockerContainer
func (c *ContainerConnection) Type() shared.ConnectionType {
return ContainerConnectionType
}

func (c *DockerContainerConnection) Asset() *inventory.Asset {
func (c *ContainerConnection) Asset() *inventory.Asset {
return c.asset
}

func (c *DockerContainerConnection) ContainerId() string {
func (c *ContainerConnection) ContainerId() string {
return c.container
}

func (c *DockerContainerConnection) Capabilities() shared.Capabilities {
func (c *ContainerConnection) Capabilities() shared.Capabilities {
return shared.Capability_File | shared.Capability_RunCommand
}

func (c *DockerContainerConnection) FileInfo(path string) (shared.FileInfoDetails, error) {
func (c *ContainerConnection) FileInfo(path string) (shared.FileInfoDetails, error) {
fs := c.FileSystem()
afs := &afero.Afero{Fs: fs}
stat, err := afs.Stat(path)
Expand All @@ -151,11 +151,11 @@ func (c *DockerContainerConnection) FileInfo(path string) (shared.FileInfoDetail
}, nil
}

func (c *DockerContainerConnection) FileSystem() afero.Fs {
func (c *ContainerConnection) FileSystem() afero.Fs {
return c.Fs
}

func (c *DockerContainerConnection) RunCommand(command string) (*shared.Command, error) {
func (c *ContainerConnection) RunCommand(command string) (*shared.Command, error) {
log.Debug().Str("command", command).Msg("docker> run command")
cmd := &Command{Client: c.Client, Container: c.container}
res, err := cmd.Exec(command)
Expand All @@ -174,7 +174,7 @@ func (c *DockerContainerConnection) RunCommand(command string) (*shared.Command,

func NewDockerEngineContainer(id uint32, conf *inventory.Config, asset *inventory.Asset) (shared.Connection, error) {
// could be an image id/name, container id/name or a short reference to an image in docker engine
ded, err := docker_discovery.NewDockerEngineDiscovery()
ded, err := dockerDiscovery.NewDockerEngineDiscovery()
if err != nil {
return nil, err
}
Expand All @@ -187,7 +187,7 @@ func NewDockerEngineContainer(id uint32, conf *inventory.Config, asset *inventor
if ci.Running {
log.Debug().Msg("found running container " + ci.ID)

conn, err := NewDockerContainerConnection(id, &inventory.Config{
conn, err := NewContainerConnection(id, &inventory.Config{
Host: ci.ID,
}, asset)
if err != nil {
Expand All @@ -201,7 +201,7 @@ func NewDockerEngineContainer(id uint32, conf *inventory.Config, asset *inventor
return conn, nil
} else {
log.Debug().Msg("found stopped container " + ci.ID)
conn, err := NewFromDockerEngine(id, &inventory.Config{
conn, err := NewSnapshotConnection(id, &inventory.Config{
Host: ci.ID,
}, asset)
if err != nil {
Expand All @@ -216,7 +216,7 @@ func NewDockerEngineContainer(id uint32, conf *inventory.Config, asset *inventor
}
}

func NewDockerContainerImageConnection(id uint32, conf *inventory.Config, asset *inventory.Asset) (*tar.TarConnection, error) {
func NewDockerContainerImageConnection(id uint32, conf *inventory.Config, asset *inventory.Asset) (*tar.Connection, error) {
disableInmemoryCache := false
if _, ok := conf.Options["disable-cache"]; ok {
var err error
Expand All @@ -226,7 +226,7 @@ func NewDockerContainerImageConnection(id uint32, conf *inventory.Config, asset
}
}
// Determine whether the image is locally present or not.
resolver := docker_discovery.Resolver{}
resolver := dockerDiscovery.Resolver{}
resolvedAssets, err := resolver.Resolve(context.Background(), asset, conf, nil)
if err != nil {
return nil, err
Expand All @@ -241,11 +241,11 @@ func NewDockerContainerImageConnection(id uint32, conf *inventory.Config, asset
asset.Name = resolvedAssets[0].Name
asset.PlatformIds = resolvedAssets[0].PlatformIds
asset.Labels = resolvedAssets[0].Labels
return container.NewContainerRegistryImage(id, conf, asset)
return container.NewRegistryImage(id, conf, asset)
}

// could be an image id/name, container id/name or a short reference to an image in docker engine
ded, err := docker_discovery.NewDockerEngineDiscovery()
ded, err := dockerDiscovery.NewDockerEngineDiscovery()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -296,7 +296,7 @@ func NewDockerContainerImageConnection(id uint32, conf *inventory.Config, asset
// 2. a locally present image, referenced by tag or digest
// 3. a remote image, referenced by tag or digest
func FetchConnectionType(target string) (string, error) {
ded, err := docker_discovery.NewDockerEngineDiscovery()
ded, err := dockerDiscovery.NewDockerEngineDiscovery()
if err != nil {
return "", err
}
Expand All @@ -321,7 +321,7 @@ func FetchConnectionType(target string) (string, error) {

// Used with docker snapshots
// NewWithReader provides a tar provider from a container image stream
func NewWithReader(id uint32, conf *inventory.Config, asset *inventory.Asset, rc io.ReadCloser) (*tar.TarConnection, error) {
func NewWithReader(id uint32, conf *inventory.Config, asset *inventory.Asset, rc io.ReadCloser) (*tar.Connection, error) {
filename := ""
if x, ok := rc.(*os.File); ok {
filename = x.Name()
Expand All @@ -337,12 +337,12 @@ func NewWithReader(id uint32, conf *inventory.Config, asset *inventory.Asset, rc

err = tar.StreamToTmpFile(rc, f)
if err != nil {
os.Remove(filename)
_ = os.Remove(filename)
return nil, err
}
}

return tar.NewTarConnection(
return tar.NewConnection(
id,
&inventory.Config{
Type: "tar",
Expand All @@ -354,6 +354,6 @@ func NewWithReader(id uint32, conf *inventory.Config, asset *inventory.Asset, rc
asset,
tar.WithCloseFn(func() {
log.Debug().Str("tar", filename).Msg("tar> remove temporary tar file on connection close")
os.Remove(filename)
_ = os.Remove(filename)
}))
}
Loading

0 comments on commit 7078a29

Please sign in to comment.