Skip to content

Commit

Permalink
Support login by Web (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
yahavi authored Aug 6, 2023
1 parent 8e85bd1 commit 3800d9b
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;

import static org.apache.commons.lang3.StringUtils.isNotBlank;

/**
* This class is used for managing JFrog CLI's configuration, so that is can be used by the IDEs.
*
Expand Down Expand Up @@ -55,10 +56,6 @@ public String getPassword() {

@Override
public String getAccessToken() {
// Prefer username/password if possible.
if (isNotBlank(getValueFromJson(USER_NAME))) {
return "";
}
return getValueFromJson(ACCESS_TOKEN);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import static com.jfrog.ide.common.utils.Utils.createSSLContext;

import static com.jfrog.ide.common.utils.Utils.resolveArtifactoryUrl;

/**
* Represents connection utils for Artifactory.
*
Expand All @@ -20,8 +22,9 @@
public class ArtifactoryConnectionUtils {

public static ArtifactoryManagerBuilder createArtifactoryManagerBuilder(ServerConfig serverConfig, Log logger) throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
String artifactoryUrl = resolveArtifactoryUrl(serverConfig.getArtifactoryUrl(), serverConfig.getUrl());
SSLContext sslContext = createSSLContext(serverConfig);
return createAnonymousAccessArtifactoryManagerBuilder(serverConfig.getArtifactoryUrl(), serverConfig.getProxyConfForTargetUrl(serverConfig.getArtifactoryUrl()), logger)
return createAnonymousAccessArtifactoryManagerBuilder(artifactoryUrl, serverConfig.getProxyConfForTargetUrl(artifactoryUrl), logger)
.setUsername(serverConfig.getUsername())
.setPassword(serverConfig.getPassword())
.setAccessToken(serverConfig.getAccessToken())
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/com/jfrog/ide/common/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
import static org.apache.commons.lang3.StringUtils.*;

/**
* @author yahavi
Expand Down Expand Up @@ -148,4 +149,46 @@ public static Set<Path> consolidatePaths(Set<Path> paths) {
public static String removeComponentIdPrefix(String compId) {
return StringUtils.substringAfter(compId, "://");
}

/**
* Resolve Xray URL from the input Xray URL (may be blank) or from the input platform URL.
*
* @param xrayUrl - Customize Xray URL or blank
* @param platformUrl - JFrog platform URL
* @return the resolved Xray URL
*/
public static String resolveXrayUrl(String xrayUrl, String platformUrl) {
return resolveProductUrl(xrayUrl, platformUrl, "xray");
}

/**
* Resolve Artifactory URL from the input Artifactory URL (may be blank) or from the input platform URL.
*
* @param artifactoryUrl - Customize Artifactory URL or blank
* @param platformUrl - JFrog platform URL
* @return the resolved Xray URL
*/
public static String resolveArtifactoryUrl(String artifactoryUrl, String platformUrl) {
return resolveProductUrl(artifactoryUrl, platformUrl, "artifactory");
}

/**
* Resolve Artifactory or Xray URL from the input product URL (may be blank) or from the input platform URL.
*
* @param productUrl - Customize Artifactory/Xray URL or blank
* @param platformUrl - JFrog platform URL
* @param productEndpoint - "artifactory" or "xray"
* @return the resolved Artifactory or Xray URL
*/
private static String resolveProductUrl(String productUrl, String platformUrl, String productEndpoint) {
String url = trimToEmpty(productUrl);
if (isNotBlank(url)) {
return removeEnd(url, "/");
}
url = trimToEmpty(platformUrl);
if (isBlank(url)) {
return "";
}
return removeEnd(url, "/") + "/" + productEndpoint;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.IOException;

import static com.jfrog.ide.common.utils.Constants.MINIMAL_XRAY_VERSION_SUPPORTED;
import static com.jfrog.ide.common.utils.Utils.resolveXrayUrl;

/**
* @author yahavi
Expand Down Expand Up @@ -61,14 +62,15 @@ public static Pair<Boolean, String> testComponentPermission(Xray xrayClient) thr
}

public static XrayClientBuilder createXrayClientBuilder(ServerConfig serverConfig, Log logger) {
String xrayUrl = resolveXrayUrl(serverConfig.getXrayUrl(), serverConfig.getUrl());
return (XrayClientBuilder) new XrayClientBuilder()
.setUrl(serverConfig.getXrayUrl())
.setUrl(xrayUrl)
.setUserName(serverConfig.getUsername())
.setPassword(serverConfig.getPassword())
.setAccessToken(serverConfig.getAccessToken())
.setInsecureTls(serverConfig.isInsecureTls())
.setSslContext(serverConfig.getSslContext())
.setProxyConfiguration(serverConfig.getProxyConfForTargetUrl(serverConfig.getXrayUrl()))
.setProxyConfiguration(serverConfig.getProxyConfForTargetUrl(xrayUrl))
.setConnectionRetries(serverConfig.getConnectionRetries())
.setTimeout(serverConfig.getConnectionTimeout())
.setLog(logger);
Expand Down
74 changes: 74 additions & 0 deletions src/test/java/com/jfrog/ide/common/utils/ConsolidatePathsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.jfrog.ide.common.utils;

import com.google.common.collect.Sets;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;

import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;

/**
* @author yahavi
*/
public class ConsolidatePathsTest {

private static String rootFolderA, rootFolderB, rootFolderC;

@BeforeTest
public void setUp() {
if (isWindows()) {
rootFolderA = "C:\\";
rootFolderB = "D:\\";
rootFolderC = "E:\\";
} else {
rootFolderA = "/a";
rootFolderB = "/b";
rootFolderC = "/c";
}
}

@Test(dataProvider = "pathsDataProvider")
public void testConsolidatePaths(Set<Path> given, Set<Path> expected) {
Set<Path> results = Utils.consolidatePaths(given);
assertEquals(expected.size(), results.size());
expected.forEach(path -> assertTrue(results.contains(path)));
}

@DataProvider
public static Object[][] pathsDataProvider() {
return new Object[][]{
{Sets.newHashSet((Object) Paths.get(rootFolderA)),
Sets.newHashSet((Object) Paths.get(rootFolderA))},

{Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderB), Paths.get(rootFolderC)),
Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderB), Paths.get(rootFolderC))},

{Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderB, "a"), Paths.get(rootFolderC, "b", "d"), Paths.get(rootFolderC, "b")),
Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderB, "a"), Paths.get(rootFolderC, "b"))},

{Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderA, "b"), Paths.get(rootFolderC, "b")),
Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderC, "b"))},

{Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderA, "a"), Paths.get(rootFolderA, "b")),
Sets.newHashSet((Object) Paths.get(rootFolderA))},

{Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderA, "a"), Paths.get(rootFolderC, "b"), Paths.get(rootFolderC, "b", "fff"), Paths.get(rootFolderC, "f", "fff")),
Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderC, "b"), Paths.get(rootFolderC, "f", "fff"))},

{Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderA, "a/"), Paths.get(rootFolderC, "b"), Paths.get(rootFolderC, "b", "..", "b", "..", "b", "fff"), Paths.get(rootFolderC, "f", "fff")),
Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderC, "b"), Paths.get(rootFolderC, "f", "fff"))},

{Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderA, "a"), Paths.get(rootFolderC, "b", ".."), Paths.get(rootFolderC, "b", "..", "b", "..", "b", "fff"), Paths.get(rootFolderC, "f", "fff")),
Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderC))},
};
}

private boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("win");
}
}
81 changes: 25 additions & 56 deletions src/test/java/com/jfrog/ide/common/utils/UtilsTest.java
Original file line number Diff line number Diff line change
@@ -1,74 +1,43 @@
package com.jfrog.ide.common.utils;

import com.google.common.collect.Sets;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
import static com.jfrog.ide.common.utils.Utils.resolveArtifactoryUrl;
import static com.jfrog.ide.common.utils.Utils.resolveXrayUrl;
import static org.testng.Assert.assertEquals;

import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;

