Skip to content

Commit

Permalink
Use the Run context to retrieve credentials if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
kneirinck committed Feb 14, 2024
1 parent 48120c1 commit 62f18a3
Show file tree
Hide file tree
Showing 33 changed files with 59 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected BuildInfo createBuildInfo(String buildAgentName, String buildAgentVers
long duration = System.currentTimeMillis() - startedTimestamp.getTimeInMillis();
builder.durationMillis(duration);

String artifactoryPrincipal = configurator.getArtifactoryServer().getResolvingCredentialsConfig().provideCredentials(build.getParent()).getUsername();
String artifactoryPrincipal = configurator.getArtifactoryServer().getResolvingCredentialsConfig().provideCredentials(build).getUsername();
if (StringUtils.isBlank(artifactoryPrincipal)) {
artifactoryPrincipal = "";
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jfrog/hudson/ArtifactoryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ public FormValidation doTestConnection(
String username = StringUtils.EMPTY;
String password = StringUtils.EMPTY;

StringCredentials accessTokenCredentials = PluginsUtils.accessTokenCredentialsLookup(deployerCredentialsId, null);
StringCredentials accessTokenCredentials = PluginsUtils.accessTokenCredentialsLookup(deployerCredentialsId, (Item) null);
if (accessTokenCredentials != null) {
accessToken = accessTokenCredentials.getSecret().getPlainText();
} else {
if (useCredentialsPlugin) {
Credentials credentials = PluginsUtils.usernamePasswordCredentialsLookup(deployerCredentialsId, null);
Credentials credentials = PluginsUtils.usernamePasswordCredentialsLookup(deployerCredentialsId, (Item) null);
username = credentials.getUsername();
password = credentials.getPassword();
} else {
Expand Down Expand Up @@ -236,7 +236,7 @@ public FormValidation doTestJFrogPipelinesConnection(
return FormValidation.error("Connection Retries can not be less then 0");
}

StringCredentials accessTokenCredentials = PluginsUtils.accessTokenCredentialsLookup(credentialsId, null);
StringCredentials accessTokenCredentials = PluginsUtils.accessTokenCredentialsLookup(credentialsId, (Item) null);
if (accessTokenCredentials == null) {
return FormValidation.error("Please set credentials with access token as 'Secret text'");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis

ArtifactoryServer server = getArtifactoryServer();
CredentialsConfig preferredDeployer = CredentialManager.getPreferredDeployer(this, server);
try (ArtifactoryManager artifactoryManager = server.createArtifactoryManager(preferredDeployer.provideCredentials(((MavenModuleSetBuild) build).getProject()), ProxyUtils.createProxyConfiguration())) {
try (ArtifactoryManager artifactoryManager = server.createArtifactoryManager(preferredDeployer.provideCredentials(build), ProxyUtils.createProxyConfiguration())) {
server.setLog(listener, artifactoryManager);
if (deployArtifacts) {
new ArtifactsDeployer(this, artifactoryManager, mavenBuild, listener).deploy();
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/org/jfrog/hudson/ArtifactoryServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.jfrog.hudson;

import hudson.model.Item;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.util.XStream2;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -193,9 +194,9 @@ public Map getStagingStrategy(PluginSettings selectedStagingPlugin, String build
}
}

public List<ResolutionRepository> getVirtualRepositoryKeys(ResolverOverrider resolverOverrider, Item item) {
public List<ResolutionRepository> getVirtualRepositoryKeys(ResolverOverrider resolverOverrider, Run run) {
CredentialsConfig preferredResolver = CredentialManager.getPreferredResolver(resolverOverrider, this);
try (ArtifactoryManager artifactoryManager = createArtifactoryManager(preferredResolver.provideCredentials(item),
try (ArtifactoryManager artifactoryManager = createArtifactoryManager(preferredResolver.provideCredentials(run),
createProxyConfiguration())) {
resolutionRepositories = RepositoriesUtils.generateResolutionRepos(artifactoryManager);
} catch (IOException e) {
Expand Down Expand Up @@ -231,14 +232,16 @@ public boolean isArtifactoryPro(DeployerOverrider deployerOverrider, Item item)
public List<UserPluginInfo> getStagingUserPluginInfo(DeployerOverrider deployerOverrider, Item item) {
List<UserPluginInfo> infosToReturn = new ArrayList<>();
infosToReturn.add(UserPluginInfo.NO_PLUGIN);
gatherUserPluginInfo(infosToReturn, "staging", deployerOverrider, item);
gatherUserPluginInfo(infosToReturn, "staging", deployerOverrider,
CredentialManager.getPreferredResolver((ResolverOverrider) deployerOverrider, this).provideCredentials(item));
return infosToReturn;
}

public List<UserPluginInfo> getPromotionsUserPluginInfo(DeployerOverrider deployerOverrider, Item item) {
public List<UserPluginInfo> getPromotionsUserPluginInfo(DeployerOverrider deployerOverrider, Run run) {
List<UserPluginInfo> infosToReturn = new ArrayList<>();
infosToReturn.add(UserPluginInfo.NO_PLUGIN);
gatherUserPluginInfo(infosToReturn, "promotions", deployerOverrider, item);
gatherUserPluginInfo(infosToReturn, "promotions", deployerOverrider,
CredentialManager.getPreferredResolver((ResolverOverrider) deployerOverrider, this).provideCredentials(run));
return infosToReturn;
}

Expand Down Expand Up @@ -307,10 +310,8 @@ public CredentialsConfig getResolvingCredentialsConfig() {
return CredentialsConfig.EMPTY_CREDENTIALS_CONFIG;
}

private void gatherUserPluginInfo(List<UserPluginInfo> infosToReturn, String pluginKey, DeployerOverrider deployerOverrider, Item item) {
CredentialsConfig credentialsConfig = CredentialManager.getPreferredDeployer(deployerOverrider, this);
try (ArtifactoryManager artifactoryManager = createArtifactoryManager(credentialsConfig.provideCredentials(item),
createProxyConfiguration())) {
private void gatherUserPluginInfo(List<UserPluginInfo> infosToReturn, String pluginKey, DeployerOverrider deployerOverrider, Credentials credentials) {
try (ArtifactoryManager artifactoryManager = createArtifactoryManager(credentials, createProxyConfiguration())) {
Map<String, List<Map>> userPluginInfo = artifactoryManager.getUserPluginInfo();
if (userPluginInfo != null && userPluginInfo.containsKey(pluginKey)) {
List<Map> stagingUserPluginInfo = userPluginInfo.get(pluginKey);
Expand Down Expand Up @@ -366,7 +367,7 @@ public void reportUsage(String stepName, WorkflowRun build, Log logger) {
if (config == null) {
config = this.getResolverCredentialsConfig();
}
Credentials credentials = config.provideCredentials(build.getParent());
Credentials credentials = config.provideCredentials(build);
String[] featureIdArray = new String[]{stepName};
UsageReporter usageReporter = new UsageReporter("jenkins-artifactory-plugin/" + ActionableHelper.getArtifactoryPluginVersion(), featureIdArray);
usageReporter.reportUsage(this.getArtifactoryUrl(), credentials.getUsername(), credentials.getPassword(), credentials.getAccessToken(), Utils.getProxyConfiguration(this), null, logger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public Environment setUp(final AbstractBuild build, Launcher launcher, BuildList
ArtifactoryServer resolverServer = getArtifactoryResolverServer();
CredentialsConfig preferredResolver = CredentialManager.getPreferredResolver(ArtifactoryGenericConfigurator.this,
resolverServer);
Credentials resolverCredentials = preferredResolver.provideCredentials(build.getProject());
Credentials resolverCredentials = preferredResolver.provideCredentials(build);
ProxyConfiguration proxyConfiguration = null;
if (Jenkins.get().proxy != null && !resolverServer.isBypassProxy()) {
proxyConfiguration = createProxyConfiguration();
Expand Down Expand Up @@ -409,7 +409,7 @@ public boolean tearDown(AbstractBuild build, BuildListener listener) {
ArtifactoryServer server = getArtifactoryServer();
CredentialsConfig preferredDeployer = CredentialManager.getPreferredDeployer(ArtifactoryGenericConfigurator.this, server);

try (ArtifactoryManager artifactoryManager = server.createArtifactoryManager(preferredDeployer.provideCredentials(build.getProject()), createProxyConfiguration())) {
try (ArtifactoryManager artifactoryManager = server.createArtifactoryManager(preferredDeployer.provideCredentials(build), createProxyConfiguration())) {
server.setLog(listener, artifactoryManager);
boolean isFiltered = false;
if (isMultiConfProject(build)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void deploy()
if (configurator.isUseSpecs()) {
String spec = SpecUtils.getSpecStringFromSpecConf(configurator.getUploadSpec(), env, workingDir, listener.getLogger());
artifactsToDeploy = workingDir.act(new FilesDeployerCallable(listener, spec, artifactoryServer,
credentialsConfig.provideCredentials(build.getParent()), propertiesToAdd,
credentialsConfig.provideCredentials(build), propertiesToAdd,
createProxyConfiguration(), artifactoryServer.getDeploymentThreads()));
} else {
String deployPattern = Util.replaceMacro(configurator.getDeployPattern(), env);
Expand All @@ -97,7 +97,7 @@ public void deploy()
}
String repositoryKey = Util.replaceMacro(configurator.getRepositoryKey(), env);
artifactsToDeploy = workingDir.act(new FilesDeployerCallable(listener, pairs, artifactoryServer,
credentialsConfig.provideCredentials(build.getParent()), repositoryKey, propertiesToAdd,
credentialsConfig.provideCredentials(build), repositoryKey, propertiesToAdd,
createProxyConfiguration()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ public void buildEnvVars(Map<String, String> env) {
CredentialsConfig resolverCredentials = CredentialManager.getPreferredResolver(
ArtifactoryGradleConfigurator.this, getArtifactoryServer());
resolverContext = new ResolverContext(getArtifactoryResolverServer(), resolverServerDetails,
resolverCredentials.provideCredentials(build.getProject()), ArtifactoryGradleConfigurator.this);
resolverCredentials.provideCredentials(build), ArtifactoryGradleConfigurator.this);
}

try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jfrog.hudson.jfpipelines;

import hudson.model.Item;
import hudson.model.Queue;
import hudson.model.Result;
import hudson.model.Run;
Expand Down Expand Up @@ -91,7 +92,7 @@ public int getConnectionRetries() {
}

private JFrogPipelinesHttpClient createHttpClient(Log logger) {
JFrogPipelinesHttpClient client = new JFrogPipelinesHttpClient(integrationUrl, credentialsConfig.provideCredentials(null).getAccessToken(), logger);
JFrogPipelinesHttpClient client = new JFrogPipelinesHttpClient(integrationUrl, credentialsConfig.provideCredentials((Item) null).getAccessToken(), logger);
client.setConnectionRetries(getConnectionRetries());
client.setConnectionTimeout(getTimeout());
if (!isBypassProxy()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ public Environment setUp(final AbstractBuild build, final Launcher launcher, fin
if (isEnableResolveArtifacts()) {
CredentialsConfig credentialResolver = CredentialManager.getPreferredResolver(
ArtifactoryMaven3Configurator.this, getArtifactoryServer());
resolver = new ResolverContext(getArtifactoryServer(), getResolverDetails(), credentialResolver.provideCredentials(build.getProject()),
resolver = new ResolverContext(getArtifactoryServer(), getResolverDetails(), credentialResolver.provideCredentials(build),
ArtifactoryMaven3Configurator.this);
}
final ResolverContext resolverContext = resolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void buildEnvVars(Map<String, String> env) {
CredentialsConfig resolverCredentials = CredentialManager.getPreferredResolver(resolver,
resolver.getArtifactoryServer());
resolverContext = new ResolverContext(resolver.getArtifactoryServer(), resolver.getResolverDetails(),
resolverCredentials.provideCredentials(build.getProject()), resolver);
resolverCredentials.provideCredentials(build), resolver);
}

ArtifactoryClientConfiguration configuration = ExtractorUtils.addBuilderInfoArguments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void execute() throws Exception {
// Prepare Artifactory server
org.jfrog.hudson.ArtifactoryServer server = Utils.prepareArtifactoryServer(null, pipelineServer);
CredentialsConfig credentialsConfig = server.getResolverCredentialsConfig();
Credentials credentials = credentialsConfig.provideCredentials(build.getParent());
Credentials credentials = credentialsConfig.provideCredentials(build);

// Calculate build timestamp
long timestamp = getBuildTimestamp(server, credentials);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void execute() throws IOException, InterruptedException {
private ArtifactoryManagerBuilder getArtifactoryManagerBuilder(ArtifactoryServer pipelineServer, Run build, TaskListener listener) {
org.jfrog.hudson.ArtifactoryServer server = Utils.prepareArtifactoryServer(null, pipelineServer);
CredentialsConfig preferredDeployer = CredentialManager.getPreferredDeployer(new ArtifactoryConfigurator(server), server);
return server.createArtifactoryManagerBuilder(preferredDeployer.provideCredentials(build.getParent()),
return server.createArtifactoryManagerBuilder(preferredDeployer.provideCredentials(build),
ProxyUtils.createProxyConfiguration(), new JenkinsBuildInfoLog(listener));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public DistributionExecutor(ArtifactoryServer server, Run build, TaskListener li
public void execute() throws IOException {
ArtifactoryConfigurator configurator = new ArtifactoryConfigurator(server);
CredentialsConfig deployerConfig = CredentialManager.getPreferredDeployer(configurator, server);
ArtifactoryManager artifactoryManager = server.createArtifactoryManager(deployerConfig.provideCredentials(build.getParent()),
ArtifactoryManager artifactoryManager = server.createArtifactoryManager(deployerConfig.provideCredentials(build),
ProxyUtils.createProxyConfiguration());

DistributionBuilder distributionBuilder = new DistributionBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public EditPropsExecutor(ArtifactoryServer server, TaskListener listener, Run bu
public void execute() throws IOException, InterruptedException {
CredentialsConfig preferredDeployer = server.getDeployerCredentialsConfig();
Boolean success = ws.act(new EditPropertiesCallable(new JenkinsBuildInfoLog(listener),
preferredDeployer.provideCredentials(build.getParent()),
preferredDeployer.provideCredentials(build),
server.getArtifactoryUrl(), spec, Utils.getProxyConfiguration(server), editType, props));

if (failNoOp && !success) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public ArtifactoryClientConfiguration createArtifactoryClientConfiguration() thr
CredentialsConfig resolverCredentials = CredentialManager.getPreferredResolver(resolver,
resolver.getArtifactoryServer());
resolverContext = new ResolverContext(resolver.getArtifactoryServer(), resolver.getResolverDetails(),
resolverCredentials.provideCredentials(build.getParent()), resolver);
resolverCredentials.provideCredentials(build), resolver);
}

return ExtractorUtils.getArtifactoryClientConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public BuildInfo getBuildInfo() {

public void execute() throws IOException, InterruptedException {
CredentialsConfig preferredResolver = server.getDeployerCredentialsConfig();
Credentials resolverCredentials = preferredResolver.provideCredentials(build.getParent());
Credentials resolverCredentials = preferredResolver.provideCredentials(build);
List<Dependency> resolvedDependencies =
ws.act(new FilesResolverCallable(new JenkinsBuildInfoLog(listener),
resolverCredentials, server.getArtifactoryUrl(), spec, Utils.getProxyConfiguration(server)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public BuildInfo getBuildInfo() {
}

public void execute() throws IOException, InterruptedException {
Credentials credentials = server.getDeployerCredentialsConfig().provideCredentials(build.getParent());
Credentials credentials = server.getDeployerCredentialsConfig().provideCredentials(build);
ProxyConfiguration proxyConfiguration = Utils.getProxyConfiguration(server);

buildInfo.appendVcs(Utils.extractVcs(ws, new JenkinsBuildInfoLog(listener)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public void execute() {
throw new ServerNotFoundException("Duplicate configured JFrog instance ID: " + jfrogInstancesID);
}
JFrogPlatformInstance jfrogPlatformInstance = jfrogInstancesFound.get(0);
DistributionServer distributionServer = new DistributionServer(jfrogPlatformInstance, build.getParent());
ArtifactoryServer artifactoryServer = new ArtifactoryServer(jfrogPlatformInstance.getArtifactory(), build.getParent());
DistributionServer distributionServer = new DistributionServer(jfrogPlatformInstance, build);
ArtifactoryServer artifactoryServer = new ArtifactoryServer(jfrogPlatformInstance.getArtifactory(), build);
this.jfrogPlatformInstance = new org.jfrog.hudson.pipeline.common.types.JFrogPlatformInstance(artifactoryServer, distributionServer, jfrogPlatformInstance.getUrl(), jfrogPlatformInstance.getId());
artifactoryServer.setPlatformUrl(this.jfrogPlatformInstance.getUrl());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public PromotionExecutor(ArtifactoryServer server, Run<?, ?> build, TaskListener
public void execute() throws IOException {
ArtifactoryConfigurator configurator = new ArtifactoryConfigurator(server);
CredentialsConfig deployerConfig = CredentialManager.getPreferredDeployer(configurator, server);
ArtifactoryManager artifactoryManager = server.createArtifactoryManager(deployerConfig.provideCredentials(build.getParent()),
ArtifactoryManager artifactoryManager = server.createArtifactoryManager(deployerConfig.provideCredentials(build),
ProxyUtils.createProxyConfiguration());

PromotionBuilder promotionBuilder = new PromotionBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void execute() throws Exception {

private ArtifactoryManager createArtifactoryManager(org.jfrog.hudson.ArtifactoryServer server, Run build, TaskListener listener) {
CredentialsConfig preferredDeployer = CredentialManager.getPreferredDeployer(new ArtifactoryConfigurator(server), server);
return server.createArtifactoryManager(preferredDeployer.provideCredentials(build.getParent()),
return server.createArtifactoryManager(preferredDeployer.provideCredentials(build),
ProxyUtils.createProxyConfiguration(), new JenkinsBuildInfoLog(listener));
}
}
Loading

0 comments on commit 62f18a3

Please sign in to comment.