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

fix(gms): Change MessageDigest to be thread safe #5405

Merged
merged 1 commit into from
Jul 15, 2022
Merged
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 @@ -16,13 +16,6 @@
import com.linkedin.metadata.utils.AuditStampUtils;
import com.linkedin.metadata.utils.GenericRecordUtils;
import com.linkedin.mxe.MetadataChangeProposal;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ArrayUtils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
Expand All @@ -31,11 +24,13 @@
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.ArrayUtils;

import static com.datahub.authentication.token.TokenClaims.ACTOR_ID_CLAIM_NAME;
import static com.datahub.authentication.token.TokenClaims.ACTOR_TYPE_CLAIM_NAME;
import static com.datahub.authentication.token.TokenClaims.TOKEN_TYPE_CLAIM_NAME;
import static com.datahub.authentication.token.TokenClaims.TOKEN_VERSION_CLAIM_NAME;
import static com.datahub.authentication.token.TokenClaims.*;


/**
Expand All @@ -48,7 +43,6 @@ public class StatefulTokenService extends StatelessTokenService {
private final EntityService _entityService;
private final LoadingCache<String, Boolean> _revokedTokenCache;
private final String salt;
private final MessageDigest sha256;

public StatefulTokenService(@Nonnull final String signingKey, @Nonnull final String signingAlgorithm,
@Nullable final String iss, @Nonnull final EntityService entityService, @Nonnull final String salt) {
Expand All @@ -65,11 +59,6 @@ public Boolean load(final String key) {
}
});
this.salt = salt;
try {
this.sha256 = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Unable to get SHA-256 algorithm.");
}
}

/**
Expand Down Expand Up @@ -179,22 +168,14 @@ public void revokeAccessToken(@Nonnull String hashedToken) throws TokenException
throw new TokenException("Access token no longer exists");
}

public boolean isTokenRevoked(@Nonnull String hashToken) {
try {
return _revokedTokenCache.get(hashToken);
} catch (ExecutionException e) {
return false;
}
}

/**
* Hashes the input after salting it.
*/
public String hash(String input) {
final byte[] saltingKeyBytes = this.salt.getBytes();
final byte[] inputBytes = input.getBytes();
final byte[] concatBytes = ArrayUtils.addAll(inputBytes, saltingKeyBytes);
final byte[] bytes = sha256.digest(concatBytes);
final byte[] bytes = DigestUtils.sha256(concatBytes);
return Base64.getEncoder().encodeToString(bytes);
}
}