Пример #1
0
 public void Cleanup()
 {
     if (!string.IsNullOrEmpty(_cloudServiceToDelete))
     {
         Assert.IsTrue(AzureUtility.DeleteCloudServiceWithRetry(publishSettingsFilePath, _cloudServiceToDelete), "Failed to delete cloud service.");
     }
 }
Пример #2
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            string serverName = request.DataStore.GetValue("ASServerUrl");
            string username   = request.DataStore.GetValue("ASAdmin") ??
                                AzureUtility.GetEmailFromToken(request.DataStore.GetJson("AzureToken"));
            string password = request.DataStore.GetValue("ASAdminPassword");

            string connectionString = string.Empty;

            if (serverName.ToLowerInvariant().StartsWith("asazure"))
            {
                connectionString += "Provider=MSOLAP;";
            }

            connectionString += $"Data Source={serverName};";

            if (!string.IsNullOrEmpty(password))
            {
                connectionString +=
                    $"User ID={username};Password={password};Persist Security Info=True; Impersonation Level=Impersonate;UseADALCache=0";
            }

            try
            {
                Server server = new Server();
                server.Connect(connectionString);
                request.DataStore.AddToDataStore("ASConnectionString", connectionString, DataStoreType.Private);
            }
            catch (Exception ex)
            {
                return(new ActionResponse(ActionStatus.FailureExpected, null, ex, null));
            }

            return(new ActionResponse(ActionStatus.Success));
        }
        public static string GetASConnectionString(ActionRequest request, JToken azureToken, string serverUrl)
        {
            string connectionString = $"Provider=MSOLAP;Data Source={serverUrl}";
            Uri    uri      = new Uri(serverUrl);
            string resource = "https://" + uri.Host;

            var    asToken       = AzureTokenUtility.GetTokenForResource(request, azureToken, resource);
            string asAccessToken = AzureUtility.GetAccessToken(asToken);

            if (!string.IsNullOrEmpty(asAccessToken))
            {
                connectionString +=
                    $";Password={asAccessToken};UseADALCache=0";
            }

            try
            {
                Server server = new Server();
                server.Connect(connectionString);
                request.DataStore.AddToDataStore("ASConnectionString", connectionString, DataStoreType.Private);
                return(connectionString);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #4
0
        public async Task GetAzureEmail()
        {
            DataStore dataStore = new DataStore();
            var       datastore = await AAD.GetUserTokenFromPopup();

            var emailAddress = AzureUtility.GetEmailFromToken(datastore.GetJson("AzureToken"));
        }
Пример #5
0
        public void Will_map_severity_string_to_number(string severityName, int expected)
        {
            var res = AzureUtility.MapSeverity(severityName);


            res.ShouldBe(expected);
        }
Пример #6
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            var azureToken     = request.DataStore.GetJson("AzureToken")["access_token"].ToString();
            var subscription   = request.DataStore.GetJson("SelectedSubscription")["SubscriptionId"].ToString();
            var resourceGroup  = request.DataStore.GetValue("SelectedResourceGroup");
            var deploymentName = request.DataStore.GetValue("DeploymentName") ?? "AzureMLDeployment";

            var workspaceName      = request.DataStore.GetValue("WorkspaceName");
            var storageAccountName = request.DataStore.GetValue("StorageAccountName");
            var planName           = request.DataStore.GetValue("PlanName") ?? "azuremlplan";
            var skuName            = request.DataStore.GetValue("SkuName") ?? "S1";
            var skuTier            = request.DataStore.GetValue("SkuTier") ?? "Standard";
            var skuCapacity        = request.DataStore.GetValue("SkuCapacity") ?? "1";

            // Get email address

            var param = new AzureArmParameterGenerator();

            param.AddStringParam("name", workspaceName);
            param.AddStringParam("resourcegroup", resourceGroup);
            param.AddStringParam("subscription", subscription);
            param.AddStringParam("newStorageAccountName", storageAccountName);
            param.AddStringParam("planName", planName);
            param.AddStringParam("skuName", skuName);
            param.AddStringParam("skuTier", skuTier);
            param.AddStringParam("skuCapacity", skuCapacity);
            param.AddStringParam("ownerEmail", AzureUtility.GetEmailFromToken(request.DataStore.GetJson("AzureToken")));

            SubscriptionCloudCredentials creds = new TokenCloudCredentials(subscription, azureToken);

            Microsoft.Azure.Management.Resources.ResourceManagementClient client = new ResourceManagementClient(creds);

            var armTemplate      = JsonUtility.GetJObjectFromJsonString(System.IO.File.ReadAllText(Path.Combine(request.ControllerModel.SiteCommonFilePath, "Service/Arm/azureml.json")));
            var armParamTemplate = JsonUtility.GetJObjectFromObject(param.GetDynamicObject());

            armTemplate.Remove("parameters");
            armTemplate.Add("parameters", armParamTemplate["parameters"]);


            var deployment = new Microsoft.Azure.Management.Resources.Models.Deployment()
            {
                Properties = new DeploymentPropertiesExtended()
                {
                    Template   = armTemplate.ToString(),
                    Parameters = JsonUtility.GetEmptyJObject().ToString()
                }
            };

            var validate = client.Deployments.ValidateAsync(resourceGroup, deploymentName, deployment, new CancellationToken()).Result;

            if (!validate.IsValid)
            {
                return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetJObjectFromObject(validate), null,
                                          DefaultErrorCodes.DefaultErrorCode, $"Azure:{validate.Error.Message} Details:{validate.Error.Details}"));
            }

            var deploymentItem = client.Deployments.CreateOrUpdateAsync(resourceGroup, deploymentName, deployment, new CancellationToken()).Result;

            return(new ActionResponse(ActionStatus.Success));
        }
