Skip to content

Commit

Permalink
#25 implementing
Browse files Browse the repository at this point in the history
  • Loading branch information
hlafaille committed May 15, 2024
1 parent 98507c3 commit 092d5e7
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 88 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ We recommend you use an OCI Container (Docker, Podman) to deploy ATC. Follow the
"hosts": [
"api.weather.gov"
],
"scopes": [
"provides": [
{
"endpoint": "/",
"methods": [
Expand All @@ -47,8 +47,8 @@ We recommend you use an OCI Container (Docker, Podman) to deploy ATC. Follow the
"hosts": [
"api.kerosenelabs.net"
],
"scopesPrefix": "/v1",
"scopes": [
"providesPrefix": "/v1",
"provides": [
{
"endpoint": "/healthcheck",
"methods": [
Expand Down
6 changes: 3 additions & 3 deletions configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"hosts": [
"api.weather.gov"
],
"scopes": [
"provides": [
{
"endpoint": "/",
"methods": [
Expand All @@ -32,8 +32,8 @@
"hosts": [
"api.kerosenelabs.net"
],
"scopesPrefix": "/v1",
"scopes": [
"providesPrefix": "/v1",
"provides": [
{
"endpoint": "/healthcheck",
"methods": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public class Service {
private String maintainer;
private List<String> hosts;
private String scopesPrefix;
private List<ServiceScope> scopes;
private List<ServiceScope> provides;
private List<ServiceConsumedScope> consumes;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.smokelabs.atc.util.ErrorCode;
import com.smokelabs.atc.util.HttpStatus;

public class RequestServiceInvalidScopeException extends AtcException {
public RequestServiceInvalidScopeException() {
public class ConsumingServiceInvalidScopeException extends AtcException {
public ConsumingServiceInvalidScopeException() {
super(HttpStatus.FORBIDDEN, ErrorCode.REQUESTING_SERVICE_HAS_INVALID_SCOPE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.smokelabs.atc.util.ErrorCode;
import com.smokelabs.atc.util.HttpStatus;

public class RequestServiceNotFoundException extends AtcException {
public RequestServiceNotFoundException(String name) {
public class ConsumingServiceNotFoundException extends AtcException {
public ConsumingServiceNotFoundException(String name) {
super(HttpStatus.BAD_REQUEST, ErrorCode.REQUEST_SERVICE_NOT_FOUND,
String.format("'%s' is not a valid service", name));
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/smokelabs/atc/server/AtcHttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class AtcHttpRequest {
private HttpHeaderContainer headers = new HttpHeaderContainer();

@Getter
private String requestingServiceIdentity;
private String consumingServiceIdentity;

@Getter
private String content;
Expand Down Expand Up @@ -166,7 +166,7 @@ public AtcHttpRequest(BufferedReader bufferedReader)
// ensure that the X-RD-ServiceIdentity header is set
try {
HttpHeader serviceIdentityHeader = headers.getByName("x-rd-serviceidentity");
requestingServiceIdentity = serviceIdentityHeader.getValue();
consumingServiceIdentity = serviceIdentityHeader.getValue();
} catch (HeaderNotFoundException e) {
throw new InvalidRequestServiceIdentityException();
}
Expand Down
95 changes: 20 additions & 75 deletions src/main/java/com/smokelabs/atc/server/RequestDirector.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@

import com.smokelabs.atc.client.HttpForwarder;
import com.smokelabs.atc.configuration.ConfigurationHandler;
import com.smokelabs.atc.configuration.pojo.Configuration;
import com.smokelabs.atc.configuration.pojo.service.Service;
import com.smokelabs.atc.configuration.pojo.service.ServiceConsumedScope;
import com.smokelabs.atc.configuration.pojo.service.ServiceScope;
import com.smokelabs.atc.exception.AtcException;
import com.smokelabs.atc.exception.HeaderNotFoundException;
import com.smokelabs.atc.exception.InvalidHttpRequestException;
import com.smokelabs.atc.exception.RequestServiceInvalidScopeException;
import com.smokelabs.atc.exception.RequestServiceNotFoundException;
import com.smokelabs.atc.exception.ConsumingServiceInvalidScopeException;
import com.smokelabs.atc.exception.ConsumingServiceNotFoundException;
import com.smokelabs.atc.exception.ServiceNotFoundException;
import com.smokelabs.atc.util.HttpStatus;

Expand All @@ -28,8 +25,6 @@
*/
@Slf4j
public class RequestDirector {
private Configuration loadedConfiguration;

@NonNull
private AtcHttpRequest httpRequest;

Expand All @@ -45,7 +40,6 @@ public class RequestDirector {
public RequestDirector(AtcHttpRequest httpRequest, String traceId) {
this.httpRequest = httpRequest;
this.traceId = traceId;
this.loadedConfiguration = ConfigurationHandler.getInstance().getLoadedConfiguration();
}

/**
Expand All @@ -57,67 +51,19 @@ private void generateBaseResponseHeaders() {
}

/**
* Get the {@link ServiceConsumedScope} of this {@code requestingService} (see
* {@link Service}).
*
* @param requestingService The service making the request
* @throws InvalidScopeException If the
* @return {@link ServiceConsumedScope}, being the a service's {@code consumes}
* declaration for this destination endpoint.
*/
// private ServiceConsumedScope getRequestingServiceConsumedScope(Service
// requestingService)
// throws RequestServiceInvalidScopeException {
// ServiceConsumedScope requestingConsumedScope = null;
// for (ServiceConsumedScope consumedScope : requestingService.getConsumes()) {
// if (consumedScope.getService().equals(requestingConsumedScope)
// && httpRequest.getResource().equals(consumedScope.getEndpoint())) {
// requestingConsumedScope = consumedScope;
// }
// }
// if (requestingConsumedScope == null
// || !requestingConsumedScope.getMethods().contains(httpRequest.getMethod())) {
// throw new RequestServiceInvalidScopeException();
// }
// return requestingConsumedScope;
// }

/**
* If the {@code requestingService} should have access the
* {@code destinationService}.
* The outcome of this is determined by the declared {@code scopes} and
* If the {@code consuming} service should have access the
* {@code providing} service.
* The outcome of this is determined by the declared {@code provides} and
* {@code consumes} blocks under each respective service in
* {@code configuration.json}.
*
* @param requestingService The {@link Service} making the request
* @param destinationService The {@link Service} receiving the request
* @throws RequestServiceInvalidScopeException If the {@code requestingService}
* is forbidden access on
* {@code destinationService}'s
* scope.
* @param consuming The {@link Service} making the request
* @param providing The {@link Service} receiving the request
* @throws ConsumingServiceInvalidScopeException If the consumnig service
*/
// private boolean isRequestingScopedForDestination(Service requestingService,
// Service destinationService)
// throws RequestServiceInvalidScopeException {
// // ensure the requesting service has this resource under its 'consumes'
// ServiceConsumedScope requestingServiceConsume =
// getRequestingServiceConsumedScope(requestingService);

// // find a matching scope on the destination
// ServiceScope destinationScope = null;
// for (ServiceScope scope : destinationService.getScopes()) {
// if (httpRequest.getResource().equals(scope.getEndpoint())) {
// destinationScope = scope;
// }
// }

// // if we didn't find a destination scope, we're not scoped for this
// destination
// if (destinationScope == null) {
// return false;
// }
// return true; // todo finish
// }
private boolean isConsumingServiceScopedForProvidingService(Service consuming, Service providing) {
return true;
}

/**
* Determine what to do with this request
Expand All @@ -139,23 +85,22 @@ public AtcHttpResponse directRequest()
// set a base response
AtcHttpResponse httpResponse = new AtcHttpResponse(HttpStatus.OK, headers, null);

// get our requesting service
Service requestingService;
// get our consuming service
Service consuming;
try {
requestingService = ConfigurationHandler.getByName(httpRequest.getRequestingServiceIdentity());
consuming = ConfigurationHandler.getByName(httpRequest.getConsumingServiceIdentity());
} catch (ServiceNotFoundException e) {
throw new RequestServiceNotFoundException(httpRequest.getRequestingServiceIdentity());
throw new ConsumingServiceNotFoundException(httpRequest.getConsumingServiceIdentity());
}

// get our destination service
Service destinationService = ConfigurationHandler.getByHost(requestHost);
Service providingService = ConfigurationHandler.getByHost(requestHost);

// ensure we have access
// boolean isScoped = isRequestingScopedForDestination(requestingService,
// destinationService);
// if (!isScoped) {
// throw new InvalidScopeException();
// }
boolean isScoped = isConsumingServiceScopedForProvidingService(consuming, providingService);
if (!isScoped) {
throw new ConsumingServiceInvalidScopeException();
}

log.info(String.format("sending request to upstream (%s)",
httpRequest.getHeaders().getByName("host").getValue()));
Expand Down

0 comments on commit 092d5e7

Please sign in to comment.