-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to change encryption algorithm (#4591)
- Loading branch information
1 parent
7d0db6a
commit dbe75a8
Showing
5 changed files
with
105 additions
and
78 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/modules/secrets/Elsa.Secrets/Encryption/AesSecretEncryptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
namespace Elsa.Secrets.Encryption | ||
{ | ||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Elsa.Secrets.Models; | ||
using Elsa.Secrets.Options; | ||
using Microsoft.Extensions.Options; | ||
|
||
public class AesSecretEncryptor : ISecretEncryptor | ||
{ | ||
private readonly SecretsConfigOptions _secretsConfig; | ||
|
||
public AesSecretEncryptor(IOptions<SecretsConfigOptions> options) | ||
{ | ||
_secretsConfig = options.Value; | ||
} | ||
|
||
public Task EncryptProperties(Secret secret, CancellationToken cancellationToken = default) | ||
{ | ||
if (_secretsConfig.EncryptionEnabled == true) | ||
{ | ||
foreach (var property in secret.Properties) | ||
{ | ||
if (property.IsEncrypted || (_secretsConfig.EncryptedProperties?.Contains(property.Name, StringComparer.OrdinalIgnoreCase) ?? false)) | ||
{ | ||
continue; | ||
} | ||
|
||
foreach (var (key, value) in property.Expressions) | ||
{ | ||
property.Expressions[key] = AesEncryption.Encrypt(_secretsConfig.EncryptionKey, value); | ||
} | ||
|
||
property.IsEncrypted = true; | ||
} | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public Task DecryptPropertiesAsync(Secret secret, CancellationToken cancellationToken = default) | ||
{ | ||
if (_secretsConfig.EncryptionEnabled == true) | ||
{ | ||
foreach (var property in secret.Properties) | ||
{ | ||
if (!property.IsEncrypted) | ||
{ | ||
continue; | ||
} | ||
|
||
foreach (var (key, value) in property.Expressions) | ||
{ | ||
property.Expressions[key] = AesEncryption.Decrypt(_secretsConfig.EncryptionKey, value); | ||
} | ||
|
||
property.IsEncrypted = false; | ||
} | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/modules/secrets/Elsa.Secrets/Encryption/ISecretEncryptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Elsa.Secrets.Encryption | ||
{ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Elsa.Secrets.Models; | ||
|
||
public interface ISecretEncryptor | ||
{ | ||
Task EncryptProperties(Secret secret, CancellationToken cancellationToken = default); | ||
Task DecryptPropertiesAsync(Secret secret, CancellationToken cancellationToken = default); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters