You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by nc...@apache.org on 2016/02/23 22:21:49 UTC

[04/11] ambari git commit: AMBARI-14798. Users cannot login with uppercase username (Oliver Szabo via rlevas)

AMBARI-14798. Users cannot login with uppercase username (Oliver Szabo via rlevas)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/2b42559f
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/2b42559f
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/2b42559f

Branch: refs/heads/branch-dev-patch-upgrade
Commit: 2b42559f516d0f45072174dac5f9d230b8e7c31c
Parents: f7ebe91
Author: Oliver Szabo <os...@hortonworks.com>
Authored: Tue Feb 23 11:14:02 2016 -0500
Committer: Robert Levas <rl...@hortonworks.com>
Committed: Tue Feb 23 11:14:02 2016 -0500

----------------------------------------------------------------------
 .../api/services/ActiveWidgetLayoutService.java | 10 +--
 .../api/services/UserAuthorizationService.java  |  4 +-
 .../api/services/UserPrivilegeService.java      |  3 +-
 .../ambari/server/api/services/UserService.java |  3 +-
 .../services/ActiveWidgetLayoutServiceTest.java | 76 ++++++++++++++++++++
 .../services/UserAuthorizationServiceTest.java  | 12 ++++
 .../api/services/UserPrivilegeServiceTest.java  | 13 ++++
 .../server/api/services/UserServiceTest.java    | 71 ++++++++++++++++++
 8 files changed, 182 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/2b42559f/ambari-server/src/main/java/org/apache/ambari/server/api/services/ActiveWidgetLayoutService.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/ActiveWidgetLayoutService.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/ActiveWidgetLayoutService.java
index c4403df..a0c3386 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/ActiveWidgetLayoutService.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/ActiveWidgetLayoutService.java
@@ -17,25 +17,21 @@
  */
 package org.apache.ambari.server.api.services;
 
-import com.sun.jersey.core.util.Base64;
 import org.apache.ambari.server.api.resources.ResourceInstance;
 import org.apache.ambari.server.controller.spi.Resource;
 
-import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
-import javax.ws.rs.POST;
 import javax.ws.rs.PUT;
-import javax.ws.rs.Path;
-import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
-import java.nio.charset.Charset;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.commons.lang.StringUtils;
+
 /**
  * WidgetLayout Service
  */
