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

Remove ssh credentials from support plugin #862

Merged
merged 3 commits into from
Apr 29, 2019
Merged
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
2 changes: 1 addition & 1 deletion integrations/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>ssh-credentials</artifactId>
<version>1.13</version>
<version>1.16</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@
import com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import hudson.ExtensionList;
import hudson.util.Secret;
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import io.jenkins.plugins.casc.model.CNode;
import io.jenkins.plugins.casc.model.Mapping;
import io.jenkins.plugins.casc.support.credentials.CredentialsRootConfigurator;
import java.util.Collections;
import java.util.List;
import jenkins.model.Jenkins;
import org.junit.Rule;
import org.junit.Test;

import static java.util.Objects.requireNonNull;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

public class CredentialsTest {
Expand All @@ -31,9 +39,14 @@ public void testGlobalScopedCredentials() {

List<BasicSSHUserPrivateKey> creds2 = CredentialsProvider.lookupCredentials(BasicSSHUserPrivateKey.class,Jenkins.getInstanceOrNull(), null, Collections.emptyList());
assertThat(creds2.size(), is(1));
assertEquals("agentuser", creds2.get(0).getUsername());
assertEquals("password", creds2.get(0).getPassphrase().getPlainText());
assertEquals("ssh private key used to connect ssh slaves", creds2.get(0).getDescription());
BasicSSHUserPrivateKey basicSSHUserPrivateKey = creds2.get(0);
assertEquals("agentuser", basicSSHUserPrivateKey.getUsername());
assertEquals("password", basicSSHUserPrivateKey.getPassphrase().getPlainText());
assertEquals("ssh private key used to connect ssh slaves", basicSSHUserPrivateKey.getDescription());
assertThat(basicSSHUserPrivateKey.getPrivateKeySource().getPrivateKeys().size(), is(1));
String directKey = basicSSHUserPrivateKey.getPrivateKeySource().getPrivateKeys().get(0);
assertThat(directKey, is("sp0ds9d+skkfjf"));

}


Expand All @@ -47,4 +60,43 @@ public void testDomainScopedCredentials() {
assertEquals("secret", creds.get(0).getPassword().getPlainText());
}

@ConfiguredWithCode("GlobalCredentials.yml")
@Test
public void testExportSSHCredentials() throws Exception {
ConfiguratorRegistry registry = ConfiguratorRegistry.get();
ConfigurationContext context = new ConfigurationContext(registry);
CredentialsRootConfigurator root = ExtensionList.lookupSingleton(CredentialsRootConfigurator.class);

CNode node = root.describe(root.getTargetComponent(context), context);
assertNotNull(node);
final Mapping mapping = node.asMapping();

Mapping sshCredential = mapping.get("system")
.asMapping()
.get("domainCredentials")
.asSequence().get(0)
.asMapping().get("credentials")
.asSequence().get(1)
.asMapping().get("basicSSHUserPrivateKey").asMapping();

assertThat(sshCredential.getScalarValue("scope"), is("SYSTEM"));
assertThat(sshCredential.getScalarValue("id"), is("agent-private-key"));
assertThat(sshCredential.getScalarValue("username"), is("agentuser"));

String passphrase = sshCredential.getScalarValue("passphrase");
assertThat(passphrase, not("password"));
assertThat(requireNonNull(Secret.decrypt(passphrase)).getPlainText(), is("password"));

String sshKeyExported = sshCredential.get("privateKeySource")
.asMapping()
.get("directEntry")
.asMapping()
.get("privateKey")
.asScalar()
.getValue();

assertThat(sshKeyExported, not("sp0ds9d+skkfjf"));
assertThat(requireNonNull(Secret.decrypt(sshKeyExported)).getPlainText(), is("sp0ds9d+skkfjf"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,13 @@ public void configure_system_credentials() throws Exception {
List<BasicSSHUserPrivateKey> sshPrivateKeys = CredentialsProvider.lookupCredentials(
BasicSSHUserPrivateKey.class, jenkins, ACL.SYSTEM, Collections.emptyList()
);
assertThat(sshPrivateKeys, hasSize(2));
final BasicSSHUserPrivateKey ssh_with_passphrase = sshPrivateKeys.stream()
.filter(k -> k.getId().equals("ssh_with_passphrase_provided"))
.findFirst().orElseThrow(AssertionError::new);
assertThat(sshPrivateKeys, hasSize(1));

final BasicSSHUserPrivateKey ssh_with_passphrase = sshPrivateKeys.get(0);
assertThat(ssh_with_passphrase.getPassphrase().getPlainText(), equalTo("ABCD"));

final DirectEntryPrivateKeySource source = (DirectEntryPrivateKeySource) ssh_with_passphrase.getPrivateKeySource();
assertThat(source.getPrivateKey(), equalTo("s3cr3t"));
assertThat(source.getPrivateKey().getPlainText(), equalTo("s3cr3t"));
Copy link
Member

@jvz jvz Apr 26, 2019

Choose a reason for hiding this comment

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

Not a blocker by any means, but it could be useful to create a matcher for secrets so you'd be able to assert something like assertThat(source.getPrivateKey(), hasPlainText("s3cr3t")); which would read better. Most appropriate place to add it would be here.

Edit: I submitted a PR for that idea.



// credentials should not appear in plain text in log
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ credentials:
keyStoreSource:
fileOnMaster:
keyStoreFile: /docker/secret/id_rsa
- basicSSHUserPrivateKey:
scope: SYSTEM
id: ssh_with_passphrase
username: ssh_root
passphrase: ${SSH_KEY_PASSWORD}
description: "SSH passphrase with private key file"
privateKeySource:
FileOnMasterPrivateKeySource:
privateKeyFile: /docker/secret/id_rsa_2
- basicSSHUserPrivateKey:
scope: SYSTEM
id: ssh_with_passphrase_provided
Expand Down
7 changes: 0 additions & 7 deletions support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@
<optional>true</optional>
</dependency>

<dependency>
Copy link
Member

Choose a reason for hiding this comment

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

have you verified / is there a test that shows that the private key isn't exported in plain text?

Copy link
Member Author

Choose a reason for hiding this comment

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

YES 😆

Arguably this should be updated to check the direct key

List<BasicSSHUserPrivateKey> creds2 = CredentialsProvider.lookupCredentials(BasicSSHUserPrivateKey.class,Jenkins.getInstanceOrNull(), null, Collections.emptyList());
assertThat(creds2.size(), is(1));
assertEquals("agentuser", creds2.get(0).getUsername());
assertEquals("password", creds2.get(0).getPassphrase().getPlainText());
assertEquals("ssh private key used to connect ssh slaves", creds2.get(0).getDescription());

privateKeySource:
directEntry:
privateKey: sp0ds9d+skkfjf

Copy link
Member Author

Choose a reason for hiding this comment

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

apparently not ready, see CI... the test ran locally 😕

Copy link
Member Author

@jetersen jetersen Apr 25, 2019

Choose a reason for hiding this comment

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

@jvz we need some more magic it seems!
We just needed to fix our tests

Copy link
Member

Choose a reason for hiding this comment

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

It really needs to be, as it was a security issue, and we don't want it accidentally re-introduced

Copy link
Member Author

Choose a reason for hiding this comment

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

it works locally, I don't get it 😕

Copy link
Member

Choose a reason for hiding this comment

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

I'm confused why its trying to use this:

Caused by: java.lang.IllegalArgumentException: No com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$PrivateKeySource implementation found for FileOnMasterPrivateKeySource
Possible solution: Try to install 'configuration-as-code-support' plugin

it should be directEntry not the fileOnMaster...

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed in: 202ab7b

<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>ssh-credentials</artifactId>
<version>1.13</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>job-dsl</artifactId>
Expand Down

This file was deleted.