Skip to content

Commit 9994456

Browse files
committed
C-WCOW: Add a test for verified CIM policy enforcement
Signed-off-by: Mahati Chamarthy <mahati.chamarthy@gmail.com>
1 parent ae93ae0 commit 9994456

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

internal/gcs-sidecar/handlers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -741,19 +741,19 @@ func (b *Bridge) modifySettings(req *request) (err error) {
741741
layerPath := settings.CombinedLayers.Layers[0].Path
742742
if guidStr, ok := volumeGUIDFromLayerPath(layerPath); ok {
743743
hashes, haveHashes := b.hostState.blockCIMVolumeHashes[guidStr]
744+
// This must always be true for every container as the hashes are recorded during initial CIM mount in ResourceTypeWCOWBlockCims request
744745
if haveHashes {
745-
// Only do this if it wasn't already enforced by the ResourceTypeWCOWBlockCims request
746746
containers := b.hostState.blockCIMVolumeContainers[guidStr]
747747
if _, seen := containers[containerID]; !seen {
748-
// This is a container with CIMs already mounted (container with similar layers as an existing container). Call EnforceVerifiedCIMsPolicy on this new container
748+
// This is a container with CIMs already mounted. Just Call EnforceVerifiedCIMsPolicy on this to record in policy metadata
749749
log.G(ctx).Tracef("Verified CIM hashes for reused mount volume %s (container %s)", guidStr, containerID)
750750
if err := b.hostState.securityPolicyEnforcer.EnforceVerifiedCIMsPolicy(ctx, containerID, hashes); err != nil {
751751
return fmt.Errorf("CIM mount is denied by policy for this container: %w", err)
752752
}
753753
containers[containerID] = struct{}{}
754754
}
755755
} else {
756-
log.G(ctx).Debugf("No cached CIM hashes found for volume %s", guidStr)
756+
return fmt.Errorf("No CIM hashes found for container ID %s", containerID)
757757
}
758758
}
759759
}

pkg/securitypolicy/regopolicy_windows_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
_ "embed"
99
"fmt"
1010
"math/rand"
11+
"strconv"
1112
"strings"
1213
"testing"
1314
"testing/quick"
@@ -315,6 +316,45 @@ func Test_Rego_EnforceCreateContainer_Same_Container_Twice_Windows(t *testing.T)
315316
}
316317
}
317318

319+
func Test_Rego_EnforceVerifiedCIMSPolicy_Multiple_Instances_Same_Container(t *testing.T) {
320+
for containersToCreate := 5; containersToCreate <= maxContainersInGeneratedConstraints; containersToCreate++ {
321+
constraints := new(generatedWindowsConstraints)
322+
constraints.ctx = context.Background()
323+
constraints.externalProcesses = generateExternalProcesses(testRand)
324+
325+
for i := 1; i <= containersToCreate; i++ {
326+
arg := "command " + strconv.Itoa(i)
327+
c := &securityPolicyWindowsContainer{
328+
Command: []string{arg},
329+
Layers: []string{"1", "2"},
330+
}
331+
332+
constraints.containers = append(constraints.containers, c)
333+
}
334+
335+
securityPolicy := constraints.toPolicy()
336+
policy, err := newRegoPolicy(securityPolicy.marshalWindowsRego(), []oci.Mount{}, []oci.Mount{}, testOSType)
337+
338+
if err != nil {
339+
t.Fatalf("failed create enforcer")
340+
}
341+
342+
for _, container := range constraints.containers {
343+
// Reverse container.Layers to satisfy layerHashes_ok ordering
344+
layerHashes := make([]string, len(container.Layers))
345+
for i, layer := range container.Layers {
346+
layerHashes[len(container.Layers)-1-i] = layer
347+
}
348+
349+
id := testDataGenerator.uniqueContainerID()
350+
err = policy.EnforceVerifiedCIMsPolicy(constraints.ctx, id, layerHashes)
351+
if err != nil {
352+
t.Fatalf("failed with %d containers", containersToCreate)
353+
}
354+
}
355+
}
356+
}
357+
318358
// -- Capabilities/Mount/Rego version tests are removed -- Add back Rego versions test//
319359
func Test_Rego_ExecInContainerPolicy_Windows(t *testing.T) {
320360
f := func(p *generatedWindowsConstraints) bool {

0 commit comments

Comments
 (0)