diff --git a/Configuration.sln b/Configuration.sln index 62b1ac7..0370cc9 100644 --- a/Configuration.sln +++ b/Configuration.sln @@ -27,12 +27,6 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.Configu EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.ConfigurationModel.Test.Common", "test\Microsoft.Framework.ConfigurationModel.Test.Common\Microsoft.Framework.ConfigurationModel.Test.Common.xproj", "{29C120E5-F682-4BFB-826B-040A594802CA}" EndProject -Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.ConfigurationModel.UserSecrets", "src\Microsoft.Framework.ConfigurationModel.UserSecrets\Microsoft.Framework.ConfigurationModel.UserSecrets.xproj", "{58B6443B-1278-4DF9-B7BB-DDF3BFFCF868}" -EndProject -Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "SecretManager", "src\SecretManager\SecretManager.xproj", "{8730E848-CA0F-4E0A-9A2F-BC22AD0B2C4E}" -EndProject -Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "SecretManager.Tests", "test\SecretManager.Tests\SecretManager.Tests.xproj", "{113EBBD2-E857-4CAF-9B53-7A8742CBCD4A}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -147,42 +141,6 @@ Global {29C120E5-F682-4BFB-826B-040A594802CA}.Release|Mixed Platforms.Build.0 = Release|Any CPU {29C120E5-F682-4BFB-826B-040A594802CA}.Release|x86.ActiveCfg = Release|Any CPU {29C120E5-F682-4BFB-826B-040A594802CA}.Release|x86.Build.0 = Release|Any CPU - {58B6443B-1278-4DF9-B7BB-DDF3BFFCF868}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {58B6443B-1278-4DF9-B7BB-DDF3BFFCF868}.Debug|Any CPU.Build.0 = Debug|Any CPU - {58B6443B-1278-4DF9-B7BB-DDF3BFFCF868}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU - {58B6443B-1278-4DF9-B7BB-DDF3BFFCF868}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU - {58B6443B-1278-4DF9-B7BB-DDF3BFFCF868}.Debug|x86.ActiveCfg = Debug|Any CPU - {58B6443B-1278-4DF9-B7BB-DDF3BFFCF868}.Debug|x86.Build.0 = Debug|Any CPU - {58B6443B-1278-4DF9-B7BB-DDF3BFFCF868}.Release|Any CPU.ActiveCfg = Release|Any CPU - {58B6443B-1278-4DF9-B7BB-DDF3BFFCF868}.Release|Any CPU.Build.0 = Release|Any CPU - {58B6443B-1278-4DF9-B7BB-DDF3BFFCF868}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU - {58B6443B-1278-4DF9-B7BB-DDF3BFFCF868}.Release|Mixed Platforms.Build.0 = Release|Any CPU - {58B6443B-1278-4DF9-B7BB-DDF3BFFCF868}.Release|x86.ActiveCfg = Release|Any CPU - {58B6443B-1278-4DF9-B7BB-DDF3BFFCF868}.Release|x86.Build.0 = Release|Any CPU - {8730E848-CA0F-4E0A-9A2F-BC22AD0B2C4E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {8730E848-CA0F-4E0A-9A2F-BC22AD0B2C4E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {8730E848-CA0F-4E0A-9A2F-BC22AD0B2C4E}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU - {8730E848-CA0F-4E0A-9A2F-BC22AD0B2C4E}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU - {8730E848-CA0F-4E0A-9A2F-BC22AD0B2C4E}.Debug|x86.ActiveCfg = Debug|Any CPU - {8730E848-CA0F-4E0A-9A2F-BC22AD0B2C4E}.Debug|x86.Build.0 = Debug|Any CPU - {8730E848-CA0F-4E0A-9A2F-BC22AD0B2C4E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {8730E848-CA0F-4E0A-9A2F-BC22AD0B2C4E}.Release|Any CPU.Build.0 = Release|Any CPU - {8730E848-CA0F-4E0A-9A2F-BC22AD0B2C4E}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU - {8730E848-CA0F-4E0A-9A2F-BC22AD0B2C4E}.Release|Mixed Platforms.Build.0 = Release|Any CPU - {8730E848-CA0F-4E0A-9A2F-BC22AD0B2C4E}.Release|x86.ActiveCfg = Release|Any CPU - {8730E848-CA0F-4E0A-9A2F-BC22AD0B2C4E}.Release|x86.Build.0 = Release|Any CPU - {113EBBD2-E857-4CAF-9B53-7A8742CBCD4A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {113EBBD2-E857-4CAF-9B53-7A8742CBCD4A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {113EBBD2-E857-4CAF-9B53-7A8742CBCD4A}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU - {113EBBD2-E857-4CAF-9B53-7A8742CBCD4A}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU - {113EBBD2-E857-4CAF-9B53-7A8742CBCD4A}.Debug|x86.ActiveCfg = Debug|Any CPU - {113EBBD2-E857-4CAF-9B53-7A8742CBCD4A}.Debug|x86.Build.0 = Debug|Any CPU - {113EBBD2-E857-4CAF-9B53-7A8742CBCD4A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {113EBBD2-E857-4CAF-9B53-7A8742CBCD4A}.Release|Any CPU.Build.0 = Release|Any CPU - {113EBBD2-E857-4CAF-9B53-7A8742CBCD4A}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU - {113EBBD2-E857-4CAF-9B53-7A8742CBCD4A}.Release|Mixed Platforms.Build.0 = Release|Any CPU - {113EBBD2-E857-4CAF-9B53-7A8742CBCD4A}.Release|x86.ActiveCfg = Release|Any CPU - {113EBBD2-E857-4CAF-9B53-7A8742CBCD4A}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -198,8 +156,5 @@ Global {8967162D-4966-40A7-9970-395A206732AC} = {F141E2D0-F9B8-4ADB-A19A-7B6FF4CA19A1} {3F1CB08E-9FBD-4CAE-A78A-4AC43F24FC49} = {F141E2D0-F9B8-4ADB-A19A-7B6FF4CA19A1} {29C120E5-F682-4BFB-826B-040A594802CA} = {B54371FF-B920-46C8-8D55-6B19DBB43EBF} - {58B6443B-1278-4DF9-B7BB-DDF3BFFCF868} = {F141E2D0-F9B8-4ADB-A19A-7B6FF4CA19A1} - {8730E848-CA0F-4E0A-9A2F-BC22AD0B2C4E} = {F141E2D0-F9B8-4ADB-A19A-7B6FF4CA19A1} - {113EBBD2-E857-4CAF-9B53-7A8742CBCD4A} = {B54371FF-B920-46C8-8D55-6B19DBB43EBF} EndGlobalSection EndGlobal diff --git a/src/Microsoft.Framework.ConfigurationModel.UserSecrets/ConfigurationExtensions.cs b/src/Microsoft.Framework.ConfigurationModel.UserSecrets/ConfigurationExtensions.cs deleted file mode 100644 index e5a357f..0000000 --- a/src/Microsoft.Framework.ConfigurationModel.UserSecrets/ConfigurationExtensions.cs +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.IO; -using Microsoft.Framework.ConfigurationModel.UserSecrets; -using Microsoft.Framework.Internal; -using Microsoft.Framework.Runtime; -using Microsoft.Framework.Runtime.Infrastructure; - -namespace Microsoft.Framework.ConfigurationModel -{ - public static class ConfigurationExtensions - { - /// - /// Adds the user secrets configuration source. - /// - /// - /// - public static IConfigurationSourceRoot AddUserSecrets([NotNull]this IConfigurationSourceRoot configuration) - { - var appEnv = (IApplicationEnvironment)CallContextServiceLocator.Locator.ServiceProvider.GetService(typeof(IApplicationEnvironment)); - var secretPath = PathHelper.GetSecretsPath(appEnv.ApplicationBasePath); - - if (!File.Exists(secretPath)) - { - // TODO: Use the optional config add after that's available?. - return configuration; - } - - return configuration.AddJsonFile(secretPath); - } - - /// - /// Adds the user secrets configuration source with specified secrets id. - /// - /// - /// - public static IConfigurationSourceRoot AddUserSecrets([NotNull]this IConfigurationSourceRoot configuration, [NotNull]string userSecretsId) - { - var secretPath = PathHelper.GetSecretsPathFromSecretsId(userSecretsId); - - if (!File.Exists(secretPath)) - { - // TODO: Use the optional config add after that's available?. - return configuration; - } - - return configuration.AddJsonFile(secretPath); - } - } -} \ No newline at end of file diff --git a/src/Microsoft.Framework.ConfigurationModel.UserSecrets/Microsoft.Framework.ConfigurationModel.UserSecrets.xproj b/src/Microsoft.Framework.ConfigurationModel.UserSecrets/Microsoft.Framework.ConfigurationModel.UserSecrets.xproj deleted file mode 100644 index 3acb550..0000000 --- a/src/Microsoft.Framework.ConfigurationModel.UserSecrets/Microsoft.Framework.ConfigurationModel.UserSecrets.xproj +++ /dev/null @@ -1,20 +0,0 @@ - - - - 14.0 - $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) - - - - 58b6443b-1278-4df9-b7bb-ddf3bffcf868 - - - - - - - - 2.0 - - - \ No newline at end of file diff --git a/src/Microsoft.Framework.ConfigurationModel.UserSecrets/PathHelper.cs b/src/Microsoft.Framework.ConfigurationModel.UserSecrets/PathHelper.cs deleted file mode 100644 index b988837..0000000 --- a/src/Microsoft.Framework.ConfigurationModel.UserSecrets/PathHelper.cs +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.IO; -using Microsoft.Framework.Internal; -using Newtonsoft.Json.Linq; - -namespace Microsoft.Framework.ConfigurationModel.UserSecrets -{ - public class PathHelper - { - private const string Secrets_File_Name = "secrets.json"; - - public static string GetSecretsPath([NotNull]string projectPath) - { - var projectFilePath = Path.Combine(projectPath, "project.json"); - - if (!File.Exists(projectFilePath)) - { - throw new InvalidOperationException( - string.Format(Resources.Error_Missing_Project_Json, projectFilePath)); - } - - var obj = JObject.Parse(File.ReadAllText(projectFilePath)); - var userSecretsId = obj.Value("userSecretsId"); - - if (string.IsNullOrEmpty(userSecretsId)) - { - throw new InvalidOperationException( - string.Format(Resources.Error_Missing_UserSecretId_In_Project_Json, projectFilePath)); - } - - return GetSecretsPathFromSecretsId(userSecretsId); - } - - public static string GetSecretsPathFromSecretsId([NotNull]string userSecretsId) - { - var badCharIndex = userSecretsId.IndexOfAny(Path.GetInvalidPathChars()); - if (badCharIndex != -1) - { - throw new InvalidOperationException( - string.Format( - Resources.Error_Invalid_Character_In_UserSecrets_Id, - userSecretsId[badCharIndex], - badCharIndex)); - } - - var root = Environment.GetEnvironmentVariable("APPDATA") ?? // On Windows it goes to %APPDATA%\Microsoft\UserSecrets\ - Environment.GetEnvironmentVariable("HOME"); // On Mac/Linux it goes to ~/.microsoft/usersecrets/ - - if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("APPDATA"))) - { - return Path.Combine(root, "Microsoft", "UserSecrets", userSecretsId, Secrets_File_Name); - } - else - { - return Path.Combine(root, ".microsoft", "usersecrets", userSecretsId, Secrets_File_Name); - } - } - } -} \ No newline at end of file diff --git a/src/Microsoft.Framework.ConfigurationModel.UserSecrets/Properties/Resources.Designer.cs b/src/Microsoft.Framework.ConfigurationModel.UserSecrets/Properties/Resources.Designer.cs deleted file mode 100644 index 13c3c6f..0000000 --- a/src/Microsoft.Framework.ConfigurationModel.UserSecrets/Properties/Resources.Designer.cs +++ /dev/null @@ -1,78 +0,0 @@ -// -namespace Microsoft.Framework.ConfigurationModel.UserSecrets -{ - using System.Globalization; - using System.Reflection; - using System.Resources; - - internal static class Resources - { - private static readonly ResourceManager _resourceManager - = new ResourceManager("Microsoft.Framework.ConfigurationModel.UserSecrets.Resources", typeof(Resources).GetTypeInfo().Assembly); - - /// - /// Invalid character '{0}' found in 'userSecretsId' value at index '{1}'. - /// - internal static string Error_Invalid_Character_In_UserSecrets_Id - { - get { return GetString("Error_Invalid_Character_In_UserSecrets_Id"); } - } - - /// - /// Invalid character '{0}' found in 'userSecretsId' value at index '{1}'. - /// - internal static string FormatError_Invalid_Character_In_UserSecrets_Id(object p0, object p1) - { - return string.Format(CultureInfo.CurrentCulture, GetString("Error_Invalid_Character_In_UserSecrets_Id"), p0, p1); - } - - /// - /// Unable to locate a project.json at '{0}'. - /// - internal static string Error_Missing_Project_Json - { - get { return GetString("Error_Missing_Project_Json"); } - } - - /// - /// Unable to locate a project.json at '{0}'. - /// - internal static string FormatError_Missing_Project_Json(object p0) - { - return string.Format(CultureInfo.CurrentCulture, GetString("Error_Missing_Project_Json"), p0); - } - - /// - /// Missing 'userSecretsId' in '{0}'. - /// - internal static string Error_Missing_UserSecretId_In_Project_Json - { - get { return GetString("Error_Missing_UserSecretId_In_Project_Json"); } - } - - /// - /// Missing 'userSecretsId' in '{0}'. - /// - internal static string FormatError_Missing_UserSecretId_In_Project_Json(object p0) - { - return string.Format(CultureInfo.CurrentCulture, GetString("Error_Missing_UserSecretId_In_Project_Json"), p0); - } - - private static string GetString(string name, params string[] formatterNames) - { - var value = _resourceManager.GetString(name); - - System.Diagnostics.Debug.Assert(value != null); - - if (formatterNames != null) - { - for (var i = 0; i < formatterNames.Length; i++) - { - value = value.Replace("{" + formatterNames[i] + "}", "{" + i + "}"); - } - } - - return value; - } - } -} diff --git a/src/Microsoft.Framework.ConfigurationModel.UserSecrets/Resources.resx b/src/Microsoft.Framework.ConfigurationModel.UserSecrets/Resources.resx deleted file mode 100644 index eedd189..0000000 --- a/src/Microsoft.Framework.ConfigurationModel.UserSecrets/Resources.resx +++ /dev/null @@ -1,129 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - Invalid character '{0}' found in 'userSecretsId' value at index '{1}'. - - - Unable to locate a project.json at '{0}'. - - - Missing 'userSecretsId' in '{0}'. - - \ No newline at end of file diff --git a/src/Microsoft.Framework.ConfigurationModel.UserSecrets/project.json b/src/Microsoft.Framework.ConfigurationModel.UserSecrets/project.json deleted file mode 100644 index 7be6fbc..0000000 --- a/src/Microsoft.Framework.ConfigurationModel.UserSecrets/project.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "version": "1.0.0-*", - "description": "ASP.NET 5 Configuration extensions to load user secrets.", - "dependencies": { - "Microsoft.Framework.ConfigurationModel.Json": "1.0.0-*", - "Microsoft.Framework.NotNullAttribute.Internal": { "type": "build", "version": "1.0.0-*" } - }, - "frameworks": { - "dnx451": { }, - "dnxcore50": { - "dependencies": { - "System.Runtime": "4.0.20-beta-*" - } - } - } -} \ No newline at end of file diff --git a/src/SecretManager/CommandOutputLogger.cs b/src/SecretManager/CommandOutputLogger.cs deleted file mode 100644 index 311ccf0..0000000 --- a/src/SecretManager/CommandOutputLogger.cs +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.Framework.Logging; -using Microsoft.Framework.Runtime.Common.CommandLine; - -namespace SecretManager -{ - /// - /// Logger to print formatted command output. - /// - public class CommandOutputLogger : ILogger - { - private readonly CommandOutputProvider _provider; - - public CommandOutputLogger(CommandOutputProvider commandOutputProvider) - { - _provider = commandOutputProvider; - } - - public IDisposable BeginScope(object state) - { - throw new NotImplementedException(); - } - - public bool IsEnabled(LogLevel logLevel) - { - if (logLevel < _provider.LogLevel) - { - return false; - } - - return true; - } - - public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func formatter) - { - if (IsEnabled(logLevel)) - { - AnsiConsole.Output.WriteLine(string.Format("{0}: {1}", Caption(logLevel), formatter(state, exception))); - } - } - - private string Caption(LogLevel logLevel) - { - switch (logLevel) - { - case LogLevel.Debug: return "\x1b[35mdebug\x1b[39m"; - case LogLevel.Verbose: return "\x1b[35mverbose\x1b[39m"; - case LogLevel.Information: return "\x1b[32minfo\x1b[39m"; - case LogLevel.Warning: return "\x1b[33mwarn\x1b[39m"; - case LogLevel.Error: return "\x1b[31mfail\x1b[39m"; - case LogLevel.Critical: return "\x1b[31mcritical\x1b[39m"; - } - - throw new Exception("Unknown LogLevel"); - } - } -} \ No newline at end of file diff --git a/src/SecretManager/CommandOutputProvider.cs b/src/SecretManager/CommandOutputProvider.cs deleted file mode 100644 index 840eed3..0000000 --- a/src/SecretManager/CommandOutputProvider.cs +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using Microsoft.Framework.Logging; - -namespace SecretManager -{ - public class CommandOutputProvider : ILoggerProvider - { - public ILogger CreateLogger(string name) - { - return new CommandOutputLogger(this); - } - - public LogLevel LogLevel { get; set; } = LogLevel.Information; - } -} \ No newline at end of file diff --git a/src/SecretManager/Program.cs b/src/SecretManager/Program.cs deleted file mode 100644 index 96f4bf6..0000000 --- a/src/SecretManager/Program.cs +++ /dev/null @@ -1,225 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.IO; -using Microsoft.Framework.ConfigurationModel.UserSecrets; -using Microsoft.Framework.Logging; -using Microsoft.Framework.Runtime.Common.CommandLine; -using Newtonsoft.Json.Linq; - -namespace SecretManager -{ - public class Program - { - private ILogger _logger; - private CommandOutputProvider _loggerProvider; - - public Program() - { - var loggerFactory = new LoggerFactory(); - CommandOutputProvider = new CommandOutputProvider(); - loggerFactory.AddProvider(CommandOutputProvider); - Logger = loggerFactory.CreateLogger(); - } - - public ILogger Logger - { - get { return _logger; } - set - { - if (value == null) - { - throw new ArgumentNullException("value"); - } - - _logger = value; - } - } - - public CommandOutputProvider CommandOutputProvider - { - get { return _loggerProvider; } - set - { - if (value == null) - { - throw new ArgumentNullException("value"); - } - - _loggerProvider = value; - } - } - - public int Main(string[] args) - { - try - { - var app = new CommandLineApplication(); - app.Name = "user-secret"; - app.Description = "Manages user secrets"; - - app.HelpOption("-?|-h|--help"); - var optVerbose = app.Option("-v|--verbose", "Verbose output", CommandOptionType.NoValue); - - app.Command("set", c => - { - c.Description = "Sets the user secret to the specified value"; - - var optionProject = c.Option("-p|--project ", "Path to project, default is current directory", CommandOptionType.SingleValue); - var keyArg = c.Argument("[name]", "Name of the secret"); - var valueArg = c.Argument("[value]", "Value of the secret"); - c.HelpOption("-?|-h|--help"); - - c.OnExecute(() => - { - var projectPath = optionProject.Value() ?? Directory.GetCurrentDirectory(); - - if (optVerbose.HasValue()) - { - CommandOutputProvider.LogLevel = LogLevel.Verbose; - } - - ProcessSecretFile(projectPath, secrets => - { - secrets[keyArg.Value] = valueArg.Value; - }); - - Logger.LogInformation(Resources.Message_Saved_Secret, keyArg.Value, valueArg.Value); - return 0; - }); - }); - - app.Command("remove", c => - { - c.Description = "Removes the specified user secret"; - - var optionProject = c.Option("-p|--project ", "Path to project, default is current directory", CommandOptionType.SingleValue); - var keyArg = c.Argument("[name]", "Name of the secret"); - c.HelpOption("-?|-h|--help"); - - c.OnExecute(() => - { - var projectPath = optionProject.Value() ?? Directory.GetCurrentDirectory(); - - if (optVerbose.HasValue()) - { - CommandOutputProvider.LogLevel = LogLevel.Verbose; - } - - ProcessSecretFile(projectPath, secrets => - { - if (secrets[keyArg.Value] == null) - { - Logger.LogWarning(Resources.Error_Missing_Secret, keyArg.Value); - } - else - { - secrets.Remove(keyArg.Value); - } - }); - - return 0; - }); - }); - - app.Command("list", c => - { - c.Description = "Lists all the application secrets"; - - var optionProject = c.Option("-p|--project ", "Path to project, default is current directory", CommandOptionType.SingleValue); - c.HelpOption("-?|-h|--help"); - - c.OnExecute(() => - { - var projectPath = optionProject.Value() ?? Directory.GetCurrentDirectory(); - - if (optVerbose.HasValue()) - { - CommandOutputProvider.LogLevel = LogLevel.Verbose; - } - - ProcessSecretFile(projectPath, secrets => - { - PrintAll(secrets); - }, - persist: false); - return 0; - }); - }); - - app.Command("clear", c => - { - c.Description = "Deletes all the application secrets"; - - var optionProject = c.Option("-p|--project ", "Path to project, default is current directory", CommandOptionType.SingleValue); - c.HelpOption("-?|-h|--help"); - - c.OnExecute(() => - { - var projectPath = optionProject.Value() ?? Directory.GetCurrentDirectory(); - - if (optVerbose.HasValue()) - { - CommandOutputProvider.LogLevel = LogLevel.Verbose; - } - - ProcessSecretFile(projectPath, secrets => - { - secrets.RemoveAll(); - }); - - return 0; - }); - }); - - // Show help information if no subcommand/option was specified. - app.OnExecute(() => - { - app.ShowHelp(); - return 2; - }); - - return app.Execute(args); - } - catch (Exception exception) - { - Logger.LogCritical(Resources.Error_Command_Failed, exception.Message); - return 1; - } - } - - private void PrintAll(JObject secrets) - { - if (secrets.Count == 0) - { - Logger.LogInformation(Resources.Error_No_Secrets_Found); - } - else - { - foreach (var secret in secrets) - { - Logger.LogInformation(Resources.Message_Secret_Value_Format, secret.Key, secret.Value); - } - } - } - - private void ProcessSecretFile(string projectPath, Action observer, bool persist = true) - { - Logger.LogVerbose(Resources.Message_Project_File_Path, projectPath); - var secretsFilePath = PathHelper.GetSecretsPath(projectPath); - Logger.LogVerbose(Resources.Message_Secret_File_Path, secretsFilePath); - var secretObj = File.Exists(secretsFilePath) ? - JObject.Parse(File.ReadAllText(secretsFilePath)) : - new JObject(); - - observer(secretObj); - - if (persist) - { - Directory.CreateDirectory(Path.GetDirectoryName(secretsFilePath)); - File.WriteAllText(secretsFilePath, secretObj.ToString()); - } - } - } -} \ No newline at end of file diff --git a/src/SecretManager/Properties/AssemblyInfo.cs b/src/SecretManager/Properties/AssemblyInfo.cs deleted file mode 100644 index 883c90a..0000000 --- a/src/SecretManager/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,6 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Runtime.CompilerServices; - -[assembly: InternalsVisibleTo("SecretManager.Tests")] \ No newline at end of file diff --git a/src/SecretManager/Properties/Resources.Designer.cs b/src/SecretManager/Properties/Resources.Designer.cs deleted file mode 100644 index b854091..0000000 --- a/src/SecretManager/Properties/Resources.Designer.cs +++ /dev/null @@ -1,142 +0,0 @@ -// -namespace SecretManager -{ - using System.Globalization; - using System.Reflection; - using System.Resources; - - internal static class Resources - { - private static readonly ResourceManager _resourceManager - = new ResourceManager("SecretManager.Resources", typeof(Resources).GetTypeInfo().Assembly); - - /// - /// Command failed : {message} - /// - internal static string Error_Command_Failed - { - get { return GetString("Error_Command_Failed"); } - } - - /// - /// Command failed : {message} - /// - internal static string FormatError_Command_Failed(object message) - { - return string.Format(CultureInfo.CurrentCulture, GetString("Error_Command_Failed", "message"), message); - } - - /// - /// Cannot find '{key}' in the secret store. - /// - internal static string Error_Missing_Secret - { - get { return GetString("Error_Missing_Secret"); } - } - - /// - /// Cannot find '{key}' in the secret store. - /// - internal static string FormatError_Missing_Secret(object key) - { - return string.Format(CultureInfo.CurrentCulture, GetString("Error_Missing_Secret", "key"), key); - } - - /// - /// No secrets configured for this application. - /// - internal static string Error_No_Secrets_Found - { - get { return GetString("Error_No_Secrets_Found"); } - } - - /// - /// No secrets configured for this application. - /// - internal static string FormatError_No_Secrets_Found() - { - return GetString("Error_No_Secrets_Found"); - } - - /// - /// Project file path {project}. - /// - internal static string Message_Project_File_Path - { - get { return GetString("Message_Project_File_Path"); } - } - - /// - /// Project file path {project}. - /// - internal static string FormatMessage_Project_File_Path(object project) - { - return string.Format(CultureInfo.CurrentCulture, GetString("Message_Project_File_Path", "project"), project); - } - - /// - /// Successfully saved {key} = {value} to the secret store. - /// - internal static string Message_Saved_Secret - { - get { return GetString("Message_Saved_Secret"); } - } - - /// - /// Successfully saved {key} = {value} to the secret store. - /// - internal static string FormatMessage_Saved_Secret(object key, object value) - { - return string.Format(CultureInfo.CurrentCulture, GetString("Message_Saved_Secret", "key", "value"), key, value); - } - - /// - /// Secrets file path {secretsFilePath}. - /// - internal static string Message_Secret_File_Path - { - get { return GetString("Message_Secret_File_Path"); } - } - - /// - /// Secrets file path {secretsFilePath}. - /// - internal static string FormatMessage_Secret_File_Path(object secretsFilePath) - { - return string.Format(CultureInfo.CurrentCulture, GetString("Message_Secret_File_Path", "secretsFilePath"), secretsFilePath); - } - - /// - /// {key} = {value} - /// - internal static string Message_Secret_Value_Format - { - get { return GetString("Message_Secret_Value_Format"); } - } - - /// - /// {key} = {value} - /// - internal static string FormatMessage_Secret_Value_Format(object key, object value) - { - return string.Format(CultureInfo.CurrentCulture, GetString("Message_Secret_Value_Format", "key", "value"), key, value); - } - - private static string GetString(string name, params string[] formatterNames) - { - var value = _resourceManager.GetString(name); - - System.Diagnostics.Debug.Assert(value != null); - - if (formatterNames != null) - { - for (var i = 0; i < formatterNames.Length; i++) - { - value = value.Replace("{" + formatterNames[i] + "}", "{" + i + "}"); - } - } - - return value; - } - } -} diff --git a/src/SecretManager/Resources.resx b/src/SecretManager/Resources.resx deleted file mode 100644 index 13c953c..0000000 --- a/src/SecretManager/Resources.resx +++ /dev/null @@ -1,141 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - Command failed : {message} - - - Cannot find '{key}' in the secret store. - - - No secrets configured for this application. - - - Project file path {project}. - - - Successfully saved {key} = {value} to the secret store. - - - Secrets file path {secretsFilePath}. - - - {key} = {value} - - \ No newline at end of file diff --git a/src/SecretManager/SecretManager.xproj b/src/SecretManager/SecretManager.xproj deleted file mode 100644 index 83f72ab..0000000 --- a/src/SecretManager/SecretManager.xproj +++ /dev/null @@ -1,20 +0,0 @@ - - - - 14.0 - $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) - - - - 8730e848-ca0f-4e0a-9a2f-bc22ad0b2c4e - - - - - - - - 2.0 - - - \ No newline at end of file diff --git a/src/SecretManager/project.json b/src/SecretManager/project.json deleted file mode 100644 index 5aaf626..0000000 --- a/src/SecretManager/project.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "version": "1.0.0-*", - "description": "ASP.NET 5 tool to manage user secrets.", - "dependencies": { - "Microsoft.Framework.ConfigurationModel.UserSecrets": "1.0.0-*", - "Microsoft.Framework.CommandLineUtils": { "version": "1.0.0-*", "type": "build" }, - "Microsoft.Framework.Logging": "1.0.0-*", - "Newtonsoft.Json": "6.0.6" - }, - "commands": { - "user-secret": "SecretManager" - }, - "userSecretsId": "testuserSecretsId", - "frameworks": { - "dnx451": { }, - "dnxcore50": { - "dependencies": { - "System.Console": "4.0.0-beta-*" - } - } - } -} \ No newline at end of file diff --git a/test/SecretManager.Tests/PathHelperTests.cs b/test/SecretManager.Tests/PathHelperTests.cs deleted file mode 100644 index 493f215..0000000 --- a/test/SecretManager.Tests/PathHelperTests.cs +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.IO; -using Microsoft.Framework.ConfigurationModel.UserSecrets; -using Xunit; - -namespace SecretManager.Tests -{ - public class PathHelperTests - { - [Fact] - public void Gives_Correct_Secret_Path() - { - string userSecretsId; - var projectPath = UserSecretHelper.GetTempSecretProject(out userSecretsId); - var actualSecretPath = PathHelper.GetSecretsPath(projectPath); - - var root = Environment.GetEnvironmentVariable("APPDATA") ?? // On Windows it goes to %APPDATA%\Microsoft\UserSecrets\ - Environment.GetEnvironmentVariable("HOME"); // On Mac/Linux it goes to ~/.microsoft/usersecrets/ - - var expectedSecretPath = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("APPDATA"))? - Path.Combine(root, "Microsoft", "UserSecrets", userSecretsId, "secrets.json") : - Path.Combine(root, ".microsoft", "usersecrets", userSecretsId, "secrets.json"); - - Assert.Equal(expectedSecretPath, actualSecretPath); - - UserSecretHelper.DeleteTempSecretProject(projectPath); - } - - [Fact] - public void Throws_If_Project_Json_Not_Found() - { - var projectPath = UserSecretHelper.GetTempSecretProject(); - File.Delete(Path.Combine(projectPath, "project.json")); - - Assert.Throws(() => - { - PathHelper.GetSecretsPath(projectPath); - }); - - UserSecretHelper.DeleteTempSecretProject(projectPath); - } - - [Fact] - public void Throws_If_Project_Json_Does_Not_Contain_UserSecretId() - { - var projectPath = UserSecretHelper.GetTempSecretProject(); - File.WriteAllText(Path.Combine(projectPath, "project.json"), "{}"); - - Assert.Throws(() => - { - PathHelper.GetSecretsPath(projectPath); - }); - - UserSecretHelper.DeleteTempSecretProject(projectPath); - } - - [Fact] - public void Throws_If_UserSecretId_Contains_Invalid_Characters() - { - var projectPath = UserSecretHelper.GetTempSecretProject(); - - foreach (var character in Path.GetInvalidPathChars()) - { - UserSecretHelper.SetTempSecretInProject(projectPath, "Test" + character); - Assert.Throws(() => - { - PathHelper.GetSecretsPath(projectPath); - }); - } - - UserSecretHelper.DeleteTempSecretProject(projectPath); - } - } -} \ No newline at end of file diff --git a/test/SecretManager.Tests/SecretManager.Tests.xproj b/test/SecretManager.Tests/SecretManager.Tests.xproj deleted file mode 100644 index 8ca18b2..0000000 --- a/test/SecretManager.Tests/SecretManager.Tests.xproj +++ /dev/null @@ -1,20 +0,0 @@ - - - - 14.0 - $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) - - - - - 113ebbd2-e857-4caf-9b53-7a8742cbcd4a - SecretManager.Tests - ..\..\artifacts\obj\$(MSBuildProjectName) - ..\..\artifacts\bin\$(MSBuildProjectName)\ - - - - 2.0 - - - \ No newline at end of file diff --git a/test/SecretManager.Tests/SecretManagerTests.cs b/test/SecretManager.Tests/SecretManagerTests.cs deleted file mode 100644 index 424ff73..0000000 --- a/test/SecretManager.Tests/SecretManagerTests.cs +++ /dev/null @@ -1,247 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using System.IO; -using Microsoft.Framework.ConfigurationModel.UserSecrets; -using Xunit; - -namespace SecretManager.Tests -{ - public class SecretManagerTests - { - [Fact] - public void SetSecret_With_ProjectPath_As_CommandLine_Parameter() - { - SetSecrets(fromCurrentDirectory: false); - } - - [Fact] - public void SetSecret_From_CurrentDirectory() - { - var backUpCurrentDirectory = Directory.GetCurrentDirectory(); - - try - { - SetSecrets(fromCurrentDirectory: true); - } - catch (Exception) - { - Directory.SetCurrentDirectory(backUpCurrentDirectory); - throw; - } - } - - private void SetSecrets(bool fromCurrentDirectory) - { - var secrets = new KeyValuePair[] - { - new KeyValuePair("key1", Guid.NewGuid().ToString()), - new KeyValuePair("Facebook:AppId", Guid.NewGuid().ToString()), - new KeyValuePair(@"key-@\/.~123!#$%^&*())-+==", @"key-@\/.~123!#$%^&*())-+=="), - new KeyValuePair("key2", string.Empty) - }; - - var projectPath = UserSecretHelper.GetTempSecretProject(); - if (fromCurrentDirectory) - { - Directory.SetCurrentDirectory(projectPath); // Point current directory to the project.json directory. - } - - var logger = new TestLogger(); - var secretManager = new Program() { Logger = logger }; - - foreach (var secret in secrets) - { - var parameters = fromCurrentDirectory ? - new string[] { "set", secret.Key, secret.Value } : - new string[] { "set", secret.Key, secret.Value, "-p", projectPath }; - secretManager.Main(parameters); - } - - Assert.Equal(4, logger.Messages.Count); - - foreach (var keyValue in secrets) - { - Assert.Contains( - string.Format("Successfully saved {0} = {1} to the secret store.", keyValue.Key, keyValue.Value), - logger.Messages); - } - - logger.Messages.Clear(); - var args = fromCurrentDirectory ? - new string[] { "list" } : new string[] { "list", "-p", projectPath }; - secretManager.Main(args); - Assert.Equal(4, logger.Messages.Count); - foreach (var keyValue in secrets) - { - Assert.Contains( - string.Format("{0} = {1}", keyValue.Key, keyValue.Value), - logger.Messages); - } - - // Remove secrets. - logger.Messages.Clear(); - foreach (var secret in secrets) - { - var parameters = fromCurrentDirectory ? - new string[] { "remove", secret.Key } : - new string[] { "remove", secret.Key, "-p", projectPath }; - secretManager.Main(parameters); - } - - // Verify secrets are removed. - logger.Messages.Clear(); - args = fromCurrentDirectory ? - new string[] { "list" } : new string[] { "list", "-p", projectPath }; - secretManager.Main(args); - Assert.Equal(1, logger.Messages.Count); - Assert.Contains(Resources.Error_No_Secrets_Found, logger.Messages); - - UserSecretHelper.DeleteTempSecretProject(projectPath); - } - - [Fact] - public void SetSecret_Update_Existing_Secret() - { - var projectPath = UserSecretHelper.GetTempSecretProject(); - var logger = new TestLogger(); - var secretManager = new Program() { Logger = logger }; - - secretManager.Main(new string[] { "set", "secret1", "value1", "-p", projectPath }); - Assert.Equal(1, logger.Messages.Count); - Assert.Contains("Successfully saved secret1 = value1 to the secret store.", logger.Messages); - secretManager.Main(new string[] { "set", "secret1", "value2", "-p", projectPath }); - Assert.Equal(2, logger.Messages.Count); - Assert.Contains("Successfully saved secret1 = value2 to the secret store.", logger.Messages); - - logger.Messages.Clear(); - - secretManager.Main(new string[] { "list", "-p", projectPath }); - Assert.Equal(1, logger.Messages.Count); - Assert.Contains("secret1 = value2", logger.Messages); - - UserSecretHelper.DeleteTempSecretProject(projectPath); - } - - [Fact] - public void SetSecret_With_Verbose_Flag() - { - var projectPath = UserSecretHelper.GetTempSecretProject(); - var logger = new TestLogger(verbose: true); - var secretManager = new Program() { Logger = logger }; - - secretManager.Main(new string[] { "-v", "set", "secret1", "value1", "-p", projectPath }); - Assert.Equal(3, logger.Messages.Count); - Assert.Contains(string.Format("Project file path {0}.", projectPath), logger.Messages); - Assert.Contains(string.Format("Secrets file path {0}.", PathHelper.GetSecretsPath(projectPath)), logger.Messages); - Assert.Contains("Successfully saved secret1 = value1 to the secret store.", logger.Messages); - logger.Messages.Clear(); - - secretManager.Main(new string[] { "-v", "list", "-p", projectPath }); - Assert.Equal(3, logger.Messages.Count); - Assert.Contains(string.Format("Project file path {0}.", projectPath), logger.Messages); - Assert.Contains(string.Format("Secrets file path {0}.", PathHelper.GetSecretsPath(projectPath)), logger.Messages); - Assert.Contains("secret1 = value1", logger.Messages); - - UserSecretHelper.DeleteTempSecretProject(projectPath); - } - - [Fact] - public void Remove_Non_Existing_Secret() - { - var projectPath = UserSecretHelper.GetTempSecretProject(); - var logger = new TestLogger(); - var secretManager = new Program() { Logger = logger }; - secretManager.Main(new string[] { "remove", "secret1", "-p", projectPath }); - Assert.Equal(1, logger.Messages.Count); - Assert.Contains("Cannot find 'secret1' in the secret store.", logger.Messages); - } - - [Fact] - public void List_Empty_Secrets_File() - { - var projectPath = UserSecretHelper.GetTempSecretProject(); - var logger = new TestLogger(); - var secretManager = new Program() { Logger = logger }; - secretManager.Main(new string[] { "list", "-p", projectPath }); - Assert.Equal(1, logger.Messages.Count); - Assert.Contains(Resources.Error_No_Secrets_Found, logger.Messages); - } - - [Fact] - public void Clear_All_Secrets_With_ProjectPath_As_Parameter() - { - Clear_Secrets(fromCurrentDirectory: false); - } - - [Fact] - public void Clear_All_Secrets_From_Current_Directory() - { - Clear_Secrets(fromCurrentDirectory: true); - } - - private void Clear_Secrets(bool fromCurrentDirectory) - { - var projectPath = UserSecretHelper.GetTempSecretProject(); - if (fromCurrentDirectory) - { - Directory.SetCurrentDirectory(projectPath); // Point current directory to the project.json directory. - } - - var logger = new TestLogger(); - var secretManager = new Program() { Logger = logger }; - - var secrets = new KeyValuePair[] - { - new KeyValuePair("key1", Guid.NewGuid().ToString()), - new KeyValuePair("Facebook:AppId", Guid.NewGuid().ToString()), - new KeyValuePair(@"key-@\/.~123!#$%^&*())-+==", @"key-@\/.~123!#$%^&*())-+=="), - new KeyValuePair("key2", string.Empty) - }; - - foreach (var secret in secrets) - { - var parameters = fromCurrentDirectory ? - new string[] { "set", secret.Key, secret.Value } : - new string[] { "set", secret.Key, secret.Value, "-p", projectPath }; - secretManager.Main(parameters); - } - - Assert.Equal(4, logger.Messages.Count); - - foreach (var keyValue in secrets) - { - Assert.Contains( - string.Format("Successfully saved {0} = {1} to the secret store.", keyValue.Key, keyValue.Value), - logger.Messages); - } - - // Verify secrets are persisted. - logger.Messages.Clear(); - var args = fromCurrentDirectory ? - new string[] { "list" } : - new string[] { "list", "-p", projectPath }; - secretManager.Main(args); - Assert.Equal(4, logger.Messages.Count); - foreach (var keyValue in secrets) - { - Assert.Contains( - string.Format("{0} = {1}", keyValue.Key, keyValue.Value), - logger.Messages); - } - - // Clear secrets. - logger.Messages.Clear(); - args = fromCurrentDirectory ? new string[] { "clear" } : new string[] { "clear", "-p", projectPath }; - secretManager.Main(args); - Assert.Equal(0, logger.Messages.Count); - - args = fromCurrentDirectory ? new string[] { "list" } : new string[] { "list", "-p", projectPath }; - secretManager.Main(args); - Assert.Equal(1, logger.Messages.Count); - Assert.Contains(Resources.Error_No_Secrets_Found, logger.Messages); - } - } -} \ No newline at end of file diff --git a/test/SecretManager.Tests/TestLogger.cs b/test/SecretManager.Tests/TestLogger.cs deleted file mode 100644 index bd113fb..0000000 --- a/test/SecretManager.Tests/TestLogger.cs +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.Framework.Logging; - -namespace SecretManager.Tests -{ - public class TestLogger : ILogger - { - private readonly CommandOutputLogger _commandOutputLogger; - - public TestLogger(bool verbose = false) - { - var commandOutputProvider = new CommandOutputProvider(); - if (verbose) - { - commandOutputProvider.LogLevel = LogLevel.Verbose; - } - - _commandOutputLogger = new CommandOutputLogger(commandOutputProvider); - } - - public List Messages { get; set; } = new List(); - - public IDisposable BeginScope(object state) - { - throw new NotImplementedException(); - } - - public bool IsEnabled(LogLevel logLevel) - { - return _commandOutputLogger.IsEnabled(logLevel); - } - - public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func formatter) - { - if (IsEnabled(logLevel)) - { - Messages.Add(formatter(state, exception)); - } - } - } -} \ No newline at end of file diff --git a/test/SecretManager.Tests/UserSecretHelper.cs b/test/SecretManager.Tests/UserSecretHelper.cs deleted file mode 100644 index 51a2102..0000000 --- a/test/SecretManager.Tests/UserSecretHelper.cs +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.IO; -using Newtonsoft.Json; - -namespace SecretManager.Tests -{ - public class UserSecretHelper - { - internal static string GetTempSecretProject() - { - string userSecretsId; - return GetTempSecretProject(out userSecretsId); - } - - internal static string GetTempSecretProject(out string userSecretsId) - { - var projectPath = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString())); - userSecretsId = Guid.NewGuid().ToString(); - File.WriteAllText( - Path.Combine(projectPath.FullName, "project.json"), - string.Format("{{\"userSecretsId\": {0}}}", JsonConvert.ToString(userSecretsId))); - return projectPath.FullName; - } - - internal static void SetTempSecretInProject(string projectPath, string userSecretsId) - { - File.WriteAllText( - Path.Combine(projectPath, "project.json"), - string.Format("{{\"userSecretsId\": {0}}}", JsonConvert.ToString(userSecretsId))); - } - - internal static void DeleteTempSecretProject(string projectPath) - { - try - { - Directory.Delete(projectPath, true); - } - catch (Exception) - { - // Ignore failures. - } - } - } -} \ No newline at end of file diff --git a/test/SecretManager.Tests/project.json b/test/SecretManager.Tests/project.json deleted file mode 100644 index 469a644..0000000 --- a/test/SecretManager.Tests/project.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "dependencies": { - "SecretManager": "1.0.0-*", - "xunit.runner.aspnet": "2.0.0-aspnet-*" - }, - "frameworks": { - "dnx451": { }, - "dnxcore50": { } - }, - "commands": { - "test": "xunit.runner.aspnet" - } -} \ No newline at end of file