Search in sources :

Example 6 with Policy

use of io.apiman.gateway.engine.beans.Policy in project apiman by apiman.

the class SecureRegistryWrapper method decryptPolicies.

/**
 * @param policies
 */
protected void decryptPolicies(String orgId, String entityId, String entityVersion, EntityType entityType, List<Policy> policies) {
    if (policies != null) {
        DataEncryptionContext ctx = new DataEncryptionContext(orgId, entityId, entityVersion, entityType);
        for (Policy policy : policies) {
            String encryptedConfig = policy.getPolicyJsonConfig();
            policy.setPolicyJsonConfig(encrypter.decrypt(encryptedConfig, ctx));
        }
    }
}
Also used : Policy(io.apiman.gateway.engine.beans.Policy) DataEncryptionContext(io.apiman.common.util.crypt.DataEncryptionContext)

Example 7 with Policy

use of io.apiman.gateway.engine.beans.Policy in project apiman by apiman.

the class PolicyTester method publishApi.

/**
 * Publish a API configured with the correct policy and policy config.
 * @param method
 * @throws Throwable
 */
protected void publishApi(FrameworkMethod method) {
    version++;
    try {
        // Get the policy class under test.
        TestingPolicy tp = method.getMethod().getAnnotation(TestingPolicy.class);
        if (tp == null) {
            tp = getTestClass().getJavaClass().getAnnotation(TestingPolicy.class);
        }
        if (tp == null) {
            throw new Exception("Missing test annotation @TestingPolicy.");
        }
        Class<? extends IPolicy> policyUnderTest = tp.value();
        // Get the configuration JSON to use
        Configuration config = method.getMethod().getAnnotation(Configuration.class);
        if (config == null) {
            config = getTestClass().getJavaClass().getAnnotation(Configuration.class);
        }
        if (config == null) {
            throw new Exception("Missing test annotation @Configuration.");
        }
        // Get the back end API simulator to use
        BackEndApi backEnd = method.getMethod().getAnnotation(BackEndApi.class);
        if (backEnd == null) {
            backEnd = getTestClass().getJavaClass().getAnnotation(BackEndApi.class);
        }
        Class<? extends IPolicyTestBackEndApi> backEndApi;
        if (backEnd == null) {
            backEndApi = EchoBackEndApi.class;
        } else {
            backEndApi = backEnd.value();
        }
        final Set<Throwable> errorHolder = new HashSet<>();
        Policy policy = new Policy();
        policy.setPolicyImpl("class:" + policyUnderTest.getName());
        policy.setPolicyJsonConfig(getPolicyConfiguration(config));
        Api api = new Api();
        api.setEndpoint(backEndApi.getName());
        api.setEndpointType("TEST");
        api.setOrganizationId(orgId);
        api.setApiId(apiId);
        api.setVersion(String.valueOf(version));
        api.setPublicAPI(true);
        api.setApiPolicies(Collections.singletonList(policy));
        api.setParsePayload(true);
        getEngine().getRegistry().publishApi(api, new IAsyncResultHandler<Void>() {

            @Override
            public void handle(IAsyncResult<Void> result) {
                if (result.isError()) {
                    errorHolder.add(result.getError());
                }
            }
        });
        if (!errorHolder.isEmpty()) {
            throw errorHolder.iterator().next();
        }
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
}
Also used : IPolicy(io.apiman.gateway.engine.policy.IPolicy) Policy(io.apiman.gateway.engine.beans.Policy) IOException(java.io.IOException) Api(io.apiman.gateway.engine.beans.Api) HashSet(java.util.HashSet)

Example 8 with Policy

use of io.apiman.gateway.engine.beans.Policy in project apiman by apiman.

the class StorageImportDispatcher method publishApis.

/**
 * Publishes any apis that were imported in the "Published" state.
 * @throws StorageException
 */
private void publishApis() throws StorageException {
    // $NON-NLS-1$
    logger.info(Messages.i18n.format("StorageExporter.PublishingApis"));
    try {
        for (EntityInfo info : apisToPublish) {
            // $NON-NLS-1$
            logger.info(Messages.i18n.format("StorageExporter.PublishingApi", info));
            ApiVersionBean versionBean = storage.getApiVersion(info.organizationId, info.id, info.version);
            Api gatewayApi = new Api();
            gatewayApi.setEndpoint(versionBean.getEndpoint());
            gatewayApi.setEndpointType(versionBean.getEndpointType().toString());
            gatewayApi.setEndpointProperties(versionBean.getEndpointProperties());
            gatewayApi.setOrganizationId(versionBean.getApi().getOrganization().getId());
            gatewayApi.setApiId(versionBean.getApi().getId());
            gatewayApi.setVersion(versionBean.getVersion());
            gatewayApi.setPublicAPI(versionBean.isPublicAPI());
            gatewayApi.setParsePayload(versionBean.isParsePayload());
            if (versionBean.isPublicAPI()) {
                List<Policy> policiesToPublish = new ArrayList<>();
                Iterator<PolicyBean> apiPolicies = storage.getAllPolicies(info.organizationId, info.id, info.version, PolicyType.Api);
                while (apiPolicies.hasNext()) {
                    PolicyBean apiPolicy = apiPolicies.next();
                    Policy policyToPublish = new Policy();
                    policyToPublish.setPolicyJsonConfig(apiPolicy.getConfiguration());
                    policyToPublish.setPolicyImpl(apiPolicy.getDefinition().getPolicyImpl());
                    policiesToPublish.add(policyToPublish);
                }
                gatewayApi.setApiPolicies(policiesToPublish);
            }
            // Publish the api to all relevant gateways
            Set<ApiGatewayBean> gateways = versionBean.getGateways();
            if (gateways == null) {
                // $NON-NLS-1$
                throw new RuntimeException("No gateways specified for api!");
            }
            for (ApiGatewayBean apiGatewayBean : gateways) {
                IGatewayLink gatewayLink = createGatewayLink(apiGatewayBean.getGatewayId());
                gatewayLink.publishApi(gatewayApi);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Policy(io.apiman.gateway.engine.beans.Policy) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) ArrayList(java.util.ArrayList) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink) StorageException(io.apiman.manager.api.core.exceptions.StorageException) ImportNotNeededException(io.apiman.manager.api.exportimport.exceptions.ImportNotNeededException) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) Api(io.apiman.gateway.engine.beans.Api) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean)

Example 9 with Policy

use of io.apiman.gateway.engine.beans.Policy in project apiman by apiman.

the class SecureRegistryWrapper method encryptPolicies.

/**
 * @param entityType
 * @param entityVersion
 * @param entityId
 * @param orgId
 * @param entityType
 * @param policies
 */
protected void encryptPolicies(String orgId, String entityId, String entityVersion, EntityType entityType, List<Policy> policies) {
    if (policies != null) {
        DataEncryptionContext ctx = new DataEncryptionContext(orgId, entityId, entityVersion, entityType);
        for (Policy policy : policies) {
            String jsonConfig = policy.getPolicyJsonConfig();
            policy.setPolicyJsonConfig(encrypter.encrypt(jsonConfig, ctx));
        }
    }
}
Also used : Policy(io.apiman.gateway.engine.beans.Policy) DataEncryptionContext(io.apiman.common.util.crypt.DataEncryptionContext)

Example 10 with Policy

use of io.apiman.gateway.engine.beans.Policy in project apiman by apiman.

the class StorageImportDispatcher method aggregateContractPolicies.

/**
 * Aggregates the api, client, and plan policies into a single ordered list.
 * @param contractBean
 * @param clientInfo
 */
private List<Policy> aggregateContractPolicies(ContractBean contractBean, EntityInfo clientInfo) throws StorageException {
    List<Policy> policies = new ArrayList<>();
    PolicyType[] types = new PolicyType[] { PolicyType.Client, PolicyType.Plan, PolicyType.Api };
    for (PolicyType policyType : types) {
        String org, id, ver;
        switch(policyType) {
            case Client:
                {
                    org = clientInfo.organizationId;
                    id = clientInfo.id;
                    ver = clientInfo.version;
                    break;
                }
            case Plan:
                {
                    org = contractBean.getApi().getApi().getOrganization().getId();
                    id = contractBean.getPlan().getPlan().getId();
                    ver = contractBean.getPlan().getVersion();
                    break;
                }
            case Api:
                {
                    org = contractBean.getApi().getApi().getOrganization().getId();
                    id = contractBean.getApi().getApi().getId();
                    ver = contractBean.getApi().getVersion();
                    break;
                }
            default:
                {
                    // $NON-NLS-1$
                    throw new RuntimeException("Missing case for switch!");
                }
        }
        Iterator<PolicyBean> clientPolicies = storage.getAllPolicies(org, id, ver, policyType);
        while (clientPolicies.hasNext()) {
            PolicyBean policyBean = clientPolicies.next();
            Policy policy = new Policy();
            policy.setPolicyJsonConfig(policyBean.getConfiguration());
            policy.setPolicyImpl(policyBean.getDefinition().getPolicyImpl());
            policies.add(policy);
        }
    }
    return policies;
}
Also used : Policy(io.apiman.gateway.engine.beans.Policy) PolicyType(io.apiman.manager.api.beans.policies.PolicyType) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) ArrayList(java.util.ArrayList)

