Skip to content

Commit

Permalink
Merge branch 'main' into fix/add-edit-query-table-dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
pranita09 authored Dec 18, 2024
2 parents 026c0aa + 4b9948d commit b4c5ef8
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class CatalogExceptionMessage {
public static final String PASSWORD_INVALID_FORMAT =
"Password must be of minimum 8 characters, with one special, one Upper, one lower case character, and one Digit.";
public static final String MAX_FAILED_LOGIN_ATTEMPT =
"Failed Login Attempts Exceeded. Please try after some time.";
"Failed Login Attempts Exceeded. Use Forgot Password or retry after some time.";

public static final String INCORRECT_OLD_PASSWORD = "INCORRECT_OLD_PASSWORD";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.openmetadata.service.secrets.SecretsManager;
import org.openmetadata.service.secrets.SecretsManagerFactory;
import org.openmetadata.service.security.JwtFilter;
import org.openmetadata.service.security.auth.LoginAttemptCache;
import org.openmetadata.service.util.JsonUtils;
import org.openmetadata.service.util.OpenMetadataConnectionBuilder;
import org.openmetadata.service.util.RestUtil;
Expand Down Expand Up @@ -249,6 +250,10 @@ private void postUpdate(SettingsType settingsType) {
WorkflowHandler workflowHandler = WorkflowHandler.getInstance();
workflowHandler.initializeNewProcessEngine(workflowHandler.getProcessEngineConfiguration());
}

if (settingsType == SettingsType.LOGIN_CONFIGURATION) {
LoginAttemptCache.updateLoginConfiguration();
}
}

