Skip to content

Commit

Permalink
Add un-encrypted passwords mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Malík committed Jul 22, 2018
1 parent f8deee9 commit 93148a7
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 41 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ In case you want export statistics or any other data from unifi controller, this
### Configuration
Before you use this exporter you have to change configuration in config (_`MalikP.Ubiquiti.DatabaseExporter.Service.exe.config`_) file.

**Setting defines if your password is encrypted or in plain text**<br />
`<add key="Use-Encrypted-Psswords" value="true|false"/>`

_In case you will use `false` bellow configs what describe password encryption need to use unencrypted password. Use when you do not have certificate to encrypt password. **Using plaintext passwords create security risk that password can be stolen by unatorized person**. **Using un-encrypted passwords is not recommended !**_<br />


**Certificate used to decrypt passwords**<br />
`<add key="Encryption-Certificate-Identifier" value="" />`

Expand Down Expand Up @@ -71,4 +77,4 @@ _Your password should be also automatically copied into clipboard._
If you want to use SQL Server Database to store your data you also have to create database and prepare schema of that database.
1. create empty SQL database
2. execute script located in project `~\src\MalikP.Ubiquiti.DatabaseExporter.Data\Scripts\InstallScript.sql`
3. done
3. done
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="MalikP.Cryptography, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!-- certificate have to be in certification store local machine -->
<add key="Encryption-Certificate-Identifier" value=""/>
</appSettings>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
Expand Down Expand Up @@ -56,7 +57,9 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="App.config">
<SubType>Designer</SubType>
</None>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
2 changes: 2 additions & 0 deletions src/MalikP.Ubiquiti.DatabaseExporter.Service/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<appSettings>
<add key="Use-Encrypted-Psswords" value="true"/>

<!-- certificate have to be in certification store local machine-->
<add key="Encryption-Certificate-Identifier" value="[Encryption-Certificate-Serial-Number]" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
Expand Down Expand Up @@ -90,7 +91,9 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="App.config">
<SubType>Designer</SubType>
</None>
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
Expand Down
53 changes: 31 additions & 22 deletions src/MalikP.Ubiquiti.DatabaseExporter.Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ private static void SetupInversionOfControl()
{
_ioc.Register<ISSHTunel, SSHTunel>()
.RegistrationBuilder<IExtendedRegistrationBuilder>()
.WithConstructorResolvingStrategy(ConstructorResolveStrategy.Complex);
.WithConstructorResolvingStrategy(ConstructorResolveStrategy.Complex);
}

_ioc.Register<IMongoDataSource, MongoDatabaseDataSource>()
.RegistrationBuilder<IExtendedRegistrationBuilder>()
.WithPrimitiveParameter<string>(ConfigurationManager.AppSettings["Mongo-Connection-String"])
.WithConstructorResolvingStrategy(ConstructorResolveStrategy.Complex);
.WithPrimitiveParameter<string>(ConfigurationManager.AppSettings["Mongo-Connection-String"])
.WithConstructorResolvingStrategy(ConstructorResolveStrategy.Complex);

_ioc.Register<IServiceExporter, ServiceMainExporter>();
_ioc.Register<DatabaseExporterWindowsService>();
Expand All @@ -117,7 +117,7 @@ private static void SetupInversionOfControl()
{
_ioc.Register<ISpecificUnifiExporter, UnifiToFileSystemExporter>()
.RegistrationBuilder<IExtendedRegistrationBuilder>()
.WithPrimitiveParameter<string>(ConfigurationManager.AppSettings["Backup-Path"]);
.WithPrimitiveParameter<string>(ConfigurationManager.AppSettings["Backup-Path"]);
}

if (bool.Parse(ConfigurationManager.AppSettings["Export-To-DB"]))
Expand All @@ -129,32 +129,41 @@ private static void SetupInversionOfControl()

_ioc.Register<IDatabaseChecker, RecordCecker>()
.RegistrationBuilder<IExtendedRegistrationBuilder>()
.WithPrimitiveParameter<string>(connectionString);
.WithPrimitiveParameter<string>(connectionString);

_ioc.Register<IDatabaseWriter, RecordWriter>()
.RegistrationBuilder<IExtendedRegistrationBuilder>()
.WithPrimitiveParameter<string>(connectionString);
.WithPrimitiveParameter<string>(connectionString);

