Skip to content

Commit

Permalink
policies can now be added/modified/removed from apps even when the ap…
Browse files Browse the repository at this point in the history
…p is registered
  • Loading branch information
EricWittmann committed Dec 1, 2015
1 parent f0bdfc5 commit 52cd5ca
Show file tree
Hide file tree
Showing 30 changed files with 497 additions and 162 deletions.
Expand Up @@ -208,7 +208,7 @@ public class OrganizationResourceImpl implements IOrganizationResource {
@Inject IApplicationValidator applicationValidator;
@Inject IServiceValidator serviceValidator;
@Inject IApiKeyGenerator apiKeyGenerator;

@Inject IDownloadManager downloadManager;

@Inject IUserResource users;
Expand Down Expand Up @@ -676,19 +676,19 @@ public AppUsagePerServiceBean getAppUsagePerService(String organizationId, Strin
InvalidMetricCriteriaException {
if (!securityContext.hasPermission(PermissionType.appView, organizationId))
throw ExceptionFactory.notAuthorizedException();

if (fromDate == null) {
throw ExceptionFactory.invalidMetricCriteriaException(Messages.i18n.format("MissingOrInvalidParam", "fromDate")); //$NON-NLS-1$ //$NON-NLS-2$
}

if (toDate == null) {
throw ExceptionFactory.invalidMetricCriteriaException(Messages.i18n.format("MissingOrInvalidParam", "toDate")); //$NON-NLS-1$ //$NON-NLS-2$
}

DateTime from = parseFromDate(fromDate);
DateTime to = parseToDate(toDate);
validateMetricRange(from, to);

return metrics.getAppUsagePerService(organizationId, applicationId, version, from, to);
}

Expand Down Expand Up @@ -964,7 +964,7 @@ public List<ContractSummaryBean> getApplicationVersionContracts(String organizat
throw new SystemErrorException(e);
}
}

/**
* @see io.apiman.manager.api.rest.contract.IOrganizationResource#getApiRegistryJSON(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
*/
Expand All @@ -984,7 +984,7 @@ public Response getApiRegistryJSON(String organizationId, String applicationId,
return getApiRegistryJSON(organizationId, applicationId, version, hasPermission);
}
}

/**
* @see io.apiman.manager.api.rest.contract.IOrganizationResource#getApiRegistryJSON(java.lang.String, java.lang.String, java.lang.String, boolean)
*/
Expand All @@ -996,7 +996,7 @@ public Response getApiRegistryJSON(String organizationId, String applicationId,
.header("Content-Disposition", "attachment; filename=api-registry.json") //$NON-NLS-1$ //$NON-NLS-2$
.build();
}

/**
* @see io.apiman.manager.api.rest.contract.IOrganizationResource#getApiRegistryXML(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
*/
Expand All @@ -1016,7 +1016,7 @@ public Response getApiRegistryXML(String organizationId, String applicationId, S
return getApiRegistryXML(organizationId, applicationId, version, hasPermission);
}
}

/**
* @see io.apiman.manager.api.rest.contract.IOrganizationResource#getApiRegistryXML(java.lang.String, java.lang.String, java.lang.String, boolean)
*/
Expand All @@ -1028,7 +1028,7 @@ public Response getApiRegistryXML(String organizationId, String applicationId, S
.header("Content-Disposition", "attachment; filename=api-registry.xml") //$NON-NLS-1$ //$NON-NLS-2$
.build();
}

