Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ private String constructDatabaseUrl(JdbcProtocol jdbcProtocol, String host, Stri
case "oracle" -> "jdbc:oracle:thin:@" + host + ":" + port
+ "/" + (jdbcProtocol.getDatabase() == null ? "" : jdbcProtocol.getDatabase());
case "dm" -> "jdbc:dm://" + host + ":" + port;
case "testcontainers" -> "jdbc:tc:" + host + ":" + port
Comment on lines 385 to +386
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardcoded Database Credentials in JDBC URL

The code adds a new case for testcontainers that includes hardcoded database credentials ('root'/'root') directly in the JDBC URL. While this is only used for testing purposes, hardcoded credentials in source code are generally considered a security risk as they could be inadvertently exposed or reused in non-test environments.

            case "testcontainers" -> "jdbc:tc:" + host + ":" + port
                    + "://" + (jdbcProtocol.getDatabase() == null ? "" : jdbcProtocol.getDatabase()) + "?user=" + 
                    (jdbcProtocol.getUsername() == null ? "root" : jdbcProtocol.getUsername()) + "&password=" + 
                    (jdbcProtocol.getPassword() == null ? "root" : jdbcProtocol.getPassword());

References

Standard: CWE-798
Standard: OWASP Top 10 2021: A07 - Identification and Authentication Failures

+ ":///" + (jdbcProtocol.getDatabase() == null ? "" : jdbcProtocol.getDatabase()) + "?user=root&password=root";
default -> throw new IllegalArgumentException("Not support database platform: " + jdbcProtocol.getPlatform());
};
}
Expand Down
42 changes: 40 additions & 2 deletions hertzbeat-e2e/hertzbeat-collector-basic-e2e/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mysql.version>8.0.30</mysql.version>
<postgresql.version>42.5.5</postgresql.version>
</properties>

<dependencies>
Expand All @@ -53,10 +55,46 @@
<version>${hertzbeat.version}</version>
<scope>test</scope>
</dependency>

<!-- Testcontainers Jupiter / JUnit 5 -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<!-- Testcontainers JDBC Support -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<artifactId>jdbc</artifactId>
<scope>test</scope>
</dependency>
<!-- Testcontainers Database Modules -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>

<!-- JDBC Drivers -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
<scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
<scope>test</scope>
<optional>true</optional>
</dependency>

</dependencies>
</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.hertzbeat.collector.collect.basic.database;