_ioc.Register<ICertificateIdentifier, CertificateIdentifier>()
.RegistrationBuilder<IExtendedRegistrationBuilder>()
.WithPrimitiveParameter<string>(ConfigurationManager.AppSettings["Encryption-Certificate-Identifier"]);
if (bool.Parse(ConfigurationManager.AppSettings["Use-Encrypted-Psswords"]))
{
_ioc.Register<ICertificateIdentifier, CertificateIdentifier>()
.RegistrationBuilder<IExtendedRegistrationBuilder>()
.WithPrimitiveParameter<string>(ConfigurationManager.AppSettings["Encryption-Certificate-Identifier"]);

_ioc.Register<CertificateObtainerSettings>()
.RegistrationBuilder<IExtendedRegistrationBuilder>()
.WithPrimitiveParameter<StoreName>(StoreName.My)
.WithPrimitiveParameter<StoreLocation>(StoreLocation.LocalMachine)
.WithPrimitiveParameter<X509FindType>(X509FindType.FindBySerialNumber)
.WithConstructorResolvingStrategy(ConstructorResolveStrategy.Complex);
_ioc.Register<CertificateObtainerSettings>()
.RegistrationBuilder<IExtendedRegistrationBuilder>()
.WithPrimitiveParameter<StoreName>(StoreName.My)
.WithPrimitiveParameter<StoreLocation>(StoreLocation.LocalMachine)
.WithPrimitiveParameter<X509FindType>(X509FindType.FindBySerialNumber)
.WithConstructorResolvingStrategy(ConstructorResolveStrategy.Complex);

_ioc.Register<ICertificateObtainer, CertificationStoreExactCertificateObtainer>()
.RegistrationBuilder<IExtendedRegistrationBuilder>()
.WithConstructorResolvingStrategy(ConstructorResolveStrategy.Complex);

_ioc.RegisterByConstructor<ICustomCredential>(provider => new EncryptedCredential(ConfigurationManager.AppSettings["Sql-User-Id"],
ConfigurationManager.AppSettings["Sql-User-Password"],
provider.Resolve<RsaCertificateEncryptor>()));
_ioc.RegisterByConstructor<RsaCertificateEncryptor>(provider => new RsaCertificateEncryptor(provider.Resolve<ICertificateObtainer>(), RSAEncryptionPadding.OaepSHA1));

_ioc.Register<ICertificateObtainer, CertificationStoreExactCertificateObtainer>()
.RegistrationBuilder<IExtendedRegistrationBuilder>()
.WithConstructorResolvingStrategy(ConstructorResolveStrategy.Complex);

_ioc.RegisterByConstructor<RsaCertificateEncryptor>(provider => new RsaCertificateEncryptor(provider.Resolve<ICertificateObtainer>(), RSAEncryptionPadding.OaepSHA1));
_ioc.RegisterByConstructor<ICustomCredential>(provider => new EncryptedCredential(ConfigurationManager.AppSettings["Sql-User-Id"],
ConfigurationManager.AppSettings["Sql-User-Password"],
provider.Resolve<RsaCertificateEncryptor>()));
}
else
{
_ioc.RegisterByConstructor<ICustomCredential>(provider => new CustomCredential(ConfigurationManager.AppSettings["Sql-User-Id"],
ConfigurationManager.AppSettings["Sql-User-Password"]));
}

_ioc.Register<ICheckerCommandCreatorProvider, CheckerCommandCreatorProvider>();
_ioc.Register<IWriterCommandCreatorProvider, WriterCommandCreatorProvider>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
11 changes: 10 additions & 1 deletion src/MalikP.Ubiquiti.DatabaseExporter/SSHTuneling/SSHTunel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,18 @@ public SSHTunel(RsaCertificateEncryptor passwordEncryptor)
_passwordEncryptor = passwordEncryptor;
}

// in case encrypted mode is disabled
public SSHTunel()
{
}

public SshClient OpenTunel()
{
var password = _passwordEncryptor.Decrypt(ConfigurationManager.AppSettings["SSH-Tunel-Password"]);
var encryptedPassword = ConfigurationManager.AppSettings["SSH-Tunel-Password"];

// in case encrypted mode is disabled
var password = _passwordEncryptor?.Decrypt(encryptedPassword) ?? encryptedPassword;

var tunel = new SshClient(ConfigurationManager.AppSettings["SSH-Tunel-Host"],
ConfigurationManager.AppSettings["SSH-Tunel-UserName"],
password);
Expand Down

0 comments on commit 93148a7

Please sign in to comment.