/**
* Gets the API registry.
* @param organizationId
Expand Down Expand Up @@ -1104,12 +1104,6 @@ public PolicyBean createAppPolicy(String organizationId, String applicationId, S
if (!securityContext.hasPermission(PermissionType.appEdit, organizationId))
throw ExceptionFactory.notAuthorizedException();

// Make sure the app version exists and is in the right state.
ApplicationVersionBean avb = getAppVersion(organizationId, applicationId, version);
if (avb.getStatus() == ApplicationStatus.Registered || avb.getStatus() == ApplicationStatus.Retired) {
throw ExceptionFactory.invalidApplicationStatusException();
}

return doCreatePolicy(organizationId, applicationId, version, bean, PolicyType.Application);
}

Expand Down Expand Up @@ -1144,7 +1138,7 @@ public void updateAppPolicy(String organizationId, String applicationId, String
throw ExceptionFactory.notAuthorizedException();

// Make sure the app version exists.
getAppVersion(organizationId, applicationId, version);
ApplicationVersionBean avb = getAppVersion(organizationId, applicationId, version);

try {
storage.beginTx();
Expand All @@ -1154,12 +1148,15 @@ public void updateAppPolicy(String organizationId, String applicationId, String
}
if (AuditUtils.valueChanged(policy.getConfiguration(), bean.getConfiguration())) {
policy.setConfiguration(bean.getConfiguration());
// TODO figure out what changed an include that in the audit entry
// TODO figure out what changed and include that in the audit entry
}
policy.setModifiedOn(new Date());
policy.setModifiedBy(this.securityContext.getCurrentUser());
storage.updatePolicy(policy);
storage.createAuditEntry(AuditUtils.policyUpdated(policy, PolicyType.Application, securityContext));
avb.setModifiedBy(securityContext.getCurrentUser());
avb.setModifiedOn(new Date());
storage.updateApplicationVersion(avb);
storage.commitTx();
} catch (AbstractRestException e) {
storage.rollbackTx();
Expand All @@ -1181,10 +1178,7 @@ public void deleteAppPolicy(String organizationId, String applicationId, String
throw ExceptionFactory.notAuthorizedException();

// Make sure the app version exists;
ApplicationVersionBean app = getAppVersion(organizationId, applicationId, version);
if (app.getStatus() == ApplicationStatus.Registered || app.getStatus() == ApplicationStatus.Retired) {
throw ExceptionFactory.invalidApplicationStatusException();
}
ApplicationVersionBean avb = getAppVersion(organizationId, applicationId, version);

try {
storage.beginTx();
Expand All @@ -1194,6 +1188,9 @@ public void deleteAppPolicy(String organizationId, String applicationId, String
}
storage.deletePolicy(policy);
storage.createAuditEntry(AuditUtils.policyRemoved(policy, PolicyType.Application, securityContext));
avb.setModifiedBy(securityContext.getCurrentUser());
avb.setModifiedOn(new Date());
storage.updateApplicationVersion(avb);
storage.commitTx();
} catch (AbstractRestException e) {
storage.rollbackTx();
Expand Down Expand Up @@ -1614,7 +1611,7 @@ public ServiceVersionBean getServiceVersion(String organizationId, String servic
throw new SystemErrorException(e);
}
}

/**
* @see io.apiman.manager.api.rest.contract.IOrganizationResource#getServiceVersionStatus(java.lang.String, java.lang.String, java.lang.String)
*/
Expand Down Expand Up @@ -2247,14 +2244,14 @@ public UsageHistogramBean getUsage(String organizationId, String serviceId, Stri
if (fromDate == null) {
throw ExceptionFactory.invalidMetricCriteriaException(Messages.i18n.format("MissingOrInvalidParam", "fromDate")); //$NON-NLS-1$ //$NON-NLS-2$
}

if (toDate == null) {
throw ExceptionFactory.invalidMetricCriteriaException(Messages.i18n.format("MissingOrInvalidParam", "toDate")); //$NON-NLS-1$ //$NON-NLS-2$
}

DateTime from = parseFromDate(fromDate);
DateTime to = parseToDate(toDate);

if (interval == null) {
interval = HistogramIntervalType.day;
}
Expand All @@ -2271,11 +2268,11 @@ public UsagePerAppBean getUsagePerApp(String organizationId, String serviceId, S
String fromDate, String toDate) throws NotAuthorizedException, InvalidMetricCriteriaException {
if (!securityContext.hasPermission(PermissionType.svcView, organizationId))
throw ExceptionFactory.notAuthorizedException();

if (fromDate == null) {
throw ExceptionFactory.invalidMetricCriteriaException(Messages.i18n.format("MissingOrInvalidParam", "fromDate")); //$NON-NLS-1$ //$NON-NLS-2$
}

if (toDate == null) {
throw ExceptionFactory.invalidMetricCriteriaException(Messages.i18n.format("MissingOrInvalidParam", "toDate")); //$NON-NLS-1$ //$NON-NLS-2$
}
Expand All @@ -2294,11 +2291,11 @@ public UsagePerPlanBean getUsagePerPlan(String organizationId, String serviceId,
String fromDate, String toDate) throws NotAuthorizedException, InvalidMetricCriteriaException {
if (!securityContext.hasPermission(PermissionType.svcView, organizationId))
throw ExceptionFactory.notAuthorizedException();

if (fromDate == null) {
throw ExceptionFactory.invalidMetricCriteriaException(Messages.i18n.format("MissingOrInvalidParam", "fromDate")); //$NON-NLS-1$ //$NON-NLS-2$
}

if (toDate == null) {
throw ExceptionFactory.invalidMetricCriteriaException(Messages.i18n.format("MissingOrInvalidParam", "toDate")); //$NON-NLS-1$ //$NON-NLS-2$
}
Expand All @@ -2318,11 +2315,11 @@ public ResponseStatsHistogramBean getResponseStats(String organizationId, String
throws NotAuthorizedException, InvalidMetricCriteriaException {
if (!securityContext.hasPermission(PermissionType.svcView, organizationId))
throw ExceptionFactory.notAuthorizedException();

if (fromDate == null) {
throw ExceptionFactory.invalidMetricCriteriaException(Messages.i18n.format("MissingOrInvalidParam", "fromDate")); //$NON-NLS-1$ //$NON-NLS-2$
}

if (toDate == null) {
throw ExceptionFactory.invalidMetricCriteriaException(Messages.i18n.format("MissingOrInvalidParam", "toDate")); //$NON-NLS-1$ //$NON-NLS-2$
}
Expand All @@ -2346,11 +2343,11 @@ public ResponseStatsSummaryBean getResponseStatsSummary(String organizationId, S
InvalidMetricCriteriaException {
if (!securityContext.hasPermission(PermissionType.svcView, organizationId))
throw ExceptionFactory.notAuthorizedException();

if (fromDate == null) {
throw ExceptionFactory.invalidMetricCriteriaException(Messages.i18n.format("MissingOrInvalidParam", "fromDate")); //$NON-NLS-1$ //$NON-NLS-2$
}

if (toDate == null) {
throw ExceptionFactory.invalidMetricCriteriaException(Messages.i18n.format("MissingOrInvalidParam", "toDate")); //$NON-NLS-1$ //$NON-NLS-2$
}
Expand All @@ -2370,11 +2367,11 @@ public ResponseStatsPerAppBean getResponseStatsPerApp(String organizationId, Str
InvalidMetricCriteriaException {
if (!securityContext.hasPermission(PermissionType.svcView, organizationId))
throw ExceptionFactory.notAuthorizedException();

if (fromDate == null) {
throw ExceptionFactory.invalidMetricCriteriaException(Messages.i18n.format("MissingOrInvalidParam", "fromDate")); //$NON-NLS-1$ //$NON-NLS-2$
}

if (toDate == null) {
throw ExceptionFactory.invalidMetricCriteriaException(Messages.i18n.format("MissingOrInvalidParam", "toDate")); //$NON-NLS-1$ //$NON-NLS-2$
}
Expand All @@ -2394,11 +2391,11 @@ public ResponseStatsPerPlanBean getResponseStatsPerPlan(String organizationId, S
InvalidMetricCriteriaException {
if (!securityContext.hasPermission(PermissionType.svcView, organizationId))
throw ExceptionFactory.notAuthorizedException();

if (fromDate == null) {
throw ExceptionFactory.invalidMetricCriteriaException(Messages.i18n.format("MissingOrInvalidParam", "fromDate")); //$NON-NLS-1$ //$NON-NLS-2$
}

if (toDate == null) {
throw ExceptionFactory.invalidMetricCriteriaException(Messages.i18n.format("MissingOrInvalidParam", "toDate")); //$NON-NLS-1$ //$NON-NLS-2$
}
Expand Down Expand Up @@ -2889,12 +2886,10 @@ protected PolicyBean doCreatePolicy(String organizationId, String entityId, Stri
if (def == null) {
throw ExceptionFactory.policyDefNotFoundException(bean.getDefinitionId());
}
storage.commitTx();
} catch (AbstractRestException e) {
storage.rollbackTx();
} catch (AbstractRestException e) {
throw e;
} catch (Exception e) {
storage.rollbackTx();
throw new SystemErrorException(e);
}

Expand Down Expand Up @@ -2922,6 +2917,24 @@ protected PolicyBean doCreatePolicy(String organizationId, String entityId, Stri
policy.setOrderIndex(newIdx);

storage.beginTx();

if (type == PolicyType.Application) {
ApplicationVersionBean avb = storage.getApplicationVersion(organizationId, entityId, entityVersion);
avb.setModifiedBy(securityContext.getCurrentUser());
avb.setModifiedOn(new Date());
storage.updateApplicationVersion(avb);
} else if (type == PolicyType.Service) {
ServiceVersionBean svb = storage.getServiceVersion(organizationId, entityId, entityVersion);
svb.setModifiedBy(securityContext.getCurrentUser());
svb.setModifiedOn(new Date());
storage.updateServiceVersion(svb);
} else if (type == PolicyType.Plan) {
PlanVersionBean pvb = storage.getPlanVersion(organizationId, entityId, entityVersion);
pvb.setModifiedBy(securityContext.getCurrentUser());
pvb.setModifiedOn(new Date());
storage.updatePlanVersion(pvb);
}

storage.createPolicy(policy);
storage.createAuditEntry(AuditUtils.policyAdded(policy, type, securityContext));
storage.commitTx();
Expand Down Expand Up @@ -2993,7 +3006,7 @@ public void revoke(String organizationId, String roleId, String userId)

MembershipData auditData = new MembershipData();
auditData.setUserId(userId);

try {
storage.beginTx();
storage.deleteMembership(userId, roleId, organizationId);
Expand Down
@@ -0,0 +1,46 @@
/*
* Copyright 2014 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.apiman.manager.test;

import io.apiman.manager.test.junit.ManagerRestTestGatewayLog;
import io.apiman.manager.test.junit.ManagerRestTestPlan;
import io.apiman.manager.test.junit.ManagerRestTestPublishPayload;
import io.apiman.manager.test.junit.ManagerRestTester;

import org.junit.runner.RunWith;

/**
* Runs the "reregister-app" test plan.
*
* @author eric.wittmann@redhat.com
*/
@RunWith(ManagerRestTester.class)
@ManagerRestTestPlan("test-plans/reregister-app-testPlan.xml")
@ManagerRestTestGatewayLog(
"GET:/mock-gateway/system/status\n" +
"PUT:/mock-gateway/services\n" +
"GET:/mock-gateway/system/status\n" +
"PUT:/mock-gateway/applications\n" +
"GET:/mock-gateway/system/status\n" +
"PUT:/mock-gateway/applications\n" +
""
)
@ManagerRestTestPublishPayload({
"",
"{\"publicService\":false,\"organizationId\":\"Organization\",\"serviceId\":\"Service\",\"version\":\"1.0\",\"endpointType\":\"rest\",\"endpoint\":\"http://localhost:9999/echo\",\"endpointProperties\":{},\"servicePolicies\":[]}"
})
public class ReregisterAppTest {
}
@@ -0,0 +1,12 @@
POST /organizations admin/admin
Content-Type: application/json

{"name":"Organization"}
----
200
Content-Type: application/json

{
"name" : "Organization",
"id" : "Organization"
}
@@ -0,0 +1,32 @@
POST /policyDefs admin/admin
Content-Type: application/json

{
"name" : "Policy Def",
"description" : "This is the test policy def.",
"policyImpl" : "org.example.policies.PolicyDef",
"icon" : "policy-def",
"templates" : [
{
"language" : null,
"template" : "This is a policy that does nothing but test."
}
]
}
----
200
Content-Type: application/json

{
"id" : "PolicyDef",
"name" : "Policy Def",
"description" : "This is the test policy def.",
"policyImpl" : "org.example.policies.PolicyDef",
"icon" : "policy-def",
"templates" : [
{
"language" : null,
"template" : "This is a policy that does nothing but test."
}
]
}

0 comments on commit 52cd5ca

Please sign in to comment.