Search in sources :

Example 1 with GatewayAuthenticationException

use of io.apiman.manager.api.gateway.GatewayAuthenticationException in project apiman by apiman.

the class OrganizationResourceImpl method getApiRegistry.

/**
 * Gets the API registry.
 * @param organizationId
 * @param clientId
 * @param version
 * @throws ClientVersionNotFoundException
 */
private ApiRegistryBean getApiRegistry(String organizationId, String clientId, String version) throws ClientVersionNotFoundException {
    // Try to get the client first - will throw a ClientVersionNotFoundException if not found.
    ClientVersionBean clientVersion = getClientVersionInternal(organizationId, clientId, version);
    Map<String, IGatewayLink> gatewayLinks = new HashMap<>();
    Map<String, GatewayBean> gateways = new HashMap<>();
    boolean txStarted = false;
    try {
        ApiRegistryBean apiRegistry = query.getApiRegistry(organizationId, clientId, version);
        apiRegistry.setApiKey(clientVersion.getApikey());
        List<ApiEntryBean> apis = apiRegistry.getApis();
        storage.beginTx();
        txStarted = true;
        for (ApiEntryBean api : apis) {
            String gatewayId = api.getGatewayId();
            // Don't return the gateway id.
            api.setGatewayId(null);
            GatewayBean gateway = gateways.get(gatewayId);
            if (gateway == null) {
                gateway = storage.getGateway(gatewayId);
                gateways.put(gatewayId, gateway);
            }
            IGatewayLink link = gatewayLinks.get(gatewayId);
            if (link == null) {
                link = gatewayLinkFactory.create(gateway);
                gatewayLinks.put(gatewayId, link);
            }
            ApiEndpoint se = link.getApiEndpoint(api.getApiOrgId(), api.getApiId(), api.getApiVersion());
            String apiEndpoint = se.getEndpoint();
            api.setHttpEndpoint(apiEndpoint);
        }
        return apiRegistry;
    } catch (StorageException | GatewayAuthenticationException e) {
        throw new SystemErrorException(e);
    } finally {
        if (txStarted) {
            storage.rollbackTx();
        }
        for (IGatewayLink link : gatewayLinks.values()) {
            link.close();
        }
    }
}
Also used : NewClientVersionBean(io.apiman.manager.api.beans.clients.NewClientVersionBean) ClientVersionBean(io.apiman.manager.api.beans.clients.ClientVersionBean) SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) ApiRegistryBean(io.apiman.manager.api.beans.summary.ApiRegistryBean) HashMap(java.util.HashMap) ApiEndpoint(io.apiman.gateway.engine.beans.ApiEndpoint) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) ApiEntryBean(io.apiman.manager.api.beans.summary.ApiEntryBean) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) GatewayBean(io.apiman.manager.api.beans.gateways.GatewayBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 2 with GatewayAuthenticationException

use of io.apiman.manager.api.gateway.GatewayAuthenticationException in project apiman by apiman.

the class ContractService method probePolicy.

// TODO make properly optimised query for this
public List<IPolicyProbeResponse> probePolicy(Long contractId, long policyId, String rawPayload) throws ClientNotFoundException, ContractNotFoundException {
    ContractBean contract = getContract(contractId);
    ApiVersionBean avb = contract.getApi();
    OrganizationBean apiOrg = avb.getApi().getOrganization();
    String apiKey = contract.getClient().getApikey();
    Set<String> gatewayIds = contract.getApi().getGateways().stream().map(ApiGatewayBean::getGatewayId).collect(Collectors.toSet());
    if (gatewayIds.size() == 0) {
        return List.of();
    }
    List<PolicyBean> policyChain = aggregateContractPolicies(contract);
    int idxFound = -1;
    for (int i = 0, policyChainSize = policyChain.size(); i < policyChainSize; i++) {
        PolicyBean policy = policyChain.get(i);
        if (policy.getId().equals(policyId)) {
            idxFound = i;
        }
    }
    if (idxFound == -1) {
        throw new IllegalArgumentException("Provided policy ID not found in contract " + policyId);
    }
    List<GatewayBean> gateways = tryAction(() -> storage.getGateways(gatewayIds));
    LOGGER.debug("Gateways for contract {0}: {1}", contractId, gateways);
    List<IPolicyProbeResponse> probeResponses = new ArrayList<>(gateways.size());
    for (GatewayBean gateway : gateways) {
        IGatewayLink link = gatewayLinkFactory.create(gateway);
        try {
            probeResponses.add(link.probe(apiOrg.getId(), avb.getApi().getId(), avb.getVersion(), idxFound, apiKey, rawPayload));
        } catch (GatewayAuthenticationException e) {
            throw new SystemErrorException(e);
        }
    }
    LOGGER.debug("Probe responses for contract {0}: {1}", contractId, probeResponses);
    return probeResponses;
}
Also used : SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) IPolicyProbeResponse(io.apiman.gateway.engine.beans.IPolicyProbeResponse) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) ArrayList(java.util.ArrayList) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) NewContractBean(io.apiman.manager.api.beans.contracts.NewContractBean) ContractBean(io.apiman.manager.api.beans.contracts.ContractBean) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) GatewayBean(io.apiman.manager.api.beans.gateways.GatewayBean) OrganizationBean(io.apiman.manager.api.beans.orgs.OrganizationBean) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean)

