Skip to content

Commit

Permalink
SOLR-15111 Use JDK8 Base64 instead of own implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
asalamon74 committed Aug 30, 2021
1 parent cfa7ec9 commit 524285b
Show file tree
Hide file tree
Showing 22 changed files with 73 additions and 214 deletions.
2 changes: 2 additions & 0 deletions solr/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ Other Changes

* SOLR-15324: Upgrade Jaeger dependency from 1.1.0 to 1.6.0 and thus also libthrift to 0.14.1 (janhoy)

* SOLR-15111: Use JDK8 Base64 instead of own implementation (Andras Salamon via janhoy)

================== 8.9.0 ==================

Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@

import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
Expand Down Expand Up @@ -66,7 +69,6 @@
import org.apache.solr.common.luke.FieldFlag;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.Base64;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.handler.RequestHandlerBase;
Expand Down Expand Up @@ -294,7 +296,7 @@ private static SimpleOrderedMap<Object> getDocumentFieldsInfo( Document doc, int

BytesRef bytes = field.binaryValue();
if (bytes != null) {
f.add( "binary", Base64.byteArrayToBase64(bytes.bytes, bytes.offset, bytes.length));
f.add( "binary", new String(Base64.getEncoder().encode(ByteBuffer.wrap(bytes.bytes, bytes.offset, bytes.length)).array(), StandardCharsets.ISO_8859_1));
}
if (!ftype.isPointField()) {
Term t = new Term(field.name(), ftype!=null ? ftype.storedToIndexed(field) : field.stringValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

import java.io.IOException;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -40,7 +43,6 @@
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.params.TermVectorParams;
import org.apache.solr.common.util.Base64;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.schema.SchemaField;
Expand Down Expand Up @@ -364,7 +366,7 @@ private void mapOneVector(NamedList<Object> docNL, FieldOptions fieldOptions, In
thePayloads = new NamedList<>();
termInfo.add("payloads", thePayloads);
}
thePayloads.add("payload", Base64.byteArrayToBase64(payload.bytes, payload.offset, payload.length));
thePayloads.add("payload", new String(Base64.getEncoder().encode(ByteBuffer.wrap(payload.bytes, payload.offset, payload.length)).array(), StandardCharsets.ISO_8859_1));
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions solr/core/src/java/org/apache/solr/schema/BinaryField.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

import org.apache.lucene.index.IndexableField;
import org.apache.lucene.search.SortField;
import org.apache.lucene.util.BytesRef;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.util.Base64;
import org.apache.solr.response.TextResponseWriter;
import org.apache.solr.uninverting.UninvertingReader.Type;
import org.slf4j.Logger;
Expand All @@ -44,7 +45,7 @@ public void checkSchemaField(SchemaField field) {
}

private String toBase64String(ByteBuffer buf) {
return Base64.byteArrayToBase64(buf.array(), buf.arrayOffset() + buf.position(), buf.limit()-buf.position());
return new String(Base64.getEncoder().encode(ByteBuffer.wrap(buf.array(), buf.arrayOffset() + buf.position(), buf.limit()-buf.position()).array()), StandardCharsets.ISO_8859_1);
}

@Override
Expand Down Expand Up @@ -98,7 +99,7 @@ public IndexableField createField(SchemaField field, Object val) {
} else {
String strVal = val.toString();
//the string has to be a base64 encoded string
buf = Base64.base64ToByteArray(strVal);
buf = Base64.getDecoder().decode(strVal);
offset = 0;
len = buf.length;
}
Expand All @@ -112,7 +113,7 @@ public Object toNativeType(Object val) {
return ByteBuffer.wrap((byte[]) val);
} else if (val instanceof CharSequence) {
final CharSequence valAsCharSequence = (CharSequence) val;
return ByteBuffer.wrap(Base64.base64ToByteArray(valAsCharSequence.toString()));
return ByteBuffer.wrap(Base64.getDecoder().decode(valAsCharSequence.toString()));
}
return super.toNativeType(val);
}
Expand Down
8 changes: 5 additions & 3 deletions solr/core/src/java/org/apache/solr/schema/FieldType.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -67,7 +70,6 @@
import org.apache.solr.common.IteratorWriter;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.common.util.Base64;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.common.util.StrUtils;
import org.apache.solr.query.SolrRangeQuery;
Expand Down Expand Up @@ -1327,7 +1329,7 @@ protected static Object marshalBase64SortValue(Object value) {
return null;
}
final BytesRef val = (BytesRef)value;
return Base64.byteArrayToBase64(val.bytes, val.offset, val.length);
return new String(Base64.getEncoder().encode(ByteBuffer.wrap(val.bytes, val.offset, val.length)).array(), StandardCharsets.ISO_8859_1);
}

/**
Expand All @@ -1338,7 +1340,7 @@ protected static Object unmarshalBase64SortValue(Object value) {
return null;
}
final String val = (String)value;
final byte[] bytes = Base64.base64ToByteArray(val);
final byte[] bytes = Base64.getDecoder().decode(val);
return new BytesRef(bytes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import java.io.IOException;
import java.io.Reader;
import java.lang.invoke.MethodHandles;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
Expand All @@ -40,7 +43,6 @@
import org.apache.lucene.util.AttributeSource;
import org.apache.lucene.util.AttributeSource.State;
import org.apache.lucene.util.BytesRef;
import org.apache.solr.common.util.Base64;
import org.apache.solr.schema.PreAnalyzedField.ParseResult;
import org.apache.solr.schema.PreAnalyzedField.PreAnalyzedParser;
import org.noggit.JSONUtil;
Expand Down Expand Up @@ -100,7 +102,7 @@ public ParseResult parse(Reader reader, AttributeSource parent)
res.str = (String)map.get(STRING_KEY);
String bin = (String)map.get(BINARY_KEY);
if (bin != null) {
byte[] data = Base64.base64ToByteArray(bin);
byte[] data = Base64.getDecoder().decode(bin);
res.bin = data;
}
List<Object> tokens = (List<Object>)map.get(TOKENS_KEY);
Expand Down Expand Up @@ -166,7 +168,7 @@ public ParseResult parse(Reader reader, AttributeSource parent)
} else if (key.equals(PAYLOAD_KEY)) {
String str = String.valueOf(e.getValue());
if (str.length() > 0) {
byte[] data = Base64.base64ToByteArray(str);
byte[] data = Base64.getDecoder().decode(str);
PayloadAttribute p = parent.addAttribute(PayloadAttribute.class);
if (data != null && data.length > 0) {
p.setPayload(new BytesRef(data));
Expand Down Expand Up @@ -216,7 +218,7 @@ public String toFormattedString(Field f) throws IOException {
}
BytesRef binaryValue = f.binaryValue();
if (binaryValue != null) {
map.put(BINARY_KEY, Base64.byteArrayToBase64(binaryValue.bytes, binaryValue.offset, binaryValue.length));
map.put(BINARY_KEY, new String(Base64.getEncoder().encode(ByteBuffer.wrap(binaryValue.bytes, binaryValue.offset, binaryValue.length)).array(), StandardCharsets.ISO_8859_1));
}
}
TokenStream ts = f.tokenStreamValue();
Expand Down Expand Up @@ -248,7 +250,7 @@ public String toFormattedString(Field f) throws IOException {
} else if (cl.isAssignableFrom(PayloadAttribute.class)) {
BytesRef p = ((PayloadAttribute)att).getPayload();
if (p != null && p.length > 0) {
tok.put(PAYLOAD_KEY, Base64.byteArrayToBase64(p.bytes, p.offset, p.length));
tok.put(PAYLOAD_KEY, new String(Base64.getEncoder().encode(ByteBuffer.wrap(p.bytes, p.offset, p.length)).array(), StandardCharsets.ISO_8859_1));
}
} else if (cl.isAssignableFrom(PositionIncrementAttribute.class)) {
tok.put(POSINCR_KEY, ((PositionIncrementAttribute)att).getPositionIncrement());
Expand Down
6 changes: 3 additions & 3 deletions solr/core/src/java/org/apache/solr/search/CursorMark.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@

import static org.apache.solr.common.params.CursorMarkParams.*;

import org.apache.solr.common.util.Base64;
import org.apache.solr.common.util.JavaBinCodec;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.schema.FieldType;
import org.apache.solr.schema.SchemaField;

import java.util.Base64;
import java.util.List;
import java.util.ArrayList;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -188,7 +188,7 @@ public void parseSerializedTotem(final String serialized) {

List<Object> pieces = null;
try {
final byte[] rawData = Base64.base64ToByteArray(serialized);
final byte[] rawData = Base64.getDecoder().decode(serialized);
try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayInputStream in = new ByteArrayInputStream(rawData)){
pieces = (List<Object>) jbc.unmarshal(in);
boolean b = false;
Expand Down Expand Up @@ -265,7 +265,7 @@ public String getSerializedTotem() {
try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayOutputStream out = new ByteArrayOutputStream(256)) {
jbc.marshal(marshalledValues, out);
byte[] rawData = out.toByteArray();
return Base64.byteArrayToBase64(rawData, 0, rawData.length);
return Base64.getEncoder().encodeToString(rawData);
} catch (Exception ex) {
throw new SolrException(ErrorCode.SERVER_ERROR,
"Unable to format search after totem", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SpecProvider;
import org.apache.solr.common.StringUtils;
import org.apache.solr.common.util.Base64;
import org.apache.solr.common.util.CommandOperation;
import org.apache.solr.common.util.Utils;
import org.apache.solr.common.util.ValidatingJsonMap;
Expand Down Expand Up @@ -61,6 +60,7 @@
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -625,7 +625,7 @@ protected String generateAuthDataHeader() {
data.put("scope", adminUiScope);
data.put("redirect_uris", redirectUris);
String headerJson = Utils.toJSONString(data);
return Base64.byteArrayToBase64(headerJson.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(headerJson.getBytes(StandardCharsets.UTF_8));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.nio.ByteBuffer;
import java.security.Principal;
import java.security.PublicKey;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -43,7 +44,6 @@
import org.apache.solr.client.solrj.impl.HttpClientUtil;
import org.apache.solr.client.solrj.impl.HttpListenerFactory;
import org.apache.solr.client.solrj.impl.SolrHttpClientBuilder;
import org.apache.solr.common.util.Base64;
import org.apache.solr.common.util.ExecutorUtil;
import org.apache.solr.common.util.StrUtils;
import org.apache.solr.common.util.SuppressForbidden;
Expand Down Expand Up @@ -180,7 +180,7 @@ private PKIHeaderData decipherHeader(String nodeName, String cipherBase64) {
private static PKIHeaderData parseCipher(String cipher, PublicKey key) {
byte[] bytes;
try {
bytes = CryptoKeys.decryptRSA(Base64.base64ToByteArray(cipher), key);
bytes = CryptoKeys.decryptRSA(Base64.getDecoder().decode(cipher), key);
} catch (Exception e) {
log.error("Decryption failed , key must be wrong", e);
return null;
Expand Down Expand Up @@ -322,7 +322,7 @@ private Optional<String> generateToken() {

byte[] payload = s.getBytes(UTF_8);
byte[] payloadCipher = publicKeyHandler.keyPair.encrypt(ByteBuffer.wrap(payload));
String base64Cipher = Base64.byteArrayToBase64(payloadCipher);
String base64Cipher = Base64.getEncoder().encodeToString(payloadCipher);
log.trace("generateToken: usr={} token={}", usr, base64Cipher);
return Optional.of(base64Cipher);
}
Expand Down
14 changes: 7 additions & 7 deletions solr/core/src/java/org/apache/solr/util/CryptoKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.google.common.collect.ImmutableMap;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.util.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -46,6 +45,7 @@
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -77,7 +77,7 @@ public String verify(String sig, ByteBuffer data) {
for (Map.Entry<String, PublicKey> entry : keys.entrySet()) {
boolean verified;
try {
verified = CryptoKeys.verify(entry.getValue(), Base64.base64ToByteArray(sig), data);
verified = CryptoKeys.verify(entry.getValue(), Base64.getDecoder().decode(sig), data);
log.debug("verified {} ", verified);
if (verified) return entry.getKey();
} catch (Exception e) {
Expand All @@ -95,7 +95,7 @@ public String verify(String sig, InputStream is) {
for (Map.Entry<String, PublicKey> entry : keys.entrySet()) {
boolean verified;
try {
verified = CryptoKeys.verify(entry.getValue(), Base64.base64ToByteArray(sig), is);
verified = CryptoKeys.verify(entry.getValue(), Base64.getDecoder().decode(sig), is);
log.debug("verified {} ", verified);
if (verified) return entry.getKey();
} catch (Exception e) {
Expand Down Expand Up @@ -172,7 +172,7 @@ public static boolean verify(PublicKey publicKey, byte[] sig, InputStream is)
public static PublicKey deserializeX509PublicKey(String pubKey) {
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64.base64ToByteArray(pubKey));
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(pubKey));
return keyFactory.generatePublic(publicKeySpec);
} catch (Exception e) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,e);
Expand Down Expand Up @@ -247,7 +247,7 @@ public RSAKeyPair() {
java.security.KeyPair keyPair = keyGen.genKeyPair();
privateKey = keyPair.getPrivate();
publicKey = keyPair.getPublic();
pubKeyStr = Base64.byteArrayToBase64(publicKey.getEncoded());
pubKeyStr = Base64.getEncoder().encodeToString(publicKey.getEncoded());
}

/**
Expand All @@ -263,7 +263,7 @@ public RSAKeyPair(URL privateKeyResourceName, URL publicKeyResourceName) throws
String privateString = new String(inPrivate.readAllBytes(), StandardCharsets.UTF_8)
.replaceAll("-----(BEGIN|END) PRIVATE KEY-----", "");

PKCS8EncodedKeySpec privateSpec = new PKCS8EncodedKeySpec(java.util.Base64.getMimeDecoder().decode(privateString));
PKCS8EncodedKeySpec privateSpec = new PKCS8EncodedKeySpec(Base64.getMimeDecoder().decode(privateString));
KeyFactory rsaFactory = KeyFactory.getInstance("RSA");
privateKey = rsaFactory.generatePrivate(privateSpec);
} catch (NoSuchAlgorithmException e) {
Expand All @@ -272,7 +272,7 @@ public RSAKeyPair(URL privateKeyResourceName, URL publicKeyResourceName) throws

try (InputStream inPublic = publicKeyResourceName.openStream()) {
publicKey = getX509PublicKey(inPublic.readAllBytes());
pubKeyStr = Base64.byteArrayToBase64(publicKey.getEncoded());
pubKeyStr = Base64.getEncoder().encodeToString(publicKey.getEncoded());
}
}

Expand Down
Loading

0 comments on commit 524285b

Please sign in to comment.