diff --git a/lib/Rhino.Licensing/Rhino.Licensing.dll b/lib/Rhino.Licensing/Rhino.Licensing.dll index 11edab73fe..9e2570dbd2 100644 Binary files a/lib/Rhino.Licensing/Rhino.Licensing.dll and b/lib/Rhino.Licensing/Rhino.Licensing.dll differ diff --git a/src/chocolatey.console/Program.cs b/src/chocolatey.console/Program.cs index ecc88d1ac3..54de739080 100644 --- a/src/chocolatey.console/Program.cs +++ b/src/chocolatey.console/Program.cs @@ -52,7 +52,9 @@ private static void Main(string[] args) Bootstrap.initialize(); Bootstrap.startup(); + var license = LicenseValidation.validate(); var container = SimpleInjectorContainer.Container; + var config = container.GetInstance(); var fileSystem = container.GetInstance(); @@ -62,6 +64,7 @@ private static void Main(string[] args) args, config, container, + license, warning => { warnings.Add(warning); } ); Config.initialize_with(config); @@ -74,18 +77,17 @@ private static void Main(string[] args) { "logfile".Log().Info(() => "".PadRight(60, '=')); #if DEBUG - "chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} v{1} (DEBUG BUILD)".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion)); -#else + "chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} v{1}{2} (DEBUG BUILD)".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion, license.is_licensed_version() ? " {0}".format_with(config.Information.LicenseType) : string.Empty)); +#else if (config.Information.ChocolateyVersion == config.Information.ChocolateyProductVersion && args.Any()) { - "logfile".Log().Info(() => "{0} v{1}".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion)); + "logfile".Log().Info(() => "{0} v{1}{2}".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion, license.is_licensed_version() ? " {0}".format_with(config.Information.LicenseType) : string.Empty)); } else { - "chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} v{1}".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion)); + "chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} v{1}{2}".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion, license.is_licensed_version() ? " {0}".format_with(config.Information.LicenseType) : string.Empty)); } #endif - } if (warnings.Count != 0 && config.RegularOutput) @@ -109,8 +111,6 @@ private static void Main(string[] args) remove_old_chocolatey_exe(fileSystem); - LicenseValidation.validate(fileSystem); - //refactor - thank goodness this is temporary, cuz manifest resource streams are dumb IList folders = new List { diff --git a/src/chocolatey.tests.integration/NUnitSetup.cs b/src/chocolatey.tests.integration/NUnitSetup.cs index d2e187acc4..d150b16131 100644 --- a/src/chocolatey.tests.integration/NUnitSetup.cs +++ b/src/chocolatey.tests.integration/NUnitSetup.cs @@ -21,6 +21,7 @@ namespace chocolatey.tests.integration using System.Linq; using System.Reflection; using System.Threading; + using chocolatey.infrastructure.licensing; using NUnit.Framework; using SimpleInjector; using chocolatey.infrastructure.app; @@ -56,7 +57,7 @@ public override void BeforeEverything() unpack_self(Container, config); build_packages(Container, config); - ConfigurationBuilder.set_up_configuration(new List(), config, Container.GetInstance(), Container.GetInstance(), null); + ConfigurationBuilder.set_up_configuration(new List(), config, Container, new ChocolateyLicense(), null); MockLogger.reset(); } diff --git a/src/chocolatey/GetChocolatey.cs b/src/chocolatey/GetChocolatey.cs index 3a96efc275..de3e8f11a9 100644 --- a/src/chocolatey/GetChocolatey.cs +++ b/src/chocolatey/GetChocolatey.cs @@ -17,6 +17,7 @@ namespace chocolatey { using System; using System.Collections.Generic; + using infrastructure.licensing; using SimpleInjector; using infrastructure.adapters; using infrastructure.app; @@ -51,6 +52,7 @@ public class GetChocolatey { private readonly Container _container; private Action _propConfig; + private ChocolateyLicense _license; /// /// Initializes a new instance of the class. @@ -59,6 +61,7 @@ public GetChocolatey() { Log4NetAppenderConfiguration.configure(); Bootstrap.initialize(); + _license = LicenseValidation.validate(); _container = SimpleInjectorContainer.Container; } @@ -218,7 +221,7 @@ public int ListCount() private ChocolateyConfiguration create_configuration(IList args) { var configuration = new ChocolateyConfiguration(); - ConfigurationBuilder.set_up_configuration(args, configuration, _container, null); + ConfigurationBuilder.set_up_configuration(args, configuration, _container, _license, null); Config.initialize_with(configuration); configuration.PromptForConfirmation = false; diff --git a/src/chocolatey/chocolatey.csproj b/src/chocolatey/chocolatey.csproj index 9cb61a21b2..764c16fda4 100644 --- a/src/chocolatey/chocolatey.csproj +++ b/src/chocolatey/chocolatey.csproj @@ -125,6 +125,8 @@ + + diff --git a/src/chocolatey/infrastructure.app/ApplicationParameters.cs b/src/chocolatey/infrastructure.app/ApplicationParameters.cs index a80e7dff8a..74685204b6 100644 --- a/src/chocolatey/infrastructure.app/ApplicationParameters.cs +++ b/src/chocolatey/infrastructure.app/ApplicationParameters.cs @@ -44,6 +44,8 @@ public static class ApplicationParameters public static readonly string ChocolateyConfigFileResource = @"chocolatey.infrastructure.app.configuration.chocolatey.config"; public static readonly string GlobalConfigFileLocation = _fileSystem.combine_paths(InstallLocation, "config", "chocolatey.config"); public static readonly string LicenseFileLocation = _fileSystem.combine_paths(InstallLocation, "license", "chocolatey.license.xml"); + public static readonly string LicensedAssemblyLocation = _fileSystem.combine_paths(InstallLocation, "extensions", "chocolatey", "chocolatey.licensed.dll"); + public static readonly string LicensedComponentRegistry = @"chocolatey.licensed.infrastructure.app.registration.ContainerBinding"; public static readonly string PackageNamesSeparator = ";"; public static readonly string OfficialChocolateyPublicKey = "79d02ea9cad655eb"; diff --git a/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs b/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs index ac4e48576f..0f5e290fed 100644 --- a/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs +++ b/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs @@ -28,6 +28,7 @@ namespace chocolatey.infrastructure.app.builders using information; using infrastructure.commands; using infrastructure.services; + using licensing; using logging; using nuget; using platforms; @@ -59,8 +60,9 @@ private static IEnvironment Environment /// The arguments. /// The configuration. /// The container. + /// The license. /// Notify warn logging action - public static void set_up_configuration(IList args, ChocolateyConfiguration config, Container container, Action notifyWarnLoggingAction) + public static void set_up_configuration(IList args, ChocolateyConfiguration config, Container container, ChocolateyLicense license, Action notifyWarnLoggingAction) { var fileSystem = container.GetInstance(); var xmlService = container.GetInstance(); @@ -68,6 +70,7 @@ public static void set_up_configuration(IList args, ChocolateyConfigurat ConfigurationOptions.reset_options(); set_global_options(args, config, container); set_environment_options(config); + set_license_options(config, license); set_environment_variables(config); } @@ -349,6 +352,15 @@ private static void set_environment_options(ChocolateyConfiguration config) config.Information.IsProcessElevated = ProcessInformation.process_is_elevated(); } + private static void set_license_options(ChocolateyConfiguration config, ChocolateyLicense license) + { + config.Information.LicenseExpirationDate = license.ExpirationDate; + config.Information.LicenseIsValid = license.IsValid; + config.Information.LicenseUserName = license.Name ?? string.Empty; + config.Information.LicenseVersion = license.Version ?? string.Empty; + config.Information.LicenseType = license.is_licensed_version() ? license.LicenseType.get_description_or_value() : string.Empty; + } + public static void set_environment_variables(ChocolateyConfiguration config) { Environment.SetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName, ApplicationParameters.InstallLocation); diff --git a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs index c5ca158ba2..dcec097875 100644 --- a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs +++ b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs @@ -20,6 +20,7 @@ namespace chocolatey.infrastructure.app.configuration using System.Reflection; using System.Text; using domain; + using licensing; using logging; using platforms; @@ -326,6 +327,11 @@ public sealed class InformationCommandConfiguration public bool IsInteractive { get; set; } public bool IsUserAdministrator { get; set; } public bool IsProcessElevated { get; set; } + public string LicenseType { get; set; } + public bool LicenseIsValid { get; set; } + public string LicenseVersion { get; set; } + public string LicenseUserName { get; set; } + public DateTime? LicenseExpirationDate { get; set; } } diff --git a/src/chocolatey/infrastructure/licensing/ChocolateyLicense.cs b/src/chocolatey/infrastructure/licensing/ChocolateyLicense.cs new file mode 100644 index 0000000000..97b82c50c1 --- /dev/null +++ b/src/chocolatey/infrastructure/licensing/ChocolateyLicense.cs @@ -0,0 +1,39 @@ +// Copyright © 2011 - Present RealDimensions Software, LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace chocolatey.infrastructure.licensing +{ + using System; + + public sealed class ChocolateyLicense + { + public string Name { get; set; } + public ChocolateyLicenseType LicenseType { get; set; } + public bool IsValid { get; set; } + public bool AssemblyLoaded { get; set; } + //todo: get version + public string Version { get; set; } + public string InvalidReason { get; set; } + public DateTime? ExpirationDate { get; set; } + + public bool is_licensed_version() + { + return IsValid + && LicenseType != ChocolateyLicenseType.Unknown + && LicenseType != ChocolateyLicenseType.Foss + ; + } + } +} diff --git a/src/chocolatey/infrastructure/licensing/ChocolateyLicenseType.cs b/src/chocolatey/infrastructure/licensing/ChocolateyLicenseType.cs new file mode 100644 index 0000000000..442736de6d --- /dev/null +++ b/src/chocolatey/infrastructure/licensing/ChocolateyLicenseType.cs @@ -0,0 +1,26 @@ +// Copyright © 2011 - Present RealDimensions Software, LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace chocolatey.infrastructure.licensing +{ + public enum ChocolateyLicenseType + { + Unknown, + Foss, + Professional, + Business, + Enterprise, + } +} diff --git a/src/chocolatey/infrastructure/licensing/LicenseValidation.cs b/src/chocolatey/infrastructure/licensing/LicenseValidation.cs index c03e3a960a..0a012b9485 100644 --- a/src/chocolatey/infrastructure/licensing/LicenseValidation.cs +++ b/src/chocolatey/infrastructure/licensing/LicenseValidation.cs @@ -15,26 +15,68 @@ namespace chocolatey.infrastructure.licensing { - using Rhino.Licensing; + using System; + using System.IO; using app; - using filesystem; + using Rhino.Licensing; public sealed class LicenseValidation { - private const string PUBLIC_KEY = @""; + private const string PUBLIC_KEY = + @"rznyhs3OslLqL7A7qav9bSHYGQmgWVsP/L47dWU7yF3EHsiYZuJNLlq8tQkPql/LB1FfLihiGsOKKUF1tmxihcRUrDaYkK1IYY3A+uJWkBglDUOUjnoDboI1FgF3wmXSb07JC8JCVYWjchq+h6MV9aDZaigA5MqMKNj9FE14f68=AQAB"; - public static void validate(IFileSystem fileSystem) + public static ChocolateyLicense validate() { - string licenseFile = ApplicationParameters.LicenseFileLocation; + var chocolateyLicense = new ChocolateyLicense + { + LicenseType = ChocolateyLicenseType.Unknown + }; - if (fileSystem.file_exists(licenseFile)) + string licenseFile = ApplicationParameters.LicenseFileLocation; + //no file system at this point + if (File.Exists(licenseFile)) { - new LicenseValidator(PUBLIC_KEY, licenseFile).AssertValidLicense(); + var license = new LicenseValidator(PUBLIC_KEY, licenseFile); + + try + { + license.AssertValidLicense(); + chocolateyLicense.IsValid = true; + } + catch (Exception e) + { + //license may be invalid + chocolateyLicense.IsValid = false; + chocolateyLicense.InvalidReason = e.Message; + "chocolatey".Log().Error("A license was found for a licensed version of Chocolatey, but is invalid:{0} {1}".format_with(Environment.NewLine, e.Message)); + } + + switch (license.LicenseType) + { + case LicenseType.Professional : + chocolateyLicense.LicenseType = ChocolateyLicenseType.Professional; + break; + case LicenseType.Business : + chocolateyLicense.LicenseType = ChocolateyLicenseType.Business; + break; + case LicenseType.Enterprise : + chocolateyLicense.LicenseType = ChocolateyLicenseType.Enterprise; + break; + } + + chocolateyLicense.ExpirationDate = license.ExpirationDate; + chocolateyLicense.Name = license.Name; + + //todo: if it is expired, provide a warning. + // one month after it should stop working } else { //free version + chocolateyLicense.LicenseType = ChocolateyLicenseType.Foss; } + + return chocolateyLicense; } } -} \ No newline at end of file +}