How to configure Krakend so it return http redirect response as-is instead of following the http redirect? - api

I am currently using Krakend (https://krakend.io) API Gateway to proxy request to my backend service. One of my backend service API response is a redirect response with http 303. The redirect response looks like this below :
HTTP/1.1 303 See Other
content-length: 48
content-type: text/plain; charset=utf-8
date: Thu, 16 Jul 2020 10:25:41 GMT
location: https://www.detik.com/
vary: Accept
x-powered-by: Express
x-envoy-upstream-service-time: 17
server: istio-envoy
The problem is that, instead of returning the http 303 response to client (with location response header) as-is, Krakend is actually following the http redirect and return the response of the redirect Url, which is the html response of https://www.detik.com/.
My current krakend configuration looks like this below :
{
"version": 2,
"extra_config": {
"github_com/devopsfaith/krakend-cors": {
"allow_origins": [],
"expose_headers": [
"Content-Length",
"Content-Type",
"Location"
],
"allow_headers": [
"Content-Type",
"Origin",
"X-Requested-With",
"Accept",
"Authorization",
"secret",
"Host"
],
"max_age": "12h",
"allow_methods": [
"GET",
"POST",
"PUT"
]
},
"github_com/devopsfaith/krakend-gologging": {
"level": "ERROR",
"prefix": "[GATEWAY]",
"syslog": false,
"stdout": true,
"format": "default"
},
"github_com/devopsfaith/krakend-logstash": {
"enabled": false
}
},
"timeout": "10000ms",
"cache_ttl": "300s",
"output_encoding": "json",
"name": "api-gateway",
"port": 8080,
"endpoints": [
{
"endpoint": "/ramatestredirect",
"method": "GET",
"extra_config": {},
"output_encoding": "no-op",
"concurrent_calls": 1,
"backend": [
{
"url_pattern": "/",
"encoding": "no-op",
"sd": "static",
"extra_config": {},
"method": "GET",
"host": [
"http://ramatestredirect.default.svc.cluster.local"
],
"disable_host_sanitize": false
}
]
}
]
}
So how can I make krakend to return original http 303 response unaltered from my backend service to the client ?
Thank You

I assume that you're calling this endpoint /ramatestredirect
To get backend http status code (as you said it return 303 http status code), you can use this way:
{
"endpoint": "/ramatestredirect",
"method": "GET",
"extra_config": {},
"output_encoding": "no-op",
"concurrent_calls": 1,
"backend": [
{
"url_pattern": "/",
"encoding": "no-op",
"sd": "static",
"extra_config": {
"github.com/devopsfaith/krakend/http": {
"return_error_details": "authentication"
}
},
"method": "GET",
"host": [
"http://ramatestredirect.default.svc.cluster.local"
],
"disable_host_sanitize": false
}
]
}
So, basically with this plugin you can get the original backend http status code
"github.com/devopsfaith/krakend/http": {
"return_error_details": "authentication"
}

If you use Lura Framework (formerly known as Kraken framework), then you may have to disable redirects for your http client.
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}

Related

How to implement Kerio client API

I am trying implement Kerio webmail client API, at least get it working in Postman.
This is my request:
{
"jsonrpc": "2.0",
"id": 1,
"method": "Session.login",
"params": {
"userName": "my#email.com",
"password": "myPassword",
"application": {
"name": "Petr Test",
"vendor": "Petr",
"version": "1.0"
}
}
}
I have all the needed headers there
Accept: application/json-rpc
Content-Type: application/json-rpc; charset=UTF-8
User-Agent: Kerio APIs Client Library for PHP/1.3
Host: mail.company.tld:443
Content-Length: 217
Connection: close
But keep getting some parse error in the response:
{
"jsonrpc": "2.0",
"id": null,
"error": {
"code": -32602,
"message": "Parse error.",
"data": {
"messageParameters": {
"positionalParameters": [],
"plurality": 1
}
}
}
}
Any ideas?

Sending attribute from Request JSON in Response JSON is not working in Wiremock