Пример #7
0
        private static void CreateStandardStates(IConfiguration configuration, IQuiltContextFactory quiltContextFactory)
        {
            var text  = AzureUtility.LoadAzureStringBlob(configuration, "database-load", "states.csv");
            var lines = text.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);

            using var ctx = quiltContextFactory.Create();

            var dbCountry = ctx.Countries.Where(r => r.CountryCode == "US").SingleOrDefault();

            if (dbCountry == null)
            {
                dbCountry = new Country()
                {
                    CountryCode = "US",
                    Name        = "United States"
                };
                _ = ctx.Countries.Add(dbCountry);
            }

            foreach (var line in lines)
            {
                var fields = line.Split(new char[] { ',' });

                var stateCode   = fields[0];
                var name        = fields[1];
                var countryCode = fields[2];

                CreateState(ctx, stateCode, name, countryCode);
            }

            _ = ctx.SaveChanges();
        }
 public void Cleanup()
 {
     if (!string.IsNullOrEmpty(_webSiteToDelete))
     {
         Assert.IsTrue(AzureUtility.DeleteWebSiteWithRetry(publishSettingsFilePath, _webSiteToDelete));
     }
 }
Пример #9
0
        public FileStreamResult StreamPrivate(Guid id)
        {
            string videoSecurity = TempData["VideoSecurity"] as string;

            if (!("VideoEdit" == videoSecurity || "MessageFromUrl" == videoSecurity || "MessagePreview" == videoSecurity))
            {
                throw new Exception("Video request is not authorized.");
            }

            Video vid = _videoRepository.GetById(id);

            BlobContainer container = AzureUtility.GetAzureContainer(vid.Path);

            BlobContents   contents = new BlobContents(new MemoryStream());
            BlobProperties blob     = container.GetBlob(vid.Path.Substring(vid.Path.IndexOf("/") + 1), contents, false);

            if (blob.ContentType == null)
            {
                throw new FormatException("No content type set for blob.");
            }

            Stream stream = contents.AsStream;

            stream.Seek(0, SeekOrigin.Begin);
            return(File(stream, blob.ContentType));
        }
Пример #10
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            var azureToken   = request.DataStore.GetJson("AzureToken");
            var subscription = request.DataStore.GetJson("SelectedSubscription", "SubscriptionId");

            JObject graphToken = AzureTokenUtility.GetTokenForResource(request, azureToken, "https://graph.windows.net");

            var tenantId = AzureUtility.GetTenantFromToken(request.DataStore.GetJson("AzureToken"));

            // Generate new key for ClientSecret
            string key          = GetNewKey();
            string graphUriBase = "https://graph.windows.net/{0}/applications";

            string graphApi = string.Format(graphUriBase, tenantId);

            AzureHttpClient client  = new AzureHttpClient(graphToken["access_token"].ToString(), subscription);
            dynamic         payload = new ExpandoObject();

            payload.displayName             = "solutiontemplate";
            payload.availableToOtherTenants = false;
            payload.homepage          = "www.test.com";
            payload.identifierUris    = new string[1];
            payload.identifierUris[0] = "https://test.com/" + RandomGenerator.GetRandomLowerCaseCharacters(10);

            payload.passwordCredentials              = new ExpandoObject[1];
            payload.passwordCredentials[0]           = new ExpandoObject();
            payload.passwordCredentials[0].startDate = DateTime.UtcNow.ToString("o");
            payload.passwordCredentials[0].endDate   = DateTime.UtcNow.AddYears(3).ToString("o");
            payload.passwordCredentials[0].keyId     = Guid.NewGuid();
            payload.passwordCredentials[0].value     = key;

            string body = JsonUtility.GetJsonStringFromObject(payload);

            var response = await client.ExecuteGenericRequestWithHeaderAsync(HttpMethod.Post, graphApi + "?api-version=1.6", body);

            string responseBody = await response.Content.ReadAsStringAsync();

            JObject responseBodyObj = JsonUtility.GetJObjectFromJsonString(responseBody);

            if (response.IsSuccessStatusCode)
            {
                string appId = responseBodyObj["appId"].ToString();
                string obbId = responseBodyObj["objectId"].ToString();

                responseBodyObj.Add("SPNAppId", appId);
                responseBodyObj.Add("SPNKey", key);
                responseBodyObj.Add("SPNUser", "app:" + appId + "@" + tenantId);
                responseBodyObj.Add("SPNTenantId", tenantId);

                // Delete the SPN if required
                //string graphUriBaseWithApplication = "https://graph.windows.net/{0}/applications/{1}";
                //string graphApiWithApp = string.Format(graphUriBaseWithApplication, tenantId, obbId);
                //response = await client.ExecuteGenericRequestWithHeaderAsync(HttpMethod.Delete, graphApiWithApp + "?api-version=1.6", body);

                return(new ActionResponse(ActionStatus.Success, responseBodyObj, true));
            }

            return(new ActionResponse(ActionStatus.Failure, responseBody, null, null, "Unable to create a Service Principal"));
        }
