Skip to content

Commit 65ae444

Browse files
committed
Final Copilot comments
1 parent 6bf39e9 commit 65ae444

3 files changed

Lines changed: 40 additions & 5 deletions

File tree

src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleRunner.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,10 +1286,26 @@ public String encryptExtraVarsKey(String extraVars) throws Exception {
12861286

12871287
/**
12881288
* Sanitizes a node name to be safe for use in filesystem paths.
1289-
* Replaces unsafe characters with underscores and prevents hidden files and problematic names.
1289+
* <p>
1290+
* This method performs the following transformations:
1291+
* <ul>
1292+
* <li>Replaces all characters except alphanumeric, dot, underscore, and hyphen with underscores</li>
1293+
* <li>Prevents hidden files by prepending underscore if name starts with dot</li>
1294+
* <li>Handles empty strings by prepending underscore</li>
1295+
* </ul>
1296+
* </p>
1297+
* <p>
1298+
* Examples:
1299+
* <ul>
1300+
* <li>{@code "node-1"} → {@code "node-1"} (no change)</li>
1301+
* <li>{@code "node@host"} → {@code "node_host"} (@ replaced)</li>
1302+
* <li>{@code ".hidden"} → {@code "_.hidden"} (prevents hidden file)</li>
1303+
* <li>{@code ""} → {@code "_"} (handles empty)</li>
1304+
* </ul>
1305+
* </p>
12901306
*
1291-
* @param nodeName The original node name
1292-
* @return A sanitized node name safe for use in file paths
1307+
* @param nodeName The original node name to sanitize
1308+
* @return A sanitized node name safe for use in file paths, never null
12931309
*/
12941310
String sanitizeNodeNameForFilesystem(String nodeName) {
12951311
// Replace unsafe characters with underscores

src/main/groovy/com/rundeck/plugins/ansible/ansible/AnsibleRunnerContextBuilder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,9 @@ public String getPasswordFromPath(String storagePath) throws ConfigurationExcept
226226
*
227227
* @param storagePath The storage path to read from
228228
* @param resourceType Description of resource type for error messages (e.g., "ssh password", "ssh private key")
229-
* @return The content as a UTF-8 string, or null if storagePath is null
230-
* @throws ConfigurationException if reading fails
229+
* @return The content as a UTF-8 string. Returns null ONLY if storagePath parameter is null (early return).
230+
* After attempting to read content, this method either returns a non-null string or throws an exception.
231+
* @throws ConfigurationException if the storage path cannot be read or does not exist
231232
*/
232233
private String readFromStoragePath(String storagePath, String resourceType) throws ConfigurationException {
233234
if (storagePath == null) {

src/test/groovy/com/rundeck/plugins/ansible/ansible/AnsibleRunnerSpec.groovy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,24 @@ class AnsibleRunnerSpec extends Specification{
623623
"key with spaces" || "key with spaces" // internal spaces are valid in unquoted YAML keys; node names with leading/trailing spaces are not supported
624624
}
625625

626+
def "escapeYamlKey: node names with leading/trailing spaces are not modified"() {
627+
given:
628+
def builder = AnsibleRunner.playbookInline("test")
629+
builder.customTmpDirPath("/tmp")
630+
def runner = builder.build()
631+
632+
expect:
633+
// This documents that escapeYamlKey does not normalize leading/trailing spaces.
634+
// Node names with such spaces are considered unsupported at a higher level.
635+
runner.escapeYamlKey(key) == expectedResult
636+
637+
where:
638+
key || expectedResult
639+
" leading-space" || " leading-space"
640+
"trailing-space " || "trailing-space "
641+
" both-sides " || " both-sides "
642+
}
643+
626644
def "escapeYamlValue: should quote values with special characters"() {
627645
given:
628646
def builder = AnsibleRunner.playbookInline("test")

0 commit comments

Comments
 (0)