I am working on wiremock POC and I am facing issues when I try to send an attribute from request payload in response payload.
Request Payload:
{
"work_order": "12345678"
}
Mapping file:
{
"request": {
"method": "POST",
"urlPath": "/transform"
},
"response": {
"status": 200,
"body": "{\"randomInteger\": \"{{randomValue type = 'UUID'}}\",\"CorrelationID\": \"{{{request.body}}}\"}",
"headers": {
"Content-Type": "application/json"
},
"transformers": ["response-template"]
}
}
Response Payload:
{
"randomInteger": "d24ea3bf-36c0-40b8-b84b-b9268ee85f55",
"CorrelationID": "{
"work_order": "12345678"
}"
}
Above is working fine.
Now if I try to select the attribute value instead of entire body file, it is not working.
Mapping File:
{
"request": {
"method": "POST",
"urlPath": "/transform"
},
"response": {
"status": 200,
"body": "{\"randomInteger\": \"{{randomValue type = 'UUID'}}\",\"CorrelationID\": \"{{{request.body.work_order}}}\"}",
"headers": {
"Content-Type": "application/json"
},
"transformers": ["response-template"]
}
}
Response Payload:
{"randomInteger": "e35e4d5f-b043-4770-b659-4131d313e002","CorrelationID": ""}

traefik expose internal metrics

I would like to expose internal metrics of traefik.
After reading the documentation I created the following configuration file:
logLevel = "INFO"
[entryPoints]
[entryPoints.http]
address = ":80"
[entryPoints.dashboard]
address = ":16081"
# API definition
[api]
entryPoint = "dashboard"
dashboard = true
debug = false
[api.statistics]
recentErrors = 10
# Metrics definition
[metrics]
# DataDog metrics exporter type
[metrics.datadog]
address = "172.17.0.1:8125"
pushInterval = "10s"
################################################################
# Mesos/Marathon Provider
################################################################
# Enable Marathon Provider.
[marathon]
endpoint = "http://mesos.lan:8080/"
watch = true
domain = "service.lan"
exposedByDefault = false
When I query the dashboard entrypoint I got a 404 error on /metrics:
curl -s http://localhost:16081/health | jq
{
"pid": 1,
"uptime": "3h31m3.5252748s",
"uptime_sec": 12663.5252748,
"time": "2018-09-04 16:53:17.7128687 +0000 UTC m=+12663.602939001",
"unixtime": 1536079997,
"status_code_count": {},
"total_status_code_count": {
"404": 5
},
"count": 0,
"total_count": 5,
"total_response_time": "390.7µs",
"total_response_time_sec": 0.0003907,
"average_response_time": "78.14µs",
"average_response_time_sec": 7.814e-05,
"recent_errors": [
{
"status_code": 404,
"status": "Not Found",
"method": "GET",
"host": "localhost:16081",
"path": "/metrics",
"time": "2018-09-04T16:53:12.0232879Z"
},
{
"status_code": 404,
"status": "Not Found",
"method": "GET",
"host": "localhost:16081",
"path": "/metrics",
"time": "2018-09-04T13:18:52.7206202Z"
},
{
"status_code": 404,
"status": "Not Found",
"method": "GET",
"host": "localhost:16081",
"path": "/metrics",
"time": "2018-09-04T13:18:51.853093Z"
},
{
"status_code": 404,
"status": "Not Found",
"method": "GET",
"host": "localhost:16081",
"path": "/metrics",
"time": "2018-09-04T13:18:50.9894516Z"
},
{
"status_code": 404,
"status": "Not Found",
"method": "GET",
"host": "localhost:16081",
"path": "/metrics",
"time": "2018-09-04T13:18:49.8598176Z"
}
]
}
curl -s http://localhost:16081/metrics
404 page not found
Did I miss something ?
My main objective is to be able to get metrics per frontend/backend.
I would like to be able to know the number of requests and returned status code per frontend.
Thanks,
Renaud
This is solved, long story short, /metrics is only exposed when promotheus provider is enable. When Datadog provider is enable all the metrics are sent to datadog.
Details can be found here: github.com/containous/traefik/issues/3877

BigCommerce Create Shipment - no response

I am coding a API to create shipments on Big Commerce.
I am getting responses from the 'Get' URL's - I just can't seem to get the API to respond on the 'PUT'
I fired up a 'Web Responder' and it returns the following:
The tokens etc are moved for security.
Header:
{
"VERSION": "HTTP/1.1",
"CONNECTION": "close",
"ACCEPT-ENCODING": "gzip",
"CONTENT-TYPE": "application/json",
"AUTHORIZATION": "Bearer ---------------------",
"X-AUTH-CLIENT": "======================",
"X-AUTH-TOKEN": "=========================",
"ACCEPT": "application/json;",
"ACCEPT-CHARSET": "UTF-8;",
"USER-AGENT": "West Wind Internet Protocols 5.56",
"CACHE-CONTROL": "no-cache",
"COOKIE": "__cfduid=dfebfa0729eeaf50601b1fe187807c6fc1529278210; owner_token=cdc79c402c05c15d01ce0996dcc40654e3a0fe75a256eae3",
"CONTENT-LENGTH": "171"
}
The 'PUT' has:
PUT /b7ezoY2bqq2DKg0soyMy
{
"tracking_number": "PBT0000124",
"comments": "Shipped by PBT",
"order_address_id": 392,
"shipping_provider": "",
"items": [
{
"order_product_id": 1540,
"quantity": 1
}
]
}
As far as I can tell, all the details are correct. I just get no response. Please not this is a 'Desktop' application - not a Website.
Any clues?