Пример #11
0
        public static JToken AzureLoad(IConfiguration configuration, string name)
        {
            var text = AzureUtility.LoadAzureStringBlob(configuration, "libraries", string.Format("{0}.json", name));

            var json = JToken.Parse(text);

            return(json);
        }
Пример #12
0
        public void Will_parse_resource_group_name_from_full_resource_id()
        {
            string id  = $@"/subscriptions/{AzureEnvVars.SubscriptionId}/resourcegroups/blog/providers/microsoft.operationalinsights/workspaces/";
            string res = AzureUtility.ParseResourceGroupName(id);

            _testOutputHelper.WriteLine(res);

            res.ShouldBe("blog");
        }
Пример #13
0
        public static string GetASConnectionString(ActionRequest request, JToken azureToken, string serverUrl)
        {
            if (string.IsNullOrEmpty(serverUrl))
            {
                throw new ArgumentNullException(nameof(serverUrl));
            }

            if (!Uri.IsWellFormedUriString(serverUrl, UriKind.Absolute))
            {
                throw new ArgumentException("Invalid URL specified for Analysis Services", nameof(serverUrl));
            }

            string connectionString = $"Provider=MSOLAP;Data Source={serverUrl}";
            Uri    uri      = new Uri(serverUrl);
            string resource = $"{Uri.UriSchemeHttps}://{uri.Host}";

            var    asToken       = AzureTokenUtility.GetTokenForResourceFromExistingToken("as", request.Info.WebsiteRootUrl, azureToken, resource);
            string asAccessToken = AzureUtility.GetAccessToken(asToken);

            if (!string.IsNullOrEmpty(asAccessToken))
            {
                connectionString += $";Password={asAccessToken};UseADALCache=0";
            }

            Server server = null;

            try
            {
                server = new Server();
                server.Connect(connectionString);
                server.Disconnect(true);
                request.DataStore.AddToDataStore("ASConnectionString", connectionString, DataStoreType.Private);

                return(connectionString);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (server != null)
                {
                    // In theory, we could end up here with a connected server object
                    if (server.Connected)
                    {
                        try
                        {
                            server.Disconnect(true);
                        }
                        catch { }
                    }
                    server.Dispose();
                }
            }
        }
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            string  code      = request.DataStore.GetValue("code");
            string  aadTenant = request.DataStore.GetValue("AADTenant");
            string  oauthType = (request.DataStore.GetValue("oauthType") ?? string.Empty).ToLowerInvariant();
            JObject token     = new JObject();

            token = oauthType == "mscrm" ? AzureTokenUtility.GetTokenForResourceFromCode(Constants.AzureManagementCoreApi, Constants.MsCrmClientId, aadTenant, request.Info.WebsiteRootUrl, code) :
                    AzureTokenUtility.GetTokenForResourceFromCode(oauthType, aadTenant, request.Info.WebsiteRootUrl, code);

            if (token.SelectToken("error") != null)
            {
                return(new ActionResponse(ActionStatus.Failure, token, null, DefaultErrorCodes.DefaultLoginFailed, token.SelectToken("error_description")?.ToString()));
            }

            var emailAddress = AzureUtility.GetEmailFromToken(token);

            if (emailAddress.Contains('#'))
            {
                emailAddress = emailAddress.Split('#')?[1];
            }
            request.DataStore.AddToDataStore("EmailAddress", emailAddress);

            switch (oauthType)
            {
            case "keyvault":
                request.DataStore.AddToDataStore("AzureTokenKV", token);
                break;

            case "as":
                request.DataStore.AddToDataStore("AzureTokenAS", token);
                break;

            case "mscrm":
                JObject crmToken = AzureTokenUtility.GetTokenForResourceFromExistingToken(oauthType, request.Info.WebsiteRootUrl, token, Constants.MsCrmResource);
                request.DataStore.AddToDataStore("MsCrmToken", crmToken);
                request.DataStore.AddToDataStore("AzureToken", token);
                break;

            case "powerbi":
                request.DataStore.AddToDataStore("PBIToken", token);
                request.DataStore.AddToDataStore("DirectoryName", emailAddress.Split('@').Last());
                request.DataStore.AddToDataStore("PowerBITenantId", AzureUtility.GetTenantFromToken(token));
                break;

            default:
                request.DataStore.AddToDataStore("AzureToken", token);
                var tenantId      = AzureUtility.GetTenantFromToken(token);
                var directoryName = emailAddress.Split('@').Last();
                request.DataStore.AddToDataStore("DirectoryName", directoryName);
                request.DataStore.AddToDataStore("PowerBITenantId", tenantId);
                break;
            }

            return(new ActionResponse(ActionStatus.Success, token, true));
        }