Example 3 with GatewayAuthenticationException

use of io.apiman.manager.api.gateway.GatewayAuthenticationException in project apiman by apiman.

the class GatewayResourceImpl method test.

/**
 * @see IGatewayResource#test(io.apiman.manager.api.beans.gateways.NewGatewayBean)
 */
@Override
public GatewayTestResultBean test(NewGatewayBean gatewayToTest) throws NotAuthorizedException {
    securityContext.checkAdminPermissions();
    GatewayTestResultBean rval = new GatewayTestResultBean();
    try {
        GatewayBean testGateway = new GatewayBean();
        testGateway.setName(gatewayToTest.getName());
        testGateway.setType(gatewayToTest.getType());
        testGateway.setConfiguration(gatewayToTest.getConfiguration());
        IGatewayLink gatewayLink = gatewayLinkFactory.create(testGateway);
        SystemStatus status = gatewayLink.getStatus();
        String detail = MAPPER.writer().writeValueAsString(status);
        rval.setSuccess(true);
        rval.setDetail(detail);
    } catch (GatewayAuthenticationException e) {
        rval.setSuccess(false);
        // $NON-NLS-1$
        rval.setDetail(Messages.i18n.format("GatewayResourceImpl.AuthenticationFailed"));
    } catch (Exception e) {
        rval.setSuccess(false);
        rval.setDetail(e.getMessage());
    }
    return rval;
}
Also used : SystemStatus(io.apiman.gateway.engine.beans.SystemStatus) GatewayTestResultBean(io.apiman.manager.api.beans.summary.GatewayTestResultBean) NewGatewayBean(io.apiman.manager.api.beans.gateways.NewGatewayBean) GatewayBean(io.apiman.manager.api.beans.gateways.GatewayBean) UpdateGatewayBean(io.apiman.manager.api.beans.gateways.UpdateGatewayBean) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) GatewayNotFoundException(io.apiman.manager.api.rest.exceptions.GatewayNotFoundException) SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) GatewayAlreadyExistsException(io.apiman.manager.api.rest.exceptions.GatewayAlreadyExistsException) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException)

Example 4 with GatewayAuthenticationException

use of io.apiman.manager.api.gateway.GatewayAuthenticationException in project apiman by apiman.

the class GatewayClient method getApiEndpoint.

/**
 * @see IApiResource#getApiEndpoint(java.lang.String, java.lang.String, java.lang.String)
 */
