Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
477 changes: 242 additions & 235 deletions .editorconfig

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ jobs:
echo "informationalVersion=$informationalVersion" >> $env:GITHUB_OUTPUT

- name: Run tests
run: dotnet.exe test .\src\PSDataProtection.sln --configuration Release --runtime win-x64
run: |
cd .\src
dotnet.exe test --project .\Tests\Tests.csproj --configuration Debug --output Detailed

- name: Clean solution
run: dotnet.exe clean .\src\PSDataProtection.sln --configuration Release
run: dotnet.exe clean .\src\PSDataProtection.slnx --configuration Release

- name: Build project
run: dotnet.exe publish .\src\PSDataProtection\PSDataProtection.csproj --configuration Release --runtime win-x64 --output .\publish
run: dotnet.exe publish .\src\PSDataProtection\PSDataProtection.csproj --configuration Release --output .\publish

- name: Prepare release
run: Remove-Item ./publish/* -Include *.pdb,*.xml -ErrorAction Stop
Expand Down
11 changes: 11 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,15 @@
<Copyright>Copyright (c) Joseph L. Casale. All rights reserved.</Copyright>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Meziantou.Analyzer">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers.Unstable">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
<NoWarn>$(NoWarn);NU1507</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Meziantou.Analyzer" Version="2.0.264" />
<PackageVersion Include="Microsoft.PowerShell.5.1.ReferenceAssemblies" Version="1.0.0" />
<PackageVersion Include="System.Security.Cryptography.ProtectedData" Version="10.0.1" />
<PackageVersion Include="StyleCop.Analyzers.Unstable" Version="1.2.0.556" />
</ItemGroup>
</Project>
36 changes: 0 additions & 36 deletions src/PSDataProtection.sln

This file was deleted.

10 changes: 10 additions & 0 deletions src/PSDataProtection.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Solution>
<Folder Name="/Solution Items/">
<File Path="../.editorconfig" />
<File Path="Directory.Build.props" />
<File Path="Directory.Packages.props" />
<File Path="global.json" />
</Folder>
<Project Path="PSDataProtection/PSDataProtection.csproj" />
<Project Path="Tests/Tests.csproj" />
</Solution>
76 changes: 41 additions & 35 deletions src/PSDataProtection/NewDataProtectionSecretCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace PSDataProtection;
[OutputType(typeof(string))]
public class NewDataProtectionSecretCommand : PSCmdlet
{
private readonly System.Text.UTF8Encoding encoding = new();
private readonly System.Text.UTF8Encoding _encoding = new();

[Parameter(
Position = 0,
Expand All @@ -35,15 +35,16 @@ protected override void ProcessRecord()
IntPtr bString;
try
{
bString = Marshal.SecureStringToBSTR(this.SecureString);
bString = Marshal.SecureStringToBSTR(SecureString);
}
catch (Exception e)
{
this.ThrowTerminatingError(new ErrorRecord(
e,
"SecureStringToBinaryStringError",
ErrorCategory.InvalidData,
null));
ThrowTerminatingError(
new(
e,
"SecureStringToBinaryStringError",
ErrorCategory.InvalidData,
null));

return;
}
Expand All @@ -55,11 +56,12 @@ protected override void ProcessRecord()
}
catch (Exception e)
{
this.ThrowTerminatingError(new ErrorRecord(
e,
"BinaryStringToManagedStringError",
ErrorCategory.InvalidData,
null));
ThrowTerminatingError(
new(
e,
"BinaryStringToManagedStringError",
ErrorCategory.InvalidData,
null));

return;
}
Expand All @@ -70,43 +72,46 @@ protected override void ProcessRecord()

if (string.IsNullOrWhiteSpace(insecurePassword))
{
this.ThrowTerminatingError(new ErrorRecord(
new InvalidOperationException("The secure string was empty."),
"EmptyPassword",
ErrorCategory.InvalidData,
null));
ThrowTerminatingError(
new(
new InvalidOperationException("The secure string was empty."),
"EmptyPassword",
ErrorCategory.InvalidData,
null));

return;
}

byte[] bytes;
try
{
bytes = this.encoding.GetBytes(insecurePassword);
bytes = _encoding.GetBytes(insecurePassword);
}
catch (Exception e)
{
this.ThrowTerminatingError(new ErrorRecord(
e,
"BinaryEncodingError",
ErrorCategory.InvalidData,
null));
ThrowTerminatingError(
new(
e,
"BinaryEncodingError",
ErrorCategory.InvalidData,
null));

return;
}

byte[] secret;
try
{
secret = ProtectedData.Protect(bytes, null, this.Scope!.Value);
secret = ProtectedData.Protect(bytes, null, Scope!.Value);
}
catch (Exception e)
{
this.ThrowTerminatingError(new ErrorRecord(
e,
"ProtectionError",
ErrorCategory.NotSpecified,
null));
ThrowTerminatingError(
new(
e,
"ProtectionError",
ErrorCategory.NotSpecified,
null));

return;
}
Expand All @@ -118,15 +123,16 @@ protected override void ProcessRecord()
}
catch (Exception e)
{
this.ThrowTerminatingError(new ErrorRecord(
e,
"Base64EncodingError",
ErrorCategory.NotSpecified,
null));
ThrowTerminatingError(
new(
e,
"Base64EncodingError",
ErrorCategory.NotSpecified,
null));

return;
}

this.WriteObject(encoded);
WriteObject(encoded);
}
}
5 changes: 3 additions & 2 deletions src/PSDataProtection/PSDataProtection.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<AssemblyName>PSDataProtection</AssemblyName>
<LangVersion>preview</LangVersion>
<AnalysisLevel>preview-All</AnalysisLevel>
<NoWarn>CA1031</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.PowerShell.5.1.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="System.Security.Cryptography.ProtectedData" Version="10.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.PowerShell.5.1.ReferenceAssemblies" PrivateAssets="All" />
<PackageReference Include="System.Security.Cryptography.ProtectedData" PrivateAssets="All" />
</ItemGroup>

<PropertyGroup>
Expand Down
51 changes: 27 additions & 24 deletions src/PSDataProtection/ReadDataProtectionSecretCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace PSDataProtection;
[OutputType(typeof(SecureString), ParameterSetName = [SecureStringParameterSetName])]
public class ReadDataProtectionSecretCommand : PSCmdlet
{
private readonly System.Text.UTF8Encoding encoding = new();
private const string StringParameterSetName = "StringOutput";
private const string SecureStringParameterSetName = "SecureStringOutput";

public const string StringParameterSetName = "StringOutput";
public const string SecureStringParameterSetName = "SecureStringOutput";
private readonly System.Text.UTF8Encoding _encoding = new();

[Parameter(
Position = 0,
Expand Down Expand Up @@ -43,54 +43,57 @@ protected override void ProcessRecord()
byte[] bytes;
try
{
bytes = Convert.FromBase64String(this.Protected);
bytes = Convert.FromBase64String(Protected);
}
catch (Exception e)
{
this.ThrowTerminatingError(new ErrorRecord(
e,
"Base64DecodingError",
ErrorCategory.InvalidData,
null));
ThrowTerminatingError(
new(
e,
"Base64DecodingError",
ErrorCategory.InvalidData,
null));

return;
}

byte[] secret;
try
{
secret = ProtectedData.Unprotect(bytes, null, this.Scope!.Value);
secret = ProtectedData.Unprotect(bytes, null, Scope!.Value);
}
catch (Exception e)
{
this.ThrowTerminatingError(new ErrorRecord(
e,
"DecryptionError",
ErrorCategory.NotSpecified,
null));
ThrowTerminatingError(
new(
e,
"DecryptionError",
ErrorCategory.NotSpecified,
null));

return;
}

string decoded;
try
{
decoded = this.encoding.GetString(secret);
decoded = _encoding.GetString(secret);
}
catch (Exception e)
{
this.ThrowTerminatingError(new ErrorRecord(
e,
"DecodingError",
ErrorCategory.NotSpecified,
null));
ThrowTerminatingError(
new(
e,
"DecodingError",
ErrorCategory.NotSpecified,
null));

return;
}

object result;

switch (this.ParameterSetName)
switch (ParameterSetName)
{
case StringParameterSetName:

Expand Down Expand Up @@ -118,9 +121,9 @@ protected override void ProcessRecord()

default:

throw new InvalidOperationException($"Unknown parameter set name: \"{this.ParameterSetName}\".");
throw new InvalidOperationException($"Unknown parameter set name: \"{ParameterSetName}\".");
}

this.WriteObject(result);
WriteObject(result);
}
}
Loading