public UserDataStorageService(
     IOptions <CosmosConfiguration> options,
     SasTokenService sasTokens,
     ArmTemplatesMap armTemplatesMap,
     StaticAssetsMap staticAssetsMap,
     IOptions <WebServerConfiguration> webServerOptions,
     IOptions <BatchConfiguration> batchOptions,
     ValidationService validationService)
     : base(options, sasTokens)
 {
     ArmTemplatesMap        = armTemplatesMap;
     StaticAssetsMap        = staticAssetsMap;
     BatchOptions           = batchOptions;
     ValidationService      = validationService;
     WebServerConfiguration = webServerOptions.Value;
 }
Example #2
0
        public void ConfigureServices()
        {
            var services = new ServiceCollection();

            services.AddMemoryCache();

            services.AddOptions();
            services.Configure <SearchConfiguration>(options =>
            {
                Configuration.GetSection("search").Bind(options);
                SearchConfiguration.Validate(options);
            });
            services.Configure <CosmosConfiguration>(options =>
            {
                Configuration.GetSection("documents").Bind(options);
                CosmosConfiguration.Validate(options);
            });
            services.Configure <StorageConfiguration>((config) =>
            {
                Configuration.GetSection("storage").Bind(config);
                StorageConfiguration.Validate(config);
            });
            services.Configure <BatchConfiguration>((config) =>
            {
                Configuration.GetSection("batch").Bind(config);
                BatchConfiguration.Validate(config);
            });

            var armTemplatesMap = new ArmTemplatesMap();

            services.AddSingleton(armTemplatesMap);
            var staticAssetsMap = new StaticAssetsMap();

            services.AddSingleton(staticAssetsMap);

            services.AddSingleton <ApplicationJobs>();
            services.AddSingleton <DatasetStorageService>();
            services.AddSingleton <DatasetOwnersService>();
            services.AddSingleton <DatasetEditStorageService>();
            services.AddSingleton <UserDataStorageService>();
            services.AddSingleton <SasTokenService>();
            services.AddSingleton <ValidationService>();
            services.AddSingleton <DatasetSearchService>();
            services.AddSingleton <FileSearchService>();

            ServiceProvider = services.BuildServiceProvider();
        }
