Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.

351 tenant resolver #896

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2016 Stormpath, 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 com.stormpath.sdk.servlet.tenant;

import com.stormpath.sdk.accountStoreMapping.AccountStoreMapping;
import com.stormpath.sdk.application.Application;
import com.stormpath.sdk.application.ApplicationAccountStoreMappingList;
import com.stormpath.sdk.directory.AccountStore;
import com.stormpath.sdk.directory.AccountStoreVisitor;
import com.stormpath.sdk.directory.Directory;
import com.stormpath.sdk.group.Group;
import com.stormpath.sdk.organization.Organization;
import com.stormpath.sdk.servlet.application.ApplicationResolver;
import com.stormpath.sdk.servlet.http.Resolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @since 1.0.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.1.0

*/
public class DefaultTenantResolver implements TenantResolver<Organization> {

Resolver<String> organizationNameKeyResolver;
ApplicationResolver applicationResolver = ApplicationResolver.INSTANCE;

public void setOrganizationNameKeyResolver(Resolver<String> organizationNameKeyResolver) {
this.organizationNameKeyResolver = organizationNameKeyResolver;
}

public Organization get(HttpServletRequest request, HttpServletResponse response) {

final String domainName = organizationNameKeyResolver.get(request, response);
if (domainName == null) {
return null;
}

Application application = applicationResolver.getApplication(request);
ApplicationAccountStoreMappingList accountStoreMappings = application.getAccountStoreMappings();
final Organization organization[] = {null};
for (AccountStoreMapping accountStoreMapping : accountStoreMappings) {
final AccountStore accountStore = accountStoreMapping.getAccountStore();

accountStore.accept(new AccountStoreVisitor() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use AccountStoreVisitorAdapter, you won't need the empty implementations from the interface here.

@Override
public void visit(Group group) {
//no-op
}

@Override
public void visit(Directory directory) {
//no-op
}

@Override
public void visit(Organization org) {
if (domainName.equals(org.getName())) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be: domainName.equals(org.getNameKey())

organization[0] = org;
}
}
});

if (organization[0] != null) break;
}
return organization[0];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2016 Stormpath, 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 com.stormpath.sdk.servlet.tenant;

import com.stormpath.sdk.organization.Organization;
import com.stormpath.sdk.servlet.config.ConfigSingletonFactory;
import com.stormpath.sdk.servlet.http.Resolver;
import com.stormpath.sdk.servlet.organization.DefaultOrganizationNameKeyResolver;

import javax.servlet.ServletContext;

