Skip to content

Commit 4e1a34f

Browse files
author
Pankaj Agrawal
committed
static analysis spot bug in build and fixes
1 parent aad936f commit 4e1a34f

File tree

10 files changed

+112
-43
lines changed

10 files changed

+112
-43
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
distribution: 'zulu'
4949
java-version: ${{ matrix.java }}
5050
- name: Build with Maven
51-
run: mvn -B package --file pom.xml
51+
run: mvn -Pbuild-without-spotbugs -B package --file pom.xml
5252
savepr:
5353
runs-on: ubuntu-latest
5454
name: Save PR number if running on PR by dependabot

pom.xml

+53-1
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,62 @@
399399
</build>
400400
</profile>
401401
<profile>
402-
<id>build-extras</id>
402+
<id>build-with-spotbugs</id>
403403
<activation>
404404
<activeByDefault>true</activeByDefault>
405405
</activation>
406+
<build>
407+
<plugins>
408+
<plugin>
409+
<groupId>com.github.spotbugs</groupId>
410+
<artifactId>spotbugs-maven-plugin</artifactId>
411+
<version>4.2.2</version>
412+
<executions>
413+
<execution>
414+
<id>test</id>
415+
<goals>
416+
<goal>check</goal>
417+
</goals>
418+
</execution>
419+
</executions>
420+
<configuration>
421+
<xmlOutput>true</xmlOutput>
422+
<failOnError>true</failOnError>
423+
</configuration>
424+
</plugin>
425+
<plugin>
426+
<groupId>org.apache.maven.plugins</groupId>
427+
<artifactId>maven-source-plugin</artifactId>
428+
<executions>
429+
<execution>
430+
<id>attach-sources</id>
431+
<goals>
432+
<goal>jar-no-fork</goal>
433+
</goals>
434+
</execution>
435+
</executions>
436+
</plugin>
437+
<plugin>
438+
<groupId>org.apache.maven.plugins</groupId>
439+
<artifactId>maven-javadoc-plugin</artifactId>
440+
<configuration>
441+
<doclint>none</doclint>
442+
<detectJavaApiLink>false</detectJavaApiLink>
443+
</configuration>
444+
<executions>
445+
<execution>
446+
<id>attach-javadocs</id>
447+
<goals>
448+
<goal>jar</goal>
449+
</goals>
450+
</execution>
451+
</executions>
452+
</plugin>
453+
</plugins>
454+
</build>
455+
</profile>
456+
<profile>
457+
<id>build-without-spotbugs</id>
406458
<build>
407459
<plugins>
408460
<plugin>

