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));
}
}
}
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);
}
}
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);
}
}
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));
}
}
}
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;
}
Aggregations