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 the Base64-decoder failing if JWT_SECRET is not provided #1118

Merged
merged 1 commit into from
Sep 3, 2021
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 @@ -22,15 +22,16 @@
import io.cassandrareaper.storage.PostgresStorage;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.Base64;

import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.xml.bind.DatatypeConverter;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
Expand All @@ -55,6 +56,7 @@ String getJwt(HttpServletRequest request) throws IOException {
}

private static Key getSigningKey(AppContext cxt) {
byte[] key;
String txt = System.getenv("JWT_SECRET");
if (null == txt) {
if (cxt.storage instanceof CassandraStorage) {
Expand All @@ -64,7 +66,10 @@ private static Key getSigningKey(AppContext cxt) {
} else {
txt = AppContext.REAPER_INSTANCE_ADDRESS;
}
key = txt.getBytes(StandardCharsets.UTF_8);
} else {
key = Base64.getDecoder().decode(txt);
}
return new SecretKeySpec(DatatypeConverter.parseBase64Binary(txt), SIG_ALG.getJcaName());
return new SecretKeySpec(key, SIG_ALG.getJcaName());
}
}