Skip to content

Commit ca32710

Browse files
authored
Merge branch 'master' into andrea.marziali/ensure-metrics-run
2 parents de1e111 + 5353d51 commit ca32710

35 files changed

+2423
-20
lines changed

.github/workflows/analyze-changes.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
${{ runner.os }}-gradle-
4141
4242
- name: Initialize CodeQL
43-
uses: github/codeql-action/init@ce28f5bb42b7a9f2c824e633a3f6ee835bab6858 # v3.29.0
43+
uses: github/codeql-action/init@181d5eefc20863364f96762470ba6f862bdef56b # v3.29.2
4444
with:
4545
languages: 'java'
4646
build-mode: 'manual'
@@ -57,7 +57,7 @@ jobs:
5757
--build-cache --parallel --stacktrace --no-daemon --max-workers=4
5858
5959
- name: Perform CodeQL Analysis and upload results to GitHub Security tab
60-
uses: github/codeql-action/analyze@ce28f5bb42b7a9f2c824e633a3f6ee835bab6858 # v3.29.0
60+
uses: github/codeql-action/analyze@181d5eefc20863364f96762470ba6f862bdef56b # v3.29.2
6161

6262
trivy:
6363
name: Analyze changes with Trivy
@@ -122,7 +122,7 @@ jobs:
122122
TRIVY_JAVA_DB_REPOSITORY: ghcr.io/aquasecurity/trivy-java-db,public.ecr.aws/aquasecurity/trivy-java-db
123123

124124
- name: Upload Trivy scan results to GitHub Security tab
125-
uses: github/codeql-action/upload-sarif@ce28f5bb42b7a9f2c824e633a3f6ee835bab6858 # v3.29.0
125+
uses: github/codeql-action/upload-sarif@181d5eefc20863364f96762470ba6f862bdef56b # v3.29.2
126126
if: always()
127127
with:
128128
sarif_file: 'trivy-results.sarif'

.gitlab-ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ default:
121121

