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

SOLR-15111: Use JDK8 Base64 instead of own implementation #2252

Open
wants to merge 1 commit into
base: master
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
5 changes: 5 additions & 0 deletions lucene/core/src/java/org/apache/lucene/util/BytesRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.lucene.util;

import java.nio.ByteBuffer;
import java.util.Arrays;

/**
Expand Down Expand Up @@ -211,4 +212,8 @@ public boolean isValid() {
}
return true;
}

public ByteBuffer wrapToByteBuffer() {
return ByteBuffer.wrap(bytes, offset, length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

import java.io.IOException;
import java.lang.invoke.MethodHandles;
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 +68,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 @@ -298,7 +299,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(bytes.wrapToByteBuffer()).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 @@ -29,6 +29,7 @@
import java.net.URL;
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 @@ -75,7 +76,6 @@
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.Base64;
import org.apache.solr.common.util.ExecutorUtil;
import org.apache.solr.common.util.JavaBinCodec;
import org.apache.solr.common.util.NamedList;
Expand Down Expand Up @@ -948,7 +948,7 @@ private NamedList<Object> getDbData(RrdDb db, String[] dsNames, Format format, S
graph.render(bi.getGraphics());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "png", baos);
values.add(name, Base64.byteArrayToBase64(baos.toByteArray()));
values.add(name, Base64.getEncoder().encodeToString(baos.toByteArray()));
break;
case STRING:
str.setLength(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package org.apache.solr.handler.component;

import java.io.IOException;
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 @@ -39,7 +41,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.core.SolrCore;
import org.apache.solr.schema.IndexSchema;
Expand Down Expand Up @@ -366,7 +367,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(payload.wrapToByteBuffer()).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.position(), buf.limit()-buf.position());
return new String(Base64.getEncoder().encode(buf).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
7 changes: 4 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,9 @@

import java.io.IOException;
import java.lang.invoke.MethodHandles;
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 +69,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 +1328,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(val.wrapToByteBuffer()).array(), StandardCharsets.ISO_8859_1);
}

/**
Expand All @@ -1338,7 +1339,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,8 @@
import java.io.IOException;
import java.io.Reader;
import java.lang.invoke.MethodHandles;
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 +42,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 +101,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 +167,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 +217,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(binaryValue.wrapToByteBuffer()).array(), StandardCharsets.ISO_8859_1));
}
}
TokenStream ts = f.tokenStreamValue();
Expand Down Expand Up @@ -248,7 +249,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(p.wrapToByteBuffer()).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 @@ -183,7 +183,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 @@ -260,7 +260,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,6 +26,7 @@
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -47,7 +48,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 @@ -577,7 +577,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 @@ -42,7 +43,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 @@ -161,7 +161,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 @@ -304,7 +304,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
16 changes: 8 additions & 8 deletions solr/core/src/java/org/apache/solr/util/CryptoKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

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 @@ -76,7 +76,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 @@ -94,7 +94,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 @@ -258,7 +258,7 @@ public static String decodeAES(String base64CipherTxt, String pwd, final int key
final int CIPHERTEXT_OFFSET = SALT_OFFSET + SALT_SIZE;

try {
byte[] headerSaltAndCipherText = Base64.base64ToByteArray(base64CipherTxt);
byte[] headerSaltAndCipherText = Base64.getDecoder().decode(base64CipherTxt);

// --- extract salt & encrypted ---
// header is "Salted__", ASCII encoded, if salt is being used (the default)
Expand Down Expand Up @@ -307,7 +307,7 @@ public static String decodeAES(String base64CipherTxt, String pwd, final int key
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 @@ -349,7 +349,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 @@ -365,7 +365,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 @@ -374,7 +374,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