Axios Request Does Not Return a Token

i'm trying to apply token for my request. So i tried to console.log the result of the request, and cannot find any token there inside the object array.
Object {
"config": Object {
"adapter": [Function xhrAdapter],
"data": "ktp=3578270708950002&member=199508070003",
"headers": Object {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/x-www-form-urlencoded",
},
"maxContentLength": -1,
"method": "post",
"timeout": 0,
"transformRequest": Object {
"0": [Function transformRequest],
},
"transformResponse": Object {
"0": [Function transformResponse],
},
"url": "http://103.53.10.122/mobile/LoginCheck.php",
"validateStatus": [Function validateStatus],
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
},
"data": Array [
Object {
"status": "67457",
},
],
"headers": Object {
"connection": "keep-alive",
"content-type": "text/html; charset=UTF-8",
"date": "Fri, 22 Dec 2017 05:38:21 GMT",
"server": "nginx",
"transfer-encoding": "chunked",
"vary": "Accept-Encoding",
},
"request": XMLHttpRequest {
"DONE": 4,
"HEADERS_RECEIVED": 2,
"LOADING": 3,
"OPENED": 1,
"UNSENT": 0,
"_aborted": false,
"_cachedResponse": undefined,
"_hasError": false,
"_headers": Object {
"accept": "application/json, text/plain, */*",
"content-type": "application/x-www-form-urlencoded",
},
"_incrementalEvents": false,
"_lowerCaseResponseHeaders": Object {
"connection": "keep-alive",
"content-type": "text/html; charset=UTF-8",
"date": "Fri, 22 Dec 2017 05:38:21 GMT",
"server": "nginx",
"transfer-encoding": "chunked",
"vary": "Accept-Encoding",
},
"_method": "POST",
"_requestId": null,
"_response": "[{\"status\":\"67457\"}]",
"_responseType": "",
"_sent": true,
"_subscriptions": Array [],
"_timedOut": false,
"_trackingName": "unknown",
"_url": "http://103.53.10.122/mobile/LoginCheck.php",
"readyState": 4,
"responseHeaders": Object {
"Connection": "keep-alive",
"Content-Type": "text/html; charset=UTF-8",
"Date": "Fri, 22 Dec 2017 05:38:21 GMT",
"Server": "nginx",
"Transfer-Encoding": "chunked",
"Vary": "Accept-Encoding",
},
"responseURL": "http://103.53.10.122/mobile/LoginCheck.php",
"status": 200,
"timeout": 0,
"upload": XMLHttpRequestEventTarget {},
"withCredentials": true,
},
"status": 200,
"statusText": undefined,
}
Can someone point me how to add token to authenticate, because i'm still confuse about the concept even after reading it. So if i'm not mistaken, i should do the following in order:
Generate the token when user successfully login
Save the token in local storage
Use the token for each request (How does the backend check the validity of the token?)
Any help would be appreciated
Depending on the way the backend handles API requests you should either use axios.get and append the token to the URL or use axios.post and pass a object as body to the method with your token.
E.g.
axios.post('http://103.53.10.122/mobile/LoginCheck.php', {
username: "test",
password: "1234"
})
.then((res) => {
console.log(res);
/*
In this example I assume that res.data has the token returned from the backend
The res.data should look like this then:
{
token: "1234"
}
*/
let token = res.data.token;
AsyncStorage.setItem("token", token);
})
.catch((err) => {
console.log(err);
});
To use the token for each request save it in AsyncStorage for persistent storage or in the redux state if you are using redux.
On the server side you could generate a JSON web token which contains all data you need for authenticating the user and validating it with the data stored in a database. Due to the fact that you are using PHP I can recommend you this introduction to JSON web tokens in combination with PHP: https://www.sitepoint.com/php-authorization-jwt-json-web-tokens/