761417898 commented on code in PR #16494: URL: https://github.com/apache/iotdb/pull/16494#discussion_r2386596199
########## iotdb-core/datanode/src/main/java/org/apache/iotdb/db/auth/LoginLockManager.java: ########## @@ -0,0 +1,371 @@ +/* + * 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.iotdb.db.auth; + +import org.apache.iotdb.db.conf.IoTDBDescriptor; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.util.Deque; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedDeque; +import java.util.concurrent.ConcurrentMap; + +public class LoginLockManager { + private static final Logger LOGGER = LoggerFactory.getLogger(LoginLockManager.class); + + // Configuration parameters + private final int failedLoginAttempts; + private final int failedLoginAttemptsPerUser; + private final int passwordLockTimeMinutes; + + // Lock records storage (in-memory only) + private final ConcurrentMap<Long, UserLockInfo> userLocks = new ConcurrentHashMap<>(); + private final ConcurrentMap<String, UserLockInfo> userIpLocks = new ConcurrentHashMap<>(); + + // Exempt users who should never be locked (only valid if request is from local host) + private final Set<Long> exemptUsers; + + public LoginLockManager() { + this( + IoTDBDescriptor.getInstance().getConfig().getFailedLoginAttempts(), + IoTDBDescriptor.getInstance().getConfig().getFailedLoginAttemptsPerUser(), + IoTDBDescriptor.getInstance().getConfig().getPasswordLockTimeMinutes()); + } + + public LoginLockManager( + int failedLoginAttempts, int failedLoginAttemptsPerUser, int passwordLockTimeMinutes) { + // Initialize exempt users + this.exemptUsers = new HashSet<>(); + this.exemptUsers.add(10000L); // root + + // Set and validate failedLoginAttempts (IP level) + if (failedLoginAttempts == -1) { + this.failedLoginAttempts = -1; // Completely disable IP-level restrictions + } else { + this.failedLoginAttempts = failedLoginAttempts >= 1 ? failedLoginAttempts : 5; + } + + // Set and validate failedLoginAttemptsPerUser (user level) + if (failedLoginAttemptsPerUser == -1) { + // If IP-level is enabled, user-level cannot be disabled + if (this.failedLoginAttempts != -1) { + this.failedLoginAttemptsPerUser = 1000; // Default user-level value + LOGGER.error( + "User-level login attempts cannot be disabled when IP-level is enabled. " + + "Setting user-level attempts to default (1000)"); + } else { + this.failedLoginAttemptsPerUser = -1; // Both are disabled + } + } else { + this.failedLoginAttemptsPerUser = + failedLoginAttemptsPerUser >= 1 ? failedLoginAttemptsPerUser : 1000; + } + + // Set and validate passwordLockTimeMinutes (default 10, minimum 1) + this.passwordLockTimeMinutes = passwordLockTimeMinutes >= 1 ? passwordLockTimeMinutes : 10; + + // Log final effective configuration + LOGGER.info( + "Login lock manager initialized with: IP-level attempts={}, User-level attempts={}, Lock time={} minutes", + this.failedLoginAttempts == -1 ? "disabled" : this.failedLoginAttempts, + this.failedLoginAttemptsPerUser == -1 ? "disabled" : this.failedLoginAttemptsPerUser, + this.passwordLockTimeMinutes); + } + + /** Inner class to store user lock information */ + static class UserLockInfo { + // Deque to store timestamps of failed attempts (milliseconds) + private final Deque<Long> failureTimestamps = new ConcurrentLinkedDeque<>(); + + void addFailureTime(long timestamp) { + failureTimestamps.addLast(timestamp); + } + + void removeOldFailures(long cutoffTime) { + // Remove timestamps older than cutoffTime + while (!failureTimestamps.isEmpty() && failureTimestamps.peekFirst() < cutoffTime) { + failureTimestamps.pollFirst(); + } + } + + int getFailureCount() { + return failureTimestamps.size(); + } + } + + /** + * Check if user or user@ip is locked + * + * @param userId user ID + * @param ip IP address + * @return true if locked, false otherwise + */ + public boolean checkLock(long userId, String ip) { + cleanExpiredLocks(); // Clean expired records (no failures in window) Review Comment: fixed -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
