Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Commit

Permalink
Issue #17: Sending signed messages back to jupyter
Browse files Browse the repository at this point in the history
  • Loading branch information
zabirauf committed Mar 26, 2016
1 parent 90506aa commit ecedc1d
Show file tree
Hide file tree
Showing 13 changed files with 269 additions and 84 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ solution: iCSharp.sln

install:
- nuget restore ./iCSharp.sln
- nuget restore ./Engine/ScriptCs.sln

script:
- cd ./Engine
- ./build.sh
- mkdir artifacts --parents
- xbuild ./ScriptCs.sln /property:Configuration=Release /nologo /verbosity:normal
- mono ./packages/xunit.runners.1.9.2/tools/xunit.console.clr4.exe test/ScriptCs.Tests.Acceptance/bin/Release/ScriptCs.Tests.Acceptance.dll /xml artifacts/ScriptCs.Tests.Acceptance.dll.TestResult.xml /html artifacts/ScriptCs.Tests.Acceptance.dll.TestResult.html
- cd ../
- xbuild ./iCSharp.sln /property:Configuration=Release /nologo /verbosity:normal
12 changes: 12 additions & 0 deletions Kernel/Helpers/IMessageSender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

namespace iCSharp.Kernel.Helpers
{
using iCSharp.Messages;
using NetMQ;

public interface IMessageSender
{
bool Send(Message message, NetMQSocket socket);
}
}

24 changes: 24 additions & 0 deletions Kernel/Helpers/ISignatureValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

