Skip to content

Commit b606172

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

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

internal/gcs-sidecar/handlers.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,8 @@ func (b *Bridge) modifySettings(req *request) (err error) {
698698
} else {
699699
log.G(ctx).Debugf("No cached CIM hashes found for volume %s", guidStr)
700700
}
701+
} else {
702+
return fmt.Errorf("no cim hashes found for container ID %s", containerID)
701703
}
702704
}
703705

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)