forked from ratify-project/ratify
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Policies interface to wrap operations on namespaced policies
- Loading branch information
Showing
8 changed files
with
221 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
Copyright The Ratify Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package policies | ||
|
||
import "github.com/deislabs/ratify/pkg/policyprovider" | ||
|
||
// PolicyManager is an interface that defines the methods for managing policies across different scopes. | ||
type PolicyManager interface { | ||
// GetPolicy returns the policy for the given scope. | ||
GetPolicy(scope string) policyprovider.PolicyProvider | ||
|
||
// AddPolicy adds the given policy under the given scope. | ||
AddPolicy(scope, policyName string, policy policyprovider.PolicyProvider) | ||
|
||
// DeletePolicy deletes the policy from the given scope. | ||
DeletePolicy(scope, policyName string) | ||
|
||
// IsEmpty returns true if there are no policies. | ||
IsEmpty() bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
Copyright The Ratify Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package policies | ||
|
||
import ( | ||
"github.com/deislabs/ratify/internal/constants" | ||
"github.com/deislabs/ratify/pkg/policyprovider" | ||
) | ||
|
||
// PolicyWrapper wraps policy provider with its policy name. | ||
type PolicyWrapper struct { | ||
Name string | ||
Policy policyprovider.PolicyProvider | ||
} | ||
|
||
// ActivePolicies implements PolicyManager interface. | ||
type ActivePolicies struct { | ||
// TODO: Implement concurrent safety using sync.Map | ||
// ScopedPolicies is a mapping from scope to a policy. | ||
// Note: Scope is utilized for organizing and isolating verifiers. In a Kubernetes (K8s) environment, the scope can be either a namespace or an empty string ("") for cluster-wide verifiers. | ||
ScopedPolicies map[string]PolicyWrapper | ||
} | ||
|
||
func NewActivePolicies() PolicyManager { | ||
return &ActivePolicies{ | ||
ScopedPolicies: make(map[string]PolicyWrapper), | ||
} | ||
} | ||
|
||
// GetPolicy implements the Policies interface. | ||
// It returns the policy for the given scope. If no policy is found for the given scope, it returns cluster-wide policy. | ||
// TODO: Current implementation always fetches the cluster-wide policy. Will implement the logic to fetch the policy for the given scope. | ||
func (p *ActivePolicies) GetPolicy(_ string) policyprovider.PolicyProvider { | ||
policy, ok := p.ScopedPolicies[constants.EmptyNamespace] | ||
if ok { | ||
return policy.Policy | ||
} | ||
return nil | ||
} | ||
|
||
func (p *ActivePolicies) AddPolicy(scope, policyName string, policy policyprovider.PolicyProvider) { | ||
p.ScopedPolicies[scope] = PolicyWrapper{ | ||
Name: policyName, | ||
Policy: policy, | ||
} | ||
} | ||
|
||
func (p *ActivePolicies) DeletePolicy(scope, policyName string) { | ||
if policy, ok := p.ScopedPolicies[scope]; ok { | ||
if policy.Name == policyName { | ||
delete(p.ScopedPolicies, scope) | ||
} | ||
} | ||
} | ||
|
||
func (p *ActivePolicies) IsEmpty() bool { | ||
return len(p.ScopedPolicies) == 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
Copyright The Ratify Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package policies | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/deislabs/ratify/internal/constants" | ||
"github.com/deislabs/ratify/pkg/common" | ||
"github.com/deislabs/ratify/pkg/executor/types" | ||
"github.com/deislabs/ratify/pkg/ocispecs" | ||
) | ||
|
||
type mockPolicy struct{} | ||
|
||
func (p mockPolicy) VerifyNeeded(_ context.Context, _ common.Reference, _ ocispecs.ReferenceDescriptor) bool { | ||
return true | ||
} | ||
|
||
func (p mockPolicy) ContinueVerifyOnFailure(_ context.Context, _ common.Reference, _ ocispecs.ReferenceDescriptor, _ types.VerifyResult) bool { | ||
return true | ||
} | ||
|
||
func (p mockPolicy) ErrorToVerifyResult(_ context.Context, _ string, _ error) types.VerifyResult { | ||
return types.VerifyResult{} | ||
} | ||
|
||
func (p mockPolicy) OverallVerifyResult(_ context.Context, _ []interface{}) bool { | ||
return true | ||
} | ||
|
||
func (p mockPolicy) GetPolicyType(_ context.Context) string { | ||
return "" | ||
} | ||
|
||
const ( | ||
namespace1 = constants.EmptyNamespace | ||
namespace2 = "namespace2" | ||
name1 = "name1" | ||
name2 = "name2" | ||
) | ||
|
||
var ( | ||
policy1 = mockPolicy{} | ||
policy2 = mockPolicy{} | ||
) | ||
|
||
func TestPoliciesOperations(t *testing.T) { | ||
policies := NewActivePolicies() | ||
|
||
if !policies.IsEmpty() { | ||
t.Errorf("Expected policies to be empty") | ||
} | ||
|
||
policies.AddPolicy(namespace1, name1, policy1) | ||
policies.AddPolicy(namespace2, name1, policy2) | ||
|
||
if policies.IsEmpty() { | ||
t.Errorf("Expected policies to not be empty") | ||
} | ||
|
||
if policies.GetPolicy(namespace1) != policy1 { | ||
t.Errorf("Expected policy1 to be returned") | ||
} | ||
|
||
if policies.GetPolicy(namespace2) != policy2 { | ||
t.Errorf("Expected policy2 to be returned") | ||
} | ||
|
||
policies.DeletePolicy(namespace2, name1) | ||
|
||
if policies.GetPolicy(namespace2) != policy1 { | ||
t.Errorf("Expected policy1 to be returned") | ||
} | ||
|
||
policies.DeletePolicy(namespace1, name1) | ||
|
||
if policies.GetPolicy(namespace1) != nil { | ||
t.Errorf("Expected no policy to be returned") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters