Skip to content

Commit 02686fc

Browse files
authored
NuGet - add allowInsecureConnections attribute to config (#810)
1 parent 2a5003d commit 02686fc

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

build-info-extractor-nuget/src/main/java/org/jfrog/build/extractor/nuget/extractor/NugetRun.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.stream.Stream;
3636

3737
import static org.jfrog.build.api.util.FileChecksumCalculator.*;
38+
import static org.jfrog.build.extractor.clientConfiguration.ArtifactoryClientConfiguration.DEFAULT_NUGET_ALLOW_INSECURE_CONNECTIONS;
3839
import static org.jfrog.build.extractor.clientConfiguration.ArtifactoryClientConfiguration.DEFAULT_NUGET_PROTOCOL;
3940
import static org.jfrog.build.extractor.packageManager.PackageManagerUtils.createArtifactoryClientConfiguration;
4041

@@ -47,7 +48,7 @@ public class NugetRun extends PackageManagerExtractor {
4748
private static final String CONFIG_FILE_FORMAT = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
4849
"<configuration>\n" +
4950
"\t<packageSources>\n" +
50-
"\t\t<add key=\"JFrogJenkins\" value=\"%s\" protocolVersion=\"%s\" />\n" +
51+
"\t\t<add key=\"JFrogJenkins\" value=\"%s\" protocolVersion=\"%s\" allowInsecureConnections=\"%s\"/>\n" +
5152
"\t</packageSources>\n" +
5253
"\t<packageSourceCredentials>\n" +
5354
"\t\t<JFrogJenkins>\n" +
@@ -75,6 +76,7 @@ public class NugetRun extends PackageManagerExtractor {
7576
private String apiProtocol;
7677
private String module;
7778
private String nugetCmdArgs;
79+
private boolean allowInsecureConnections;
7880
private List<String> dependenciesSources;
7981
private List<Module> modulesList = new ArrayList<>();
8082

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

97-
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) {
100+
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) {
98101
this.artifactoryManagerBuilder = artifactoryManagerBuilder;
99102
this.toolchainDriver = useDotnetCli ? new DotnetDriver(env, path, logger) : new NugetDriver(env, path, logger);
100103
this.workingDir = Files.isDirectory(path) ? path : path.toAbsolutePath().getParent();
@@ -106,6 +109,7 @@ public NugetRun(ArtifactoryManagerBuilder artifactoryManagerBuilder, String reso
106109
this.password = password;
107110
this.apiProtocol = StringUtils.isBlank(apiProtocol) ? DEFAULT_NUGET_PROTOCOL : apiProtocol;
108111
this.module = module;
112+
this.allowInsecureConnections = allowInsecureConnections == null ? DEFAULT_NUGET_ALLOW_INSECURE_CONNECTIONS : allowInsecureConnections;
109113
}
110114

111115
private static String removeQuotes(String str) {
@@ -160,7 +164,8 @@ public static void main(String[] ignored) {
160164
handler.getModule(),
161165
clientConfiguration.resolver.getUsername(),
162166
clientConfiguration.resolver.getPassword(),
163-
clientConfiguration.dotnetHandler.apiProtocol());
167+
clientConfiguration.dotnetHandler.apiProtocol(),
168+
clientConfiguration.getNuGetAllowInsecureConnections());
164169
nugetRun.executeAndSaveBuildInfo(clientConfiguration);
165170
} catch (RuntimeException e) {
166171
ExceptionUtils.printRootCauseStackTrace(e, System.out);
@@ -208,7 +213,7 @@ private File prepareConfig(ArtifactoryManager artifactoryManager) throws Excepti
208213
if (!nugetCmdArgs.contains(toolchainDriver.getFlagSyntax(ToolchainDriverBase.CONFIG_FILE_FLAG)) && !nugetCmdArgs.contains(toolchainDriver.getFlagSyntax(ToolchainDriverBase.SOURCE_FLAG))) {
209214
configFile = File.createTempFile(NUGET_CONFIG_FILE_PREFIX, null);
210215
configFile.deleteOnExit();
211-
addSourceToConfigFile(configFile.getAbsolutePath(), artifactoryManager, resolutionRepo, username, password, apiProtocol);
216+
addSourceToConfigFile(configFile.getAbsolutePath(), artifactoryManager, resolutionRepo, username, password, apiProtocol, allowInsecureConnections);
212217
}
213218
return configFile;
214219
}
@@ -217,10 +222,10 @@ private File prepareConfig(ArtifactoryManager artifactoryManager) throws Excepti
217222
* We will write a temporary NuGet configuration using a string formater in order to support NuGet v3 protocol.
218223
* Currently the NuGet configuration utility doesn't allow setting protocolVersion.
219224
*/
220-
private void addSourceToConfigFile(String configPath, ArtifactoryManager client, String repo, String username, String password, String apiProtocol) throws Exception {
225+
private void addSourceToConfigFile(String configPath, ArtifactoryManager client, String repo, String username, String password, String apiProtocol, boolean allowInsecureConnections) throws Exception {
221226
String sourceUrl = toolchainDriver.buildNugetSourceUrl(client, repo, apiProtocol);
222227
String protocolVersion = apiProtocol.substring(apiProtocol.length() - 1);
223-
String configFileText = String.format(CONFIG_FILE_FORMAT, sourceUrl, protocolVersion, username, password);
228+
String configFileText = String.format(CONFIG_FILE_FORMAT, sourceUrl, protocolVersion, Boolean.toString(allowInsecureConnections), username, password);
224229
try (PrintWriter out = new PrintWriter(configPath)) {
225230
out.println(configFileText);
226231
}

build-info-extractor-nuget/src/test/java/org/jfrog/build/extractor/nuget/extractor/NugetExtractorTest.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class NugetExtractorTest extends IntegrationTestsBase {
3131

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

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

@@ -95,7 +96,7 @@ public void nugetRunTest(Project project, String args, String moduleName, String
9596
try {
9697
// Run nuget restore install
9798
projectDir = createProjectDir(project);
98-
NugetRun nugetRun = new NugetRun(artifactoryManagerBuilder, remoteRepo, false, args, log, projectDir, env, moduleName, getUsername(), getAdminToken(), "v2");
99+
NugetRun nugetRun = new NugetRun(artifactoryManagerBuilder, remoteRepo, false, args, log, projectDir, env, moduleName, getUsername(), getAdminToken(), "v2",ALLOW_INSECURE_CONNECTIONS_TEST);
99100
executeAndAssertBuildInfo(nugetRun, expectedModules, expectedDependencies);
100101
} catch (Exception e) {
101102
fail(ExceptionUtils.getStackTrace(e));
@@ -117,7 +118,7 @@ public void dotnetCliRunTest(Project project, String args, String moduleName, St
117118
try {
118119
// Run nuget restore install
119120
projectDir = createProjectDir(project);
120-
NugetRun nugetRun = new NugetRun(artifactoryManagerBuilder, remoteRepo, true, args, log, projectDir, env, moduleName, getUsername(), getAdminToken(), "v2");
121+
NugetRun nugetRun = new NugetRun(artifactoryManagerBuilder, remoteRepo, true, args, log, projectDir, env, moduleName, getUsername(), getAdminToken(), "v2",ALLOW_INSECURE_CONNECTIONS_TEST);
121122
executeAndAssertBuildInfo(nugetRun, expectedModules, expectedDependencies);
122123
} catch (Exception e) {
123124
fail(ExceptionUtils.getStackTrace(e));
@@ -167,7 +168,7 @@ private Object[][] projectRootProvider() {
167168
private void getProjectRootTest(String args, String expectedProjectRootFileName) {
168169
try {
169170
File rootDir = PROJECTS_ROOT.resolve("projectRootTestDir").toFile();
170-
NugetRun nugetRun = new NugetRun(artifactoryManagerBuilder, remoteRepo, false, args, log, rootDir.toPath(), env, null, getUsername(), getAdminToken(), "v2");
171+
NugetRun nugetRun = new NugetRun(artifactoryManagerBuilder, remoteRepo, false, args, log, rootDir.toPath(), env, null, getUsername(), getAdminToken(), "v2",ALLOW_INSECURE_CONNECTIONS_TEST);
171172
File projectRoot = nugetRun.getProjectRootPath();
172173
assertTrue(projectRoot.getPath().endsWith(expectedProjectRootFileName));
173174
} catch (Exception e) {

build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/ArtifactoryClientConfiguration.java

+7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class ArtifactoryClientConfiguration {
4141
// Try checksum deploy of files greater than 10KB
4242
public static final transient int DEFAULT_MIN_CHECKSUM_DEPLOY_SIZE_KB = 10;
4343
public static final String DEFAULT_NUGET_PROTOCOL = "v2";
44+
public static final boolean DEFAULT_NUGET_ALLOW_INSECURE_CONNECTIONS = false;
4445

4546
public final ResolverHandler resolver;
4647
public final PublisherHandler publisher;
@@ -54,6 +55,8 @@ public class ArtifactoryClientConfiguration {
5455
public final DockerHandler dockerHandler;
5556
public final GoHandler goHandler;
5657
public final PrefixPropertyHandler root;
58+
59+
5760
/**
5861
* To configure the props builder itself, so all method of this classes delegated from here
5962
*/
@@ -208,6 +211,10 @@ public boolean getInsecureTls() {
208211
return root.getBooleanValue(PROP_INSECURE_TLS, false);
209212
}
210213

214+
public boolean getNuGetAllowInsecureConnections() {
215+
return root.getBooleanValue(PROP_NUGET_ALLOW_INSECURE_CONNECTIONS, false);
216+
}
217+
211218
public void setInsecureTls(boolean enabled) {
212219
root.setBooleanValue(PROP_INSECURE_TLS, enabled);
213220
}

build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/ClientProperties.java

+6
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,10 @@ public interface ClientProperties {
7070
* Property for whether to use relaxed ssl check and ignore issues with server certificate
7171
*/
7272
String PROP_INSECURE_TLS = "insecureTls";
73+
74+
/**
75+
* Property to allow NuGet package sources to use insecure connections (HTTP).
76+
* This setting is enforced by the NuGet client and is not recommended for production use.
77+
*/
78+
String PROP_NUGET_ALLOW_INSECURE_CONNECTIONS = "nuget.AllowInsecureConnections";
7379
}

0 commit comments

Comments
 (0)