From da3c2f77377b61fc2c388796b7cc283d0c57b811 Mon Sep 17 00:00:00 2001 From: max Date: Tue, 3 Sep 2024 21:40:11 +0200 Subject: [PATCH 1/2] Allow sources to be specified as Environment Variables resolves #1796 --- src/Paket.Core/Versioning/PackageSources.fs | 7 ++++++- .../DependenciesFile/ParserSpecs.fs | 20 +++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Paket.Core/Versioning/PackageSources.fs b/src/Paket.Core/Versioning/PackageSources.fs index c32f11de41..842f80d703 100644 --- a/src/Paket.Core/Versioning/PackageSources.fs +++ b/src/Paket.Core/Versioning/PackageSources.fs @@ -132,7 +132,12 @@ type PackageSource = else parts.[1].Replace("\"","").TrimEnd([| '/' |]) - let feed = normalizeFeedUrl source + let feed = + EnvironmentVariable.Create(source) + |> Option.map (fun var -> var.Value) + |> Option.defaultValue source + |> normalizeFeedUrl + PackageSource.Parse(feed, parseAuth(line, feed)) static member Parse(source,auth) = diff --git a/tests/Paket.Tests/DependenciesFile/ParserSpecs.fs b/tests/Paket.Tests/DependenciesFile/ParserSpecs.fs index 1a5914bc7b..d0a7c55493 100644 --- a/tests/Paket.Tests/DependenciesFile/ParserSpecs.fs +++ b/tests/Paket.Tests/DependenciesFile/ParserSpecs.fs @@ -750,7 +750,7 @@ nuget Rx-Main let ``should read config with password in env variable``() = Environment.SetEnvironmentVariable("FEED_USERNAME", "user XYZ", EnvironmentVariableTarget.Process) Environment.SetEnvironmentVariable("FEED_PASSWORD", "pw Love", EnvironmentVariableTarget.Process) - let cfg = DependenciesFile.FromSource( configWithPasswordInEnvVariable) + let cfg = DependenciesFile.FromSource(configWithPasswordInEnvVariable) cfg.Groups.[Constants.MainDependencyGroup].Sources |> shouldEqual [ @@ -769,7 +769,7 @@ nuget Rx-Main let ``should read config with password in env variable and auth type specified``() = Environment.SetEnvironmentVariable("FEED_USERNAME", "user XYZ", EnvironmentVariableTarget.Process) Environment.SetEnvironmentVariable("FEED_PASSWORD", "pw Love", EnvironmentVariableTarget.Process) - let cfg = DependenciesFile.FromSource( configWithPasswordInEnvVariableAndAuthType) + let cfg = DependenciesFile.FromSource(configWithPasswordInEnvVariableAndAuthType) cfg.Groups.[Constants.MainDependencyGroup].Sources |> shouldEqual [ @@ -779,6 +779,22 @@ let ``should read config with password in env variable and auth type specified`` cfg.Groups.[Constants.MainDependencyGroup].Sources.Head.Auth.Retrieve true |> shouldEqual (Some (Credentials{ Username = "user XYZ"; Password = "pw Love"; Type = NetUtils.AuthType.NTLM})) +let configWithSourceInEnvVariable = """ +source %FEED_URL% +nuget Rx-Main +""" + +[] +let ``should read config with source in env variable``() = + Environment.SetEnvironmentVariable("FEED_URL", "http://www.nuget.org/api/v2", EnvironmentVariableTarget.Process) + let cfg = DependenciesFile.FromSource(configWithSourceInEnvVariable) + + cfg.Groups.[Constants.MainDependencyGroup].Sources + |> shouldEqual [ + PackageSource.NuGetV2 { + Url = "http://www.nuget.org/api/v2" + Authentication = AuthProvider.empty} ] + let configWithExplicitVersions = """ source "http://www.nuget.org/api/v2" From eec381032cc5b37e798a102b2b5e57506d24f757 Mon Sep 17 00:00:00 2001 From: max Date: Wed, 4 Sep 2024 13:39:20 +0200 Subject: [PATCH 2/2] Allow credentials to be stored within the environment variable as environment variables themselves --- src/Paket.Core/Versioning/PackageSources.fs | 16 +++++++------- .../DependenciesFile/ParserSpecs.fs | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/Paket.Core/Versioning/PackageSources.fs b/src/Paket.Core/Versioning/PackageSources.fs index 842f80d703..328fcf0747 100644 --- a/src/Paket.Core/Versioning/PackageSources.fs +++ b/src/Paket.Core/Versioning/PackageSources.fs @@ -126,21 +126,21 @@ type PackageSource = static member Parse(line : string) = let sourceRegex = Regex("source[ ]*[\"]([^\"]*)[\"]", RegexOptions.IgnoreCase) let parts = line.Split ' ' + let source = if sourceRegex.IsMatch line then sourceRegex.Match(line).Groups.[1].Value.TrimEnd([| '/' |]) else parts.[1].Replace("\"","").TrimEnd([| '/' |]) - let feed = - EnvironmentVariable.Create(source) - |> Option.map (fun var -> var.Value) - |> Option.defaultValue source - |> normalizeFeedUrl - - PackageSource.Parse(feed, parseAuth(line, feed)) + match EnvironmentVariable.Create(source) with + | None -> + let feed = normalizeFeedUrl source + PackageSource.Parse(feed, parseAuth(line, feed)) + | Some var -> + PackageSource.Parse ("source " + var.Value) - static member Parse(source,auth) = + static member Parse(source, auth) = match tryParseWindowsStyleNetworkPath source with | Some path -> PackageSource.Parse(path) | _ -> diff --git a/tests/Paket.Tests/DependenciesFile/ParserSpecs.fs b/tests/Paket.Tests/DependenciesFile/ParserSpecs.fs index d0a7c55493..966083bce9 100644 --- a/tests/Paket.Tests/DependenciesFile/ParserSpecs.fs +++ b/tests/Paket.Tests/DependenciesFile/ParserSpecs.fs @@ -795,6 +795,27 @@ let ``should read config with source in env variable``() = Url = "http://www.nuget.org/api/v2" Authentication = AuthProvider.empty} ] + +let configWithSourceAndCredentialsInEnvVariable = """ +source %FEED_URL% +nuget Rx-Main +""" + +[] +let ``should read config with source and credentials in env variable``() = + Environment.SetEnvironmentVariable("FEED_USERNAME", "user XYZ", EnvironmentVariableTarget.Process) + Environment.SetEnvironmentVariable("FEED_PASSWORD", "pw Love", EnvironmentVariableTarget.Process) + Environment.SetEnvironmentVariable("FEED_URL", "http://www.nuget.org/api/v2 username: \"%FEED_USERNAME%\" password: \"%FEED_PASSWORD%\" authtype: \"nTlM\"", EnvironmentVariableTarget.Process) + let cfg = DependenciesFile.FromSource(configWithSourceAndCredentialsInEnvVariable) + + cfg.Groups.[Constants.MainDependencyGroup].Sources + |> shouldEqual [ + PackageSource.NuGetV2 { + Url = "http://www.nuget.org/api/v2" + Authentication = AuthProvider.empty} ] + cfg.Groups.[Constants.MainDependencyGroup].Sources.Head.Auth.Retrieve true + |> shouldEqual (Some (Credentials{ Username = "user XYZ"; Password = "pw Love"; Type = NetUtils.AuthType.NTLM})) + let configWithExplicitVersions = """ source "http://www.nuget.org/api/v2"