Skip to content

Commit 18cd908

Browse files
authored
Fix windows encoding issues (opensearch-project#2206) (opensearch-project#2217)
Adds spotbugs [1] to detect internalization before they are added to the codebase, also fixed several encoding bugs that impact windows users. [1] https://spotbugs.readthedocs.io/en/stable/index.html Closes opensearch-project#2194 Signed-off-by: Peter Nied <petern@amazon.com> (cherry picked from commit a040b86)
1 parent 920fdf0 commit 18cd908

File tree

11 files changed

+43
-11
lines changed

11 files changed

+43
-11
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ jobs:
3333
-x spotlessCheck
3434
-x checkstyleMain
3535
-x checkstyleTest
36+
-x spotbugsMain
3637
3738
- name: Coverage
3839
uses: codecov/codecov-action@v1

.github/workflows/code-hygiene.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,18 @@ jobs:
4242
- uses: gradle/gradle-build-action@v2
4343
with:
4444
arguments: checkstyleMain checkstyleTest
45+
46+
spotbugs:
47+
runs-on: ubuntu-latest
48+
name: Spotbugs scan
49+
steps:
50+
- uses: actions/checkout@v2
51+
52+
- uses: actions/setup-java@v2
53+
with:
54+
distribution: temurin # Temurin is a distribution of adoptium
55+
java-version: 11
56+
57+
- uses: gradle/gradle-build-action@v2
58+
with:
59+
arguments: spotbugsMain

build.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ plugins {
5454
id 'checkstyle'
5555
id 'nebula.ospackage' version "8.3.0"
5656
id "org.gradle.test-retry" version "1.3.1"
57+
id "com.github.spotbugs" version "5.0.13"
5758
}
5859

5960
allprojects {
@@ -76,6 +77,14 @@ spotless {
7677
}
7778
}
7879

80+
spotbugs {
81+
includeFilter = file('spotbugs-include.xml')
82+
}
83+
84+
spotbugsTest {
85+
enabled = false
86+
}
87+
7988
java.sourceCompatibility = JavaVersion.VERSION_11
8089
java.targetCompatibility = JavaVersion.VERSION_11
8190

spotbugs-include.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<FindBugsFilter>
2+
<Match>
3+
<Bug category="I18N" />
4+
</Match>
5+
</FindBugsFilter>

src/main/java/com/amazon/dlic/auth/http/saml/AuthTokenProcessorHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.io.IOException;
1515
import java.net.URI;
1616
import java.net.URISyntaxException;
17+
import java.nio.charset.StandardCharsets;
1718
import java.security.AccessController;
1819
import java.security.PrivilegedActionException;
1920
import java.security.PrivilegedExceptionAction;
@@ -152,7 +153,7 @@ private AuthTokenProcessorAction.Response handleImpl(RestRequest restRequest, Re
152153
SettingsException {
153154
if (token_log.isDebugEnabled()) {
154155
try {
155-
token_log.debug("SAMLResponse for {}\n{}", samlRequestId, new String(Util.base64decoder(samlResponseBase64), "UTF-8"));
156+
token_log.debug("SAMLResponse for {}\n{}", samlRequestId, new String(Util.base64decoder(samlResponseBase64), StandardCharsets.UTF_8));
156157
} catch (Exception e) {
157158
token_log.warn(
158159
"SAMLResponse for {} cannot be decoded from base64\n{}",

src/main/java/org/opensearch/security/auditlog/impl/AbstractAuditLog.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ public void logDocumentWritten(ShardId shardId, GetResult originalResult, Index
419419
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, THROW_UNSUPPORTED_OPERATION, originalResult.internalSourceRef(), XContentType.JSON)) {
420420
Object base64 = parser.map().values().iterator().next();
421421
if(base64 instanceof String) {
422-
originalSource = (new String(BaseEncoding.base64().decode((String) base64)));
422+
originalSource = (new String(BaseEncoding.base64().decode((String) base64), StandardCharsets.UTF_8));
423423
} else {
424424
originalSource = XContentHelper.convertToJson(originalResult.internalSourceRef(), false, XContentType.JSON);
425425
}
@@ -430,7 +430,7 @@ public void logDocumentWritten(ShardId shardId, GetResult originalResult, Index
430430
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, THROW_UNSUPPORTED_OPERATION, currentIndex.source(), XContentType.JSON)) {
431431
Object base64 = parser.map().values().iterator().next();
432432
if(base64 instanceof String) {
433-
currentSource = (new String(BaseEncoding.base64().decode((String) base64)));
433+
currentSource = new String(BaseEncoding.base64().decode((String) base64), StandardCharsets.UTF_8);
434434
} else {
435435
currentSource = XContentHelper.convertToJson(currentIndex.source(), false, XContentType.JSON);
436436
}
@@ -457,7 +457,7 @@ public void logDocumentWritten(ShardId shardId, GetResult originalResult, Index
457457
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, THROW_UNSUPPORTED_OPERATION, currentIndex.source(), XContentType.JSON)) {
458458
Object base64 = parser.map().values().iterator().next();
459459
if(base64 instanceof String) {
460-
msg.addSecurityConfigContentToRequestBody(new String(BaseEncoding.base64().decode((String) base64)), id);
460+
msg.addSecurityConfigContentToRequestBody(new String(BaseEncoding.base64().decode((String) base64), StandardCharsets.UTF_8), id);
461461
} else {
462462
msg.addSecurityConfigTupleToRequestBody(new Tuple<XContentType, BytesReference>(XContentType.JSON, currentIndex.source()), id);
463463
}

src/main/java/org/opensearch/security/configuration/ConfigurationLoaderSecurity7.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
package org.opensearch.security.configuration;
2828

2929
import java.io.IOException;
30+
import java.nio.charset.StandardCharsets;
3031
import java.util.Arrays;
3132
import java.util.HashMap;
3233
import java.util.Map;
@@ -271,7 +272,7 @@ private SecurityDynamicConfiguration<?> toConfig(GetResponse singleGetResponse,
271272

272273
parser.nextToken();
273274

274-
final String jsonAsString = SecurityUtils.replaceEnvVars(new String(parser.binaryValue()), settings);
275+
final String jsonAsString = SecurityUtils.replaceEnvVars(new String(parser.binaryValue(), StandardCharsets.UTF_8), settings);
275276
final JsonNode jsonNode = DefaultObjectMapper.readTree(jsonAsString);
276277
int configVersion = 1;
277278

src/main/java/org/opensearch/security/ssl/DefaultSecurityKeyStore.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.ByteArrayInputStream;
2121
import java.io.File;
2222
import java.io.IOException;
23+
import java.nio.charset.StandardCharsets;
2324
import java.nio.file.Files;
2425
import java.nio.file.LinkOption;
2526
import java.nio.file.Path;
@@ -638,7 +639,7 @@ private boolean areSameCerts(final X509Certificate[] currentX509Certs, final X50
638639

639640
final Function<? super X509Certificate, String> certificateSignature = cert -> {
640641
final byte[] signature = cert !=null && cert.getSignature() != null ? cert.getSignature() : null;
641-
return new String(signature);
642+
return new String(signature, StandardCharsets.UTF_8);
642643
};
643644

644645
final Set<String> currentCertSignatureSet = Arrays.stream(currentX509Certs)

src/main/java/org/opensearch/security/support/ConfigHelper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.IOException;
3232
import java.io.Reader;
3333
import java.io.StringReader;
34+
import java.nio.charset.StandardCharsets;
3435

3536
import org.apache.logging.log4j.LogManager;
3637
import org.apache.logging.log4j.Logger;
@@ -91,7 +92,7 @@ public static void uploadFile(Client tc, String filepath, String index, CType cT
9192
public static Reader createFileOrStringReader(CType cType, int configVersion, String filepath, boolean populateEmptyIfFileMissing) throws Exception {
9293
Reader reader;
9394
if (!populateEmptyIfFileMissing || new File(filepath).exists()) {
94-
reader = new FileReader(filepath);
95+
reader = new FileReader(filepath, StandardCharsets.UTF_8);
9596
} else {
9697
reader = new StringReader(createEmptySdcYaml(cType, configVersion));
9798
}
@@ -143,7 +144,7 @@ public static <T> SecurityDynamicConfiguration<T> fromYamlReader(Reader yamlRead
143144
}
144145

145146
public static <T> SecurityDynamicConfiguration<T> fromYamlFile(String filepath, CType ctype, int version, long seqNo, long primaryTerm) throws IOException {
146-
return fromYamlReader(new FileReader(filepath), ctype, version, seqNo, primaryTerm);
147+
return fromYamlReader(new FileReader(filepath, StandardCharsets.UTF_8), ctype, version, seqNo, primaryTerm);
147148
}
148149

149150
public static <T> SecurityDynamicConfiguration<T> fromYamlString(String yamlString, CType ctype, int version, long seqNo, long primaryTerm) throws IOException {

src/main/java/org/opensearch/security/tools/SecurityAdmin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ private static boolean retrieveFile(final RestHighLevelClient restHighLevelClien
888888
}
889889

890890
System.out.println("Will retrieve '"+"/" +id+"' into "+filepath+" "+(legacy?"(legacy mode)":""));
891-
try (Writer writer = new FileWriter(filepath)) {
891+
try (Writer writer = new FileWriter(filepath, StandardCharsets.UTF_8)) {
892892

893893
final GetResponse response = restHighLevelClient.get(new GetRequest(index).id(id).refresh(true).realtime(false), RequestOptions.DEFAULT);
894894

0 commit comments

Comments
 (0)