Skip to content
This repository was archived by the owner on Aug 3, 2024. It is now read-only.

Commit 18f153a

Browse files
authored
Key vault writer (#407)
* Key vault writer. * ISecretWriter descends from ISecretReader
1 parent 227c986 commit 18f153a

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
7+
namespace NuGet.Services.KeyVault
8+
{
9+
public interface ISecretWriter : ISecretReader
10+
{
11+
Task SetSecretAsync(string secretName, string secretValue, DateTimeOffset? expiration = null);
12+
}
13+
}

src/NuGet.Services.KeyVault/KeyVaultReader.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public class KeyVaultReader : ISecretReader
2121
private readonly Lazy<KeyVaultClient> _keyVaultClient;
2222
private ClientAssertionCertificate _clientAssertionCertificate;
2323

24+
protected string VaultBaseUrl => _vault;
25+
protected KeyVaultClient KeyVaultClient => _keyVaultClient.Value;
26+
2427
public KeyVaultReader(KeyVaultConfiguration configuration)
2528
{
2629
if (configuration == null)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
using Microsoft.Azure.KeyVault;
7+
using Microsoft.Azure.KeyVault.Models;
8+
9+
namespace NuGet.Services.KeyVault
10+
{
11+
public class KeyVaultWriter : KeyVaultReader, ISecretWriter
12+
{
13+
public KeyVaultWriter(KeyVaultConfiguration configuration) : base(configuration)
14+
{
15+
}
16+
17+
public async Task SetSecretAsync(
18+
string secretName,
19+
string secretValue,
20+
DateTimeOffset? expiration = null)
21+
{
22+
SecretAttributes attributes = null;
23+
if (expiration.HasValue)
24+
{
25+
attributes = new SecretAttributes
26+
{
27+
Expires = expiration.Value.UtcDateTime,
28+
};
29+
}
30+
31+
await KeyVaultClient.SetSecretAsync(VaultBaseUrl, secretName, secretValue, secretAttributes: attributes);
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)