Skip to content

Commit

Permalink
add spring-boot-to-azure-system-config rule (#968)
Browse files Browse the repository at this point in the history
Co-authored-by: kaiqianyang <kaiqianyang@microsoftcom>
  • Loading branch information
KaiqianYang and kaiqianyang committed Jun 2, 2023
1 parent 9c87a1d commit 646b9fc
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0"?>
<ruleset id="spring-boot-to-azure-system-config"
xmlns="http://windup.jboss.org/schema/jboss-ruleset"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://windup.jboss.org/schema/jboss-ruleset http://windup.jboss.org/schema/jboss-ruleset/windup-jboss-ruleset.xsd">
<metadata>
<description>
Find usages of System.getenv, System.getProperty, System.setProperty, System.setProperties
</description>
<dependencies>
<addon id="org.jboss.windup.rules,windup-rules-xml,3.0.0.Final"/>
</dependencies>
<sourceTechnology id="springboot"/>
<targetTechnology id="azure-spring-apps"/>
<targetTechnology id="azure-appservice"/>
<targetTechnology id="azure-aks"/>
<targetTechnology id="azure-container-apps"/>
<tag>config</tag>
</metadata>
<rules>
<rule id="spring-boot-to-azure-system-config-01000">
<when>
<or>
<javaclass references="java.lang.System.getenv({*})">
<location>METHOD_CALL</location>
</javaclass>
<javaclass references="java.lang.System.getProperty({*})">
<location>METHOD_CALL</location>
</javaclass>
<javaclass references="java.lang.System.setProperty({*})">
<location>METHOD_CALL</location>
</javaclass>
<javaclass references="java.lang.System.setProperties({*})">
<location>METHOD_CALL</location>
</javaclass>
</or>
</when>
<perform>
<hint title="The application uses environment variables/system properties" category-id="optional" effort="1">
<message>
<![CDATA[
Review the usage of environment variables and system properties and externalize them.
You can inject any per-service configuration settings into each service as environment variables.
Any system properties that the code depends on will need to be defined in JVM options.
]]>
</message>

<link title="Configure per-service secrets and externalized settings" href="https://learn.microsoft.com/azure/developer/java/migration/migrate-spring-boot-to-azure-spring-apps#configure-per-service-secrets-and-externalized-settings"/>
</hint>
</perform>
</rule>
</rules>
</ruleset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Contains code that should match for the following tests:
//
// eap-to-azure-appservice-environment-variables-001

public class SystemGetEnvGetProperty {

public static void main(String[] args) {
int PORT = -1;

// Attempt to read a port number from the environment
try {
PORT = Integer.parseInt(System.getenv("PORT"));
} catch (Exception ex) {
}

// Attempt to read the port number from a System property
try {
PORT = Integer.parseInt(System.getProperty("PORT"));
} catch (Exception ex) {
}

if (PORT < 0) {
PORT = 8080;
}

System.out.println(String.format("Found port: %d", PORT));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0"?>
<ruletest xmlns="http://windup.jboss.org/schema/jboss-ruleset"
id="spring-boot-to-azure-system-config-test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://windup.jboss.org/schema/jboss-ruleset http://windup.jboss.org/schema/jboss-ruleset/windup-jboss-ruleset.xsd">
<testDataPath>data/spring-boot-to-azure-system-config</testDataPath>
<!-- By default sourceMode is True, so it will only consider .java files, not .jar binaries -->
<sourceMode>true</sourceMode>
<rulePath>../spring-boot-to-azure-system-config.windup.xml</rulePath>
<ruleset>
<rules>
<rule id="spring-boot-to-azure-system-config-001-test">
<when>
<not>
<iterable-filter size="2">
<hint-exists message="Review the usage of environment variables and system properties and externalize them*"/>
</iterable-filter>
</not>
</when>
<perform>
<fail message="[spring-boot-to-azure-system-config-001-test] hint was not found!" />
</perform>
</rule>
</rules>
</ruleset>
</ruletest>

0 comments on commit 646b9fc

Please sign in to comment.