Skip to content

Commit 147bc0b

Browse files
authored
Blob: Remove branch from cache universe (#102)
1 parent 89d947d commit 147bc0b

File tree

2 files changed

+6
-24
lines changed

2 files changed

+6
-24
lines changed

src/AzureBlobStorage/MSBuildCacheAzureBlobStoragePlugin.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
using BuildXL.Cache.MemoizationStore.Sessions;
2323
using Microsoft.Build.Experimental.ProjectCache;
2424
using Microsoft.MSBuildCache.Caching;
25-
using Microsoft.MSBuildCache.SourceControl;
2625

2726
namespace Microsoft.MSBuildCache.AzureBlobStorage;
2827

@@ -75,25 +74,17 @@ protected override async Task<ICacheClient> CreateCacheClientAsync(PluginLoggerB
7574

7675
ICacheSession localCacheSession = await StartCacheSessionAsync(context, localCache, "local");
7776

78-
// We want our caches to be secure by default. For Pipeline Caching, branches are isolated on the server-side.
79-
// For Blob L3, we need to isolate the cache namespace on the client-side. We do this by using the branch name as the cache namespace.
80-
// Note: The build still has access to broad access to the underlying Storage account, so this is *not* a true security boundary,
81-
// but rather a best effort attempt.
82-
83-
// The cache universe and namespace are directly applied to the name of the container, so we need to sanitize and summarize with hash.
84-
string @namespace = await Git.BranchNameAsync(logger, Settings.RepoRoot);
85-
string cacheContainer = $"{Settings.CacheUniverse}-{@namespace}";
86-
77+
// The cache universe and namespace are directly applied to the name of the container, so we need to sanitize and summarize with lowercase hash.
8778
#pragma warning disable CA1308 // Azure Storage only supports lowercase
88-
string cacheContainerHash = ContentHasher.GetContentHash(Encoding.UTF8.GetBytes(cacheContainer)).ToShortString(includeHashType: false).ToLowerInvariant();
79+
string cacheUniverse = ContentHasher.GetContentHash(Encoding.UTF8.GetBytes(Settings.CacheUniverse)).ToShortString(includeHashType: false).ToLowerInvariant();
8980
#pragma warning restore CA1308 // Azure Storage only supports lowercase
9081

91-
logger.LogMessage($"Using cache namespace '{cacheContainer}' as '{cacheContainerHash}'.");
82+
logger.LogMessage($"Using cache universe '{Settings.CacheUniverse}' as '{cacheUniverse}'.");
9283

9384
IAzureStorageCredentials credentials = CreateAzureStorageCredentials(Settings, cancellationToken);
9485

9586
#pragma warning disable CA2000 // Dispose objects before losing scope. Expected to be disposed by TwoLevelCache
96-
ICache remoteCache = CreateRemoteCache(new OperationContext(context, cancellationToken), cacheContainerHash, Settings.RemoteCacheIsReadOnly, credentials);
87+
ICache remoteCache = CreateRemoteCache(new OperationContext(context, cancellationToken), cacheUniverse, Settings.RemoteCacheIsReadOnly, credentials);
9788
#pragma warning restore CA2000 // Dispose objects before losing scope
9889

9990
ICacheSession remoteCacheSession = await StartCacheSessionAsync(context, remoteCache, "remote");
@@ -192,7 +183,7 @@ private static ICache CreateRemoteCache(OperationContext context, string cacheUn
192183
AzureBlobStorageCacheFactory.Configuration cacheConfig = new(
193184
ShardingScheme: new ShardingScheme(ShardingAlgorithm.SingleShard, [accountName]),
194185
Universe: cacheUniverse,
195-
Namespace: "0",
186+
Namespace: AzureBlobStorageCacheFactory.Configuration.DefaultNamespace,
196187
RetentionPolicyInDays: null,
197188
IsReadOnly: isReadOnly);
198189
return AzureBlobStorageCacheFactory.Create(context, cacheConfig, new StaticBlobCacheSecretsProvider(credentials)).Cache;

src/Common/SourceControl/Git.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,11 @@
1212

1313
namespace Microsoft.MSBuildCache.SourceControl;
1414

15-
public static class Git
15+
internal static class Git
1616
{
1717
// UTF8 - NO BOM
1818
private static readonly Encoding InputEncoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
1919

20-
public static async Task<string> BranchNameAsync(PluginLoggerBase logger, string repoRoot)
21-
{
22-
string branchName = await RunAsync(logger, repoRoot, "rev-parse --abbrev-ref HEAD",
23-
(_, stdout) => stdout.ReadToEndAsync(),
24-
(exitCode, result) => result,
25-
CancellationToken.None);
26-
return branchName.Trim();
27-
}
28-
2920
public static async Task<T> RunAsync<T>(
3021
PluginLoggerBase logger,
3122
string workingDir, string args,

0 commit comments

Comments
 (0)