Aggregations

Policy (io.apiman.gateway.engine.beans.Policy)17 IPolicy (io.apiman.gateway.engine.policy.IPolicy)7 ArrayList (java.util.ArrayList)7 Api (io.apiman.gateway.engine.beans.Api)6 PolicyBean (io.apiman.manager.api.beans.policies.PolicyBean)6 StorageException (io.apiman.manager.api.core.exceptions.StorageException)5 IPolicyProbeResponse (io.apiman.gateway.engine.beans.IPolicyProbeResponse)4 PublishingException (io.apiman.gateway.engine.beans.exceptions.PublishingException)4 IPolicyFactory (io.apiman.gateway.engine.policy.IPolicyFactory)4 IPolicyProbe (io.apiman.gateway.engine.policy.IPolicyProbe)4 PolicyContextImpl (io.apiman.gateway.engine.policy.PolicyContextImpl)4 ProbeContext (io.apiman.gateway.engine.policy.ProbeContext)4 PolicySummaryBean (io.apiman.manager.api.beans.summary.PolicySummaryBean)4 NotAuthorizedException (io.apiman.manager.api.rest.exceptions.NotAuthorizedException)4 ApiGatewayBean (io.apiman.manager.api.beans.apis.ApiGatewayBean)3 ApiVersionBean (io.apiman.manager.api.beans.apis.ApiVersionBean)3 PolicyType (io.apiman.manager.api.beans.policies.PolicyType)3 IGatewayLink (io.apiman.manager.api.gateway.IGatewayLink)3 ActionException (io.apiman.manager.api.rest.exceptions.ActionException)3 ApiVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException)3