diff --git a/design/design.go b/design/design.go index 16a698890a20ddf0466f2f8c1175e5ddcdcd6d0e..ab4a588b968db49bc06f167adf45f447df6868f9 100644 --- a/design/design.go +++ b/design/design.go @@ -29,6 +29,9 @@ var _ = Service("policy", func() { Header("evaluationID:x-evaluation-id", String, "EvaluationID allows overwriting the randomly generated evaluationID", func() { Example("did:web:example.com") }) + Header("ttl:x-cache-ttl", Int, "Policy result cache TTL in seconds", func() { + Example(60) + }) Body("input") Response(StatusOK, func() { Body("result") diff --git a/design/types.go b/design/types.go index e301f2853833ccdc5e3cb2492ca05cc42e87c8d0..33542dfcf8e0900821cc4909eda4f17610be8df9 100644 --- a/design/types.go +++ b/design/types.go @@ -15,6 +15,7 @@ var EvaluateRequest = Type("EvaluateRequest", func() { }) Field(4, "input", Any, "Input data passed to the policy execution runtime.") Field(5, "evaluationID", String, "Identifier created by external system and passed as parameter to overwrite the randomly generated evaluationID.") + Field(6, "ttl", Int, "TTL for storing policy result in cache") Required("group", "policyName", "version") }) diff --git a/gen/http/cli/policy/cli.go b/gen/http/cli/policy/cli.go index 1cf7727a658f322ecd60c117563b31ce5db4d0d6..ca43ff33441aa387383df8030004a07068cf8d99 100644 --- a/gen/http/cli/policy/cli.go +++ b/gen/http/cli/policy/cli.go @@ -32,7 +32,7 @@ policy (evaluate|lock|unlock) // UsageExamples produces an example of a valid invocation of the CLI tool. func UsageExamples() string { return os.Args[0] + ` health liveness` + "\n" + - os.Args[0] + ` policy evaluate --body "Id odio aperiam voluptatem molestias corrupti sunt." --group "example" --policy-name "example" --version "1.0" --evaluation-id "Et voluptates."` + "\n" + + os.Args[0] + ` policy evaluate --body "Ipsum nihil quo." --group "example" --policy-name "example" --version "1.0" --evaluation-id "Aut id odio." --ttl 1804438149079407927` + "\n" + "" } @@ -60,6 +60,7 @@ func ParseEndpoint( policyEvaluatePolicyNameFlag = policyEvaluateFlags.String("policy-name", "REQUIRED", "Policy name.") policyEvaluateVersionFlag = policyEvaluateFlags.String("version", "REQUIRED", "Policy version.") policyEvaluateEvaluationIDFlag = policyEvaluateFlags.String("evaluation-id", "", "") + policyEvaluateTTLFlag = policyEvaluateFlags.String("ttl", "", "") policyLockFlags = flag.NewFlagSet("lock", flag.ExitOnError) policyLockGroupFlag = policyLockFlags.String("group", "REQUIRED", "Policy group.") @@ -172,7 +173,7 @@ func ParseEndpoint( switch epn { case "evaluate": endpoint = c.Evaluate() - data, err = policyc.BuildEvaluatePayload(*policyEvaluateBodyFlag, *policyEvaluateGroupFlag, *policyEvaluatePolicyNameFlag, *policyEvaluateVersionFlag, *policyEvaluateEvaluationIDFlag) + data, err = policyc.BuildEvaluatePayload(*policyEvaluateBodyFlag, *policyEvaluateGroupFlag, *policyEvaluatePolicyNameFlag, *policyEvaluateVersionFlag, *policyEvaluateEvaluationIDFlag, *policyEvaluateTTLFlag) case "lock": endpoint = c.Lock() data, err = policyc.BuildLockPayload(*policyLockGroupFlag, *policyLockPolicyNameFlag, *policyLockVersionFlag) @@ -239,7 +240,7 @@ Additional help: `, os.Args[0]) } func policyEvaluateUsage() { - fmt.Fprintf(os.Stderr, `%[1]s [flags] policy evaluate -body JSON -group STRING -policy-name STRING -version STRING -evaluation-id STRING + fmt.Fprintf(os.Stderr, `%[1]s [flags] policy evaluate -body JSON -group STRING -policy-name STRING -version STRING -evaluation-id STRING -ttl INT Evaluate executes a policy with the given 'data' as input. -body JSON: @@ -247,9 +248,10 @@ Evaluate executes a policy with the given 'data' as input. -policy-name STRING: Policy name. -version STRING: Policy version. -evaluation-id STRING: + -ttl INT: Example: - %[1]s policy evaluate --body "Id odio aperiam voluptatem molestias corrupti sunt." --group "example" --policy-name "example" --version "1.0" --evaluation-id "Et voluptates." + %[1]s policy evaluate --body "Ipsum nihil quo." --group "example" --policy-name "example" --version "1.0" --evaluation-id "Aut id odio." --ttl 1804438149079407927 `, os.Args[0]) } @@ -262,7 +264,7 @@ Lock a policy so that it cannot be evaluated. -version STRING: Policy version. Example: - %[1]s policy lock --group "Explicabo beatae quisquam officiis libero voluptatibus." --policy-name "Repudiandae dolore quod." --version "Aut ut fuga quae eius minus." + %[1]s policy lock --group "Repudiandae dolore quod." --policy-name "Aut ut fuga quae eius minus." --version "Architecto quibusdam ab." `, os.Args[0]) } @@ -275,6 +277,6 @@ Unlock a policy so it can be evaluated again. -version STRING: Policy version. Example: - %[1]s policy unlock --group "Incidunt unde consequatur voluptas dolorem nisi temporibus." --policy-name "Omnis quasi aut consequuntur." --version "Tempore minus." + %[1]s policy unlock --group "Omnis quasi aut consequuntur." --policy-name "Tempore minus." --version "Quis quos qui earum velit illum." `, os.Args[0]) } diff --git a/gen/http/openapi.json b/gen/http/openapi.json index f5208fb3310f5356b476c007a8dc07dd8b62a932..f8333f00c2c6c2f647f7289e9b3f493d7cc5deab 100644 --- a/gen/http/openapi.json +++ b/gen/http/openapi.json @@ -1 +1 @@ -{"swagger":"2.0","info":{"title":"Policy Service","description":"The policy service exposes HTTP API for executing policies.","version":""},"host":"localhost:8081","consumes":["application/json","application/xml","application/gob"],"produces":["application/json","application/xml","application/gob"],"paths":{"/liveness":{"get":{"tags":["health"],"summary":"Liveness health","operationId":"health#Liveness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/policy/{group}/{policyName}/{version}/evaluation":{"get":{"tags":["policy"],"summary":"Evaluate policy","description":"Evaluate executes a policy with the given 'data' as input.","operationId":"policy#Evaluate#1","parameters":[{"name":"group","in":"path","description":"Policy group.","required":true,"type":"string"},{"name":"policyName","in":"path","description":"Policy name.","required":true,"type":"string"},{"name":"version","in":"path","description":"Policy version.","required":true,"type":"string"},{"name":"x-evaluation-id","in":"header","description":"EvaluationID allows overwriting the randomly generated evaluationID","required":false,"type":"string"},{"name":"any","in":"body","description":"Input data passed to the policy execution runtime.","required":true,"schema":{"type":"string","format":"binary"}}],"responses":{"200":{"description":"OK response.","schema":{"type":"string","format":"binary"},"headers":{"ETag":{"description":"ETag contains unique identifier of the policy evaluation and can be used to later retrieve the results from Cache.","type":"string"}}}},"schemes":["http"]},"post":{"tags":["policy"],"summary":"Evaluate policy","description":"Evaluate executes a policy with the given 'data' as input.","operationId":"policy#Evaluate#2","parameters":[{"name":"group","in":"path","description":"Policy group.","required":true,"type":"string"},{"name":"policyName","in":"path","description":"Policy name.","required":true,"type":"string"},{"name":"version","in":"path","description":"Policy version.","required":true,"type":"string"},{"name":"x-evaluation-id","in":"header","description":"EvaluationID allows overwriting the randomly generated evaluationID","required":false,"type":"string"},{"name":"any","in":"body","description":"Input data passed to the policy execution runtime.","required":true,"schema":{"type":"string","format":"binary"}}],"responses":{"200":{"description":"OK response.","schema":{"type":"string","format":"binary"},"headers":{"ETag":{"description":"ETag contains unique identifier of the policy evaluation and can be used to later retrieve the results from Cache.","type":"string"}}}},"schemes":["http"]}},"/policy/{group}/{policyName}/{version}/evaluation/did.json":{"get":{"tags":["policy"],"summary":"Evaluate policy","description":"Evaluate executes a policy with the given 'data' as input.","operationId":"policy#Evaluate","parameters":[{"name":"group","in":"path","description":"Policy group.","required":true,"type":"string"},{"name":"policyName","in":"path","description":"Policy name.","required":true,"type":"string"},{"name":"version","in":"path","description":"Policy version.","required":true,"type":"string"},{"name":"x-evaluation-id","in":"header","description":"EvaluationID allows overwriting the randomly generated evaluationID","required":false,"type":"string"},{"name":"any","in":"body","description":"Input data passed to the policy execution runtime.","required":true,"schema":{"type":"string","format":"binary"}}],"responses":{"200":{"description":"OK response.","schema":{"type":"string","format":"binary"},"headers":{"ETag":{"description":"ETag contains unique identifier of the policy evaluation and can be used to later retrieve the results from Cache.","type":"string"}}}},"schemes":["http"]}},"/policy/{group}/{policyName}/{version}/lock":{"post":{"tags":["policy"],"summary":"Lock policy","description":"Lock a policy so that it cannot be evaluated.","operationId":"policy#Lock","parameters":[{"name":"group","in":"path","description":"Policy group.","required":true,"type":"string"},{"name":"policyName","in":"path","description":"Policy name.","required":true,"type":"string"},{"name":"version","in":"path","description":"Policy version.","required":true,"type":"string"}],"responses":{"200":{"description":"OK response."}},"schemes":["http"]},"delete":{"tags":["policy"],"summary":"Unlock policy","description":"Unlock a policy so it can be evaluated again.","operationId":"policy#Unlock","parameters":[{"name":"group","in":"path","description":"Policy group.","required":true,"type":"string"},{"name":"policyName","in":"path","description":"Policy name.","required":true,"type":"string"},{"name":"version","in":"path","description":"Policy version.","required":true,"type":"string"}],"responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/readiness":{"get":{"tags":["health"],"summary":"Readiness health","operationId":"health#Readiness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}}}} \ No newline at end of file +{"swagger":"2.0","info":{"title":"Policy Service","description":"The policy service exposes HTTP API for executing policies.","version":""},"host":"localhost:8081","consumes":["application/json","application/xml","application/gob"],"produces":["application/json","application/xml","application/gob"],"paths":{"/liveness":{"get":{"tags":["health"],"summary":"Liveness health","operationId":"health#Liveness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/policy/{group}/{policyName}/{version}/evaluation":{"get":{"tags":["policy"],"summary":"Evaluate policy","description":"Evaluate executes a policy with the given 'data' as input.","operationId":"policy#Evaluate#1","parameters":[{"name":"group","in":"path","description":"Policy group.","required":true,"type":"string"},{"name":"policyName","in":"path","description":"Policy name.","required":true,"type":"string"},{"name":"version","in":"path","description":"Policy version.","required":true,"type":"string"},{"name":"x-evaluation-id","in":"header","description":"EvaluationID allows overwriting the randomly generated evaluationID","required":false,"type":"string"},{"name":"x-cache-ttl","in":"header","description":"Policy result cache TTL in seconds","required":false,"type":"integer"},{"name":"any","in":"body","description":"Input data passed to the policy execution runtime.","required":true,"schema":{"type":"string","format":"binary"}}],"responses":{"200":{"description":"OK response.","schema":{"type":"string","format":"binary"},"headers":{"ETag":{"description":"ETag contains unique identifier of the policy evaluation and can be used to later retrieve the results from Cache.","type":"string"}}}},"schemes":["http"]},"post":{"tags":["policy"],"summary":"Evaluate policy","description":"Evaluate executes a policy with the given 'data' as input.","operationId":"policy#Evaluate#2","parameters":[{"name":"group","in":"path","description":"Policy group.","required":true,"type":"string"},{"name":"policyName","in":"path","description":"Policy name.","required":true,"type":"string"},{"name":"version","in":"path","description":"Policy version.","required":true,"type":"string"},{"name":"x-evaluation-id","in":"header","description":"EvaluationID allows overwriting the randomly generated evaluationID","required":false,"type":"string"},{"name":"x-cache-ttl","in":"header","description":"Policy result cache TTL in seconds","required":false,"type":"integer"},{"name":"any","in":"body","description":"Input data passed to the policy execution runtime.","required":true,"schema":{"type":"string","format":"binary"}}],"responses":{"200":{"description":"OK response.","schema":{"type":"string","format":"binary"},"headers":{"ETag":{"description":"ETag contains unique identifier of the policy evaluation and can be used to later retrieve the results from Cache.","type":"string"}}}},"schemes":["http"]}},"/policy/{group}/{policyName}/{version}/evaluation/did.json":{"get":{"tags":["policy"],"summary":"Evaluate policy","description":"Evaluate executes a policy with the given 'data' as input.","operationId":"policy#Evaluate","parameters":[{"name":"group","in":"path","description":"Policy group.","required":true,"type":"string"},{"name":"policyName","in":"path","description":"Policy name.","required":true,"type":"string"},{"name":"version","in":"path","description":"Policy version.","required":true,"type":"string"},{"name":"x-evaluation-id","in":"header","description":"EvaluationID allows overwriting the randomly generated evaluationID","required":false,"type":"string"},{"name":"x-cache-ttl","in":"header","description":"Policy result cache TTL in seconds","required":false,"type":"integer"},{"name":"any","in":"body","description":"Input data passed to the policy execution runtime.","required":true,"schema":{"type":"string","format":"binary"}}],"responses":{"200":{"description":"OK response.","schema":{"type":"string","format":"binary"},"headers":{"ETag":{"description":"ETag contains unique identifier of the policy evaluation and can be used to later retrieve the results from Cache.","type":"string"}}}},"schemes":["http"]}},"/policy/{group}/{policyName}/{version}/lock":{"post":{"tags":["policy"],"summary":"Lock policy","description":"Lock a policy so that it cannot be evaluated.","operationId":"policy#Lock","parameters":[{"name":"group","in":"path","description":"Policy group.","required":true,"type":"string"},{"name":"policyName","in":"path","description":"Policy name.","required":true,"type":"string"},{"name":"version","in":"path","description":"Policy version.","required":true,"type":"string"}],"responses":{"200":{"description":"OK response."}},"schemes":["http"]},"delete":{"tags":["policy"],"summary":"Unlock policy","description":"Unlock a policy so it can be evaluated again.","operationId":"policy#Unlock","parameters":[{"name":"group","in":"path","description":"Policy group.","required":true,"type":"string"},{"name":"policyName","in":"path","description":"Policy name.","required":true,"type":"string"},{"name":"version","in":"path","description":"Policy version.","required":true,"type":"string"}],"responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/readiness":{"get":{"tags":["health"],"summary":"Readiness health","operationId":"health#Readiness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}}}} \ No newline at end of file diff --git a/gen/http/openapi.yaml b/gen/http/openapi.yaml index 5612466db0f0d1cf35d0813c5d858e3e6d7e7638..ec5b9852e71d46f3a82a3bc0e7ec7f80f3035d83 100644 --- a/gen/http/openapi.yaml +++ b/gen/http/openapi.yaml @@ -52,6 +52,11 @@ paths: description: EvaluationID allows overwriting the randomly generated evaluationID required: false type: string + - name: x-cache-ttl + in: header + description: Policy result cache TTL in seconds + required: false + type: integer - name: any in: body description: Input data passed to the policy execution runtime. @@ -98,6 +103,11 @@ paths: description: EvaluationID allows overwriting the randomly generated evaluationID required: false type: string + - name: x-cache-ttl + in: header + description: Policy result cache TTL in seconds + required: false + type: integer - name: any in: body description: Input data passed to the policy execution runtime. @@ -145,6 +155,11 @@ paths: description: EvaluationID allows overwriting the randomly generated evaluationID required: false type: string + - name: x-cache-ttl + in: header + description: Policy result cache TTL in seconds + required: false + type: integer - name: any in: body description: Input data passed to the policy execution runtime. diff --git a/gen/http/openapi3.json b/gen/http/openapi3.json index 33bdcd88503d722ccc0c4b633a87d37dac82eef1..6f1ce7b8aaf5d3efd3a3a3ce5b094d42b500bf1a 100644 --- a/gen/http/openapi3.json +++ b/gen/http/openapi3.json @@ -1 +1 @@ -{"openapi":"3.0.3","info":{"title":"Policy Service","description":"The policy service exposes HTTP API for executing policies.","version":"1.0"},"servers":[{"url":"http://localhost:8081","description":"Policy Server"}],"paths":{"/liveness":{"get":{"tags":["health"],"summary":"Liveness health","operationId":"health#Liveness","responses":{"200":{"description":"OK response."}}}},"/policy/{group}/{policyName}/{version}/evaluation":{"get":{"tags":["policy"],"summary":"Evaluate policy","description":"Evaluate executes a policy with the given 'data' as input.","operationId":"policy#Evaluate#1","parameters":[{"name":"group","in":"path","description":"Policy group.","required":true,"schema":{"type":"string","description":"Policy group.","example":"example"},"example":"example"},{"name":"policyName","in":"path","description":"Policy name.","required":true,"schema":{"type":"string","description":"Policy name.","example":"example"},"example":"example"},{"name":"version","in":"path","description":"Policy version.","required":true,"schema":{"type":"string","description":"Policy version.","example":"1.0"},"example":"1.0"},{"name":"x-evaluation-id","in":"header","description":"EvaluationID allows overwriting the randomly generated evaluationID","allowEmptyValue":true,"schema":{"type":"string","description":"EvaluationID allows overwriting the randomly generated evaluationID","example":"did:web:example.com"},"example":"did:web:example.com"}],"requestBody":{"description":"Input data passed to the policy execution runtime.","required":true,"content":{"application/json":{"schema":{"type":"string","description":"Input data passed to the policy execution runtime.","example":"Quis quos qui earum velit illum.","format":"binary"},"example":"Aut voluptas."}}},"responses":{"200":{"description":"OK response.","headers":{"ETag":{"description":"ETag contains unique identifier of the policy evaluation and can be used to later retrieve the results from Cache.","required":true,"schema":{"type":"string","description":"ETag contains unique identifier of the policy evaluation and can be used to later retrieve the results from Cache.","example":"Sint nam voluptatem ea consequatur similique et."},"example":"Ut commodi perspiciatis corporis."}},"content":{"application/json":{"schema":{"type":"string","description":"Arbitrary JSON response.","example":"Aliquam atque voluptatum ut dolorem.","format":"binary"},"example":"Accusamus autem sequi."}}}}},"post":{"tags":["policy"],"summary":"Evaluate policy","description":"Evaluate executes a policy with the given 'data' as input.","operationId":"policy#Evaluate#2","parameters":[{"name":"group","in":"path","description":"Policy group.","required":true,"schema":{"type":"string","description":"Policy group.","example":"example"},"example":"example"},{"name":"policyName","in":"path","description":"Policy name.","required":true,"schema":{"type":"string","description":"Policy name.","example":"example"},"example":"example"},{"name":"version","in":"path","description":"Policy version.","required":true,"schema":{"type":"string","description":"Policy version.","example":"1.0"},"example":"1.0"},{"name":"x-evaluation-id","in":"header","description":"EvaluationID allows overwriting the randomly generated evaluationID","allowEmptyValue":true,"schema":{"type":"string","description":"EvaluationID allows overwriting the randomly generated evaluationID","example":"did:web:example.com"},"example":"did:web:example.com"}],"requestBody":{"description":"Input data passed to the policy execution runtime.","required":true,"content":{"application/json":{"schema":{"type":"string","description":"Input data passed to the policy execution runtime.","example":"Quis quos qui earum velit illum.","format":"binary"},"example":"Et nulla."}}},"responses":{"200":{"description":"OK response.","headers":{"ETag":{"description":"ETag contains unique identifier of the policy evaluation and can be used to later retrieve the results from Cache.","required":true,"schema":{"type":"string","description":"ETag contains unique identifier of the policy evaluation and can be used to later retrieve the results from Cache.","example":"In quis nesciunt autem et."},"example":"Commodi nemo fugiat id praesentium accusantium expedita."}},"content":{"application/json":{"schema":{"type":"string","description":"Arbitrary JSON response.","example":"Aliquam atque voluptatum ut dolorem.","format":"binary"},"example":"Qui non quia."}}}}}},"/policy/{group}/{policyName}/{version}/evaluation/did.json":{"get":{"tags":["policy"],"summary":"Evaluate policy","description":"Evaluate executes a policy with the given 'data' as input.","operationId":"policy#Evaluate","parameters":[{"name":"group","in":"path","description":"Policy group.","required":true,"schema":{"type":"string","description":"Policy group.","example":"example"},"example":"example"},{"name":"policyName","in":"path","description":"Policy name.","required":true,"schema":{"type":"string","description":"Policy name.","example":"example"},"example":"example"},{"name":"version","in":"path","description":"Policy version.","required":true,"schema":{"type":"string","description":"Policy version.","example":"1.0"},"example":"1.0"},{"name":"x-evaluation-id","in":"header","description":"EvaluationID allows overwriting the randomly generated evaluationID","allowEmptyValue":true,"schema":{"type":"string","description":"EvaluationID allows overwriting the randomly generated evaluationID","example":"did:web:example.com"},"example":"did:web:example.com"}],"requestBody":{"description":"Input data passed to the policy execution runtime.","required":true,"content":{"application/json":{"schema":{"type":"string","description":"Input data passed to the policy execution runtime.","example":"Quis quos qui earum velit illum.","format":"binary"},"example":"Aut facere veniam repudiandae id."}}},"responses":{"200":{"description":"OK response.","headers":{"ETag":{"description":"ETag contains unique identifier of the policy evaluation and can be used to later retrieve the results from Cache.","required":true,"schema":{"type":"string","description":"ETag contains unique identifier of the policy evaluation and can be used to later retrieve the results from Cache.","example":"Aut minus alias."},"example":"Ab accusantium ut ut aliquid sint animi."}},"content":{"application/json":{"schema":{"type":"string","description":"Arbitrary JSON response.","example":"Aliquam atque voluptatum ut dolorem.","format":"binary"},"example":"Dolorem cumque laborum quis nesciunt."}}}}}},"/policy/{group}/{policyName}/{version}/lock":{"delete":{"tags":["policy"],"summary":"Unlock policy","description":"Unlock a policy so it can be evaluated again.","operationId":"policy#Unlock","parameters":[{"name":"group","in":"path","description":"Policy group.","required":true,"schema":{"type":"string","description":"Policy group.","example":"Quam dolores architecto itaque."},"example":"Voluptas ad corporis adipisci inventore ipsum."},{"name":"policyName","in":"path","description":"Policy name.","required":true,"schema":{"type":"string","description":"Policy name.","example":"Recusandae dolorum nisi distinctio vitae ad."},"example":"Perspiciatis voluptatem."},{"name":"version","in":"path","description":"Policy version.","required":true,"schema":{"type":"string","description":"Policy version.","example":"Corporis est rem."},"example":"Totam officia necessitatibus tempore nulla animi."}],"responses":{"200":{"description":"OK response."}}},"post":{"tags":["policy"],"summary":"Lock policy","description":"Lock a policy so that it cannot be evaluated.","operationId":"policy#Lock","parameters":[{"name":"group","in":"path","description":"Policy group.","required":true,"schema":{"type":"string","description":"Policy group.","example":"Error maxime quasi quia non voluptatibus error."},"example":"Optio quia et laborum."},{"name":"policyName","in":"path","description":"Policy name.","required":true,"schema":{"type":"string","description":"Policy name.","example":"In libero perspiciatis voluptatum ut soluta."},"example":"Ut amet."},{"name":"version","in":"path","description":"Policy version.","required":true,"schema":{"type":"string","description":"Policy version.","example":"Accusamus enim."},"example":"Recusandae est rerum corrupti quia."}],"responses":{"200":{"description":"OK response."}}}},"/readiness":{"get":{"tags":["health"],"summary":"Readiness health","operationId":"health#Readiness","responses":{"200":{"description":"OK response."}}}}},"components":{},"tags":[{"name":"health","description":"Health service provides health check endpoints."},{"name":"policy","description":"Policy Service provides evaluation of policies through Open Policy Agent."}]} \ No newline at end of file +{"openapi":"3.0.3","info":{"title":"Policy Service","description":"The policy service exposes HTTP API for executing policies.","version":"1.0"},"servers":[{"url":"http://localhost:8081","description":"Policy Server"}],"paths":{"/liveness":{"get":{"tags":["health"],"summary":"Liveness health","operationId":"health#Liveness","responses":{"200":{"description":"OK response."}}}},"/policy/{group}/{policyName}/{version}/evaluation":{"get":{"tags":["policy"],"summary":"Evaluate policy","description":"Evaluate executes a policy with the given 'data' as input.","operationId":"policy#Evaluate#1","parameters":[{"name":"group","in":"path","description":"Policy group.","required":true,"schema":{"type":"string","description":"Policy group.","example":"example"},"example":"example"},{"name":"policyName","in":"path","description":"Policy name.","required":true,"schema":{"type":"string","description":"Policy name.","example":"example"},"example":"example"},{"name":"version","in":"path","description":"Policy version.","required":true,"schema":{"type":"string","description":"Policy version.","example":"1.0"},"example":"1.0"},{"name":"x-evaluation-id","in":"header","description":"EvaluationID allows overwriting the randomly generated evaluationID","allowEmptyValue":true,"schema":{"type":"string","description":"EvaluationID allows overwriting the randomly generated evaluationID","example":"did:web:example.com"},"example":"did:web:example.com"},{"name":"x-cache-ttl","in":"header","description":"Policy result cache TTL in seconds","allowEmptyValue":true,"schema":{"type":"integer","description":"Policy result cache TTL in seconds","example":60,"format":"int64"},"example":60}],"requestBody":{"description":"Input data passed to the policy execution runtime.","required":true,"content":{"application/json":{"schema":{"type":"string","description":"Input data passed to the policy execution runtime.","example":"Aliquam atque voluptatum ut dolorem.","format":"binary"},"example":"Sint nam voluptatem ea consequatur similique et."}}},"responses":{"200":{"description":"OK response.","headers":{"ETag":{"description":"ETag contains unique identifier of the policy evaluation and can be used to later retrieve the results from Cache.","required":true,"schema":{"type":"string","description":"ETag contains unique identifier of the policy evaluation and can be used to later retrieve the results from Cache.","example":"Non mollitia nesciunt impedit facere."},"example":"Accusamus autem sequi."}},"content":{"application/json":{"schema":{"type":"string","description":"Arbitrary JSON response.","example":"Aut facere veniam repudiandae id.","format":"binary"},"example":"Et nulla."}}}}},"post":{"tags":["policy"],"summary":"Evaluate policy","description":"Evaluate executes a policy with the given 'data' as input.","operationId":"policy#Evaluate#2","parameters":[{"name":"group","in":"path","description":"Policy group.","required":true,"schema":{"type":"string","description":"Policy group.","example":"example"},"example":"example"},{"name":"policyName","in":"path","description":"Policy name.","required":true,"schema":{"type":"string","description":"Policy name.","example":"example"},"example":"example"},{"name":"version","in":"path","description":"Policy version.","required":true,"schema":{"type":"string","description":"Policy version.","example":"1.0"},"example":"1.0"},{"name":"x-evaluation-id","in":"header","description":"EvaluationID allows overwriting the randomly generated evaluationID","allowEmptyValue":true,"schema":{"type":"string","description":"EvaluationID allows overwriting the randomly generated evaluationID","example":"did:web:example.com"},"example":"did:web:example.com"},{"name":"x-cache-ttl","in":"header","description":"Policy result cache TTL in seconds","allowEmptyValue":true,"schema":{"type":"integer","description":"Policy result cache TTL in seconds","example":60,"format":"int64"},"example":60}],"requestBody":{"description":"Input data passed to the policy execution runtime.","required":true,"content":{"application/json":{"schema":{"type":"string","description":"Input data passed to the policy execution runtime.","example":"Aliquam atque voluptatum ut dolorem.","format":"binary"},"example":"In quis nesciunt autem et."}}},"responses":{"200":{"description":"OK response.","headers":{"ETag":{"description":"ETag contains unique identifier of the policy evaluation and can be used to later retrieve the results from Cache.","required":true,"schema":{"type":"string","description":"ETag contains unique identifier of the policy evaluation and can be used to later retrieve the results from Cache.","example":"Sunt in et quia cum."},"example":"Qui non quia."}},"content":{"application/json":{"schema":{"type":"string","description":"Arbitrary JSON response.","example":"Aut facere veniam repudiandae id.","format":"binary"},"example":"Error maxime quasi quia non voluptatibus error."}}}}}},"/policy/{group}/{policyName}/{version}/evaluation/did.json":{"get":{"tags":["policy"],"summary":"Evaluate policy","description":"Evaluate executes a policy with the given 'data' as input.","operationId":"policy#Evaluate","parameters":[{"name":"group","in":"path","description":"Policy group.","required":true,"schema":{"type":"string","description":"Policy group.","example":"example"},"example":"example"},{"name":"policyName","in":"path","description":"Policy name.","required":true,"schema":{"type":"string","description":"Policy name.","example":"example"},"example":"example"},{"name":"version","in":"path","description":"Policy version.","required":true,"schema":{"type":"string","description":"Policy version.","example":"1.0"},"example":"1.0"},{"name":"x-evaluation-id","in":"header","description":"EvaluationID allows overwriting the randomly generated evaluationID","allowEmptyValue":true,"schema":{"type":"string","description":"EvaluationID allows overwriting the randomly generated evaluationID","example":"did:web:example.com"},"example":"did:web:example.com"},{"name":"x-cache-ttl","in":"header","description":"Policy result cache TTL in seconds","allowEmptyValue":true,"schema":{"type":"integer","description":"Policy result cache TTL in seconds","example":60,"format":"int64"},"example":60}],"requestBody":{"description":"Input data passed to the policy execution runtime.","required":true,"content":{"application/json":{"schema":{"type":"string","description":"Input data passed to the policy execution runtime.","example":"Aliquam atque voluptatum ut dolorem.","format":"binary"},"example":"Aut minus alias."}}},"responses":{"200":{"description":"OK response.","headers":{"ETag":{"description":"ETag contains unique identifier of the policy evaluation and can be used to later retrieve the results from Cache.","required":true,"schema":{"type":"string","description":"ETag contains unique identifier of the policy evaluation and can be used to later retrieve the results from Cache.","example":"At eos facilis molestias in voluptas rem."},"example":"Dolorem cumque laborum quis nesciunt."}},"content":{"application/json":{"schema":{"type":"string","description":"Arbitrary JSON response.","example":"Aut facere veniam repudiandae id.","format":"binary"},"example":"Aut voluptas."}}}}}},"/policy/{group}/{policyName}/{version}/lock":{"delete":{"tags":["policy"],"summary":"Unlock policy","description":"Unlock a policy so it can be evaluated again.","operationId":"policy#Unlock","parameters":[{"name":"group","in":"path","description":"Policy group.","required":true,"schema":{"type":"string","description":"Policy group.","example":"Voluptas ad corporis adipisci inventore ipsum."},"example":"Recusandae dolorum nisi distinctio vitae ad."},{"name":"policyName","in":"path","description":"Policy name.","required":true,"schema":{"type":"string","description":"Policy name.","example":"Perspiciatis voluptatem."},"example":"Corporis est rem."},{"name":"version","in":"path","description":"Policy version.","required":true,"schema":{"type":"string","description":"Policy version.","example":"Totam officia necessitatibus tempore nulla animi."},"example":"Consequatur vel rerum rem ipsam nam."}],"responses":{"200":{"description":"OK response."}}},"post":{"tags":["policy"],"summary":"Lock policy","description":"Lock a policy so that it cannot be evaluated.","operationId":"policy#Lock","parameters":[{"name":"group","in":"path","description":"Policy group.","required":true,"schema":{"type":"string","description":"Policy group.","example":"Optio quia et laborum."},"example":"In libero perspiciatis voluptatum ut soluta."},{"name":"policyName","in":"path","description":"Policy name.","required":true,"schema":{"type":"string","description":"Policy name.","example":"Ut amet."},"example":"Accusamus enim."},{"name":"version","in":"path","description":"Policy version.","required":true,"schema":{"type":"string","description":"Policy version.","example":"Recusandae est rerum corrupti quia."},"example":"Quam dolores architecto itaque."}],"responses":{"200":{"description":"OK response."}}}},"/readiness":{"get":{"tags":["health"],"summary":"Readiness health","operationId":"health#Readiness","responses":{"200":{"description":"OK response."}}}}},"components":{},"tags":[{"name":"health","description":"Health service provides health check endpoints."},{"name":"policy","description":"Policy Service provides evaluation of policies through Open Policy Agent."}]} \ No newline at end of file diff --git a/gen/http/openapi3.yaml b/gen/http/openapi3.yaml index 089b16b6203f3378d26823d1a40bdd2ae32c3bd6..30ff1a7abf17dd9d04faba301bbb7c1befba35f3 100644 --- a/gen/http/openapi3.yaml +++ b/gen/http/openapi3.yaml @@ -60,6 +60,16 @@ paths: description: EvaluationID allows overwriting the randomly generated evaluationID example: did:web:example.com example: did:web:example.com + - name: x-cache-ttl + in: header + description: Policy result cache TTL in seconds + allowEmptyValue: true + schema: + type: integer + description: Policy result cache TTL in seconds + example: 60 + format: int64 + example: 60 requestBody: description: Input data passed to the policy execution runtime. required: true @@ -68,9 +78,9 @@ paths: schema: type: string description: Input data passed to the policy execution runtime. - example: Quis quos qui earum velit illum. + example: Aliquam atque voluptatum ut dolorem. format: binary - example: Aut voluptas. + example: Sint nam voluptatem ea consequatur similique et. responses: "200": description: OK response. @@ -81,16 +91,16 @@ paths: schema: type: string description: ETag contains unique identifier of the policy evaluation and can be used to later retrieve the results from Cache. - example: Sint nam voluptatem ea consequatur similique et. - example: Ut commodi perspiciatis corporis. + example: Non mollitia nesciunt impedit facere. + example: Accusamus autem sequi. content: application/json: schema: type: string description: Arbitrary JSON response. - example: Aliquam atque voluptatum ut dolorem. + example: Aut facere veniam repudiandae id. format: binary - example: Accusamus autem sequi. + example: Et nulla. post: tags: - policy @@ -134,6 +144,16 @@ paths: description: EvaluationID allows overwriting the randomly generated evaluationID example: did:web:example.com example: did:web:example.com + - name: x-cache-ttl + in: header + description: Policy result cache TTL in seconds + allowEmptyValue: true + schema: + type: integer + description: Policy result cache TTL in seconds + example: 60 + format: int64 + example: 60 requestBody: description: Input data passed to the policy execution runtime. required: true @@ -142,9 +162,9 @@ paths: schema: type: string description: Input data passed to the policy execution runtime. - example: Quis quos qui earum velit illum. + example: Aliquam atque voluptatum ut dolorem. format: binary - example: Et nulla. + example: In quis nesciunt autem et. responses: "200": description: OK response. @@ -155,16 +175,16 @@ paths: schema: type: string description: ETag contains unique identifier of the policy evaluation and can be used to later retrieve the results from Cache. - example: In quis nesciunt autem et. - example: Commodi nemo fugiat id praesentium accusantium expedita. + example: Sunt in et quia cum. + example: Qui non quia. content: application/json: schema: type: string description: Arbitrary JSON response. - example: Aliquam atque voluptatum ut dolorem. + example: Aut facere veniam repudiandae id. format: binary - example: Qui non quia. + example: Error maxime quasi quia non voluptatibus error. /policy/{group}/{policyName}/{version}/evaluation/did.json: get: tags: @@ -209,6 +229,16 @@ paths: description: EvaluationID allows overwriting the randomly generated evaluationID example: did:web:example.com example: did:web:example.com + - name: x-cache-ttl + in: header + description: Policy result cache TTL in seconds + allowEmptyValue: true + schema: + type: integer + description: Policy result cache TTL in seconds + example: 60 + format: int64 + example: 60 requestBody: description: Input data passed to the policy execution runtime. required: true @@ -217,9 +247,9 @@ paths: schema: type: string description: Input data passed to the policy execution runtime. - example: Quis quos qui earum velit illum. + example: Aliquam atque voluptatum ut dolorem. format: binary - example: Aut facere veniam repudiandae id. + example: Aut minus alias. responses: "200": description: OK response. @@ -230,16 +260,16 @@ paths: schema: type: string description: ETag contains unique identifier of the policy evaluation and can be used to later retrieve the results from Cache. - example: Aut minus alias. - example: Ab accusantium ut ut aliquid sint animi. + example: At eos facilis molestias in voluptas rem. + example: Dolorem cumque laborum quis nesciunt. content: application/json: schema: type: string description: Arbitrary JSON response. - example: Aliquam atque voluptatum ut dolorem. + example: Aut facere veniam repudiandae id. format: binary - example: Dolorem cumque laborum quis nesciunt. + example: Aut voluptas. /policy/{group}/{policyName}/{version}/lock: delete: tags: @@ -255,8 +285,8 @@ paths: schema: type: string description: Policy group. - example: Quam dolores architecto itaque. - example: Voluptas ad corporis adipisci inventore ipsum. + example: Voluptas ad corporis adipisci inventore ipsum. + example: Recusandae dolorum nisi distinctio vitae ad. - name: policyName in: path description: Policy name. @@ -264,8 +294,8 @@ paths: schema: type: string description: Policy name. - example: Recusandae dolorum nisi distinctio vitae ad. - example: Perspiciatis voluptatem. + example: Perspiciatis voluptatem. + example: Corporis est rem. - name: version in: path description: Policy version. @@ -273,8 +303,8 @@ paths: schema: type: string description: Policy version. - example: Corporis est rem. - example: Totam officia necessitatibus tempore nulla animi. + example: Totam officia necessitatibus tempore nulla animi. + example: Consequatur vel rerum rem ipsam nam. responses: "200": description: OK response. @@ -292,8 +322,8 @@ paths: schema: type: string description: Policy group. - example: Error maxime quasi quia non voluptatibus error. - example: Optio quia et laborum. + example: Optio quia et laborum. + example: In libero perspiciatis voluptatum ut soluta. - name: policyName in: path description: Policy name. @@ -301,8 +331,8 @@ paths: schema: type: string description: Policy name. - example: In libero perspiciatis voluptatum ut soluta. - example: Ut amet. + example: Ut amet. + example: Accusamus enim. - name: version in: path description: Policy version. @@ -310,8 +340,8 @@ paths: schema: type: string description: Policy version. - example: Accusamus enim. - example: Recusandae est rerum corrupti quia. + example: Recusandae est rerum corrupti quia. + example: Quam dolores architecto itaque. responses: "200": description: OK response. diff --git a/gen/http/policy/client/cli.go b/gen/http/policy/client/cli.go index 4b914733cb77ee0b7a005c02a4b3cef462f4fd8c..c588b40dbbdc1984c1853727ce90f6b9ef85ee28 100644 --- a/gen/http/policy/client/cli.go +++ b/gen/http/policy/client/cli.go @@ -11,19 +11,20 @@ package client import ( "encoding/json" "fmt" + "strconv" policy "gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/policy/gen/policy" ) // BuildEvaluatePayload builds the payload for the policy Evaluate endpoint // from CLI flags. -func BuildEvaluatePayload(policyEvaluateBody string, policyEvaluateGroup string, policyEvaluatePolicyName string, policyEvaluateVersion string, policyEvaluateEvaluationID string) (*policy.EvaluateRequest, error) { +func BuildEvaluatePayload(policyEvaluateBody string, policyEvaluateGroup string, policyEvaluatePolicyName string, policyEvaluateVersion string, policyEvaluateEvaluationID string, policyEvaluateTTL string) (*policy.EvaluateRequest, error) { var err error var body interface{} { err = json.Unmarshal([]byte(policyEvaluateBody), &body) if err != nil { - return nil, fmt.Errorf("invalid JSON for body, \nerror: %s, \nexample of valid JSON:\n%s", err, "\"Id odio aperiam voluptatem molestias corrupti sunt.\"") + return nil, fmt.Errorf("invalid JSON for body, \nerror: %s, \nexample of valid JSON:\n%s", err, "\"Ipsum nihil quo.\"") } } var group string @@ -44,6 +45,18 @@ func BuildEvaluatePayload(policyEvaluateBody string, policyEvaluateGroup string, evaluationID = &policyEvaluateEvaluationID } } + var ttl *int + { + if policyEvaluateTTL != "" { + var v int64 + v, err = strconv.ParseInt(policyEvaluateTTL, 10, strconv.IntSize) + val := int(v) + ttl = &val + if err != nil { + return nil, fmt.Errorf("invalid value for ttl, must be INT") + } + } + } v := body res := &policy.EvaluateRequest{ Input: &v, @@ -52,6 +65,7 @@ func BuildEvaluatePayload(policyEvaluateBody string, policyEvaluateGroup string, res.PolicyName = policyName res.Version = version res.EvaluationID = evaluationID + res.TTL = ttl return res, nil } diff --git a/gen/http/policy/client/encode_decode.go b/gen/http/policy/client/encode_decode.go index fb9dc7c9693e9a7cea4f2d54fefeb8318debced5..e47b120eea0fc50f0120c5493779371bb6047c15 100644 --- a/gen/http/policy/client/encode_decode.go +++ b/gen/http/policy/client/encode_decode.go @@ -14,6 +14,7 @@ import ( "io" "net/http" "net/url" + "strconv" policy "gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/policy/gen/policy" goahttp "goa.design/goa/v3/http" @@ -61,6 +62,11 @@ func EncodeEvaluateRequest(encoder func(*http.Request) goahttp.Encoder) func(*ht head := *p.EvaluationID req.Header.Set("x-evaluation-id", head) } + if p.TTL != nil { + head := *p.TTL + headStr := strconv.Itoa(head) + req.Header.Set("x-cache-ttl", headStr) + } body := p.Input if err := encoder(req).Encode(&body); err != nil { return goahttp.ErrEncodingError("policy", "Evaluate", err) diff --git a/gen/http/policy/server/encode_decode.go b/gen/http/policy/server/encode_decode.go index a8f93f06304bebb18ca6be77b35cea998086b6ed..23541d267bff04cc86c134f59d6f5e8a0bc9cd34 100644 --- a/gen/http/policy/server/encode_decode.go +++ b/gen/http/policy/server/encode_decode.go @@ -12,6 +12,7 @@ import ( "context" "io" "net/http" + "strconv" policy "gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/policy/gen/policy" goahttp "goa.design/goa/v3/http" @@ -53,6 +54,7 @@ func DecodeEvaluateRequest(mux goahttp.Muxer, decoder func(*http.Request) goahtt policyName string version string evaluationID *string + ttl *int params = mux.Vars(r) ) @@ -63,7 +65,21 @@ func DecodeEvaluateRequest(mux goahttp.Muxer, decoder func(*http.Request) goahtt if evaluationIDRaw != "" { evaluationID = &evaluationIDRaw } - payload := NewEvaluateRequest(body, group, policyName, version, evaluationID) + { + ttlRaw := r.Header.Get("x-cache-ttl") + if ttlRaw != "" { + v, err2 := strconv.ParseInt(ttlRaw, 10, strconv.IntSize) + if err2 != nil { + err = goa.MergeErrors(err, goa.InvalidFieldTypeError("ttl", ttlRaw, "integer")) + } + pv := int(v) + ttl = &pv + } + } + if err != nil { + return nil, err + } + payload := NewEvaluateRequest(body, group, policyName, version, evaluationID, ttl) return payload, nil } diff --git a/gen/http/policy/server/types.go b/gen/http/policy/server/types.go index a640ce9ff3e7b66124cd14098c2c8dc6681dcc13..9c2d515d29d9fe6843434ea713748d4d786452a2 100644 --- a/gen/http/policy/server/types.go +++ b/gen/http/policy/server/types.go @@ -13,7 +13,7 @@ import ( ) // NewEvaluateRequest builds a policy service Evaluate endpoint payload. -func NewEvaluateRequest(body interface{}, group string, policyName string, version string, evaluationID *string) *policy.EvaluateRequest { +func NewEvaluateRequest(body interface{}, group string, policyName string, version string, evaluationID *string, ttl *int) *policy.EvaluateRequest { v := body res := &policy.EvaluateRequest{ Input: &v, @@ -22,6 +22,7 @@ func NewEvaluateRequest(body interface{}, group string, policyName string, versi res.PolicyName = policyName res.Version = version res.EvaluationID = evaluationID + res.TTL = ttl return res } diff --git a/gen/policy/service.go b/gen/policy/service.go index 75c54e934cf656ca496645e87f9de5f497a001e5..e1695f45662a16eda23c73a9434210009c6ca4fe 100644 --- a/gen/policy/service.go +++ b/gen/policy/service.go @@ -45,6 +45,8 @@ type EvaluateRequest struct { // Identifier created by external system and passed as parameter to overwrite // the randomly generated evaluationID. EvaluationID *string + // Cache TTL for storing policy result in cache + TTL *int } // EvaluateResult is the result type of the policy service Evaluate method. diff --git a/go.sum b/go.sum index 71a70efe1b1e921ede63667143ccd3636fc72a6c..b86296427cc4ef6bee1e78c0d778c77c36f6d3ba 100644 --- a/go.sum +++ b/go.sum @@ -290,8 +290,6 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/zach-klippenstein/goregen v0.0.0-20160303162051-795b5e3961ea h1:CyhwejzVGvZ3Q2PSbQ4NRRYn+ZWv5eS1vlaEusT+bAI= github.com/zach-klippenstein/goregen v0.0.0-20160303162051-795b5e3961ea/go.mod h1:eNr558nEUjP8acGw8FFjTeWvSgU1stO7FAO6eknhHe4= -gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/golib v0.0.0-20220913103832-19b72aa5e623 h1:VuWLKBdO5bTGimppwkk6v+cJZ6onc2J16hBXHf048GU= -gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/golib v0.0.0-20220913103832-19b72aa5e623/go.mod h1:5BPRkziGjdCI9xVfJBGY+7Ea51gtiEEs7+rbYeC8lnY= gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/golib v0.0.0-20220914094252-e40da1dc603b h1:H6PS64kBP+oNZbYFUQYpLGyek3cC9hwWL+rrwhx+0lM= gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/golib v0.0.0-20220914094252-e40da1dc603b/go.mod h1:5BPRkziGjdCI9xVfJBGY+7Ea51gtiEEs7+rbYeC8lnY= go.mongodb.org/mongo-driver v1.10.2 h1:4Wk3cnqOrQCn0P92L3/mmurMxzdvWWs5J9jinAVKD+k= diff --git a/internal/clients/cache/client.go b/internal/clients/cache/client.go index a128681312e815b82e88341fca684865faf897f8..4e317d47408226df5053806073ddb2734efd035f 100644 --- a/internal/clients/cache/client.go +++ b/internal/clients/cache/client.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net/http" + "strconv" "gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/golib/errors" ) @@ -29,7 +30,7 @@ func New(addr string, opts ...Option) *Client { return c } -func (c *Client) Set(ctx context.Context, key, namespace, scope string, value []byte) error { +func (c *Client) Set(ctx context.Context, key, namespace, scope string, value []byte, ttl int) error { req, err := http.NewRequestWithContext(ctx, "POST", c.addr+"/v1/cache", bytes.NewReader(value)) if err != nil { return err @@ -40,6 +41,9 @@ func (c *Client) Set(ctx context.Context, key, namespace, scope string, value [] "x-cache-namespace": []string{namespace}, "x-cache-scope": []string{scope}, } + if ttl != 0 { + req.Header.Add("x-cache-ttl", strconv.Itoa(ttl)) + } resp, err := c.httpClient.Do(req) if err != nil { diff --git a/internal/service/policy/policyfakes/fake_cache.go b/internal/service/policy/policyfakes/fake_cache.go index 711539af213407e90c2782fbf313642a3457a89f..957b761a068952f4c6fd20fe9c4f891ab6bbdd10 100644 --- a/internal/service/policy/policyfakes/fake_cache.go +++ b/internal/service/policy/policyfakes/fake_cache.go @@ -25,7 +25,7 @@ type FakeCache struct { result1 []byte result2 error } - SetStub func(context.Context, string, string, string, []byte) error + SetStub func(context.Context, string, string, string, []byte, int) error setMutex sync.RWMutex setArgsForCall []struct { arg1 context.Context @@ -33,6 +33,7 @@ type FakeCache struct { arg3 string arg4 string arg5 []byte + arg6 int } setReturns struct { result1 error @@ -111,7 +112,7 @@ func (fake *FakeCache) GetReturnsOnCall(i int, result1 []byte, result2 error) { }{result1, result2} } -func (fake *FakeCache) Set(arg1 context.Context, arg2 string, arg3 string, arg4 string, arg5 []byte) error { +func (fake *FakeCache) Set(arg1 context.Context, arg2 string, arg3 string, arg4 string, arg5 []byte, arg6 int) error { var arg5Copy []byte if arg5 != nil { arg5Copy = make([]byte, len(arg5)) @@ -125,13 +126,14 @@ func (fake *FakeCache) Set(arg1 context.Context, arg2 string, arg3 string, arg4 arg3 string arg4 string arg5 []byte - }{arg1, arg2, arg3, arg4, arg5Copy}) + arg6 int + }{arg1, arg2, arg3, arg4, arg5Copy, arg6}) stub := fake.SetStub fakeReturns := fake.setReturns - fake.recordInvocation("Set", []interface{}{arg1, arg2, arg3, arg4, arg5Copy}) + fake.recordInvocation("Set", []interface{}{arg1, arg2, arg3, arg4, arg5Copy, arg6}) fake.setMutex.Unlock() if stub != nil { - return stub(arg1, arg2, arg3, arg4, arg5) + return stub(arg1, arg2, arg3, arg4, arg5, arg6) } if specificReturn { return ret.result1 @@ -145,17 +147,17 @@ func (fake *FakeCache) SetCallCount() int { return len(fake.setArgsForCall) } -func (fake *FakeCache) SetCalls(stub func(context.Context, string, string, string, []byte) error) { +func (fake *FakeCache) SetCalls(stub func(context.Context, string, string, string, []byte, int) error) { fake.setMutex.Lock() defer fake.setMutex.Unlock() fake.SetStub = stub } -func (fake *FakeCache) SetArgsForCall(i int) (context.Context, string, string, string, []byte) { +func (fake *FakeCache) SetArgsForCall(i int) (context.Context, string, string, string, []byte, int) { fake.setMutex.RLock() defer fake.setMutex.RUnlock() argsForCall := fake.setArgsForCall[i] - return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4, argsForCall.arg5 + return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4, argsForCall.arg5, argsForCall.arg6 } func (fake *FakeCache) SetReturns(result1 error) { diff --git a/internal/service/policy/service.go b/internal/service/policy/service.go index 8ff2196dae5f3bf179432cb51a5c87238f1eed29..e6cd92ae23ba29cb5b0af3d43a8514a2ff9796b0 100644 --- a/internal/service/policy/service.go +++ b/internal/service/policy/service.go @@ -21,7 +21,7 @@ import ( //go:generate counterfeiter . RegoCache type Cache interface { - Set(ctx context.Context, key, namespace, scope string, value []byte) error + Set(ctx context.Context, key, namespace, scope string, value []byte, ttl int) error Get(ctx context.Context, key, namespace, scope string) ([]byte, error) } @@ -116,7 +116,11 @@ func (s *Service) Evaluate(ctx context.Context, req *policy.EvaluateRequest) (*p return nil, errors.New("error encoding result to json") } - if err := s.cache.Set(ctx, evaluationID, "", "", jsonValue); err != nil { + var ttl int + if req.TTL != nil { + ttl = *req.TTL + } + if err := s.cache.Set(ctx, evaluationID, "", "", jsonValue, ttl); err != nil { logger.Error("error storing policy result in cache", zap.Error(err)) return nil, errors.New("error storing policy result in cache") } diff --git a/internal/service/policy/service_test.go b/internal/service/policy/service_test.go index 9748c6118228ec14c8865f6fda732923c2f818ee..a239e5e7187f82f5e7a472a8efa2154918ca2c01 100644 --- a/internal/service/policy/service_test.go +++ b/internal/service/policy/service_test.go @@ -10,6 +10,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" goapolicy "gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/policy/gen/policy" "gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/policy/internal/service/policy" "gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/policy/internal/service/policy/policyfakes" @@ -72,7 +73,7 @@ func TestService_Evaluate(t *testing.T) { }, }, cache: &policyfakes.FakeCache{ - SetStub: func(ctx context.Context, s string, s2 string, s3 string, bytes []byte) error { + SetStub: func(ctx context.Context, s string, s2 string, s3 string, bytes []byte, i int) error { return nil }, }, @@ -152,7 +153,7 @@ func TestService_Evaluate(t *testing.T) { }, }, cache: &policyfakes.FakeCache{ - SetStub: func(ctx context.Context, s string, s2 string, s3 string, bytes []byte) error { + SetStub: func(ctx context.Context, s string, s2 string, s3 string, bytes []byte, i int) error { return nil }, }, @@ -181,7 +182,7 @@ func TestService_Evaluate(t *testing.T) { }, }, cache: &policyfakes.FakeCache{ - SetStub: func(ctx context.Context, s string, s2 string, s3 string, bytes []byte) error { + SetStub: func(ctx context.Context, s string, s2 string, s3 string, bytes []byte, i int) error { return errors.New("some error") }, }, @@ -209,7 +210,42 @@ func TestService_Evaluate(t *testing.T) { }, }, cache: &policyfakes.FakeCache{ - SetStub: func(ctx context.Context, s string, s2 string, s3 string, bytes []byte) error { + SetStub: func(ctx context.Context, s string, s2 string, s3 string, bytes []byte, i int) error { + return nil + }, + }, + res: &goapolicy.EvaluateResult{ + Result: map[string]interface{}{"hello": "world"}, + }, + }, + { + name: "policy is evaluated successfully with TTL sent in the request headers", + req: &goapolicy.EvaluateRequest{ + Group: "testgroup", + PolicyName: "example", + Version: "1.0", + Input: map[string]interface{}{"msg": "yes"}, + TTL: ptr.Int(30), + }, + regocache: &policyfakes.FakeRegoCache{ + GetStub: func(key string) (*rego.PreparedEvalQuery, bool) { + return nil, false + }, + }, + storage: &policyfakes.FakeStorage{ + PolicyStub: func(ctx context.Context, s string, s2 string, s3 string) (*storage.Policy, error) { + return &storage.Policy{ + Name: "example", + Group: "testgroup", + Version: "1.0", + Rego: testPolicyBlankAssignment, + Locked: false, + LastUpdate: time.Now(), + }, nil + }, + }, + cache: &policyfakes.FakeCache{ + SetStub: func(ctx context.Context, s string, s2 string, s3 string, bytes []byte, i int) error { return nil }, }, @@ -239,7 +275,7 @@ func TestService_Evaluate(t *testing.T) { }, }, cache: &policyfakes.FakeCache{ - SetStub: func(ctx context.Context, s string, s2 string, s3 string, bytes []byte) error { + SetStub: func(ctx context.Context, s string, s2 string, s3 string, bytes []byte, i int) error { return nil }, }, diff --git a/vendor/gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/golib/ptr/ptr.go b/vendor/gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/golib/ptr/ptr.go new file mode 100644 index 0000000000000000000000000000000000000000..fd522b7a70d7449dccc01ee3b2b3181eb9e7600e Binary files /dev/null and b/vendor/gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/golib/ptr/ptr.go differ diff --git a/vendor/modules.txt b/vendor/modules.txt index ed9642b410b5f8097e70ddd0cc8dbf472ffc4829..ffa34e8242fb30107a3b9d3e451a8af7abb780b0 100644 Binary files a/vendor/modules.txt and b/vendor/modules.txt differ