Пример #15
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            var    token = request.DataStore.GetJson("AzureToken", "access_token");
            string admin = request.DataStore.GetValue("ASAdmin") ??
                           AzureUtility.GetEmailFromToken(request.DataStore.GetJson("AzureToken"));

            var obj = JsonUtility.GetJObjectFromStringValue(admin);

            return(new ActionResponse(ActionStatus.Success, obj));
        }
Пример #16
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            List <string> axInstances = new List <string>();
            var           tenantId    = AzureUtility.GetTenantFromToken(request.DataStore.GetJson("AzureToken"));
            string        axToken     = request.DataStore.GetJson("AxToken", "access_token");

            var jwtToken     = new JwtSecurityToken(axToken);
            var userObjectId = jwtToken.Claims.First(e => e.Type == "oid")?.Value;

            if (string.IsNullOrEmpty(userObjectId))
            {
                return(new ActionResponse(ActionStatus.Failure, "User Object Id cannot be null. "));
            }

            if (string.IsNullOrEmpty(tenantId))
            {
                return(new ActionResponse(ActionStatus.Failure, "Tenant Id cannot be null. "));
            }

            if (string.IsNullOrEmpty(axToken))
            {
                return(new ActionResponse(ActionStatus.Failure, "No Dynamics 365 token available."));
            }

            var ctx   = new AuthenticationContext(string.Format(Constants.AxLocatorLoginAuthority, tenantId));
            var token = await ctx.AcquireTokenAsync(Constants.AxErpResource, new ClientCredential(Constants.AxLocatorClientId, Constants.AxLocatorSecret));

            HttpResponseMessage response;

            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(Constants.AxLocatorBaseUrl);

                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token.AccessToken);
                client.DefaultRequestHeaders.Add("x-ms-discovery-client-principal-id", userObjectId);
                client.DefaultRequestHeaders.Add("x-ms-discovery-client-tenant-id", tenantId);

                response = client.GetAsync($"/tenantapi/BusinessAppDiscoveryResults(guid'{tenantId}')").Result;
            }

            var content = JsonUtility.GetJsonObjectFromJsonString(await response.Content.ReadAsStringAsync());
            var apps    = content["value"]?[0]?["Apps"];

            if (apps != null)
            {
                foreach (var element in apps)
                {
                    axInstances.Add(element["AppOpenUri"].ToString());
                }

                return(new ActionResponse(ActionStatus.Success, JsonUtility.Serialize(axInstances)));
            }

            return(new ActionResponse(ActionStatus.Success));
        }
Пример #17
0
        public async Task Get_success()
        {
            var ws = await Client.WorkspacesService.ListAsync(SubscriptionId);

            var resourceGroupName = AzureUtility.ParseResourceGroupName(ws.Value.First().Id);
            var found             = await Client.WorkspacesService.GetAsync(SubscriptionId,
                                                                            resourceGroupName, ws.Value.First().Name);

            found.ShouldNotBeNull();
            _testOutputHelper.WriteLine(JsonConvert.SerializeObject(found));
        }
