Skip to content

Commit

Permalink
track credential usage
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlaverse committed Jan 29, 2023
1 parent 01ff284 commit 94fed60
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ public String writeKubeConfig() throws IOException, InterruptedException {
throw new AbortException("[kubernetes-cli] unable to find credentials with id '" + credentialsId + "'");
}

CredentialsProvider.track(this.build, credentials);

// Convert into Kubernetes credentials
KubernetesAuth auth = AuthenticationTokens.convert(KubernetesAuth.class, credentials);
if (auth == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.domains.Domain;

import org.jenkinsci.plugins.kubernetes.cli.helpers.DummyCredentials;
Expand All @@ -31,6 +32,7 @@
import org.junit.experimental.categories.Category;
import org.jvnet.hudson.test.JenkinsRule;

import hudson.model.Fingerprint;
import hudson.FilePath;
import io.jenkins.cli.shaded.org.apache.commons.lang.SystemUtils;

Expand Down Expand Up @@ -296,6 +298,27 @@ public void testMultiKubeConfigWithServer() throws Exception {
" user: {}"));
}

@Test
public void testTracking() throws Exception {
CredentialsStore store = CredentialsProvider.lookupStores(r.jenkins).iterator().next();
Credentials credentials = DummyCredentials.fileCredential(CREDENTIAL_ID);
store.addCredentials(Domain.global(), credentials);
store.addCredentials(Domain.global(),
DummyCredentials.fileCredential(SECONDARY_CREDENTIAL_ID, "test-cluster2",
"test-user2"));

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testBasicWithCa");
p.setDefinition(new CpsFlowDefinition(
TestResourceLoader.loadAsString("withKubeCredentialsPipelineConfigDump.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
r.assertBuildStatusSuccess(r.waitForCompletion(b));

Fingerprint something = CredentialsProvider.getFingerprintOf(credentials);
assertNotNull(something);
assertNotNull(something.getUsages().get("testBasicWithCa"));
}

protected boolean kubectlPresent() {
return executablePaths()
.map(p -> p.resolve(kubectlBinaryName()))
Expand Down

0 comments on commit 94fed60

Please sign in to comment.