Skip to content

Commit

Permalink
Add logging of request from proxy stub
Browse files Browse the repository at this point in the history
  • Loading branch information
ashashev authored and danslapman committed Oct 11, 2023
1 parent c9653a8 commit 511c7dc
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 108 deletions.
3 changes: 2 additions & 1 deletion backend/mockingbird/src/main/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ ru.tinkoff.tcb {
excludedRequestHeaders = []
excludedResponseHeaders = []
insecureHosts = []
logOutgoingRequests = false
}

event {
fetchInterval = "5 s"
reloadInterval = "1 m"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,40 +151,42 @@ final class PublicApiHandler(
body: RequestBody
)(uri: String, delay: Option[FiniteDuration], timeout: Option[FiniteDuration]): RIO[WLD, HttpStubResponse] = {
val requestUri = uri"$uri".pipe(query.foldLeft(_) { case (u, (key, value)) => u.addParam(key, value) })
log.debug(s"Received headers: ${headers.keys.mkString(", ")}") *> basicRequest
.headers(headers -- proxyConfig.excludedRequestHeaders)
.method(Method(method.entryName), requestUri)
.pipe(rt =>
body match {
case AbsentRequestBody => rt
case SimpleRequestBody(value) => rt.body(value)
case MultipartRequestBody(value) =>
rt.multipartBody[Any](
value.map(part =>
multipart(part.name, part.body)
.pipe(newPart =>
part.headers.foldLeft(newPart) { case (acc, header) =>
acc.header(header.name, header.value, true)
}
)
for {
_ <- log.debug(s"Received headers: ${headers.keys.mkString(", ")}")
req = basicRequest
.headers(headers -- proxyConfig.excludedRequestHeaders)
.method(Method(method.entryName), requestUri)
.pipe(rt =>
body match {
case AbsentRequestBody => rt
case SimpleRequestBody(value) => rt.body(value)
case MultipartRequestBody(value) =>
rt.multipartBody[Any](
value.map(part =>
multipart(part.name, part.body)
.pipe(newPart =>
part.headers.foldLeft(newPart) { case (acc, header) =>
acc.header(header.name, header.value, true)
}
)
)
)
)
}
)
.response(asByteArrayAlways)
.readTimeout(timeout.getOrElse(1.minute.asScala))
.send(httpBackend)
.map { response =>
BinaryResponse(
response.code.code,
response.headers
.filterNot(h => proxyConfig.excludedResponseHeaders(h.name))
.map(h => h.name -> h.value)
.toMap,
response.body.coerce[ByteArray],
delay
}
)
}
.response(asByteArrayAlways)
_ <- log.debug("Executing request: {}", req.toCurl).when(proxyConfig.logOutgoingRequests)
response <- req
.readTimeout(timeout.getOrElse(1.minute.asScala))
.send(httpBackend)
} yield BinaryResponse(
response.code.code,
response.headers
.filterNot(h => proxyConfig.excludedResponseHeaders(h.name))
.map(h => h.name -> h.value)
.toMap,
response.body.coerce[ByteArray],
delay
)
}

private def jsonProxyRequest(
Expand All @@ -202,43 +204,47 @@ final class PublicApiHandler(
val requestUri = uri"$uri".pipe(query.foldLeft(_) { case (u, (key, value)) =>
u.addParam(key, value)
})
log.debug(s"Received headers: ${headers.keys.mkString(", ")}") *> basicRequest
.headers(headers -- proxyConfig.excludedRequestHeaders)
.method(Method(method.entryName), requestUri)
.pipe(rt =>
body match {
case AbsentRequestBody => rt
case SimpleRequestBody(value) => rt.body(value)
case MultipartRequestBody(value) =>
rt.multipartBody[Any](
value.map(part =>
multipart(part.name, part.body)
.pipe(newPart =>
part.headers.foldLeft(newPart) { case (acc, header) => acc.header(header.name, header.value, true) }
)
for {
_ <- log.debug(s"Received headers: ${headers.keys.mkString(", ")}")
req = basicRequest
.headers(headers -- proxyConfig.excludedRequestHeaders)
.method(Method(method.entryName), requestUri)
.pipe(rt =>
body match {
case AbsentRequestBody => rt
case SimpleRequestBody(value) => rt.body(value)
case MultipartRequestBody(value) =>
rt.multipartBody[Any](
value.map(part =>
multipart(part.name, part.body)
.pipe(newPart =>
part.headers.foldLeft(newPart) { case (acc, header) =>
acc.header(header.name, header.value, true)
}
)
)
)
)
}
)
.response(asJsonAlways[Json])
.readTimeout(timeout.getOrElse(1.minute.asScala))
.send(httpBackend)
.map { response =>
response.body match {
case Right(jsonResponse) =>
RawResponse(
response.code.code,
response.headers
.filterNot(h => proxyConfig.excludedResponseHeaders(h.name))
.map(h => h.name -> h.value)
.toMap,
jsonResponse.patch(data, patch).noSpaces,
delay
)
case Left(error) =>
RawResponse(500, Map(), error.body, delay)
}
}
}
)
.response(asJsonAlways[Json])
_ <- log.debug("Executing request: {}", req.toCurl).when(proxyConfig.logOutgoingRequests)
response <- req
.readTimeout(timeout.getOrElse(1.minute.asScala))
.send(httpBackend)
} yield response.body match {
case Right(jsonResponse) =>
RawResponse(
response.code.code,
response.headers
.filterNot(h => proxyConfig.excludedResponseHeaders(h.name))
.map(h => h.name -> h.value)
.toMap,
jsonResponse.patch(data, patch).noSpaces,
delay
)
case Left(error) =>
RawResponse(500, Map(), error.body, delay)
}
}

private def xmlProxyRequest(
Expand All @@ -257,43 +263,47 @@ final class PublicApiHandler(
val requestUri = uri"$uri".pipe(query.foldLeft(_) { case (u, (key, value)) =>
u.addParam(key, value)
})
log.debug(s"Received headers: ${headers.keys.mkString(", ")}") *> basicRequest
.headers(headers -- proxyConfig.excludedRequestHeaders)
.method(Method(method.entryName), requestUri)
.pipe(rt =>
body match {
case AbsentRequestBody => rt
case SimpleRequestBody(value) => rt.body(value)
case MultipartRequestBody(value) =>
rt.multipartBody[Any](
value.map(part =>
multipart(part.name, part.body)
.pipe(newPart =>
part.headers.foldLeft(newPart) { case (acc, header) => acc.header(header.name, header.value, true) }
)
for {
_ <- log.debug(s"Received headers: ${headers.keys.mkString(", ")}")
req = basicRequest
.headers(headers -- proxyConfig.excludedRequestHeaders)
.method(Method(method.entryName), requestUri)
.pipe(rt =>
body match {
case AbsentRequestBody => rt
case SimpleRequestBody(value) => rt.body(value)
case MultipartRequestBody(value) =>
rt.multipartBody[Any](
value.map(part =>
multipart(part.name, part.body)
.pipe(newPart =>
part.headers.foldLeft(newPart) { case (acc, header) =>
acc.header(header.name, header.value, true)
}
)
)
)
)
}
)
.response(asXML)
.readTimeout(timeout.getOrElse(1.minute.asScala))
.send(httpBackend)
.map { response =>
response.body match {
case Right(xmlResponse) =>
RawResponse(
response.code.code,
response.headers
.filterNot(h => proxyConfig.excludedResponseHeaders(h.name))
.map(h => h.name -> h.value)
.toMap,
xmlResponse.patchFromValues(jData, xData, patch.map { case (k, v) => k.toZoom -> v }).toString(),
delay
)
case Left(error) =>
RawResponse(500, Map(), error, delay)
}
}
}
)
.response(asXML)
_ <- log.debug("Executing request: {}", req.toCurl).when(proxyConfig.logOutgoingRequests)
response <- req
.readTimeout(timeout.getOrElse(1.minute.asScala))
.send(httpBackend)
} yield response.body match {
case Right(xmlResponse) =>
RawResponse(
response.code.code,
response.headers
.filterNot(h => proxyConfig.excludedResponseHeaders(h.name))
.map(h => h.name -> h.value)
.toMap,
xmlResponse.patchFromValues(jData, xData, patch.map { case (k, v) => k.toZoom -> v }).toString(),
delay
)
case Left(error) =>
RawResponse(500, Map(), error, delay)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ case class ProxyConfig(
excludedRequestHeaders: Seq[String],
excludedResponseHeaders: Set[String],
proxyServer: Option[ProxyServerConfig],
insecureHosts: Seq[String]
insecureHosts: Seq[String],
logOutgoingRequests: Boolean
)

case class EventConfig(fetchInterval: FiniteDuration, reloadInterval: FiniteDuration)
Expand Down
8 changes: 6 additions & 2 deletions configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Mockingbird конфигурируется посредством файла sec
"excludedRequestHeaders": [..],
"excludedResponseHeaders": [..],
"insecureHosts": [..],
"logOutgoingRequests": false,
"proxyServer": {
"type": "http" | "socks",
"type": "..",
Expand Down Expand Up @@ -87,14 +88,17 @@ healthCheckRoute - необязательный параметр, позволя
"excludedResponseHeaders": ["transfer-encoding"],
"insecureHosts": [
"some.host"
]
],
"logOutgoingRequests": false
}
}
}
```

В поле insecureHosts можно указать список хостов, для которых не будет выполняться проверка сертификатов. Это может быть полезно
для случаев развёртывания во внутренней инфраструктуре
для случаев развёртывания во внутренней инфраструктуре.

Флаг logOutgoingRequests позволяет включить логирование запросов к удаленному серверу, когда http заглушка работет в режиме прокси. Запрос пишется в лог в виде команды curl с заголовками и телом запроса.

Так-же в этой секции можно указать настройки прокси сервера. Эти настройки влияют на ВСЕ http запросы, которые делаем mockingbird, т.е.:
- запросы к внешнему серверу с proxy моках
Expand Down

0 comments on commit 511c7dc

Please sign in to comment.