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

Add Jfrog platform URL into Jenkins configuration page #455

Merged
merged 6 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ To run unit tests execute the following command:
Before running the integration tests, set the following environment variables.

*JENKINS_ARTIFACTORY_URL*<br>
Or-Geva marked this conversation as resolved.
Show resolved Hide resolved
*JENKINS_PLATFORM_URL*<br>
*JENKINS_ARTIFACTORY_USERNAME*<br>
*JENKINS_ARTIFACTORY_PASSWORD*<br>
*JENKINS_ARTIFACTORY_DOCKER_PUSH_DOMAIN* (For example, server-docker-local.jfrog.io)<br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ public boolean configure(StaplerRequest req, JSONObject json) {
}

/**
* Returns the list of {@link org.jfrog.hudson.ArtifactoryServer} configured.
* Returns the list of {@link JFrogPlatformInstance} configured.
*
* @return can be empty but never null.
*/
public List<ArtifactoryServer> getArtifactoryServers() {
return RepositoriesUtils.getArtifactoryServers();
public List<JFrogPlatformInstance> getJfrogInstances() {
return RepositoriesUtils.getJFrogPlatformInstances();
}

@SuppressWarnings("unused")
Expand Down Expand Up @@ -95,7 +95,7 @@ protected RefreshServerResponse refreshResolversFromArtifactory(String url, Stri
String password, boolean overrideCredentials) {
RefreshServerResponse response = new RefreshServerResponse();
CredentialsConfig credentialsConfig = new CredentialsConfig(username, password, credentialsId, overrideCredentials);
ArtifactoryServer artifactoryServer = RepositoriesUtils.getArtifactoryServer(url, RepositoriesUtils.getArtifactoryServers());
ArtifactoryServer artifactoryServer = RepositoriesUtils.getArtifactoryServer(url);
try {
List<VirtualRepository> virtualRepositories = refreshVirtualRepositories(artifactoryServer, credentialsConfig);
response.setVirtualRepositories(virtualRepositories);
Expand Down Expand Up @@ -124,7 +124,7 @@ protected RefreshServerResponse refreshDeployersFromArtifactory(String url, Stri
String password, boolean overrideCredentials, boolean refreshUserPlugins) {
RefreshServerResponse response = new RefreshServerResponse();
CredentialsConfig credentialsConfig = new CredentialsConfig(username, password, credentialsId, overrideCredentials);
ArtifactoryServer artifactoryServer = RepositoriesUtils.getArtifactoryServer(url, RepositoriesUtils.getArtifactoryServers());
ArtifactoryServer artifactoryServer = RepositoriesUtils.getArtifactoryServer(url);
try {
response.setRepositories(refreshRepositories(artifactoryServer, credentialsConfig));
if (refreshUserPlugins) {
Expand Down
141 changes: 100 additions & 41 deletions src/main/java/org/jfrog/hudson/ArtifactoryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@
package org.jfrog.hudson;

import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.google.common.collect.Lists;
import hudson.Extension;
import hudson.model.BuildableItem;
import hudson.model.BuildableItemWithBuildWrappers;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.util.Secret;
import hudson.security.ACL;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import hudson.util.Secret;
import hudson.util.XStream2;
import jenkins.model.GlobalConfiguration;
import jenkins.model.Jenkins;
import net.sf.json.JSONNull;
Expand All @@ -42,6 +44,7 @@
import org.jfrog.hudson.jfpipelines.JFrogPipelinesServer;
import org.jfrog.hudson.util.Credentials;
import org.jfrog.hudson.util.RepositoriesUtils;
import org.jfrog.hudson.util.converters.ArtifactoryBuilderConverter;
import org.jfrog.hudson.util.plugins.PluginsUtils;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.QueryParameter;
Expand Down Expand Up @@ -73,6 +76,11 @@ public class ArtifactoryBuilder extends GlobalConfiguration {
public static final class DescriptorImpl extends Descriptor<GlobalConfiguration> {

private boolean useCredentialsPlugin;
private List<JFrogPlatformInstance> jfrogInstances;
/**
* @deprecated: Use org.jfrog.hudson.ArtifactoryBuilder.DescriptorImpl#getJfrogInstances()
*/
@Deprecated
private List<ArtifactoryServer> artifactoryServers;
Copy link

Choose a reason for hiding this comment

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

Needs to be marked transient as described in the Jenkins developer documentation. That it was not broke JCasC export.

private JFrogPipelinesServer jfrogPipelinesServer = new JFrogPipelinesServer();

Expand All @@ -92,26 +100,26 @@ public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Item project) {
}

/**
* Performs on-the-fly validation of the form field 'ServerId'.
* Performs on-the-fly validation of the form field 'InstanceId'.
*
* @param value This parameter receives the value that the user has typed.
* @return Indicates the outcome of the validation. This is sent to the browser.
*/
@SuppressWarnings("unused")
public FormValidation doCheckServerId(@QueryParameter String value) {
public FormValidation doCheckInstanceId(@QueryParameter String value) {
if (value.length() == 0) {
return FormValidation.error("Please set server ID");
}
List<ArtifactoryServer> artifactoryServers = RepositoriesUtils.getArtifactoryServers();
if (artifactoryServers == null) {
List<JFrogPlatformInstance> JFrogPlatformInstances = RepositoriesUtils.getJFrogPlatformInstances();
if (JFrogPlatformInstances == null) {
return FormValidation.ok();
}
int countServersByValueAsName = 0;
for (ArtifactoryServer server : artifactoryServers) {
if (server.getServerId().equals(value)) {
for (JFrogPlatformInstance JFrogPlatformInstance : JFrogPlatformInstances) {
if (JFrogPlatformInstance.getId().equals(value)) {
countServersByValueAsName++;
if (countServersByValueAsName > 1) {
return FormValidation.error("Duplicated server ID");
return FormValidation.error("Duplicated JFrog platform instances ID");
}
}
}
Expand All @@ -121,9 +129,10 @@ public FormValidation doCheckServerId(@QueryParameter String value) {
@SuppressWarnings("unused")
@RequirePOST
public FormValidation doTestConnection(
@QueryParameter("artifactoryUrl") final String url,
@QueryParameter("artifactory.timeout") final String timeout,
@QueryParameter("artifactory.bypassProxy") final boolean bypassProxy,
@QueryParameter("url") final String url,
@QueryParameter("artifactoryUrl") final String artifactoryUrl,
@QueryParameter("instance.timeout") final String timeout,
@QueryParameter("instance.bypassProxy") final boolean bypassProxy,
@QueryParameter("useCredentialsPlugin") final boolean useCredentialsPlugin,
@QueryParameter("credentialsId") final String deployerCredentialsId,
@QueryParameter("username") final String deployerCredentialsUsername,
Expand All @@ -132,17 +141,20 @@ public FormValidation doTestConnection(
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
return FormValidation.error("Testing the connection requires 'Administer' permission");
}
if (StringUtils.isBlank(url)) {
return FormValidation.error("Please set a valid Artifactory URL");
if (StringUtils.isBlank(artifactoryUrl) && StringUtils.isBlank(url)) {
return FormValidation.error("Please set a valid Artifactory or platform URL");
Or-Geva marked this conversation as resolved.
Show resolved Hide resolved
}
if (connectionRetry < 0) {
return FormValidation.error("Connection Retries can not be less then 0");
}

if (StringUtils.isEmpty(deployerCredentialsId) && (StringUtils.isEmpty(deployerCredentialsUsername) || StringUtils.isEmpty(deployerCredentialsPassword))) {
return FormValidation.error("Please set a valid credentials");
Or-Geva marked this conversation as resolved.
Show resolved Hide resolved
}
String targetArtUrl = StringUtils.isBlank(artifactoryUrl) ? StringUtils.removeEnd(url, "/") + "/artifactory" : artifactoryUrl;
String accessToken = StringUtils.EMPTY;
String username = StringUtils.EMPTY;
String password = StringUtils.EMPTY;

StringCredentials accessTokenCredentials = PluginsUtils.accessTokenCredentialsLookup(deployerCredentialsId, null);
if (accessTokenCredentials != null) {
accessToken = accessTokenCredentials.getSecret().getPlainText();
Expand All @@ -159,9 +171,9 @@ public FormValidation doTestConnection(

ArtifactoryBuildInfoClient client;
if (StringUtils.isNotEmpty(username) || StringUtils.isNotEmpty(accessToken)) {
client = new ArtifactoryBuildInfoClient(url, username, password, accessToken, new NullLog());
client = new ArtifactoryBuildInfoClient(targetArtUrl, username, password, accessToken, new NullLog());
} else {
client = new ArtifactoryBuildInfoClient(url, new NullLog());
client = new ArtifactoryBuildInfoClient(targetArtUrl, new NullLog());
}

try {
Expand All @@ -182,7 +194,7 @@ public FormValidation doTestConnection(
} catch (Exception e) {
return FormValidation.error(e.getMessage());
}
return FormValidation.ok("Found Artifactory " + version.toString());
return FormValidation.ok("Found Artifactory " + version.toString() + " on " + targetArtUrl);
} finally {
client.close();
}
Expand Down Expand Up @@ -250,7 +262,7 @@ public boolean configure(StaplerRequest req, JSONObject o) throws FormException
Jenkins jenkins = Jenkins.getInstanceOrNull();
if (jenkins != null && jenkins.hasPermission(Jenkins.ADMINISTER)) {
boolean useCredentialsPlugin = (Boolean) o.get("useCredentialsPlugin");
configureArtifactoryServers(req, o);
configureJfrogServers(req, o);
configureJFrogPipelinesServer(o);
if (useCredentialsPlugin && !this.useCredentialsPlugin) {
resetJobsCredentials();
Expand All @@ -263,21 +275,26 @@ public boolean configure(StaplerRequest req, JSONObject o) throws FormException
throw new FormException("User doesn't have permissions to save", "Server ID");
}

private void configureArtifactoryServers(StaplerRequest req, JSONObject o) throws FormException {
List<ArtifactoryServer> artifactoryServers = null;
Object artifactoryServerObj = o.get("artifactoryServer"); // an array or single object
if (!JSONNull.getInstance().equals(artifactoryServerObj)) {
artifactoryServers = req.bindJSONToList(ArtifactoryServer.class, artifactoryServerObj);
private void configureJfrogServers(StaplerRequest req, JSONObject o) throws FormException {
List<JFrogPlatformInstance> jfrogInstances = Lists.newArrayList();
Object jfrogInstancesObj = o.get("jfrogInstances"); // an array or single object
if (!JSONNull.getInstance().equals(jfrogInstancesObj)) {
jfrogInstances = req.bindJSONToList(JFrogPlatformInstance.class, jfrogInstancesObj);
}

if (!isServerIDConfigured(artifactoryServers)) {
throw new FormException("Please set the Artifactory server ID.", "ServerID");
if (!isJfrogServersIDConfigured(jfrogInstances)) {
throw new FormException("Please set the Instance ID.", "InstanceID");
}

if (isServerDuplicated(artifactoryServers)) {
throw new FormException("The Artifactory server ID you have entered is already configured", "Server ID");
if (isInstanceDuplicated(jfrogInstances)) {
throw new FormException("The JFrog instance ID you have entered is already configured", "Instance ID");
}
setArtifactoryServers(artifactoryServers);

if (isEmptyUrls(jfrogInstances)) {
throw new FormException("Please set the The JFrog Platform or Artifactory URL", "URL");
}
fillEmptyServers(jfrogInstances);
setJfrogInstances(jfrogInstances);
}

private void configureJFrogPipelinesServer(JSONObject o) {
Expand All @@ -291,7 +308,7 @@ private void configureJFrogPipelinesServer(JSONObject o) {
}

private void resetServersCredentials() {
for (ArtifactoryServer server : artifactoryServers) {
for (JFrogPlatformInstance server : jfrogInstances) {
if (server.getResolverCredentialsConfig() != null) {
server.getResolverCredentialsConfig().deleteCredentials();
}
Expand Down Expand Up @@ -352,32 +369,74 @@ public void setUseCredentialsPlugin(boolean useCredentialsPlugin) {
this.useCredentialsPlugin = useCredentialsPlugin;
}

private boolean isServerDuplicated(List<ArtifactoryServer> artifactoryServers) {
private boolean isEmptyUrls(List<JFrogPlatformInstance> JfrogInstances) {
if (JfrogInstances == null) {
return false;
}
for (JFrogPlatformInstance instance : JfrogInstances) {
if (StringUtils.isBlank(instance.getUrl()) && StringUtils.isBlank(instance.getArtifactoryServer().getArtifactoryUrl())) {
return true;
}
}
return false;
}

private boolean fillEmptyServers(List<JFrogPlatformInstance> JfrogInstances) {
Or-Geva marked this conversation as resolved.
Show resolved Hide resolved
if (JfrogInstances == null) {
return false;
}
for (JFrogPlatformInstance instance : JfrogInstances) {
if (StringUtils.isBlank(instance.getArtifactoryServer().getArtifactoryUrl())) {
instance.getArtifactoryServer().setArtifactoryUrl(instance.getUrl() + "/artifactory");
}
}
return false;
}

private boolean isInstanceDuplicated(List<JFrogPlatformInstance> JfrogInstances) {
Set<String> serversNames = new HashSet<>();
if (artifactoryServers == null) {
if (JfrogInstances == null) {
return false;
}
for (ArtifactoryServer server : artifactoryServers) {
String name = server.getServerId();
if (serversNames.contains(name)) {
for (JFrogPlatformInstance instance : JfrogInstances) {
String id = instance.getId();
if (serversNames.contains(id)) {
return true;
}
serversNames.add(name);
serversNames.add(id);
}
return false;
}

private boolean isServerIDConfigured(List<ArtifactoryServer> artifactoryServers) {
if (artifactoryServers == null) {
private boolean isJfrogServersIDConfigured(List<JFrogPlatformInstance> JfrogInstances) {
if (JfrogInstances == null) {
return true;
}
for (ArtifactoryServer server : artifactoryServers) {
String name = server.getServerId();
if (StringUtils.isBlank(name)) {
for (JFrogPlatformInstance server : JfrogInstances) {
String platformId = server.getId();
String artifactoryId = server.getArtifactoryServer().getServerId();
if (StringUtils.isBlank(platformId) || StringUtils.isBlank(artifactoryId)) {
return false;
}
}
return true;
}

public List<JFrogPlatformInstance> getJfrogInstances() {
return jfrogInstances;
}

public void setJfrogInstances(List<JFrogPlatformInstance> jfrogInstances) {
this.jfrogInstances = jfrogInstances;
}

/**
* Page Converter
*/
public static final class ConverterImpl extends ArtifactoryBuilderConverter {
public ConverterImpl(XStream2 xstream) {
super(xstream);
}
}
}
}
12 changes: 5 additions & 7 deletions src/main/java/org/jfrog/hudson/ArtifactoryRedeployPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public DescriptorImpl getDescriptor() {
}

public ArtifactoryServer getArtifactoryServer() {
return RepositoriesUtils.getArtifactoryServer(getArtifactoryName(), getDescriptor().getArtifactoryServers());
return RepositoriesUtils.getArtifactoryServer(getArtifactoryName());
}

private Result getTreshold() {
Expand Down Expand Up @@ -525,9 +525,7 @@ public RefreshServerResponse refreshFromArtifactory(String url, String credentia
CredentialsConfig credentialsConfig = new CredentialsConfig(username, password, credentialsId, overrideCredentials);

try {
ArtifactoryServer artifactoryServer = RepositoriesUtils.getArtifactoryServer(
url, getArtifactoryServers()
);
ArtifactoryServer artifactoryServer = RepositoriesUtils.getArtifactoryServer(url);
List<Repository> releaseRepositories = refreshRepositories(artifactoryServer, credentialsConfig);
List<PluginSettings> userPluginKeys = refreshUserPlugins(artifactoryServer, credentialsConfig);

Expand All @@ -552,12 +550,12 @@ public String getDisplayName() {
}

/**
* Returns the list of {@link ArtifactoryServer} configured.
* Returns the list of {@link JFrogPlatformInstance} configured.
*
* @return can be empty but never null.
*/
public List<ArtifactoryServer> getArtifactoryServers() {
return RepositoriesUtils.getArtifactoryServers();
public List<JFrogPlatformInstance> getJfrogInstances() {
return RepositoriesUtils.getJFrogPlatformInstances();
}

public boolean isJiraPluginEnabled() {
Expand Down
Loading