Fix SSH Agent plugin markup for empty list of users

There are cases when you want SSH Agent plugin to just start and stop
an SSH agent during the build, without adding any keys automatically.
One of such cases is when you want to use an SSH key stored on a
SmartCard -like device, such as NitroKey HSM.

So far, when the "users" property was set to an empty list then the
output XML markup didn't contain the <credentialIds/> tag and the plugin
was then crashing with NullPointerException.

Also, when the "users" property was set to a list with just one value
then the output XML markup was always generated in the "old style"
format (matching plugin versions < 1.5, so like almost 8 years old).

With this change, when using the "users" property the markup is
generated based on the actual plugin version installed in Jenkins. More
importantly, the generated markup is now properly handled by the plugin,
no matter if the input is an empty list or a list with one or more
entries.

Finally, fixed the parent of the <ignoreMissing> tag, which was wrongly
put under <credentialIds> tag when generating the "new style" markup
(1.5+), while it should simply be a top-level tag, laying next to
<credentialIds>, as in the corresponding implementation class [1].
This means the "ignore-missing-credentials" property was broken when the
"users" property was set to a list with more than one entry and it's now
fixed.

[1] https://github.com/jenkinsci/ssh-agent-plugin/blob/ssh-agent-1.5/src/main/java/com/cloudbees/jenkins/plugins/sshagent/SSHAgentBuildWrapper.java#L83

Change-Id: Ife5a08739da9ea1130f0ea7daa08c16675f6c75d
This commit is contained in:
Adam Romanek 2022-02-10 15:56:37 +01:00
parent 1590466de9
commit 23e6f39287
8 changed files with 41 additions and 9 deletions

View File

@ -2035,10 +2035,6 @@ def ssh_agent_credentials(registry, xml_parent, data):
.. literalinclude::
/../../tests/wrappers/fixtures/ssh-agent-credentials003.yaml
The **users** with one value in list equals to the **user**. In this
case old style XML will be generated. Use this format if you use
SSH-Agent plugin < 1.5.
Example:
.. literalinclude::
@ -2053,16 +2049,22 @@ def ssh_agent_credentials(registry, xml_parent, data):
logger = logging.getLogger(__name__)
plugin_info = registry.get_plugin_info("SSH Agent Plugin")
plugin_ver = pkg_resources.parse_version(
plugin_info.get("version", str(sys.maxsize))
)
entry_xml = XML.SubElement(
xml_parent, "com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper"
)
user_parent_entry_xml = entry_xml
xml_key = "user"
user_list = list()
if "users" in data:
user_list += data["users"]
if len(user_list) > 1:
entry_xml = XML.SubElement(entry_xml, "credentialIds")
if plugin_ver >= pkg_resources.parse_version("1.5.0"):
user_parent_entry_xml = XML.SubElement(entry_xml, "credentialIds")
xml_key = "string"
if "user" in data:
logger.warning(
@ -2081,7 +2083,7 @@ def ssh_agent_credentials(registry, xml_parent, data):
)
for user in user_list:
XML.SubElement(entry_xml, xml_key).text = user
XML.SubElement(user_parent_entry_xml, xml_key).text = user
mapping = [("ignore-missing-credentials", "ignoreMissing", False)]
helpers.convert_mapping_to_xml(entry_xml, data, mapping, fail_required=False)

View File

@ -6,8 +6,8 @@
<string>44747833-247a-407a-a98f-a5a2d785111c</string>
<string>f1c0f777-7ac6-43fd-b5c7-68b420aa1392</string>
<string>dd647a01-be21-402b-bfc5-a4e89be7d0c4</string>
<ignoreMissing>false</ignoreMissing>
</credentialIds>
<ignoreMissing>false</ignoreMissing>
</com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper>
</buildWrappers>
</project>

View File

@ -5,8 +5,8 @@
<credentialIds>
<string>44747833-247a-407a-a98f-a5a2d785111c</string>
<string>dd647a01-be21-402b-bfc5-a4e89be7d0c4</string>
<ignoreMissing>false</ignoreMissing>
</credentialIds>
<ignoreMissing>false</ignoreMissing>
</com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper>
</buildWrappers>
</project>

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<buildWrappers>
<com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper>
<credentialIds>
<string>49d20745-9889-4c02-b286-fc6fb89c36bd</string>
</credentialIds>
<ignoreMissing>false</ignoreMissing>
</com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper>
</buildWrappers>
</project>

View File

@ -0,0 +1,4 @@
wrappers:
- ssh-agent-credentials:
users:
- '49d20745-9889-4c02-b286-fc6fb89c36bd'

View File

@ -0,0 +1,3 @@
- longName: 'SSH Agent Plugin'
shortName: 'ssh-agent'
version: "1.4"

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<buildWrappers>
<com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper>
<credentialIds/>
<ignoreMissing>false</ignoreMissing>
</com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper>
</buildWrappers>
</project>

View File

@ -0,0 +1,3 @@
wrappers:
- ssh-agent-credentials:
users: []