private ResourceTestRule setupResourceTestRule() { final InMemoryAuthIdentityManager<ApiKey> authIdentityManager = new InMemoryAuthIdentityManager<>(); authIdentityManager.createIdentity(APIKEY_BLOB, new ApiKeyModification().addRoles("blob-role")); authIdentityManager.createIdentity(APIKEY_UNAUTHORIZED, new ApiKeyModification().addRoles("unauthorized-role")); authIdentityManager.createIdentity(APIKEY_BLOB_A, new ApiKeyModification().addRoles("blob-role-a")); authIdentityManager.createIdentity(APIKEY_BLOB_B, new ApiKeyModification().addRoles("blob-role-b")); final EmoPermissionResolver permissionResolver = new EmoPermissionResolver(mock(DataStore.class), _server); final InMemoryPermissionManager permissionManager = new InMemoryPermissionManager(permissionResolver); final RoleManager roleManager = new InMemoryRoleManager(permissionManager); createRole(roleManager, null, "blob-role", ImmutableSet.of("blob|*|*")); createRole(roleManager, null, "blob-role-a", ImmutableSet.of("blob|read|a*")); createRole(roleManager, null, "blob-role-b", ImmutableSet.of("blob|read|b*")); return setupResourceTestRule( Collections.<Object>singletonList(new BlobStoreResource1(_server, _approvedContentTypes)), authIdentityManager, permissionManager); }
private ResourceTestRule setupResourceTestRule() { final InMemoryAuthIdentityManager<ApiKey> authIdentityManager = new InMemoryAuthIdentityManager<>(); _stashKeyId = authIdentityManager.createIdentity(STASH_API_KEY, new ApiKeyModification().addRoles("stash")); _unauthKeyId = authIdentityManager.createIdentity(UNAUTH_API_KEY, new ApiKeyModification()); final EmoPermissionResolver permissionResolver = new EmoPermissionResolver(mock(DataStore.class), mock(BlobStore.class)); final InMemoryPermissionManager permissionManager = new InMemoryPermissionManager(permissionResolver); final RoleManager roleManager = new InMemoryRoleManager(permissionManager); roleManager.createRole(RoleIdentifier.fromString("stash"), new RoleModification() .withName("stash") .withPermissionUpdate(new PermissionUpdateRequest().permit("stash|*"))); _scanUploader = mock(ScanUploader.class); _stashRequestManager = mock(StashRequestManager.class); return setupResourceTestRule( ImmutableList.of(new StashResource1(_scanUploader, _stashRequestManager)), authIdentityManager, permissionManager); }
private ResourceTestRule setupDataStoreResourceTestRule() { InMemoryAuthIdentityManager<ApiKey> authIdentityManager = new InMemoryAuthIdentityManager<>(); authIdentityManager.createIdentity(APIKEY_TABLE, new ApiKeyModification().addRoles("table-role")); authIdentityManager.createIdentity(APIKEY_READ_TABLES_A, new ApiKeyModification().addRoles("tables-a-role")); authIdentityManager.createIdentity(APIKEY_READ_TABLES_B, new ApiKeyModification().addRoles("tables-b-role")); authIdentityManager.createIdentity(APIKEY_FACADE, new ApiKeyModification().addRoles("facade-role")); authIdentityManager.createIdentity(APIKEY_REVIEWS_ONLY, new ApiKeyModification().addRoles("reviews-only-role")); authIdentityManager.createIdentity(APIKEY_STANDARD, new ApiKeyModification().addRoles("standard")); authIdentityManager.createIdentity(APIKEY_STANDARD_UPDATE, new ApiKeyModification().addRoles("update-with-events")); EmoPermissionResolver permissionResolver = new EmoPermissionResolver(_server, mock(BlobStore.class)); InMemoryPermissionManager permissionManager = new InMemoryPermissionManager(permissionResolver); RoleManager roleManager = new InMemoryRoleManager(permissionManager); createRole(roleManager, null, "table-role", ImmutableSet.of("sor|*|*")); createRole(roleManager, null, "tables-a-role", ImmutableSet.of("sor|read|a*")); createRole(roleManager, null, "tables-b-role", ImmutableSet.of("sor|read|b*")); createRole(roleManager, null, "facade-role", ImmutableSet.of("facade|*|*")); createRole(roleManager, null, "reviews-only-role", ImmutableSet.of("sor|*|if({..,\"type\":\"review\"})")); createRole(roleManager, null, "standard", DefaultRoles.standard.getPermissions()); createRole(roleManager, null, "update-with-events", ImmutableSet.of("sor|update|*")); return setupResourceTestRule(Collections.<Object>singletonList(new DataStoreResource1(_server, mock(DataStoreAsync.class))), authIdentityManager, permissionManager); }
private ResourceTestRule setupResourceTestRule() { _authIdentityManager = new InMemoryAuthIdentityManager<>(); _authIdentityManager.createIdentity(UAC_ALL_API_KEY, new ApiKeyModification().addRoles("uac-all")); final EmoPermissionResolver permissionResolver = new EmoPermissionResolver(mock(DataStore.class), mock(BlobStore.class)); final InMemoryPermissionManager permissionManager = new InMemoryPermissionManager(permissionResolver); _roleManager = new InMemoryRoleManager(permissionManager); LocalSubjectUserAccessControl uac = new LocalSubjectUserAccessControl(_roleManager, permissionResolver, _authIdentityManager, HostAndPort.fromParts("localhost", 8080), new MetricRegistry()); createRole(_roleManager, null, "uac-all", ImmutableSet.of("role|*", "apikey|*")); createRole(_roleManager, null, "uac-none", ImmutableSet.of()); return setupResourceTestRule( ImmutableList.of( new UserAccessControlResource1( new RoleResource1(uac), new ApiKeyResource1(uac)), new UserAccessControlRequestMessageBodyReader()), _authIdentityManager, permissionManager); }
private ResourceTestRule getResources() { return ResourceTestRule.builder() .addResource(injector.getInstance(AccountResource.class)) .addProvider(OptionalMessageBodyWriter.class) .addProvider(OptionalParamFeature.class) .addProvider(new ServiceExceptionMapper()) .setRegisterDefaultExceptionMappers(false) .build(); }
/** * Call this method to set up authentication in the test's Jersey client. */ public static void setUpResources(ResourceTestRule.Builder test, SecurityManager securityManager) { JerseyAuthConfiguration config = JerseyAuthConfigurationBuilder.build(securityManager); addResourceFilterFactories(test, config); for (Object provider : config.getProviderInstances()) { test.addProvider(provider); } for (Class<?> providerClass : config.getProviderClasses()) { test.addProvider(providerClass); } }
@SuppressWarnings("unchecked") private static void addResourceFilterFactories(ResourceTestRule.Builder test, JerseyAuthConfiguration config) { // The ability to add properties is normally protected so we need to break in to update the resource filter // factories. Map<String, Object> properties; try { Field field = ResourceTestRule.Builder.class.getDeclaredField("properties"); field.setAccessible(true); properties = (Map<String, Object>) field.get(test); } catch (Exception e) { throw Throwables.propagate(e); } List resourceFilterFactories; // Add the new resource filter factories to any existing values Object existing = properties.get(PackagesResourceConfig.PROPERTY_RESOURCE_FILTER_FACTORIES); if (existing == null) { resourceFilterFactories = config.getResourceFilterFactories(); } else { resourceFilterFactories = Lists.newArrayList(); if (existing.getClass().isArray()) { resourceFilterFactories.addAll(Arrays.asList((Object[]) existing)); } else if (Iterable.class.isAssignableFrom(existing.getClass())) { Iterables.addAll(resourceFilterFactories, (Iterable) existing); } else { resourceFilterFactories.add(existing); } resourceFilterFactories.addAll(config.getResourceFilterFactories()); } properties.put(PackagesResourceConfig.PROPERTY_RESOURCE_FILTER_FACTORIES, resourceFilterFactories); }
protected ResourceTestRule setupResourceTestRule() { ResourceTestRule.Builder resourceTestRuleBuilder = ResourceTestRule.builder(); ResourceTestAuthUtil.setUpResources(resourceTestRuleBuilder, SecurityManagerBuilder.create() .withAuthIdentityReader(_authIdentityDAO) .withPermissionReader(_permissionDAO) .withAnonymousAccessAs("anon") .build()); resourceTestRuleBuilder.addResource(new ExplicitPermissionResource()); resourceTestRuleBuilder.addResource(new PathPermissionResource()); resourceTestRuleBuilder.addResource(new QueryPermissionResource()); return resourceTestRuleBuilder.build(); }
protected ResourceTestRule setupResourceTestRule() { ResourceTestRule.Builder resourceTestRuleBuilder = ResourceTestRule.builder(); CacheRegistry cacheRegistry = new DefaultCacheRegistry(new SimpleLifeCycleRegistry(), new MetricRegistry()); _cacheManager = new GuavaCacheManager(cacheRegistry); //noinspection unchecked Supplier<String> idSupplier = mock(Supplier.class); when(idSupplier.get()).thenReturn("id0", "id1").thenThrow(new IllegalStateException("Unexpected createIdentity call")); InMemoryAuthIdentityManager<ApiKey> authIdentityDAO = new InMemoryAuthIdentityManager<>(idSupplier); _authIdentityCaching = new CacheManagingAuthIdentityManager<>(authIdentityDAO, _cacheManager); _authIdentityManager = spy(_authIdentityCaching); InMemoryPermissionManager permissionDAO = new InMemoryPermissionManager(new MatchingPermissionResolver()); _permissionCaching = new CacheManagingPermissionManager(permissionDAO, _cacheManager); _permissionManager = spy(_permissionCaching); authIdentityDAO.createIdentity("testkey", new ApiKeyModification().addRoles("testrole")); authIdentityDAO.createIdentity("othertestkey", new ApiKeyModification().addRoles("testrole")); permissionDAO.updatePermissions(PermissionIDs.forRole("testrole"), new PermissionUpdateRequest().permit("city|get|Madrid", "country|get|Spain")); ResourceTestAuthUtil.setUpResources(resourceTestRuleBuilder, SecurityManagerBuilder.create() .withAuthIdentityReader(_authIdentityManager) .withPermissionReader(_permissionManager) .withCacheManager(_cacheManager) .withAnonymousAccessAs("anon") .build()); resourceTestRuleBuilder.addResource(new ExplicitPermissionResource()); return resourceTestRuleBuilder.build(); }
private ResourceTestRule setupDataStoreResourceTestRule() { InMemoryAuthIdentityManager<ApiKey> authIdentityManager = new InMemoryAuthIdentityManager<>(); authIdentityManager.createIdentity(API_KEY, new ApiKeyModification().addRoles("all-sor-role")); EmoPermissionResolver permissionResolver = new EmoPermissionResolver(_dataStore, mock(BlobStore.class)); InMemoryPermissionManager permissionManager = new InMemoryPermissionManager(permissionResolver); InMemoryRoleManager roleManager = new InMemoryRoleManager(permissionManager); createRole(roleManager, null, "all-sor-role", ImmutableSet.of("sor|*|*")); return setupResourceTestRule( Collections.<Object>singletonList(new DataStoreResource1(_dataStore, new DefaultDataStoreAsync(_dataStore, mock(JobService.class), mock(JobHandlerRegistry.class)))), Collections.<Object>singletonList(new ConcurrentRequestsThrottlingFilter(_deferringRegulatorSupplier)), authIdentityManager, permissionManager); }
@SuppressWarnings("unchecked") @Override protected ResourceTestRule.Builder setUpResources(ResourceTestRule.Builder builder) { verificationStorage = mock(MetadataStorage.class); processor = mock(VerificationProcessor.class); return super.setUpResources(builder) .addResource(new VerificationResource(processor)) .addProvider(new VerificationMetadataProvider(verificationStorage)); }
@Override @SuppressWarnings("unchecked") protected ResourceTestRule.Builder setUpResources(ResourceTestRule.Builder builder) { processor = mock(BackupProcessor.class); clientPermissionStorage = mock(MetadataStorage.class); backupStorage = mock(MetadataStorage.class); final MetadataStorage<Node> nodeStorage = mock(MetadataStorage.class); when(clientPermissionStorage.get(anyString(), anyString())) .thenReturn(Optional.<ClientPermission>absent()); when(clientPermissionStorage.get(eq(SERVICE_NAME), eq(TOKEN))) .thenReturn(Optional.of(new ClientPermission(TOKEN,SERVICE_NAME))); try { when(nodeStorage.get(eq(OTHER_NODE), eq(OTHER_NODE))) .thenReturn(Optional.of(new Node(OTHER_NODE, new URL(OTHER_NODE_URL)))); when(nodeStorage.get(eq(LOCAL_NODE), eq(LOCAL_NODE))) .thenReturn(Optional.of(new Node(LOCAL_NODE, new URL(LOCAL_NODE_URL)))); } catch (MalformedURLException e) { throw Throwables.propagate(e); } final TemporaryTokenGenerator tokenGenerator = mock(TemporaryTokenGenerator.class); return super.setUpResources(builder) .addProvider(new TokenAuthProvider<>(new TokenAuthenticator(clientPermissionStorage, tokenGenerator))) .addResource(new BackupResource(processor, clientPermissionStorage, LOCAL_NODE)) .addProvider(new BackupMetadataProvider(backupStorage)) .addProvider(new IncorrectNodeExceptionMapper(nodeStorage)); }
protected ResourceTestRule.Builder setUpResources(ResourceTestRule.Builder builder) { return builder.addProvider(new SingletonTypeInjectableProvider<Context, HttpServletRequest>(HttpServletRequest.class, REQUEST) {}) .addProvider(new NoContentExceptionMapper()) .addProvider(new MetadataNotFoundExceptionMapper()) .addProvider(new InvalidMD5ExceptionMapper()) .addProvider(new IllegalStateExceptionMapper()); }
protected static ResourceTestRule setupResourceTestRule(List<Object> resourceList, Map<String, ApiKey> apiKeys, Multimap<String, String> permissionsByRole) { return setupResourceTestRule(resourceList, ImmutableList.of(), apiKeys, permissionsByRole); }
protected static ResourceTestRule setupResourceTestRule(List<Object> resourceList, AuthIdentityManager<ApiKey> authIdentityManager, PermissionManager permissionManager) { return setupResourceTestRule(resourceList, ImmutableList.of(), authIdentityManager, permissionManager); }
protected static ResourceTestRule setupResourceTestRule(List<Object> resourceList, List<Object> filters, AuthIdentityManager<ApiKey> authIdentityManager, PermissionManager permissionManager) { ResourceTestRule.Builder resourceTestRuleBuilder = ResourceTestRule.builder(); for (Object resource : resourceList) { resourceTestRuleBuilder.addResource(resource); } List<ResourceFilterFactory> resourceFilterFactories = Lists.newArrayList(); List<ContainerRequestFilter> containerRequestFilters = Lists.newArrayList(); List<ContainerResponseFilter> containerResponseFilters = Lists.newArrayList(); for (Object filter : filters) { if (filter instanceof ResourceFilterFactory) { resourceFilterFactories.add((ResourceFilterFactory) filter); } if (filter instanceof ContainerRequestFilter) { containerRequestFilters.add((ContainerRequestFilter) filter); } if (filter instanceof ContainerResponseFilter) { containerResponseFilters.add((ContainerResponseFilter) filter); } } resourceTestRuleBuilder.addProperty(ResourceConfig.PROPERTY_RESOURCE_FILTER_FACTORIES, resourceFilterFactories); resourceTestRuleBuilder.addProperty(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS, containerRequestFilters); resourceTestRuleBuilder.addProperty(ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS, containerResponseFilters); // Jersey tests don't inject Context parameters, so create an injector to provide a mock instance. resourceTestRuleBuilder.addProvider(new DatabusJerseyTest.ContextInjectableProvider<>(HttpServletRequest.class, mock(HttpServletRequest.class))); ResourceTestAuthUtil.setUpResources(resourceTestRuleBuilder, SecurityManagerBuilder.create() .withAuthIdentityReader(authIdentityManager) .withPermissionReader(permissionManager) .build()); for (Object mapper : ExceptionMappers.getMappers()) { resourceTestRuleBuilder.addProvider(mapper); } for (Class mapperType : ExceptionMappers.getMapperTypes()) { resourceTestRuleBuilder.addProvider(mapperType); } ResourceTestRule resourceTestRule = resourceTestRuleBuilder.build(); // Write Date objects using ISO8601 strings instead of numeric milliseconds-since-1970. SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); fmt.setTimeZone(TimeZone.getTimeZone("GMT")); resourceTestRule.getObjectMapper().setDateFormat(fmt); return resourceTestRule; }
protected static ResourceTestRule.Builder createRule() { return ResourceTestRule.builder() .addProvider(new BasicAuthProvider<>(new DummyAuthenticator("username", "password", Data.Player1.PLAYER), "test")); }
protected WebResource.Builder request(ResourceTestRule resources, String url) { return resources.client().resource(url).type(MediaType.APPLICATION_JSON_TYPE); }
public static ResourceTestRule.Builder builder() { return new ResourceTestRule.Builder() .addProvider(OptionalMessageBodyWriter.class) .addProvider(OptionalParamFeature.class); }
public ResourceTestRule getResources() { return null; }
@Override public ResourceTestRule getResources() { return resources; }