Skip to content

Commit

Permalink
#30 updating readme, adding stack trace
Browse files Browse the repository at this point in the history
  • Loading branch information
hlafaille committed Aug 9, 2024
1 parent e0988bf commit 2e895c5
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 69 deletions.
93 changes: 37 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,62 +12,43 @@ We recommend you use an OCI Container (Docker, Podman) to deploy ATC. Follow the

1. Write your configuration file

```json
{
"services": [
{
"name": "weather",
"description": "National Weather Service API",
"maintainer": "United States Government",
"hosts": [
"api.weather.gov"
],
"provides": [
{
"endpoint": "/",
"methods": [
"GET"
]
}
],
"consumes": [
{
"service": "exampleApi",
"endpoint": "/healthcheck",
"methods": [
"GET"
]
}
]
},
{
"name": "exampleApi",
"description": "Example API",
"maintainer": "Kerosene Labs",
"hosts": [
"api.kerosenelabs.net"
],
"providesPrefix": "/v1",
"provides": [
{
"endpoint": "/healthcheck",
"methods": [
"GET"
]
}
],
"consumes": [
{
"service": "weather",
"endpoint": "/",
"methods": [
"GET"
]
}
]
}
]
}
```yaml
api:
rootAccessToken: secureRootAccessToken
services:
# Weather API
- name: weather
description: Weather Channel API
maintainer: United States Government
identityToken: secureIdentityToken
hosts:
- api.weather.gov
provides:
- endpoint: /
methods:
- GET
consumes:
- service: exampleApi
endpoint: /healthcheck
methods:
- GET

# Example API
- name: exampleApi
description: Example API
maintainer: Kerosene Labs
identityToken: anotherSecureIdentityToken
hosts:
- exampleapi.kerosenelabs.io
provides:
- endpoint: /healthcheck
methods:
- GET
consumes:
- service: weather
endpoint: /
methods:
- GET
```
2. Pull the latest image
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.kerosenelabs.atc.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ExceptionErrorResponse {
private String message;
private List<String> stackTrace;
}
45 changes: 32 additions & 13 deletions src/main/java/io/kerosenelabs/atc/server/CentralRequestHandler.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.kerosenelabs.atc.server;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.kerosenelabs.atc.configuration.ConfigurationHandler;
import io.kerosenelabs.atc.configuration.model.Configuration;
import io.kerosenelabs.atc.model.ExceptionErrorResponse;
import io.kerosenelabs.kindling.HttpRequest;
import io.kerosenelabs.kindling.HttpResponse;
import io.kerosenelabs.kindling.constant.HttpStatus;
Expand All @@ -11,8 +14,10 @@

import java.io.IOException;
import java.io.ObjectInputFilter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class CentralRequestHandler extends RequestHandler {
Expand Down Expand Up @@ -61,7 +66,7 @@ public HttpResponse handle(HttpRequest httpRequest) throws KindlingException {
}
}
if (consumedEndpoint == null) {
throw new KindlingException("Requesting service is not allowed to consume the requested service (service is not declared as allowed to consume the endpoint)");
throw new KindlingException("Disallowed request, consumer does not declare the provider as consumed");
}

// iterate over services, ensure that we have a service providing this endpoint
Expand All @@ -76,10 +81,11 @@ public HttpResponse handle(HttpRequest httpRequest) throws KindlingException {
}
}
}
if (providedEndpoint == null || providingService == null) {
throw new KindlingException("Requesting service is not allowed to provide the requested service (requested service does not provide the requested endpoint to the requester)");
if (providedEndpoint == null) {
throw new KindlingException("Disallowed request, provider does not declare this endpoint as providable");
}


return new HttpResponse.Builder().status(HttpStatus.NO_CONTENT).build();
}

Expand All @@ -90,16 +96,29 @@ public boolean accepts(HttpRequest httpRequest) throws KindlingException {

@Override
public HttpResponse handleError(Throwable t) {
if (t instanceof KindlingException) {
return new HttpResponse.Builder()
.status(HttpStatus.BAD_REQUEST)
.headers(new HashMap<>() {
{
put("Content-Type", "application/json");
}
})
.content(String.format("{\"message\": \"%s\"}", t.getMessage()))
.build();
ObjectMapper objectMapper = new ObjectMapper();

if (t instanceof KindlingException || System.getProperty("atc.http.allowExceptionsInErrorResponse").equals("true")) {
try {
return new HttpResponse.Builder()
.status(HttpStatus.BAD_REQUEST)
.headers(new HashMap<>() {
{
put("Content-Type", "application/json");
}
})
.content(objectMapper.writeValueAsString(
new ExceptionErrorResponse(
t.getMessage(),
Arrays.stream(t.getStackTrace())
.map(Object::toString)
.toList())
)
)
.build();
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

return new HttpResponse.Builder()
Expand Down

0 comments on commit 2e895c5

Please sign in to comment.