Skip to content

Commit

Permalink
test allowInsecure connections for test
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalDelarea committed Nov 20, 2024
1 parent 13afda7 commit d4415f2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class NugetRun extends PackageManagerExtractor {
private static final String CONFIG_FILE_FORMAT = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<configuration>\n" +
"\t<packageSources>\n" +
"\t\t<add key=\"JFrogJenkins\" value=\"%s\" protocolVersion=\"%s\" />\n" +
"\t\t<add key=\"JFrogJenkins\" value=\"%s\" protocolVersion=\"%s\" allowInsecureConnections=\"%b\" />\n" +
"\t</packageSources>\n" +
"\t<packageSourceCredentials>\n" +
"\t\t<JFrogJenkins>\n" +
Expand Down Expand Up @@ -75,6 +75,7 @@ public class NugetRun extends PackageManagerExtractor {
private String apiProtocol;
private String module;
private String nugetCmdArgs;
private boolean allowInsecureConnections;
private List<String> dependenciesSources;
private List<Module> modulesList = new ArrayList<>();

Expand All @@ -91,10 +92,11 @@ public class NugetRun extends PackageManagerExtractor {
* @param module - NuGet module
* @param username - JFrog platform username.
* @param password - JFrog platform password.
* @param allowInsecureConnections - Allow insecure package sources connection, should be used only for developing.
* @param apiProtocol - A string indicates which NuGet protocol should be used (V2/V3).
*/

public NugetRun(ArtifactoryManagerBuilder artifactoryManagerBuilder, String resolutionRepo, boolean useDotnetCli, String nugetCmdArgs, Log logger, Path path, Map<String, String> env, String module, String username, String password, String apiProtocol) {
public NugetRun(ArtifactoryManagerBuilder artifactoryManagerBuilder, String resolutionRepo, boolean useDotnetCli, String nugetCmdArgs, Log logger, Path path, Map<String, String> env, String module, String username, String password, String apiProtocol, boolean allowInsecureConnections) {
this.artifactoryManagerBuilder = artifactoryManagerBuilder;
this.toolchainDriver = useDotnetCli ? new DotnetDriver(env, path, logger) : new NugetDriver(env, path, logger);
this.workingDir = Files.isDirectory(path) ? path : path.toAbsolutePath().getParent();
Expand All @@ -106,6 +108,7 @@ public NugetRun(ArtifactoryManagerBuilder artifactoryManagerBuilder, String reso
this.password = password;
this.apiProtocol = StringUtils.isBlank(apiProtocol) ? DEFAULT_NUGET_PROTOCOL : apiProtocol;
this.module = module;
this.allowInsecureConnections = allowInsecureConnections;
}

private static String removeQuotes(String str) {
Expand Down Expand Up @@ -160,7 +163,8 @@ public static void main(String[] ignored) {
handler.getModule(),
clientConfiguration.resolver.getUsername(),
clientConfiguration.resolver.getPassword(),
clientConfiguration.dotnetHandler.apiProtocol());
clientConfiguration.dotnetHandler.apiProtocol(),
clientConfiguration.nuGetAllowInsecureConnections);
nugetRun.executeAndSaveBuildInfo(clientConfiguration);
} catch (RuntimeException e) {
ExceptionUtils.printRootCauseStackTrace(e, System.out);
Expand Down Expand Up @@ -208,7 +212,7 @@ private File prepareConfig(ArtifactoryManager artifactoryManager) throws Excepti
if (!nugetCmdArgs.contains(toolchainDriver.getFlagSyntax(ToolchainDriverBase.CONFIG_FILE_FLAG)) && !nugetCmdArgs.contains(toolchainDriver.getFlagSyntax(ToolchainDriverBase.SOURCE_FLAG))) {
configFile = File.createTempFile(NUGET_CONFIG_FILE_PREFIX, null);
configFile.deleteOnExit();
addSourceToConfigFile(configFile.getAbsolutePath(), artifactoryManager, resolutionRepo, username, password, apiProtocol);
addSourceToConfigFile(configFile.getAbsolutePath(), artifactoryManager, resolutionRepo, username, password, apiProtocol, allowInsecureConnections);
}
return configFile;
}
Expand All @@ -217,10 +221,10 @@ private File prepareConfig(ArtifactoryManager artifactoryManager) throws Excepti
* We will write a temporary NuGet configuration using a string formater in order to support NuGet v3 protocol.
* Currently the NuGet configuration utility doesn't allow setting protocolVersion.
*/
private void addSourceToConfigFile(String configPath, ArtifactoryManager client, String repo, String username, String password, String apiProtocol) throws Exception {
private void addSourceToConfigFile(String configPath, ArtifactoryManager client, String repo, String username, String password, String apiProtocol, boolean allowInsecureConnections) throws Exception {
String sourceUrl = toolchainDriver.buildNugetSourceUrl(client, repo, apiProtocol);
String protocolVersion = apiProtocol.substring(apiProtocol.length() - 1);
String configFileText = String.format(CONFIG_FILE_FORMAT, sourceUrl, protocolVersion, username, password);
String configFileText = String.format(CONFIG_FILE_FORMAT, sourceUrl, protocolVersion, username, password, Boolean.toString(allowInsecureConnections));
try (PrintWriter out = new PrintWriter(configPath)) {
out.println(configFileText);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class NugetExtractorTest extends IntegrationTestsBase {

private static final String NUGET_REMOTE_REPO = "build-info-tests-nuget-remote";
private static final String CUSTOM_MODULE = "custom-module-name";
private static final boolean ALLOW_INSECURE_CONNECTIONS_TEST = true;

private static final Path PROJECTS_ROOT = Paths.get(".").toAbsolutePath().normalize().resolve(Paths.get("src", "test", "resources", "org", "jfrog", "build", "extractor"));

Expand Down Expand Up @@ -95,7 +96,7 @@ public void nugetRunTest(Project project, String args, String moduleName, String
try {
// Run nuget restore install
projectDir = createProjectDir(project);
NugetRun nugetRun = new NugetRun(artifactoryManagerBuilder, remoteRepo, false, args, log, projectDir, env, moduleName, getUsername(), getAdminToken(), "v2");
NugetRun nugetRun = new NugetRun(artifactoryManagerBuilder, remoteRepo, false, args, log, projectDir, env, moduleName, getUsername(), getAdminToken(), "v2",ALLOW_INSECURE_CONNECTIONS_TEST);
executeAndAssertBuildInfo(nugetRun, expectedModules, expectedDependencies);
} catch (Exception e) {
fail(ExceptionUtils.getStackTrace(e));
Expand All @@ -117,7 +118,7 @@ public void dotnetCliRunTest(Project project, String args, String moduleName, St
try {
// Run nuget restore install
projectDir = createProjectDir(project);
NugetRun nugetRun = new NugetRun(artifactoryManagerBuilder, remoteRepo, true, args, log, projectDir, env, moduleName, getUsername(), getAdminToken(), "v2");
NugetRun nugetRun = new NugetRun(artifactoryManagerBuilder, remoteRepo, true, args, log, projectDir, env, moduleName, getUsername(), getAdminToken(), "v2",false);
executeAndAssertBuildInfo(nugetRun, expectedModules, expectedDependencies);
} catch (Exception e) {
fail(ExceptionUtils.getStackTrace(e));
Expand Down Expand Up @@ -167,7 +168,7 @@ private Object[][] projectRootProvider() {
private void getProjectRootTest(String args, String expectedProjectRootFileName) {
try {
File rootDir = PROJECTS_ROOT.resolve("projectRootTestDir").toFile();
NugetRun nugetRun = new NugetRun(artifactoryManagerBuilder, remoteRepo, false, args, log, rootDir.toPath(), env, null, getUsername(), getAdminToken(), "v2");
NugetRun nugetRun = new NugetRun(artifactoryManagerBuilder, remoteRepo, false, args, log, rootDir.toPath(), env, null, getUsername(), getAdminToken(), "v2",ALLOW_INSECURE_CONNECTIONS_TEST);
File projectRoot = nugetRun.getProjectRootPath();
assertTrue(projectRoot.getPath().endsWith(expectedProjectRootFileName));
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.jfrog.build.api.util.CommonUtils;
import org.jfrog.build.api.util.Log;
import org.jfrog.build.extractor.ci.BuildInfo;
import org.jfrog.build.extractor.ci.BuildInfoFields;
import org.jfrog.build.extractor.ci.Issue;
import org.jfrog.build.extractor.clientConfiguration.util.IssuesTrackerUtils;
import org.jfrog.build.extractor.clientConfiguration.util.encryption.EncryptionKeyPair;
Expand Down Expand Up @@ -54,6 +53,8 @@ public class ArtifactoryClientConfiguration {
public final DockerHandler dockerHandler;
public final GoHandler goHandler;
public final PrefixPropertyHandler root;
public final boolean nuGetAllowInsecureConnections;

/**
* To configure the props builder itself, so all method of this classes delegated from here
*/
Expand All @@ -73,6 +74,7 @@ public ArtifactoryClientConfiguration(Log log) {
this.dotnetHandler = new DotnetHandler();
this.dockerHandler = new DockerHandler();
this.goHandler = new GoHandler();
this.nuGetAllowInsecureConnections = false;
}

public void fillFromProperties(Map<String, String> props, IncludeExcludePatterns patterns) {
Expand Down Expand Up @@ -1289,15 +1291,15 @@ public static void addDefaultPublisherAttributes(ArtifactoryClientConfiguration
buildName = defaultProjectName;
config.info.setBuildName(buildName);
}
config.publisher.setMatrixParam(BuildInfoFields.BUILD_NAME, buildName);
config.publisher.setMatrixParam(BUILD_NAME, buildName);

// Build number
String buildNumber = config.info.getBuildNumber();
if (StringUtils.isBlank(buildNumber)) {
buildNumber = new Date().getTime() + "";
config.info.setBuildNumber(buildNumber);
}
config.publisher.setMatrixParam(BuildInfoFields.BUILD_NUMBER, buildNumber);
config.publisher.setMatrixParam(BUILD_NUMBER, buildNumber);

// Build start (was set by the plugin - no need to make up a fallback val)
String buildTimestamp = config.info.getBuildTimestamp();
Expand All @@ -1312,7 +1314,7 @@ public static void addDefaultPublisherAttributes(ArtifactoryClientConfiguration
buildTimestamp = String.valueOf(buildStartDate.getTime());
config.info.setBuildTimestamp(buildTimestamp);
}
config.publisher.setMatrixParam(BuildInfoFields.BUILD_TIMESTAMP, buildTimestamp);
config.publisher.setMatrixParam(BUILD_TIMESTAMP, buildTimestamp);

// Build agent
String buildAgentName = config.info.getBuildAgentName();
Expand Down

0 comments on commit d4415f2

Please sign in to comment.