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

Added rule policy #166

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions pkg/apis/softwarecomposition/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ type ApplicationProfileContainer struct {
Endpoints []HTTPEndpoint
ImageID string
ImageTag string
PolicyByRuleId map[string]RulePolicy
}

type RulePolicy struct {
AllowedProcesses []string
AllowedContainer bool
}

type ExecCalls struct {
Expand Down
69 changes: 69 additions & 0 deletions pkg/apis/softwarecomposition/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,72 @@ func TestHTTPEndpoint_String(t *testing.T) {
})
}
}

func TestApplicationProfileContainer_PolicyValidation(t *testing.T) {
tests := []struct {
name string
container ApplicationProfileContainer
wantPolicy RulePolicy
policyRuleID string
wantExists bool
}{
{
name: "Empty container",
container: ApplicationProfileContainer{
PolicyByRuleId: map[string]RulePolicy{},
},
policyRuleID: "rule1",
wantExists: false,
},
{
name: "Container with policies",
container: ApplicationProfileContainer{
Name: "nginx",
Capabilities: []string{"NET_BIND_SERVICE", "CHOWN"},
ImageID: "sha256:abc123",
ImageTag: "1.21-alpine",
PolicyByRuleId: map[string]RulePolicy{
"rule1": {
AllowedProcesses: []string{"nginx", "sh"},
AllowedContainer: true,
},
"rule2": {
AllowedProcesses: []string{},
AllowedContainer: false,
},
},
},
policyRuleID: "rule1",
wantPolicy: RulePolicy{
AllowedProcesses: []string{"nginx", "sh"},
AllowedContainer: true,
},
wantExists: true,
},
{
name: "Non-existent rule",
container: ApplicationProfileContainer{
Name: "nginx",
PolicyByRuleId: map[string]RulePolicy{
"rule1": {
AllowedProcesses: []string{"nginx"},
AllowedContainer: true,
},
},
},
policyRuleID: "rule2",
wantExists: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
policy, exists := tt.container.PolicyByRuleId[tt.policyRuleID]
assert.Equal(t, tt.wantExists, exists, "policy existence")

if tt.wantExists {
assert.Equal(t, tt.wantPolicy, policy, "policy content")
}
})
}
}
Loading
Loading