Skip to content

Commit f03c2fb

Browse files
authored
Remove the standard JSON enforcer and the JSON policy parsing in the Rego enforcer (#2539)
* securitypolicyenforcer: Remove the standard JSON enforcer This commit removes the long deprecated standard JSON enforcer - all confidential containers now has to either use Rego (the default), or the open_door enforcer if provided with an empty policy or a policy that is `{"allow_all": true}` (both case checked against host data). Note that the host can still choose either rego or open_door, and this is not measured into host data, but the policy check in createOpenDoorEnforcer ensures that if the policy is a rego policy, trying to use an open_door enforcer will error, leaving the enforcer at the default (which for confidential is a deny-everything ClosedDoorSecurityPolicyEnforcer). Signed-off-by: Tingmao Wang <[email protected]> * rego enforcer: Remove support for JSON policies using Rego enforcer This removes the ability to send in a standard JSON policy to the Rego enforcer. Previously it would translate it into the equivalent Rego policy (the format is different from the `containers := [...]` definition in Rego), but we need to stop supporting this. Signed-off-by: Tingmao Wang <[email protected]> * Do not allow {"allow_all": true} as a valid "open_door" policy. For the open door enforcer to be used, policy must be empty (i.e. the case in non-confidential containers). Signed-off-by: Tingmao Wang <[email protected]> * Remove the custom "JSON policy is not supported." error message Suggested-by: Ken Gordon <[email protected]> Signed-off-by: Tingmao Wang <[email protected]> * Remove unused function and add nolint where it's wrong Signed-off-by: Tingmao Wang <[email protected]> * Move substituteUVMPath to test specific go files This function is now only used by tests, and having it in non _test files causses unused function lints. Signed-off-by: Tingmao Wang <[email protected]> * Fix function name typo in comment Signed-off-by: Tingmao Wang <[email protected]> --------- Signed-off-by: Tingmao Wang <[email protected]>
1 parent bdc6744 commit f03c2fb

9 files changed

+70
-1780
lines changed

pkg/securitypolicy/rego_utils_test.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2218,40 +2218,6 @@ func (*generatedConstraints) Generate(r *rand.Rand, _ int) reflect.Value {
22182218
return reflect.ValueOf(c)
22192219
}
22202220

2221-
type testConfig struct {
2222-
container *securityPolicyContainer
2223-
layers []string
2224-
containerID string
2225-
policy *StandardSecurityPolicyEnforcer
2226-
}
2227-
2228-
func setupContainerWithOverlay(gc *generatedConstraints, valid bool) (tc *testConfig, err error) {
2229-
sp := NewStandardSecurityPolicyEnforcer(gc.containers, ignoredEncodedPolicyString)
2230-
2231-
containerID := testDataGenerator.uniqueContainerID()
2232-
c := selectContainerFromContainerList(gc.containers, testRand)
2233-
2234-
var layerPaths []string
2235-
if valid {
2236-
layerPaths, err = testDataGenerator.createValidOverlayForContainer(sp, c)
2237-
if err != nil {
2238-
return nil, fmt.Errorf("error creating valid overlay: %w", err)
2239-
}
2240-
} else {
2241-
layerPaths, err = testDataGenerator.createInvalidOverlayForContainer(sp, c)
2242-
if err != nil {
2243-
return nil, fmt.Errorf("error creating invalid overlay: %w", err)
2244-
}
2245-
}
2246-
2247-
return &testConfig{
2248-
container: c,
2249-
layers: layerPaths,
2250-
containerID: containerID,
2251-
policy: sp,
2252-
}, nil
2253-
}
2254-
22552221
func generateConstraints(r *rand.Rand, maxContainers int32) *generatedConstraints {
22562222
var containers []*securityPolicyContainer
22572223

pkg/securitypolicy/regopolicy_linux_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
"testing"
1818
"testing/quick"
1919

20+
specInternal "github.com/Microsoft/hcsshim/internal/guest/spec"
21+
"github.com/Microsoft/hcsshim/internal/guestpath"
2022
rpi "github.com/Microsoft/hcsshim/internal/regopolicyinterpreter"
2123
oci "github.com/opencontainers/runtime-spec/specs-go"
2224
)
@@ -6324,3 +6326,15 @@ func testGetUserInfo(t *testing.T, tc getUserInfoTestCase, userStr string, regoE
63246326
}
63256327
})
63266328
}
6329+
6330+
// substituteUVMPath substitutes mount prefix to an appropriate path inside
6331+
// UVM. At policy generation time, it's impossible to tell what the sandboxID
6332+
// will be, so the prefix substitution needs to happen during runtime.
6333+
func substituteUVMPath(sandboxID string, m mountInternal) mountInternal {
6334+
if strings.HasPrefix(m.Source, guestpath.SandboxMountPrefix) {
6335+
m.Source = specInternal.SandboxMountSource(sandboxID, m.Source)
6336+
} else if strings.HasPrefix(m.Source, guestpath.HugePagesMountPrefix) {
6337+
m.Source = specInternal.HugePagesMountSource(sandboxID, m.Source)
6338+
}
6339+
return m
6340+
}

