Skip to content

Commit

Permalink
🐛 fix dockerfile asset family (#4717)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Milchev <[email protected]>
  • Loading branch information
imilchev authored Oct 7, 2024
1 parent 761f540 commit fcf386c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 24 deletions.
18 changes: 16 additions & 2 deletions providers/os/connection/docker/docker_file_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"os"
"path/filepath"
"slices"

"go.mondoo.com/cnquery/v11/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v11/providers/os/connection/local"
Expand All @@ -24,6 +25,7 @@ type DockerfileConnection struct {
// FileAbsSrc must be the absolute path of the Dockerfile so
// that we find the file downstream
FileAbsSrc string
osFamily shared.OSFamily
}

func NewDockerfileConnection(_ uint32,
Expand Down Expand Up @@ -63,8 +65,6 @@ func NewDockerfileConnection(_ uint32,
Runtime: "docker",
TechnologyUrlSegments: []string{"iac", "dockerfile"},
}
// this helps with running commands against the local connection
asset.Platform.Family = append(asset.Platform.Family, localFamily...)

if url, ok := conf.Options["ssh-url"]; ok {
domain, org, repo, err := urlx.ParseGitSshUrl(url)
Expand Down Expand Up @@ -94,5 +94,19 @@ func NewDockerfileConnection(_ uint32,
FileAbsSrc: absSrc,
}

if slices.Contains(localFamily, "darwin") {
conn.osFamily = shared.OSFamily_Darwin
} else if slices.Contains(localFamily, "unix") {
conn.osFamily = shared.OSFamily_Unix
} else if slices.Contains(localFamily, "windows") {
conn.osFamily = shared.OSFamily_Windows
} else {
conn.osFamily = shared.OSFamily_None
}

return conn, nil
}

func (p *DockerfileConnection) OSFamily() shared.OSFamily {
return p.osFamily
}
13 changes: 13 additions & 0 deletions providers/os/connection/shared/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ const (
ContainerProxyOption string = "container-proxy"
)

type OSFamily string

const (
OSFamily_Darwin OSFamily = "darwin"
OSFamily_Unix OSFamily = "unix"
OSFamily_Windows OSFamily = "windows"
OSFamily_None OSFamily = "none"
)

type Connection interface {
plugin.Connection
RunCommand(command string) (*Command, error)
Expand All @@ -59,6 +68,10 @@ type Connection interface {
Capabilities() Capabilities
}

type ConnectionWithOSFamily interface {
OSFamily() OSFamily
}

type SimpleConnection interface {
plugin.Connection
Name() string
Expand Down
37 changes: 25 additions & 12 deletions providers/os/resources/groups/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,32 @@ func ResolveManager(conn shared.Connection) (OSGroupManager, error) {
var gm OSGroupManager

asset := conn.Asset()
if asset == nil || asset.Platform == nil {
return nil, errors.New("cannot find OS information for users detection")
if osFamilyConn, ok := conn.(shared.ConnectionWithOSFamily); ok {
osFamily := osFamilyConn.OSFamily()
switch osFamily {
case shared.OSFamily_Windows:
gm = &WindowsGroupManager{conn: conn}
case shared.OSFamily_Unix:
gm = &UnixGroupManager{conn: conn}
case shared.OSFamily_Darwin:
gm = &OSXGroupManager{conn: conn}
default:
return nil, errors.New("could not detect suitable group manager for platform: " + string(osFamily))
}
} else {
if asset == nil || asset.Platform == nil {
return nil, errors.New("cannot find OS information for users detection")
}

// check darwin before unix since darwin is also a unix
if asset.Platform.IsFamily("darwin") {
gm = &OSXGroupManager{conn: conn}
} else if asset.Platform.IsFamily("unix") {
gm = &UnixGroupManager{conn: conn}
} else if asset.Platform.IsFamily("windows") {
gm = &WindowsGroupManager{conn: conn}
}
}

// check darwin before unix since darwin is also a unix
if asset.Platform.IsFamily("darwin") {
gm = &OSXGroupManager{conn: conn}
} else if asset.Platform.IsFamily("unix") {
gm = &UnixGroupManager{conn: conn}
} else if asset.Platform.IsFamily("windows") {
gm = &WindowsGroupManager{conn: conn}
}

if gm == nil {
return nil, errors.New("could not detect suitable group manager for platform: " + asset.Platform.Name)
}
Expand Down
34 changes: 24 additions & 10 deletions providers/os/resources/users/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,31 @@ func ResolveManager(conn shared.Connection) (OSUserManager, error) {
var um OSUserManager

asset := conn.Asset()
if asset == nil || asset.Platform == nil {
return nil, errors.New("cannot find OS information for users detection")
}
if osFamilyConn, ok := conn.(shared.ConnectionWithOSFamily); ok {
osFamily := osFamilyConn.OSFamily()
switch osFamily {
case shared.OSFamily_Windows:
um = &WindowsUserManager{conn: conn}
case shared.OSFamily_Unix:
um = &UnixUserManager{conn: conn}
case shared.OSFamily_Darwin:
um = &OSXUserManager{conn: conn}
default:
return nil, errors.New("could not detect suitable group manager for platform: " + string(osFamily))
}
} else {
if asset == nil || asset.Platform == nil {
return nil, errors.New("cannot find OS information for users detection")
}

// check darwin before unix since darwin is also a unix
if asset.Platform.IsFamily("darwin") {
um = &OSXUserManager{conn: conn}
} else if asset.Platform.IsFamily("unix") {
um = &UnixUserManager{conn: conn}
} else if asset.Platform.IsFamily("windows") {
um = &WindowsUserManager{conn: conn}
// check darwin before unix since darwin is also a unix
if asset.Platform.IsFamily("darwin") {
um = &OSXUserManager{conn: conn}
} else if asset.Platform.IsFamily("unix") {
um = &UnixUserManager{conn: conn}
} else if asset.Platform.IsFamily("windows") {
um = &WindowsUserManager{conn: conn}
}
}

if um == nil {
Expand Down

0 comments on commit fcf386c

Please sign in to comment.