public ApiEndpoint getApiEndpoint(String organizationId, String apiId, String version) throws GatewayAuthenticationException {
    InputStream is = null;
    try {
        @SuppressWarnings("nls") URI uri = new URI(this.endpoint + APIs + "/" + organizationId + "/" + apiId + "/" + version + "/endpoint");
        HttpGet get = new HttpGet(uri);
        HttpResponse response = httpClient.execute(get);
        int actualStatusCode = response.getStatusLine().getStatusCode();
        if (actualStatusCode == 401 || actualStatusCode == 403) {
            throw new GatewayAuthenticationException();
        }
        if (actualStatusCode != 200) {
            // $NON-NLS-1$
            throw new RuntimeException("Failed to get the API endpoint: " + actualStatusCode);
        }
        is = response.getEntity().getContent();
        return mapper.reader(ApiEndpoint.class).readValue(is);
    } catch (GatewayAuthenticationException | RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(is);
    }
}
Also used : InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) ApiEndpoint(io.apiman.gateway.engine.beans.ApiEndpoint) URI(java.net.URI) ApiEndpoint(io.apiman.gateway.engine.beans.ApiEndpoint) GatewayEndpoint(io.apiman.gateway.engine.beans.GatewayEndpoint) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) RegistrationException(io.apiman.gateway.engine.beans.exceptions.RegistrationException)

Example 5 with GatewayAuthenticationException

use of io.apiman.manager.api.gateway.GatewayAuthenticationException in project apiman by apiman.

the class GatewayClient method register.

/**
 * @see IClientResource#register(io.apiman.gateway.engine.beans.Client)
 */
public void register(Client client) throws RegistrationException, GatewayAuthenticationException {
    try {
        URI uri = new URI(this.endpoint + CLIENTS);
        HttpPut put = new HttpPut(uri);
        // $NON-NLS-1$ //$NON-NLS-2$
        put.setHeader("Content-Type", "application/json; charset=utf-8");
        String jsonPayload = mapper.writer().writeValueAsString(client);
        HttpEntity entity = new StringEntity(jsonPayload);
        put.setEntity(entity);
        HttpResponse response = httpClient.execute(put);
        int actualStatusCode = response.getStatusLine().getStatusCode();
        if (actualStatusCode == 401 || actualStatusCode == 403) {
            throw new GatewayAuthenticationException();
        }
        if (actualStatusCode == 500) {
            // $NON-NLS-1$
            Header[] headers = response.getHeaders("X-API-Gateway-Error");
            if (headers != null && headers.length > 0) {
                throw readRegistrationException(response);
            }
        }
        if (actualStatusCode >= 300) {
            // $NON-NLS-1$
            throw new RuntimeException(Messages.i18n.format("GatewayClient.ClientRegistrationFailed", actualStatusCode));
        }
    } catch (GatewayAuthenticationException | RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) ApiEndpoint(io.apiman.gateway.engine.beans.ApiEndpoint) GatewayEndpoint(io.apiman.gateway.engine.beans.GatewayEndpoint) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) RegistrationException(io.apiman.gateway.engine.beans.exceptions.RegistrationException) StringEntity(org.apache.http.entity.StringEntity) Header(org.apache.http.Header)

Aggregations

GatewayAuthenticationException (io.apiman.manager.api.gateway.GatewayAuthenticationException)12 ApiEndpoint (io.apiman.gateway.engine.beans.ApiEndpoint)10 GatewayEndpoint (io.apiman.gateway.engine.beans.GatewayEndpoint)8 PublishingException (io.apiman.gateway.engine.beans.exceptions.PublishingException)8 RegistrationException (io.apiman.gateway.engine.beans.exceptions.RegistrationException)8 HttpResponse (org.apache.http.HttpResponse)8 URI (java.net.URI)7 GatewayBean (io.apiman.manager.api.beans.gateways.GatewayBean)4 IGatewayLink (io.apiman.manager.api.gateway.IGatewayLink)4 SystemErrorException (io.apiman.manager.api.rest.exceptions.SystemErrorException)4 InputStream (java.io.InputStream)4 Header (org.apache.http.Header)4 StorageException (io.apiman.manager.api.core.exceptions.StorageException)3 HttpEntity (org.apache.http.HttpEntity)3 HttpGet (org.apache.http.client.methods.HttpGet)3 SystemStatus (io.apiman.gateway.engine.beans.SystemStatus)2 ApiGatewayBean (io.apiman.manager.api.beans.apis.ApiGatewayBean)2 ClientVersionBean (io.apiman.manager.api.beans.clients.ClientVersionBean)2 ApiEntryBean (io.apiman.manager.api.beans.summary.ApiEntryBean)2 ApiRegistryBean (io.apiman.manager.api.beans.summary.ApiRegistryBean)2