Fix: Enhance robustness of file-based Base64 decoding in credential migration scripts #244
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem Addressed: java.lang.IllegalArgumentException: Illegal base64 character when reading encoded data from file
This PR addresses a common issue encountered when users attempt to load the encoded credentials data from a file (e.g., /home/jenkins/system_credentials.txt), as suggested by the comments in the credential migration scripts.
The Base64.getDecoder().decode() method expects a pure Base64 string. However, when text content is read from a file using new File(...).text, it often includes trailing whitespace or newline characters (e.g., \n, \r\n) that are automatically appended by text editors or file systems. These non-Base64 characters, even if invisible, cause the java.lang.IllegalArgumentException: Illegal base64 character a (or similar, depending on the character) during the decoding process, leading to script failure.
Solution Implemented: Trim whitespace from file content
To prevent this, I've added a .trim() call to the string read from the file in both relevant credential migration scripts. This ensures that any leading or trailing whitespace, including newline characters, is removed before the Base64 decoding attempt.
Testing:
I have tested this change by creating a system_credentials.txt file containing a valid Base64 string followed by a newline character. Before this change, both scripts would fail with the IllegalArgumentException. With the .trim() addition, both scripts successfully decode the content and proceed as expected.