Пример #18
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            string azureToken    = request.DataStore.GetJson("AzureToken", "access_token");
            string subscription  = request.DataStore.GetJson("SelectedSubscription", "SubscriptionId");
            string resourceGroup = request.DataStore.GetValue("SelectedResourceGroup");

            string serverName = request.DataStore.GetValue("ASServerName") ?? "analysisserver-" + RandomGenerator.GetRandomLowerCaseCharacters(5);
            string location   = request.DataStore.GetValue("ASLocation") ?? "westus";
            string sku        = request.DataStore.GetValue("ASSku") ?? "D1";
            string admin      = AzureUtility.GetEmailFromToken(request.DataStore.GetJson("AzureToken"));

            SubscriptionCloudCredentials creds = new TokenCloudCredentials(subscription, azureToken);
            AzureArmParameterGenerator   param = new AzureArmParameterGenerator();

            param.AddStringParam("name", serverName);
            param.AddStringParam("location", location);
            param.AddStringParam("sku", sku);
            param.AddStringParam("admin", admin);

            string armTemplatefilePath = File.ReadAllText(request.ControllerModel.SiteCommonFilePath + "/service/arm/NewAzureAS.json");
            string template            = AzureUtility.GetAzureArmParameters(armTemplatefilePath, param);
            await AzureUtility.ValidateAndDeployArm(creds, resourceGroup, "ASDeployment", template);

            await AzureUtility.WaitForArmDeployment(creds, resourceGroup, "ASDeployment");

            AzureHttpClient client   = new AzureHttpClient(azureToken, subscription, resourceGroup);
            var             response = await client.ExecuteWithSubscriptionAndResourceGroupAsync(HttpMethod.Get
                                                                                                 , $"/providers/Microsoft.AnalysisServices/servers/{serverName}/"
                                                                                                 , "2016-05-16"
                                                                                                 , string.Empty
                                                                                                 , new Dictionary <string, string>());

            string responseBody = await response.Content.ReadAsStringAsync();

            if (!response.IsSuccessStatusCode)
            {
                return(new ActionResponse(ActionStatus.Failure));
            }

            JObject responseObj = JsonUtility.GetJObjectFromJsonString(responseBody);

            request.DataStore.AddToDataStore("ASServerUrl", responseObj["properties"]["serverFullName"], DataStoreType.Public);

            request.Logger.LogResource(request.DataStore, responseObj["properties"]["serverFullName"].ToString(),
                                       DeployedResourceType.AzureAnalysisServices, CreatedBy.BPST, DateTime.UtcNow.ToString("o"), string.Empty, sku);

            return(new ActionResponse(ActionStatus.Success, responseObj));
        }
Пример #19
0
        private static async Task CreateTestInventoryItemsAsync(IConfiguration configuration, IQuiltContextFactory quiltContextFactory, DateTime utcNow)
        {
            var unitOfMeasureCodes = new List <string>
            {
                UnitOfMeasureCodes.FatQuarter,
                UnitOfMeasureCodes.HalfYardage,
                UnitOfMeasureCodes.Yardage,
                UnitOfMeasureCodes.TwoYards,
                UnitOfMeasureCodes.ThreeYards
            };

            var text  = AzureUtility.LoadAzureStringBlob(configuration, "fabrics-default", "index.csv");
            var lines = text.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var line in lines)
            {
                var fields = line.Split(new char[] { ',' });

                var sku  = fields[0];
                var name = fields[1];

                //var entry = library.GetEntry(sku);
                //if (entry == null)
                //{
                var image = (Bitmap)AzureUtility.LoadAzureImageBlob(configuration, "fabrics-default", sku + ".jpg");

                var averageColor = GetAverageColor(image);

                _ = await CreateEntryAsync(
                    quiltContextFactory,
                    sku,
                    InventoryItemTypeCodes.Fabric,
                    name,
                    "Kona Cotton Solid", // Collection
                    "Robert Kaufman",    // Manufacturer
                    (int)averageColor.GetHue(),
                    (int)(averageColor.GetSaturation() * 100),
                    (int)(averageColor.GetBrightness() * 100),
                    unitOfMeasureCodes,
                    TestPricingScheduleName,
                    utcNow).ConfigureAwait(false);

                //}
            }
        }
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            string connectionString = request.DataStore.GetValue("ASConnectionString");

            AzureUtility.GetEmailFromToken(request.DataStore.GetJson("AzureToken"));
            string asDatabase = request.DataStore.GetValue("ASDatabase");

            Server server = new Server();

            server.Connect(connectionString);

            // Process
            var db = server.Databases.Find(asDatabase);

            db.Model.RequestRefresh(RefreshType.Full);
            db.Model.SaveChanges();
            return(new ActionResponse(ActionStatus.Success));
        }
Пример #21
0
        public static void DoDeployment(TestContext context)
        {
            AssertListener.Initialize();
            PythonTestData.Deploy();

            // The tests currently only support Azure Tools v2.2.
            // Support for other versions will be added later.
            var azureToolsVersion = AzureUtility.ToolsVersion.V22;

            if (!AzureUtility.AzureToolsInstalled(azureToolsVersion))
            {
                Assert.Inconclusive(string.Format("Azure Tools v{0} required", azureToolsVersion));
            }

            publishSettingsFilePath = Environment.GetEnvironmentVariable("TEST_AZURE_SUBSCRIPTION_FILE");
            Assert.IsFalse(string.IsNullOrEmpty(publishSettingsFilePath), "TEST_AZURE_SUBSCRIPTION_FILE environment variable must be set to the path of a .publishSettings file for the Azure subscription.");
            Assert.IsTrue(File.Exists(publishSettingsFilePath), "Azure subscription settings file does not exist '{0}'.", publishSettingsFilePath);
        }
Пример #22
0
        public ActionResult Delete(Guid id, string confirmButton)
        {
            Video vid = _videoRepository.GetById(id);

            if (null == vid)
            {
                return(View("NotFound"));
            }

            //Should also delete the physical file on the server
            BlobContainer container = AzureUtility.GetAzureContainer(vid.Path);

            container.DeleteBlob(vid.Path.Substring(vid.Path.IndexOf("/") + 1));

            _videoRepository.Delete(vid);
            _videoRepository.Save();

            return(View("Deleted"));
        }
