Skip to content

Commit d0c9879

Browse files
authored
✨ add spaceId support for MondooAuditConfig to route assets to specific spaces (#1464)
- Introduced `spaceId` field in `MondooAuditConfig` CRD to allow routing of scanned assets to a specified Mondoo space, enabling the use of org-level service accounts across multiple spaces. - Updated sample configurations and documentation to reflect the new `spaceId` functionality. - Modified controller logic to create derived Secrets with injected `scope_mrn` when `spaceId` is set. - Enhanced tests to cover new functionality, including the creation and verification of derived Secrets. - Added end-to-end tests for space splitting scenarios, ensuring correct routing of assets to designated spaces. ✨ clean up leftover override secrets when spaceId is removed and add warning for space-scoped service accounts ✨ enhance SyncConfigOverrideSecret to handle deletion errors and improve comments for clarity ✨ remove ownership of Secrets in MondooAuditConfig reconciler setup
1 parent d8bdfa6 commit d0c9879

28 files changed

+896
-13
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,6 @@ tests/e2e/**/kubeconfig
6767
tests/e2e/**/kubeconfig-target
6868
tests/e2e/**/gke_gcloud_auth_plugin_cache
6969
tests/e2e/**/mondoo.json
70+
71+
# macOS
72+
.DS_Store

api/v1alpha2/mondooauditconfig_types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ type MondooAuditConfigSpec struct {
2929
Filtering Filtering `json:"filtering,omitempty"`
3030
Containers Containers `json:"containers,omitempty"`
3131

32+
// SpaceID optionally specifies the target Mondoo space for asset routing.
33+
// When set, scanned assets are sent to this space instead of the space
34+
// associated with the service account credentials. This allows using an
35+
// org-level service account across multiple spaces.
36+
// +optional
37+
SpaceID string `json:"spaceId,omitempty"`
38+
3239
// Annotations allows adding custom annotations to all scanned assets. These key-value pairs
3340
// will be attached to every asset discovered by the operator, making them searchable
3441
// and filterable in the Mondoo Console.

charts/mondoo-operator/crds/k8s.mondoo.com_mondooauditconfigs.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,13 @@ spec:
13121312
default: mondoo-operator-k8s-resources-scanning
13131313
type: string
13141314
type: object
1315+
spaceId:
1316+
description: |-
1317+
SpaceID optionally specifies the target Mondoo space for asset routing.
1318+
When set, scanned assets are sent to this space instead of the space
1319+
associated with the service account credentials. This allows using an
1320+
org-level service account across multiple spaces.
1321+
type: string
13151322
required:
13161323
- mondooCredsSecretRef
13171324
type: object

charts/mondoo-operator/crds/k8s.mondoo.com_mondoooperatorconfigs.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ spec:
9393
ResourceLabels allows providing a list of extra labels to apply to the metrics-related
9494
resources (eg. ServiceMonitor)
9595
type: object
96+
secureMetrics:
97+
description: |-
98+
SecureMetrics enables authentication and authorization on the metrics endpoint
99+
using controller-runtime's built-in TLS and RBAC-based auth.
100+
When enabled, metrics are served over HTTPS on port 8443 with token-based auth.
101+
When disabled, metrics are served over HTTP on port 8080 without auth.
102+
type: boolean
96103
type: object
97104
noProxy:
98105
description: NoProxy specifies a comma-separated list of hosts that

charts/mondoo-operator/files/crds/k8s.mondoo.com_mondooauditconfigs.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,13 @@ spec:
13121312
default: mondoo-operator-k8s-resources-scanning
13131313
type: string
13141314
type: object
1315+
spaceId:
1316+
description: |-
1317+
SpaceID optionally specifies the target Mondoo space for asset routing.
1318+
When set, scanned assets are sent to this space instead of the space
1319+
associated with the service account credentials. This allows using an
1320+
org-level service account across multiple spaces.
1321+
type: string
13151322
required:
13161323
- mondooCredsSecretRef
13171324
type: object

charts/mondoo-operator/templates/manager-rbac.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ rules:
3636
- create
3737
- delete
3838
- get
39+
- update
3940
- apiGroups:
4041
- ""
4142
resources:

cmd/mondoo-operator/garbage_collect/cmd.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func init() {
3030
filterPlatformRuntime := Cmd.Flags().String("filter-platform-runtime", "", "Cleanup assets by an asset's PlatformRuntime (k8s-cluster or docker-image).")
3131
filterManagedBy := Cmd.Flags().String("filter-managed-by", "", "Cleanup assets with matching ManagedBy field.")
3232
filterOlderThan := Cmd.Flags().String("filter-older-than", "", "Cleanup assets which have not been updated in over the time provided (eg 12m or 48h or anything time.ParseDuration() accepts).")
33+
spaceMrnOverride := Cmd.Flags().String("space-mrn", "", "Override the space MRN for garbage collection (used when spaceId is set in MondooAuditConfig).")
3334
Cmd.RunE = func(cmd *cobra.Command, args []string) error {
3435
log.SetLogger(logger.NewLogger())
3536
logger := log.Log.WithName("garbage-collect")
@@ -77,9 +78,12 @@ func init() {
7778
return fmt.Errorf("no filters provided to garbage collect by")
7879
}
7980

80-
spaceMrn := serviceAccount.SpaceMrn
81+
spaceMrn := *spaceMrnOverride
8182
if spaceMrn == "" {
82-
spaceMrn = mondoo.SpaceMrnFromServiceAccountMrn(serviceAccount.Mrn)
83+
spaceMrn = serviceAccount.SpaceMrn
84+
if spaceMrn == "" {
85+
spaceMrn = mondoo.SpaceMrnFromServiceAccountMrn(serviceAccount.Mrn)
86+
}
8387
}
8488
return GarbageCollectCmd(ctx, client, spaceMrn, *filterPlatformRuntime, *filterOlderThan, *filterManagedBy, logger)
8589
}

config/crd/bases/k8s.mondoo.com_mondooauditconfigs.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,13 @@ spec:
13121312
default: mondoo-operator-k8s-resources-scanning
13131313
type: string
13141314
type: object
1315+
spaceId:
1316+
description: |-
1317+
SpaceID optionally specifies the target Mondoo space for asset routing.
1318+
When set, scanned assets are sent to this space instead of the space
1319+
associated with the service account credentials. This allows using an
1320+
org-level service account across multiple spaces.
1321+
type: string
13151322
required:
13161323
- mondooCredsSecretRef
13171324
type: object

config/samples/k8s_v1alpha2_mondooauditconfig.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@ metadata:
88
name: mondoo-client
99
namespace: mondoo-operator
1010
spec:
11-
# Required: Reference to secret containing Mondoo service account credentials
11+
# Required: Reference to secret containing Mondoo service account credentials.
12+
# Can be a space-level or org-level service account.
1213
mondooCredsSecretRef:
1314
name: mondoo-client
1415

16+
# Optional: Route scanned assets to a specific Mondoo space.
17+
# When set, the operator injects the target space into the scanner config,
18+
# overriding the space derived from the service account credentials.
19+
# This enables using a single org-level service account across multiple
20+
# MondooAuditConfigs, each targeting a different space.
21+
# spaceId: "your-space-1234"
22+
1523
# Optional: Reference to secret containing a Mondoo registration token
1624
# If provided and mondooCredsSecretRef secret doesn't exist, the operator
1725
# will exchange this token for a service account

controllers/container_image/resources.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func CronJob(image, integrationMrn, clusterUid, privateRegistrySecretName string
140140
},
141141
{
142142
Secret: &corev1.SecretProjection{
143-
LocalObjectReference: m.Spec.MondooCredsSecretRef,
143+
LocalObjectReference: k8s.ConfigSecretRef(*m),
144144
Items: []corev1.KeyToPath{{
145145
Key: "config",
146146
Path: "mondoo.yml",

0 commit comments

Comments
 (0)