Skip to content

Commit

Permalink
upgrade to .net 4.6, edit webAPI to retrieve/create encrypted master/…
Browse files Browse the repository at this point in the history
  • Loading branch information
watashiSHUN committed Oct 26, 2016
1 parent 6141363 commit 10edfa7
Show file tree
Hide file tree
Showing 40 changed files with 435 additions and 60 deletions.
2 changes: 2 additions & 0 deletions Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,7 @@ public static TimeSpan MaxAllowedExecutionTime
public const string Secrets = "secrets";
public const string SampleData = "sampledata";
public const string FunctionsPortal = "FunctionsPortal";
public const string FunctionKeyNewFormat = "~0.7";
public const string FunctionRunTimeVersion = "FUNCTIONS_EXTENSION_VERSION";
}
}
2 changes: 1 addition & 1 deletion Kudu.Client/Kudu.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Kudu.Client</RootNamespace>
<AssemblyName>Kudu.Client</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<TargetFrameworkProfile />
</PropertyGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Kudu.Client/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
</dependentAssembly>
</assemblyBinding>
</runtime>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/></startup></configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/></startup></configuration>
2 changes: 1 addition & 1 deletion Kudu.Console/Kudu.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AssemblyName>kudu</AssemblyName>
<Platform>x86</Platform>
<PlatformTarget>x86</PlatformTarget>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' OR '$(Configuration)|$(Platform)' == 'Release|x86' " />
Expand Down
2 changes: 1 addition & 1 deletion Kudu.Console/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<add key="WaitForDebuggerOnStart" value="false"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
</startup>
</configuration>
78 changes: 78 additions & 0 deletions Kudu.Contracts/Functions/FunctionSecretsJsonOps.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;