Пример #23
0
        public static JObject GetTokenForResource(ActionRequest request, JToken azureToken, string resource)
        {
            JObject tokenObj;

            using (HttpClient httpClient = new HttpClient())
            {
                string tenantId     = AzureUtility.GetTenantFromToken(azureToken);
                string refreshToken = AzureUtility.GetRefreshToken(azureToken);
                string tokenUrl     = string.Format(Constants.AzureTokenUri, tenantId);

                string        clientId = GetAzureToken.GetClientIdFromRequest(request);
                string        token    = GetAzureToken.GetTokenUri2(refreshToken, resource, request.Info.WebsiteRootUrl, clientId);
                StringContent content  = new StringContent(token);
                content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
                string response2 = httpClient.PostAsync(new Uri(tokenUrl), content).Result.Content.AsString();
                tokenObj = JsonUtility.GetJsonObjectFromJsonString(response2);
            }

            return(tokenObj);
        }
Пример #24
0
        public static JObject GetTokenForResourceFromExistingToken(string oauthType, string redirect, JToken tokenWithRefresh, string resource)
        {
            JObject tokenObj;

            using (HttpClient httpClient = new HttpClient())
            {
                string tenantId     = AzureUtility.GetTenantFromToken(tokenWithRefresh);
                string refreshToken = AzureUtility.GetRefreshToken(tokenWithRefresh);
                string tokenUrl     = string.Format(Constants.AzureTokenUri, tenantId);

                var           tokenMeta = GetMetaFromOAuthType(oauthType);
                string        token     = AzureTokenUtility.GetTokenBodyFromRefreshToken(refreshToken, resource, redirect, tokenMeta.ClientId);
                StringContent content   = new StringContent(token);
                content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
                string response2 = httpClient.PostAsync(new Uri(tokenUrl), content).Result.Content.AsString();
                tokenObj = JsonUtility.GetJsonObjectFromJsonString(response2);
            }

            return(tokenObj);
        }
Пример #25
0
        private string PostProcessVideo(UploadStatus status, decimal totalCount)
        {
            string resultMessage = String.Empty;
            string localFilePath = String.Empty;
            int    counter       = 0;

            //Setup the Azure Storage Container
            BlobContainer container = AzureUtility.GetAzureContainer(null);

            foreach (UploadedFile file in status.GetUploadedFiles())
            {
                try
                {
                    localFilePath = file.LocationInfo["fileName"].ToString();

                    //Save a skeleton record of the upload in the database
                    Video vid = _videoRepository.New();
                    _videoRepository.AddVideoToUser(vid, User.Identity.Name);
                    vid.Description         = file.FormValues["fileDescription"];
                    vid.StartProcessingDate = DateTime.Now;
                    vid.OriginalFileFormat  = file.ContentType;
                    vid.UploadSize          = file.ContentLength;
                    _videoRepository.Save();
                    Guid videoId = vid.Id;

                    string encodedFilePath = FFmpegEncoder.EncodeToWmv(localFilePath, file.ContentType);

                    if (!String.IsNullOrEmpty(encodedFilePath))
                    {
                        string fileNameOnly = encodedFilePath.Substring(encodedFilePath.LastIndexOf("Upload") + 7);

                        //TODO: Take a screenshot/Thumbnail of the video & upload it

                        BlobProperties properties = AzureUtility.CollectBlobMetadata(file, fileNameOnly, User.Identity.Name);

                        // Create the blob & Upload
                        long finalSize = 0;
                        using (FileStream uploadedFile = new FileStream(encodedFilePath, FileMode.Open, FileAccess.Read))
                        {
                            BlobContents fileBlob = new BlobContents(uploadedFile);
                            finalSize = fileBlob.AsStream.Length;
                            container.CreateBlob(properties, fileBlob, true);
                        }

                        //Create the database record for this video
                        vid = _videoRepository.GetById(videoId);
                        vid.CreationDate = DateTime.Now;
                        vid.FinalSize    = finalSize;
                        vid.Path         = String.Format("{0}/{1}", container.ContainerName, fileNameOnly);
                        _videoRepository.Save();

                        resultMessage = string.Format("Your video ({0}) is ready for you to use.", file.FormValues["fileDescription"]);

                        //Delete the local copy of the encoded file
                        System.IO.File.Delete(encodedFilePath);
                    }
                    else
                    {
                        resultMessage = "ERROR: video (" + file.FormValues["fileDescription"] + ") could not be converted to a recognizable video format.";
                    }

                    //Create a notification record so the user knows that processing is done for this video
                    Notification note = _notificationRepository.New();
                    _notificationRepository.AddNotificationToUser(note, User.Identity.Name);
                    note.UserNotified = false;
                    note.Message      = resultMessage;
                    note.CreationDate = DateTime.Now;
                    _notificationRepository.Save();
                }
                catch (Exception ex)
                {
                    resultMessage = string.Format("ERROR: we tried to process the video, {0}, but it did not finish.  You might want to try again.", file.FormValues["fileDescription"]);

                    //Create a notification record so the user knows that there was an error
                    Notification note = _notificationRepository.New();
                    _notificationRepository.AddNotificationToUser(note, User.Identity.Name);
                    note.UserNotified = false;
                    note.Message      = resultMessage;
                    note.CreationDate = DateTime.Now;
                    _notificationRepository.Save();

                    throw new Exception(resultMessage, ex);
                }
                finally
                {
                    //Delete the local copy of the original file
                    System.IO.File.Delete(localFilePath);

                    counter++;
                }
            }

            return(resultMessage);
        }