namespace iCSharp.Kernel.Helpers
{
using System;
using iCSharp.Messages;

public interface ISignatureValidator
{
/// <summary>
/// Creates the signature.
/// </summary>
/// <returns>The signature.</returns>
/// <param name="message">Message.</param>
string CreateSignature(Message message);

/// <summary>
/// Determines whether this instance is valid signature the specified message.
/// </summary>
/// <returns><c>true</c> if this instance is valid signature the specified message; otherwise, <c>false</c>.</returns>
/// <param name="message">Message.</param>
bool IsValidSignature(Message message);
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


namespace iCSharp.Kernel
namespace iCSharp.Kernel.Helpers
{
using System;
using iCSharp.Messages;
Expand Down
39 changes: 39 additions & 0 deletions Kernel/Helpers/MessageSender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

namespace iCSharp.Kernel.Helpers
{
using Common.Serializer;
using iCSharp.Messages;
using NetMQ;

public class MessageSender : IMessageSender
{
private const string Delimeter = "<IDS|MSG>";

private readonly ISignatureValidator _signatureValidator;

public MessageSender(ISignatureValidator signatureValidator)
{
this._signatureValidator = signatureValidator;
}

public bool Send(Message message, NetMQSocket socket)
{
string hmac = this._signatureValidator.CreateSignature (message);

Send(message.UUID, socket);
Send(Delimeter, socket);
Send(hmac, socket);
Send(JsonSerializer.Serialize(message.Header), socket);
Send(JsonSerializer.Serialize(message.ParentHeader), socket);
Send(JsonSerializer.Serialize(message.MetaData), socket);
Send(message.Content, socket, false);

return true;
}

private void Send(string message, NetMQSocket socket, bool sendMore = true)
{
socket.Send(message, false, sendMore);
}
}
}
94 changes: 94 additions & 0 deletions Kernel/Helpers/SignatureValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@

namespace iCSharp.Kernel.Helpers
{
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using Common.Logging;
using Common.Serializer;
using iCSharp.Messages;


public class SignatureValidator : ISignatureValidator
{
private readonly ILog _logger;

private readonly HMAC _signatureGenerator;

private readonly Encoding _encoder;
/// <summary>
/// Initializes a new instance of the <see cref="iCSharp.Kernel.Helpers.Sha256SignatureValidator"/> class.
/// </summary>
/// <param name="key">Shared key used to initialize the digest.</param>
public SignatureValidator (ILog logger, string key, string algorithm)
{
this._logger = logger;
this._encoder = new UTF8Encoding ();

this._signatureGenerator = HMAC.Create (algorithm);
this._signatureGenerator.Key = this._encoder.GetBytes (key);
}

#region ISignatureValidator implementation

/// <summary>
/// Creates the signature.
/// </summary>
/// <returns>The signature.</returns>
/// <param name="message">Message.</param>
public string CreateSignature (Message message)
{
this._signatureGenerator.Initialize ();

List<string> messages = this.GetMessagesToAddForDigest (message);

// For all items update the signature
foreach (string item in messages) {
byte[] sourceBytes = this._encoder.GetBytes (item);
this._signatureGenerator.TransformBlock (sourceBytes, 0, sourceBytes.Length, null, 0);
}

this._signatureGenerator.TransformFinalBlock (new byte[0], 0, 0);

// Calculate the digest and remove -
return BitConverter.ToString(this._signatureGenerator.Hash).Replace("-","").ToLower();
}

/// <summary>
/// Determines whether this instance is valid signature the specified message.
/// </summary>
/// <returns>true</returns>
/// <c>false</c>
/// <param name="message">Message.</param>
public bool IsValidSignature (Message message)
{
string calculatedSignature = this.CreateSignature (message);
this._logger.Info (string.Format ("Expected Signature: {0}", calculatedSignature));
return string.Equals (message.HMac, calculatedSignature, StringComparison.OrdinalIgnoreCase);
}

#endregion

/// <summary>
/// Gets the messages to add for digest.
/// </summary>
/// <returns>The messages to add for digest.</returns>
/// <param name="message">Message.</param>
private List<string> GetMessagesToAddForDigest(Message message)
{
if (message == null)
{
return new List<string> ();
}

return new List<string>(){
JsonSerializer.Serialize(message.Header),
JsonSerializer.Serialize(message.ParentHeader),
JsonSerializer.Serialize(message.MetaData),
message.Content
};
}
}
}

42 changes: 23 additions & 19 deletions Kernel/Kernel.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -34,17 +34,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Common.Logging, Version=2.1.2.0, Culture=neutral, PublicKeyToken=af08829b84f0328e, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Common.Logging.2.1.2\lib\net40\Common.Logging.dll</HintPath>
</Reference>
<Reference Include="NetMQ">
<HintPath>..\packages\NetMQ.3.3.0.10\lib\net40\NetMQ.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
Expand All @@ -53,15 +45,19 @@
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="Common.Logging">
<HintPath>..\packages\Common.Logging.2.1.2\lib\net40\Common.Logging.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Constants.cs" />
<Compile Include="Control\Control.cs" />
<Compile Include="Heartbeat\Heartbeat.cs" />
<Compile Include="IServer.cs" />
<Compile Include="KernelCreator.cs" />
<Compile Include="MessageBuilder.cs" />
<Compile Include="MessageSender.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReplEngineFactory.cs" />
Expand All @@ -75,6 +71,11 @@
<Compile Include="Shell\KernelInfoRequestHandler.cs" />
<Compile Include="Shell\Shell.cs" />
<Compile Include="Stdin\Stdin.cs" />
<Compile Include="Helpers\MessageBuilder.cs" />
<Compile Include="Helpers\MessageSender.cs" />
<Compile Include="Helpers\ISignatureValidator.cs" />
<Compile Include="Helpers\IMessageSender.cs" />
<Compile Include="Helpers\SignatureValidator.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand All @@ -84,35 +85,35 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj">
<Project>{0330b333-d8b9-451e-b3b7-20daf7a2fcd4}</Project>
<Project>{0330B333-D8B9-451E-B3B7-20DAF7A2FCD4}</Project>
<Name>Common</Name>
</ProjectReference>
<ProjectReference Include="..\Engine\src\ScriptCs.Contracts\ScriptCs.Contracts.csproj">
<Project>{6049e205-8b5f-4080-b023-70600e51fd64}</Project>
<Project>{6049E205-8B5F-4080-B023-70600E51FD64}</Project>
<Name>ScriptCs.Contracts</Name>
</ProjectReference>
<ProjectReference Include="..\Engine\src\ScriptCs.Core\ScriptCs.Core.csproj">
<Project>{e590e710-e159-48e6-a3e6-1a83d3fe732c}</Project>
<Project>{E590E710-E159-48E6-A3E6-1A83D3FE732C}</Project>
<Name>ScriptCs.Core</Name>
</ProjectReference>
<ProjectReference Include="..\Engine\src\ScriptCs.Engine.Mono\ScriptCs.Engine.Mono.csproj">
<Project>{e4adcfee-ff3b-4ef5-8298-2b31f407f58b}</Project>
<Project>{E4ADCFEE-FF3B-4EF5-8298-2B31F407F58B}</Project>
<Name>ScriptCs.Engine.Mono</Name>
</ProjectReference>
<ProjectReference Include="..\Engine\src\ScriptCs.Engine.Roslyn\ScriptCs.Engine.Roslyn.csproj">
<Project>{e79ec231-e27d-4057-91c9-2d001a3a8c3b}</Project>
<Project>{E79EC231-E27D-4057-91C9-2D001A3A8C3B}</Project>
<Name>ScriptCs.Engine.Roslyn</Name>
</ProjectReference>
<ProjectReference Include="..\Engine\src\ScriptCs.Hosting\ScriptCs.Hosting.csproj">
<Project>{9aef2d95-87fb-4829-b384-34bfe076d531}</Project>
<Project>{9AEF2D95-87FB-4829-B384-34BFE076D531}</Project>
<Name>ScriptCs.Hosting</Name>
</ProjectReference>
<ProjectReference Include="..\Engine\src\ScriptCs\ScriptCs.csproj">
<Project>{25080671-1a80-4041-b9c7-260578ff4849}</Project>
<Project>{25080671-1A80-4041-B9C7-260578FF4849}</Project>
<Name>ScriptCs</Name>
</ProjectReference>
<ProjectReference Include="..\Messages\Messages.csproj">
<Project>{1dc4bcd0-8e6b-4657-ad7c-2e1e01e3eb34}</Project>
<Project>{1DC4BCD0-8E6B-4657-AD7C-2E1E01E3EB34}</Project>
<Name>Messages</Name>
</ProjectReference>
</ItemGroup>
Expand All @@ -131,4 +132,7 @@
<Target Name="AfterBuild">
</Target>
-->
<ItemGroup>
<Folder Include="Helpers\" />
</ItemGroup>
</Project>
48 changes: 38 additions & 10 deletions Kernel/KernelCreator.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@



using System.Collections.Generic;
using iCSharp.Kernel.ScriptEngine;
using iCSharp.Kernel.Shell;

namespace iCSharp.Kernel
{
using System.Collections.Generic;
using Common.Logging;
using Common.Logging.Simple;
using iCSharp.Messages;
using NetMQ;
using iCSharp.Kernel.Helpers;
using iCSharp.Kernel.ScriptEngine;
using iCSharp.Kernel.Shell;
using iCSharp.Messages;
using NetMQ;


public class KernelCreator
{
private ILog _logger;
private ISignatureValidator _signatureValidator;
private IMessageSender _messageSender;

private ConnectionInformation _connectionInformation;
private NetMQContext _context;
Expand Down Expand Up @@ -46,6 +46,33 @@ public KernelCreator(ConnectionInformation connectionInformation)
this._replEngineFactory = new ReplEngineFactory(this._logger, new string[] {});
}

public ISignatureValidator SignatureValidator
{
get
{
if (this._signatureValidator == null)
{
string signatureAlgorithm = this._connectionInformation.SignatureScheme.Replace ("-", "").ToUpperInvariant ();
this._signatureValidator = new SignatureValidator (this._logger, this._connectionInformation.Key, signatureAlgorithm);
}

return this._signatureValidator;
}
}

public IMessageSender MessageSender
{
get
{
if (this._messageSender == null)
{
this._messageSender = new MessageSender (this.SignatureValidator);
}

return this._messageSender;
}
}

public IServer ShellServer
{
get
Expand All @@ -57,6 +84,7 @@ public IServer ShellServer
this.GetAddress(this._connectionInformation.ShellPort),
this.GetAddress(this._connectionInformation.IOPubPort),
this._context,
this.SignatureValidator,
this.MessageHandler);
}

Expand Down Expand Up @@ -97,7 +125,7 @@ private IShellMessageHandler KernelInfoRequestHandler
{
if (this._kernelInfoRequestHandler == null)
{
this._kernelInfoRequestHandler = new KernelInfoRequestHandler(this._logger);
this._kernelInfoRequestHandler = new KernelInfoRequestHandler(this._logger, this.MessageSender);
}

return this._kernelInfoRequestHandler;
Expand All @@ -123,7 +151,7 @@ private IShellMessageHandler ExecuteRequestHandler
{
if (this._executeRequestHandler == null)
{
this._executeRequestHandler = new ExecuteRequestHandler(this._logger, this.ReplEngine);
this._executeRequestHandler = new ExecuteRequestHandler(this._logger, this.ReplEngine, this.MessageSender);
}

return this._executeRequestHandler;
Expand Down
Loading

0 comments on commit ecedc1d

Please sign in to comment.