Skip to content

Commit

Permalink
Merge e3b10fd into 35f46b2
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlaverse committed Jul 15, 2018
2 parents 35f46b2 + e3b10fd commit 61dc592
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 29 deletions.
Expand Up @@ -31,7 +31,7 @@ public void addFakeSlave() throws Exception {
@Test
public void testScopedCredentials() throws Exception {
Folder folder = new Folder(r.jenkins.getItemGroup(), "test-folder");
CredentialsProvider.lookupStores(folder).iterator().next().addCredentials(Domain.global(), usernamePasswordCredential());
CredentialsProvider.lookupStores(folder).iterator().next().addCredentials(Domain.global(), usernamePasswordCredential(CREDENTIAL_ID));

WorkflowJob p = folder.createProject(WorkflowJob.class, "testScopedCredentials");
p.setDefinition(new CpsFlowDefinition(loadResource("mockedKubectl.groovy"), true));
Expand All @@ -43,7 +43,7 @@ public void testScopedCredentials() throws Exception {
@Test
public void testMissingScopedCredentials() throws Exception {
Folder folder = new Folder(r.jenkins.getItemGroup(), "test-folder");
CredentialsProvider.lookupStores(folder).iterator().next().addCredentials(Domain.global(), usernamePasswordCredential());
CredentialsProvider.lookupStores(folder).iterator().next().addCredentials(Domain.global(), usernamePasswordCredential(CREDENTIAL_ID));

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testMissingScopedCredentials");
p.setDefinition(new CpsFlowDefinition(loadResource("mockedKubectl.groovy"), true));
Expand All @@ -55,7 +55,7 @@ public void testMissingScopedCredentials() throws Exception {

@Test
public void testSecretWithSpace() throws Exception {
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), secretCredentialWithSpace());
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), secretCredentialWithSpace(CREDENTIAL_ID));

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testSecretWithSpace");
p.setDefinition(new CpsFlowDefinition(loadResource("mockedKubectl.groovy"), true));
Expand All @@ -68,7 +68,7 @@ public void testSecretWithSpace() throws Exception {

@Test
public void testUsernamePasswordWithSpace() throws Exception {
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), usernamePasswordCredentialWithSpace());
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), usernamePasswordCredentialWithSpace(CREDENTIAL_ID));

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testUsernamePasswordWithSpace");
p.setDefinition(new CpsFlowDefinition(loadResource("mockedKubectl.groovy"), true));
Expand All @@ -81,7 +81,7 @@ public void testUsernamePasswordWithSpace() throws Exception {

@Test
public void testKubeConfigDisposed() throws Exception {
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), usernamePasswordCredentialWithSpace());
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), usernamePasswordCredentialWithSpace(CREDENTIAL_ID));

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testUsernamePasswordWithSpace");
p.setDefinition(new CpsFlowDefinition(loadResource("mockedKubectl.groovy"), true));
Expand Down
@@ -1,17 +1,20 @@
package org.jenkinsci.plugins.kubernetes.cli;

import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.domains.Domain;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.util.ListBoxModel;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.File;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
Expand All @@ -30,12 +33,12 @@ public void addFakeSlave() throws Exception {

@Test
public void testEnvVariablePresent() throws Exception {
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), secretCredential());
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), secretCredential(CREDENTIAL_ID));

FreeStyleProject p = r.createFreeStyleProject();

KubectlBuildWrapper bw = new KubectlBuildWrapper();
bw.credentialsId = secretCredential().getId();
bw.credentialsId = CREDENTIAL_ID;
p.getBuildWrappersList().add(bw);


Expand All @@ -47,12 +50,12 @@ public void testEnvVariablePresent() throws Exception {

@Test
public void testKubeConfigDisposed() throws Exception {
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), secretCredential());
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), secretCredential(CREDENTIAL_ID));

FreeStyleProject p = r.createFreeStyleProject();

KubectlBuildWrapper bw = new KubectlBuildWrapper();
bw.credentialsId = secretCredential().getId();
bw.credentialsId = CREDENTIAL_ID;
p.getBuildWrappersList().add(bw);


Expand All @@ -61,4 +64,20 @@ public void testKubeConfigDisposed() throws Exception {
r.assertBuildStatus(Result.SUCCESS, r.waitForCompletion(b));
r.assertLogContains("kubectl configuration cleaned up", b);
}