namespace Kudu.Core.Functions
{
public class FunctionSecretsJsonOps : IKeyJsonOps<FunctionSecrets>
{
public int NumberOfKeysInDefaultFormat
{
get
{
return 1;
}
}

// have the schema related info enclosed in this class
public string GenerateKeyJson(Tuple<string, string>[] keyPair, string functionRt, out string unencryptedKey)
{
unencryptedKey = keyPair[0].Item1;
if (string.CompareOrdinal(functionRt, Constants.FunctionKeyNewFormat) < 0)
{
return $"{{\"key\":\"{unencryptedKey}\"}}";
}
else
{
return $"{{\"keys\":[{{\"name\":\"default\",\"value\":\"{keyPair[0].Item2}\",\"encrypted\": true }}]}}";
}
}

public string GetKeyValueFromJson(string json, out bool isEncrypted)
{
try
{
JObject hostJson = JObject.Parse(json);
if (hostJson["key"]?.Type == JTokenType.String)
{
isEncrypted = false;
return hostJson.Value<string>("key");
}
else if (hostJson["keys"]?.Type == JTokenType.Array)
{
JArray keys = hostJson.Value<JArray>("keys");
if (keys.Count >= 1)
{
JObject keyObject = (JObject)keys[0];
for (int i = 1; i < keys.Count; i++)
{
// start from the second
// if we can't find the key named default, return the 1st key found
if (String.Equals(keys[i].Value<string>("name"), "default"))
{
keyObject = (JObject)keys[i];
break;
}
}
isEncrypted = keyObject.Value<bool>("encrypted");
return keyObject.Value<string>("value");
}
}
}
catch (JsonException)
{
// all parse issue ==> format exception
}
throw new FormatException($"Invalid secrets json: {json}");
}

public FunctionSecrets GenerateKeyObject(string functionKey, string functionName)
{
return new FunctionSecrets
{
Key = functionKey,
TriggerUrl = String.Format(@"https://{0}/api/{1}?code={2}", System.Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME") ?? "localhost", functionName, functionKey)
};
}
}
}
1 change: 1 addition & 0 deletions Kudu.Contracts/Functions/IFunctionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public interface IFunctionManager
Task<IEnumerable<FunctionEnvelope>> ListFunctionsConfigAsync();
Task<FunctionEnvelope> GetFunctionConfigAsync(string name);
Task<FunctionSecrets> GetFunctionSecretsAsync(string name);
Task<MasterKey> GetMasterKeyAsync();
Task<JObject> GetHostConfigAsync();
Task<JObject> PutHostConfigAsync(JObject content);
void DeleteFunction(string name, bool ignoreErrors);
Expand Down
21 changes: 21 additions & 0 deletions Kudu.Contracts/Functions/IKeyJsonOps.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Newtonsoft.Json.Linq;
using System;

namespace Kudu.Core.Functions
{
public interface IKeyJsonOps<T>
{
int NumberOfKeysInDefaultFormat
{
get;
}

// key generation is based on run time
string GenerateKeyJson(Tuple<string,string>[] keyPairs, string functionRt, out string unencryptedKey);

// read existing key file based on the content format, not the run time version
string GetKeyValueFromJson(string json, out bool isEncrypted);

T GenerateKeyObject(string functionKey, string functionName);
}
}
10 changes: 10 additions & 0 deletions Kudu.Contracts/Functions/MasterKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace Kudu.Core.Functions
{
public class MasterKey
{
[JsonProperty(PropertyName = "masterKey")]
public string Key { get; set; }
}
}
60 changes: 60 additions & 0 deletions Kudu.Contracts/Functions/MasterKeyJsonOps.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;

namespace Kudu.Core.Functions
{
public class MasterKeyJsonOps : IKeyJsonOps<MasterKey>
{
public int NumberOfKeysInDefaultFormat
{
get
{
return 2; // 1 masterkey, 1 functionkey in host.json
}
}

public string GenerateKeyJson(Tuple<string, string>[] keyPair, string functionRt, out string unencryptedKey)
{
unencryptedKey = keyPair[0].Item1;
if (string.CompareOrdinal(functionRt, Constants.FunctionKeyNewFormat) < 0)
{
return $"{{\"masterKey\":\"{unencryptedKey}\",\"functionKey\":\"{keyPair[1].Item1}\"}}";
}
else
{
return $"{{\"masterKey\":{{\"name\":\"master\",\"value\":\"{keyPair[0].Item2}\",\"encrypted\": true }},\"functionKeys\":[{{\"name\": \"default\",\"value\": \"{keyPair[1].Item2}\",\"encrypted\": true}}]}}";
}
}

public string GetKeyValueFromJson(string json, out bool isEncrypted)
{
try
{
JObject hostJson = JObject.Parse(json);
if (hostJson["masterKey"]?.Type == JTokenType.String && hostJson["functionKey"]?.Type == JTokenType.String)
{
isEncrypted = false;
return hostJson.Value<string>("masterKey");
}
else if (hostJson["masterKey"]?.Type == JTokenType.Object && hostJson["functionKeys"]?.Type == JTokenType.Array)
{
JObject keyObject = hostJson.Value<JObject>("masterKey");
isEncrypted = keyObject.Value<bool>("encrypted");
return keyObject.Value<string>("value");
}
}
catch (JsonException)
{
// all parse issue ==> format exception
}
throw new FormatException($"Invalid secrets json: {json}");
}

public MasterKey GenerateKeyObject(string masterKey, string Name)
{
// name is not used
return new MasterKey { Key = masterKey };
}
}
}
4 changes: 4 additions & 0 deletions Kudu.Contracts/Kudu.Contracts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@
<Compile Include="Editor\VfsStatEntry.cs" />
<Compile Include="Functions\FunctionSecrets.cs" />
<Compile Include="Functions\FunctionEnvelope.cs" />
<Compile Include="Functions\FunctionSecretsJsonOps.cs" />
<Compile Include="Functions\IFunctionManager.cs" />
<Compile Include="Functions\IKeyJsonOps.cs" />
<Compile Include="Functions\MasterKey.cs" />
<Compile Include="Functions\MasterKeyJsonOps.cs" />
<Compile Include="HashHelpers.cs" />
<Compile Include="Hooks\ConflictException.cs" />
<Compile Include="Hooks\WebHook.cs" />
Expand Down
2 changes: 1 addition & 1 deletion Kudu.Core.Test/Kudu.Core.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Kudu.Core.Test</RootNamespace>
<AssemblyName>Kudu.Core.Test</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<TargetFrameworkProfile />
</PropertyGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Kudu.Core.Test/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
</dependentAssembly>
</assemblyBinding>
</runtime>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/></startup></configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/></startup></configuration>
Loading

0 comments on commit 10edfa7

Please sign in to comment.