Search in sources :

Example 6 with ApiEntity

use of io.gravitee.management.model.ApiEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiManager method undeploy.

public void undeploy(String apiId) {
    ApiEntity currentApi = apis.remove(apiId);
    if (currentApi != null) {
        logger.info("Undeployment of {}", currentApi);
        eventManager.publishEvent(ApiEvent.UNDEPLOY, currentApi);
        logger.info("{} has been undeployed", apiId);
    }
}
Also used : ApiEntity(io.gravitee.management.model.ApiEntity)

Example 7 with ApiEntity

use of io.gravitee.management.model.ApiEntity in project gravitee-management-rest-api by gravitee-io.

the class DynamicPropertyUpdater method update.

private void update(Collection<DynamicProperty> dynamicProperties) {
    // Get latest changes
    ApiEntity latestApi = apiService.findById(api.getId());
    List<Property> properties = (latestApi.getProperties() != null) ? latestApi.getProperties().getProperties() : Collections.emptyList();
    List<Property> userDefinedProperties = properties.stream().filter(property -> !property.isDynamic()).collect(Collectors.toList());
    Map<String, Property> propertyMap = properties.stream().collect(Collectors.toMap(Property::getKey, property -> property));
    List<Property> updatedProperties = new ArrayList<>();
    boolean needToBeSaved = false;
    for (DynamicProperty dynamicProperty : dynamicProperties) {
        Property property = propertyMap.get(dynamicProperty.getKey());
        if (property == null || property.isDynamic()) {
            updatedProperties.add(dynamicProperty);
        }
        // save properties only if there's something new
        if (property == null || (property.isDynamic() && !property.getValue().equals(dynamicProperty.getValue()))) {
            needToBeSaved = true;
        }
    }
    if (needToBeSaved) {
        // Add previous user-defined properties
        updatedProperties.addAll(userDefinedProperties);
        // Sort properties alphabetically to avoid redeploy if just the order has changed.
        List<Property> sortedUpdatedProperties = updatedProperties.stream().sorted(Comparator.comparing(Property::getKey)).collect(Collectors.toList());
        // Create properties container
        Properties apiProperties = new Properties();
        try {
            apiProperties.setProperties(sortedUpdatedProperties);
        } catch (RuntimeException e) {
            logger.error(e.getMessage(), e);
        }
        latestApi.setProperties(apiProperties);
        boolean isSync = apiService.isSynchronized(api.getId());
        // Update API
        apiService.update(latestApi.getId(), convert(latestApi));
        // Do not deploy if there are manual changes to push
        if (isSync) {
            // Publish API only in case of changes
            if (!updatedProperties.containsAll(properties) || !properties.containsAll(updatedProperties)) {
                apiService.deploy(latestApi.getId(), "dynamic-property-updater", EventType.PUBLISH_API);
            }
        }
    }
}
Also used : DynamicProperty(io.gravitee.management.services.dynamicproperties.model.DynamicProperty) java.util(java.util) Logger(org.slf4j.Logger) Properties(io.gravitee.definition.model.Properties) ApiEntity(io.gravitee.management.model.ApiEntity) ApiService(io.gravitee.management.service.ApiService) Property(io.gravitee.definition.model.Property) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) UpdateApiEntity(io.gravitee.management.model.UpdateApiEntity) Provider(io.gravitee.management.services.dynamicproperties.provider.Provider) EventType(io.gravitee.management.model.EventType) Handler(io.vertx.core.Handler) DynamicProperty(io.gravitee.management.services.dynamicproperties.model.DynamicProperty) ApiEntity(io.gravitee.management.model.ApiEntity) UpdateApiEntity(io.gravitee.management.model.UpdateApiEntity) Properties(io.gravitee.definition.model.Properties) DynamicProperty(io.gravitee.management.services.dynamicproperties.model.DynamicProperty) Property(io.gravitee.definition.model.Property)

Example 8 with ApiEntity

use of io.gravitee.management.model.ApiEntity in project gravitee-management-rest-api by gravitee-io.

the class PermissionFilterTest method shouldBeAuthorizedWhenApiPermissions.

