Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id F3798200D08 for ; Wed, 23 Aug 2017 02:54:52 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id F1E4C167F2B; Wed, 23 Aug 2017 00:54:52 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 1E471167F2A for ; Wed, 23 Aug 2017 02:54:51 +0200 (CEST) Received: (qmail 14866 invoked by uid 500); 23 Aug 2017 00:54:46 -0000 Mailing-List: contact dev-help@geode.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@geode.apache.org Delivered-To: mailing list dev@geode.apache.org Received: (qmail 14464 invoked by uid 99); 23 Aug 2017 00:54:45 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 23 Aug 2017 00:54:45 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 1AEC4F32B0; Wed, 23 Aug 2017 00:54:45 +0000 (UTC) From: galen-pivotal To: dev@geode.apache.org Reply-To: dev@geode.apache.org References: In-Reply-To: Subject: [GitHub] geode pull request #719: GEODE-3447 Implement client authorization for the n... Content-Type: text/plain Message-Id: <20170823005445.1AEC4F32B0@git1-us-west.apache.org> Date: Wed, 23 Aug 2017 00:54:45 +0000 (UTC) archived-at: Wed, 23 Aug 2017 00:54:53 -0000 Github user galen-pivotal commented on a diff in the pull request: https://github.com/apache/geode/pull/719#discussion_r134635734 --- Diff: geode-protobuf/src/test/java/org/apache/geode/protocol/AuthorizationIntegrationTest.java --- @@ -0,0 +1,206 @@ +/* + * 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.protocol; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.same; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; +import java.util.Properties; +import java.util.concurrent.TimeUnit; + +import org.awaitility.Awaitility; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.RestoreSystemProperties; +import org.junit.experimental.categories.Category; + +import org.apache.geode.cache.Cache; +import org.apache.geode.cache.CacheFactory; +import org.apache.geode.cache.server.CacheServer; +import org.apache.geode.internal.AvailablePortHelper; +import org.apache.geode.management.internal.security.ResourceConstants; +import org.apache.geode.protocol.protobuf.AuthenticationAPI; +import org.apache.geode.protocol.protobuf.ClientProtocol; +import org.apache.geode.protocol.protobuf.ProtobufSerializationService; +import org.apache.geode.protocol.protobuf.ProtocolErrorCode; +import org.apache.geode.protocol.protobuf.RegionAPI; +import org.apache.geode.protocol.protobuf.serializer.ProtobufProtocolSerializer; +import org.apache.geode.protocol.protobuf.utilities.ProtobufUtilities; +import org.apache.geode.security.ResourcePermission; +import org.apache.geode.security.SecurityManager; +import org.apache.geode.serialization.registry.exception.CodecAlreadyRegisteredForTypeException; +import org.apache.geode.test.junit.categories.IntegrationTest; + +@Category(IntegrationTest.class) +public class AuthorizationIntegrationTest { + + private static final String TEST_USERNAME = "bob"; + private static final String TEST_PASSWORD = "bobspassword"; + public static final String TEST_REGION = "testRegion"; + + @Rule + public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties(); + + private Cache cache; + private int cacheServerPort; + private CacheServer cacheServer; + private Socket socket; + private OutputStream outputStream; + private ProtobufSerializationService serializationService; + private InputStream inputStream; + private ProtobufProtocolSerializer protobufProtocolSerializer; + private Object securityPrincipal; + private SecurityManager mockSecurityManager; + private String testRegion; + public static final ResourcePermission READ_PERMISSION = + new ResourcePermission(ResourcePermission.Resource.DATA, ResourcePermission.Operation.READ); + public static final ResourcePermission WRITE_PERMISSION = + new ResourcePermission(ResourcePermission.Resource.DATA, ResourcePermission.Operation.WRITE); + + @Before + public void setUp() throws IOException, CodecAlreadyRegisteredForTypeException { + Properties expectedAuthProperties = new Properties(); + expectedAuthProperties.setProperty(ResourceConstants.USER_NAME, TEST_USERNAME); + expectedAuthProperties.setProperty(ResourceConstants.PASSWORD, TEST_PASSWORD); + + securityPrincipal = new Object(); + mockSecurityManager = mock(SecurityManager.class); + when(mockSecurityManager.authenticate(expectedAuthProperties)).thenReturn(securityPrincipal); + + Properties properties = new Properties(); + CacheFactory cacheFactory = new CacheFactory(properties); + cacheFactory.set("mcast-port", "0"); // sometimes it isn't due to other tests. + + cacheFactory.setSecurityManager(mockSecurityManager); + cache = cacheFactory.create(); + + cacheServer = cache.addCacheServer(); + cacheServerPort = AvailablePortHelper.getRandomAvailableTCPPort(); + cacheServer.setPort(cacheServerPort); + cacheServer.start(); + + cache.createRegionFactory().create(TEST_REGION); + + System.setProperty("geode.feature-protobuf-protocol", "true"); + System.setProperty("geode.protocol-authentication-mode", "SIMPLE"); + socket = new Socket("localhost", cacheServerPort); + + Awaitility.await().atMost(5, TimeUnit.SECONDS).until(socket::isConnected); + outputStream = socket.getOutputStream(); + inputStream = socket.getInputStream(); + outputStream.write(110); + + serializationService = new ProtobufSerializationService(); + protobufProtocolSerializer = new ProtobufProtocolSerializer(); + + when(mockSecurityManager.authorize(same(securityPrincipal), any())).thenReturn(false); + AuthenticationAPI.SimpleAuthenticationRequest authenticationRequest = + AuthenticationAPI.SimpleAuthenticationRequest.newBuilder().setUsername(TEST_USERNAME) + .setPassword(TEST_PASSWORD).build(); + authenticationRequest.writeDelimitedTo(outputStream); + + AuthenticationAPI.SimpleAuthenticationResponse authenticationResponse = + AuthenticationAPI.SimpleAuthenticationResponse.parseDelimitedFrom(inputStream); + assertTrue(authenticationResponse.getAuthenticated()); + } + + @After + public void shutDown() throws IOException { + cache.close(); + socket.close(); + } + + + @Test + public void validateNoPermissions() throws Exception { + when(mockSecurityManager.authorize(securityPrincipal, READ_PERMISSION)).thenReturn(false); + when(mockSecurityManager.authorize(securityPrincipal, WRITE_PERMISSION)).thenReturn(false); + + verifyOperations(false, false); + } + + @Test + public void validateWritePermission() throws Exception { + when(mockSecurityManager.authorize(securityPrincipal, READ_PERMISSION)).thenReturn(false); + when(mockSecurityManager.authorize(securityPrincipal, WRITE_PERMISSION)).thenReturn(true); + + verifyOperations(false, true); + } + + @Test + public void validateReadPermission() throws Exception { + when(mockSecurityManager.authorize(securityPrincipal, READ_PERMISSION)).thenReturn(true); + when(mockSecurityManager.authorize(securityPrincipal, WRITE_PERMISSION)).thenReturn(false); + + verifyOperations(true, false); + } + + @Test + public void validateReadAndWritePermission() throws Exception { + when(mockSecurityManager.authorize(securityPrincipal, READ_PERMISSION)).thenReturn(true); + when(mockSecurityManager.authorize(securityPrincipal, WRITE_PERMISSION)).thenReturn(true); + + verifyOperations(true, true); + } + + private void verifyOperations(boolean readAllowed, boolean writeAllowed) throws Exception { --- End diff -- +1 thorough and clear tests. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---