-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add exclusion list option for calling DatabaseMetaData.getUserName (#…
…3568) * use a dummy user for testing * exclusion list option for calling getUserName * changelog and test break fixed
- Loading branch information
1 parent
a10ecd4
commit 2e1bce7
Showing
5 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...ins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/helper/JdbcConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. 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 co.elastic.apm.agent.jdbc.helper; | ||
|
||
import co.elastic.apm.agent.common.util.WildcardMatcher; | ||
import co.elastic.apm.agent.tracer.configuration.WildcardMatcherValueConverter; | ||
import org.stagemonitor.configuration.ConfigurationOption; | ||
import org.stagemonitor.configuration.ConfigurationOptionProvider; | ||
import org.stagemonitor.configuration.converter.DoubleValueConverter; | ||
import org.stagemonitor.configuration.converter.ListValueConverter; | ||
import org.stagemonitor.configuration.converter.StringValueConverter; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class JdbcConfiguration extends ConfigurationOptionProvider { | ||
|
||
private final ConfigurationOption<List<String>> databaseMetaDataExclusionList = ConfigurationOption | ||
.builder(new ListValueConverter<String>(StringValueConverter.INSTANCE), List.class) | ||
.key("exclude_from_getting_username") | ||
.configurationCategory("Datastore") | ||
.description("If any of these strings match part of the package or class name of the DatabaseMetaData instance, getUserName() won't be called" + | ||
"\n" + | ||
WildcardMatcher.DOCUMENTATION | ||
) | ||
.tags("internal","added[1.49.0]") | ||
.dynamic(true) | ||
.buildWithDefault(Arrays.asList( | ||
"hikari" | ||
)); | ||
|
||
public List<String> getDatabaseMetaDataExclusionList() { | ||
return databaseMetaDataExclusionList.get(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...in/resources/META-INF/services/org.stagemonitor.configuration.ConfigurationOptionProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
co.elastic.apm.agent.jdbc.helper.JdbcConfiguration |
77 changes: 77 additions & 0 deletions
77
...c-plugin/src/test/java/co/elastic/apm/agent/jdbc/helper/JdbcGetUserNameExclusionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. 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 co.elastic.apm.agent.jdbc.helper; | ||
|
||
import co.elastic.apm.agent.AbstractInstrumentationTest; | ||
import co.elastic.apm.agent.MockTracer; | ||
import co.elastic.apm.agent.bci.ElasticApmAgent; | ||
import co.elastic.apm.agent.configuration.SpyConfiguration; | ||
import co.elastic.apm.agent.impl.ElasticApmTracer; | ||
import net.bytebuddy.agent.ByteBuddyAgent; | ||
import org.junit.After; | ||
import org.junit.BeforeClass; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.stagemonitor.configuration.ConfigurationRegistry; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
import java.sql.DatabaseMetaData; | ||
import java.sql.SQLException; | ||
import java.util.List; | ||
|
||
import static co.elastic.apm.agent.testutils.assertions.Assertions.assertThat; | ||
import static org.mockito.Mockito.doReturn; | ||
|
||
public class JdbcGetUserNameExclusionTest extends AbstractInstrumentationTest { | ||
|
||
protected static JdbcConfiguration jdbcconfig; | ||
|
||
@Test | ||
public void hasUsernameCorrectlyExcludes() throws SQLException { | ||
DatabaseMetaData meta = (DatabaseMetaData) Proxy.newProxyInstance( | ||
this.getClass().getClassLoader(), | ||
new Class[] { DatabaseMetaData.class }, | ||
new MetadataInvocationHandler()); | ||
|
||
assertThat(JdbcHelper.maybeGetUserName(meta, config.getConfig(JdbcConfiguration.class))).isEqualTo("testuser"); | ||
|
||
String classname = meta.getClass().getName(); | ||
String excludeName = classname.substring(classname.indexOf('$')+1); | ||
doReturn(List.of(excludeName)) | ||
.when(config.getConfig(JdbcConfiguration.class)) | ||
.getDatabaseMetaDataExclusionList(); | ||
|
||
assertThat(JdbcHelper.maybeGetUserName(meta, config.getConfig(JdbcConfiguration.class))).isEqualTo(null); | ||
} | ||
|
||
public class MetadataInvocationHandler implements InvocationHandler { | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
if (method.getName().equals("getUserName")) { | ||
return "testuser"; | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
} |