Skip to content

Commit

Permalink
Merge pull request #1692 from lilai23/fix_pathcheck_2.1.x
Browse files Browse the repository at this point in the history
fix_2.1.x: support dynamic install agent multi-times with different agent directory
  • Loading branch information
Sherlockhan authored Nov 30, 2024
2 parents 949f104 + 8bea7a1 commit d8a2864
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.sermant.core.classloader.ClassLoaderManager;
import io.sermant.core.common.AgentType;
import io.sermant.core.common.BootArgsIndexer;
import io.sermant.core.common.CommonConstant;
import io.sermant.core.common.LoggerFactory;
import io.sermant.core.config.ConfigManager;
import io.sermant.core.event.EventManager;
Expand All @@ -35,6 +36,7 @@
import io.sermant.core.plugin.agent.info.EnhancementManager;
import io.sermant.core.plugin.agent.template.DefaultAdviser;
import io.sermant.core.service.ServiceManager;
import io.sermant.core.utils.FileUtils;
import io.sermant.god.common.SermantManager;

import java.lang.instrument.Instrumentation;
Expand Down Expand Up @@ -90,6 +92,8 @@ public static void install(String artifact, Map<String, Object> argsMap, Instrum
LoggerFactory.initDefaultLogger(artifact);
adviserCache = new DefaultAdviser();

FileUtils.setAgentPath((String) argsMap.get(CommonConstant.AGENT_PATH_KEY));

// Initialize the classloader of framework
ClassLoaderManager.init(argsMap);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ public class CommonConstant {
*/
public static final String ARTIFACT_NAME_KEY = "artifact";

/**
* The key of agent path
*/
public static final String AGENT_PATH_KEY = "agentPath";

private CommonConstant() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public class FileUtils {

private static final String[] INVALID_SYMBOL = {"../", "..\\"};

private static final String AGENT_PATH = new File(new File(FileUtils.class.getProtectionDomain().getCodeSource()
.getLocation().getPath()).getParent()).getParent();
private static String agentPath;

/**
* file name of unmatched class name
Expand All @@ -79,7 +78,7 @@ private FileUtils() {
* @return path
*/
public static String getAgentPath() {
return AGENT_PATH;
return agentPath;
}

/**
Expand All @@ -89,7 +88,7 @@ public static String getAgentPath() {
* @return fixed path
*/
public static String validatePath(String path) {
if (!path.startsWith(AGENT_PATH)) {
if (!path.startsWith(agentPath)) {
return "";
}

Expand Down Expand Up @@ -288,12 +287,16 @@ private static String buildUnmatchedFileString() {
AgentConfig config = ConfigManager.getConfig(AgentConfig.class);
String preFilterPath = config.getPreFilterPath();
if (StringUtils.isEmpty(preFilterPath)) {
preFilterPath = AGENT_PATH;
preFilterPath = agentPath;
}
String preFilterFile = config.getPreFilterFile();
if (StringUtils.isEmpty(preFilterFile)) {
preFilterFile = DEFAULT_OUTPUT_CLASS_NAME_FILE;
}
return preFilterPath + File.separatorChar + preFilterFile;
}

public static void setAgentPath(String path) {
agentPath = path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class FileUtilsTest {
@Test
public void testPath() {
String pathValid = FileUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath();
Assert.assertEquals(pathValid, FileUtils.validatePath(pathValid));
FileUtils.setAgentPath(pathValid);
String pathInvalid = "/test/path";
Assert.assertEquals("", FileUtils.validatePath(pathInvalid));
String pathInvalidSymbolA = pathValid + "../" + "/test";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.sermant.core.operation.converter.api.YamlConverter;
import io.sermant.core.service.dynamicconfig.common.DynamicConfigEvent;
import io.sermant.core.service.dynamicconfig.common.DynamicConfigEventType;
import io.sermant.core.utils.FileUtils;
import io.sermant.core.utils.JarFileUtils;
import io.sermant.core.utils.StringUtils;
import io.sermant.implement.operation.converter.YamlConverterImpl;
Expand Down Expand Up @@ -84,6 +85,8 @@ public void setUp() throws Exception {
.thenReturn(YAML_CONVERTER);
commandProcessorMockedStatic.verify(() -> CommandProcessor.process(any()), times(0));
jarFileUtilsMockedStatic.when(() -> JarFileUtils.getManifestAttr(any(), anyString())).thenReturn("1.0.0");
String pathValid = FileUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath();
FileUtils.setAgentPath(pathValid);
Map<String, Object> argsMap = new HashMap<>();
argsMap.put(CommonConstant.CORE_IMPLEMENT_DIR_KEY, StringUtils.EMPTY);
argsMap.put(CommonConstant.CORE_CONFIG_FILE_KEY, StringUtils.EMPTY);
Expand Down

0 comments on commit d8a2864

Please sign in to comment.