Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GEODE-3974: Improve permissions for geode-modules functions #1258

Merged
merged 5 commits into from
Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
Expand Up @@ -17,6 +17,8 @@
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

Expand All @@ -32,6 +34,8 @@
import org.apache.geode.distributed.internal.InternalDistributedSystem;
import org.apache.geode.distributed.internal.MembershipListener;
import org.apache.geode.distributed.internal.membership.InternalDistributedMember;
import org.apache.geode.management.internal.security.ResourcePermissions;
import org.apache.geode.security.ResourcePermission;

public class BootstrappingFunction implements Function, MembershipListener, DataSerializable {

Expand Down Expand Up @@ -87,6 +91,11 @@ private Cache verifyCacheExists() {
return cache;

Choose a reason for hiding this comment

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

I think this would be a good opportunity to eliminate the CacheFactory.getAnyInstance() call above. The cache should be available through the FunctionConext.

Copy link
Contributor Author

@jdeppe-pivotal jdeppe-pivotal Jan 9, 2018

Choose a reason for hiding this comment

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

I'm going to open a separate Jira for that - it seems a bit orthogonal to what's in this PR. (GEODE-4262).

}

@Override
public Collection<ResourcePermission> getRequiredPermissions(String regionName) {
return Collections.singletonList(ResourcePermissions.CLUSTER_MANAGE);
}

private void registerAsMembershipListener(Cache cache) {
DistributionManager dm =
((InternalDistributedSystem) cache.getDistributedSystem()).getDistributionManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Collections;
import java.util.Properties;

import javax.xml.crypto.Data;

import org.apache.geode.DataSerializable;
import org.apache.geode.InternalGemFireError;
import org.apache.geode.cache.AttributesFactory;
Expand All @@ -32,8 +32,6 @@
import org.apache.geode.cache.Declarable;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.RegionAttributes;
import org.apache.geode.cache.RegionFactory;
import org.apache.geode.cache.RegionShortcut;
import org.apache.geode.cache.Scope;
import org.apache.geode.cache.client.ClientCache;
import org.apache.geode.cache.execute.Function;
Expand All @@ -46,6 +44,8 @@
import org.apache.geode.internal.cache.PartitionedRegion;
import org.apache.geode.internal.cache.xmlcache.CacheXmlGenerator;
import org.apache.geode.internal.i18n.LocalizedStrings;
import org.apache.geode.management.internal.security.ResourcePermissions;
import org.apache.geode.security.ResourcePermission;

public class CreateRegionFunction implements Function, Declarable, DataSerializable {

Expand Down Expand Up @@ -96,6 +96,11 @@ public void execute(FunctionContext context) {
context.getResultSender().lastResult(status);
}

@Override
public Collection<ResourcePermission> getRequiredPermissions(String regionName) {
return Collections.singletonList(ResourcePermissions.DATA_MANAGE);
}

private RegionStatus createOrRetrieveRegion(RegionConfiguration configuration) {
RegionStatus status = null;
String regionName = configuration.getRegionName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Properties;

import org.apache.geode.DataSerializable;
import org.apache.geode.cache.Declarable;
import org.apache.geode.cache.execute.Function;
import org.apache.geode.cache.execute.FunctionContext;
import org.apache.geode.cache.execute.RegionFunctionContext;
import org.apache.geode.security.ResourcePermission;

public class RegionSizeFunction implements Function, Declarable, DataSerializable {

Expand All @@ -38,6 +41,12 @@ public void execute(FunctionContext context) {
context.getResultSender().lastResult(rfc.getDataSet().size());
}

@Override
public Collection<ResourcePermission> getRequiredPermissions(String regionName) {
return Collections.singletonList(new ResourcePermission(ResourcePermission.Resource.DATA,
ResourcePermission.Operation.READ, regionName));
}

public String getId() {
return ID;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Properties;
import java.util.Set;

Expand All @@ -29,6 +31,7 @@
import org.apache.geode.cache.execute.FunctionContext;
import org.apache.geode.cache.execute.RegionFunctionContext;
import org.apache.geode.cache.partition.PartitionRegionHelper;
import org.apache.geode.security.ResourcePermission;

/**
* Touches the keys contained in the set of keys by performing a get on the partitioned region.
Expand Down Expand Up @@ -76,6 +79,12 @@ public void execute(FunctionContext context) {
context.getResultSender().lastResult(true);
}

@Override
public Collection<ResourcePermission> getRequiredPermissions(String regionName) {
return Collections.singletonList(new ResourcePermission(ResourcePermission.Resource.DATA,
ResourcePermission.Operation.READ, regionName));
}

public String getId() {
return ID;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Properties;
import java.util.Set;

Expand All @@ -27,6 +29,7 @@
import org.apache.geode.cache.Region;
import org.apache.geode.cache.execute.Function;
import org.apache.geode.cache.execute.FunctionContext;
import org.apache.geode.security.ResourcePermission;

/**
* Touches the keys contained in the set of keys by performing a get on the replicated region. This
Expand Down Expand Up @@ -71,6 +74,12 @@ public void execute(FunctionContext context) {
context.getResultSender().lastResult(true);
}

@Override
public Collection<ResourcePermission> getRequiredPermissions(String regionName) {
return Collections.singletonList(new ResourcePermission(ResourcePermission.Resource.DATA,
ResourcePermission.Operation.READ, regionName));
}

public String getId() {
return ID;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You 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 org.apache.geode.modules.util;

import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import org.apache.geode.cache.RegionShortcut;
import org.apache.geode.cache.execute.FunctionService;
import org.apache.geode.examples.SimpleSecurityManager;
import org.apache.geode.test.junit.categories.IntegrationTest;
import org.apache.geode.test.junit.rules.ConnectionConfiguration;
import org.apache.geode.test.junit.rules.GfshCommandRule;
import org.apache.geode.test.junit.rules.ServerStarterRule;

@Category(IntegrationTest.class)
Copy link
Member

Choose a reason for hiding this comment

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

I know we don't really just them for anything, but this deserves the SecurityTest.class category, too.

public class ModuleFunctionsSecurityTest {
Copy link
Member

Choose a reason for hiding this comment

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

Should there not be any positive testing for these commands? That'll make the interaction a lot more complicated, true, but it seems strange that we'd only test some selective negatives here and assume positive security coverage elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Discussed with @jinmeiliao which is why I removed them. The tests are targeted to ensure that these functions require specific permissions and not that the security framework is working.


@ClassRule
public static ServerStarterRule server =
new ServerStarterRule().withJMXManager().withSecurityManager(SimpleSecurityManager.class)
.withRegion(RegionShortcut.REPLICATE, "REPLICATE_1")
.withRegion(RegionShortcut.PARTITION, "PARTITION_1").withAutoStart();

@Rule
public GfshCommandRule gfsh = new GfshCommandRule(server::getJmxPort, GfshCommandRule.PortType.jmxManager);

@BeforeClass
public static void setupClass() {
FunctionService.registerFunction(new BootstrappingFunction());
FunctionService.registerFunction(new CreateRegionFunction());
FunctionService.registerFunction(new RegionSizeFunction());
FunctionService.registerFunction(new TouchPartitionedRegionEntriesFunction());
FunctionService.registerFunction(new TouchReplicatedRegionEntriesFunction());
}

@Test
@ConnectionConfiguration(user = "dataWrite", password = "dataWrite")
public void testInvalidPermissionsForBootstrappingFunction() throws Exception {
Copy link
Member

@jinmeiliao jinmeiliao Jan 9, 2018

Choose a reason for hiding this comment

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

Another way to have the test auto connect with a specific user/password is to do this:

@rule // instead of a classrule so that it connect/disconnect around each test
public GfshCommandRule gfsh = new GfshCommandRule(server::getJmxPort, PortType.JmxManager);

@test
@ConnectionConfiguration(user = "dataWrite", password = "dataWrite")
public void test(){
gfsh.executeAndAssertThat("execute function --id=test");
}

gfsh.executeAndAssertThat("execute function --id=" + BootstrappingFunction.ID)
.containsOutput("not authorized for CLUSTER:MANAGE").statusIsSuccess();
}

@Test
@ConnectionConfiguration(user = "dataWrite", password = "dataWrite")
public void testInvalidPermissionsForCreateRegionFunction() throws Exception {
gfsh.executeAndAssertThat("execute function --id=" + CreateRegionFunction.ID)
.containsOutput("not authorized for DATA:MANAGE").statusIsSuccess();
}

@Test
@ConnectionConfiguration(user = "dataWrite", password = "dataWrite")
public void testInvalidPermissionsForRegionSizeFunction() throws Exception {
gfsh.executeAndAssertThat("execute function --region=REPLICATE_1 --id=" + RegionSizeFunction.ID)
.containsOutput("not authorized for DATA:READ:REPLICATE_1").statusIsSuccess();
}

@Test
@ConnectionConfiguration(user = "dataWrite", password = "dataWrite")
public void testInvalidPermissionsForTouchPartitionedRegionEntriesFunction() throws Exception {
gfsh.executeAndAssertThat(
"execute function --region=PARTITION_1 --id=" + TouchPartitionedRegionEntriesFunction.ID)
.containsOutput("not authorized for DATA:READ:PARTITION_1").statusIsSuccess();
}

@Test
@ConnectionConfiguration(user = "dataWrite", password = "dataWrite")
public void testInvalidPermissionsForTouchReplicatedRegionEntriesFunction() throws Exception {
gfsh.executeAndAssertThat(
"execute function --region=REPLICATE_1 --id=" + TouchReplicatedRegionEntriesFunction.ID)
.containsOutput("not authorized for DATA:READ:REPLICATE_1").statusIsSuccess();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.apache.geode.management.internal.cli.functions;

import java.util.Collection;
import java.util.Collections;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -49,6 +51,8 @@
import org.apache.geode.management.internal.cli.i18n.CliStrings;
import org.apache.geode.management.internal.cli.util.RegionPath;
import org.apache.geode.management.internal.configuration.domain.XmlEntity;
import org.apache.geode.management.internal.security.ResourcePermissions;
import org.apache.geode.security.ResourcePermission;

/**
*
Expand Down Expand Up @@ -121,6 +125,11 @@ public void execute(FunctionContext context) {
}
}

@Override
public Collection<ResourcePermission> getRequiredPermissions(String regionName) {
return Collections.singletonList(ResourcePermissions.DATA_MANAGE);
}

private CliFunctionResult handleException(final String memberNameOrId, final String exceptionMsg,
final Exception e) {
if (e != null && logger.isDebugEnabled()) {
Expand Down