8
8
using System . Linq ;
9
9
using System . Threading . Tasks ;
10
10
using Microsoft . Azure . WebJobs . Script . IO ;
11
+ using Microsoft . Azure . WebJobs . Script . WebHost . Diagnostics . Extensions ;
12
+ using Microsoft . Azure . WebJobs . Script . WebHost . Properties ;
11
13
using Microsoft . Extensions . Logging ;
12
14
using Microsoft . WindowsAzure . Storage ;
13
15
using Microsoft . WindowsAzure . Storage . Blob ;
@@ -25,7 +27,13 @@ public class BlobStorageSecretsRepository : BaseSecretsRepository
25
27
private readonly string _secretsContainerName = "azure-webjobs-secrets" ;
26
28
private readonly string _accountConnectionString ;
27
29
28
- public BlobStorageSecretsRepository ( string secretSentinelDirectoryPath , string accountConnectionString , string siteSlotName ) : base ( secretSentinelDirectoryPath )
30
+ public BlobStorageSecretsRepository ( string secretSentinelDirectoryPath , string accountConnectionString , string siteSlotName )
31
+ : this ( secretSentinelDirectoryPath , accountConnectionString , siteSlotName , null )
32
+ {
33
+ }
34
+
35
+ public BlobStorageSecretsRepository ( string secretSentinelDirectoryPath , string accountConnectionString , string siteSlotName , ILogger logger )
36
+ : base ( secretSentinelDirectoryPath , logger )
29
37
{
30
38
if ( secretSentinelDirectoryPath == null )
31
39
{
@@ -71,11 +79,18 @@ public override async Task<ScriptSecrets> ReadAsync(ScriptSecretsType type, stri
71
79
{
72
80
string secretsContent = null ;
73
81
string blobPath = GetSecretsBlobPath ( type , functionName ) ;
74
- CloudBlockBlob secretBlob = _blobContainer . GetBlockBlobReference ( blobPath ) ;
75
-
76
- if ( await secretBlob . ExistsAsync ( ) )
82
+ try
83
+ {
84
+ CloudBlockBlob secretBlob = _blobContainer . GetBlockBlobReference ( blobPath ) ;
85
+ if ( await secretBlob . ExistsAsync ( ) )
86
+ {
87
+ secretsContent = await secretBlob . DownloadTextAsync ( ) ;
88
+ }
89
+ }
90
+ catch ( Exception e )
77
91
{
78
- secretsContent = await secretBlob . DownloadTextAsync ( ) ;
92
+ LogErrorMessage ( "read" ) ;
93
+ throw e ;
79
94
}
80
95
81
96
return string . IsNullOrEmpty ( secretsContent ) ? null : ScriptSecretSerializer . DeserializeSecrets ( type , secretsContent ) ;
@@ -89,7 +104,15 @@ public override async Task WriteAsync(ScriptSecretsType type, string functionNam
89
104
}
90
105
91
106
string blobPath = GetSecretsBlobPath ( type , functionName ) ;
92
- await WriteToBlobAsync ( blobPath , ScriptSecretSerializer . SerializeSecrets ( secrets ) ) ;
107
+ try
108
+ {
109
+ await WriteToBlobAsync ( blobPath , ScriptSecretSerializer . SerializeSecrets ( secrets ) ) ;
110
+ }
111
+ catch ( Exception e )
112
+ {
113
+ LogErrorMessage ( "write" ) ;
114
+ throw e ;
115
+ }
93
116
94
117
string filePath = GetSecretsSentinelFilePath ( type , functionName ) ;
95
118
await FileUtility . WriteAsync ( filePath , DateTime . UtcNow . ToString ( ) ) ;
@@ -104,7 +127,16 @@ public override async Task WriteSnapshotAsync(ScriptSecretsType type, string fun
104
127
105
128
string blobPath = GetSecretsBlobPath ( type , functionName ) ;
106
129
blobPath = SecretsUtility . GetNonDecryptableName ( blobPath ) ;
107
- await WriteToBlobAsync ( blobPath , ScriptSecretSerializer . SerializeSecrets ( secrets ) ) ;
130
+
131
+ try
132
+ {
133
+ await WriteToBlobAsync ( blobPath , ScriptSecretSerializer . SerializeSecrets ( secrets ) ) ;
134
+ }
135
+ catch ( Exception e )
136
+ {
137
+ LogErrorMessage ( "write" ) ;
138
+ throw e ;
139
+ }
108
140
}
109
141
110
142
public override async Task PurgeOldSecretsAsync ( IList < string > currentFunctions , ILogger logger )
@@ -118,7 +150,16 @@ public override async Task<string[]> GetSecretSnapshots(ScriptSecretsType type,
118
150
// Prefix is secret blob path without extension
119
151
string prefix = Path . GetFileNameWithoutExtension ( GetSecretsBlobPath ( type , functionName ) ) + $ ".{ ScriptConstants . Snapshot } ";
120
152
121
- BlobResultSegment segmentResult = await _blobContainer . ListBlobsSegmentedAsync ( string . Format ( "{0}/{1}" , _secretsBlobPath , prefix . ToLowerInvariant ( ) ) , null ) ;
153
+ BlobResultSegment segmentResult ;
154
+ try
155
+ {
156
+ segmentResult = await _blobContainer . ListBlobsSegmentedAsync ( string . Format ( "{0}/{1}" , _secretsBlobPath , prefix . ToLowerInvariant ( ) ) , null ) ;
157
+ }
158
+ catch ( Exception e )
159
+ {
160
+ LogErrorMessage ( "list" ) ;
161
+ throw e ;
162
+ }
122
163
return segmentResult . Results . Select ( x => x . Uri . ToString ( ) ) . ToArray ( ) ;
123
164
}
124
165
@@ -137,5 +178,10 @@ private async Task WriteToBlobAsync(string blobPath, string secretsContent)
137
178
await writer . WriteAsync ( secretsContent ) ;
138
179
}
139
180
}
181
+
182
+ protected virtual void LogErrorMessage ( string operation )
183
+ {
184
+ Logger ? . BlobStorageSecretRepoError ( operation , "AzureWebJobsStorage" ) ;
185
+ }
140
186
}
141
187
}
0 commit comments