I'm working on a cloud function event trigger in GCP to respond to messages on a topic. The following code works fine when I run locally, but when I deploy out to GCP, which triggers the Startup during deployment, my calls to SecretsManager end in "DeadlineExceeded." I'm not quite sure what the problem is. Relevant code is below. Any direction would be fantastic.
Startup.cs
public class Startup : FunctionsStartup
{
public override async void ConfigureServices(WebHostBuilderContext context, IServiceCollection services)
{
try
{
base.ConfigureServices(context, services);
Console.WriteLine("Configuring AppSettings");
services.Configure<AppSettings>(context.Configuration.GetSection("AppSettings"));
await ConfigureNonDev(context, services);
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.GetBaseException().Message);
throw;
}
}
private async Task ConfigureNonDev(WebHostBuilderContext context, IServiceCollection services)
{
Console.WriteLine("Configuring for non-Development machine");
Console.WriteLine("Initializing Secret Manager");
services.AddTransient<SecretManagerRepository>();
var sp = services.BuildServiceProvider();
var sm = sp.GetService<SecretManagerRepository>();
if (sm == null) Console.WriteLine("SecretManagerRepository is null");
var appSettings = sp.GetService<IOptions<AppSettings>>()?.Value;
if (appSettings == null) Console.WriteLine("appSettings is null");
if (appSettings.ProjectId == null) Console.WriteLine("appSettings.ProjectId is null");
Console.WriteLine("Retrieving API credentials");
var apiCreds = await sm.GetApiCredsAsync(appSettings.ProjectId, appSettings.APICredSecretId);
if (apiCreds == null)
Console.WriteLine("API Credentials are empty");
Console.WriteLine("Setting API credentials");
var apiConfiguration = new APICreds(apiCreds);
services.AddSingleton(apiConfiguration);
}
}
SecretManagerRepository.cs
public class SecretManagerRepository
{
private readonly string _secretVersionId = "latest";
private readonly SecretManagerServiceClient _secretManagerServiceClient;
public SecretManagerRepository()
{
_secretManagerServiceClient = SecretManagerServiceClient.Create();
}
public async Task<ApiCreds> GetApiCredsAsync(string projectId, string secretId)
{
Console.WriteLine($"Fetching secret: {projectId}/{secretId}");
SecretVersionName secretVersionName = new SecretVersionName(
projectId,
secretId,
_secretVersionId);
// This call dies after 30 seconds of waiting
var response = await _secretManagerServiceClient.AccessSecretVersionAsync(secretVersionName);
if(response.Payload == null || response.Payload.Data == null || response.Payload.Data.Length == 0)
{
Console.WriteLine("Secret payload not found");
}
return ProcessSecretPayload(response);
}
// A valid function named ProcessSecretPayload has been omitted. It's not the problem
}