Permalink
Browse files

Move UserSecrets extension into current repo (#513)

  • Loading branch information...
natemcmaster committed Sep 19, 2016
1 parent 1376ae0 commit 9518bec5b69e305315309a87213e0153cb26de7f
@@ -52,6 +52,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{AB01
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "KeyVaultSample", "samples\KeyVaultSample\KeyVaultSample.xproj", "{7AAC49A7-939A-40B1-A86A-705464B4667D}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Extensions.Configuration.UserSecrets", "src\Microsoft.Extensions.Configuration.UserSecrets\Microsoft.Extensions.Configuration.UserSecrets.xproj", "{58B6443B-1278-4DF9-B7BB-DDF3BFFCF868}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Extensions.Configuration.UserSecrets.Test", "test\Microsoft.Extensions.Configuration.UserSecrets.Test\Microsoft.Extensions.Configuration.UserSecrets.Test.xproj", "{AC7FAD2A-5763-404D-B0FC-3CCA81A16B0A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -312,6 +316,30 @@ Global
{7AAC49A7-939A-40B1-A86A-705464B4667D}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{7AAC49A7-939A-40B1-A86A-705464B4667D}.Release|x86.ActiveCfg = Release|Any CPU
{7AAC49A7-939A-40B1-A86A-705464B4667D}.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
{AC7FAD2A-5763-404D-B0FC-3CCA81A16B0A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AC7FAD2A-5763-404D-B0FC-3CCA81A16B0A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AC7FAD2A-5763-404D-B0FC-3CCA81A16B0A}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{AC7FAD2A-5763-404D-B0FC-3CCA81A16B0A}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{AC7FAD2A-5763-404D-B0FC-3CCA81A16B0A}.Debug|x86.ActiveCfg = Debug|Any CPU
{AC7FAD2A-5763-404D-B0FC-3CCA81A16B0A}.Debug|x86.Build.0 = Debug|Any CPU
{AC7FAD2A-5763-404D-B0FC-3CCA81A16B0A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AC7FAD2A-5763-404D-B0FC-3CCA81A16B0A}.Release|Any CPU.Build.0 = Release|Any CPU
{AC7FAD2A-5763-404D-B0FC-3CCA81A16B0A}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{AC7FAD2A-5763-404D-B0FC-3CCA81A16B0A}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{AC7FAD2A-5763-404D-B0FC-3CCA81A16B0A}.Release|x86.ActiveCfg = Release|Any CPU
{AC7FAD2A-5763-404D-B0FC-3CCA81A16B0A}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -339,5 +367,7 @@ Global
{A538F609-E902-40CE-8459-4248F9F63558} = {F141E2D0-F9B8-4ADB-A19A-7B6FF4CA19A1}
{DA9C1F35-3F92-4F3A-B3B1-A62CCE626D48} = {B54371FF-B920-46C8-8D55-6B19DBB43EBF}
{7AAC49A7-939A-40B1-A86A-705464B4667D} = {AB015580-541D-4E2D-B904-E71F8582CC68}
{58B6443B-1278-4DF9-B7BB-DDF3BFFCF868} = {F141E2D0-F9B8-4ADB-A19A-7B6FF4CA19A1}
{AC7FAD2A-5763-404D-B0FC-3CCA81A16B0A} = {B54371FF-B920-46C8-8D55-6B19DBB43EBF}
EndGlobalSection
EndGlobal
@@ -13,6 +13,7 @@
"Microsoft.Extensions.Configuration.FileExtensions": { },
"Microsoft.Extensions.Configuration.Ini": { },
"Microsoft.Extensions.Configuration.Json": { },
"Microsoft.Extensions.Configuration.UserSecrets": { },
"Microsoft.Extensions.Configuration.Xml": { }
}
},
@@ -0,0 +1,62 @@
// Copyright (c) .NET Foundation. 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.Extensions.Configuration.UserSecrets;
using Microsoft.Extensions.FileProviders;
namespace Microsoft.Extensions.Configuration
{
public static class ConfigurationExtensions
{
private const string Secrets_File_Name = "secrets.json";
/// <summary>
/// Adds the user secrets configuration source.
/// </summary>
/// <param name="configuration"></param>
/// <returns></returns>
public static IConfigurationBuilder AddUserSecrets(this IConfigurationBuilder configuration)
{
if (configuration == null)
{
throw new ArgumentNullException(nameof(configuration));
}
var fileProvider = configuration.GetFileProvider();
return AddSecretsFile(configuration, PathHelper.GetSecretsPath(fileProvider));
}
/// <summary>
/// Adds the user secrets configuration source with specified secrets id.
/// </summary>
/// <param name="configuration"></param>
/// <param name="userSecretsId"></param>
/// <returns></returns>
public static IConfigurationBuilder AddUserSecrets(this IConfigurationBuilder configuration, string userSecretsId)
{
if (configuration == null)
{
throw new ArgumentNullException(nameof(configuration));
}
if (userSecretsId == null)
{
throw new ArgumentNullException(nameof(userSecretsId));
}
return AddSecretsFile(configuration, PathHelper.GetSecretsPathFromSecretsId(userSecretsId));
}
private static IConfigurationBuilder AddSecretsFile(IConfigurationBuilder configuration, string secretPath)
{
var directoryPath = Path.GetDirectoryName(secretPath);
var fileProvider = Directory.Exists(directoryPath)
? new PhysicalFileProvider(directoryPath)
: null;
return configuration.AddJsonFile(fileProvider, PathHelper.Secrets_File_Name, optional: true, reloadOnChange: false);
}
}
}
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>58b6443b-1278-4df9-b7bb-ddf3bffcf868</ProjectGuid>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
@@ -0,0 +1,92 @@
// Copyright (c) .NET Foundation. 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.Extensions.FileProviders;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Microsoft.Extensions.Configuration.UserSecrets
{
public class PathHelper
{
internal const string Secrets_File_Name = "secrets.json";
internal const string Config_File_Name = "project.json";
public static string GetSecretsPath(IFileProvider provider)
{
if (provider == null)
{
throw new ArgumentNullException(nameof(provider));
}
var fileInfo = provider.GetFileInfo(Config_File_Name);
if (fileInfo == null || !fileInfo.Exists || string.IsNullOrEmpty(fileInfo.PhysicalPath))
{
throw new InvalidOperationException(
string.Format(Resources.Error_Missing_Project_Json, provider.GetFileInfo("/")?.PhysicalPath ?? "unknown"));
}
using (var stream = fileInfo.CreateReadStream())
using (var streamReader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(streamReader))
{
var obj = JObject.Load(jsonReader);
var userSecretsId = obj.Value<string>("userSecretsId");
if (string.IsNullOrEmpty(userSecretsId))
{
throw new InvalidOperationException(
string.Format(Resources.Error_Missing_UserSecretId_In_Project_Json, fileInfo.Name));
}
return GetSecretsPathFromSecretsId(userSecretsId);
}
}
public static string GetSecretsPath(string projectPath)
{
if (projectPath == null)
{
throw new ArgumentNullException(nameof(projectPath));
}
using (var provider = new PhysicalFileProvider(projectPath))
{
return GetSecretsPath(provider);
}
}
public static string GetSecretsPathFromSecretsId(string userSecretsId)
{
if (userSecretsId == null)
{
throw new ArgumentNullException(nameof(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);
}
}
}
}
@@ -0,0 +1,11 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Reflection;
using System.Resources;
[assembly: AssemblyMetadata("Serviceable", "True")]
[assembly: NeutralResourcesLanguage("en-us")]
[assembly: AssemblyCompany("Microsoft Corporation.")]
[assembly: AssemblyCopyright("© Microsoft Corporation. All rights reserved.")]
[assembly: AssemblyProduct("Microsoft .NET Extensions")]

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 9518bec

Please sign in to comment.