Skip to content

Commit

Permalink
Merge pull request #116 from intergral/cf-2023-windows-cfplugin-fix
Browse files Browse the repository at this point in the history
fix(CFPlugin): fix CF server detection for CF 2023 Windows service
  • Loading branch information
Umaaz committed Mar 12, 2024
2 parents f9f0573 + f0b74b1 commit 8240d95
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 1.2.1 - (12/03/2024)
- **[BUGFIX]**: plugin: fix ColdFusion detection on Windows [#116](https://github.com/intergral/deep/pull/116) [@LMarkie](https://github.com/LMarkie)

# 1.2.0 - (06/02/2024)
- **[CHANGE]**: change log config to allow better control of logging [#103](https://github.com/intergral/deep/pull/103) [@Umaaz](https://github.com/Umaaz)
- **[CHANGE]**: change tracepoint logger to be a plugin [#106](https://github.com/intergral/deep/pull/106) [@Umaaz](https://github.com/Umaaz)
Expand Down
9 changes: 9 additions & 0 deletions plugins/cf-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.junit-pioneer/junit-pioneer -->
<dependency>
<groupId>org.junit-pioneer</groupId>
<artifactId>junit-pioneer</artifactId>
<version>2.2.0</version>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,22 @@ private Utils() {
/**
* Are we running on a CF server.
* <p>
* By looking at the java start up command we can tell if this is a CF server.
* By checking that the {@code coldfusion.home} system property exists, or by looking at the java start up command,
* we can tell if this is a CF server.
*
* @return {@code true} if we are on a coldfusion server.
*/
public static boolean isCFServer() {
return System.getProperty("sun.java.command").contains("coldfusion");
if (System.getProperty("coldfusion.home") != null) {
return true;
}

final String javaCommand = System.getProperty("sun.java.command");
// has the potential to not exist on Windows services
if (javaCommand == null) {
return false;
}
return javaCommand.contains("coldfusion");
}

/**
Expand Down
27 changes: 27 additions & 0 deletions plugins/cf-plugin/src/test/java/coldfusion/Version.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2024 Intergral GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package coldfusion;

public class Version {
public static int getMajor() {
if (System.getProperty("cf.test.error") != null) {
throw new RuntimeException("failed to load version");
}
return 10;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (C) 2024 Intergral GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.intergral.deep.plugin.cf;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import com.intergral.deep.agent.api.plugin.EvaluationException;
import com.intergral.deep.agent.api.plugin.ISnapshotContext;
import com.intergral.deep.agent.api.resource.Resource;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

class CFPluginTest {

@Test
void canDecorate() throws EvaluationException {
final CFPlugin cfPlugin = new CFPlugin();
final ISnapshotContext mock = Mockito.mock(ISnapshotContext.class);

final Resource decorate = cfPlugin.decorate(null, mock);
assertNotNull(decorate);
assertEquals("10", decorate.getAttributes().get("cf_version"));
assertNull(decorate.getAttributes().get("app_name"));

Mockito.verify(mock).evaluateExpression("APPLICATION.applicationname");
}

@Test
void canHandleEvaluateDecorate() throws EvaluationException {
final CFPlugin cfPlugin = new CFPlugin();

final ISnapshotContext mock = Mockito.mock(ISnapshotContext.class);

Mockito.when(mock.evaluateExpression("APPLICATION.applicationname")).thenReturn("app_name");

final Resource decorate = cfPlugin.decorate(null, mock);
assertNotNull(decorate);
assertEquals("10", decorate.getAttributes().get("cf_version"));
assertEquals("app_name", decorate.getAttributes().get("app_name"));

Mockito.verify(mock).evaluateExpression("APPLICATION.applicationname");
}

@Test
void canHandleEvaluateFailedDecorate() throws EvaluationException {
final CFPlugin cfPlugin = new CFPlugin();

final ISnapshotContext mock = Mockito.mock(ISnapshotContext.class);

Mockito.when(mock.evaluateExpression("APPLICATION.applicationname")).thenThrow(new RuntimeException("test exception"));

final Resource decorate = cfPlugin.decorate(null, mock);
assertNotNull(decorate);
assertEquals("10", decorate.getAttributes().get("cf_version"));
assertNull(decorate.getAttributes().get("app_name"));

Mockito.verify(mock).evaluateExpression("APPLICATION.applicationname");
}

@Test
void isActive() {
assertFalse(new CFPlugin().isActive());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2024 Intergral GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.intergral.deep.plugin.cf;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.ClearSystemProperty;
import org.junitpioneer.jupiter.SetSystemProperty;

class UtilsTest {

@Test
void canDetectNotCF() {
assertFalse(Utils.isCFServer());
}

@Test
@ClearSystemProperty(key = "sun.java.command")
void canHandleNoSunCommand() {
assertFalse(Utils.isCFServer());
}

@Test
@SetSystemProperty(key = "coldfusion.home", value = "doesn't matter")
void canDetectCFHome() {
assertTrue(Utils.isCFServer());
}

@Test
@SetSystemProperty(key = "sun.java.command", value = "/some/coldfusion")
void canUseSunCommand() {
assertTrue(Utils.isCFServer());
}

@Test
void loadCFVersion() {
assertEquals("10", Utils.loadCFVersion());
}

@Test
@SetSystemProperty(key = "cf.test.error", value = "anything")
void failLoadCFVersion() {
assertNull(Utils.loadCFVersion());
}
}
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,8 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.1</version>
<!-- Version upgrade blocked by junit-pioneer version -->
<version>5.9.3</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
Expand Down

0 comments on commit 8240d95

Please sign in to comment.