From fcf386cf07c8ebf9ae3e438f56c7734aa0eb431e Mon Sep 17 00:00:00 2001 From: Ivan Milchev Date: Mon, 7 Oct 2024 15:15:52 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20dockerfile=20asset=20famil?= =?UTF-8?q?y=20(#4717)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ivan Milchev --- .../docker/docker_file_connection.go | 18 ++++++++- providers/os/connection/shared/shared.go | 13 +++++++ providers/os/resources/groups/manager.go | 37 +++++++++++++------ providers/os/resources/users/manager.go | 34 ++++++++++++----- 4 files changed, 78 insertions(+), 24 deletions(-) diff --git a/providers/os/connection/docker/docker_file_connection.go b/providers/os/connection/docker/docker_file_connection.go index 12a003a2d4..05470445b5 100644 --- a/providers/os/connection/docker/docker_file_connection.go +++ b/providers/os/connection/docker/docker_file_connection.go @@ -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" @@ -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, @@ -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) @@ -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 +} diff --git a/providers/os/connection/shared/shared.go b/providers/os/connection/shared/shared.go index 02c9e62e54..bca4e18341 100644 --- a/providers/os/connection/shared/shared.go +++ b/providers/os/connection/shared/shared.go @@ -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) @@ -59,6 +68,10 @@ type Connection interface { Capabilities() Capabilities } +type ConnectionWithOSFamily interface { + OSFamily() OSFamily +} + type SimpleConnection interface { plugin.Connection Name() string diff --git a/providers/os/resources/groups/manager.go b/providers/os/resources/groups/manager.go index b3b20d3643..aff56af974 100644 --- a/providers/os/resources/groups/manager.go +++ b/providers/os/resources/groups/manager.go @@ -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) } diff --git a/providers/os/resources/users/manager.go b/providers/os/resources/users/manager.go index 6c10b345b7..e569f4dbd3 100644 --- a/providers/os/resources/users/manager.go +++ b/providers/os/resources/users/manager.go @@ -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 {