Skip to content

[JENKINS-74964] Display error message when adding invalid certificate credentials - #580

Merged
fcojfernandez merged 9 commits into
jenkinsci:masterfrom
Priya-CB:JENKINS-74964
Dec 20, 2024
Merged

fcojfernandez merged 9 commits into
jenkinsci:masterfrom
Priya-CB:JENKINS-74964

Conversation

@Priya-CB

@Priya-CB Priya-CB commented Dec 6, 2024 •

Copy link
Copy Markdown
Contributor

JENKINS-74964

  • The "Add Credentials" popup now correctly displays an error message when users attempt to add invalid certificate credentials. Previously, the validation errors were shown on the form but no error message appeared upon clicking "Add," leading to a silent rejection of the credentials.

Testing done

Automated Unit Testing

Implemented unit tests to verify that the error messages are returned in the response on form submit

Manual Testing

  1. Adding certificate credentials with invalid inputs (e.g., empty PEM-encoded certificates or password < 14 characters in FIPS mode).
  2. Verified that clicking "Add" prevents invalid credentials from being stored and displays an appropriate error message to the user.

Popup screen:

Screenshot 2024-12-06 at 9 19 12 PM Screenshot 2024-12-06 at 9 14 04 PM Screenshot 2024-12-06 at 9 23 39 PM Screenshot 2024-12-06 at 8 19 14 PM

Submitter checklist

  • Make sure you are opening from a topic/feature/bugfix branch (right side) and not your main branch!
  • Ensure that the pull request title represents the desired changelog entry
  • Please describe what you did
  • Link to relevant issues in GitHub or Jira
  • Link to relevant pull requests, esp. upstream and downstream changes
  • Ensure you have provided tests - that demonstrates feature works or fixes the issue

@Priya-CB
Priya-CB marked this pull request as ready for review December 6, 2024 18:57
@Priya-CB
Priya-CB requested a review from a team as a code owner December 6, 2024 18:57
.catch((e) => {
// notificationBar.show(...) with logging ID could be handy here?
console.error("Could not add credentials:", e);
window.notificationBar.show("Credentials creation failed.", window.notificationBar["ERROR"]);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

"Add Credentials" popup does not handle HTML responses ("angry Jenkins"), causing the catch block to execute. Added notificationBar to inform the user about the failure(unexpected cases) in credential creation.

@jtnord
jtnord requested a review from a team December 10, 2024 11:54
Comment on lines +626 to +627
* Descriptor#newInstanceImpl throws a LinkageError if the constructor throws any exception other
* than HTTP response exceptions such as Descriptor.FormException.

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.

AFAIK FormException is not a RuntimeException, and only `RuntimeException are not causing a LinkageError: https://github.com/jenkinsci/jenkins/blob/171097618b5b2b70ec3142e3efc80b4f2aff9601/core/src/main/java/hudson/model/Descriptor.java#L623 so

Suggested change
* Descriptor#newInstanceImpl throws a LinkageError if the constructor throws any exception other
* than HTTP response exceptions such as Descriptor.FormException.
* Descriptor#newInstanceImpl throws a LinkageError if the DataBoundConstructor or any DataBoundSetter throw any exception other
* than RuntimeException implementing HttpResponse.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

While FormException itself is not a RuntimeException, it is wrapped into HttpResponses, which is a RuntimeException,
https://github.com/jenkinsci/stapler/blob/master/core/src/main/java/org/kohsuke/stapler/RequestImpl.java#L691

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.

no, that's incorrect. As for https://github.com/jenkinsci/jenkins/blob/171097618b5b2b70ec3142e3efc80b4f2aff9601/core/src/main/java/hudson/model/Descriptor.java#L622-L627 the Linkage error only can happen if the caught exception is RuntimeException AND HttpResponse. FormException is not in the catch clause, so it's not caught at all but directly thrown as stated in the method signature. The only way of getting a LinkageError is that the error is a NoSuchMethodException, an InstantiationException, an IllegalAccessException, an InvocationTargetException, or a RuntimeException that is not implementing HttpResponse

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You are correct that FormException is not explicitly in the catch clause of Descriptor#newInstanceImpl. However, my point was that FormException, when thrown, is wrapped into HttpResponses and HttpResponses is a RuntimeException.
Because of this wrapping, it ultimately satisfies the condition of being a RuntimeException and implementing HttpResponse

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated the comment

@jmdesprez jmdesprez 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.

I agree with previous reviews/comments

@fcojfernandez
fcojfernandez merged commit bcda74a into jenkinsci:master Dec 20, 2024
lohitkolluri added a commit to lohitkolluri/credentials-plugin that referenced this pull request Jul 2, 2026
When adding or updating CertificateCredentialsImpl with invalid PEM data,
Stapler's Descriptor.bindJSON wraps the constructor exception in a
LinkageError, causing an unhandled stack trace instead of a user-facing
error message.

Added LinkageError catch blocks to DomainWrapper.doCreateCredentials()
and CredentialsWrapper.doUpdateSubmit(), extracting the root cause
(IOException | IllegalArgumentException | GeneralSecurityException) and
returning a JSON error response for json requests, matching the pattern
established in PR jenkinsci#580 for the popup add-credentials path.

When addCredentials returns false, check whether the domain still exists
before assuming the credential ID is a duplicate. If the domain was
removed between page load and form submit, the error now says
'Domain X does not exist' instead of the misleading 'Credentials with
specified ID already exist'.

Test validates that creating credentials with invalid PEM returns a
JSON error (notificationType=ERROR, not a 500).

Closes jenkinsci#971

Signed-off-by: Lohit Kolluri <lohitkolluri@gmail.com>
lohitkolluri added a commit to lohitkolluri/credentials-plugin that referenced this pull request Jul 2, 2026
When adding or updating CertificateCredentialsImpl with invalid PEM data,
Stapler's Descriptor.bindJSON wraps the constructor exception in a
LinkageError, causing an unhandled stack trace instead of a user-facing
error message.

Added LinkageError catch blocks to DomainWrapper.doCreateCredentials()
and CredentialsWrapper.doUpdateSubmit(), extracting the root cause
(IOException | IllegalArgumentException | GeneralSecurityException) and
returning a JSON error response for json requests, matching the pattern
established in PR jenkinsci#580 for the popup add-credentials path.

When addCredentials returns false, check whether the domain still exists
before assuming the credential ID is a duplicate. If the domain was
removed between page load and form submit, the error now says
'Domain X does not exist' instead of the misleading 'Credentials with
specified ID already exist'.

Test validates that creating credentials with invalid PEM returns a
JSON error (notificationType=ERROR, not a 500).

Closes jenkinsci#971

Signed-off-by: Lohit Kolluri <lohitkolluri@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants