[
https://issues.apache.org/jira/browse/KNOX-2777?focusedWorklogId=791098&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-791098
]
ASF GitHub Bot logged work on KNOX-2777:
----------------------------------------
Author: ASF GitHub Bot
Created on: 14/Jul/22 18:52
Start Date: 14/Jul/22 18:52
Worklog Time Spent: 10m
Work Description: moresandeep commented on code in PR #608:
URL: https://github.com/apache/knox/pull/608#discussion_r921454648
##########
gateway-server/src/main/java/org/apache/knox/gateway/config/impl/GatewayConfigImpl.java:
##########
@@ -293,6 +293,14 @@ public class GatewayConfigImpl extends Configuration
implements GatewayConfig {
private static final String GATEWAY_DATABASE_VERIFY_SERVER_CERT =
GATEWAY_CONFIG_FILE_PREFIX + ".database.ssl.verify.server.cert";
private static final String GATEWAY_DATABASE_TRUSTSTORE_FILE =
GATEWAY_CONFIG_FILE_PREFIX + ".database.ssl.truststore.file";
+ // Concurrent session properties
+ private static final String GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT
= GATEWAY_CONFIG_FILE_PREFIX + ".privileged.user.concurrent.session.limit";
Review Comment:
`priviledged.user` can be a static variable.
##########
gateway-server/src/main/java/org/apache/knox/gateway/config/impl/GatewayConfigImpl.java:
##########
@@ -293,6 +293,14 @@ public class GatewayConfigImpl extends Configuration
implements GatewayConfig {
private static final String GATEWAY_DATABASE_VERIFY_SERVER_CERT =
GATEWAY_CONFIG_FILE_PREFIX + ".database.ssl.verify.server.cert";
private static final String GATEWAY_DATABASE_TRUSTSTORE_FILE =
GATEWAY_CONFIG_FILE_PREFIX + ".database.ssl.truststore.file";
+ // Concurrent session properties
+ private static final String GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT
= GATEWAY_CONFIG_FILE_PREFIX + ".privileged.user.concurrent.session.limit";
+ private static final String
GATEWAY_NON_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT =
GATEWAY_CONFIG_FILE_PREFIX + ".non.privileged.user.concurrent.session.limit";
+ private static final int
GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT = 3;
+ private static final int
GATEWAY_NON_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT = 2;
+ private static final String GATEWAY_PRIVILEGED_USERS =
GATEWAY_CONFIG_FILE_PREFIX + ".privileged.users";
+ private static final String GATEWAY_NON_PRIVILEGED_USERS =
GATEWAY_CONFIG_FILE_PREFIX + ".non.privileged.users";
Review Comment:
nit: `non.priviledged.user` can be a static variable (using the previous
static variable) :)
##########
gateway-server/src/main/java/org/apache/knox/gateway/config/impl/GatewayConfigImpl.java:
##########
@@ -1335,4 +1343,49 @@ public int getJettyMaxFormKeys() {
return getInt(JETTY_MAX_FORM_KEYS, ContextHandler.DEFAULT_MAX_FORM_KEYS);
}
+ @Override
+ public int getPrivilegedUserConcurrentSessionLimit(){
+ int limit = getInt(GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT,
GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT);
+ if(limit < 0)
Review Comment:
nit: brackets :)
##########
gateway-server/src/main/java/org/apache/knox/gateway/config/impl/GatewayConfigImpl.java:
##########
@@ -1335,4 +1343,49 @@ public int getJettyMaxFormKeys() {
return getInt(JETTY_MAX_FORM_KEYS, ContextHandler.DEFAULT_MAX_FORM_KEYS);
}
+ @Override
+ public int getPrivilegedUserConcurrentSessionLimit(){
+ int limit = getInt(GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT,
GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT);
+ if(limit < 0)
+ return GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT;
+ else
+ return limit;
+ }
+
+ @Override
+ public int getNonPrivilegedUserConcurrentSessionLimit(){
+ int limit = getInt(GATEWAY_NON_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT,
GATEWAY_NON_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT);
+ if(limit < 0)
+ return GATEWAY_NON_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT;
+ else
+ return limit;
+ }
+
+ @Override
+ public Set<String> getPrivilegedUsers(){
+ Set<String> set = new HashSet<>();
+
+ String value = get( GATEWAY_PRIVILEGED_USERS );
+ if ((value != null) && (!value.trim().equals("")) ) {
Review Comment:
You can use `StringUtils.isBlank(value)` here (commons3 utils)
##########
gateway-server/src/main/java/org/apache/knox/gateway/config/impl/GatewayConfigImpl.java:
##########
@@ -1335,4 +1343,49 @@ public int getJettyMaxFormKeys() {
return getInt(JETTY_MAX_FORM_KEYS, ContextHandler.DEFAULT_MAX_FORM_KEYS);
}
+ @Override
+ public int getPrivilegedUserConcurrentSessionLimit(){
+ int limit = getInt(GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT,
GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT);
+ if(limit < 0)
+ return GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT;
+ else
+ return limit;
+ }
+
+ @Override
+ public int getNonPrivilegedUserConcurrentSessionLimit(){
+ int limit = getInt(GATEWAY_NON_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT,
GATEWAY_NON_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT);
+ if(limit < 0)
Review Comment:
nit: brackets
##########
gateway-server/src/main/java/org/apache/knox/gateway/config/impl/GatewayConfigImpl.java:
##########
@@ -1335,4 +1343,49 @@ public int getJettyMaxFormKeys() {
return getInt(JETTY_MAX_FORM_KEYS, ContextHandler.DEFAULT_MAX_FORM_KEYS);
}
+ @Override
+ public int getPrivilegedUserConcurrentSessionLimit(){
+ int limit = getInt(GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT,
GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT);
+ if(limit < 0)
+ return GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT;
+ else
+ return limit;
+ }
+
+ @Override
+ public int getNonPrivilegedUserConcurrentSessionLimit(){
+ int limit = getInt(GATEWAY_NON_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT,
GATEWAY_NON_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT);
+ if(limit < 0)
+ return GATEWAY_NON_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT;
+ else
+ return limit;
+ }
+
+ @Override
+ public Set<String> getPrivilegedUsers(){
+ Set<String> set = new HashSet<>();
+
+ String value = get( GATEWAY_PRIVILEGED_USERS );
+ if ((value != null) && (!value.trim().equals("")) ) {
+ List<String> temp = new ArrayList<>(Arrays.asList(value.split(",")));
+ temp.forEach(e -> { temp.set(temp.indexOf(e), e.trim()); });
Review Comment:
Interesting, you don't get a ConcurrentModificationException here modifying
list in place.
You can slightly improve it by
`temp.replaceAll(String::trim)` or if this does not work
`temp.stream().map(String::trim).collect(Collectors.toList())`
P.S. pardon any syntax errors I did not check it in my IDE
##########
gateway-server/src/main/java/org/apache/knox/gateway/config/impl/GatewayConfigImpl.java:
##########
@@ -1335,4 +1343,49 @@ public int getJettyMaxFormKeys() {
return getInt(JETTY_MAX_FORM_KEYS, ContextHandler.DEFAULT_MAX_FORM_KEYS);
}
+ @Override
+ public int getPrivilegedUserConcurrentSessionLimit(){
+ int limit = getInt(GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT,
GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT);
+ if(limit < 0)
+ return GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT;
+ else
+ return limit;
+ }
+
+ @Override
+ public int getNonPrivilegedUserConcurrentSessionLimit(){
+ int limit = getInt(GATEWAY_NON_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT,
GATEWAY_NON_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT);
+ if(limit < 0)
+ return GATEWAY_NON_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT;
+ else
+ return limit;
+ }
+
+ @Override
+ public Set<String> getPrivilegedUsers(){
+ Set<String> set = new HashSet<>();
+
+ String value = get( GATEWAY_PRIVILEGED_USERS );
+ if ((value != null) && (!value.trim().equals("")) ) {
+ List<String> temp = new ArrayList<>(Arrays.asList(value.split(",")));
+ temp.forEach(e -> { temp.set(temp.indexOf(e), e.trim()); });
+ set.addAll(temp);
+ }
+
+ return set;
+ }
+
+ @Override
+ public Set<String> getNonPrivilegedUsers(){
Review Comment:
All the same comments as above.
##########
gateway-util-common/src/main/java/org/apache/knox/gateway/util/ConcurrentSessionVerifier.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.knox.gateway.util;
+
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+public class ConcurrentSessionVerifier {
+ private static Set<String> privilegedUsers = new HashSet<>();
+ private static Set<String> nonPrivilegedUsers = new HashSet<>();;
+ private static int privilegedUserConcurrentSessionLimit = 3;
+ private static int nonPrivilegedUserConcurrentSessionLimit = 2;
+ private static Map<String, Integer> concurrentSessionCounter = new
HashMap<>();
+
+ private ConcurrentSessionVerifier(){}
+
+ public static void init(Set<String> _privilegedUsers, Set<String>
_nonPrivilegedUsers, int _privilegedUserConcurrentSessionLimit, int
_nonPrivilegedUserConcurrentSessionLimit){
Review Comment:
We don't use _ in front of variable names, I see that you did not use it
elsewhere so avoid using it just to be more consistant.
##########
gateway-util-common/src/main/java/org/apache/knox/gateway/util/ConcurrentSessionVerifier.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.knox.gateway.util;
+
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+public class ConcurrentSessionVerifier {
+ private static Set<String> privilegedUsers = new HashSet<>();
+ private static Set<String> nonPrivilegedUsers = new HashSet<>();;
+ private static int privilegedUserConcurrentSessionLimit = 3;
+ private static int nonPrivilegedUserConcurrentSessionLimit = 2;
+ private static Map<String, Integer> concurrentSessionCounter = new
HashMap<>();
+
+ private ConcurrentSessionVerifier(){}
+
+ public static void init(Set<String> _privilegedUsers, Set<String>
_nonPrivilegedUsers, int _privilegedUserConcurrentSessionLimit, int
_nonPrivilegedUserConcurrentSessionLimit){
+ privilegedUsers = _privilegedUsers;
+ nonPrivilegedUsers = _nonPrivilegedUsers;
+ privilegedUserConcurrentSessionLimit =
_privilegedUserConcurrentSessionLimit;
+ nonPrivilegedUserConcurrentSessionLimit =
_nonPrivilegedUserConcurrentSessionLimit;
+ concurrentSessionCounter = new HashMap<>();
+ }
+
+ public static boolean verifySessionForUser(String username){
+ if(!privilegedUsers.contains(username) &&
!nonPrivilegedUsers.contains(username)) {
+ return true;
+ }
+ if(!concurrentSessionCounter.containsKey(username)){
+ concurrentSessionCounter.put(username, 0);
+ }
+
+ if((privilegedUsers.contains(username) &&
!(concurrentSessionCounter.get(username) <
privilegedUserConcurrentSessionLimit)) ||
(nonPrivilegedUsers.contains(username) &&
!(concurrentSessionCounter.get(username) <
nonPrivilegedUserConcurrentSessionLimit))){
+ return false;
+ }
+
+ incrementConcurrentSessionCount(username);
+ return true;
+ }
+
+ private static void incrementConcurrentSessionCount(String username){
Review Comment:
same comment as above.
##########
gateway-util-common/src/main/java/org/apache/knox/gateway/util/ConcurrentSessionVerifier.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.knox.gateway.util;
+
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+public class ConcurrentSessionVerifier {
+ private static Set<String> privilegedUsers = new HashSet<>();
+ private static Set<String> nonPrivilegedUsers = new HashSet<>();;
+ private static int privilegedUserConcurrentSessionLimit = 3;
+ private static int nonPrivilegedUserConcurrentSessionLimit = 2;
+ private static Map<String, Integer> concurrentSessionCounter = new
HashMap<>();
Review Comment:
The name suggests this is not thread safe, should this be
`ConcurrentHashMap` ?
##########
gateway-server/src/main/java/org/apache/knox/gateway/config/impl/GatewayConfigImpl.java:
##########
@@ -1335,4 +1343,49 @@ public int getJettyMaxFormKeys() {
return getInt(JETTY_MAX_FORM_KEYS, ContextHandler.DEFAULT_MAX_FORM_KEYS);
}
+ @Override
+ public int getPrivilegedUserConcurrentSessionLimit(){
+ int limit = getInt(GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT,
GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT);
+ if(limit < 0)
+ return GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT;
+ else
+ return limit;
+ }
+
+ @Override
+ public int getNonPrivilegedUserConcurrentSessionLimit(){
+ int limit = getInt(GATEWAY_NON_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT,
GATEWAY_NON_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT);
+ if(limit < 0)
+ return GATEWAY_NON_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT;
+ else
+ return limit;
+ }
+
+ @Override
+ public Set<String> getPrivilegedUsers(){
+ Set<String> set = new HashSet<>();
+
+ String value = get( GATEWAY_PRIVILEGED_USERS );
+ if ((value != null) && (!value.trim().equals("")) ) {
+ List<String> temp = new ArrayList<>(Arrays.asList(value.split(",")));
+ temp.forEach(e -> { temp.set(temp.indexOf(e), e.trim()); });
+ set.addAll(temp);
Review Comment:
Wait, if you are adding it to set, just add the trimmed value to set, easier
and cleaner.
`temp.forEach(e -> { set.add(e.trim()) });`
##########
gateway-util-common/src/main/java/org/apache/knox/gateway/util/ConcurrentSessionVerifier.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.knox.gateway.util;
+
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+public class ConcurrentSessionVerifier {
+ private static Set<String> privilegedUsers = new HashSet<>();
+ private static Set<String> nonPrivilegedUsers = new HashSet<>();;
+ private static int privilegedUserConcurrentSessionLimit = 3;
+ private static int nonPrivilegedUserConcurrentSessionLimit = 2;
+ private static Map<String, Integer> concurrentSessionCounter = new
HashMap<>();
+
+ private ConcurrentSessionVerifier(){}
+
+ public static void init(Set<String> _privilegedUsers, Set<String>
_nonPrivilegedUsers, int _privilegedUserConcurrentSessionLimit, int
_nonPrivilegedUserConcurrentSessionLimit){
+ privilegedUsers = _privilegedUsers;
+ nonPrivilegedUsers = _nonPrivilegedUsers;
+ privilegedUserConcurrentSessionLimit =
_privilegedUserConcurrentSessionLimit;
+ nonPrivilegedUserConcurrentSessionLimit =
_nonPrivilegedUserConcurrentSessionLimit;
+ concurrentSessionCounter = new HashMap<>();
+ }
+
+ public static boolean verifySessionForUser(String username){
Review Comment:
i am scared of this method. This is static, so we have to be very careful
about the state here especially since we are dealing with usernames. Should
this be synchronized? the data structures used in the method are not thread
safe which will cause issues.
##########
gateway-util-common/src/main/java/org/apache/knox/gateway/util/ConcurrentSessionVerifier.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.knox.gateway.util;
+
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+public class ConcurrentSessionVerifier {
+ private static Set<String> privilegedUsers = new HashSet<>();
+ private static Set<String> nonPrivilegedUsers = new HashSet<>();;
+ private static int privilegedUserConcurrentSessionLimit = 3;
+ private static int nonPrivilegedUserConcurrentSessionLimit = 2;
+ private static Map<String, Integer> concurrentSessionCounter = new
HashMap<>();
+
+ private ConcurrentSessionVerifier(){}
+
+ public static void init(Set<String> _privilegedUsers, Set<String>
_nonPrivilegedUsers, int _privilegedUserConcurrentSessionLimit, int
_nonPrivilegedUserConcurrentSessionLimit){
+ privilegedUsers = _privilegedUsers;
+ nonPrivilegedUsers = _nonPrivilegedUsers;
+ privilegedUserConcurrentSessionLimit =
_privilegedUserConcurrentSessionLimit;
+ nonPrivilegedUserConcurrentSessionLimit =
_nonPrivilegedUserConcurrentSessionLimit;
+ concurrentSessionCounter = new HashMap<>();
Review Comment:
The name suggests this is not thread safe, should this be
`ConcurrentHashMap` ?
##########
gateway-server/src/main/java/org/apache/knox/gateway/config/impl/GatewayConfigImpl.java:
##########
@@ -1335,4 +1343,49 @@ public int getJettyMaxFormKeys() {
return getInt(JETTY_MAX_FORM_KEYS, ContextHandler.DEFAULT_MAX_FORM_KEYS);
}
+ @Override
+ public int getPrivilegedUserConcurrentSessionLimit(){
+ int limit = getInt(GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT,
GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT);
+ if(limit < 0)
+ return GATEWAY_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT;
+ else
+ return limit;
+ }
+
+ @Override
+ public int getNonPrivilegedUserConcurrentSessionLimit(){
+ int limit = getInt(GATEWAY_NON_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT,
GATEWAY_NON_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT);
+ if(limit < 0)
+ return GATEWAY_NON_PRIVILEGED_USER_CONCURRENT_SESSION_LIMIT_DEFAULT;
+ else
+ return limit;
+ }
+
+ @Override
+ public Set<String> getPrivilegedUsers(){
+ Set<String> set = new HashSet<>();
Review Comment:
set is not a good name since it is too close to the built in type Set
##########
gateway-util-common/src/main/java/org/apache/knox/gateway/util/ConcurrentSessionVerifier.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.knox.gateway.util;
+
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+public class ConcurrentSessionVerifier {
+ private static Set<String> privilegedUsers = new HashSet<>();
+ private static Set<String> nonPrivilegedUsers = new HashSet<>();;
+ private static int privilegedUserConcurrentSessionLimit = 3;
+ private static int nonPrivilegedUserConcurrentSessionLimit = 2;
+ private static Map<String, Integer> concurrentSessionCounter = new
HashMap<>();
+
+ private ConcurrentSessionVerifier(){}
+
+ public static void init(Set<String> _privilegedUsers, Set<String>
_nonPrivilegedUsers, int _privilegedUserConcurrentSessionLimit, int
_nonPrivilegedUserConcurrentSessionLimit){
+ privilegedUsers = _privilegedUsers;
+ nonPrivilegedUsers = _nonPrivilegedUsers;
+ privilegedUserConcurrentSessionLimit =
_privilegedUserConcurrentSessionLimit;
+ nonPrivilegedUserConcurrentSessionLimit =
_nonPrivilegedUserConcurrentSessionLimit;
+ concurrentSessionCounter = new HashMap<>();
+ }
+
+ public static boolean verifySessionForUser(String username){
+ if(!privilegedUsers.contains(username) &&
!nonPrivilegedUsers.contains(username)) {
+ return true;
+ }
+ if(!concurrentSessionCounter.containsKey(username)){
+ concurrentSessionCounter.put(username, 0);
+ }
+
+ if((privilegedUsers.contains(username) &&
!(concurrentSessionCounter.get(username) <
privilegedUserConcurrentSessionLimit)) ||
(nonPrivilegedUsers.contains(username) &&
!(concurrentSessionCounter.get(username) <
nonPrivilegedUserConcurrentSessionLimit))){
+ return false;
+ }
+
+ incrementConcurrentSessionCount(username);
+ return true;
+ }
+
+ private static void incrementConcurrentSessionCount(String username){
+ int count = concurrentSessionCounter.get(username);
+ count++;
+ concurrentSessionCounter.put(username, count);
+ }
+
+ public static void sessionEndedForUser(String username){
Review Comment:
same comment as above.
Issue Time Tracking
-------------------
Worklog Id: (was: 791098)
Time Spent: 0.5h (was: 20m)
> Implement concurrent session verifier
> -------------------------------------
>
> Key: KNOX-2777
> URL: https://issues.apache.org/jira/browse/KNOX-2777
> Project: Apache Knox
> Issue Type: Sub-task
> Components: Server
> Affects Versions: 2.0.0
> Reporter: Sandor Molnar
> Priority: Major
> Fix For: 2.0.0
>
> Time Spent: 0.5h
> Remaining Estimate: 0h
>
> The following needs to be implemented in the scope of this JIRA:
> * we need 4 new Gateway-level configurations:
> ** privileged user list (defaults to an empty collection)
> ** non-privileged user list (defaults to an empty collection)
> ** session limit for privileged users (defaults to 3)
> ** session limit for non-privileged users (defaults to 2)
> * In addition to the new configs, a verifier has to be implemented that
> enforces the following business logic: if a user is listed in the
> above-introduced privileged/non-privileged collection AND is about to pass a
> configured session limit the verification should fail. The verification
> should succeed if the given user is declared neither a privileged nor a
> non-privileged user.
> The new verifier implementation may be placed in the {{gateway-util-common}}
> project for now.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)