Skip to content

Commit

Permalink
[INLONG-10723][Manager] Update base64 encoder (apache#10724)
Browse files Browse the repository at this point in the history
  • Loading branch information
aloyszhang authored Jul 26, 2024
1 parent 2d48736 commit ab543f8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException.NotFound;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Base64.Encoder;
import java.util.List;
import java.util.Map;

Expand All @@ -54,6 +55,8 @@
@Component
public class ElasticsearchApi {

private static final Logger LOG = LoggerFactory.getLogger(ElasticsearchApi.class);

private static final Gson GSON = new GsonBuilder().create();

private static final String MAPPINGS_KEY = "mappings";
Expand All @@ -68,7 +71,7 @@ public class ElasticsearchApi {

private static final String CONTENT_TYPE_VALUE = "application/json;charset=UTF-8";

private static final Logger LOG = LoggerFactory.getLogger(ElasticsearchApi.class);
private final Encoder base64Encoder = Base64.getEncoder();

@Autowired
private ElasticsearchConfig esConfig;
Expand All @@ -84,7 +87,7 @@ private HttpHeaders getHttpHeaders() {
if (esConfig.getAuthEnable()) {
if (StringUtils.isNotEmpty(esConfig.getUsername()) && StringUtils.isNotEmpty(esConfig.getPassword())) {
String tokenStr = esConfig.getUsername() + ":" + esConfig.getPassword();
String token = String.valueOf(new BASE64Encoder().encode(tokenStr.getBytes(StandardCharsets.UTF_8)));
String token = base64Encoder.encodeToString(tokenStr.getBytes(StandardCharsets.UTF_8));
headers.add("Authorization", "Basic " + token);
}
}
Expand Down Expand Up @@ -214,6 +217,7 @@ public Map<String, ElasticsearchFieldInfo> getMappingMap(String indexName) throw
JsonObject fields = null;
Map<String, ElasticsearchFieldInfo> fieldInfos = Maps.newHashMap();
if (ObjectUtils.isNotEmpty(mappings)) {
String MAPPINGS_KEY = "mappings";
properties = mappings.getAsJsonObject(MAPPINGS_KEY);
}
if (ObjectUtils.isNotEmpty(properties)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.inlong.manager.service.resource.sink;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import sun.misc.BASE64Encoder;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Base64.Encoder;

public class Base64CompatibleTest {

@Test
public void base64CompatibleForDifferentVersionTest() {
String tokenStr = "root" + ":" + "admin";
byte[] tokeBytes = tokenStr.getBytes(StandardCharsets.UTF_8);
// token encode by sun.misc.BASE64Encoder, this test will fail if the jdk version is 9 or later
// so, this test should be removed when Apache InLong project support jdk 9 or later
String tokenOld = String.valueOf(new BASE64Encoder().encode(tokeBytes));
// token encode by java.util.Base64.Encoder
Encoder encoder = Base64.getEncoder();
String tokenNew = encoder.encodeToString(tokeBytes);
Assertions.assertEquals(tokenOld, tokenNew);
}
}

0 comments on commit ab543f8

Please sign in to comment.