-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⭐️ add aws-ecs id-detector, ensure the iddetectors get passed thru (#880
) adding aws-ecs id-detector to allow for ecs container id detection when running a local scan on an ecs container. also, it looks like we dropped support for passing thru the user-id-detector on the local scan path at least at some point, it's back in there now `scan local --id-detector aws-ecs`
- Loading branch information
Showing
12 changed files
with
285 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package awsecsid | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws/arn" | ||
"github.com/cockroachdb/errors" | ||
"go.mondoo.com/cnquery/motor/platform" | ||
"go.mondoo.com/cnquery/motor/providers/local" | ||
"go.mondoo.com/cnquery/motor/providers/mock" | ||
"go.mondoo.com/cnquery/motor/providers/os" | ||
) | ||
|
||
func MondooECSContainerID(containerArn string) string { | ||
var account, region, id string | ||
if arn.IsARN(containerArn) { | ||
if p, err := arn.Parse(containerArn); err == nil { | ||
account = p.AccountID | ||
region = p.Region | ||
id = p.Resource | ||
} | ||
} | ||
return "//platformid.api.mondoo.app/runtime/aws/ecs/v1/accounts/" + account + "/regions/" + region + "/" + id | ||
} | ||
|
||
type Identity struct { | ||
ContainerArn string | ||
Name string | ||
RuntimeID string | ||
PlatformIds []string | ||
AccountPlatformID string | ||
} | ||
type InstanceIdentifier interface { | ||
Identify() (Identity, error) | ||
} | ||
|
||
func Resolve(provider os.OperatingSystemProvider, pf *platform.Platform) (InstanceIdentifier, error) { | ||
_, ok := provider.(*local.Provider) | ||
if ok { | ||
return NewContainerMetadata(provider, pf), nil | ||
} | ||
_, ok = provider.(*mock.Provider) | ||
if ok { | ||
return NewContainerMetadata(provider, pf), nil | ||
} | ||
|
||
return nil, errors.New(fmt.Sprintf("awsecs id detector is not supported for your asset: %s %s", pf.Name, pf.Version)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package awsecsid | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws/arn" | ||
"github.com/cockroachdb/errors" | ||
"github.com/rs/zerolog/log" | ||
"go.mondoo.com/cnquery/motor/motorid/containerid" | ||
"go.mondoo.com/cnquery/motor/platform" | ||
"go.mondoo.com/cnquery/motor/providers/os" | ||
) | ||
|
||
const ( | ||
identityUrl = "${ECS_CONTAINER_METADATA_URI_V4}" | ||
) | ||
|
||
func NewContainerMetadata(provider os.OperatingSystemProvider, pf *platform.Platform) *ContainerMetadata { | ||
return &ContainerMetadata{ | ||
provider: provider, | ||
platform: pf, | ||
} | ||
} | ||
|
||
type ContainerMetadata struct { | ||
provider os.OperatingSystemProvider | ||
platform *platform.Platform | ||
} | ||
|
||
func (m *ContainerMetadata) Identify() (Identity, error) { | ||
log.Debug().Msg("getting ecs container identity") | ||
|
||
containerDocument, err := m.containerIdentityDocument() | ||
if err != nil { | ||
return Identity{}, err | ||
} | ||
// parse into struct | ||
doc := EcrContainerIdentityDoc{} | ||
if err := json.NewDecoder(strings.NewReader(containerDocument)).Decode(&doc); err != nil { | ||
return Identity{}, errors.Wrap(err, "failed to decode ECS container identity document") | ||
} | ||
var accountID string | ||
if arn.IsARN(doc.ContainerArn) { | ||
if p, err := arn.Parse(doc.ContainerArn); err == nil { | ||
accountID = p.AccountID | ||
} | ||
} | ||
return Identity{ | ||
Name: doc.Name, | ||
ContainerArn: doc.ContainerArn, | ||
RuntimeID: doc.DockerId, | ||
AccountPlatformID: "//platformid.api.mondoo.app/runtime/aws/accounts/" + accountID, | ||
PlatformIds: []string{MondooECSContainerID(doc.ContainerArn), containerid.MondooContainerID(doc.DockerId)}, | ||
}, nil | ||
} | ||
|
||
func (m *ContainerMetadata) curlDocument(url string) (string, error) { | ||
cmd, err := m.provider.RunCommand("curl " + url) | ||
if err != nil { | ||
return "", err | ||
} | ||
data, err := ioutil.ReadAll(cmd.Stdout) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return strings.TrimSpace(string(data)), nil | ||
} | ||
|
||
func (m *ContainerMetadata) containerIdentityDocument() (string, error) { | ||
return m.curlDocument(identityUrl) | ||
} | ||
|
||
type EcrContainerIdentityDoc struct { | ||
DockerId string `json:"DockerId"` | ||
Name string `json:"Name"` | ||
ContainerArn string `json:"ContainerARN"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package awsecsid | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.mondoo.com/cnquery/motor" | ||
"go.mondoo.com/cnquery/motor/providers/mock" | ||
) | ||
|
||
func TestEC2RoleProviderInstanceIdentityUnix(t *testing.T) { | ||
provider, err := mock.NewFromTomlFile("./testdata/container-identity.toml") | ||
require.NoError(t, err) | ||
|
||
m, err := motor.New(provider) | ||
require.NoError(t, err) | ||
|
||
p, err := m.Platform() | ||
require.NoError(t, err) | ||
|
||
metadata := NewContainerMetadata(provider, p) | ||
ident, err := metadata.Identify() | ||
|
||
require.Nil(t, err) | ||
require.Equal(t, "fargate-app", ident.Name) | ||
require.Equal(t, "arn:aws:ecs:us-east-1:172746783610:container/vjtest/f088b38d61ac45d6a946b5aebbe7197a/314e35e0-2d0a-4408-b37e-16063461d73a", ident.ContainerArn) | ||
require.Equal(t, "f088b38d61ac45d6a946b5aebbe7197a-3681984407", ident.RuntimeID) | ||
require.Contains(t, ident.PlatformIds, "//platformid.api.mondoo.app/runtime/docker/containers/f088b38d61ac45d6a946b5aebbe7197a-3681984407") | ||
require.Contains(t, ident.PlatformIds, "//platformid.api.mondoo.app/runtime/aws/ecs/v1/accounts/172746783610/regions/us-east-1/container/vjtest/f088b38d61ac45d6a946b5aebbe7197a/314e35e0-2d0a-4408-b37e-16063461d73a") | ||
require.Contains(t, ident.AccountPlatformID, "//platformid.api.mondoo.app/runtime/aws/accounts/172746783610") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[commands."uname -s"] | ||
stdout = "Linux" | ||
|
||
[commands."uname -m"] | ||
stdout = "x86_64" | ||
|
||
[commands."uname -r"] | ||
stdout = "4.9.125-linuxkit" | ||
|
||
[files."/etc/redhat-release"] | ||
content = "Red Hat Enterprise Linux Server release 7.2 (Maipo)" | ||
|
||
[commands."curl ${ECS_CONTAINER_METADATA_URI_V4}"] | ||
stdout = """ | ||
{ | ||
"DockerId":"f088b38d61ac45d6a946b5aebbe7197a-3681984407", | ||
"Name":"fargate-app", | ||
"DockerName":"fargate-app", | ||
"Image":"public.ecr.aws/docker/library/httpd:latest", | ||
"ImageID":"sha256:87a012bf99bf5e3e0f628ac1f69abbeab534282857fba3a359ca3a3f4a02429a", | ||
"Labels":{"com.amazonaws.ecs.cluster":"arn:aws:ecs:us-east-1:172746783610:cluster/vjtest","com.amazonaws.ecs.container-name":"fargate-app","com.amazonaws.ecs.task-arn":"arn:aws:ecs:us-east-1:172746783610:task/vjtest/f088b38d61ac45d6a946b5aebbe7197a","com.amazonaws.ecs.task-definition-family":"sample-fargate","com.amazonaws.ecs.task-definition-version":"2"}, | ||
"DesiredStatus":"RUNNING", | ||
"KnownStatus":"RUNNING", | ||
"Limits":{"CPU":2}, | ||
"CreatedAt":"2023-01-31T06:19:11.226060573Z", | ||
"StartedAt":"2023-01-31T06:19:11.226060573Z", | ||
"Type":"NORMAL", | ||
"Networks":[{"NetworkMode":"awsvpc","IPv4Addresses":["172.31.12.124"],"AttachmentIndex":0,"MACAddress":"02:ee:fc:59:ac:5f","IPv4SubnetCIDRBlock":"172.31.0.0/20","DomainNameServers":["172.31.0.2"],"DomainNameSearchList":["ec2.internal"],"PrivateDNSName":"ip-172-31-12-124.ec2.internal","SubnetGatewayIpv4Address":"172.31.0.1/20"}], | ||
"ContainerARN":"arn:aws:ecs:us-east-1:172746783610:container/vjtest/f088b38d61ac45d6a946b5aebbe7197a/314e35e0-2d0a-4408-b37e-16063461d73a" | ||
} | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
motor/motorid/clouddetect/providers/aws/testdata/container.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
[commands."uname -s"] | ||
stdout = "Linux" | ||
|
||
[commands."uname -m"] | ||
stdout = "x86_64" | ||
|
||
[commands."uname -r"] | ||
stdout = "4.14.301-224.520.amzn2.x86_64t" | ||
|
||
[files."/etc/os-release"] | ||
content = "PRETTY_NAME='Debian GNU/Linux 11 (bullseye)' NAME='Debian GNU/Linux' VERSION_ID='11' VERSION='11 (bullseye)' VERSION_CODENAME=bullseye ID=debian HOME_URL='https://www.debian.org/' SUPPORT_URL='https://www.debian.org/support' BUG_REPORT_URL='https://bugs.debian.org/'" | ||
|
||
[files."/sys/class/dmi/id/bios_vendor"] | ||
path = "/sys/class/dmi/id/bios_vendor" | ||
enoent = false | ||
content = "Amazon EC2" | ||
|
||
[commands."curl ${ECS_CONTAINER_METADATA_URI_V4}"] | ||
stdout = """ | ||
{ | ||
"DockerId":"f088b38d61ac45d6a946b5aebbe7197a-3681984407", | ||
"Name":"fargate-app", | ||
"DockerName":"fargate-app", | ||
"Image":"public.ecr.aws/docker/library/httpd:latest", | ||
"ImageID":"sha256:87a012bf99bf5e3e0f628ac1f69abbeab534282857fba3a359ca3a3f4a02429a", | ||
"Labels":{"com.amazonaws.ecs.cluster":"arn:aws:ecs:us-east-1:172746783610:cluster/vjtest","com.amazonaws.ecs.container-name":"fargate-app","com.amazonaws.ecs.task-arn":"arn:aws:ecs:us-east-1:172746783610:task/vjtest/f088b38d61ac45d6a946b5aebbe7197a","com.amazonaws.ecs.task-definition-family":"sample-fargate","com.amazonaws.ecs.task-definition-version":"2"}, | ||
"DesiredStatus":"RUNNING", | ||
"KnownStatus":"RUNNING", | ||
"Limits":{"CPU":2}, | ||
"CreatedAt":"2023-01-31T06:19:11.226060573Z", | ||
"StartedAt":"2023-01-31T06:19:11.226060573Z", | ||
"Type":"NORMAL", | ||
"Networks":[{"NetworkMode":"awsvpc","IPv4Addresses":["172.31.12.124"],"AttachmentIndex":0,"MACAddress":"02:ee:fc:59:ac:5f","IPv4SubnetCIDRBlock":"172.31.0.0/20","DomainNameServers":["172.31.0.2"],"DomainNameSearchList":["ec2.internal"],"PrivateDNSName":"ip-172-31-12-124.ec2.internal","SubnetGatewayIpv4Address":"172.31.0.1/20"}], | ||
"ContainerARN":"arn:aws:ecs:us-east-1:172746783610:container/vjtest/f088b38d61ac45d6a946b5aebbe7197a/314e35e0-2d0a-4408-b37e-16063461d73a" | ||
} | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters