Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-66465] Support implementations of SSHUserPrivateKey for Wind… #282

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import static com.google.jenkins.plugins.computeengine.ComputeEngineCloud.checkPermissions;

import com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey;
import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
Expand Down Expand Up @@ -98,7 +98,7 @@ public StandardUsernameCredentials getPrivateKeyCredentials() {
}
return CredentialsMatchers.firstOrNull(
new SystemCredentialsProvider.ProviderImpl()
.getCredentials(BasicSSHUserPrivateKey.class, Jenkins.get(), ACL.SYSTEM),
.getCredentials(SSHUserPrivateKey.class, Jenkins.get(), ACL.SYSTEM),
CredentialsMatchers.withId(privateKeyCredentialsId));
}

Expand All @@ -116,14 +116,13 @@ public ListBoxModel doFillPasswordCredentialsIdItems(@AncestorInPath Jenkins con
return new StandardListBoxModel();
}
return new StandardListBoxModel()
.withEmptySelection()
.withMatching(
CredentialsMatchers.instanceOf(StandardUsernamePasswordCredentials.class),
CredentialsProvider.lookupCredentials(
StandardUsernamePasswordCredentials.class,
context,
ACL.SYSTEM,
new ArrayList<>()));
.includeEmptyValue()
.includeMatchingAs(
ACL.SYSTEM,
context,
StandardUsernamePasswordCredentials.class,
new ArrayList<>(),
CredentialsMatchers.instanceOf(StandardUsernamePasswordCredentials.class));
}

public ListBoxModel doFillPrivateKeyCredentialsIdItems(@AncestorInPath Jenkins context) {
Expand All @@ -132,11 +131,13 @@ public ListBoxModel doFillPrivateKeyCredentialsIdItems(@AncestorInPath Jenkins c
return new StandardUsernameListBoxModel();
}
return new StandardUsernameListBoxModel()
.withEmptySelection()
.withMatching(
CredentialsMatchers.instanceOf(BasicSSHUserPrivateKey.class),
CredentialsProvider.lookupCredentials(
StandardUsernameCredentials.class, context, ACL.SYSTEM, new ArrayList<>()));
.includeEmptyValue()
.includeMatchingAs(
ACL.SYSTEM,
context,
SSHUserPrivateKey.class,
new ArrayList<>(),
CredentialsMatchers.instanceOf(SSHUserPrivateKey.class));
}

public FormValidation doCheckPrivateKeyCredentialsId(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@
wjWcyy7pIUfBrtiZCuojGGTdNBhvG20ohMp/lfs52Hg=<br/>
-----END RSA PRIVATE KEY-----<br/>
</code>

Note: Other implementations of <a href="https://javadoc.jenkins.io/plugin/ssh-credentials/com/cloudbees/jenkins/plugins/sshcredentials/SSHUserPrivateKey.html">SSHUserPrivateKey</a>. And the same rules about the empty passphrase and the private key format apply.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package com.google.jenkins.plugins.computeengine;

import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
import com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey;
import com.cloudbees.plugins.credentials.CredentialsDescriptor;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.util.ListBoxModel;
import hudson.util.Secret;
import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.util.Collections;
import java.util.List;

public class WindowsConfigurationTest {

@Rule public JenkinsRule r = new JenkinsRule();

@Test
public void testHttpCredentials() {
WindowsConfiguration.DescriptorImpl descriptor =
r.jenkins.getDescriptorByType(WindowsConfiguration.DescriptorImpl.class);
Assert.assertNotNull(descriptor);
ListBoxModel m = descriptor.doFillPasswordCredentialsIdItems(r.jenkins);
// Ensure empty value is added
MatcherAssert.assertThat(m.size(), CoreMatchers.is(1));
SystemCredentialsProvider.getInstance()
.getCredentials()
.add(
new UsernamePasswordCredentialsImpl(
CredentialsScope.SYSTEM, "system_id", "system_ak", "system_sk", "system_desc"));
// Ensure added SYSTEM credential is displayed
m = descriptor.doFillPasswordCredentialsIdItems(r.jenkins);
MatcherAssert.assertThat(m.size(), CoreMatchers.is(2));
// Ensure added GLOBAL credential is displayed
SystemCredentialsProvider.getInstance()
.getCredentials()
.add(
new UsernamePasswordCredentialsImpl(
CredentialsScope.GLOBAL, "global_id", "global_ak", "global_sk", "global_desc"));
m = descriptor.doFillPasswordCredentialsIdItems(r.jenkins);
MatcherAssert.assertThat(m.size(), CoreMatchers.is(3));
}

@Test
public void testSshCredentials() {
WindowsConfiguration.DescriptorImpl descriptor =
r.jenkins.getDescriptorByType(WindowsConfiguration.DescriptorImpl.class);
Assert.assertNotNull(descriptor);
ListBoxModel m = descriptor.doFillPrivateKeyCredentialsIdItems(r.jenkins);
// Ensure empty value is added
MatcherAssert.assertThat(m.size(), CoreMatchers.is(1));
SystemCredentialsProvider.getInstance()
.getCredentials()
.add(
new BasicSSHUserPrivateKey(
CredentialsScope.SYSTEM,
"shi",
"key",
new BasicSSHUserPrivateKey.DirectEntryPrivateKeySource("somekey"),
"",
""));
// Ensure added SYSTEM credential is displayed
m = descriptor.doFillPrivateKeyCredentialsIdItems(r.jenkins);
MatcherAssert.assertThat(m.size(), CoreMatchers.is(2));
// Ensure added GLOBAL credential is displayed
SystemCredentialsProvider.getInstance()
.getCredentials()
.add(
new BasicSSHUserPrivateKey(
CredentialsScope.SYSTEM,
"ghi",
"key",
new BasicSSHUserPrivateKey.DirectEntryPrivateKeySource("somekey"),
"",
""));
m = descriptor.doFillPrivateKeyCredentialsIdItems(r.jenkins);
MatcherAssert.assertThat(m.size(), CoreMatchers.is(3));

// Ensure other implementations of SSHUserPrivateKey are selectable
SystemCredentialsProvider.getInstance()
.getCredentials()
.add(new SSHUserPrivateKey() {
@NonNull
@Override
public String getPrivateKey() {
return "somekey";
}

@Override
public Secret getPassphrase() {
return null;
}

@NonNull
@Override
public List<String> getPrivateKeys() {
return Collections.emptyList();
}

@NonNull
@Override
public String getDescription() {
return "custom-ssk-key";
}

@NonNull
@Override
public String getId() {
return "custom-ssk-key";
}

@NonNull
@Override
public String getUsername() {
return "test";
}

@Override
public CredentialsScope getScope() {
return CredentialsScope.GLOBAL;
}

@NonNull
@Override
public CredentialsDescriptor getDescriptor() {
return null;
}
});
m = descriptor.doFillPrivateKeyCredentialsIdItems(r.jenkins);
MatcherAssert.assertThat(m.size(), CoreMatchers.is(4));
}
}
Loading