pkg/securitypolicy/regopolicy_windows_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,3 +1329,13 @@ func Test_Rego_DumpStacksPolicy_Off(t *testing.T) {
13291329
t.Errorf("Test_Rego_DumpStacksPolicy_Off: %v", err)
13301330
}
13311331
}
1332+
1333+
// This is a no-op for windows.
1334+
// substituteUVMPath substitutes mount prefix to an appropriate path inside
1335+
// UVM. At policy generation time, it's impossible to tell what the sandboxID
1336+
// will be, so the prefix substitution needs to happen during runtime.
1337+
func substituteUVMPath(sandboxID string, m mountInternal) mountInternal {
1338+
//no-op for windows
1339+
_ = sandboxID
1340+
return m
1341+
}

pkg/securitypolicy/securitypolicy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var apiCodeTemplate string
2626
var APICode = strings.Replace(apiCodeTemplate, "@@API_VERSION@@", apiVersion, 1)
2727
var FrameworkCode = strings.Replace(frameworkCodeTemplate, "@@FRAMEWORK_VERSION@@", frameworkVersion, 1)
2828

29-
var ErrInvalidOpenDoorPolicy = errors.New("allow_all cannot be set to 'true' when Containers are non-empty")
29+
var ErrInvalidOpenDoorPolicy = errors.New("Invalid policy for open-door enforcer")
3030

3131
type EnvVarRule string
3232

pkg/securitypolicy/securitypolicy_linux.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ import (
88
"os"
99
"path/filepath"
1010
"strconv"
11-
"strings"
1211

1312
specInternal "github.com/Microsoft/hcsshim/internal/guest/spec"
14-
"github.com/Microsoft/hcsshim/internal/guestpath"
1513
"github.com/moby/sys/user"
1614
oci "github.com/opencontainers/runtime-spec/specs-go"
1715
"github.com/pkg/errors"
@@ -20,19 +18,6 @@ import (
2018
//nolint:unused
2119
const osType = "linux"
2220

23-
// This is being used by StandEnforcer.
24-
// substituteUVMPath substitutes mount prefix to an appropriate path inside
25-
// UVM. At policy generation time, it's impossible to tell what the sandboxID
26-
// will be, so the prefix substitution needs to happen during runtime.
27-
func substituteUVMPath(sandboxID string, m mountInternal) mountInternal {
28-
if strings.HasPrefix(m.Source, guestpath.SandboxMountPrefix) {
29-
m.Source = specInternal.SandboxMountSource(sandboxID, m.Source)
30-
} else if strings.HasPrefix(m.Source, guestpath.HugePagesMountPrefix) {
31-
m.Source = specInternal.HugePagesMountSource(sandboxID, m.Source)
32-
}
33-
return m
34-
}
35-
3621
// SandboxMountsDir returns sandbox mounts directory inside UVM/host.
3722
func SandboxMountsDir(sandboxID string) string {
3823
return specInternal.SandboxMountsDir((sandboxID))

pkg/securitypolicy/securitypolicy_windows.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,6 @@ import oci "github.com/opencontainers/runtime-spec/specs-go"
88
//nolint:unused
99
const osType = "windows"
1010

11-
// This is being used by StandEnforcer and is a no-op for windows.
12-
// substituteUVMPath substitutes mount prefix to an appropriate path inside
13-
// UVM. At policy generation time, it's impossible to tell what the sandboxID
14-
// will be, so the prefix substitution needs to happen during runtime.
15-
func substituteUVMPath(sandboxID string, m mountInternal) mountInternal {
16-
//no-op for windows
17-
_ = sandboxID
18-
return m
19-
}
20-
2111
// SandboxMountsDir returns sandbox mounts directory inside UVM/host.
2212
func SandboxMountsDir(sandboxID string) string {
2313
return ""

0 commit comments

Comments
 (0)