Skip to content

Commit 0fc5d6e

Browse files
MahatiCambarve
authored andcommitted
C-WCOW: Address review comments
Signed-off-by: Mahati Chamarthy <mahati.chamarthy@gmail.com>
1 parent 69ebd30 commit 0fc5d6e

File tree

7 files changed

+26
-28
lines changed

7 files changed

+26
-28
lines changed

internal/gcs-sidecar/handlers.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ func (b *Bridge) createContainer(req *request) (err error) {
8484
user := securitypolicy.IDName{
8585
Name: spec.Process.User.Username,
8686
}
87-
log.G(ctx).Tracef("user test: %v", user)
8887
_, _, _, err := b.hostState.securityPolicyEnforcer.EnforceCreateContainerPolicyV2(req.ctx, containerID, spec.Process.Args, spec.Process.Env, spec.Process.Cwd, spec.Mounts, user, nil)
8988

9089
if err != nil {
@@ -97,11 +96,12 @@ func (b *Bridge) createContainer(req *request) (err error) {
9796
}
9897
log.G(ctx).Tracef("Adding ContainerID: %v", containerID)
9998
if err := b.hostState.AddContainer(req.ctx, containerID, c); err != nil {
100-
log.G(ctx).Tracef("Container exists in the map!")
99+
log.G(ctx).Tracef("Container exists in the map.")
100+
return err
101101
}
102102
defer func(err error) {
103103
if err != nil {
104-
b.hostState.RemoveContainer(containerID)
104+
b.hostState.RemoveContainer(ctx, containerID)
105105
}
106106
}(err)
107107
// Write security policy, signed UVM reference and host AMD certificate to
@@ -242,9 +242,6 @@ func (b *Bridge) shutdownGraceful(req *request) (err error) {
242242
return fmt.Errorf("failed to unmarshal shutdownGraceful: %w", err)
243243
}
244244

245-
// TODO (kiashok/Mahati): Since gcs-sidecar can be used for all types of windows
246-
// containers, it is important to check if we want to
247-
// enforce policy or not.
248245
err = b.hostState.securityPolicyEnforcer.EnforceShutdownContainerPolicy(req.ctx, r.ContainerID)
249246
if err != nil {
250247
return fmt.Errorf("rpcShudownGraceful operation not allowed: %w", err)
@@ -313,7 +310,7 @@ func (b *Bridge) executeProcess(req *request) (err error) {
313310
c, err := b.hostState.GetCreatedContainer(req.ctx, containerID)
314311
if err != nil {
315312
log.G(req.ctx).Tracef("Container not found during exec: %v", containerID)
316-
return errors.Wrapf(err, "containerID doesn't exist")
313+
return fmt.Errorf("failed to get created container: %w", err)
317314
}
318315

319316
// if this is an exec of Container command line, then it's already enforced
@@ -420,7 +417,7 @@ func (b *Bridge) signalProcess(req *request) (err error) {
420417
containerID := r.ContainerID
421418
c, err := b.hostState.GetCreatedContainer(req.ctx, containerID)
422419
if err != nil {
423-
return err
420+
return fmt.Errorf("failed to get created container: %w", err)
424421
}
425422

426423
p, err := c.GetProcess(r.ProcessID)
@@ -515,15 +512,13 @@ func (b *Bridge) deleteContainerState(req *request) (err error) {
515512
if err := commonutils.UnmarshalJSONWithHresult(req.message, &r); err != nil {
516513
return fmt.Errorf("failed to unmarshal deleteContainerState: %w", err)
517514
}
518-
519-
//TODO (Mahati): Remove container state locally before passing it to inbox-gcs
520-
/*
521-
c, err := b.hostState.GetCreatedContainer(request.ContainerID)
522-
if err != nil {
523-
return nil, err
524-
}
525-
// remove container state regardless of delete's success
526-
defer b.hostState.RemoveContainer(request.ContainerID)*/
515+
_, err = b.hostState.GetCreatedContainer(req.ctx, r.ContainerID)
516+
if err != nil {
517+
log.G(req.ctx).Tracef("Container not found during deleteContainerState: %v", r.ContainerID)
518+
return fmt.Errorf("container not found: %w", err)
519+
}
520+
// remove container state regardless of delete's success
521+
defer b.hostState.RemoveContainer(req.ctx, r.ContainerID)
527522

528523
b.forwardRequestToGcs(req)
529524
return nil

internal/gcs-sidecar/host.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ func (h *Host) SetWCOWConfidentialUVMOptions(ctx context.Context, securityPolicy
169169
DefaultCRIMounts(),
170170
DefaultCRIPrivilegedMounts(),
171171
maxErrorMessageLength,
172-
"windows",
173172
)
174173
if err != nil {
175174
return fmt.Errorf("error creating security policy enforcer: %w", err)
@@ -193,18 +192,20 @@ func (h *Host) AddContainer(ctx context.Context, id string, c *Container) error
193192

194193
if _, ok := h.containers[id]; ok {
195194
log.G(ctx).Tracef("Container exists in the map: %v", ok)
195+
return gcserr.NewHresultError(gcserr.HrVmcomputeSystemAlreadyExists)
196196
}
197197
log.G(ctx).Tracef("AddContainer: ID: %v", id)
198198
h.containers[id] = c
199199
return nil
200200
}
201201

202-
func (h *Host) RemoveContainer(id string) {
202+
func (h *Host) RemoveContainer(ctx context.Context, id string) {
203203
h.containersMutex.Lock()
204204
defer h.containersMutex.Unlock()
205205

206206
_, ok := h.containers[id]
207207
if !ok {
208+
log.G(ctx).Tracef("RemoveContainer: Container not found: ID: %v", id)
208209
return
209210
}
210211

@@ -217,6 +218,7 @@ func (h *Host) GetCreatedContainer(ctx context.Context, id string) (*Container,
217218

218219
c, ok := h.containers[id]
219220
if !ok {
221+
log.G(ctx).Tracef("GetCreatedContainer: Container not found: ID: %v", id)
220222
return nil, gcserr.NewHresultError(gcserr.HrVmcomputeSystemNotFound)
221223
}
222224
return c, nil

internal/guest/runtime/hcsv2/uvm.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ func (h *Host) SetConfidentialUVMOptions(ctx context.Context, r *guestresource.L
121121
policy.DefaultCRIMounts(),
122122
policy.DefaultCRIPrivilegedMounts(),
123123
maxErrorMessageLength,
124-
"linux",
125124
)
126125
if err != nil {
127126
return err

pkg/securitypolicy/securitypolicy_linux.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import (
1717
"github.com/pkg/errors"
1818
)
1919

20+
//nolint:unused
21+
const osType = "linux"
22+
2023
// This is being used by StandEnforcer.
2124
// substituteUVMPath substitutes mount prefix to an appropriate path inside
2225
// UVM. At policy generation time, it's impossible to tell what the sandboxID

pkg/securitypolicy/securitypolicy_windows.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ package securitypolicy
55

66
import oci "github.com/opencontainers/runtime-spec/specs-go"
77

8+
//nolint:unused
9+
const osType = "windows"
10+
811
// This is being used by StandEnforcer and is a no-op for windows.
912
// substituteUVMPath substitutes mount prefix to an appropriate path inside
1013
// UVM. At policy generation time, it's impossible to tell what the sandboxID

pkg/securitypolicy/securitypolicyenforcer.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"regexp"
99
"strconv"
10-
"strings"
1110
"sync"
1211
"syscall"
1312

@@ -57,11 +56,6 @@ var (
5756
defaultEnforcer = standardEnforcer
5857
)
5958

60-
// Package-level variable for OS type, set during enforcer creation
61-
//
62-
//nolint:unused
63-
var osType string = "unknown"
64-
6559
func init() {
6660
registeredEnforcers[openDoorEnforcer] = createOpenDoorEnforcer
6761
registeredEnforcers[standardEnforcer] = createStandardEnforcer
@@ -260,15 +254,13 @@ func CreateSecurityPolicyEnforcer(
260254
criMounts,
261255
criPrivilegedMounts []oci.Mount,
262256
maxErrorMessageLength int,
263-
operatingSystem string,
264257
) (SecurityPolicyEnforcer, error) {
265258
if enforcer == "" {
266259
enforcer = defaultEnforcer
267260
if base64EncodedPolicy == "" {
268261
enforcer = openDoorEnforcer
269262
}
270263
}
271-
osType = strings.ToLower(operatingSystem)
272264

273265
if createEnforcer, ok := registeredEnforcers[enforcer]; !ok {
274266
return nil, fmt.Errorf("unknown enforcer: %q", enforcer)

pkg/securitypolicy/securitypolicyenforcer_rego.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ func (policy *regoEnforcer) enableLogging(path string, logLevel rpi.LogLevel) {
211211
func newRegoPolicy(code string, defaultMounts []oci.Mount, privilegedMounts []oci.Mount, osType string) (policy *regoEnforcer, err error) {
212212
policy = new(regoEnforcer)
213213

214+
if osType != "windows" && osType != "linux" {
215+
return nil, fmt.Errorf("unsupported operating system: %q", osType)
216+
}
217+
214218
policy.osType = osType
215219
policy.defaultMounts = make([]oci.Mount, len(defaultMounts))
216220
copy(policy.defaultMounts, defaultMounts)

0 commit comments

Comments
 (0)