Skip to content

Commit f29b949

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 20cfbf5 commit f29b949

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
@@ -883,15 +883,29 @@ class AwsBatchTaskHandler extends TaskHandler implements BatchHandler<String,Job
883883
final key = entry.getKey()
884884
final value = entry.getValue()
885885

886-
if (key != null && value != null) {
887-
final sanitizedKey = sanitizeAwsBatchLabel(key.toString(), 128)
888-
final sanitizedValue = sanitizeAwsBatchLabel(value.toString(), 256)
889-
890-
// Only add non-empty keys and values
891-
if (sanitizedKey && sanitizedValue) {
892-
result.put(sanitizedKey, sanitizedValue)
893-
}
886+
// Handle null keys or values
887+
if (key == null || value == null) {
888+
log.warn "AWS Batch label dropped due to null ${key == null ? 'key' : 'value'}: key=${key}, value=${value}"
889+
continue
894890
}
891+
892+
final originalKey = key.toString()
893+
final originalValue = value.toString()
894+
final sanitizedKey = sanitizeAwsBatchLabel(originalKey, 128)
895+
final sanitizedValue = sanitizeAwsBatchLabel(originalValue, 256)
896+
897+
// Check if sanitization resulted in empty strings
898+
if (!sanitizedKey || !sanitizedValue) {
899+
log.warn "AWS Batch label dropped after sanitization - key: '${originalKey}' -> '${sanitizedKey ?: ''}', value: '${originalValue}' -> '${sanitizedValue ?: ''}'"
900+
continue
901+
}
902+
903+
// Log if values were modified during sanitization
904+
if (sanitizedKey != originalKey || sanitizedValue != originalValue) {
905+
log.warn "AWS Batch label sanitized - key: '${originalKey}' -> '${sanitizedKey}', value: '${originalValue}' -> '${sanitizedValue}'"
906+
}
907+
908+
result.put(sanitizedKey, sanitizedValue)
895909
}
896910

897911
return result
@@ -908,24 +922,19 @@ class AwsBatchTaskHandler extends TaskHandler implements BatchHandler<String,Job
908922
protected String sanitizeAwsBatchLabel(String input, int maxLength) {
909923
if (!input) return input
910924

911-
// Replace invalid characters with underscores
925+
// Replace invalid characters and clean up the string
912926
// AWS Batch allows: letters, numbers, spaces, and: _ . : / = + - @
913-
String sanitized = input.replaceAll(/[^a-zA-Z0-9\s_.\:\/=+\-@]/, '_')
914-
915-
// Replace multiple consecutive underscores/spaces with single underscore
916-
sanitized = sanitized.replaceAll(/[_\s]{2,}/, '_')
927+
final sanitized = input
928+
.replaceAll(/[^a-zA-Z0-9\s_.\:\/=+\-@]/, '_') // Replace invalid chars with underscores
929+
.replaceAll(/[_\s]{2,}/, '_') // Replace multiple consecutive underscores/spaces
930+
.replaceAll(/^[_\s]+|[_\s]+$/, '') // Remove leading/trailing underscores and spaces
917931

918-
// Remove leading/trailing underscores and spaces
919-
sanitized = sanitized.replaceAll(/^[_\s]+|[_\s]+$/, '')
920-
921-
// Truncate if too long
922-
if (sanitized.length() > maxLength) {
923-
sanitized = sanitized.substring(0, maxLength)
924-
// Remove trailing underscore/space after truncation
925-
sanitized = sanitized.replaceAll(/[_\s]+$/, '')
926-
}
932+
// Truncate if necessary and clean up any trailing underscores/spaces
933+
final result = sanitized.size() > maxLength
934+
? sanitized.substring(0, maxLength).replaceAll(/[_\s]+$/, '')
935+
: sanitized
927936

928-
return sanitized ?: null
937+
return result ?: null
929938
}
930939

931940
/**

0 commit comments

Comments
 (0)