|  | 
| 6 | 6 | 
 | 
| 7 | 7 | namespace Dfe.Analytics; | 
| 8 | 8 | 
 | 
| 9 |  | -#pragma warning disable CA1812 | 
| 10 | 9 | internal class DfeAnalyticsConfigureOptions(IConfiguration configuration) : IConfigureOptions<DfeAnalyticsOptions> | 
| 11 |  | -#pragma warning restore CA1812 | 
| 12 | 10 | { | 
| 13 |  | -    private readonly IConfiguration _configuration = configuration; | 
| 14 |  | - | 
| 15 | 11 |     public void Configure(DfeAnalyticsOptions options) | 
| 16 | 12 |     { | 
| 17 | 13 |         ArgumentNullException.ThrowIfNull(options); | 
| 18 | 14 | 
 | 
| 19 |  | -        var section = _configuration.GetSection(Constants.RootConfigurationSectionName); | 
|  | 15 | +        var section = configuration.GetSection(Constants.ConfigurationSectionName); | 
| 20 | 16 | 
 | 
| 21 | 17 |         section.AssignConfigurationValueIfNotEmpty("DatasetId", v => options.DatasetId = v); | 
| 22 | 18 |         section.AssignConfigurationValueIfNotEmpty("Environment", v => options.Environment = v); | 
| 23 | 19 |         section.AssignConfigurationValueIfNotEmpty("Namespace", v => options.Namespace = v); | 
| 24 | 20 |         section.AssignConfigurationValueIfNotEmpty("TableId", v => options.TableId = v); | 
| 25 | 21 |         section.AssignConfigurationValueIfNotEmpty("ProjectId", v => options.ProjectId = v); | 
|  | 22 | +        section.AssignConfigurationValueIfNotEmpty("Audience", v => | 
|  | 23 | +        { | 
|  | 24 | +            options.FederatedAksAuthentication ??= new(); | 
|  | 25 | +            options.FederatedAksAuthentication.Audience = v; | 
|  | 26 | +        }); | 
|  | 27 | +        section.AssignConfigurationValueIfNotEmpty("GenerateAccessTokenUrl", v => | 
|  | 28 | +        { | 
|  | 29 | +            options.FederatedAksAuthentication ??= new(); | 
|  | 30 | +            options.FederatedAksAuthentication.ServiceAccountImpersonationUrl = v; | 
|  | 31 | +        }); | 
| 26 | 32 | 
 | 
| 27 | 33 |         var credentialsJson = section["CredentialsJson"]; | 
| 28 |  | - | 
| 29 | 34 |         if (!string.IsNullOrEmpty(credentialsJson)) | 
| 30 | 35 |         { | 
| 31 | 36 |             using var credentialsJsonDoc = JsonDocument.Parse(credentialsJson); | 
|  | 37 | +            AssignConfigurationFromCredentialsJson(options, credentialsJsonDoc); | 
|  | 38 | +        } | 
|  | 39 | +    } | 
|  | 40 | + | 
|  | 41 | +    private void AssignConfigurationFromCredentialsJson(DfeAnalyticsOptions options, JsonDocument credentialsJson) | 
|  | 42 | +    { | 
|  | 43 | +        if (options.ProjectId is null && | 
|  | 44 | +            credentialsJson.RootElement.TryGetProperty("project_id", out var projectIdElement)) | 
|  | 45 | +        { | 
|  | 46 | +            options.ProjectId = projectIdElement.GetString(); | 
|  | 47 | +        } | 
| 32 | 48 | 
 | 
| 33 |  | -            // We don't have ProjectId configured explicitly; see if it's set in the JSON credentials | 
| 34 |  | -            if (options.ProjectId is null && | 
| 35 |  | -                credentialsJsonDoc.RootElement.TryGetProperty("project_id", out var projectIdElement) && | 
| 36 |  | -                projectIdElement.ValueKind == JsonValueKind.String) | 
|  | 49 | +        if (options.FederatedAksAuthentication?.Audience is null && | 
|  | 50 | +            credentialsJson.RootElement.TryGetProperty("audience", out var audienceElement)) | 
|  | 51 | +        { | 
|  | 52 | +            options.FederatedAksAuthentication ??= new(); | 
|  | 53 | +            options.FederatedAksAuthentication.Audience = audienceElement.GetString()!; | 
|  | 54 | +        } | 
|  | 55 | + | 
|  | 56 | +        if (options.FederatedAksAuthentication?.ServiceAccountImpersonationUrl is null && | 
|  | 57 | +            credentialsJson.RootElement.TryGetProperty("service_account_impersonation_url", out var impersonationUrlElement)) | 
|  | 58 | +        { | 
|  | 59 | +            options.FederatedAksAuthentication ??= new(); | 
|  | 60 | +            options.FederatedAksAuthentication.ServiceAccountImpersonationUrl = impersonationUrlElement.GetString()!; | 
|  | 61 | +        } | 
|  | 62 | + | 
|  | 63 | +        if (options.BigQueryClient is null && options.ProjectId is { } projectId) | 
|  | 64 | +        { | 
|  | 65 | +            if (credentialsJson.RootElement.TryGetProperty("private_key", out _)) | 
| 37 | 66 |             { | 
| 38 |  | -                options.ProjectId = projectIdElement.GetString(); | 
|  | 67 | +                options.BigQueryClient = BigQueryClient.Create( | 
|  | 68 | +                    projectId, | 
|  | 69 | +                    GoogleCredential.FromJson(credentialsJson.ToString())); | 
| 39 | 70 |             } | 
| 40 |  | - | 
| 41 |  | -            if (credentialsJsonDoc.RootElement.TryGetProperty("private_key", out _) && options.ProjectId is string projectId) | 
|  | 71 | +            else if (Environment.GetEnvironmentVariable(FederatedAksSubjectTokenProvider.TokenPathEnvironmentVariableName) is not null && | 
|  | 72 | +                options.FederatedAksAuthentication is { Audience: { } audience, ServiceAccountImpersonationUrl: { } serviceAccountImpersonationUrl }) | 
| 42 | 73 |             { | 
| 43 |  | -                var creds = GoogleCredential.FromJson(credentialsJson); | 
| 44 |  | -                options.BigQueryClient = BigQueryClient.Create(projectId, creds); | 
|  | 74 | +                options.BigQueryClient = BigQueryClient.Create( | 
|  | 75 | +                    projectId, | 
|  | 76 | +                    GoogleCredential.FromProgrammaticExternalAccountCredential( | 
|  | 77 | +                        new ProgrammaticExternalAccountCredential( | 
|  | 78 | +                            new ProgrammaticExternalAccountCredential.Initializer( | 
|  | 79 | +                                tokenUrl: "https://sts.googleapis.com/v1/token", | 
|  | 80 | +                                audience, | 
|  | 81 | +                                FederatedAksSubjectTokenProvider.SubjectTokenType, | 
|  | 82 | +#pragma warning disable CA2000 | 
|  | 83 | +                                new FederatedAksSubjectTokenProvider()) | 
|  | 84 | +                            { | 
|  | 85 | +                                ServiceAccountImpersonationUrl = serviceAccountImpersonationUrl | 
|  | 86 | +                            }))); | 
|  | 87 | +#pragma warning restore CA2000 | 
| 45 | 88 |             } | 
| 46 | 89 |         } | 
| 47 | 90 |     } | 
|  | 
0 commit comments