Skip to content
Snippets Groups Projects
Commit 5dcd5e84 authored by Yordan Kinkov's avatar Yordan Kinkov
Browse files

Add cache TTL attribute in export configuration

parent 1a6cf17d
Branches
Tags
No related merge requests found
......@@ -8,11 +8,15 @@ import (
"io"
"net/http"
"net/url"
"strconv"
"gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/golib/errors"
)
const headerEvaluationID = "x-evaluation-id"
const (
headerEvaluationID = "x-evaluation-id"
headerCacheTTL = "x-cache-ttl"
)
type Client struct {
addr string
......@@ -36,7 +40,7 @@ func New(addr string, opts ...ClientOption) *Client {
// The policy is expected as a string path uniquely identifying the
// policy that has to be evaluated. For example, with policy = `gaiax/didResolve/1.0`,
// the client will do HTTP request to http://policyhost/policy/gaiax/didResolve/1.0/evaluation.
func (c *Client) Evaluate(ctx context.Context, policy string, data interface{}, evaluationID string) ([]byte, error) {
func (c *Client) Evaluate(ctx context.Context, policy string, data interface{}, evaluationID string, ttl *int) ([]byte, error) {
uri := c.addr + "/policy/" + policy + "/evaluation"
policyURL, err := url.ParseRequestURI(uri)
if err != nil {
......@@ -57,6 +61,10 @@ func (c *Client) Evaluate(ctx context.Context, policy string, data interface{},
req.Header.Set(headerEvaluationID, evaluationID)
}
if ttl != nil {
req.Header.Set(headerCacheTTL, strconv.Itoa(*ttl))
}
resp, err := c.httpClient.Do(req.WithContext(ctx))
if err != nil {
return nil, err
......
......@@ -4,8 +4,8 @@ package infohubfakes
import (
"sync"
"gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/infohub/internal/service/infohub"
"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable"
"gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/infohub/internal/service/infohub"
)
type FakeCredentials struct {
......
......@@ -9,13 +9,14 @@ import (
)
type FakePolicy struct {
EvaluateStub func(context.Context, string, interface{}, string) ([]byte, error)
EvaluateStub func(context.Context, string, interface{}, string, *int) ([]byte, error)
evaluateMutex sync.RWMutex
evaluateArgsForCall []struct {
arg1 context.Context
arg2 string
arg3 interface{}
arg4 string
arg5 *int
}
evaluateReturns struct {
result1 []byte
......@@ -29,7 +30,7 @@ type FakePolicy struct {
invocationsMutex sync.RWMutex
}
func (fake *FakePolicy) Evaluate(arg1 context.Context, arg2 string, arg3 interface{}, arg4 string) ([]byte, error) {
func (fake *FakePolicy) Evaluate(arg1 context.Context, arg2 string, arg3 interface{}, arg4 string, arg5 *int) ([]byte, error) {
fake.evaluateMutex.Lock()
ret, specificReturn := fake.evaluateReturnsOnCall[len(fake.evaluateArgsForCall)]
fake.evaluateArgsForCall = append(fake.evaluateArgsForCall, struct {
......@@ -37,13 +38,14 @@ func (fake *FakePolicy) Evaluate(arg1 context.Context, arg2 string, arg3 interfa
arg2 string
arg3 interface{}
arg4 string
}{arg1, arg2, arg3, arg4})
arg5 *int
}{arg1, arg2, arg3, arg4, arg5})
stub := fake.EvaluateStub
fakeReturns := fake.evaluateReturns
fake.recordInvocation("Evaluate", []interface{}{arg1, arg2, arg3, arg4})
fake.recordInvocation("Evaluate", []interface{}{arg1, arg2, arg3, arg4, arg5})
fake.evaluateMutex.Unlock()
if stub != nil {
return stub(arg1, arg2, arg3, arg4)
return stub(arg1, arg2, arg3, arg4, arg5)
}
if specificReturn {
return ret.result1, ret.result2
......@@ -57,17 +59,17 @@ func (fake *FakePolicy) EvaluateCallCount() int {
return len(fake.evaluateArgsForCall)
}
func (fake *FakePolicy) EvaluateCalls(stub func(context.Context, string, interface{}, string) ([]byte, error)) {
func (fake *FakePolicy) EvaluateCalls(stub func(context.Context, string, interface{}, string, *int) ([]byte, error)) {
fake.evaluateMutex.Lock()
defer fake.evaluateMutex.Unlock()
fake.EvaluateStub = stub
}
func (fake *FakePolicy) EvaluateArgsForCall(i int) (context.Context, string, interface{}, string) {
func (fake *FakePolicy) EvaluateArgsForCall(i int) (context.Context, string, interface{}, string, *int) {
fake.evaluateMutex.RLock()
defer fake.evaluateMutex.RUnlock()
argsForCall := fake.evaluateArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4, argsForCall.arg5
}
func (fake *FakePolicy) EvaluateReturns(result1 []byte, result2 error) {
......
......@@ -5,8 +5,8 @@ import (
"context"
"sync"
"gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/infohub/internal/service/infohub"
"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable"
"gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/infohub/internal/service/infohub"
)
type FakeSigner struct {
......
......@@ -26,7 +26,7 @@ type Storage interface {
}
type Policy interface {
Evaluate(ctx context.Context, policy string, data interface{}, evaluationID string) ([]byte, error)
Evaluate(ctx context.Context, policy string, data interface{}, evaluationID string, ttl *int) ([]byte, error)
}
type Cache interface {
......@@ -207,7 +207,7 @@ func (s *Service) triggerExport(ctx context.Context, exportCfg *storage.ExportCo
s.logger.Info("export triggered", zap.String("exportName", exportCfg.ExportName))
for policy, input := range exportCfg.Policies {
cacheKey := exportCacheKey(exportCfg.ExportName, policy)
_, err := s.policy.Evaluate(ctx, policy, input, cacheKey)
_, err := s.policy.Evaluate(ctx, policy, input, cacheKey, exportCfg.TTL)
if err != nil {
return err
}
......
......@@ -9,6 +9,7 @@ import (
"go.uber.org/zap"
"gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/golib/errors"
"gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/golib/ptr"
goasigner "gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/infohub/gen/infohub"
"gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/infohub/internal/service/infohub"
"gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/infohub/internal/service/infohub/infohubfakes"
......@@ -85,6 +86,7 @@ func TestService_Export(t *testing.T) {
ExportName: "testexport",
Contexts: []string{"https://www.w3.org/2018/credentials/examples/v1"},
Policies: map[string]interface{}{"test/test/1.0": map[string]interface{}{"hello": "test world"}},
TTL: ptr.Int(60),
}, nil
},
},
......@@ -94,7 +96,7 @@ func TestService_Export(t *testing.T) {
},
},
policy: &infohubfakes.FakePolicy{
EvaluateStub: func(ctx context.Context, policy string, input interface{}, cachekey string) ([]byte, error) {
EvaluateStub: func(ctx context.Context, policy string, input interface{}, cachekey string, ttl *int) ([]byte, error) {
return nil, errors.New("error evaluation policy")
},
},
......@@ -119,7 +121,32 @@ func TestService_Export(t *testing.T) {
},
},
policy: &infohubfakes.FakePolicy{
EvaluateStub: func(ctx context.Context, policy string, input interface{}, cachekey string) ([]byte, error) {
EvaluateStub: func(ctx context.Context, policy string, input interface{}, cachekey string, ttl *int) ([]byte, error) {
return []byte(`{"allow":"true"}`), nil
},
},
res: map[string]interface{}{"result": "export request is accepted"},
},
{
name: "export triggering successfully with TTL provided in export configuration",
req: &goasigner.ExportRequest{ExportName: "testexport"},
storage: &infohubfakes.FakeStorage{
ExportConfigurationStub: func(ctx context.Context, s string) (*storage.ExportConfiguration, error) {
return &storage.ExportConfiguration{
ExportName: "testexport",
Contexts: []string{"https://www.w3.org/2018/credentials/examples/v1"},
Policies: map[string]interface{}{"test/test/1.0": map[string]interface{}{"hello": "test world"}},
TTL: ptr.Int(60),
}, nil
},
},
cache: &infohubfakes.FakeCache{
GetStub: func(ctx context.Context, key string, namespace string, scope string) ([]byte, error) {
return nil, errors.New(errors.NotFound, "no data")
},
},
policy: &infohubfakes.FakePolicy{
EvaluateStub: func(ctx context.Context, policy string, input interface{}, cachekey string, ttl *int) ([]byte, error) {
return []byte(`{"allow":"true"}`), nil
},
},
......
......@@ -15,6 +15,7 @@ type ExportConfiguration struct {
ExportName string
Contexts []string
Policies map[string]interface{}
TTL *int
}
type Storage struct {
......
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment