Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blob: Remove branch from cache universe #102

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions src/AzureBlobStorage/MSBuildCacheAzureBlobStoragePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
using BuildXL.Cache.MemoizationStore.Sessions;
using Microsoft.Build.Experimental.ProjectCache;
using Microsoft.MSBuildCache.Caching;
using Microsoft.MSBuildCache.SourceControl;

namespace Microsoft.MSBuildCache.AzureBlobStorage;

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

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

// We want our caches to be secure by default. For Pipeline Caching, branches are isolated on the server-side.
// 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.
// Note: The build still has access to broad access to the underlying Storage account, so this is *not* a true security boundary,
// but rather a best effort attempt.

// The cache universe and namespace are directly applied to the name of the container, so we need to sanitize and summarize with hash.
string @namespace = await Git.BranchNameAsync(logger, Settings.RepoRoot);
string cacheContainer = $"{Settings.CacheUniverse}-{@namespace}";

// The cache universe and namespace are directly applied to the name of the container, so we need to sanitize and summarize with lowercase hash.
#pragma warning disable CA1308 // Azure Storage only supports lowercase
string cacheContainerHash = ContentHasher.GetContentHash(Encoding.UTF8.GetBytes(cacheContainer)).ToShortString(includeHashType: false).ToLowerInvariant();
string cacheUniverse = ContentHasher.GetContentHash(Encoding.UTF8.GetBytes(Settings.CacheUniverse)).ToShortString(includeHashType: false).ToLowerInvariant();
#pragma warning restore CA1308 // Azure Storage only supports lowercase

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

IAzureStorageCredentials credentials = CreateAzureStorageCredentials(Settings, cancellationToken);

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

ICacheSession remoteCacheSession = await StartCacheSessionAsync(context, remoteCache, "remote");
Expand Down Expand Up @@ -192,7 +183,7 @@ private static ICache CreateRemoteCache(OperationContext context, string cacheUn
AzureBlobStorageCacheFactory.Configuration cacheConfig = new(
ShardingScheme: new ShardingScheme(ShardingAlgorithm.SingleShard, [accountName]),
Universe: cacheUniverse,
Namespace: "0",
Namespace: AzureBlobStorageCacheFactory.Configuration.DefaultNamespace,
RetentionPolicyInDays: null,
IsReadOnly: isReadOnly);
return AzureBlobStorageCacheFactory.Create(context, cacheConfig, new StaticBlobCacheSecretsProvider(credentials)).Cache;
Expand Down
11 changes: 1 addition & 10 deletions src/Common/SourceControl/Git.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,11 @@

namespace Microsoft.MSBuildCache.SourceControl;

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

public static async Task<string> BranchNameAsync(PluginLoggerBase logger, string repoRoot)
{
string branchName = await RunAsync(logger, repoRoot, "rev-parse --abbrev-ref HEAD",
(_, stdout) => stdout.ReadToEndAsync(),
(exitCode, result) => result,
CancellationToken.None);
return branchName.Trim();
}

public static async Task<T> RunAsync<T>(
PluginLoggerBase logger,
string workingDir, string args,
Expand Down