Skip to content

fix: handle LinkageError in createCredentials and updateSubmit for invalid certificate credentials - #1060

Merged
timja merged 1 commit into
jenkinsci:masterfrom
lohitkolluri:fix/971-handle-certificate-errors
Jul 3, 2026
Merged

timja merged 1 commit into
jenkinsci:masterfrom
lohitkolluri:fix/971-handle-certificate-errors

Conversation

@lohitkolluri

@lohitkolluri lohitkolluri commented Jun 20, 2026 •

Copy link
Copy Markdown
Contributor

Closes #971

Root Cause

When adding or updating CertificateCredentialsImpl with invalid PEM data, the constructor throws IllegalArgumentException. Stapler's Descriptor.bindJSON wraps this in a LinkageError, which was unhandled in DomainWrapper.doCreateCredentials() and CredentialsWrapper.doUpdateSubmit(), resulting in a raw stack trace instead of a user-facing error message.

Fix

Added LinkageError catch blocks to both methods, extracting the root cause via ExceptionUtils.getRootCause and returning a JSON error response for application/json requests. This matches the exact pattern established in PR #580 for the popup add-credentials path.

Testing

  • New test: createCredentialsWithInvalidPEMCertificateReturnsJsonError β€” POSTs invalid PEM CertificateCredentialsImpl JSON to /credentials/store/system/domain/_/createCredentials and asserts 200 response with notificationType: ERROR
  • All existing tests pass

Risks

  • Minimal. The catch block only handles LinkageError where root cause is IOException, IllegalArgumentException, or GeneralSecurityException. All other LinkageErrors are rethrown.
  • Non-JSON requests redirect back on error (preserving pre-existing behavior for HTML form submissions).

DCO

Signed-off-by: Lohit Kolluri lohitkolluri@gmail.com

@lohitkolluri
lohitkolluri requested a review from a team as a code owner June 20, 2026 16:50
@lohitkolluri

Copy link
Copy Markdown
Contributor Author

Pushed a follow-up commit that resolves the TODO I noticed in doCreateCredentials() β€” when addCredentials returns false, it could either mean the credential ID already exists or the domain got deleted in the meantime. Now it checks whether the domain still exists and gives a different error message for each case.]

@lohitkolluri

Copy link
Copy Markdown
Contributor Author

@timja Hey just following up on the status of this PR. Thank You!!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request improves user-facing error handling for invalid certificate credentials created/updated via CredentialsStoreAction endpoints by catching Stapler-wrapped LinkageError and returning JSON error responses (matching the established pattern used for the add-credentials popup flow).

Changes:

  • Catch LinkageError in doCreateCredentials and doUpdateSubmit, unwrap root causes, and return JSON notificationType: ERROR for Accept: application/json requests.
  • Add logging for these failures to avoid raw/unhandled stack traces.
  • Add a new test covering invalid PEM certificate creation via the /createCredentials JSON form endpoint.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/main/java/com/cloudbees/plugins/credentials/CredentialsStoreAction.java Adds LinkageError handling + root-cause extraction to return JSON errors for invalid credential submissions.
src/test/java/com/cloudbees/plugins/credentials/CredentialsStoreActionTest.java Adds a regression test asserting JSON error response when creating invalid PEM certificate credentials.

πŸ’‘ Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@lohitkolluri
lohitkolluri force-pushed the fix/971-handle-certificate-errors branch from 7bf1669 to f621a95 Compare July 2, 2026 08:16

@timja timja left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems ok

@timja timja added the bug label Jul 2, 2026
@timja
timja enabled auto-merge (squash) July 2, 2026 08:17
auto-merge was automatically disabled July 2, 2026 08:19

Head branch was pushed to by a user without write access

@lohitkolluri
lohitkolluri force-pushed the fix/971-handle-certificate-errors branch from f621a95 to 40f8e94 Compare July 2, 2026 08:19
@timja
timja enabled auto-merge (squash) July 2, 2026 08:19
Invalid PEM data in CertificateCredentialsImpl makes Stapler's
Descriptor.bindJSON wrap the constructor failure in a LinkageError.
DomainWrapper.doCreateCredentials and CredentialsWrapper.doUpdateSubmit
did not catch it, so JSON clients got a 500 stack trace instead of a
user-facing error.

Catch LinkageError in both handlers, unwrap root causes
(IOException, IllegalArgumentException, GeneralSecurityException), and
return notificationType ERROR for application/json requests. Same
pattern as the popup add-credentials path in CredentialsStoreAction.

When addCredentials returns false, check whether the domain still
exists before reporting a duplicate credential ID.

Signed-off-by: Lohit Kolluri <lohitkolluri@gmail.com>
auto-merge was automatically disabled July 2, 2026 08:21

Head branch was pushed to by a user without write access

@lohitkolluri
lohitkolluri force-pushed the fix/971-handle-certificate-errors branch from 40f8e94 to 7be6285 Compare July 2, 2026 08:21
@lohitkolluri

Copy link
Copy Markdown
Contributor Author

Hey! It looks like auto-merge was disabled after I updated the branch with the latest changes.

@timja
timja merged commit 948bbb7 into jenkinsci:master Jul 3, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[JENKINS-74964] Error messages are not shown when adding invalid certificate credentials

3 participants