Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update securitypolicy struct names and docstrings #1278

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions internal/guest/runtime/hcsv2/uvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Host struct {

// state required for the security policy enforcement
policyMutex sync.Mutex
securityPolicyEnforcer securitypolicy.SecurityPolicyEnforcer
securityPolicyEnforcer securitypolicy.PolicyEnforcer
securityPolicyEnforcerSet bool
}

Expand All @@ -62,7 +62,7 @@ func NewHost(rtime runtime.Runtime, vsock transport.Transport) *Host {
rtime: rtime,
vsock: vsock,
securityPolicyEnforcerSet: false,
securityPolicyEnforcer: &securitypolicy.OpenDoorSecurityPolicyEnforcer{},
securityPolicyEnforcer: &securitypolicy.OpenDoorEnforcer{},
}
}

Expand Down Expand Up @@ -217,7 +217,7 @@ func (h *Host) CreateContainer(ctx context.Context, id string, settings *prot.VM
// security policy variable cannot be included in the security policy as its value is not available
// security policy construction time.

if policyEnforcer, ok := (h.securityPolicyEnforcer).(*securitypolicy.StandardSecurityPolicyEnforcer); ok {
if policyEnforcer, ok := (h.securityPolicyEnforcer).(*securitypolicy.StandardEnforcer); ok {
secPolicyEnv := fmt.Sprintf("SECURITY_POLICY=%s", policyEnforcer.EncodedSecurityPolicy)
settings.OCISpecification.Process.Env = append(settings.OCISpecification.Process.Env, secPolicyEnv)
}
Expand Down Expand Up @@ -440,7 +440,7 @@ func newInvalidRequestTypeError(rt prot.ModifyRequestType) error {
return errors.Errorf("the RequestType \"%s\" is not supported", rt)
}

func modifyMappedVirtualDisk(ctx context.Context, rt prot.ModifyRequestType, mvd *prot.MappedVirtualDiskV2, securityPolicy securitypolicy.SecurityPolicyEnforcer) (err error) {
func modifyMappedVirtualDisk(ctx context.Context, rt prot.ModifyRequestType, mvd *prot.MappedVirtualDiskV2, securityPolicy securitypolicy.PolicyEnforcer) (err error) {
switch rt {
case prot.MreqtAdd:
mountCtx, cancel := context.WithTimeout(ctx, time.Second*5)
Expand Down Expand Up @@ -472,7 +472,7 @@ func modifyMappedDirectory(ctx context.Context, vsock transport.Transport, rt pr
}
}

func modifyMappedVPMemDevice(ctx context.Context, rt prot.ModifyRequestType, vpd *prot.MappedVPMemDeviceV2, securityPolicy securitypolicy.SecurityPolicyEnforcer) (err error) {
func modifyMappedVPMemDevice(ctx context.Context, rt prot.ModifyRequestType, vpd *prot.MappedVPMemDeviceV2, securityPolicy securitypolicy.PolicyEnforcer) (err error) {
switch rt {
case prot.MreqtAdd:
return pmem.Mount(ctx, vpd.DeviceNumber, vpd.MountPath, vpd.MappingInfo, vpd.VerityInfo, securityPolicy)
Expand All @@ -492,7 +492,7 @@ func modifyMappedVPCIDevice(ctx context.Context, rt prot.ModifyRequestType, vpci
}
}

func modifyCombinedLayers(ctx context.Context, rt prot.ModifyRequestType, cl *prot.CombinedLayersV2, securityPolicy securitypolicy.SecurityPolicyEnforcer) (err error) {
func modifyCombinedLayers(ctx context.Context, rt prot.ModifyRequestType, cl *prot.CombinedLayersV2, securityPolicy securitypolicy.PolicyEnforcer) (err error) {
switch rt {
case prot.MreqtAdd:
layerPaths := make([]string, len(cl.Layers))
Expand Down
9 changes: 8 additions & 1 deletion internal/guest/storage/overlay/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ var (

// MountLayer first enforces the security policy for the container's layer paths
// and then calls Mount to mount the layer paths as an overlayfs
func MountLayer(ctx context.Context, layerPaths []string, upperdirPath, workdirPath, rootfsPath string, readonly bool, containerId string, securityPolicy securitypolicy.SecurityPolicyEnforcer) (err error) {
func MountLayer(
ctx context.Context,
layerPaths []string,
upperdirPath, workdirPath, rootfsPath string,
readonly bool,
containerId string,
securityPolicy securitypolicy.PolicyEnforcer,
) (err error) {
_, span := trace.StartSpan(ctx, "overlay::MountLayer")
defer span.End()
defer func() { oc.SetSpanStatus(span, err) }()
Expand Down
8 changes: 4 additions & 4 deletions internal/guest/storage/overlay/overlay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ func Test_Security_Policy_Enforcement(t *testing.T) {
}
}

func openDoorSecurityPolicyEnforcer() securitypolicy.SecurityPolicyEnforcer {
return &securitypolicy.OpenDoorSecurityPolicyEnforcer{}
func openDoorSecurityPolicyEnforcer() securitypolicy.PolicyEnforcer {
return &securitypolicy.OpenDoorEnforcer{}
}

func mountMonitoringSecurityPolicyEnforcer() *policy.MountMonitoringSecurityPolicyEnforcer {
return &policy.MountMonitoringSecurityPolicyEnforcer{}
func mountMonitoringSecurityPolicyEnforcer() *policy.MountMonitoringEnforcer {
return &policy.MountMonitoringEnforcer{}
}
4 changes: 2 additions & 2 deletions internal/guest/storage/pmem/pmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func mount(ctx context.Context, source, target string) (err error) {
//
// Note: both mappingInfo and verityInfo can be non-nil at the same time, in that case
// linear target is created first and it becomes the data/hash device for verity target.
func Mount(ctx context.Context, device uint32, target string, mappingInfo *prot.DeviceMappingInfo, verityInfo *prot.DeviceVerityInfo, securityPolicy securitypolicy.SecurityPolicyEnforcer) (err error) {
func Mount(ctx context.Context, device uint32, target string, mappingInfo *prot.DeviceMappingInfo, verityInfo *prot.DeviceVerityInfo, securityPolicy securitypolicy.PolicyEnforcer) (err error) {
mCtx, span := trace.StartSpan(ctx, "pmem::Mount")
defer span.End()
defer func() { oc.SetSpanStatus(span, err) }()
Expand Down Expand Up @@ -123,7 +123,7 @@ func Mount(ctx context.Context, device uint32, target string, mappingInfo *prot.
}

// Unmount unmounts `target` and removes corresponding linear and verity targets when needed
func Unmount(ctx context.Context, devNumber uint32, target string, mappingInfo *prot.DeviceMappingInfo, verityInfo *prot.DeviceVerityInfo, securityPolicy securitypolicy.SecurityPolicyEnforcer) (err error) {
func Unmount(ctx context.Context, devNumber uint32, target string, mappingInfo *prot.DeviceMappingInfo, verityInfo *prot.DeviceVerityInfo, securityPolicy securitypolicy.PolicyEnforcer) (err error) {
_, span := trace.StartSpan(ctx, "pmem::Unmount")
defer span.End()
defer func() { oc.SetSpanStatus(span, err) }()
Expand Down
8 changes: 4 additions & 4 deletions internal/guest/storage/pmem/pmem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,12 @@ func Test_Security_Policy_Enforcement_Unmount_Calls(t *testing.T) {
}
}

func openDoorSecurityPolicyEnforcer() securitypolicy.SecurityPolicyEnforcer {
return &securitypolicy.OpenDoorSecurityPolicyEnforcer{}
func openDoorSecurityPolicyEnforcer() securitypolicy.PolicyEnforcer {
return &securitypolicy.OpenDoorEnforcer{}
}

func mountMonitoringSecurityPolicyEnforcer() *policy.MountMonitoringSecurityPolicyEnforcer {
return &policy.MountMonitoringSecurityPolicyEnforcer{}
func mountMonitoringSecurityPolicyEnforcer() *policy.MountMonitoringEnforcer {
return &policy.MountMonitoringEnforcer{}
}

// device mapper tests
Expand Down
4 changes: 2 additions & 2 deletions internal/guest/storage/scsi/scsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const (
//
// If `encrypted` is set to true, the SCSI device will be encrypted using
// dm-crypt.
func Mount(ctx context.Context, controller, lun uint8, target string, readonly bool, encrypted bool, options []string, verityInfo *prot.DeviceVerityInfo, securityPolicy securitypolicy.SecurityPolicyEnforcer) (err error) {
func Mount(ctx context.Context, controller, lun uint8, target string, readonly bool, encrypted bool, options []string, verityInfo *prot.DeviceVerityInfo, securityPolicy securitypolicy.PolicyEnforcer) (err error) {
spnCtx, span := trace.StartSpan(ctx, "scsi::Mount")
defer span.End()
defer func() { oc.SetSpanStatus(span, err) }()
Expand Down Expand Up @@ -150,7 +150,7 @@ func Mount(ctx context.Context, controller, lun uint8, target string, readonly b
// Unmount unmounts a SCSI device mounted at `target`.
//
// If `encrypted` is true, it removes all its associated dm-crypto state.
func Unmount(ctx context.Context, controller, lun uint8, target string, encrypted bool, verityInfo *prot.DeviceVerityInfo, securityPolicy securitypolicy.SecurityPolicyEnforcer) (err error) {
func Unmount(ctx context.Context, controller, lun uint8, target string, encrypted bool, verityInfo *prot.DeviceVerityInfo, securityPolicy securitypolicy.PolicyEnforcer) (err error) {
ctx, span := trace.StartSpan(ctx, "scsi::Unmount")
defer span.End()
defer func() { oc.SetSpanStatus(span, err) }()
Expand Down
8 changes: 4 additions & 4 deletions internal/guest/storage/scsi/scsi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,12 +616,12 @@ func Test_Security_Policy_Enforcement_Unmount_Calls(t *testing.T) {
}
}

func openDoorSecurityPolicyEnforcer() securitypolicy.SecurityPolicyEnforcer {
return &securitypolicy.OpenDoorSecurityPolicyEnforcer{}
func openDoorSecurityPolicyEnforcer() securitypolicy.PolicyEnforcer {
return &securitypolicy.OpenDoorEnforcer{}
}

func mountMonitoringSecurityPolicyEnforcer() *policy.MountMonitoringSecurityPolicyEnforcer {
return &policy.MountMonitoringSecurityPolicyEnforcer{}
func mountMonitoringSecurityPolicyEnforcer() *policy.MountMonitoringEnforcer {
return &policy.MountMonitoringEnforcer{}
}

// dm-verity tests
Expand Down
34 changes: 34 additions & 0 deletions internal/guest/storage/test/policy/mountmonitorenforcer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package policy

import (
"github.com/Microsoft/hcsshim/pkg/securitypolicy"
)

// For testing. Records the number of calls to each method so we can verify
// the expected interactions took place.
type MountMonitoringEnforcer struct {
DeviceMountCalls int
DeviceUnmountCalls int
OverlayMountCalls int
}

var _ securitypolicy.PolicyEnforcer = (*MountMonitoringEnforcer)(nil)

func (p *MountMonitoringEnforcer) EnforceDeviceMountPolicy(_ string, _ string) (err error) {
p.DeviceMountCalls++
return nil
}

func (p *MountMonitoringEnforcer) EnforceDeviceUnmountPolicy(_ string) (err error) {
p.DeviceUnmountCalls++
return nil
}

func (p *MountMonitoringEnforcer) EnforceOverlayMountPolicy(_ string, _ []string) (err error) {
p.OverlayMountCalls++
return nil
}

func (p *MountMonitoringEnforcer) EnforceCreateContainerPolicy(_ string, _ []string, _ []string) (err error) {
return nil
}

This file was deleted.

10 changes: 5 additions & 5 deletions internal/tools/securitypolicy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func main() {
return err
}

policy, err := func() (securitypolicy.SecurityPolicy, error) {
policy, err := func() (securitypolicy.Policy, error) {
if config.AllowAll {
return createOpenDoorPolicy(), nil
} else {
Expand Down Expand Up @@ -99,14 +99,14 @@ type Config struct {
Containers []Container `toml:"container"`
}

func createOpenDoorPolicy() securitypolicy.SecurityPolicy {
return securitypolicy.SecurityPolicy{
func createOpenDoorPolicy() securitypolicy.Policy {
return securitypolicy.Policy{
AllowAll: true,
}
}

func createPolicyFromConfig(config Config) (securitypolicy.SecurityPolicy, error) {
p := securitypolicy.SecurityPolicy{
func createPolicyFromConfig(config Config) (securitypolicy.Policy, error) {
p := securitypolicy.Policy{
Containers: securitypolicy.Containers{
Elements: map[string]securitypolicy.Container{},
},
Expand Down
Loading