/**
* Database type (image mame) and image tag enumeration class
*/
public enum DatabaseImagesEnum {
MYSQL("mysql", "8.0.36"),
POSTGRESQL("postgresql", "15");

private final String imageName;
private final String defaultTag;

DatabaseImagesEnum(String imageName, String defaultTag) {
this.imageName = imageName;
this.defaultTag = defaultTag;
}

public String getImageName() {
return imageName;
}

public String getDefaultTag() {
return defaultTag;
}

public String getFullImageName() {
return imageName + ":" + defaultTag;
}

public static DatabaseImagesEnum fromImageName(String imageName) {
for (DatabaseImagesEnum value : values()) {
if (value.getImageName().equalsIgnoreCase(imageName)) {
return value;
}
}
Comment on lines +45 to +52
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing Error Handling in TestContainers Database Type Resolution

The DatabaseImagesEnum.fromImageName method throws an IllegalArgumentException when an unknown database image name is provided, but there's no error handling in the JdbcCommonCollectE2eTest that uses this method with a hardcoded value. If the database type is changed or removed in the future, tests will fail with an uncaught exception rather than gracefully handling the error. This violates error handling best practices and could lead to difficult-to-diagnose test failures.

    public static DatabaseImagesEnum fromImageName(String imageName) {
        if (imageName == null || imageName.trim().isEmpty()) {
            return POSTGRESQL; // Default to PostgreSQL if no name provided
        }
        
        for (DatabaseImagesEnum value : values()) {
            if (value.getImageName().equalsIgnoreCase(imageName)) {
                return value;
            }
        }
        throw new IllegalArgumentException("Unknown database image name: " + imageName);
    }

References

Standard: Design by Contract - Precondition Validation

throw new IllegalArgumentException("Unknown database image name: " + imageName);
}

public static DatabaseImagesEnum fromFullImageName(String fullImageName) {
for (DatabaseImagesEnum value : values()) {
if (value.getFullImageName().equalsIgnoreCase(fullImageName)) {
return value;
}
}
throw new IllegalArgumentException("Unknown full database image name: " + fullImageName);
}

@Override
public String toString() {
return "DatabaseImageNameEnum{" +
"imageName='" + imageName + '\'' +
", defaultTag='" + defaultTag + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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.hertzbeat.collector.collect.basic.database;

import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.collector.collect.AbstractCollectE2eTest;
import org.apache.hertzbeat.collector.collect.database.JdbcCommonCollect;
import org.apache.hertzbeat.collector.util.CollectUtil;
import org.apache.hertzbeat.common.entity.job.Configmap;
import org.apache.hertzbeat.common.entity.job.Job;
import org.apache.hertzbeat.common.entity.job.Metrics;
import org.apache.hertzbeat.common.entity.job.protocol.JdbcProtocol;
import org.apache.hertzbeat.common.entity.job.protocol.Protocol;
import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

/**
* E2e test monitored by Jdbc Common
Comment on lines +38 to +43
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing Container Cleanup in JdbcCommonCollectE2eTest

The test class uses TestContainers for database testing but doesn't properly manage container lifecycle. TestContainers typically requires explicit container initialization and cleanup. Without proper container management, tests may leave containers running after completion, causing resource leaks, potential port conflicts, and increased resource consumption during test execution. This violates resource management best practices and could lead to unstable test environments.

@Testcontainers
public class JdbcCommonCollectE2eTest extends AbstractCollectE2eTest {
    private static final DatabaseImagesEnum databaseImage = DatabaseImagesEnum.fromImageName("postgresql");

    private static final String DATABASE_IMAGE_NAME = databaseImage.getImageName();

    private static final String DATABASE_IMAGE_TAG = databaseImage.getDefaultTag();

    @Container
    private static final PostgreSQLContainer<?> postgresContainer = new PostgreSQLContainer<>(databaseImage.getFullImageName())
            .withDatabaseName("test")
            .withUsername("root")
            .withPassword("root");

References

Standard: ISO/IEC 25010 Reliability - Resource Utilization

*/
@Slf4j
@ExtendWith(MockitoExtension.class)
public class JdbcCommonCollectE2eTest extends AbstractCollectE2eTest {
private static final DatabaseImagesEnum databaseImage = DatabaseImagesEnum.fromImageName("postgresql");

private static final String DATABASE_IMAGE_NAME = databaseImage.getImageName();

private static final String DATABASE_IMAGE_TAG = databaseImage.getDefaultTag();

@BeforeEach
public void setUp() throws Exception {
super.setUp();
collect = new JdbcCommonCollect();
metrics = new Metrics();
}

@Override
protected CollectRep.MetricsData.Builder collectMetrics(Metrics metricsDef) {
JdbcProtocol jdbcProtocol = (JdbcProtocol) buildProtocol(metricsDef);
metrics.setJdbc(jdbcProtocol);
CollectRep.MetricsData.Builder metricsData = CollectRep.MetricsData.newBuilder();
metricsData.setApp(DATABASE_IMAGE_NAME);
metrics.setAliasFields(metricsDef.getAliasFields());
return collectMetricsData(metrics, metricsDef, metricsData);
}

@Override
protected Protocol buildProtocol(Metrics metricsDef) {
JdbcProtocol jdbcProtocol = metricsDef.getJdbc();
jdbcProtocol.setHost(DATABASE_IMAGE_NAME);
jdbcProtocol.setPort(DATABASE_IMAGE_TAG);
jdbcProtocol.setDatabase("test");
jdbcProtocol.setPlatform("testcontainers");
return jdbcProtocol;
}
Comment on lines +71 to +79
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Verify if port assignment is correct

Line 75 assigns DATABASE_IMAGE_TAG to the port property, which seems unusual as ports are typically numeric values. Consider verifying if this is the intended behavior or if it should be using an actual port number.


🏁 Script executed:

#!/bin/bash
# Check how DATABASE_IMAGE_TAG is used and if it's appropriate for port configuration
echo "Checking DatabaseImagesEnum class for tag definition..."
fd -t f -e java "DatabaseImagesEnum.java" | xargs cat

echo "Checking JdbcProtocol class for port field type..."
fd -t f -e java "JdbcProtocol.java" | xargs cat | grep -A 10 "private.*port"

Length of output: 2986


🏁 Script executed:

#!/bin/bash
# Display the relevant test file context around the buildProtocol method
fd -t f -e java "JdbcCommonCollectE2eTest.java" | xargs sed -n '1,200p'

Length of output: 4427


🏁 Script executed:

#!/bin/bash
# Locate and display AbstractCollectE2eTest class implementation context around container setup or protocol handling
fd -t f -e java "AbstractCollectE2eTest.java" | xargs sed -n '1,200p'

Length of output: 6135


Confirm port assignment in buildProtocol

The test currently does:

jdbcProtocol.setHost(DATABASE_IMAGE_NAME);
jdbcProtocol.setPort(DATABASE_IMAGE_TAG);

Here, DATABASE_IMAGE_TAG comes from DatabaseImagesEnum.getDefaultTag() (e.g. "15"), which is the Docker image version—not a TCP port. Since JdbcProtocol.port is the port used to connect to the database, this will direct the driver to port 15 rather than the actual database port (e.g. 5432 or the Testcontainers–mapped port).

Please verify and update accordingly:

  • If you’re using Testcontainers, start the container and set:
    jdbcProtocol.setPort(String.valueOf(container.getFirstMappedPort()));
  • Otherwise, use the default database port (e.g. "5432") or inject it via configuration.

Location:

  • hertzbeat-e2e/hertzbeat-collector-basic-e2e/src/test/java/org/apache/hertzbeat/collector/collect/basic/database/JdbcCommonCollectE2eTest.java
    protected Protocol buildProtocol(Metrics metricsDef) { … }


@Test
public void testWithJdbcTcUrl() {
Job dockerJob = appService.getAppDefine(DATABASE_IMAGE_NAME);
List<Map<String, Configmap>> configmapFromPreCollectData = new LinkedList<>();
for (Metrics metricsDef : dockerJob.getMetrics()) {
metricsDef = CollectUtil.replaceCryPlaceholderToMetrics(metricsDef, configmapFromPreCollectData.size() > 0 ? configmapFromPreCollectData.get(0) : new HashMap<>());
String metricName = metricsDef.getName();
if ("slow_sql".equals(metricName)) {
Metrics finalMetricsDef = metricsDef;
assertDoesNotThrow(() -> collectMetrics(finalMetricsDef),
String.format("%s failed to collect metrics)", metricName));
log.info("{} metrics validation passed", metricName);
continue; // skip slow_sql, extra extensions
}
CollectRep.MetricsData metricsData = validateMetricsCollection(metricsDef, metricName, true);
CollectUtil.getConfigmapFromPreCollectData(metricsData);
}
Comment on lines +95 to +97
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

The return value of getConfigmapFromPreCollectData is not used

Line 96 calls CollectUtil.getConfigmapFromPreCollectData(metricsData) but doesn't use the return value. Consider either storing and using the return value or adding a comment explaining why it's called without using the result.

-CollectUtil.getConfigmapFromPreCollectData(metricsData);
+// Extract configmap data for potential use in subsequent test iterations
+Map<String, Configmap> configmap = CollectUtil.getConfigmapFromPreCollectData(metricsData);
+if (!configmap.isEmpty()) {
+    configmapFromPreCollectData.add(configmap);
+}

Committable suggestion skipped: line range outside the PR's diff.

}
Comment on lines +91 to +98
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing Error Handling in Database Test Implementation

The test method handles the 'slow_sql' metric case differently but doesn't provide sufficient error handling or logging for diagnostic purposes. While the code uses assertDoesNotThrow() to verify execution, it doesn't capture or validate the actual result of the metrics collection. This makes it harder to diagnose test failures and understand what specifically went wrong with slow_sql metric collection.

            if ("slow_sql".equals(metricName)) {
                Metrics finalMetricsDef = metricsDef;
                try {
                    CollectRep.MetricsData metricsData = collectMetrics(finalMetricsDef).build();
                    assertNotNull(metricsData, String.format("%s metrics data should not be null", metricName));
                    log.info("{} metrics validation passed with {} rows", metricName,
                             metricsData.getValuesCount() > 0 ? metricsData.getValuesCount() : "no");
                } catch (Exception e) {
                    log.error("Error collecting {} metrics: {}", metricName, e.getMessage());
                    fail(String.format("%s metrics collection failed: %s", metricName, e.getMessage()));
                }
                continue;  // skip slow_sql, extra extensions
            }

References

Standard: JUnit Best Practices - Error Diagnostics, Clean Code - Error Handling

}
12 changes: 12 additions & 0 deletions hertzbeat-e2e/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,16 @@
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${testcontainers.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>