public void updateSetting(Settings setting) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private static void createDefaultConfiguration(OpenMetadataApplicationConfig app
.withConfigValue(
new LoginConfiguration()
.withMaxLoginFailAttempts(3)
.withAccessBlockTime(600)
.withAccessBlockTime(30)
.withJwtTokenExpiryTime(3600));
systemRepository.createNewSetting(setting);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ public class BasicAuthenticator implements AuthenticatorHandler {
private static final int HASHING_COST = 12;
private UserRepository userRepository;
private TokenRepository tokenRepository;
private LoginAttemptCache loginAttemptCache;
private AuthorizerConfiguration authorizerConfiguration;
private boolean isSelfSignUpAvailable;

Expand All @@ -111,7 +110,6 @@ public void init(OpenMetadataApplicationConfig config) {
this.userRepository = (UserRepository) Entity.getEntityRepository(Entity.USER);
this.tokenRepository = Entity.getTokenRepository();
this.authorizerConfiguration = config.getAuthorizerConfiguration();
this.loginAttemptCache = new LoginAttemptCache();
this.isSelfSignUpAvailable = config.getAuthenticationConfiguration().getEnableSelfSignup();
}

Expand Down Expand Up @@ -267,7 +265,7 @@ public void resetUserPasswordWithToken(UriInfo uriInfo, PasswordResetRequest req
LOG.error("Error in sending Password Change Mail to User. Reason : " + ex.getMessage(), ex);
throw new CustomExceptionMessage(424, FAILED_SEND_EMAIL, EMAIL_SENDING_ISSUE);
}
loginAttemptCache.recordSuccessfulLogin(request.getUsername());
LoginAttemptCache.getInstance().recordSuccessfulLogin(request.getUsername());
}

@Override
Expand Down Expand Up @@ -312,7 +310,7 @@ public void changeUserPwdWithOldPwd(
storedUser.getAuthenticationMechanism().setConfig(storedBasicAuthMechanism);
PutResponse<User> response = userRepository.createOrUpdate(uriInfo, storedUser);
// remove login/details from cache
loginAttemptCache.recordSuccessfulLogin(userName);
LoginAttemptCache.getInstance().recordSuccessfulLogin(userName);

// in case admin updates , send email to user
if (request.getRequestType() == USER && getSmtpSettings().getEnableSmtpServer()) {
Expand Down Expand Up @@ -476,23 +474,23 @@ public JwtResponse loginUser(LoginRequest loginRequest) throws IOException, Temp

@Override
public void checkIfLoginBlocked(String email) {
if (loginAttemptCache.isLoginBlocked(email)) {
if (LoginAttemptCache.getInstance().isLoginBlocked(email)) {
throw new AuthenticationException(MAX_FAILED_LOGIN_ATTEMPT);
}
}

@Override
public void recordFailedLoginAttempt(String email, String userName)
throws TemplateException, IOException {
loginAttemptCache.recordFailedLogin(email);
int failedLoginAttempt = loginAttemptCache.getUserFailedLoginCount(email);
LoginAttemptCache.getInstance().recordFailedLogin(email);
int failedLoginAttempt = LoginAttemptCache.getInstance().getUserFailedLoginCount(email);
if (failedLoginAttempt == SecurityUtil.getLoginConfiguration().getMaxLoginFailAttempts()) {
sendAccountStatus(
userName,
email,
"Multiple Failed Login Attempts.",
String.format(
"Someone is trying to access your account. Login is Blocked for %s minutes. Please change your password.",
"Someone is trying to access your account. Login is Blocked for %s seconds. Please change your password.",
SecurityUtil.getLoginConfiguration().getAccessBlockTime()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public class LdapAuthenticator implements AuthenticatorHandler {
private RoleRepository roleRepository;
private UserRepository userRepository;
private TokenRepository tokenRepository;
private LoginAttemptCache loginAttemptCache;
private LdapConfiguration ldapConfiguration;
private LDAPConnectionPool ldapLookupConnectionPool;
private boolean isSelfSignUpEnabled;
Expand All @@ -102,7 +101,6 @@ public void init(OpenMetadataApplicationConfig config) {
this.roleRepository = (RoleRepository) Entity.getEntityRepository(Entity.ROLE);
this.tokenRepository = Entity.getTokenRepository();
this.ldapConfiguration = config.getAuthenticationConfiguration().getLdapConfiguration();
this.loginAttemptCache = new LoginAttemptCache();
this.isSelfSignUpEnabled = config.getAuthenticationConfiguration().getEnableSelfSignup();
}

Expand Down Expand Up @@ -176,16 +174,16 @@ private User checkAndCreateUser(String userDn, String email, String userName) th

@Override
public void checkIfLoginBlocked(String email) {
if (loginAttemptCache.isLoginBlocked(email)) {
if (LoginAttemptCache.getInstance().isLoginBlocked(email)) {
throw new AuthenticationException(MAX_FAILED_LOGIN_ATTEMPT);
}
}

@Override
public void recordFailedLoginAttempt(String email, String userName)
throws TemplateException, IOException {
loginAttemptCache.recordFailedLogin(email);
int failedLoginAttempt = loginAttemptCache.getUserFailedLoginCount(email);
LoginAttemptCache.getInstance().recordFailedLogin(email);
int failedLoginAttempt = LoginAttemptCache.getInstance().getUserFailedLoginCount(email);
if (failedLoginAttempt == SecurityUtil.getLoginConfiguration().getMaxLoginFailAttempts()) {
EmailUtil.sendAccountStatus(
userName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import io.dropwizard.logback.shaded.guava.annotations.VisibleForTesting;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import lombok.NonNull;
Expand All @@ -12,10 +13,11 @@
import org.openmetadata.service.resources.settings.SettingsCache;

public class LoginAttemptCache {
private static LoginAttemptCache INSTANCE;
private int maxAttempt = 3;
private final LoadingCache<String, Integer> attemptsCache;

public LoginAttemptCache() {
private LoginAttemptCache() {
LoginConfiguration loginConfiguration =
SettingsCache.getSetting(SettingsType.LOGIN_CONFIGURATION, LoginConfiguration.class);
long accessBlockTime = 600;
Expand All @@ -35,6 +37,18 @@ public LoginAttemptCache() {
});
}

public static LoginAttemptCache getInstance() {
if (INSTANCE == null) {
INSTANCE = new LoginAttemptCache();
}
return INSTANCE;
}

public static void updateLoginConfiguration() {
INSTANCE = new LoginAttemptCache();
}

@VisibleForTesting
public LoginAttemptCache(int maxAttempt, int blockTimeInSec) {
this.maxAttempt = maxAttempt;
attemptsCache =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void get_Login_Configuration_200_OK() throws IOException {
LoginConfiguration loginConfiguration =
TestUtils.get(target, LoginConfiguration.class, TEST_AUTH_HEADERS);
assertEquals(3, loginConfiguration.getMaxLoginFailAttempts());
assertEquals(600, loginConfiguration.getAccessBlockTime());
assertEquals(30, loginConfiguration.getAccessBlockTime());
assertEquals(3600, loginConfiguration.getJwtTokenExpiryTime());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ void testLoginConfigurationSettings() throws HttpResponseException {

// Assert default values
assertEquals(3, loginConfig.getMaxLoginFailAttempts());
assertEquals(600, loginConfig.getAccessBlockTime());
assertEquals(30, loginConfig.getAccessBlockTime());
assertEquals(3600, loginConfig.getJwtTokenExpiryTime());

// Update login configuration
Expand Down

0 comments on commit b4c5ef8

Please sign in to comment.