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

Merge branch '1-add-export-configuration-attribute-for-cache-ttl' into 'main'

Add cache TTL attribute in export configuration

Closes #1

See merge request gaia-x/data-infrastructure-federation-services/tsa/infohub!1
parents 1a6cf17d 1a8ea0db
Branches
Tags
No related merge requests found
Pipeline #54325 failed
...@@ -8,11 +8,15 @@ import ( ...@@ -8,11 +8,15 @@ import (
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
"strconv"
"gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/golib/errors" "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 { type Client struct {
addr string addr string
...@@ -36,7 +40,7 @@ func New(addr string, opts ...ClientOption) *Client { ...@@ -36,7 +40,7 @@ func New(addr string, opts ...ClientOption) *Client {
// The policy is expected as a string path uniquely identifying the // 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`, // 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. // 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, cacheTTL *int) ([]byte, error) {
uri := c.addr + "/policy/" + policy + "/evaluation" uri := c.addr + "/policy/" + policy + "/evaluation"
policyURL, err := url.ParseRequestURI(uri) policyURL, err := url.ParseRequestURI(uri)
if err != nil { if err != nil {
...@@ -57,6 +61,10 @@ func (c *Client) Evaluate(ctx context.Context, policy string, data interface{}, ...@@ -57,6 +61,10 @@ func (c *Client) Evaluate(ctx context.Context, policy string, data interface{},
req.Header.Set(headerEvaluationID, evaluationID) req.Header.Set(headerEvaluationID, evaluationID)
} }
if cacheTTL != nil {
req.Header.Set(headerCacheTTL, strconv.Itoa(*cacheTTL))
}
resp, err := c.httpClient.Do(req.WithContext(ctx)) resp, err := c.httpClient.Do(req.WithContext(ctx))
if err != nil { if err != nil {
return nil, err return nil, err
......
...@@ -4,8 +4,8 @@ package infohubfakes ...@@ -4,8 +4,8 @@ package infohubfakes
import ( import (
"sync" "sync"
"gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/infohub/internal/service/infohub"
"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable" "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 { type FakeCredentials struct {
......
...@@ -9,13 +9,14 @@ import ( ...@@ -9,13 +9,14 @@ import (
) )
type FakePolicy struct { 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 evaluateMutex sync.RWMutex
evaluateArgsForCall []struct { evaluateArgsForCall []struct {
arg1 context.Context arg1 context.Context
arg2 string arg2 string
arg3 interface{} arg3 interface{}
arg4 string arg4 string
arg5 *int
} }
evaluateReturns struct { evaluateReturns struct {
result1 []byte result1 []byte
...@@ -29,7 +30,7 @@ type FakePolicy struct { ...@@ -29,7 +30,7 @@ type FakePolicy struct {
invocationsMutex sync.RWMutex 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() fake.evaluateMutex.Lock()
ret, specificReturn := fake.evaluateReturnsOnCall[len(fake.evaluateArgsForCall)] ret, specificReturn := fake.evaluateReturnsOnCall[len(fake.evaluateArgsForCall)]
fake.evaluateArgsForCall = append(fake.evaluateArgsForCall, struct { fake.evaluateArgsForCall = append(fake.evaluateArgsForCall, struct {
...@@ -37,13 +38,14 @@ func (fake *FakePolicy) Evaluate(arg1 context.Context, arg2 string, arg3 interfa ...@@ -37,13 +38,14 @@ func (fake *FakePolicy) Evaluate(arg1 context.Context, arg2 string, arg3 interfa
arg2 string arg2 string
arg3 interface{} arg3 interface{}
arg4 string arg4 string
}{arg1, arg2, arg3, arg4}) arg5 *int
}{arg1, arg2, arg3, arg4, arg5})
stub := fake.EvaluateStub stub := fake.EvaluateStub
fakeReturns := fake.evaluateReturns fakeReturns := fake.evaluateReturns
fake.recordInvocation("Evaluate", []interface{}{arg1, arg2, arg3, arg4}) fake.recordInvocation("Evaluate", []interface{}{arg1, arg2, arg3, arg4, arg5})
fake.evaluateMutex.Unlock() fake.evaluateMutex.Unlock()
if stub != nil { if stub != nil {
return stub(arg1, arg2, arg3, arg4) return stub(arg1, arg2, arg3, arg4, arg5)
} }
if specificReturn { if specificReturn {
return ret.result1, ret.result2 return ret.result1, ret.result2
...@@ -57,17 +59,17 @@ func (fake *FakePolicy) EvaluateCallCount() int { ...@@ -57,17 +59,17 @@ func (fake *FakePolicy) EvaluateCallCount() int {
return len(fake.evaluateArgsForCall) 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() fake.evaluateMutex.Lock()
defer fake.evaluateMutex.Unlock() defer fake.evaluateMutex.Unlock()
fake.EvaluateStub = stub 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() fake.evaluateMutex.RLock()
defer fake.evaluateMutex.RUnlock() defer fake.evaluateMutex.RUnlock()
argsForCall := fake.evaluateArgsForCall[i] 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) { func (fake *FakePolicy) EvaluateReturns(result1 []byte, result2 error) {
......
...@@ -5,8 +5,8 @@ import ( ...@@ -5,8 +5,8 @@ import (
"context" "context"
"sync" "sync"
"gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/infohub/internal/service/infohub"
"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable" "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 { type FakeSigner struct {
......
...@@ -3,7 +3,6 @@ package infohub ...@@ -3,7 +3,6 @@ package infohub
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable" "github.com/hyperledger/aries-framework-go/pkg/doc/verifiable"
"go.uber.org/zap" "go.uber.org/zap"
...@@ -26,7 +25,7 @@ type Storage interface { ...@@ -26,7 +25,7 @@ type Storage interface {
} }
type Policy 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 { type Cache interface {
...@@ -207,7 +206,7 @@ func (s *Service) triggerExport(ctx context.Context, exportCfg *storage.ExportCo ...@@ -207,7 +206,7 @@ func (s *Service) triggerExport(ctx context.Context, exportCfg *storage.ExportCo
s.logger.Info("export triggered", zap.String("exportName", exportCfg.ExportName)) s.logger.Info("export triggered", zap.String("exportName", exportCfg.ExportName))
for policy, input := range exportCfg.Policies { for policy, input := range exportCfg.Policies {
cacheKey := exportCacheKey(exportCfg.ExportName, policy) cacheKey := exportCacheKey(exportCfg.ExportName, policy)
_, err := s.policy.Evaluate(ctx, policy, input, cacheKey) _, err := s.policy.Evaluate(ctx, policy, input, cacheKey, exportCfg.CacheTTL)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"go.uber.org/zap" "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/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" 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"
"gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/infohub/internal/service/infohub/infohubfakes" "gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/infohub/internal/service/infohub/infohubfakes"
...@@ -85,6 +86,7 @@ func TestService_Export(t *testing.T) { ...@@ -85,6 +86,7 @@ func TestService_Export(t *testing.T) {
ExportName: "testexport", ExportName: "testexport",
Contexts: []string{"https://www.w3.org/2018/credentials/examples/v1"}, Contexts: []string{"https://www.w3.org/2018/credentials/examples/v1"},
Policies: map[string]interface{}{"test/test/1.0": map[string]interface{}{"hello": "test world"}}, Policies: map[string]interface{}{"test/test/1.0": map[string]interface{}{"hello": "test world"}},
CacheTTL: ptr.Int(60),
}, nil }, nil
}, },
}, },
...@@ -94,7 +96,7 @@ func TestService_Export(t *testing.T) { ...@@ -94,7 +96,7 @@ func TestService_Export(t *testing.T) {
}, },
}, },
policy: &infohubfakes.FakePolicy{ 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") return nil, errors.New("error evaluation policy")
}, },
}, },
...@@ -119,7 +121,32 @@ func TestService_Export(t *testing.T) { ...@@ -119,7 +121,32 @@ func TestService_Export(t *testing.T) {
}, },
}, },
policy: &infohubfakes.FakePolicy{ 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"}},
CacheTTL: 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 return []byte(`{"allow":"true"}`), nil
}, },
}, },
......
...@@ -15,6 +15,7 @@ type ExportConfiguration struct { ...@@ -15,6 +15,7 @@ type ExportConfiguration struct {
ExportName string ExportName string
Contexts []string Contexts []string
Policies map[string]interface{} Policies map[string]interface{}
CacheTTL *int
} }
type Storage struct { 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