/**
* @since 1.0.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.1.0

*/
public class DefaultTenantResolverFactory extends ConfigSingletonFactory<Resolver<Organization>> {

protected Resolver<Organization> createInstance(ServletContext servletContext) throws Exception {
DefaultTenantResolver defaultTenantResolver = new DefaultTenantResolver();
DefaultOrganizationNameKeyResolver organizationNameKeyResolver = new DefaultOrganizationNameKeyResolver();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need to call setSubdomainResolver on organizationsNameKeyResolver?

defaultTenantResolver.setOrganizationNameKeyResolver(organizationNameKeyResolver);
return defaultTenantResolver;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2016 Stormpath, 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 com.stormpath.sdk.servlet.tenant;

import com.stormpath.sdk.servlet.http.Resolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @since 1.0.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.1.0

*/
public interface TenantResolver<T extends com.stormpath.sdk.resource.Resource> extends Resolver<T> {

T get(HttpServletRequest request, HttpServletResponse response);

}
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ stormpath.web.idSite.OrganizationResolverFactory = com.stormpath.sdk.servlet.fil
# Inferred based on heuristics by default. However if your application is not deployed to an apex domain, like
# myapp.com, you *must* specify your application's base domain, e.g. myapp.mycompany.com
stormpath.web.application.domain =
stormpath.web.application.tenant.resolver = com.stormpath.sdk.servlet.tenant.DefaultTenantResolverFactory

stormpath.web.request.event.publisher = com.stormpath.sdk.servlet.event.impl.EventPublisherFactory
stormpath.web.request.event.listener = com.stormpath.sdk.servlet.event.RequestEventListenerAdapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,7 @@ class SpecConfigVersusWebPropertiesTest {
defaultProperties = new ResourcePropertiesSource(defaultConfig).properties
}

/**
* NOTE: This test is temporarily disabled as 15 new properties have been added to the framework spec for
* multi-tenancy that are not yet implemented in the SDK.
* Per high priority ticket: https://github.com/stormpath/stormpath-sdk-java/issues/1033,
* Todo: this should be re-enabled and support for the new properties should be added asap
*/
@Test(enabled=false)
@Test
void verifyPropertiesInSpecAreInDefault() {

def diff = specProperties.findResults { k,v ->
Expand All @@ -74,9 +68,9 @@ class SpecConfigVersusWebPropertiesTest {
println "Or you could adjust the assertEquals statement in this method to allow for this missing key as a temporary solution."
}

//todo: 15 new properties related to organizations were added to the spec, we do not yet suppor them.
//todo: 22 new properties related to organizations were added to the spec, we do not yet support them.
//see https://github.com/stormpath/stormpath-sdk-java/issues/1052
assertEquals 15, diff.size(), "Missing keys in default config: ${diff}"
assertEquals diff.size(), 22, "Missing keys in default config: ${diff}"
}

@Test
Expand All @@ -85,7 +79,7 @@ class SpecConfigVersusWebPropertiesTest {
specProperties.containsKey(k) ? null : k
}

def expected_diff_size = 82
def expected_diff_size = 83

if (diff.size != expected_diff_size) {
println "It looks like a property was added or removed from the Framework Spec or web.stormpath.properties."
Expand All @@ -95,20 +89,20 @@ class SpecConfigVersusWebPropertiesTest {
assertEquals diff.size(), expected_diff_size, "Missing keys in spec config: ${diff}"

// to see the keys missing in spec, uncomment the following
/*if (diff.size > 0) {
if (diff.size > 0) {
println "Missing keys in spec:"
diff.each {
println "${it}"
}
}*/
}

// to see the keys and their values for updating the wiki, uncomment the following
// https://github.com/stormpath/stormpath-sdk-java/wiki/1.0-Configuration-Changes-&-Additions-Guide#not-in-specification
/*

SortedSet<String> keys = new TreeSet<String>(properties.keySet());
keys.each {
println("|${it}|" + properties.get(it) + "|")
}*/
}
}

@Test(enabled = false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* Copyright 2016 Stormpath, 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 com.stormpath.sdk.servlet.tenant

import com.stormpath.sdk.accountStoreMapping.AccountStoreMapping
import com.stormpath.sdk.application.Application
import com.stormpath.sdk.application.ApplicationAccountStoreMappingList
import com.stormpath.sdk.directory.AccountStoreVisitor
import com.stormpath.sdk.directory.Directory
import com.stormpath.sdk.group.Group
import com.stormpath.sdk.organization.Organization
import com.stormpath.sdk.servlet.organization.DefaultOrganizationNameKeyResolver
import com.stormpath.sdk.servlet.util.SubdomainResolver
import org.easymock.Capture
import org.easymock.IAnswer
import org.testng.annotations.Test

import javax.servlet.http.HttpServletRequest

import static org.easymock.EasyMock.*
import static org.testng.Assert.assertEquals

/**
* @since 1.0.0
*/
class DefaultTenantResolverTest {

@Test
void testOrganizationExistInAccountStores() {
def request = createMock(HttpServletRequest)
def application = createStrictMock(Application)
def applicationAccountStoreMappingList = createStrictMock(ApplicationAccountStoreMappingList)
def iterator = createMock(Iterator)
def accountStoreMapping = createMock(AccountStoreMapping)
def dir = createMock(Directory)
def organization = createMock(Organization)

expect(request.getAttribute(Application.getCanonicalName())).andReturn(application)
expect(request.getHeader(eq('Host'))).andStubReturn('bar.foo.com')
expect(application.getAccountStoreMappings()).andReturn(applicationAccountStoreMappingList)
expect(applicationAccountStoreMappingList.iterator()).andReturn(iterator)
expect(iterator.hasNext()).andReturn(true)
expect(iterator.next()).andReturn(accountStoreMapping)
expect(accountStoreMapping.getAccountStore()).andReturn(dir)
expect(dir.accept(anyObject())).andVoid()
expect(iterator.hasNext()).andReturn(true)
expect(iterator.next()).andReturn(accountStoreMapping)
expect(accountStoreMapping.getAccountStore()).andReturn(organization)

Capture capturedArgument = new Capture<AccountStoreVisitor>();
expect(organization.accept(and(capture(capturedArgument), isA(AccountStoreVisitor)))).andAnswer(
new IAnswer<AccountStoreVisitor>() {
@Override
public AccountStoreVisitor answer() throws Throwable {
AccountStoreVisitor accountStoreVisitor = (AccountStoreVisitor) capturedArgument.getValue();
accountStoreVisitor.visit(organization);
}
}
)
expect(organization.getName()).andReturn("bar") //we return "bar" as the organization name found in account store mappings

replay(request, application, applicationAccountStoreMappingList, iterator, accountStoreMapping, dir, organization)

def organizationNameKeyResolver = new DefaultOrganizationNameKeyResolver();
organizationNameKeyResolver.setSubdomainResolver(new SubdomainResolver())
def resolver = new DefaultTenantResolver();
resolver.setOrganizationNameKeyResolver(organizationNameKeyResolver)

assertEquals(resolver.get(request, null), organization) //expected Organization is 'organization'

verify(request, application, applicationAccountStoreMappingList, iterator, accountStoreMapping, dir, organization)
}

@Test
void testOrganizationDoesNotExistInAccountStores() {
def request = createMock(HttpServletRequest)
def application = createStrictMock(Application)
def applicationAccountStoreMappingList = createStrictMock(ApplicationAccountStoreMappingList)
def iterator = createMock(Iterator)
def accountStoreMapping = createMock(AccountStoreMapping)
def group = createMock(Group)

expect(request.getAttribute(Application.getCanonicalName())).andReturn(application)
expect(request.getHeader(eq('Host'))).andStubReturn('bar.foo.com')
expect(application.getAccountStoreMappings()).andReturn(applicationAccountStoreMappingList)
expect(applicationAccountStoreMappingList.iterator()).andReturn(iterator)
expect(iterator.hasNext()).andReturn(true)
expect(iterator.next()).andReturn(accountStoreMapping)
expect(accountStoreMapping.getAccountStore()).andReturn(group)
expect(group.accept(anyObject())).andVoid()
expect(iterator.hasNext()).andReturn(false)

replay(request, application, applicationAccountStoreMappingList, iterator, accountStoreMapping, group)

def organizationNameKeyResolver = new DefaultOrganizationNameKeyResolver();
organizationNameKeyResolver.setSubdomainResolver(new SubdomainResolver())
def resolver = new DefaultTenantResolver();
resolver.setOrganizationNameKeyResolver(organizationNameKeyResolver)

assertEquals(resolver.get(request, null), null) //expected Organization is null

verify(request, application, applicationAccountStoreMappingList, iterator, accountStoreMapping, group)
}

@Test
void testNoOrganizationDomain() {
def request = createMock(HttpServletRequest)

expect(request.getHeader(eq('Host'))).andStubReturn('foo.com') //no organization found in url

replay(request)

def organizationNameKeyResolver = new DefaultOrganizationNameKeyResolver();
organizationNameKeyResolver.setSubdomainResolver(new SubdomainResolver())
def resolver = new DefaultTenantResolver();
resolver.setOrganizationNameKeyResolver(organizationNameKeyResolver)

assertEquals(resolver.get(request, null), null)

verify(request)
}

}