@Test
public void shouldBeAuthorizedWhenApiPermissions() {
    ApiEntity api = initApiMocks();
    when(roleService.hasPermission(any(), any(), any())).thenReturn(true);
    permissionFilter.filter(permissions, containerRequestContext);
    verify(apiService, times(1)).findById(api.getId());
    verify(applicationService, never()).findById(any());
    verify(roleService, times(1)).hasPermission(any(), any(), any());
    verify(membershipService, times(1)).getMemberPermissions(api, USERNAME);
    verify(membershipService, never()).getRole(any(), any(), any(), any());
}
Also used : ApiEntity(io.gravitee.management.model.ApiEntity) Test(org.junit.Test)

Example 9 with ApiEntity

use of io.gravitee.management.model.ApiEntity in project gravitee-management-rest-api by gravitee-io.

the class PermissionFilterTest method shouldThrowForbiddenExceptionWhenNoApiPermissions.

@Test(expected = ForbiddenAccessException.class)
public void shouldThrowForbiddenExceptionWhenNoApiPermissions() {
    ApiEntity api = initApiMocks();
    when(roleService.hasPermission(any(), any(), any())).thenReturn(false);
    try {
        permissionFilter.filter(permissions, containerRequestContext);
    } catch (ForbiddenAccessException e) {
        verify(apiService, times(1)).findById(api.getId());
        verify(applicationService, never()).findById(any());
        verify(roleService, times(1)).hasPermission(any(), any(), any());
        verify(membershipService, times(1)).getMemberPermissions(api, USERNAME);
        verify(membershipService, never()).getRole(any(), any(), any(), any());
        throw e;
    }
    Assert.fail("Should throw a ForbiddenAccessException");
}
Also used : ApiEntity(io.gravitee.management.model.ApiEntity) ForbiddenAccessException(io.gravitee.management.service.exceptions.ForbiddenAccessException) Test(org.junit.Test)

Example 10 with ApiEntity

use of io.gravitee.management.model.ApiEntity in project gravitee-management-rest-api by gravitee-io.

the class PermissionFilterTest method initApiMocks.

/**
 * API Tests
 */
private ApiEntity initApiMocks() {
    ApiEntity api = new ApiEntity();
    api.setId(API_ID);
    Principal user = () -> USERNAME;
    when(apiService.findById(api.getId())).thenReturn(api);
    when(securityContext.getUserPrincipal()).thenReturn(user);
    Permission perm = mock(Permission.class);
    when(perm.value()).thenReturn(RolePermission.API_ANALYTICS);
    when(perm.acls()).thenReturn(new RolePermissionAction[] { RolePermissionAction.UPDATE });
    when(permissions.value()).thenReturn(new Permission[] { perm });
    UriInfo uriInfo = mock(UriInfo.class);
    MultivaluedHashMap<String, String> map = new MultivaluedHashMap<>();
    map.put("api", Collections.singletonList(api.getId()));
    when(uriInfo.getPathParameters()).thenReturn(map);
    when(containerRequestContext.getUriInfo()).thenReturn(uriInfo);
    return api;
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) RolePermission(io.gravitee.management.model.permissions.RolePermission) Permission(io.gravitee.management.rest.security.Permission) ApiEntity(io.gravitee.management.model.ApiEntity) Principal(java.security.Principal) UriInfo(javax.ws.rs.core.UriInfo)

Aggregations

ApiEntity (io.gravitee.management.model.ApiEntity)50 Test (org.junit.Test)34 Response (javax.ws.rs.core.Response)19 UpdateApiEntity (io.gravitee.management.model.UpdateApiEntity)13 PageEntity (io.gravitee.management.model.PageEntity)5 NewApiEntity (io.gravitee.management.model.NewApiEntity)4 NewTopApiEntity (io.gravitee.management.model.NewTopApiEntity)3 TopApiEntity (io.gravitee.management.model.TopApiEntity)3 UpdateTopApiEntity (io.gravitee.management.model.UpdateTopApiEntity)3 Permission (io.gravitee.management.rest.security.Permission)3 HashMap (java.util.HashMap)3 Collectors (java.util.stream.Collectors)3 Proxy (io.gravitee.definition.model.Proxy)2 ApplicationEntity (io.gravitee.management.model.ApplicationEntity)2 MemberEntity (io.gravitee.management.model.MemberEntity)2 SubscriptionEntity (io.gravitee.management.model.SubscriptionEntity)2 UserEntity (io.gravitee.management.model.UserEntity)2 RolePermission (io.gravitee.management.model.permissions.RolePermission)2 RolePermissionAction (io.gravitee.management.model.permissions.RolePermissionAction)2 SubscriptionQuery (io.gravitee.management.model.subscription.SubscriptionQuery)2