Пример #26
0
        public static void AzureSave(IConfiguration configuration, JToken json)
        {
            var name = json.Value <string>(Json_Name);

            AzureUtility.SaveAzureStringBlob(configuration, "libraries", string.Format("{0}.json", name), json.ToString());
        }
        private static void CreateEtlInMemoryProducerWithAzureBlobConsumer(
            string logDirectory,
            ITraceFileEventReaderFactory traceFileEventReaderFactory,
            out EtlInMemoryProducer etlInMemoryProducer,
            out string containerName,
            out StorageAccountFactory storageAccountFactory,
            AccessCondition uploadStreamAccessCondition = null,
            AccessCondition uploadFileAccessCondition   = null)
        {
            var mockDiskSpaceManager        = TestUtility.MockRepository.Create <DiskSpaceManager>();
            var etlInMemoryProducerSettings = new EtlInMemoryProducerSettings(
                true,
                TimeSpan.FromSeconds(1),
                TimeSpan.FromDays(3000),
                WinFabricEtlType.DefaultEtl,
                logDirectory,
                TestEtlFilePatterns,
                true);

            var configReader = TestUtility.MockRepository.Create <IEtlInMemoryProducerConfigReader>();

            configReader.Setup(c => c.GetSettings()).Returns(etlInMemoryProducerSettings);

            var mockTraceEventSourceFactory = TestUtility.MockRepository.Create <ITraceEventSourceFactory>();

            mockTraceEventSourceFactory
            .Setup(tsf => tsf.CreateTraceEventSource(It.IsAny <EventTask>()))
            .Returns(new ErrorAndWarningFreeTraceEventSource());

            var configStore = TestUtility.MockRepository.Create <IConfigStore>();

            configStore
            .Setup(cs => cs.ReadUnencryptedString("Diagnostics", "MaxDiskQuotaInMB"))
            .Returns("100");
            configStore
            .Setup(cs => cs.ReadUnencryptedString("Diagnostics", "DiskFullSafetySpaceInMB"))
            .Returns("0");
            configStore
            .Setup(cs => cs.ReadUnencryptedString(TestConfigSectionName, AzureConstants.EnabledParamName))
            .Returns("true");

            containerName = string.Format("{0}-{1}", "fabriclogs", Guid.NewGuid());
            configStore
            .Setup(cs => cs.ReadUnencryptedString(TestConfigSectionName, AzureConstants.ContainerParamName))
            .Returns(containerName);

            configStore
            .Setup(cs => cs.ReadUnencryptedString(TestConfigSectionName, AzureConstants.FileSyncIntervalParamName))
            .Returns("0.25");
            configStore
            .Setup(cs => cs.ReadUnencryptedString(TestConfigSectionName, AzureConstants.DataDeletionAgeParamName))
            .Returns("1");
            configStore
            .Setup(cs => cs.ReadUnencryptedString(TestConfigSectionName, AzureConstants.TestDataDeletionAgeParamName))
            .Returns("0");
            configStore
            .Setup(cs => cs.ReadUnencryptedString(TestConfigSectionName, AzureConstants.LogFilterParamName))
            .Returns("*.*:4");
            configStore
            .Setup(cs => cs.ReadUnencryptedString(TestConfigSectionName, AzureConstants.DeploymentId))
            .Returns(AzureConstants.DefaultDeploymentId);

            var accountKey              = GetTestStorageAccountKey();
            var isEncrypted             = false;
            var storageConnectionString = string.Format(@"xstore:DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", AccountName, accountKey);

            configStore
            .Setup(cs => cs.ReadString(TestConfigSectionName, AzureConstants.ConnectionStringParamName, out isEncrypted))
            .Returns(storageConnectionString);

            Utility.InitializeConfigStore(configStore.Object);

            var mockConfigReaderFactory = TestUtility.MockRepository.Create <IEtlInMemoryProducerConfigReaderFactory>();

            mockConfigReaderFactory.Setup(f => f.CreateEtlInMemoryProducerConfigReader(It.IsAny <FabricEvents.ExtensionsEvents>(), It.IsAny <string>()))
            .Returns(configReader.Object);

            // Create the Azure Blob Uploader consumer
            ConfigReader.AddAppConfig(Utility.WindowsFabricApplicationInstanceId, null);
            var consumerInitParam = new ConsumerInitializationParameters(
                Utility.WindowsFabricApplicationInstanceId,
                TestConfigSectionName,
                TestFabricNodeId,
                TestFabricNodeName,
                Utility.LogDirectory,
                Utility.DcaWorkFolder,
                new DiskSpaceManager());

            // Save storage connection for clean up
            var azureUtility = new AzureUtility(new FabricEvents.ExtensionsEvents(FabricEvents.Tasks.FabricDCA), TraceType);

            storageAccountFactory = azureUtility.GetStorageAccountFactory(
                new ConfigReader(consumerInitParam.ApplicationInstanceId),
                TestConfigSectionName,
                AzureConstants.ConnectionStringParamName);

            var azureBlobConsumer         = new AzureBlobEtwUploader(consumerInitParam, uploadStreamAccessCondition, uploadFileAccessCondition);
            var etlToInMemoryBufferWriter = azureBlobConsumer.GetDataSink();

            var producerInitParam = new ProducerInitializationParameters
            {
                ConsumerSinks = new[] { etlToInMemoryBufferWriter }
            };

            // Create the in-memory producer
            etlInMemoryProducer = new EtlInMemoryProducer(
                mockDiskSpaceManager.Object,
                mockConfigReaderFactory.Object,
                traceFileEventReaderFactory,
                mockTraceEventSourceFactory.Object,
                producerInitParam);
        }
