Skip to content

Commit

Permalink
feat: Use self-signed JWTs in Spanner clients
Browse files Browse the repository at this point in the history
  • Loading branch information
jskeet committed Jun 6, 2022
1 parent e029508 commit d465906
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ internal sealed class SpannerClientCreationOptions : IEquatable<SpannerClientCre
private static async Task<ChannelCredentials> CreatedScopedDefaultCredentials()
{
var appDefaultCredentials = await GoogleCredential.GetApplicationDefaultAsync().ConfigureAwait(false);
// TODO: Use a JWT, so no scoping?
return appDefaultCredentials.CreateScoped(SpannerClient.DefaultScopes).ToChannelCredentials();
return ConvertGoogleCredential(appDefaultCredentials);
}

/// <summary>
Expand Down Expand Up @@ -174,9 +173,19 @@ internal async Task<ChannelCredentials> GetCredentialsAsync()
}
}

// TODO: Use JWT instead? (No scopes.)
// TODO: Use an async overload
return GoogleCredential.FromFile(file).CreateScoped(SpannerClient.DefaultScopes).ToChannelCredentials();
var credential = await GoogleCredential.FromFileAsync(file, cancellationToken: default).ConfigureAwait(false);
return ConvertGoogleCredential(credential);
}

private static ChannelCredentials ConvertGoogleCredential(GoogleCredential credential)
{
credential = credential.CreateScoped(SpannerClient.DefaultScopes);
// Use self-signed JWTs for service accounts.
if (credential.UnderlyingCredential is ServiceAccountCredential serviceCredential)
{
credential = GoogleCredential.FromServiceAccountCredential(serviceCredential.WithUseJwtAccessWithScopes(true));
}
return credential.ToChannelCredentials();
}
}
}

0 comments on commit d465906

Please sign in to comment.