122122
.gitlab_base_ref_params: &gitlab_base_ref_params
123123
- |
124-
if [[ ! $CI_COMMIT_BRANCH =~ ^(master|release/.*)$ ]]; then
124+
# FIXME: Disabled until we find a way to not hit GitHub API rate limit
125+
if false && [[ ! $CI_COMMIT_BRANCH =~ ^(master|release/.*)$ ]]; then
125126
export GIT_BASE_REF=$(.gitlab/find-gh-base-ref.sh)
126127
if [[ -n "$GIT_BASE_REF" ]]; then
127128
export GRADLE_PARAMS="$GRADLE_PARAMS -PgitBaseRef=origin/$GIT_BASE_REF"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
plugins {
2+
`java-library`
3+
id("com.gradleup.shadow")
4+
}
5+
6+
apply(from = "$rootDir/gradle/java.gradle")
7+
8+
/*
9+
* Add an addition gradle configuration to be consumed by bootstrap only.
10+
* "datadog.trace." prefix is required to be excluded from Jacoco instrumentation.
11+
* See ConfigDefaults.DEFAULT_CIVISIBILITY_JACOCO_PLUGIN_EXCLUDES for more details.
12+
*/
13+
tasks.shadowJar {
14+
relocate("datadog.environment", "datadog.trace.bootstrap.environment")
15+
}
16+
17+
/*
18+
* Configure test coverage.
19+
*/
20+
extra.set("minimumInstructionCoverage", 0.7)
21+
val excludedClassesCoverage by extra {
22+
listOf(
23+
"datadog.environment.JavaVirtualMachine", // depends on OS and JVM vendor
24+
"datadog.environment.JavaVirtualMachine.JvmOptionsHolder", // depends on OS and JVM vendor
25+
"datadog.environment.JvmOptions", // depends on OS and JVM vendor
26+
"datadog.environment.OperatingSystem", // depends on OS
27+
)
28+
}
29+
val excludedClassesBranchCoverage by extra {
30+
listOf("datadog.environment.CommandLine") // tested using forked process
31+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package datadog.environment;
2+
3+
import static java.util.Collections.emptyList;
4+
5+
import de.thetaphi.forbiddenapis.SuppressForbidden;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
/**
10+
* Fetches and captures the command line, both command and its arguments. It relies on a
11+
* non-standard {@code sun.java.command} system property and was tested on:
12+
*
13+
* <ul>
14+
* <li>OracleJDK,
15+
* <li>OpenJDK,
16+
* <li>Temurin based JDK,
17+
* <li>IMB JDK,
18+
* <li>Azul Zulu,
19+
* <li>Amazon Coretto,
20+
* </ul>
21+
*
22+
* This should be replaced by {@code ProcessHandle} and {@code ProcessHandle.Info} once Java 9+
23+
* become available.
24+
*/
25+
class CommandLine {
26+
private static final String SUN_JAVA_COMMAND_PROPERTY = "sun.java.command";
27+
final List<String> fullCommand = findFullCommand();
28+
final String name = getCommandName();
29+
final List<String> arguments = getCommandArguments();
30+
31+
@SuppressForbidden // split on single-character uses fast path
32+
private List<String> findFullCommand() {
33+
String command = SystemProperties.getOrDefault(SUN_JAVA_COMMAND_PROPERTY, "").trim();
34+
return command.isEmpty() ? emptyList() : Arrays.asList(command.split(" "));
35+
}
36+
37+
private String getCommandName() {
38+
return fullCommand.isEmpty() ? null : fullCommand.get(0);
39+
}
40+
41+
private List<String> getCommandArguments() {
42+
if (fullCommand.isEmpty()) {
43+
return fullCommand;
44+
} else {
45+
return fullCommand.subList(1, fullCommand.size());
46+
}
47+
}
48+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package datadog.environment;
2+
3+
import javax.annotation.Nonnull;
4+
import javax.annotation.Nullable;
5+
6+
/**
7+
* Safely queries environment variables against security manager.
8+
*
9+
* @see <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/SecurityManager.html">Security
10+
* Manager</a>
11+
*/
12+
public final class EnvironmentVariables {
13+
private EnvironmentVariables() {}
14+
15+
/**
16+
* Gets an environment variable value.
17+
*
18+
* @param name The environment variable name.
19+
* @return The environment variable value, {@code null} if missing or can't be retrieved.
20+
*/
21+
public static @Nullable String get(String name) {
22+
return getOrDefault(name, null);
23+
}
24+
25+
/**
26+
* Gets an environment variable value, or default value if missing or can't be retrieved.
27+
*
28+
* @param name The environment variable name.
29+
* @param defaultValue The default value to return if the environment variable is missing or can't
30+
* be retrieved.
31+
* @return The environment variable value, {@code defaultValue} if missing or can't be retrieved.
32+
*/
33+
public static String getOrDefault(@Nonnull String name, String defaultValue) {
34+
try {
35+
String value = System.getenv(name);
36+
return value == null ? defaultValue : value;
37+
} catch (SecurityException e) {
38+
return defaultValue;
39+
}
40+
}
41+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package datadog.environment;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* This class represents a Java version according the String Naming Convention.
8+
*
9+
* @see <a href="https://www.oracle.com/java/technologies/javase/versioning-naming.html">String
10+
* Naming Convention</a>
11+
*/
12+
final class JavaVersion {
13+
final int major;
14+
final int minor;
15+
final int update;
16+
17+
JavaVersion(int major, int minor, int update) {
18+
this.major = major;
19+
this.minor = minor;
20+
this.update = update;
21+
}
22+
23+
static JavaVersion getRuntimeVersion() {
24+
return parseJavaVersion(SystemProperties.getOrDefault("java.version", ""));
25+
}
26+
27+
static JavaVersion parseJavaVersion(String javaVersion) {
28+
// Remove pre-release part, usually -ea
29+
final int indexOfDash = javaVersion.indexOf('-');
30+
if (indexOfDash >= 0) {
31+
javaVersion = javaVersion.substring(0, indexOfDash);
32+
}
33+
34+
int major = 0;
35+
int minor = 0;
36+
int update = 0;
37+
38+
try {
39+
List<Integer> nums = splitDigits(javaVersion);
40+
major = nums.get(0);
41+
42+
// for java 1.6/1.7/1.8
43+
if (major == 1) {
44+
major = nums.get(1);
45+
minor = nums.get(2);
46+
update = nums.get(3);
47+
} else {
48+
minor = nums.get(1);
49+
update = nums.get(2);
50+
}
51+
} catch (NumberFormatException | IndexOutOfBoundsException e) {
52+
// unable to parse version string - do nothing
53+
}
54+
return new JavaVersion(major, minor, update);
55+
}
56+
57+
/* The method splits java version string by digits. Delimiters are: dot, underscore and plus */
58+
private static List<Integer> splitDigits(String str) {
59+
List<Integer> results = new ArrayList<>();
60+
61+
int len = str.length();
62+
int value = 0;
63+
for (int i = 0; i < len; i++) {
64+
char ch = str.charAt(i);
65+
if (ch >= '0' && ch <= '9') {
66+
value = value * 10 + (ch - '0');
67+
} else if (ch == '.' || ch == '_' || ch == '+') {
68+
results.add(value);
69+
value = 0;
70+
} else {
71+
throw new NumberFormatException();
72+
}
73+
}
74+
results.add(value);
75+
return results;
76+
}
77+
78+
public boolean is(int major) {
79+
return this.major == major;
80+
}
81+
82+
public boolean is(int major, int minor) {
83+
return this.major == major && this.minor == minor;
84+
}
85+
86+
public boolean is(int major, int minor, int update) {
87+
return this.major == major && this.minor == minor && this.update == update;
88+
}
89+
90+
public boolean isAtLeast(int major, int minor, int update) {
91+
return isAtLeast(this.major, this.minor, this.update, major, minor, update);
92+
}
93+
94+
public boolean isBetween(
95+
int fromMajor, int fromMinor, int fromUpdate, int toMajor, int toMinor, int toUpdate) {
96+
return isAtLeast(toMajor, toMinor, toUpdate, fromMajor, fromMinor, fromUpdate)
97+
&& isAtLeast(fromMajor, fromMinor, fromUpdate)
98+
&& !isAtLeast(toMajor, toMinor, toUpdate);
99+
}
100+
101+
private static boolean isAtLeast(
102+
int major, int minor, int update, int atLeastMajor, int atLeastMinor, int atLeastUpdate) {
103+
return (major > atLeastMajor)
104+
|| (major == atLeastMajor && minor > atLeastMinor)
105+
|| (major == atLeastMajor && minor == atLeastMinor && update >= atLeastUpdate);
106+
}
107+
}

0 commit comments

Comments
 (0)