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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to