@Test public void testScopePrivilegeCheck() throws Exception { SecurityContextHolder.getContext().setAuthentication( TestAuthenticationFactory.createServiceOperator()); AmbariManagementController amc = createMock(AmbariManagementController.class); Clusters clusters = createMock(Clusters.class); Cluster cluster = createMock(Cluster.class); expect(amc.getClusters()).andReturn(clusters).atLeastOnce(); expect(clusters.getCluster((String) anyObject())).andReturn(cluster).atLeastOnce(); expect(cluster.getClusterId()).andReturn(Long.valueOf(1)).atLeastOnce(); Capture<WidgetEntity> entityCapture = EasyMock.newCapture(); dao.create(capture(entityCapture)); expectLastCall(); replay(amc, clusters, cluster, dao); PowerMock.replayAll(); Map<String, Object> requestProps = new HashMap<>(); requestProps.put(WidgetResourceProvider.WIDGET_CLUSTER_NAME_PROPERTY_ID, "c1"); requestProps.put(WidgetResourceProvider.WIDGET_WIDGET_NAME_PROPERTY_ID, "widget name"); requestProps.put(WidgetResourceProvider.WIDGET_WIDGET_TYPE_PROPERTY_ID, "GAUGE"); requestProps.put(WidgetResourceProvider.WIDGET_AUTHOR_PROPERTY_ID, "admin"); requestProps.put(WidgetResourceProvider.WIDGET_SCOPE_PROPERTY_ID, "CLUSTER"); Request request = PropertyHelper.getCreateRequest( Collections.singleton(requestProps), null); try { WidgetResourceProvider widgetResourceProvider = createProvider(amc); widgetResourceProvider.createResources(request); } catch (AccessDeniedException ex) { } }
@Override public RequestStatus createResources(final Request request) throws SystemException, UnsupportedPropertyException, ResourceAlreadyExistsException, NoSuchParentResourceException { Set<Resource> associatedResources = new HashSet<Resource>(); for (final Map<String, Object> properties : request.getProperties()) { WidgetEntity widgetEntity = createResources(new Command<WidgetEntity>() { @Override public WidgetEntity invoke() throws AmbariException { final String[] requiredProperties = { WIDGET_CLUSTER_NAME_PROPERTY_ID, WIDGET_WIDGET_NAME_PROPERTY_ID, WIDGET_WIDGET_TYPE_PROPERTY_ID, WIDGET_SCOPE_PROPERTY_ID }; for (String propertyName: requiredProperties) { if (properties.get(propertyName) == null) { throw new AmbariException("Property " + propertyName + " should be provided"); } } final WidgetEntity entity = new WidgetEntity(); String clusterName = properties.get(WIDGET_CLUSTER_NAME_PROPERTY_ID).toString(); String scope = properties.get(WIDGET_SCOPE_PROPERTY_ID).toString(); if (!isScopeAllowedForUser(scope)) { throw new AccessDeniedException("Only cluster operator can create widgets with cluster scope"); } entity.setWidgetName(properties.get(WIDGET_WIDGET_NAME_PROPERTY_ID).toString()); entity.setWidgetType(properties.get(WIDGET_WIDGET_TYPE_PROPERTY_ID).toString()); entity.setClusterId(getManagementController().getClusters().getCluster(clusterName).getClusterId()); entity.setScope(scope); String metrics = (properties.containsKey(WIDGET_METRICS_PROPERTY_ID)) ? gson.toJson(properties.get(WIDGET_METRICS_PROPERTY_ID)) : null; entity.setMetrics(metrics); entity.setAuthor(getAuthorName(properties)); String description = (properties.containsKey(WIDGET_DESCRIPTION_PROPERTY_ID)) ? properties.get(WIDGET_DESCRIPTION_PROPERTY_ID).toString() : null; entity.setDescription(description); String values = (properties.containsKey(WIDGET_VALUES_PROPERTY_ID)) ? gson.toJson(properties.get(WIDGET_VALUES_PROPERTY_ID)) : null; entity.setWidgetValues(values); Map<String, Object> widgetPropertiesMap = new HashMap<String, Object>(); for (Map.Entry<String, Object> entry : properties.entrySet()) { if (PropertyHelper.getPropertyCategory(entry.getKey()).equals(WIDGET_PROPERTIES_PROPERTY_ID)) { widgetPropertiesMap.put(PropertyHelper.getPropertyName(entry.getKey()), entry.getValue()); } } String widgetProperties = (widgetPropertiesMap.isEmpty()) ? null : gson.toJson(widgetPropertiesMap); entity.setProperties(widgetProperties); widgetDAO.create(entity); notifyCreate(Type.Widget, request); return entity; } }); Resource resource = new ResourceImpl(Type.Widget); resource.setProperty(WIDGET_ID_PROPERTY_ID, widgetEntity.getId()); associatedResources.add(resource); } return getRequestStatus(null, associatedResources); }
WidgetResourceProvider extends AbstractControllerResourceProvider { @Override public RequestStatus createResources(final Request request) throws SystemException, UnsupportedPropertyException, ResourceAlreadyExistsException, NoSuchParentResourceException { Set<Resource> associatedResources = new HashSet<Resource>(); for (final Map<String, Object> properties : request.getProperties()) { WidgetEntity widgetEntity = createResources(new Command<WidgetEntity>() { @Override public WidgetEntity invoke() throws AmbariException { final String[] requiredProperties = { WIDGET_CLUSTER_NAME_PROPERTY_ID, WIDGET_WIDGET_NAME_PROPERTY_ID, WIDGET_WIDGET_TYPE_PROPERTY_ID, WIDGET_SCOPE_PROPERTY_ID }; for (String propertyName: requiredProperties) { if (properties.get(propertyName) == null) { throw new AmbariException("Property " + propertyName + " should be provided"); } } final WidgetEntity entity = new WidgetEntity(); String clusterName = properties.get(WIDGET_CLUSTER_NAME_PROPERTY_ID).toString(); String scope = properties.get(WIDGET_SCOPE_PROPERTY_ID).toString(); if (!isScopeAllowedForUser(scope)) { throw new AccessDeniedException("Only cluster operator can create widgets with cluster scope"); } entity.setWidgetName(properties.get(WIDGET_WIDGET_NAME_PROPERTY_ID).toString()); entity.setWidgetType(properties.get(WIDGET_WIDGET_TYPE_PROPERTY_ID).toString()); entity.setClusterId(getManagementController().getClusters().getCluster(clusterName).getClusterId()); entity.setScope(scope); String metrics = (properties.containsKey(WIDGET_METRICS_PROPERTY_ID)) ? gson.toJson(properties.get(WIDGET_METRICS_PROPERTY_ID)) : null; entity.setMetrics(metrics); entity.setAuthor(getAuthorName(properties)); String description = (properties.containsKey(WIDGET_DESCRIPTION_PROPERTY_ID)) ? properties.get(WIDGET_DESCRIPTION_PROPERTY_ID).toString() : null; entity.setDescription(description); String values = (properties.containsKey(WIDGET_VALUES_PROPERTY_ID)) ? gson.toJson(properties.get(WIDGET_VALUES_PROPERTY_ID)) : null; entity.setWidgetValues(values); Map<String, Object> widgetPropertiesMap = new HashMap<String, Object>(); for (Map.Entry<String, Object> entry : properties.entrySet()) { if (PropertyHelper.getPropertyCategory(entry.getKey()).equals(WIDGET_PROPERTIES_PROPERTY_ID)) { widgetPropertiesMap.put(PropertyHelper.getPropertyName(entry.getKey()), entry.getValue()); } } String widgetProperties = (widgetPropertiesMap.isEmpty()) ? null : gson.toJson(widgetPropertiesMap); entity.setProperties(widgetProperties); widgetDAO.create(entity); notifyCreate(Type.Widget, request); return entity; } }); Resource resource = new ResourceImpl(Type.Widget); resource.setProperty(WIDGET_ID_PROPERTY_ID, widgetEntity.getId()); associatedResources.add(resource); } return getRequestStatus(null, associatedResources); } }
WidgetResourceProvider extends AbstractControllerResourceProvider { @Override public RequestStatus createResources(final Request request) throws SystemException, UnsupportedPropertyException, ResourceAlreadyExistsException, NoSuchParentResourceException { Set<Resource> associatedResources = new HashSet<Resource>(); for (final Map<String, Object> properties : request.getProperties()) { WidgetEntity widgetEntity = createResources(new Command<WidgetEntity>() { @Override public WidgetEntity invoke() throws AmbariException { final String[] requiredProperties = { WIDGET_CLUSTER_NAME_PROPERTY_ID, WIDGET_WIDGET_NAME_PROPERTY_ID, WIDGET_WIDGET_TYPE_PROPERTY_ID, WIDGET_SCOPE_PROPERTY_ID }; for (String propertyName: requiredProperties) { if (properties.get(propertyName) == null) { throw new AmbariException("Property " + propertyName + " should be provided"); } } final WidgetEntity entity = new WidgetEntity(); String clusterName = properties.get(WIDGET_CLUSTER_NAME_PROPERTY_ID).toString(); String scope = properties.get(WIDGET_SCOPE_PROPERTY_ID).toString(); if (!isScopeAllowedForUser(scope)) { throw new AccessDeniedException("Only cluster operator can create widgets with cluster scope"); } entity.setWidgetName(properties.get(WIDGET_WIDGET_NAME_PROPERTY_ID).toString()); entity.setWidgetType(properties.get(WIDGET_WIDGET_TYPE_PROPERTY_ID).toString()); entity.setClusterId(getManagementController().getClusters().getCluster(clusterName).getClusterId()); entity.setScope(scope); String metrics = (properties.containsKey(WIDGET_METRICS_PROPERTY_ID)) ? gson.toJson(properties.get(WIDGET_METRICS_PROPERTY_ID)) : null; entity.setMetrics(metrics); entity.setAuthor(getAuthorName(properties)); String description = (properties.containsKey(WIDGET_DESCRIPTION_PROPERTY_ID)) ? properties.get(WIDGET_DESCRIPTION_PROPERTY_ID).toString() : null; entity.setDescription(description); String values = (properties.containsKey(WIDGET_VALUES_PROPERTY_ID)) ? gson.toJson(properties.get(WIDGET_VALUES_PROPERTY_ID)) : null; entity.setWidgetValues(values); Map<String, Object> widgetPropertiesMap = new HashMap<String, Object>(); for (Map.Entry<String, Object> entry : properties.entrySet()) { if (PropertyHelper.getPropertyCategory(entry.getKey()).equals(WIDGET_PROPERTIES_PROPERTY_ID)) { widgetPropertiesMap.put(PropertyHelper.getPropertyName(entry.getKey()), entry.getValue()); } } String widgetProperties = (widgetPropertiesMap.isEmpty()) ? null : gson.toJson(widgetPropertiesMap); entity.setProperties(widgetProperties); widgetDAO.create(entity); notifyCreate(Type.Widget, request); return entity; } }); Resource resource = new ResourceImpl(Type.Widget); resource.setProperty(WIDGET_ID_PROPERTY_ID, widgetEntity.getId()); associatedResources.add(resource); } return getRequestStatus(null, associatedResources); } WidgetResourceProvider(AmbariManagementController managementController); }
WidgetResourceProvider extends AbstractControllerResourceProvider { @Override public RequestStatus createResources(final Request request) throws SystemException, UnsupportedPropertyException, ResourceAlreadyExistsException, NoSuchParentResourceException { Set<Resource> associatedResources = new HashSet<Resource>(); for (final Map<String, Object> properties : request.getProperties()) { WidgetEntity widgetEntity = createResources(new Command<WidgetEntity>() { @Override public WidgetEntity invoke() throws AmbariException { final String[] requiredProperties = { WIDGET_CLUSTER_NAME_PROPERTY_ID, WIDGET_WIDGET_NAME_PROPERTY_ID, WIDGET_WIDGET_TYPE_PROPERTY_ID, WIDGET_SCOPE_PROPERTY_ID }; for (String propertyName: requiredProperties) { if (properties.get(propertyName) == null) { throw new AmbariException("Property " + propertyName + " should be provided"); } } final WidgetEntity entity = new WidgetEntity(); String clusterName = properties.get(WIDGET_CLUSTER_NAME_PROPERTY_ID).toString(); String scope = properties.get(WIDGET_SCOPE_PROPERTY_ID).toString(); if (!isScopeAllowedForUser(scope)) { throw new AccessDeniedException("Only cluster operator can create widgets with cluster scope"); } entity.setWidgetName(properties.get(WIDGET_WIDGET_NAME_PROPERTY_ID).toString()); entity.setWidgetType(properties.get(WIDGET_WIDGET_TYPE_PROPERTY_ID).toString()); entity.setClusterId(getManagementController().getClusters().getCluster(clusterName).getClusterId()); entity.setScope(scope); String metrics = (properties.containsKey(WIDGET_METRICS_PROPERTY_ID)) ? gson.toJson(properties.get(WIDGET_METRICS_PROPERTY_ID)) : null; entity.setMetrics(metrics); entity.setAuthor(getAuthorName(properties)); String description = (properties.containsKey(WIDGET_DESCRIPTION_PROPERTY_ID)) ? properties.get(WIDGET_DESCRIPTION_PROPERTY_ID).toString() : null; entity.setDescription(description); String values = (properties.containsKey(WIDGET_VALUES_PROPERTY_ID)) ? gson.toJson(properties.get(WIDGET_VALUES_PROPERTY_ID)) : null; entity.setWidgetValues(values); Map<String, Object> widgetPropertiesMap = new HashMap<String, Object>(); for (Map.Entry<String, Object> entry : properties.entrySet()) { if (PropertyHelper.getPropertyCategory(entry.getKey()).equals(WIDGET_PROPERTIES_PROPERTY_ID)) { widgetPropertiesMap.put(PropertyHelper.getPropertyName(entry.getKey()), entry.getValue()); } } String widgetProperties = (widgetPropertiesMap.isEmpty()) ? null : gson.toJson(widgetPropertiesMap); entity.setProperties(widgetProperties); widgetDAO.create(entity); notifyCreate(Type.Widget, request); return entity; } }); Resource resource = new ResourceImpl(Type.Widget); resource.setProperty(WIDGET_ID_PROPERTY_ID, widgetEntity.getId()); associatedResources.add(resource); } return getRequestStatus(null, associatedResources); } WidgetResourceProvider(AmbariManagementController managementController); @Override RequestStatus createResources(final Request request); @Override Set<Resource> getResources(Request request, Predicate predicate); @Override RequestStatus updateResources(Request request, Predicate predicate); @Override RequestStatus deleteResources(Request request, Predicate predicate); }
WidgetResourceProvider extends AbstractControllerResourceProvider { @Override public RequestStatus createResources(final Request request) throws SystemException, UnsupportedPropertyException, ResourceAlreadyExistsException, NoSuchParentResourceException { Set<Resource> associatedResources = new HashSet<Resource>(); for (final Map<String, Object> properties : request.getProperties()) { WidgetEntity widgetEntity = createResources(new Command<WidgetEntity>() { @Override public WidgetEntity invoke() throws AmbariException { final String[] requiredProperties = { WIDGET_CLUSTER_NAME_PROPERTY_ID, WIDGET_WIDGET_NAME_PROPERTY_ID, WIDGET_WIDGET_TYPE_PROPERTY_ID, WIDGET_SCOPE_PROPERTY_ID }; for (String propertyName: requiredProperties) { if (properties.get(propertyName) == null) { throw new AmbariException("Property " + propertyName + " should be provided"); } } final WidgetEntity entity = new WidgetEntity(); String clusterName = properties.get(WIDGET_CLUSTER_NAME_PROPERTY_ID).toString(); String scope = properties.get(WIDGET_SCOPE_PROPERTY_ID).toString(); if (!isScopeAllowedForUser(scope)) { throw new AccessDeniedException("Only cluster operator can create widgets with cluster scope"); } entity.setWidgetName(properties.get(WIDGET_WIDGET_NAME_PROPERTY_ID).toString()); entity.setWidgetType(properties.get(WIDGET_WIDGET_TYPE_PROPERTY_ID).toString()); entity.setClusterId(getManagementController().getClusters().getCluster(clusterName).getClusterId()); entity.setScope(scope); String metrics = (properties.containsKey(WIDGET_METRICS_PROPERTY_ID)) ? gson.toJson(properties.get(WIDGET_METRICS_PROPERTY_ID)) : null; entity.setMetrics(metrics); entity.setAuthor(getAuthorName(properties)); String description = (properties.containsKey(WIDGET_DESCRIPTION_PROPERTY_ID)) ? properties.get(WIDGET_DESCRIPTION_PROPERTY_ID).toString() : null; entity.setDescription(description); String values = (properties.containsKey(WIDGET_VALUES_PROPERTY_ID)) ? gson.toJson(properties.get(WIDGET_VALUES_PROPERTY_ID)) : null; entity.setWidgetValues(values); Map<String, Object> widgetPropertiesMap = new HashMap<String, Object>(); for (Map.Entry<String, Object> entry : properties.entrySet()) { if (PropertyHelper.getPropertyCategory(entry.getKey()).equals(WIDGET_PROPERTIES_PROPERTY_ID)) { widgetPropertiesMap.put(PropertyHelper.getPropertyName(entry.getKey()), entry.getValue()); } } String widgetProperties = (widgetPropertiesMap.isEmpty()) ? null : gson.toJson(widgetPropertiesMap); entity.setProperties(widgetProperties); widgetDAO.create(entity); notifyCreate(Type.Widget, request); return entity; } }); Resource resource = new ResourceImpl(Type.Widget); resource.setProperty(WIDGET_ID_PROPERTY_ID, widgetEntity.getId()); associatedResources.add(resource); } return getRequestStatus(null, associatedResources); } WidgetResourceProvider(AmbariManagementController managementController); @Override RequestStatus createResources(final Request request); @Override Set<Resource> getResources(Request request, Predicate predicate); @Override RequestStatus updateResources(Request request, Predicate predicate); @Override RequestStatus deleteResources(Request request, Predicate predicate); static final String WIDGET_ID_PROPERTY_ID; static final String WIDGET_CLUSTER_NAME_PROPERTY_ID; static final String WIDGET_WIDGET_NAME_PROPERTY_ID; static final String WIDGET_WIDGET_TYPE_PROPERTY_ID; static final String WIDGET_TIME_CREATED_PROPERTY_ID; static final String WIDGET_AUTHOR_PROPERTY_ID; static final String WIDGET_DESCRIPTION_PROPERTY_ID; static final String WIDGET_SCOPE_PROPERTY_ID; static final String WIDGET_METRICS_PROPERTY_ID; static final String WIDGET_VALUES_PROPERTY_ID; static final String WIDGET_PROPERTIES_PROPERTY_ID; @SuppressWarnings("serial")
  static Set<String> propertyIds; @SuppressWarnings("serial")
  static Map<Type, String> keyPropertyIds; }