From 2f283bd4f148202f447c3e8fa8370863da7ada50 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Tue, 9 Apr 2019 14:53:13 +1200 Subject: [PATCH] feat: allow $IPFS_PASS to hold the passphrase (#81) --- doc/articles/envvars.md | 1 + src/IpfsEngine.cs | 24 ++++++++++++++++++++++++ test/IpfsEngineTest.cs | 28 ++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/doc/articles/envvars.md b/doc/articles/envvars.md index 5f5fd5e2..42799274 100644 --- a/doc/articles/envvars.md +++ b/doc/articles/envvars.md @@ -8,3 +8,4 @@ are used to control the behaviour of the library | HOME | The user's home directory | | HOMEPATH | same as `HOME` | | IPFS_PATH | The folder of the IPFS [repository](repository.md) | +| IPFS_PASS | Contains the passphrase for the [key chain](key.md) | diff --git a/src/IpfsEngine.cs b/src/IpfsEngine.cs index 41b72a07..6e12bb77 100644 --- a/src/IpfsEngine.cs +++ b/src/IpfsEngine.cs @@ -36,6 +36,27 @@ public partial class IpfsEngine : ICoreApi, IService, IDisposable SecureString passphrase; ConcurrentBag> stopTasks = new ConcurrentBag>(); + /// + /// Creates a new instance of the class + /// with the IPFS_PASS environment variable. + /// + /// + /// Th passphrase must be in the IPFS_PASS environment variable. + /// + public IpfsEngine() + { + var s = Environment.GetEnvironmentVariable("IPFS_PASS"); + if (s == null) + throw new Exception("The IPFS_PASS environement variable is missing."); + + passphrase = new SecureString(); + foreach (var c in s) + { + this.passphrase.AppendChar(c); + } + Init(); + } + /// /// Creates a new instance of the class /// with the specified passphrase. @@ -64,6 +85,9 @@ public IpfsEngine(char[] passphrase) /// /// The password used to access the keychain. /// + /// + /// A copy of the is made. + /// public IpfsEngine(SecureString passphrase) { this.passphrase = passphrase.Copy(); diff --git a/test/IpfsEngineTest.cs b/test/IpfsEngineTest.cs index 5165e44c..6ba4827f 100644 --- a/test/IpfsEngineTest.cs +++ b/test/IpfsEngineTest.cs @@ -35,6 +35,27 @@ public async Task SecureString_Passphrase() await ipfs.KeyChain(); } + [TestMethod] + public async Task IpfsPass_Passphrase() + { + var secret = "this is not a secure pass phrase"; + var ipfs = new IpfsEngine(secret.ToCharArray()); + ipfs.Options = TestFixture.Ipfs.Options; + await ipfs.KeyChain(); + + Environment.SetEnvironmentVariable("IPFS_PASS", secret); + try + { + ipfs = new IpfsEngine(); + ipfs.Options = TestFixture.Ipfs.Options; + await ipfs.KeyChain(); + } + finally + { + Environment.SetEnvironmentVariable("IPFS_PASS", null); + } + } + [TestMethod] public async Task Wrong_Passphrase() { @@ -51,6 +72,13 @@ public async Task Wrong_Passphrase() }); } + [TestMethod] + [ExpectedException(typeof(Exception))] + public void IpfsPass_Missing() + { + var _ = new IpfsEngine(); + } + [TestMethod] public async Task Can_Start_And_Stop() {