Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve logging in userstore managers #3245

Open
wants to merge 5 commits into
base: 4.7.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ public Object run() throws Exception {
boolean isClientException = e.getCause().getCause() instanceof UserStoreClientException;
String errorCode = ((UserStoreException) e.getCause().getCause()).getErrorCode();
String errorMessage = e.getCause().getCause().getMessage();
if (log.isDebugEnabled()) {
log.debug(errorMessage, e);
}
if (isClientException) {
if (StringUtils.isBlank(errorCode)) {
throw new UserStoreClientException(errorCode, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class JDBCUserStoreConstants {
private static final String VALIDATION_INTERVAL = "validationInterval";
private static final String DISPLAY_NAME_ATTRIBUTE_DESCRIPTION = "This is the attribute name to display as the Display Name";
public static final String DISPLAY_NAME_ATTRIBUTE = "DisplayNameAttribute";
public static final String SET_QUERY_TIMEOUT_ERROR_MSG = "Error while setting the query timeout value.";
public static final String UNSUPPORTED_CREDENTIAL_TYPE_ERROR_MSG = "Unsupported credential type.";

static {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
public class JDBCUserStoreManager extends AbstractUserStoreManager {

// private boolean useOnlyInternalRoles;
private static Log log = LogFactory.getLog(JDBCUserStoreManager.class);
private static final Log log = LogFactory.getLog(JDBCUserStoreManager.class);

private static final String QUERY_FILTER_STRING_ANY = "*";
private static final String SQL_FILTER_STRING_ANY = "%";
Expand Down Expand Up @@ -495,7 +495,7 @@ public String[] doListUsers(String filter, int maxItemLimit) throws UserStoreExc
prepStmt.setQueryTimeout(searchTime);
} catch (Exception e) {
// this can be ignored since timeout method is not implemented
log.debug(e);
log.debug(JDBCUserStoreConstants.SET_QUERY_TIMEOUT_ERROR_MSG, e);
}

try {
Expand Down Expand Up @@ -697,7 +697,11 @@ public String[] doGetRoleNames(String filter, int maxItemLimit) throws UserStore
}
throw new UserStoreException(msg, e);
} catch (Exception e) {
throw new UserStoreException("Error while retrieving the DB type. ", e);
String msg = "Error while retrieving the DB type.";
if (log.isDebugEnabled()) {
log.debug(msg, e);
}
throw new UserStoreException(msg, e);
} finally {
DatabaseUtil.closeAllConnections(dbConnection, rs, prepStmt);
}
Expand Down Expand Up @@ -734,7 +738,7 @@ private void setPSRestrictions(PreparedStatement ps, int maxItemLimit) throws SQ
ps.setQueryTimeout(searchTime);
} catch (Exception e) {
// this can be ignored since timeout method is not implemented
log.debug(e);
log.debug(JDBCUserStoreConstants.SET_QUERY_TIMEOUT_ERROR_MSG, e);
}
}

Expand Down Expand Up @@ -824,7 +828,11 @@ protected String[] doGetSharedRoleNames(String tenantDomain, String filter, int
}
throw new UserStoreException(errorMessage, e);
} catch (Exception e) {
throw new UserStoreException("Error while retrieving the DB type for tenant domain: " + tenantDomain, e);
String msg = "Error while retrieving the DB type for tenant domain: " + tenantDomain + ".";
if (log.isDebugEnabled()) {
log.debug(msg, e);
}
throw new UserStoreException(msg, e);
} finally {
DatabaseUtil.closeAllConnections(dbConnection, rs, prepStmt);
}
Expand Down Expand Up @@ -1247,6 +1255,9 @@ private String[] getStringValuesFromDatabaseWithConstraints(String sqlStmt, int
.getStringValuesFromDatabaseWithConstraints(dbConnection, sqlStmt, maxRows, queryTimeout, params);
} catch (SQLException e) {
String msg = "Error occurred while accessing the database connection.";
if (log.isDebugEnabled()) {
log.debug(msg, e);
}
throw new UserStoreException(msg, e);
}
return values;
Expand Down Expand Up @@ -1528,7 +1539,10 @@ protected void persistUser(String userName, Object credential, String[] roleList
try {
credentialObj = Secret.getSecret(credential);
} catch (UnsupportedSecretTypeException e) {
throw new UserStoreException("Unsupported credential type", e);
if (log.isDebugEnabled()) {
log.debug(JDBCUserStoreConstants.UNSUPPORTED_CREDENTIAL_TYPE_ERROR_MSG, e);
}
throw new UserStoreException(JDBCUserStoreConstants.UNSUPPORTED_CREDENTIAL_TYPE_ERROR_MSG, e);
}

try {
Expand Down Expand Up @@ -1791,7 +1805,7 @@ private int getTenantIdFromRole(String roleBase) {
try {
tenantId = Integer.parseInt(postfix[1]);
} catch (NumberFormatException e) {
log.error(e);
log.error("An error occurred while parsing the tenant id to an integer.", e);
tenantId = MultitenantConstants.SUPER_TENANT_ID;
}
}
Expand Down Expand Up @@ -2648,7 +2662,11 @@ private String generateSaltValue() {
secureRandom.nextBytes(bytes);
saltValue = Base64.encode(bytes);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("SHA1PRNG algorithm could not be found.");
String msg = "SHA1PRNG algorithm could not be found.";
if (log.isDebugEnabled()) {
log.debug(msg, e);
}
throw new RuntimeException(msg, e);
}
return saltValue;
}
Expand Down Expand Up @@ -2920,7 +2938,10 @@ protected String preparePassword(Object password, String saltValue) throws UserS
try {
credentialObj = Secret.getSecret(password);
} catch (UnsupportedSecretTypeException e) {
throw new UserStoreException("Unsupported credential type", e);
if (log.isDebugEnabled()) {
log.debug(JDBCUserStoreConstants.UNSUPPORTED_CREDENTIAL_TYPE_ERROR_MSG, e);
}
throw new UserStoreException(JDBCUserStoreConstants.UNSUPPORTED_CREDENTIAL_TYPE_ERROR_MSG, e);
}
String digestFunction = realmConfig.getUserStoreProperties().get(JDBCRealmConstants.DIGEST_FUNCTION);
if (digestFunction != null) {
Expand Down Expand Up @@ -3088,7 +3109,7 @@ public boolean isValidRememberMeToken(String userName, String token)
return isExistingRememberMeToken(userName, token);
}
} catch (Exception e) {
log.error("Validating remember me token failed for" + userName);
log.error("Validating remember me token failed for" + userName + ".", e);
// not throwing exception.
// because we need to seamlessly direct them to login uis
}
Expand Down Expand Up @@ -3171,7 +3192,7 @@ public String[] getUserListFromProperties(String property, String value, String
prepStmt.setQueryTimeout(searchTime);
} catch (Exception e) {
// this can be ignored since timeout method is not implemented
log.debug(e);
log.debug(JDBCUserStoreConstants.SET_QUERY_TIMEOUT_ERROR_MSG, e);
}
rs = prepStmt.executeQuery();
while (rs.next()) {
Expand Down Expand Up @@ -3328,6 +3349,7 @@ protected Map<String, Map<String, String>> getUsersPropertyValues(List<String> u
String errorMessage = "Error Occurred while getting property values";
if (log.isDebugEnabled()) {
errorMessage = errorMessage + ": " + users;
log.debug(errorMessage, e);
}
throw new UserStoreException(errorMessage, e);
} finally {
Expand Down Expand Up @@ -3406,6 +3428,7 @@ protected Map<String, List<String>> doGetExternalRoleListOfUsers(List<String> us
String errorMessage = "Error Occurred while getting role lists of users";
if (log.isDebugEnabled()) {
errorMessage = errorMessage + ": " + userNames;
log.debug(errorMessage, e);
}
throw new UserStoreException(errorMessage, e);
} finally {
Expand Down Expand Up @@ -3848,7 +3871,7 @@ protected PaginatedSearchResult doListUsers(String filter, int limit, int offset
prepStmt.setQueryTimeout(searchTime);
} catch (Exception e) {
// this can be ignored since timeout method is not implemented
log.debug(e);
log.debug(JDBCUserStoreConstants.SET_QUERY_TIMEOUT_ERROR_MSG, e);
}

try {
Expand Down Expand Up @@ -3964,19 +3987,27 @@ public long doCountUsersWithClaims(String claimUri, String value) throws UserSto
} catch (SQLException e) {
String msg = "Error while executing the SQL " + sqlStmt;
if (log.isDebugEnabled()) {
log.debug(msg + sqlStmt);
log.debug(msg + sqlStmt, e);
}
throw new UserStoreException(msg, e);
} catch (UserStoreException ex) {
String msg = UserCoreErrorConstants.ErrorMessages.ERROR_CODE_ERROR_WHILE_GETTING_COUNT_USERS
.getMessage();
if (log.isDebugEnabled()) {
log.debug(msg, ex);
}
handleGetUserCountFailure(UserCoreErrorConstants.ErrorMessages.ERROR_CODE_ERROR_WHILE_GETTING_COUNT_USERS
.getCode(),
String.format(UserCoreErrorConstants.ErrorMessages.ERROR_CODE_ERROR_WHILE_GETTING_COUNT_USERS
.getMessage(),
ex.getMessage()), claimUri, value);
throw ex;
} catch (org.wso2.carbon.user.api.UserStoreException e) {
String ErrorMsg = "Error while getting attribute name from " + claimUri ;
throw new UserStoreException(ErrorMsg, e);
String msg = "Error while getting attribute name from " + claimUri + ".";
if (log.isDebugEnabled()) {
log.debug(msg, e);
}
throw new UserStoreException(msg, e);
}
}

Expand Down Expand Up @@ -4037,6 +4068,11 @@ public long doCountRoles(String filter) throws UserStoreException {
throw new UserStoreException(msg, e);

} catch (UserStoreException ex) {
String msg = UserCoreErrorConstants.ErrorMessages.ERROR_CODE_ERROR_WHILE_GETTING_ROLES_COUNT
.getMessage();
if (log.isDebugEnabled()) {
log.debug(msg, ex);
}
handleGetUserCountFailure(UserCoreErrorConstants.ErrorMessages.ERROR_CODE_ERROR_WHILE_GETTING_ROLES_COUNT
.getCode(),
String.format(UserCoreErrorConstants.ErrorMessages.ERROR_CODE_ERROR_WHILE_GETTING_ROLES_COUNT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@

public class UniqueIDJDBCUserStoreManager extends JDBCUserStoreManager {

private static Log log = LogFactory.getLog(UniqueIDJDBCUserStoreManager.class);
private static final Log log = LogFactory.getLog(UniqueIDJDBCUserStoreManager.class);

private static final String QUERY_FILTER_STRING_ANY = "*";
private static final String SQL_FILTER_STRING_ANY = "%";
Expand Down Expand Up @@ -208,7 +208,7 @@ public List<User> doListUsersWithID(String filter, int maxItemLimit) throws User
} catch (Exception e) {
// this can be ignored since timeout method is not implemented
if (log.isDebugEnabled()) {
log.debug(e);
log.debug(JDBCUserStoreConstants.SET_QUERY_TIMEOUT_ERROR_MSG, e);
}
}

Expand Down Expand Up @@ -541,6 +541,9 @@ private List<User> getUsersFromDatabaseWithConstraints(UserStoreManager userStor
params);
} catch (SQLException e) {
String msg = "Error occurred while accessing the database connection.";
if (log.isDebugEnabled()) {
log.debug(msg, e);
}
throw new UserStoreException(msg, e);
}
return values;
Expand Down Expand Up @@ -725,6 +728,10 @@ protected AuthenticationResult doAuthenticateWithID(List<LoginIdentifier> loginI
user.setTenantDomain(getTenantDomain(tenantId));
user.setUserStoreDomain(UserCoreUtil.getDomainName(realmConfig));
} catch (org.wso2.carbon.user.api.UserStoreException e) {
if (log.isDebugEnabled()) {
log.debug("An error occurred while setting either the user's tenant or users store" +
" domain.", e);
}
throw new UserStoreException(e);
}
authenticationResult = new AuthenticationResult(
Expand Down Expand Up @@ -1137,7 +1144,10 @@ protected void persistUser(String userID, String userName, Object credential, St
try {
credentialObj = Secret.getSecret(credential);
} catch (UnsupportedSecretTypeException e) {
throw new UserStoreException("Unsupported credential type.", e);
if (log.isDebugEnabled()) {
log.debug(JDBCUserStoreConstants.UNSUPPORTED_CREDENTIAL_TYPE_ERROR_MSG, e);
}
throw new UserStoreException(JDBCUserStoreConstants.UNSUPPORTED_CREDENTIAL_TYPE_ERROR_MSG, e);
}

try {
Expand Down Expand Up @@ -1333,6 +1343,9 @@ public String getUserIDFromProperties(String claimURI, String claimValue, String
return userIds.get(0);
}
} catch (org.wso2.carbon.user.api.UserStoreException e) {
if (log.isDebugEnabled()) {
log.debug("An error occurred while extracting the user id from properties.", e);
}
throw new UserStoreException(
"Error occurred while retrieving the userId of domain : " + getMyDomainName() + " and " + "claim"
+ claimURI + " value: " + claimValue, e);
Expand Down Expand Up @@ -2122,7 +2135,7 @@ private String generateSaltValue() {
secureRandom.nextBytes(bytes);
saltValue = Base64.encode(bytes);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("SHA1PRNG algorithm could not be found.");
throw new RuntimeException("SHA1PRNG algorithm could not be found.", e);
}
return saltValue;
}
Expand Down Expand Up @@ -2381,7 +2394,7 @@ public List<String> doGetUserListFromPropertiesWithID(String property, String va
} catch (SQLException e) {
// If SQL exception occurred here, we can ignore cause timeout method is not implemented.
if (log.isDebugEnabled()) {
log.debug(e);
log.debug(JDBCUserStoreConstants.SET_QUERY_TIMEOUT_ERROR_MSG, e);
}
}
rs = prepStmt.executeQuery();
Expand Down Expand Up @@ -2985,7 +2998,7 @@ protected UniqueIDPaginatedSearchResult doListUsersWithID(String filter, int lim
prepStmt.setQueryTimeout(searchTime);
} catch (Exception e) {
// this can be ignored since timeout method is not implemented
log.debug(e);
log.debug(JDBCUserStoreConstants.SET_QUERY_TIMEOUT_ERROR_MSG, e);
}

try {
Expand Down
Loading