Пример #28
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            string code      = request.DataStore.GetValue("code");
            string aadTenant = request.DataStore.GetValue("AADTenant");
            string oauthType = (request.DataStore.GetLastValue("oauthType") ?? string.Empty).ToLowerInvariant();
            string api;
            string clientId;
            string tokenUrl;

            switch (oauthType)
            {
            case "powerbi":
                tokenUrl = string.Format(Constants.AzureTokenUri, aadTenant);
                clientId = Constants.MicrosoftClientIdPowerBI;
                api      = Constants.PowerBIService;
                break;

            case "mscrm":
                api      = Constants.AzureManagementCoreApi;
                clientId = Constants.MsCrmClientId;
                tokenUrl = string.Format(Constants.AzureTokenUri, aadTenant);
                break;

            case "keyvault":
                api      = Constants.AzureManagementCoreApi;
                clientId = Constants.MicrosoftClientIdCrm;
                tokenUrl = string.Format(Constants.AzureTokenUri, aadTenant);
                break;

            default:
                api      = Constants.AzureManagementCoreApi;
                clientId = Constants.MicrosoftClientId;
                tokenUrl = string.Format(Constants.AzureTokenUri, aadTenant);
                break;
            }

            JObject primaryResponse = null;
            JObject obj             = null;

            using (HttpClient client = new HttpClient())
            {
                var builder = GetTokenUri(code, api, request.Info.WebsiteRootUrl, clientId);
                var content = new StringContent(builder.ToString());
                content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
                var response = await client.PostAsync(new Uri(tokenUrl), content).Result.Content.ReadAsStringAsync();

                primaryResponse = JsonUtility.GetJsonObjectFromJsonString(response);
                obj             = new JObject(new JProperty("AzureToken", primaryResponse));

                if (primaryResponse.SelectToken("error") != null)
                {
                    return(new ActionResponse(ActionStatus.Failure, obj, null,
                                              DefaultErrorCodes.DefaultLoginFailed,
                                              primaryResponse.SelectToken("error_description")?.ToString()));
                }
            }

            var emailAddress = AzureUtility.GetEmailFromToken(primaryResponse);

            if (emailAddress.Contains('#'))
            {
                emailAddress = emailAddress.Split('#')?[1];
            }
            request.DataStore.AddToDataStore("EmailAddress", emailAddress);

            switch (oauthType)
            {
            case "keyvault":
                request.DataStore.AddToDataStore("AzureTokenKV", primaryResponse);
                break;

            case "mscrm":
                JObject crmToken = RetrieveCrmToken(primaryResponse["refresh_token"].ToString(), request.Info.WebsiteRootUrl, request.DataStore);
                request.DataStore.AddToDataStore("MsCrmToken", crmToken);
                request.DataStore.AddToDataStore("AzureToken", primaryResponse);
                break;

            default:
                request.DataStore.AddToDataStore("AzureToken", primaryResponse);

                var tenantId = new JwtSecurityToken(primaryResponse["id_token"].ToString())
                               .Claims.First(e => e.Type.ToLowerInvariant() == "tid")
                               .Value;

                var directoryName = emailAddress.Split('@').Last();

                request.DataStore.AddToDataStore("DirectoryName", directoryName);
                request.DataStore.AddToDataStore("PowerBITenantId", tenantId);
                break;
            }
            return(new ActionResponse(ActionStatus.Success, obj, true));
        }