@Test
public void testListedCredentials() throws Exception {
CredentialsStore store = CredentialsProvider.lookupStores(r.jenkins).iterator().next();
store.addCredentials(Domain.global(), usernamePasswordCredential("1"));
store.addCredentials(Domain.global(), secretCredential("2"));
store.addCredentials(Domain.global(), fileCredential("3"));
store.addCredentials(Domain.global(), certificateCredential("4"));
store.addCredentials(Domain.global(), tokenCredential("5"));

KubectlBuildWrapper.DescriptorImpl d = new KubectlBuildWrapper.DescriptorImpl();
FreeStyleProject p = r.createFreeStyleProject();

ListBoxModel s = d.doFillCredentialsIdItems(p.asItem(), "");
assertEquals(6, s.size());
}
}
Expand Up @@ -32,7 +32,7 @@ public void checkKubectlPresence() throws Exception {
@Test
public void testBasicWithCa() throws Exception {
String encodedCertificate = new String(Base64.getEncoder().encode(CA_CERTIFICATE.getBytes()));
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), usernamePasswordCredential());
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), usernamePasswordCredential(CREDENTIAL_ID));

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testBasicWithCa");
p.setDefinition(new CpsFlowDefinition(loadResource("kubectlWithCa.groovy"), true));
Expand All @@ -50,7 +50,7 @@ public void testBasicWithCa() throws Exception {

@Test
public void testBasicWithoutCa() throws Exception {
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), usernamePasswordCredential());
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), usernamePasswordCredential(CREDENTIAL_ID));

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testBasicWithoutCa");
p.setDefinition(new CpsFlowDefinition(loadResource("kubectlWithoutCa.groovy"), true));
Expand All @@ -68,7 +68,7 @@ public void testBasicWithoutCa() throws Exception {

@Test
public void testUsernamePasswordCredentials() throws Exception {
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), usernamePasswordCredentialWithSpace());
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), usernamePasswordCredentialWithSpace(CREDENTIAL_ID));

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testUsernamePasswordCredentials");
p.setDefinition(new CpsFlowDefinition(loadResource("kubectlWithoutCa.groovy"), true));
Expand All @@ -86,7 +86,7 @@ public void testUsernamePasswordCredentials() throws Exception {

@Test
public void testFileCredentials() throws Exception {
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), fileCredential());
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), fileCredential(CREDENTIAL_ID));

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "fileCredential");
p.setDefinition(new CpsFlowDefinition(loadResource("kubectlWithoutCa.groovy"), true));
Expand All @@ -102,7 +102,7 @@ public void testFileCredentials() throws Exception {

@Test
public void testSecretCredentials() throws Exception {
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), secretCredentialWithSpace());
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), secretCredentialWithSpace(CREDENTIAL_ID));

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testSecretCredentials");
p.setDefinition(new CpsFlowDefinition(loadResource("kubectlWithoutCa.groovy"), true));
Expand All @@ -119,7 +119,7 @@ public void testSecretCredentials() throws Exception {

@Test
public void testCertificateCredentials() throws Exception {
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), certificateCredential());
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), certificateCredential(CREDENTIAL_ID));

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testCertificateCredentials");
p.setDefinition(new CpsFlowDefinition(loadResource("kubectlWithoutCa.groovy"), true));
Expand Down Expand Up @@ -179,7 +179,7 @@ public void testCertificateCredentials() throws Exception {

@Test
public void testKubeConfigPathWithSpace() throws Exception {
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), usernamePasswordCredential());
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), usernamePasswordCredential(CREDENTIAL_ID));

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "kubectl path with spaces");
p.setDefinition(new CpsFlowDefinition(loadResource("kubectlDumpKubeConfigPath.groovy"), true));
Expand Down
Expand Up @@ -8,6 +8,7 @@
import hudson.model.Slave;
import hudson.util.Secret;
import org.apache.commons.compress.utils.IOUtils;
import org.jenkinsci.plugins.kubernetes.credentials.OpenShiftBearerTokenCredentialImpl;
import org.jenkinsci.plugins.plaincredentials.FileCredentials;
import org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl;
import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl;
Expand Down Expand Up @@ -55,7 +56,8 @@ protected Slave getFakeSlave(JenkinsRule r) throws Exception {
if (p.cmds().get(0).equals(KUBECTL_BINARY)) {
String[] maskedCmd = getMaskedCmd(p.cmds(), p.masks());
PrintStream ps = new PrintStream(p.stdout());
ps.println("Call stubbed for: " + String.join(", ", maskedCmd)+" with env: " + String.join(",",p.envs()));
ps.println("Call stubbed for: " + String.join(", ", maskedCmd) + " with env: " + String.join(",", p.envs()));

return new FakeLauncher.FinishedProc(0);
}
return r.createLocalLauncher().launch(p);
Expand Down Expand Up @@ -84,29 +86,33 @@ protected boolean kubectlPresent() {
.anyMatch(Files::isExecutable);
}

protected BaseStandardCredentials secretCredential() {
return new StringCredentialsImpl(CredentialsScope.GLOBAL, CREDENTIAL_ID, "sample", Secret.fromString(PASSWORD));
protected BaseStandardCredentials secretCredential(String credentialId) {
return new StringCredentialsImpl(CredentialsScope.GLOBAL, credentialId, "sample", Secret.fromString(PASSWORD));
}

protected BaseStandardCredentials secretCredentialWithSpace() {
return new StringCredentialsImpl(CredentialsScope.GLOBAL, CREDENTIAL_ID, "sample", Secret.fromString(PASSWORD_WITH_SPACE));
protected BaseStandardCredentials secretCredentialWithSpace(String credentialId) {
return new StringCredentialsImpl(CredentialsScope.GLOBAL, credentialId, "sample", Secret.fromString(PASSWORD_WITH_SPACE));
}

protected BaseStandardCredentials certificateCredential() {
protected BaseStandardCredentials certificateCredential(String credentialId) {
String storeFile = getResourceFile("/org/jenkinsci/plugins/kubernetes/cli/kubernetes.pkcs12");
CertificateCredentialsImpl.KeyStoreSource keyStoreSource = new CertificateCredentialsImpl.FileOnMasterKeyStoreSource(storeFile);
return new CertificateCredentialsImpl(CredentialsScope.GLOBAL, CREDENTIAL_ID, "sample", PASSPHRASE, keyStoreSource);
return new CertificateCredentialsImpl(CredentialsScope.GLOBAL, credentialId, "sample", PASSPHRASE, keyStoreSource);
}

protected BaseStandardCredentials usernamePasswordCredential(String credentialId) {
return new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, credentialId, "sample", USERNAME, PASSWORD);
}

protected BaseStandardCredentials usernamePasswordCredential() {
return new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, CREDENTIAL_ID, "sample", USERNAME, PASSWORD);
protected BaseStandardCredentials usernamePasswordCredentialWithSpace(String credentialId) {
return new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, credentialId, "sample", USERNAME_WITH_SPACE, PASSWORD_WITH_SPACE);
}

protected BaseStandardCredentials usernamePasswordCredentialWithSpace() {
return new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, CREDENTIAL_ID, "sample", USERNAME_WITH_SPACE, PASSWORD_WITH_SPACE);
protected FileCredentials fileCredential(String credentialId) throws UnsupportedEncodingException {
return new FileCredentialsImpl(CredentialsScope.GLOBAL, credentialId, "sample", "file-name", SecretBytes.fromBytes("---\napiVersion: v1\nclusters:\n- cluster:\n name: test-sample\n".getBytes("UTF-8")));
}

protected FileCredentials fileCredential() throws UnsupportedEncodingException {
return new FileCredentialsImpl(CredentialsScope.GLOBAL, CREDENTIAL_ID, "sample", "file-name", SecretBytes.fromBytes("---\napiVersion: v1\nclusters:\n- cluster:\n name: test-sample\n".getBytes("UTF-8")));
protected OpenShiftBearerTokenCredentialImpl tokenCredential(String credentialId) {
return new OpenShiftBearerTokenCredentialImpl(CredentialsScope.GLOBAL, credentialId, "a-description", USERNAME, PASSWORD);
}
}

0 comments on commit 61dc592

Please sign in to comment.