Skip to content

Commit

Permalink
(chocolateyGH-584) Provide licensing information
Browse files Browse the repository at this point in the history
When a user has Chocolatey Pro / Chocolatey for Business, choco should
validate the license and provide information about the license in the
configuration. It should also mention the licensed type as part of the
name and version announced with choco.
  • Loading branch information
ferventcoder committed Jan 28, 2016
1 parent 45e6ec6 commit 1e5aa27
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 18 deletions.
Binary file modified lib/Rhino.Licensing/Rhino.Licensing.dll
Binary file not shown.
14 changes: 7 additions & 7 deletions src/chocolatey.console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChocolateyConfiguration>();
var fileSystem = container.GetInstance<IFileSystem>();

Expand All @@ -62,6 +64,7 @@ private static void Main(string[] args)
args,
config,
container,
license,
warning => { warnings.Add(warning); }
);
Config.initialize_with(config);
Expand All @@ -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)
Expand All @@ -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<string> folders = new List<string>
{
Expand Down
3 changes: 2 additions & 1 deletion src/chocolatey.tests.integration/NUnitSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,7 +57,7 @@ public override void BeforeEverything()
unpack_self(Container, config);
build_packages(Container, config);

ConfigurationBuilder.set_up_configuration(new List<string>(), config, Container.GetInstance<IFileSystem>(), Container.GetInstance<IXmlService>(), null);
ConfigurationBuilder.set_up_configuration(new List<string>(), config, Container, new ChocolateyLicense(), null);

MockLogger.reset();
}
Expand Down
5 changes: 4 additions & 1 deletion src/chocolatey/GetChocolatey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace chocolatey
{
using System;
using System.Collections.Generic;
using infrastructure.licensing;
using SimpleInjector;
using infrastructure.adapters;
using infrastructure.app;
Expand Down Expand Up @@ -51,6 +52,7 @@ public class GetChocolatey
{
private readonly Container _container;
private Action<ChocolateyConfiguration> _propConfig;
private ChocolateyLicense _license;

/// <summary>
/// Initializes a new instance of the <see cref="GetChocolatey"/> class.
Expand All @@ -59,6 +61,7 @@ public GetChocolatey()
{
Log4NetAppenderConfiguration.configure();
Bootstrap.initialize();
_license = LicenseValidation.validate();
_container = SimpleInjectorContainer.Container;
}

Expand Down Expand Up @@ -218,7 +221,7 @@ public int ListCount()
private ChocolateyConfiguration create_configuration(IList<string> 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;
Expand Down
2 changes: 2 additions & 0 deletions src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@
<Compile Include="infrastructure.app\services\IConfigTransformService.cs" />
<Compile Include="infrastructure.app\services\IFilesService.cs" />
<Compile Include="infrastructure.app\services\ISourceRunner.cs" />
<Compile Include="infrastructure\licensing\ChocolateyLicense.cs" />
<Compile Include="infrastructure\licensing\ChocolateyLicenseType.cs" />
<Compile Include="infrastructure\powershell\PoshHost.cs" />
<Compile Include="infrastructure\powershell\PoshHostRawUserInterface.cs" />
<Compile Include="infrastructure\powershell\PoshHostUserInterface.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/chocolatey/infrastructure.app/ApplicationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,15 +60,17 @@ private static IEnvironment Environment
/// <param name="args">The arguments.</param>
/// <param name="config">The configuration.</param>
/// <param name="container">The container.</param>
/// <param name="license">The license.</param>
/// <param name="notifyWarnLoggingAction">Notify warn logging action</param>
public static void set_up_configuration(IList<string> args, ChocolateyConfiguration config, Container container, Action<string> notifyWarnLoggingAction)
public static void set_up_configuration(IList<string> args, ChocolateyConfiguration config, Container container, ChocolateyLicense license, Action<string> notifyWarnLoggingAction)
{
var fileSystem = container.GetInstance<IFileSystem>();
var xmlService = container.GetInstance<IXmlService>();
set_file_configuration(config, fileSystem, xmlService, notifyWarnLoggingAction);
ConfigurationOptions.reset_options();
set_global_options(args, config, container);
set_environment_options(config);
set_license_options(config, license);
set_environment_variables(config);
}

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace chocolatey.infrastructure.app.configuration
using System.Reflection;
using System.Text;
using domain;
using licensing;
using logging;
using platforms;

Expand Down Expand Up @@ -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; }

}

Expand Down
39 changes: 39 additions & 0 deletions src/chocolatey/infrastructure/licensing/ChocolateyLicense.cs
Original file line number Diff line number Diff line change
@@ -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
;
}
}
}
26 changes: 26 additions & 0 deletions src/chocolatey/infrastructure/licensing/ChocolateyLicenseType.cs
Original file line number Diff line number Diff line change
@@ -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,
}
}
58 changes: 50 additions & 8 deletions src/chocolatey/infrastructure/licensing/LicenseValidation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
@"<RSAKeyValue><Modulus>rznyhs3OslLqL7A7qav9bSHYGQmgWVsP/L47dWU7yF3EHsiYZuJNLlq8tQkPql/LB1FfLihiGsOKKUF1tmxihcRUrDaYkK1IYY3A+uJWkBglDUOUjnoDboI1FgF3wmXSb07JC8JCVYWjchq+h6MV9aDZaigA5MqMKNj9FE14f68=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";

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;
}
}
}
}

0 comments on commit 1e5aa27

Please sign in to comment.