Example #3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddApplicationInsightsTelemetry();

            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddSingleton <IConfiguration>(Configuration);

            services.AddOptions();
            services.Configure <IdentityProviderConfiguration>(options =>
            {
                Configuration.GetSection("azuread").Bind(options);
                IdentityProviderConfiguration.Validate(options);
            });
            services.Configure <SearchConfiguration>(options =>
            {
                Configuration.GetSection("search").Bind(options);
                SearchConfiguration.Validate(options);
            });
            services.Configure <CosmosConfiguration>(options =>
            {
                Configuration.GetSection("documents").Bind(options);
                CosmosConfiguration.Validate(options);
            });
            services.Configure <StorageConfiguration>((config) =>
            {
                Configuration.GetSection("storage").Bind(config);
                StorageConfiguration.Validate(config);
            });
            services.Configure <BatchConfiguration>((config) =>
            {
                Configuration.GetSection("batch").Bind(config);
                BatchConfiguration.Validate(config);
            });
            services.Configure <AppInsightsConfiguration>((config) =>
            {
                Configuration.GetSection("applicationinsights").Bind(config);
                AppInsightsConfiguration.Validate(config);
            });

            var armTemplatesMap = new ArmTemplatesMap();

            services.AddSingleton(armTemplatesMap);
            var staticAssetsMap = new StaticAssetsMap();

            services.AddSingleton(staticAssetsMap);

            services.AddMemoryCache();



            //services
            //    .AddAuthentication(options =>
            //    {
            //        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            //        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            //    })
            //    .AddJwtBearer(options =>
            //    {
            //        options.Audience = Configuration["azureADConfig:clientId"];
            //        options.Authority = Configuration["azureADConfig:authority"];
            //        options.Events = new JwtBearerEvents
            //        {
            //            OnMessageReceived = FetchAuthTokenFromCookie,
            //            OnTokenValidated = async context =>
            //            {
            //                var principal = context.Principal;
            //                var ODREmailList = Configuration["ODRAdminList"].Split(";");
            //            }
            //        };
            //    });

            //services
            //    .AddAuthentication(options =>
            //    {
            //        options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
            //    })
            //    .AddJwtBearer(options =>
            //    {
            //        options.Audience = Configuration["azureADConfig:clientId"];
            //        options.Authority = Configuration["azureADConfig:authority"];

            //        options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
            //        {
            //            ValidAudience = Configuration["azureADConfig:clientId"],
            //            ValidIssuer = $"https://login.microsoftonline.com/" + Configuration["azureADConfig:tenantId"] + "/v2.0"
            //        };

            //        options.Events = new JwtBearerEvents
            //        {
            //            OnMessageReceived = FetchAuthTokenFromCookie,
            //            //OnTokenValidated = async context =>
            //            //{
            //            //    var principal = context.Principal;

            //            //    if (Configuration.GetChildren().Any(item => item.Key == "ODRAdminList") && principal.Claims.Any(c => c.Type == "preferred_username"))
            //            //    {

            //            //        var ODRAdminsList = Configuration["ODRAdminList"].Split(";").ToList();
            //            //        var testSubject = principal.Claims.FirstOrDefault(c => c.Type == "preferred_username").Value;

            //            //        if (ODRAdminsList.Contains(testSubject))
            //            //        {

            //            //            var claims = new List<Claim>
            //            //            {
            //            //                new Claim(ClaimTypes.Role, "ODRAdmin")
            //            //            };
            //            //            var appIdentity = new ClaimsIdentity(claims);
            //            //            principal.AddIdentity(appIdentity);

            //            //        }
            //            //    }
            //            //}
            //        };
            //    });

            services
            .AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                var tenantName          = Configuration["AzureAD:Tenant"].Split('.').First();
                var policyName          = Configuration["AzureAD:Policy"];
                var audience            = Configuration["AzureAD:Audience"];
                options.MetadataAddress = $"https://{tenantName}.b2clogin.com/{tenantName}.onmicrosoft.com/{policyName}/v2.0/.well-known/openid-configuration";
                options.Audience        = audience;
                options.Events          = new JwtBearerEvents
                {
                    OnMessageReceived      = FetchAuthTokenFromCookie,
                    OnAuthenticationFailed = context =>
                    {
                        var ctx = context;
                        return(Task.FromResult(0));
                    },
                };
            });

            services.AddMvc();

            services
            .AddAuthorization(options =>
            {
                options.AddPolicy(PolicyNames.MustBeInAdminGroup, policy =>
                {
                    var authorizedAdminUsers = Configuration["AuthorizedAdminUsers"];
                    policy.Requirements.Add(new AzureActiveDirectoryGroupRequirement(authorizedAdminUsers));
                });
            });

            services.AddSingleton <IAuthorizationHandler, AzureActiveDirectoryGroupHandler>();

            //if (CurrentEnvironment.IsDevelopment())
            //{
            //    services.AddSingleton<IAuthorizationHandler, DevelopmentOnlyNoAuthDirectoryGroupHandler>();
            //}
            //else
            //{
            //    services.AddSingleton<IAuthorizationHandler, AzureActiveDirectoryGroupHandler>();
            //}

            services.AddScoped <DatasetSearchService>();
            services.AddScoped <UserDataSearchService>();
            services.AddScoped <DatasetStorageService>();
            services.AddScoped <AppInsightsService>();
            services.AddScoped <UserDataStorageService>();
            services.AddScoped <LicenseStorageService>();
            services.AddScoped <SasTokenService>();
            services.AddScoped <ValidationService>();
            services.AddScoped <ApplicationJobs>();

            // In production, the Angular files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ui-app";
            });

            // Add Swagger generator
            if (Configuration.GetValue <bool>("Application:EnableSwagger"))
            {
                services.AddSwaggerGen(c =>
                {
                    c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
                    {
                        Title   = "MSR ODR Admin API",
                        Version = "v1"
                    });
                });
            }
        }
        public async Task <string> GenerateDeploymentTemplate(
            Guid datasetId,
            Guid deploymentId,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            // Get the deployment instance document
            DeploymentStorage deployment;
            var options = new RequestOptions
            {
                PartitionKey = new PartitionKey(datasetId.ToString())
            };
            var uri = UserDataDocumentUriById(deploymentId.ToString());

            try
            {
                var response = await Client.ReadDocumentAsync <DeploymentStorage>(uri, options);

                deployment = response.Document;
            }
            catch (Microsoft.Azure.Documents.DocumentClientException ex)
            {
                if (ex.StatusCode == HttpStatusCode.NotFound)
                {
                    return(null);
                }
                throw;
            }

            // Get the ARM template
            ArmTemplatesItem templateItem;
            Document         templateDoc;

            if (!ArmTemplatesMap.TryGetValue(deployment.DeploymentId, out templateItem))
            {
                return(null);
            }
            options = new RequestOptions
            {
                PartitionKey = new PartitionKey(WellKnownIds.ConfigurationDatasetId.ToString())
            };
            uri = DatasetDocumentUriById(templateItem.Id);
            try
            {
                var response = await Client.ReadDocumentAsync(uri, options);

                templateDoc = response.Resource;
            }
            catch (Microsoft.Azure.Documents.DocumentClientException ex)
            {
                if (ex.StatusCode == HttpStatusCode.NotFound)
                {
                    return(null);
                }
                throw;
            }

            // Get the dataset storage document
            DatasetStorageItem dataset;

            options = new RequestOptions
            {
                PartitionKey = new PartitionKey(datasetId.ToString())
            };
            uri = DatasetDocumentUriById(datasetId.ToString());
            try
            {
                var response = await Client.ReadDocumentAsync <DatasetStorageItem>(uri, options);

                dataset = response.Document;
            }
            catch (Microsoft.Azure.Documents.DocumentClientException ex)
            {
                if (ex.StatusCode == HttpStatusCode.NotFound)
                {
                    return(null);
                }
                throw;
            }

            if (deployment.DeploymentId.Contains("synapse"))
            {
                var doc = templateDoc.GetPropertyValue <JObject>("template");

                var template        = JsonConvert.SerializeObject(doc);
                var templateObj     = JsonConvert.DeserializeObject <Dictionary <string, object> >(template);
                var variablesString = JsonConvert.SerializeObject(templateObj["variables"]);
                var variablesObj    = JsonConvert.DeserializeObject <Dictionary <string, object> >(variablesString);
                variablesObj["givenDataSet"] = deployment.StorageUri;
                templateObj["variables"]     = variablesObj;
                return(JsonConvert.SerializeObject(templateObj, Formatting.Indented));
            }
            else
            {
                // Set default values for ARM template parameters
                var    defaultUserName = "******";
                var    nameRegex       = new Regex(@"[^a-z0-9]", RegexOptions.IgnoreCase);
                string deploymentName  = string.IsNullOrWhiteSpace(dataset.Name)
                    ? "datasetname"
                    : nameRegex.Replace(dataset.Name, (m) => string.Empty);
                var parametersMap = new Dictionary <string, string>
                {
                    { "adminUsername", defaultUserName },
                    { "datasetUrl", deployment.StorageUri },
                    { "datasetPath", $"/home/{defaultUserName}/datasets/{deploymentName}" },
                    { "datasetDirectory", $"C:\\Datasets\\{deploymentName}" },
                };
                var variablesMap = new Dictionary <string, string>
                {
                    { "assetsRootUrl", $"{WebServerConfiguration.URL}azure-deploy/assets" },
                };
                var doc = templateDoc.GetPropertyValue <JObject>("template");
                doc
                .GetValue("parameters")
                .Cast <JProperty>()
                .Select(p => new
                {
                    p.Name,
                    Value = (JObject)p.Value
                })
                .ToList()
                .ForEach(v =>
                {
                    string value;
                    if (parametersMap.TryGetValue(v.Name, out value))
                    {
                        v.Value["defaultValue"] = value;
                    }
                });
                doc
                .GetValue("variables")
                .Cast <JProperty>()
                .ToList()
                .ForEach(prop =>
                {
                    string value;
                    if (variablesMap.TryGetValue(prop.Name, out value))
                    {
                        prop.Value = value;
                    }
                });
                return(Newtonsoft.Json.JsonConvert.SerializeObject(doc, Newtonsoft.Json.Formatting.Indented));
            }
        }