/**
* @author yahavi
*/
public class UtilsTest {

private static String rootFolderA, rootFolderB, rootFolderC;

@BeforeTest
public void setUp() {
if (isWindows()) {
rootFolderA = "C:\\";
rootFolderB = "D:\\";
rootFolderC = "E:\\";
} else {
rootFolderA = "/a";
rootFolderB = "/b";
rootFolderC = "/c";
}
@DataProvider
public static Object[][] xrayUrlDataProvider() {
return new Object[][]{
{"", "", ""},
{"", "https://acme.jfrog.io", "https://acme.jfrog.io/xray"},
{"", "https://acme.jfrog.io/", "https://acme.jfrog.io/xray"},
{"https://acme.jfrog.io/xray", "https://acme.jfrog.io", "https://acme.jfrog.io/xray"},
{"https://acme.jfrog.io/xray/", "https://acme.jfrog.io", "https://acme.jfrog.io/xray"}
};
}

@Test(dataProvider = "pathsDataProvider")
public void testConsolidatePaths(Set<Path> given, Set<Path> expected) {
Set<Path> results = Utils.consolidatePaths(given);
assertEquals(expected.size(), results.size());
expected.forEach(path -> assertTrue(results.contains(path)));
@Test(dataProvider = "xrayUrlDataProvider")
public void testResolveXrayUrl(String inputXrayUrl, String inputPlatformUrl, String expectedXrayUrl) {
assertEquals(resolveXrayUrl(inputXrayUrl, inputPlatformUrl), expectedXrayUrl);
}

@DataProvider
public static Object[][] pathsDataProvider() {
public static Object[][] artifactoryUrlDataProvider() {
return new Object[][]{
{Sets.newHashSet((Object) Paths.get(rootFolderA)),
Sets.newHashSet((Object) Paths.get(rootFolderA))},

{Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderB), Paths.get(rootFolderC)),
Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderB), Paths.get(rootFolderC))},

{Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderB, "a"), Paths.get(rootFolderC, "b", "d"), Paths.get(rootFolderC, "b")),
Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderB, "a"), Paths.get(rootFolderC, "b"))},

{Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderA, "b"), Paths.get(rootFolderC, "b")),
Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderC, "b"))},

{Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderA, "a"), Paths.get(rootFolderA, "b")),
Sets.newHashSet((Object) Paths.get(rootFolderA))},

{Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderA, "a"), Paths.get(rootFolderC, "b"), Paths.get(rootFolderC, "b", "fff"), Paths.get(rootFolderC, "f", "fff")),
Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderC, "b"), Paths.get(rootFolderC, "f", "fff"))},

{Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderA, "a/"), Paths.get(rootFolderC, "b"), Paths.get(rootFolderC, "b", "..", "b", "..", "b", "fff"), Paths.get(rootFolderC, "f", "fff")),
Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderC, "b"), Paths.get(rootFolderC, "f", "fff"))},

{Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderA, "a"), Paths.get(rootFolderC, "b", ".."), Paths.get(rootFolderC, "b", "..", "b", "..", "b", "fff"), Paths.get(rootFolderC, "f", "fff")),
Sets.newHashSet(Paths.get(rootFolderA), Paths.get(rootFolderC))},
{"", "", ""},
{"", "https://acme.jfrog.io", "https://acme.jfrog.io/artifactory"},
{"", "https://acme.jfrog.io/", "https://acme.jfrog.io/artifactory"},
{"https://acme.jfrog.io/artifactory", "https://acme.jfrog.io", "https://acme.jfrog.io/artifactory"},
{"https://acme.jfrog.io/artifactory/", "https://acme.jfrog.io", "https://acme.jfrog.io/artifactory"}
};
}

private boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("win");
@Test(dataProvider = "artifactoryUrlDataProvider")
public void testResolveArtifactoryUrl(String inputArtifactoryUrl, String inputPlatformUrl, String expectedArtifactoryUrl) {
assertEquals(resolveArtifactoryUrl(inputArtifactoryUrl, inputPlatformUrl), expectedArtifactoryUrl);
}
}
}

0 comments on commit 3800d9b

Please sign in to comment.