Skip to content

Commit 6e7e515

Browse files
edmundmillerclaude
andcommitted
fix(aws): Improve AWS Batch label sanitization with comprehensive logging
Address PR feedback by enhancing the label sanitization logic with better user experience and code maintainability: - Add warning logs when labels are dropped due to null values - Add warning logs when labels are dropped after failed sanitization - Add warning logs when labels are modified during sanitization - Simplify sanitizeAwsBatchLabel method using method chaining and ternary operator - Follow codebase patterns for length truncation (similar to AbstractGridExecutor) - Improve code readability while maintaining all existing functionality Resolves silent label dropping issue and provides clear feedback to users when resource labels are modified or removed during AWS Batch job submission. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> Signed-off-by: Edmund Miller <[email protected]>
1 parent 0e4b1fc commit 6e7e515

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

plugins/nf-amazon/src/main/nextflow/cloud/aws/batch/AwsBatchTaskHandler.groovy

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -868,15 +868,29 @@ class AwsBatchTaskHandler extends TaskHandler implements BatchHandler<String,Job
868868
final key = entry.getKey()
869869
final value = entry.getValue()
870870

871-
if (key != null && value != null) {
872-
final sanitizedKey = sanitizeAwsBatchLabel(key.toString(), 128)
873-
final sanitizedValue = sanitizeAwsBatchLabel(value.toString(), 256)
874-
875-
// Only add non-empty keys and values
876-
if (sanitizedKey && sanitizedValue) {
877-
result.put(sanitizedKey, sanitizedValue)
878-
}
871+
// Handle null keys or values
872+
if (key == null || value == null) {
873+
log.warn "AWS Batch label dropped due to null ${key == null ? 'key' : 'value'}: key=${key}, value=${value}"
874+
continue
879875
}
876+
877+
final originalKey = key.toString()
878+
final originalValue = value.toString()
879+
final sanitizedKey = sanitizeAwsBatchLabel(originalKey, 128)
880+
final sanitizedValue = sanitizeAwsBatchLabel(originalValue, 256)
881+
882+
// Check if sanitization resulted in empty strings
883+
if (!sanitizedKey || !sanitizedValue) {
884+
log.warn "AWS Batch label dropped after sanitization - key: '${originalKey}' -> '${sanitizedKey ?: ''}', value: '${originalValue}' -> '${sanitizedValue ?: ''}'"
885+
continue
886+
}
887+
888+
// Log if values were modified during sanitization
889+
if (sanitizedKey != originalKey || sanitizedValue != originalValue) {
890+
log.warn "AWS Batch label sanitized - key: '${originalKey}' -> '${sanitizedKey}', value: '${originalValue}' -> '${sanitizedValue}'"
891+
}
892+
893+
result.put(sanitizedKey, sanitizedValue)
880894
}
881895

882896
return result
@@ -893,24 +907,19 @@ class AwsBatchTaskHandler extends TaskHandler implements BatchHandler<String,Job
893907
protected String sanitizeAwsBatchLabel(String input, int maxLength) {
894908
if (!input) return input
895909

896-
// Replace invalid characters with underscores
910+
// Replace invalid characters and clean up the string
897911
// AWS Batch allows: letters, numbers, spaces, and: _ . : / = + - @
898-
String sanitized = input.replaceAll(/[^a-zA-Z0-9\s_.\:\/=+\-@]/, '_')
899-
900-
// Replace multiple consecutive underscores/spaces with single underscore
901-
sanitized = sanitized.replaceAll(/[_\s]{2,}/, '_')
912+
final sanitized = input
913+
.replaceAll(/[^a-zA-Z0-9\s_.\:\/=+\-@]/, '_') // Replace invalid chars with underscores
914+
.replaceAll(/[_\s]{2,}/, '_') // Replace multiple consecutive underscores/spaces
915+
.replaceAll(/^[_\s]+|[_\s]+$/, '') // Remove leading/trailing underscores and spaces
902916

903-
// Remove leading/trailing underscores and spaces
904-
sanitized = sanitized.replaceAll(/^[_\s]+|[_\s]+$/, '')
905-
906-
// Truncate if too long
907-
if (sanitized.length() > maxLength) {
908-
sanitized = sanitized.substring(0, maxLength)
909-
// Remove trailing underscore/space after truncation
910-
sanitized = sanitized.replaceAll(/[_\s]+$/, '')
911-
}
917+
// Truncate if necessary and clean up any trailing underscores/spaces
918+
final result = sanitized.size() > maxLength
919+
? sanitized.substring(0, maxLength).replaceAll(/[_\s]+$/, '')
920+
: sanitized
912921

913-
return sanitized ?: null
922+
return result ?: null
914923
}
915924

916925
/**

0 commit comments

Comments
 (0)