@@ -73,7 +69,7 @@ public class ActiveWidgetLayoutService extends BaseService {
 
   private ResourceInstance createResource(String widgetLayoutId) {
     Map<Resource.Type,String> mapIds = new HashMap<Resource.Type, String>();
-    mapIds.put(Resource.Type.User, userName);
+    mapIds.put(Resource.Type.User, StringUtils.lowerCase(userName));
     return createResource(Resource.Type.ActiveWidgetLayout, mapIds);
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/2b42559f/ambari-server/src/main/java/org/apache/ambari/server/api/services/UserAuthorizationService.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/UserAuthorizationService.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/UserAuthorizationService.java
index 6861d3d..c288fdb 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/UserAuthorizationService.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/UserAuthorizationService.java
@@ -32,6 +32,8 @@ import javax.ws.rs.core.UriInfo;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.commons.lang.StringUtils;
+
 /**
  * UserAuthorizationService is a read-only service responsible for user authorization resource requests.
  * <p/>
@@ -94,7 +96,7 @@ public class UserAuthorizationService extends BaseService {
    */
   protected ResourceInstance createAuthorizationResource(String authorizationId) {
     Map<Resource.Type, String> mapIds = new HashMap<Resource.Type, String>();
-    mapIds.put(Resource.Type.User, username);
+    mapIds.put(Resource.Type.User, StringUtils.lowerCase(username));
     mapIds.put(Resource.Type.UserAuthorization, authorizationId);
     return createResource(Resource.Type.UserAuthorization, mapIds);
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/2b42559f/ambari-server/src/main/java/org/apache/ambari/server/api/services/UserPrivilegeService.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/UserPrivilegeService.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/UserPrivilegeService.java
index 80769cf..86c4995 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/UserPrivilegeService.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/UserPrivilegeService.java
@@ -29,6 +29,7 @@ import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
 
+import org.apache.commons.lang.StringUtils;
 /**
  *  Service responsible for user privilege resource requests.
  */
@@ -72,7 +73,7 @@ public class UserPrivilegeService extends PrivilegeService {
   @Override
   protected ResourceInstance createPrivilegeResource(String privilegeId) {
     final Map<Resource.Type, String> mapIds = new HashMap<Resource.Type, String>();
-    mapIds.put(Resource.Type.User, userName);
+    mapIds.put(Resource.Type.User, StringUtils.lowerCase(userName));
     mapIds.put(Resource.Type.UserPrivilege, privilegeId);
     return createResource(Resource.Type.UserPrivilege, mapIds);
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/2b42559f/ambari-server/src/main/java/org/apache/ambari/server/api/services/UserService.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/UserService.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/UserService.java
index fea5eca..c46c373 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/UserService.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/UserService.java
@@ -31,6 +31,7 @@ import javax.ws.rs.core.UriInfo;
 
 import org.apache.ambari.server.api.resources.ResourceInstance;
 import org.apache.ambari.server.controller.spi.Resource;
+
 import org.apache.commons.lang.StringUtils;
 
 import java.util.Collections;
@@ -171,6 +172,6 @@ public class UserService extends BaseService {
    */
   private ResourceInstance createUserResource(String userName) {
     return createResource(Resource.Type.User,
-        Collections.singletonMap(Resource.Type.User, userName));
+        Collections.singletonMap(Resource.Type.User, StringUtils.lowerCase(userName)));
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/2b42559f/ambari-server/src/test/java/org/apache/ambari/server/api/services/ActiveWidgetLayoutServiceTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/services/ActiveWidgetLayoutServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/api/services/ActiveWidgetLayoutServiceTest.java
new file mode 100644
index 0000000..c459f7c
--- /dev/null
+++ b/ambari-server/src/test/java/org/apache/ambari/server/api/services/ActiveWidgetLayoutServiceTest.java
@@ -0,0 +1,76 @@
+/*
+ * 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.ambari.server.api.services;
+
+import org.apache.ambari.server.api.resources.ResourceInstance;
+import org.apache.ambari.server.controller.spi.Resource;
+
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+
+import org.apache.ambari.server.orm.entities.WidgetLayoutEntity;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Unit tests for ActiveWidgetLayoutService.
+ */
+public class ActiveWidgetLayoutServiceTest {
+
+  @Test
+  public void testCreateResourceWithUppercaseUsername() {
+    // GIVEN
+    ActiveWidgetLayoutService activeWidgetLayoutService = new TestActiveWidgetLayoutService("MyUser");
+    // WHEN
+    Response response = activeWidgetLayoutService.getServices(null, null, null);
+    // THEN
+    assertEquals("myuser", ((WidgetLayoutEntity) response.getEntity()).getUserName());
+  }
+
+  private class TestActiveWidgetLayoutService extends ActiveWidgetLayoutService {
+    public TestActiveWidgetLayoutService(String username) {
+      super(username);
+    }
+
+    @Override
+    protected Response handleRequest(HttpHeaders headers, String body, UriInfo uriInfo,
+                                     Request.Type requestType, final ResourceInstance resource) {
+      return new Response() {
+        @Override
+        public Object getEntity() {
+          WidgetLayoutEntity entity = new WidgetLayoutEntity();
+          entity.setUserName(resource.getKeyValueMap().get(Resource.Type.User));
+          return entity;
+        }
+
+        @Override
+        public int getStatus() {
+          return 0;
+        }
+
+        @Override
+        public MultivaluedMap<String, Object> getMetadata() {
+          return null;
+        }
+      };
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/2b42559f/ambari-server/src/test/java/org/apache/ambari/server/api/services/UserAuthorizationServiceTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/services/UserAuthorizationServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/api/services/UserAuthorizationServiceTest.java
index 9627d19..c3270bb 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/api/services/UserAuthorizationServiceTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/api/services/UserAuthorizationServiceTest.java
@@ -21,6 +21,8 @@ package org.apache.ambari.server.api.services;
 import org.apache.ambari.server.api.resources.ResourceInstance;
 import org.apache.ambari.server.api.services.parsers.RequestBodyParser;
 import org.apache.ambari.server.api.services.serializers.ResultSerializer;
+import org.apache.ambari.server.controller.spi.Resource;
+import org.junit.Test;
 
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.UriInfo;
@@ -54,6 +56,16 @@ public class UserAuthorizationServiceTest extends BaseServiceTest {
     return listInvocations;
   }
 
+  @Test
+  public void testCreateAuthorizationResourceWithUppercaseUsername() {
+    // GIVEN
+    UserAuthorizationService userAuthorizationService= new UserAuthorizationService("Jdoe");
+    // WHEN
+    ResourceInstance result = userAuthorizationService.createAuthorizationResource("id");
+    // THEN
+    assertEquals("jdoe", result.getKeyValueMap().get(Resource.Type.User));
+  }
+
 
   private class TestUserAuthorizationService extends UserAuthorizationService {
     private String id;

http://git-wip-us.apache.org/repos/asf/ambari/blob/2b42559f/ambari-server/src/test/java/org/apache/ambari/server/api/services/UserPrivilegeServiceTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/services/UserPrivilegeServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/api/services/UserPrivilegeServiceTest.java
index 269315a..db2d38a 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/api/services/UserPrivilegeServiceTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/api/services/UserPrivilegeServiceTest.java
@@ -33,10 +33,13 @@ import junit.framework.Assert;
 import org.apache.ambari.server.api.resources.ResourceInstance;
 import org.apache.ambari.server.api.services.parsers.RequestBodyParser;
 import org.apache.ambari.server.api.services.serializers.ResultSerializer;
+import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.controller.spi.Resource.Type;
 import org.easymock.EasyMock;
 import org.junit.Test;
 
+import static org.junit.Assert.assertEquals;
+
 /**
  * Unit tests for GroupService.
  */
@@ -82,6 +85,16 @@ public class UserPrivilegeServiceTest extends BaseServiceTest {
     }
   }
 
+  @Test
+  public void testCreatePrivilegeResourcesWithUppercaseUsername() {
+    // GIVEN
+    UserPrivilegeService userPrivilegeService = new UserPrivilegeService("User");
+    // WHEN
+    ResourceInstance result = userPrivilegeService.createPrivilegeResource("test");
+    // THEN
+    assertEquals( "user", result.getKeyValueMap().get(Resource.Type.User));
+  }
+
   private class TestUserPrivilegeService extends UserPrivilegeService {
 
     public TestUserPrivilegeService() {

http://git-wip-us.apache.org/repos/asf/ambari/blob/2b42559f/ambari-server/src/test/java/org/apache/ambari/server/api/services/UserServiceTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/services/UserServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/api/services/UserServiceTest.java
new file mode 100644
index 0000000..0ed0a66
--- /dev/null
+++ b/ambari-server/src/test/java/org/apache/ambari/server/api/services/UserServiceTest.java
@@ -0,0 +1,71 @@
+/*
+ * 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.ambari.server.api.services;
+
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+
+import org.apache.ambari.server.api.resources.ResourceInstance;
+import org.apache.ambari.server.controller.spi.Resource;
+import org.apache.ambari.server.orm.entities.UserEntity;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Unit tests for UserService.
+ */
+public class UserServiceTest {
+
+  @Test
+  public void testCreateResourcesWithUppercaseUsername() {
+    // GIVEN
+    UserService userService = new TestUserService();
+    // WHEN
+    Response response = userService.getUser(null, null, null, "MyUser");
+    // THEN
+    assertEquals("myuser", ((UserEntity) response.getEntity()).getUserName());
+  }
+
+  class TestUserService extends UserService {
+    @Override
+    protected Response handleRequest(HttpHeaders headers, String body, UriInfo uriInfo,
+                                     Request.Type requestType, final ResourceInstance resource) {
+      return new Response() {
+        @Override
+        public Object getEntity() {
+          UserEntity entity = new UserEntity();
+          entity.setUserName(resource.getKeyValueMap().get(Resource.Type.User));
+          return entity;
+        }
+
+        @Override
+        public int getStatus() {
+          return 0;
+        }
+
+        @Override
+        public MultivaluedMap<String, Object> getMetadata() {
+          return null;
+        }
+      };
+    }
+  }
+}