Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Preventing Leaked Secrets in Azure
Microsoft Azure profile imageEmily Freeman
Emily Freeman forMicrosoft Azure

Posted on

     

Preventing Leaked Secrets in Azure

We've selected our favorite tips and tricks created byMichael Crump and are delivering fresh technical content on Azure all April! Miss a day (or more)?Catch up with the series.

Don't have Azure?Grab a free subscription.


Preventing Leaked Secrets with .NET Core

I think almost everyone has committed a secret, key or password to git at some point in their development careers. I definitely have. And if you think you haven't, go double-check.

It sucks. And it's easy to do.

Azure's solution for secrets management isAzure Key Vault.

But what if you wanted to roll your own solution? We're engineers after all...

Rolling Your OwnSecret Manager

Azure Key Vault ischeap but not completely free. And there is an overhead of learning the service. (Though I'd argue it's extremely simple.)

Secret Manager is a Microsoft solution for storing sensitive data during the development of an ASP.NET Core project.

Information is always stored in the user profile directory such as%APPDATA%\microsoft\UserSecrets\<userSecretsId>\secrets.json
for Windows or~/.microsoft/usersecrets/<userSecretsId>/secrets.json for Mac/Linux.

This means if other folks want to get your key store, they can target those directories b/c the JSON file is unencrypted. Not that my version is encrypted, it just isn’t stored in the user profile directory.

Preventing Problematic Pushes

If you work in .NET Core, you can prevent an accidental push of sensitive data to GitHub.

Step 1

Create a new .NET Core App in Visual Studio.

Step 2

Add a file calledappSecrets.json and define a couple of secrets that you don’t want released.

{  "ConnectionStrings": {    "BitlyAPI": "A_BITLY_API_KEY",    "StorageAccountAPI": "MY_STORAGE_ACCOUNT_KEY"  }}
Enter fullscreen modeExit fullscreen mode
Step 3

Set theappSecrets.json file toCopy if newer inside of Visual Studio.

copy if newer

Step 4

Add the following NuGet packages that allow you to easily read a local JSON file (such as yourappSecrets.json) and extract key pieces of information:

  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Configuration.FileExtensions
  • Microsoft.Extensions.Configuration.Json
Step 5

Add the following code inside the Main method. This usesConfigurationBuilder and searches for the file.

var builder = new ConfigurationBuilder()    .SetBasePath(Directory.GetCurrentDirectory())    .AddJsonFile("appSecrets.json", optional: false, reloadOnChange: true);IConfigurationRoot configuration = builder.Build();
Enter fullscreen modeExit fullscreen mode

You can now access the value of the string with the following:

configuration.GetConnectionString("StorageAccountAPI")
Enter fullscreen modeExit fullscreen mode
Step 6

Set your/.gitignore to ignore theappSecrets.json that you added.

## Ignore Visual Studio temporary files, build results, and## files generated by popular Visual Studio add-ons.appSecrets.json
Enter fullscreen modeExit fullscreen mode

You can verify this file is ignored by looking for the red circle if using Visual Studio.

git ignore

visual studio verification

Not too complicated. But! I really do recommend usingAzure Key Vault as it's simple and can protect you across your entire software delivery lifecycle.

Want to read more on secrets in Azure?We've got you covered on everything keys, secrets and certificates.!


We'll be posting articles every day in April, so stay tuned or jump ahead and check out more tips and tricksnow.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Invent with purpose

Any language. Any platform.

More fromMicrosoft Azure

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp