Skip to content

Commit e221502

Browse files
more tests
1 parent 074a75c commit e221502

File tree

7 files changed

+81
-18
lines changed

7 files changed

+81
-18
lines changed

audit/agent_log.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ func NewLogAgent(l logging.Logger) Agent {
3333
}
3434

3535
func (a *logAgent) Trace(ctx context.Context, auditData Audit) {
36-
data := a.cache.Load("")
36+
data := a.cache.Load()
37+
3738
auditData.generateID()
3839
if data != nil {
3940
auditData.applyDataFromPolicy(data)

audit/cache.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ package audit
1717
type Data map[string]interface{}
1818

1919
type AuditCache interface {
20-
Store(id string, d Data)
21-
Load(id string) Data
20+
Store(d Data)
21+
Load() Data
2222
}
2323

2424
type SingleRecordCache struct {
2525
data Data
2626
}
2727

28-
func (c *SingleRecordCache) Store(id string, d Data) {
28+
func (c *SingleRecordCache) Store(d Data) {
2929
c.data = d
3030
}
3131

32-
func (c *SingleRecordCache) Load(id string) Data {
32+
func (c *SingleRecordCache) Load() Data {
3333
return c.data
3434
}

audit/cache_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2024 Mia srl
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package audit
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/require"
21+
)
22+
23+
func TestSingleRecordCache(t *testing.T) {
24+
t.Run("store + load", func(t *testing.T) {
25+
c := SingleRecordCache{}
26+
storedData := Data{"key": "val"}
27+
28+
c.Store(storedData)
29+
loadData := c.Load()
30+
31+
require.Equal(t, storedData, loadData)
32+
})
33+
}

audit/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func WithAuditCache(ctx context.Context, agent Agent) context.Context {
2828
func GetAuditCache(ctx context.Context) (AuditCache, error) {
2929
auditCache, ok := ctx.Value(cacheKey{}).(AuditCache)
3030
if !ok {
31-
return nil, fmt.Errorf("todo")
31+
return nil, fmt.Errorf("failed to extract audit cache from context")
3232
}
3333
return auditCache, nil
3434
}

audit/context_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2024 Mia srl
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package audit
16+
17+
import (
18+
"context"
19+
"testing"
20+
21+
"github.com/stretchr/testify/require"
22+
)
23+
24+
func TestContext(t *testing.T) {
25+
t.Run("add to context and get", func(t *testing.T) {
26+
ctx := context.Background()
27+
28+
agent := &noopAgent{}
29+
30+
retrievedAgent, err := GetAuditCache(WithAuditCache(ctx, agent))
31+
32+
require.NoError(t, err)
33+
require.NotNil(t, retrievedAgent)
34+
})
35+
}

audit/rego_builtin.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
package audit
1616

1717
import (
18-
"fmt"
19-
2018
"github.com/open-policy-agent/opa/ast"
2119
"github.com/open-policy-agent/opa/rego"
2220
"github.com/open-policy-agent/opa/types"
@@ -26,34 +24,30 @@ var SetLabelsDecl = &ast.Builtin{
2624
Name: "set_audit_labels",
2725
Decl: types.NewFunction(
2826
types.Args(
29-
types.S, // id
3027
types.A, // payload
3128
),
3229
types.A,
3330
),
3431
}
3532

36-
var SetLabels = rego.Function2(
33+
var SetLabels = rego.Function1(
3734
&rego.Function{
3835
Name: SetLabelsDecl.Name,
3936
Decl: SetLabelsDecl.Decl,
4037
},
41-
func(bctx rego.BuiltinContext, op1, op2 *ast.Term) (*ast.Term, error) {
42-
fmt.Println("set_audit_labels_invoked")
43-
38+
func(bctx rego.BuiltinContext, dataToStore *ast.Term) (*ast.Term, error) {
4439
auditCache, err := GetAuditCache(bctx.Context)
4540
if err != nil {
4641
// TODO we should at least log a warning, consider failing the execution?
4742
return nil, nil
4843
}
44+
4945
data := make(map[string]interface{})
50-
if err := ast.As(op2.Value, &data); err != nil {
46+
if err := ast.As(dataToStore.Value, &data); err != nil {
5147
return nil, err
5248
}
53-
fmt.Printf("ASDUTI CACHE %+v %+v\n", auditCache, data)
54-
auditCache.Store("", data)
55-
fmt.Printf("STORED\n")
5649

50+
auditCache.Store(data)
5751
return ast.BooleanTerm(true), nil
5852
},
5953
)

mocks/rego-policies/example.rego

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ assert_user {
9393
allow_policy_with_audit_data {
9494
print("HERE WE ARE")
9595
z := {"a": 42}
96-
set_audit_labels("z", z)
96+
set_audit_labels(z)
9797
true
9898
}
9999
projection_policy_with_audit_data[res] {

0 commit comments

Comments
 (0)