powertools-logging/src/main/java/software/amazon/lambda/powertools/logging/internal/LambdaLoggingAspect.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ private Object[] logFromInputStream(final ProceedingJoinPoint pjp) {
236236

237237
private byte[] bytesFromInputStreamSafely(final InputStream inputStream) throws IOException {
238238
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
239-
InputStreamReader reader = new InputStreamReader(inputStream)) {
239+
InputStreamReader reader = new InputStreamReader(inputStream, UTF_8)) {
240240
OutputStreamWriter writer = new OutputStreamWriter(out, UTF_8);
241241

242242
IOUtils.copy(reader, writer);

powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/MetricsUtils.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package software.amazon.lambda.powertools.metrics;
22

3+
import java.util.Arrays;
34
import java.util.Optional;
45
import java.util.function.Consumer;
56

@@ -121,7 +122,7 @@ public static void withSingleMetric(final String name,
121122
}
122123

123124
public static DimensionSet[] getDefaultDimensions() {
124-
return defaultDimensions;
125+
return Arrays.copyOf(defaultDimensions, defaultDimensions.length);
125126
}
126127

127128
public static boolean hasDefaultDimension() {

powertools-parameters/src/main/java/software/amazon/lambda/powertools/parameters/SecretsProvider.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import software.amazon.lambda.powertools.parameters.transform.TransformationManager;
2828
import software.amazon.lambda.powertools.parameters.transform.Transformer;
2929

30+
import static java.nio.charset.StandardCharsets.UTF_8;
31+
3032
/**
3133
* AWS Secrets Manager Parameter Provider<br/><br/>
3234
*
@@ -94,7 +96,7 @@ protected String getValue(String key) {
9496

9597
String secretValue = client.getSecretValue(request).secretString();
9698
if (secretValue == null) {
97-
secretValue = new String(Base64.getDecoder().decode(client.getSecretValue(request).secretBinary().asByteArray()));
99+
secretValue = new String(Base64.getDecoder().decode(client.getSecretValue(request).secretBinary().asByteArray()), UTF_8);
98100
}
99101
return secretValue;
100102
}

powertools-parameters/src/main/java/software/amazon/lambda/powertools/parameters/transform/Base64Transformer.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
*/
1414
package software.amazon.lambda.powertools.parameters.transform;
1515

16+
import java.util.Base64;
17+
1618
import software.amazon.lambda.powertools.parameters.exception.TransformationException;
1719

18-
import java.util.Base64;
20+
import static java.nio.charset.StandardCharsets.UTF_8;
1921

2022
/**
2123
* Transformer that take a base64 encoded string and return a decoded string.
@@ -25,7 +27,7 @@ public class Base64Transformer extends BasicTransformer {
2527
@Override
2628
public String applyTransformation(String value) throws TransformationException {
2729
try {
28-
return new String(Base64.getDecoder().decode(value));
30+
return new String(Base64.getDecoder().decode(value), UTF_8);
2931
} catch (Exception e) {
3032
throw new TransformationException(e);
3133
}

powertools-validation/src/main/java/software/amazon/lambda/powertools/validation/ValidationConfig.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,13 @@ public SpecVersion.VersionFlag getSchemaVersion() {
8585
* @param <T> Must extends {@link BaseFunction}
8686
*/
8787
public <T extends BaseFunction> void addFunction(T function) {
88-
configuration.functionRegistry().extend(function);
89-
jmesPath = new JacksonRuntime(configuration, getObjectMapper());
88+
FunctionRegistry functionRegistryWithExtendedFunctions = configuration.functionRegistry().extend(function);
89+
90+
RuntimeConfiguration updatedConfig = new RuntimeConfiguration.Builder()
91+
.withFunctionRegistry(functionRegistryWithExtendedFunctions)
92+
.build();
93+
94+
jmesPath = new JacksonRuntime(updatedConfig, getObjectMapper());
9095
}
9196

9297
/**

powertools-validation/src/main/java/software/amazon/lambda/powertools/validation/ValidationUtils.java

+29-22
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
*/
1414
package software.amazon.lambda.powertools.validation;
1515

16+
import java.io.IOException;
17+
import java.io.InputStream;
18+
import java.util.Map;
19+
import java.util.Set;
20+
import java.util.concurrent.ConcurrentHashMap;
21+
import java.util.stream.Collectors;
22+
1623
import com.fasterxml.jackson.core.JsonProcessingException;
1724
import com.fasterxml.jackson.databind.JsonNode;
1825
import com.fasterxml.jackson.databind.node.JsonNodeType;
@@ -21,12 +28,6 @@
2128
import io.burt.jmespath.Expression;
2229
import software.amazon.lambda.powertools.validation.internal.ValidationAspect;
2330

24-
import java.io.InputStream;
25-
import java.util.Map;
26-
import java.util.Set;
27-
import java.util.concurrent.ConcurrentHashMap;
28-
import java.util.stream.Collectors;
29-
3031
/**
3132
* Validation utility, used to manually validate Json against Json Schema
3233
*/
@@ -229,31 +230,37 @@ public static JsonSchema getJsonSchema(String schema) {
229230
public static JsonSchema getJsonSchema(String schema, boolean validateSchema) {
230231
JsonSchema jsonSchema = schemas.get(schema);
231232

232-
if (jsonSchema == null) {
233-
if (schema.startsWith(CLASSPATH)) {
234-
String filePath = schema.substring(CLASSPATH.length());
235-
InputStream schemaStream = ValidationAspect.class.getResourceAsStream(filePath);
233+
if (jsonSchema != null) {
234+
return jsonSchema;
235+
}
236+
237+
if (schema.startsWith(CLASSPATH)) {
238+
String filePath = schema.substring(CLASSPATH.length());
239+
try (InputStream schemaStream = ValidationAspect.class.getResourceAsStream(filePath)) {
236240
if (schemaStream == null) {
237241
throw new IllegalArgumentException("'" + schema + "' is invalid, verify '" + filePath + "' is in your classpath");
238242
}
243+
239244
jsonSchema = ValidationConfig.get().getFactory().getSchema(schemaStream);
240-
} else {
241-
jsonSchema = ValidationConfig.get().getFactory().getSchema(schema);
245+
} catch (IOException e) {
246+
throw new IllegalArgumentException("'" + schema + "' is invalid, verify '" + filePath + "' is in your classpath");
242247
}
248+
} else {
249+
jsonSchema = ValidationConfig.get().getFactory().getSchema(schema);
250+
}
243251

244-
if (validateSchema) {
245-
String version = ValidationConfig.get().getSchemaVersion().toString();
246-
try {
247-
validate(jsonSchema.getSchemaNode(),
248-
getJsonSchema("classpath:/schemas/meta_schema_" + version));
249-
} catch (ValidationException ve) {
250-
throw new IllegalArgumentException("The schema " + schema + " is not valid, it does not respect the specification " + version, ve);
251-
}
252+
if (validateSchema) {
253+
String version = ValidationConfig.get().getSchemaVersion().toString();
254+
try {
255+
validate(jsonSchema.getSchemaNode(),
256+
getJsonSchema("classpath:/schemas/meta_schema_" + version));
257+
} catch (ValidationException ve) {
258+
throw new IllegalArgumentException("The schema " + schema + " is not valid, it does not respect the specification " + version, ve);
252259
}
253-
254-
schemas.put(schema, jsonSchema);
255260
}
256261

262+
schemas.put(schema, jsonSchema);
263+
257264
return jsonSchema;
258265
}
259266

powertools-validation/src/main/java/software/amazon/lambda/powertools/validation/jmespath/Base64Function.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
*/
1414
package software.amazon.lambda.powertools.validation.jmespath;
1515

16+
import java.nio.ByteBuffer;
17+
import java.util.Base64;
18+
import java.util.List;
19+
1620
import io.burt.jmespath.Adapter;
1721
import io.burt.jmespath.JmesPathType;
1822
import io.burt.jmespath.function.ArgumentConstraints;
1923
import io.burt.jmespath.function.BaseFunction;
2024
import io.burt.jmespath.function.FunctionArgument;
2125

22-
import java.nio.ByteBuffer;
23-
import java.util.Base64;
24-
import java.util.List;
25-
2626
import static java.nio.charset.StandardCharsets.UTF_8;
2727

2828
/**
@@ -45,7 +45,7 @@ protected <T> T callFunction(Adapter<T> runtime, List<FunctionArgument<T>> argum
4545
}
4646

4747
public static String decode(String encodedString) {
48-
return new String(decode(encodedString.getBytes(UTF_8)));
48+
return new String(decode(encodedString.getBytes(UTF_8)), UTF_8);
4949
}
5050

5151
public static String decode(ByteBuffer byteBuffer) {

powertools-validation/src/main/java/software/amazon/lambda/powertools/validation/jmespath/Base64GZipFunction.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@
1313
*/
1414
package software.amazon.lambda.powertools.validation.jmespath;
1515

16-
import io.burt.jmespath.Adapter;
17-
import io.burt.jmespath.JmesPathType;
18-
import io.burt.jmespath.function.ArgumentConstraints;
19-
import io.burt.jmespath.function.BaseFunction;
20-
import io.burt.jmespath.function.FunctionArgument;
21-
2216
import java.io.BufferedReader;
2317
import java.io.ByteArrayInputStream;
2418
import java.io.IOException;
@@ -27,6 +21,12 @@
2721
import java.util.List;
2822
import java.util.zip.GZIPInputStream;
2923

24+
import io.burt.jmespath.Adapter;
25+
import io.burt.jmespath.JmesPathType;
26+
import io.burt.jmespath.function.ArgumentConstraints;
27+
import io.burt.jmespath.function.BaseFunction;
28+
import io.burt.jmespath.function.FunctionArgument;
29+
3030
import static java.nio.charset.StandardCharsets.UTF_8;
3131
import static software.amazon.lambda.powertools.validation.jmespath.Base64Function.decode;
3232

@@ -68,7 +68,7 @@ public static String decompress(byte[] compressed) {
6868
}
6969
return out.toString();
7070
} catch (IOException e) {
71-
return new String(compressed);
71+
return new String(compressed, UTF_8);
7272
}
7373
}
7474

0 commit comments

Comments
 (0)