|
4 | 4 | using System.Text; |
5 | 5 | using System.Threading.Tasks; |
6 | 6 | using Azure.Storage.Blobs; |
| 7 | +using Azure.Identity; |
7 | 8 | using Octokit; |
8 | 9 | using GitHubTeamUserStore.Constants; |
9 | 10 |
|
@@ -158,14 +159,40 @@ public async Task<IReadOnlyList<Team>> GetAllChildTeams(Team team) |
158 | 159 | /// <summary> |
159 | 160 | /// Upload the data to blob storage. Uses the BlobUriBuilder to get the blob information to created the |
160 | 161 | /// Blob clients and upload the data. |
| 162 | + /// Credentials: |
| 163 | + /// Instead of using DefaultAzureCredential [1] we use ChainedTokenCredential [2] which works |
| 164 | + /// as DefaultAzureCredential, but most importantly, it excludes ManagedIdentityCredential. |
| 165 | + /// We do so because there is an undesired managed identity available when we run this |
| 166 | + /// code in CI/CD pipelines, which takes priority over the desired AzureCliCredential coming |
| 167 | + /// from the calling AzureCLI@2 task. |
| 168 | + /// |
| 169 | + /// Running Locally: |
| 170 | + /// Your user needs to have Storage Blob Data Contributor access. This is done through |
| 171 | + /// https://ms.portal.azure.com/, selecting the azuresdkartifacts storage account, selecting Access Control (IAM) |
| 172 | + /// and adding Storage Blob Data Contributor then following the buttons at the bottom to assign this to your user. |
| 173 | + /// In Visual Studio select Tools-Options and then search for Azure and select Azure Service Authentication and |
| 174 | + /// authenticate. Once that's done the DefaultAzureCredential will use those creds. |
| 175 | + /// |
| 176 | + /// Running in a pipeline: |
| 177 | + /// Requires using the AzureCLI or AzurePowerShell task and azure subscription, which was already setup, |
| 178 | + /// is 'Azure SDK Artifacts' in both cases the exact line is as follows |
| 179 | + /// azureSubscription: 'Azure SDK Artifacts' |
| 180 | + /// The DefaultAzureCredential will use the creds setup in the task |
161 | 181 | /// </summary> |
162 | 182 | /// <param name="rawJson">The json string, representing the information that will be uploaded to blob storage.</param> |
163 | 183 | /// <param name="blobUriBuilder">BlobUriBuilder which contains the blob storage information.</param> |
164 | 184 | /// <returns></returns> |
165 | | - /// <exception cref="ApplicationException">If there is no AZURE_SDK_TEAM_USER_STORE_SAS in the environment</exception> |
166 | 185 | public async Task UploadDataToBlobStorage(string rawJson, BlobUriBuilder blobUriBuilder) |
167 | 186 | { |
168 | | - BlobServiceClient blobServiceClient = new BlobServiceClient(blobUriBuilder.ToUri()); |
| 187 | + var cred = new ChainedTokenCredential( |
| 188 | + new EnvironmentCredential(), |
| 189 | + new VisualStudioCredential(), |
| 190 | + new AzureCliCredential(), |
| 191 | + new AzurePowerShellCredential(), |
| 192 | + new InteractiveBrowserCredential() |
| 193 | + ); |
| 194 | + BlobServiceClient blobServiceClient = new BlobServiceClient(blobUriBuilder.ToUri(), cred); |
| 195 | + |
169 | 196 | BlobContainerClient blobContainerClient = blobServiceClient.GetBlobContainerClient(blobUriBuilder.BlobContainerName); |
170 | 197 | BlobClient blobClient = blobContainerClient.GetBlobClient(blobUriBuilder.BlobName); |
171 | 198 | await blobClient.UploadAsync(BinaryData.FromString(rawJson), overwrite: true); |
|
0 commit comments