Giter VIP home page Giter VIP logo

azure-durable-entities-encryption's Introduction

Application Level Encryption for Azure Durable Entities

The code within this repository demonstrates a means by which durable entity state can be encrypted at the application layer. A key is configured for the function app. That key is then used during the serialization process to encrypt any properties annotated with the Encrypted attribute.

Dependencies

Bootstrapping Encryption

As part of the Configure method in the FunctionsStartup class register the dependencies necessary to enable encryption

Register how to resolve the encryption options/configuration. The key is expected to come from a key management solution such as Azure Key Vault. See Azure Key Vault Configuration Provider in ASP.NET Core for how to retrieve secrets from Azure Key Vault.

builder.Services.AddSingleton(sp => new EncryptionOptions(config["ENCRYPTIONKey"]));

Register the implementation of IEncryptionService to be used

builder.Services.AddSingleton<IEncryptionService, EncryptionService>();

Register customization to IMessageSerializerSettingsFactory to customize how durable functions serializes the state

//necessary to engage custom settings during serialization
builder.Services.AddSingleton<IContractResolver, EncryptedContractResolver>();
builder.Services.AddSingleton(sp => new JsonSerializerSettings { ContractResolver = sp.GetService<IContractResolver>() });
builder.Services.AddSingleton<IMessageSerializerSettingsFactory, SerializerSettingsFactory>();

Register customization to Json.NET's default serialization settings to customize how durable functions deserializes the state

//necessary to engage custom settings during deserialization
JsonConvert.DefaultSettings = () => builder.Services.BuildServiceProvider().GetService<JsonSerializerSettings>();

Annotate Properties for Encryption

To indicate which entity properties should be encrypted, annotate the properties with the Encrypted attribute. The EncryptedContractResolver will scan for this attribute to determine which properties need to be encrypted/decrypted.

[JsonObject(MemberSerialization = MemberSerialization.OptOut)]
public class AccountEntity : IAccountEntity
{
    // ensure account number is encrypted when saved
    [Encrypted]
    public string AccountNumber { get; set; }

    public void Set(string accountNumber) => AccountNumber = accountNumber;

    [FunctionName(nameof(AccountEntity))]
    public static Task Run([EntityTrigger] IDurableEntityContext ctx) => ctx.DispatchAsync<AccountEntity>();
}

Testing The Encryption

To test the encryption/decryption in this sample, the following two endpoints can be used.

PUT /accounts/{accountId}

This will set the account number to a value for the account of the provided accountId.

The value when saved to Azure Table Storage should be encrypted. The following is from the Hub Instances table; Input column for the row that represents the Durable Entity.

{"exists":true,"state":"{\"AccountNumber\":\"A1k9ufGmHFMgM83sE/8EdCMls/TLeXbWDEU32ZXrhE4=\"}","sorter":{}}

GET /accounts/{accountId}

This will retrieve the account entity for the account of the provided accountId.

The returned value should be decrypted

{ "accountNumber": "123ABC" }

Customize the Encryption

To customize the specific encryption algorithm used, a new implementation of IEncryptionService can be created. This will not change what properties are selected for encryption. It will only change how the properties annotated are encrypted & decrypted. If for example, something other than AesManaged is needed, a new implementation of IEncryptionService could be created to use a different cryptography implementation.

public interface IEncryptionService
{
    string Decrypt(string value);
    string Encrypt(string value);
}

public class MyEncryptionService
{
    public string Encrypt(string value)
    {
        //todo: return encrypted value
    }

    public string Decrypt(string value)
    {
        //todo: return the decrypted value
    }
}

Then, register the new implementation with the IServiceProvider

builder.Services.AddSingleton<IEncryptionService, EncryptionService>();

azure-durable-entities-encryption's People

Contributors

charleszipp avatar jplane avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

azure-durable-entities-encryption's Issues

key rotation

thanks for this awesome bit of code

the associated guidance on the azure docs page cautions about key rotation out of phase with orchestration wall clock run time.

so the solution still seeks the key rotation tweak

as the example stands today, seems to me writing a custom encryption service that had (probably sticky) knowledge of the old key and the new key would facilitate rotation scenarios. that's the rotate on-demand scenario, as orchestrations come alive.

the batch scenario is more domain specific, as it would have to be idempotent and batch update all the encrypted properties of all the entities in an application, with all the associated batch update trimmings like locking and update pausing etc.

in effect the batch scenario best starts to resemble a redeployment with the new key. all orchestrations with the old key gracefully expire and begin again with the new key in a new taskhub. this is not a solution that requires code changes, but documentation changes and i will make the appropriate applications on the feedback page

please advise

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.