Search in sources :

Example 1 with CertificateCredentials

use of com.cloudbees.plugins.credentials.common.CertificateCredentials in project configuration-as-code-plugin by jenkinsci.

the class SystemCredentialsTest method configure_system_credentials.

@Test
@ConfiguredWithCode("SystemCredentialsTest.yml")
public void configure_system_credentials() throws Exception {
    Jenkins jenkins = Jenkins.get();
    List<UsernamePasswordCredentials> ups = CredentialsProvider.lookupCredentials(UsernamePasswordCredentials.class, jenkins, ACL.SYSTEM, Collections.emptyList());
    assertThat(ups, hasSize(1));
    final UsernamePasswordCredentials up = ups.get(0);
    assertThat(up.getPassword().getPlainText(), equalTo("1234"));
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    final ConfigurationContext context = new ConfigurationContext(registry);
    final CNode node = context.lookup(up.getClass()).describe(up, context);
    assertThat(node.asMapping().getScalarValue("password"), not(equals("1234")));
    List<CertificateCredentials> certs = CredentialsProvider.lookupCredentials(CertificateCredentials.class, jenkins, ACL.SYSTEM, Collections.emptyList());
    assertThat(certs, hasSize(0));
    // TODO: add test for uploaded certificate
    // assertThat(certs.get(0).getPassword().getPlainText(), equalTo("ABCD"));
    List<BasicSSHUserPrivateKey> sshPrivateKeys = CredentialsProvider.lookupCredentials(BasicSSHUserPrivateKey.class, jenkins, ACL.SYSTEM, Collections.emptyList());
    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().getPlainText(), equalTo("s3cr3t"));
    // credentials should not appear in plain text in log
    for (LogRecord logRecord : log.getRecords()) {
        assertThat(logRecord.getMessage(), not(containsString("1234")));
        assertThat(logRecord.getMessage(), not(containsString("ABCD")));
    }
}
Also used : Jenkins(jenkins.model.Jenkins) CNode(io.jenkins.plugins.casc.model.CNode) LogRecord(java.util.logging.LogRecord) CertificateCredentials(com.cloudbees.plugins.credentials.common.CertificateCredentials) DirectEntryPrivateKeySource(com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey.DirectEntryPrivateKeySource) BasicSSHUserPrivateKey(com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey) UsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials) Test(org.junit.Test) ConfiguredWithCode(io.jenkins.plugins.casc.misc.ConfiguredWithCode)

Example 2 with CertificateCredentials

use of com.cloudbees.plugins.credentials.common.CertificateCredentials in project configuration-as-code-plugin by jenkinsci.

the class SystemCredentialsTest method configure_system_credentials.

@Test
@ConfiguredWithCode("SystemCredentialsTest.yml")
public void configure_system_credentials() throws Exception {
    Jenkins jenkins = Jenkins.getInstance();
    List<UsernamePasswordCredentials> ups = CredentialsProvider.lookupCredentials(UsernamePasswordCredentials.class, jenkins, ACL.SYSTEM, Collections.emptyList());
    assertThat(ups, hasSize(1));
    assertThat(ups.get(0).getPassword().getPlainText(), equalTo("1234"));
    List<CertificateCredentials> certs = CredentialsProvider.lookupCredentials(CertificateCredentials.class, jenkins, ACL.SYSTEM, Collections.emptyList());
    assertThat(certs, hasSize(1));
    assertThat(certs.get(0).getPassword().getPlainText(), equalTo("ABCD"));
    List<BasicSSHUserPrivateKey> sshPrivateKeys = CredentialsProvider.lookupCredentials(BasicSSHUserPrivateKey.class, jenkins, ACL.SYSTEM, Collections.emptyList());
    assertThat(sshPrivateKeys, hasSize(2));
    assertThat(sshPrivateKeys.get(0).getPassphrase().getPlainText(), equalTo("ABCD"));
}
Also used : Jenkins(jenkins.model.Jenkins) CertificateCredentials(com.cloudbees.plugins.credentials.common.CertificateCredentials) BasicSSHUserPrivateKey(com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey) UsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials) Test(org.junit.Test) ConfiguredWithCode(org.jenkinsci.plugins.casc.misc.ConfiguredWithCode)

Aggregations

BasicSSHUserPrivateKey (com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey)2 CertificateCredentials (com.cloudbees.plugins.credentials.common.CertificateCredentials)2 UsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials)2 Jenkins (jenkins.model.Jenkins)2 Test (org.junit.Test)2 DirectEntryPrivateKeySource (com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey.DirectEntryPrivateKeySource)1 ConfiguredWithCode (io.jenkins.plugins.casc.misc.ConfiguredWithCode)1 CNode (io.jenkins.plugins.casc.model.CNode)1 LogRecord (java.util.logging.LogRecord)1 ConfiguredWithCode (org.jenkinsci.plugins.casc.misc.ConfiguredWithCode)1