Beispiel #1
0
        public void EncryptManifestFilesAndVerifyThemAfterDecryption()
        {
            List <IIngestManifestFile> files;
            IIngestManifest            ingestManifestCreated;
            string path = null;

            try
            {
                path = CreateManifestEncryptFiles(out files, out ingestManifestCreated);
                IIngestManifestAsset ingestManifestAsset = ingestManifestCreated.IngestManifestAssets.ToList().Where(c => c.Asset.Options == AssetCreationOptions.StorageEncrypted).FirstOrDefault();
                IIngestManifestFile  mFile = ingestManifestAsset.IngestManifestFiles.Where(c => c.Name == "File0.txt").FirstOrDefault();

                Dictionary <string, string> filePaths = new Dictionary <string, string>();
                foreach (var filePath in new[] { TestFile1, TestFile2 })
                {
                    FileInfo fileInfo = new FileInfo(filePath);
                    filePaths.Add(fileInfo.Name, filePath);
                }

                var encryptedPath = Path.Combine(path, mFile.Name);
                Assert.IsTrue(File.Exists(encryptedPath));
                var decryptedPath = DecryptedFile(mFile, encryptedPath, _mediaContext);
                Assert.IsTrue(AssetTests.CompareFiles(decryptedPath, filePaths[mFile.Name]), "Original file and Decrypted are not same");
            }
            finally
            {
                AssetFilesTests.CleanDirectory(path);
            }
        }
        public void ShouldThrowKeyNotFoundExceptionDuringEncryptIfKeyIsMissing()
        {
            var sourcePath = DeploymentFolder1;

            Assert.IsTrue(Directory.Exists(sourcePath));
            List <string> files = Directory.EnumerateFiles(sourcePath, "*.txt").ToList();

            //Creating empty manifest
            const string    manifestName          = "Manifest 1";
            IIngestManifest ingestManifestCreated = _context.IngestManifests.Create(manifestName);

            //Adding manifest asset info with multiple file
            IAsset emptyAsset = _context.Assets.Create(Guid.NewGuid().ToString(), AssetCreationOptions.StorageEncrypted);

            IIngestManifestAsset ingestManifestAsset = ingestManifestCreated.IngestManifestAssets.CreateAsync(emptyAsset, files.ToArray(), CancellationToken.None).Result;

            Assert.IsNotNull(ingestManifestAsset);

            //According to last REST implementation breaking a link
            //also deleting a key on server side if no other links are found
            emptyAsset.ContentKeys.RemoveAt(0);

            var path = @".\Resources\TestFiles\" + Guid.NewGuid();

            Directory.CreateDirectory(path);
            try
            {
                ingestManifestCreated.EncryptFiles(path);
            }
            catch (AggregateException ex)
            {
                Assert.AreEqual(1, ex.InnerExceptions.Count);
                throw ex.InnerExceptions[0];
            }
        }
Beispiel #3
0
        public void ListAssetsAndFilesForNewlyCreatedManifests()
        {
            IIngestManifest ingestManifest = CreateEmptyManifestAndVerifyIt();

            IAsset asset = _mediaContext.Assets.Create("name", AssetCreationOptions.None);

            Assert.IsNotNull(asset);
            IIngestManifestAsset ingestManifestAsset = ingestManifest.IngestManifestAssets.Create(asset, new[] { TestFile1 });

            VerifyManifestAsset(ingestManifestAsset);
            IIngestManifestAsset firstAsset = ingestManifest.IngestManifestAssets.FirstOrDefault();

            VerifyManifestAsset(firstAsset);
            Assert.AreEqual(ingestManifestAsset.Id, firstAsset.Id);

            _mediaContext = WindowsAzureMediaServicesTestConfiguration.CreateCloudMediaContext();
            IIngestManifest sameIngestManifest = _mediaContext.IngestManifests.Where(c => c.Id == ingestManifest.Id).FirstOrDefault();

            Assert.IsNotNull(sameIngestManifest);
            Assert.AreEqual(1, sameIngestManifest.IngestManifestAssets.Count(), "Manifest asset count is not matching expecting value 1");
            firstAsset = sameIngestManifest.IngestManifestAssets.FirstOrDefault();
            VerifyManifestAsset(firstAsset);
            Assert.AreEqual(1, firstAsset.IngestManifestFiles.Count(), "Manifest file count is not matching expecting value 1");
            IIngestManifestFile firstFile = firstAsset.IngestManifestFiles.FirstOrDefault();

            Assert.AreEqual("text/plain", firstFile.MimeType, "IngestManifestFile's MimeType is wrong");
            VerifyManifestFile(firstFile);
        }
Beispiel #4
0
        private static string DecryptedFile(IIngestManifestFile ingestManifestFile, string encryptedPath, CloudMediaContext context)
        {
            IIngestManifestAsset ingestManifestAsset = context.IngestManifestAssets.Where(a => a.Id == ingestManifestFile.ParentIngestManifestAssetId).FirstOrDefault();

            Assert.IsNotNull(ingestManifestAsset);

            IList <IContentKey> keys = ingestManifestAsset.Asset.ContentKeys.Where(c => c.ContentKeyType == ContentKeyType.StorageEncryption).ToList();

            Assert.AreEqual(1, keys.Count, "Expecting only one storage key per asset");
            IContentKey key = keys.FirstOrDefault();

            Assert.IsNotNull(ingestManifestAsset);


            Guid           keyId          = EncryptionUtils.GetKeyIdAsGuid(key.Id);
            FileEncryption fileEncryption = new FileEncryption(key.GetClearKeyValue(), keyId);

            ulong iv            = Convert.ToUInt64(ingestManifestFile.InitializationVector, CultureInfo.InvariantCulture);
            var   decryptedPath = @".\Resources\TestFiles\Decrypted" + Guid.NewGuid();

            if (!Directory.Exists(decryptedPath))
            {
                Directory.CreateDirectory(decryptedPath);
            }

            decryptedPath = Path.Combine(decryptedPath, ingestManifestFile.Name);
            FileInfo      fileInfo        = new FileInfo(encryptedPath);
            var           maxblocksize    = GetBlockSize(fileInfo.Length);
            List <string> blockList       = new List <string>();
            int           numberOfthreads = 1;
            var           queue           = PreapreDownloadQueue(maxblocksize, fileInfo.Length, ref numberOfthreads, out blockList);

            using (var fs = new FileStream(decryptedPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
            {
                KeyValuePair <int, int> block;
                while (queue.TryDequeue(out block))
                {
                    fs.Seek(block.Key * maxblocksize, SeekOrigin.Begin);
                    using (FileStream stream = File.OpenRead(encryptedPath))
                    {
                        byte[] buffer = new byte[block.Value];
                        stream.Seek(block.Key * maxblocksize, SeekOrigin.Begin);
                        int read = stream.Read(buffer, 0, (int)block.Value);
                        if (fileEncryption != null)
                        {
                            lock (fileEncryption)
                            {
                                using (FileEncryptionTransform encryptor = fileEncryption.GetTransform(iv, block.Key * maxblocksize))
                                {
                                    encryptor.TransformBlock(inputBuffer: buffer, inputOffset: 0, inputCount: buffer.Length, outputBuffer: buffer, outputOffset: 0);
                                }
                            }
                        }

                        fs.Write(buffer, 0, buffer.Length);
                    }
                }
            }
            return(decryptedPath);
        }
Beispiel #5
0
        public void EncryptManifestTestDisableOverwriteExistingFile()
        {
            CloudMediaContext context = WindowsAzureMediaServicesTestConfiguration.CreateCloudMediaContext();
            var sourcePath            = DeploymentFolder1;

            Assert.IsTrue(Directory.Exists(sourcePath));
            List <string>   files                 = Directory.EnumerateFiles(sourcePath, "*.txt").ToList();
            const string    manifestName          = "Manifest 1";
            IIngestManifest ingestManifestCreated = context.IngestManifests.Create(manifestName);

            //Adding manifest asset info with multiple file
            IAsset emptyAsset = context.Assets.Create(Guid.NewGuid().ToString(), AssetCreationOptions.StorageEncrypted);

            IIngestManifestAsset ingestManifestAsset = ingestManifestCreated.IngestManifestAssets.Create(emptyAsset, files.ToArray());

            var path = @".\Resources\TestFiles\" + Guid.NewGuid();

            try
            {
                Directory.CreateDirectory(path);
                string dupFileName = Path.Combine(path, Path.GetFileName(files[0]));
                File.WriteAllText(dupFileName, "");
                ingestManifestCreated.EncryptFiles(path, false);
            }
            catch (AggregateException ax)
            {
                var expectedExcpetion = ax.GetBaseException() as IOException;
                throw expectedExcpetion;
            }
            finally
            {
                AssetFilesTests.CleanDirectory(path);
            }
        }
        private static void EncryptFilesDecryptAndCompare(List <string> files)
        {
            CloudMediaContext context = WindowsAzureMediaServicesTestConfiguration.CreateCloudMediaContext();
            //Creating empty manifest
            const string    manifestName          = "Manifest 1";
            IIngestManifest ingestManifestCreated = context.IngestManifests.Create(manifestName);

            //Adding manifest asset info with multiple file
            IAsset emptyAsset = context.Assets.Create(Guid.NewGuid().ToString(), AssetCreationOptions.StorageEncrypted);

            IIngestManifestAsset ingestManifestAsset = ingestManifestCreated.IngestManifestAssets.CreateAsync(emptyAsset, files.ToArray(), CancellationToken.None).Result;

            var path = @".\Resources\TestFiles\" + Guid.NewGuid();

            Directory.CreateDirectory(path);
            ingestManifestCreated.EncryptFiles(path);

            Dictionary <string, string> filePaths = new Dictionary <string, string>();

            foreach (var filePath in files)
            {
                FileInfo fileInfo = new FileInfo(filePath);
                filePaths.Add(fileInfo.Name, filePath);
            }


            foreach (var assetFile in ingestManifestAsset.IngestManifestFiles)
            {
                var encryptedPath = Path.Combine(path, assetFile.Name);
                Assert.IsTrue(File.Exists(encryptedPath));
                var decryptedPath = DecryptedFile(assetFile, encryptedPath, context);
                Assert.IsTrue(AssetTests.CompareFiles(decryptedPath, filePaths[assetFile.Name]), "Original file and Decrypted are not same");
            }
        }
        /// <summary>
        /// Creates the manifest asset file asynchronously.
        /// </summary>
        /// <param name="ingestManifestAsset">The parent manifest asset.</param>
        /// <param name="filePath">The file path.</param>
        /// <param name="token"><see cref="CancellationToken"/></param>
        /// <returns><see cref="Task"/>of type <see cref="IIngestManifestFile"/></returns>
        public Task <IIngestManifestFile> CreateAsync(IIngestManifestAsset ingestManifestAsset, string filePath, CancellationToken token)
        {
            if (ingestManifestAsset == null)
            {
                throw new ArgumentNullException("ingestManifestAsset");
            }

            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, StringTable.ErrorCreatingIngestManifestFileEmptyFilePath));
            }

            AssetCreationOptions options = ingestManifestAsset.Asset.Options;

            Task <IIngestManifestFile> rootTask = new Task <IIngestManifestFile>(() =>
            {
                token.ThrowIfCancellationRequested();

                IngestManifestAssetCollection.VerifyManifestAsset(ingestManifestAsset);
                if (!File.Exists(filePath))
                {
                    throw new FileNotFoundException(String.Format(CultureInfo.InvariantCulture, StringTable.BulkIngestProvidedFileDoesNotExist, filePath));
                }
                FileInfo info = new FileInfo(filePath);

                DataServiceContext dataContext = this._cloudMediaContext.DataContextFactory.CreateDataServiceContext();

                // Set a MIME type based on the extension of the file name
                string mimeType = AssetFileData.GetMimeType(filePath);

                IngestManifestFileData data = new IngestManifestFileData
                {
                    Name     = info.Name,
                    MimeType = mimeType,
                    ParentIngestManifestId      = ingestManifestAsset.ParentIngestManifestId,
                    ParentIngestManifestAssetId = ingestManifestAsset.Id,
                    Path = filePath,
                };

                SetEncryptionSettings(ingestManifestAsset, info, options, data);

                dataContext.AddObject(EntitySet, data);

                Task <IIngestManifestFile> task = dataContext.SaveChangesAsync(data).ContinueWith <IIngestManifestFile>(t =>
                {
                    t.ThrowIfFaulted();
                    token.ThrowIfCancellationRequested();
                    IngestManifestFileData ingestManifestFile = (IngestManifestFileData)t.AsyncState;
                    return(ingestManifestFile);
                });

                return(task.Result);
            });

            rootTask.Start();
            return(rootTask);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="IngestManifestFileCollection"/> class.
 /// </summary>
 /// <param name="cloudMediaContext">The <seealso cref="CloudMediaContext"/> instance.</param>
 /// <param name="parentIngestManifestAsset">The parent manifest asset.</param>
 internal IngestManifestFileCollection(CloudMediaContext cloudMediaContext, IIngestManifestAsset parentIngestManifestAsset)
 {
     this._cloudMediaContext = cloudMediaContext;
     this._dataContext       = this._cloudMediaContext.DataContextFactory.CreateDataServiceContext();
     this._query             = new Lazy <IQueryable <IIngestManifestFile> >(() => this._dataContext.CreateQuery <IngestManifestFileData>(EntitySet));
     if (parentIngestManifestAsset != null)
     {
         this._parentIngestManifestAsset = (IngestManifestAssetData)parentIngestManifestAsset;
     }
 }
        /// <summary>
        /// Creates the manifest asset file asynchronously.
        /// </summary>
        /// <param name="ingestManifestAsset">The parent manifest asset.</param>
        /// <param name="filePath">The file path.</param>
        /// <param name="token"><see cref="CancellationToken"/></param>
        /// <returns><see cref="Task"/>of type <see cref="IIngestManifestFile"/></returns>
        public Task <IIngestManifestFile> CreateAsync(IIngestManifestAsset ingestManifestAsset, string filePath, CancellationToken token)
        {
            if (ingestManifestAsset == null)
            {
                throw new ArgumentNullException("ingestManifestAsset");
            }

            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, StringTable.ErrorCreatingIngestManifestFileEmptyFilePath));
            }

            AssetCreationOptions options = ingestManifestAsset.Asset.Options;

            Task <IIngestManifestFile> rootTask = new Task <IIngestManifestFile>(() =>
            {
                token.ThrowIfCancellationRequested();

                IngestManifestAssetCollection.VerifyManifestAsset(ingestManifestAsset);

                IMediaDataServiceContext dataContext = this.MediaContext.MediaServicesClassFactory.CreateDataServiceContext();

                // Set a MIME type based on the extension of the file name
                string mimeType = AssetFileData.GetMimeType(filePath);

                IngestManifestFileData data = new IngestManifestFileData
                {
                    Name     = Path.GetFileName(filePath),
                    MimeType = mimeType,
                    ParentIngestManifestId      = ingestManifestAsset.ParentIngestManifestId,
                    ParentIngestManifestAssetId = ingestManifestAsset.Id,
                    Path = filePath,
                };

                SetEncryptionSettings(ingestManifestAsset, options, data);

                dataContext.AddObject(EntitySet, data);

                MediaRetryPolicy retryPolicy = this.MediaContext.MediaServicesClassFactory.GetSaveChangesRetryPolicy(dataContext as IRetryPolicyAdapter);

                Task <IIngestManifestFile> task = retryPolicy.ExecuteAsync <IMediaDataServiceResponse>(() => dataContext.SaveChangesAsync(data))
                                                  .ContinueWith <IIngestManifestFile>(t =>
                {
                    t.ThrowIfFaulted();
                    token.ThrowIfCancellationRequested();
                    IngestManifestFileData ingestManifestFile = (IngestManifestFileData)t.Result.AsyncState;
                    return(ingestManifestFile);
                });

                return(task.Result);
            });

            rootTask.Start();
            return(rootTask);
        }
        private static IContentKey VerifyContentKey(IIngestManifestAsset asset1)
        {
            IAsset      asset = asset1.Asset;
            IContentKey key   = asset.ContentKeys.Where(c => c.ContentKeyType == ContentKeyType.StorageEncryption).FirstOrDefault();

            if (key == null)
            {
                throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, StringTable.StorageEncryptionContentKeyIsMissing, asset.Id));
            }
            return(key);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="IngestManifestFileCollection"/> class.
        /// </summary>
        /// <param name="mediaContext"></param>
        /// <param name="parentIngestManifestAsset">The parent manifest asset.</param>
        internal IngestManifestFileCollection(MediaContextBase mediaContext, IIngestManifestAsset parentIngestManifestAsset)
            : base(mediaContext)
        {
            MediaServicesClassFactory factory = this.MediaContext.MediaServicesClassFactory;

            this._query = new Lazy <IQueryable <IIngestManifestFile> >(() => factory.CreateDataServiceContext().CreateQuery <IIngestManifestFile, IngestManifestFileData>(EntitySet));

            if (parentIngestManifestAsset != null)
            {
                this._parentIngestManifestAsset = (IngestManifestAssetData)parentIngestManifestAsset;
            }
        }
Beispiel #12
0
        public static void UploadByBlock(string manifestName, AssetCreationOptions options, string[] files)
        {
            CloudMediaContext context  = CloudContextHelper.GetContext();
            IIngestManifest   manifest = context.IngestManifests.Create(manifestName);

            IAsset asset = context.Assets.Create(manifestName + "_Asset", options);

            IIngestManifestAsset bulkAsset = manifest.IngestManifestAssets.Create(asset, files);

            UploadBlobFile(manifest.BlobStorageUriForUpload, files);

            MonitorBulkManifest(manifest.Id);
        }
        /// <summary>
        /// Encrypts manifest files asyncroniously.
        /// </summary>
        /// <param name="outputPath">The output path where all encrypted files will be located.</param>
        /// <param name="overwriteExistingEncryptedFiles">if set to <c>true</c> method will override files in ouput folder.</param>
        /// <param name="cancellationToken"><see cref="CancellationToken"/></param>
        /// <returns><see cref="Task"/></returns>
        public Task EncryptFilesAsync(string outputPath, bool overwriteExistingEncryptedFiles, CancellationToken cancellationToken)
        {
            if (!Directory.Exists(outputPath))
            {
                throw new DirectoryNotFoundException(outputPath);
            }
            var _this = (IIngestManifest)this;

            //we are forming two arrays of tasks: validation of keys and encryption tasks
            var assetEncryptionTasks = new List <Task>();
            var assetCheckKeyTasks   = new List <Task>();

            var keys = new ConcurrentDictionary <string, IContentKey>();


            foreach (IIngestManifestAsset manifestAsset in _this.IngestManifestAssets)
            {
                IIngestManifestAsset asset = manifestAsset;
                Action checkKeyAction      = (() =>
                {
                    var key = VerifyContentKey(asset);
                    //Adding keys which we found for assets.If key has not been found thread throwing exception from VerifyContentKey
                    keys.TryAdd(asset.Id, key);
                });
                //Starting validation tasks as soon as we create it
                assetCheckKeyTasks.Add(Task.Factory.StartNew(checkKeyAction, cancellationToken));
                //Creating encryption tasks without starting it
                assetEncryptionTasks.Add(new Task(() => AssetEncryptAction(outputPath, overwriteExistingEncryptedFiles, cancellationToken, keys, asset)));
            }


            return(Task.Factory.StartNew(() =>
            {
                //Wait until all key verification is done
                Task.WaitAll(assetCheckKeyTasks.ToArray());

                //Starting all encryption tasks
                foreach (var encryptTask in assetEncryptionTasks)
                {
                    encryptTask.Start();
                }
                //Waiting all encryption tasks for completion
                Task.WaitAll(assetEncryptionTasks.ToArray());
            }, cancellationToken));
        }
Beispiel #14
0
        public void AddingAdditionalAssetInfoesToExistingManifest()
        {
            CloudMediaContext context = WindowsAzureMediaServicesTestConfiguration.CreateCloudMediaContext();
            IIngestManifest   ingestManifestCreated = CreateManifestWithAssetsAndVerifyIt(context);

            context = WindowsAzureMediaServicesTestConfiguration.CreateCloudMediaContext();

            IIngestManifest ingestManifest = context.IngestManifests.Where(c => c.Id == ingestManifestCreated.Id).FirstOrDefault();

            Assert.AreNotSame(0, ingestManifest.IngestManifestAssets.Count());
            IAsset asset1 = context.Assets.Create("Asset1", AssetCreationOptions.StorageEncrypted);
            Task <IIngestManifestAsset> task1 = ingestManifest.IngestManifestAssets.CreateAsync(asset1, new string[1] {
                InterviewWmv
            }, CancellationToken.None);

            IIngestManifestAsset assetInfo1 = task1.Result;

            Assert.AreEqual(1, assetInfo1.IngestManifestFiles.Count());
        }
        private static IIngestManifest CreateManifestWithAssetsAndVerifyIt(CloudMediaContext context)
        {
            //Creating empty manifest
            const string    manifestName   = "Manifest 1";
            IIngestManifest ingestManifest = context.IngestManifests.Create(manifestName);

            Assert.AreEqual(IngestManifestState.Inactive, ingestManifest.State, "Expecting empty manifest to be inactive");
            //Adding manifest asset info with multiple file
            IAsset asset2 = context.Assets.Create(Guid.NewGuid().ToString(), AssetCreationOptions.StorageEncrypted);
            var    files2 = new string[2] {
                TestFile1, TestFile2
            };
            IIngestManifestAsset ingestManifestAssetInfo2 = ingestManifest.IngestManifestAssets.Create(asset2, files2);

            Assert.AreEqual(1, asset2.ContentKeys.Count, "No keys associated with asset");
            VerifyManifestAsset(ingestManifestAssetInfo2);

            Assert.AreEqual(2, ingestManifestAssetInfo2.IngestManifestFiles.Count(), "Files collection size is not matching expectations");
            return(ingestManifest);
        }
Beispiel #16
0
        public void DeleteAssetShouldDeleteAllManifestAssetsFiles()
        {
            CloudMediaContext context        = WindowsAzureMediaServicesTestConfiguration.CreateCloudMediaContext();
            IIngestManifest   ingestManifest = CreateManifestWithAssetsAndVerifyIt(context);

            VerifyExistenceofAssetsAndFilesForManifest(ingestManifest, context);

            IIngestManifestAsset asset = ingestManifest.IngestManifestAssets.FirstOrDefault();

            VerifyManifestAsset(asset);
            int filescount = context.IngestManifestFiles.Where(c => c.ParentIngestManifestAssetId == asset.Id).Count();

            Assert.IsTrue(filescount > 0, "Expecting to have files for given asset");
            asset.Delete();

            context = WindowsAzureMediaServicesTestConfiguration.CreateCloudMediaContext();
            VerifyAssetIsNotExist(asset, context);

            filescount = context.IngestManifestFiles.Where(c => c.ParentIngestManifestAssetId == asset.Id).Count();
            Assert.AreEqual(0, filescount, "There are files belonging to manifest assets after asset deletion");
        }
        private static void SetEncryptionSettings(IIngestManifestAsset ingestManifestAsset, AssetCreationOptions options, IngestManifestFileData data)
        {
            if (options.HasFlag(AssetCreationOptions.StorageEncrypted))
            {
                var contentKeyData = ingestManifestAsset.Asset.ContentKeys.Where(c => c.ContentKeyType == ContentKeyType.StorageEncryption).FirstOrDefault();
                if (contentKeyData == null)
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, StringTable.StorageEncryptionContentKeyIsMissing, ingestManifestAsset.Asset.Id));
                }
                using (var fileEncryption = new FileEncryption(contentKeyData.GetClearKeyValue(), EncryptionUtils.GetKeyIdAsGuid(contentKeyData.Id)))
                {
                    if (!fileEncryption.IsInitializationVectorPresent(data.Name))
                    {
                        fileEncryption.CreateInitializationVectorForFile(data.Name);
                    }
                    ulong iv = fileEncryption.GetInitializationVectorForFile(data.Name);

                    data.IsEncrypted          = true;
                    data.EncryptionKeyId      = fileEncryption.GetKeyIdentifierAsString();
                    data.EncryptionScheme     = FileEncryption.SchemeName;
                    data.EncryptionVersion    = FileEncryption.SchemeVersion;
                    data.InitializationVector = iv.ToString(CultureInfo.InvariantCulture);
                }
            }
            else if (options.HasFlag(AssetCreationOptions.CommonEncryptionProtected))
            {
                data.IsEncrypted       = true;
                data.EncryptionScheme  = CommonEncryption.SchemeName;
                data.EncryptionVersion = CommonEncryption.SchemeVersion;
            }
            else if (options.HasFlag(AssetCreationOptions.EnvelopeEncryptionProtected))
            {
                data.IsEncrypted       = true;
                data.EncryptionScheme  = EnvelopeEncryption.SchemeName;
                data.EncryptionVersion = EnvelopeEncryption.SchemeVersion;
            }
        }
 private void VerifyAssetIsNotExist(IIngestManifestAsset asset, CloudMediaContext context)
 {
     Assert.AreEqual(0, context.IngestManifestAssets.Where(c => c.Id == asset.Id).Count(), "Manifest Asset exists.Expected result that asset is not returned by REST API");
 }
 private static void VerifyManifestAsset(IIngestManifestAsset ingestManifestAsset)
 {
     Assert.IsNotNull(ingestManifestAsset);
     Assert.IsFalse(String.IsNullOrEmpty(ingestManifestAsset.Id), "Manifest asset id is is null or empty");
     Assert.IsFalse(String.IsNullOrEmpty(ingestManifestAsset.ParentIngestManifestId), "ParentManifestId is null or empty");
 }
Beispiel #20
0
 private static void VerifyManifestAsset(IIngestManifestAsset ingestManifestAsset)
 {
     Assert.IsNotNull(ingestManifestAsset);
     Assert.IsFalse(String.IsNullOrEmpty(ingestManifestAsset.Id), "Manifest asset id is is null or empty");
     Assert.IsFalse(String.IsNullOrEmpty(ingestManifestAsset.ParentIngestManifestId), "ParentManifestId is null or empty");
 }
        private static IContentKey VerifyContentKey(IIngestManifestAsset asset1)
        {
            IAsset asset = asset1.Asset;
            IContentKey key = asset.ContentKeys.Where(c => c.ContentKeyType == ContentKeyType.StorageEncryption).FirstOrDefault();

            if (key == null)
            {
                throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, StringTable.StorageEncryptionContentKeyIsMissing, asset.Id));
            }
            return key;
        }
Beispiel #22
0
 private void VerifyAssetIsNotExist(IIngestManifestAsset asset, CloudMediaContext context)
 {
     Assert.AreEqual(0, context.IngestManifestAssets.Where(c => c.Id == asset.Id).Count(), "Manifest Asset exists.Expected result that asset is not returned by REST API");
 }
        private void AssetEncryptAction(string outputPath, bool overwriteExistingEncryptedFiles, CancellationToken cancellationToken, ConcurrentDictionary <string, IContentKey> keys, IIngestManifestAsset asset)
        {
            cancellationToken.ThrowIfCancellationRequested();

            List <Task> encryptTasks = new List <Task>();

            AssetCreationOptions assetCreationOptions = asset.Asset.Options;

            if (assetCreationOptions.HasFlag(AssetCreationOptions.StorageEncrypted))
            {
                IContentKey contentKeyData = keys[asset.Id];
                var         fileEncryption = new FileEncryption(contentKeyData.GetClearKeyValue(), EncryptionUtils.GetKeyIdAsGuid(contentKeyData.Id));


                foreach (IngestManifestFileData file in asset.IngestManifestFiles)
                {
                    ulong iv = Convert.ToUInt64(file.InitializationVector, CultureInfo.InvariantCulture);
                    fileEncryption.SetInitializationVectorForFile(file.Name, iv);

                    FileInfo fileInfo = null;
                    fileInfo = TrackedFilesPaths.ContainsKey(file.Id) ? new FileInfo(TrackedFilesPaths[file.Id]) : new FileInfo(file.Name);

                    string destinationPath = Path.Combine(outputPath, fileInfo.Name);
                    if (File.Exists(destinationPath))
                    {
                        if (overwriteExistingEncryptedFiles)
                        {
                            File.Delete(destinationPath);
                        }
                        else
                        {
                            throw new IOException(string.Format(CultureInfo.InvariantCulture, StringTable.BulkIngestFileExists, destinationPath));
                        }
                    }

                    long fileSize     = fileInfo.Length;
                    int  maxBlockSize = GetBlockSize(fileSize);

                    int numThreads = MaxNumberOfEncryptionThreadsForFilePerCore * Environment.ProcessorCount;
                    ConcurrentQueue <Tuple <int, int> > queue = PrepareUploadQueue(maxBlockSize, fileSize);
                    if (queue.Count < numThreads)
                    {
                        numThreads = queue.Count;
                    }


                    File.Create(destinationPath).Dispose();

                    Action action = GetEncryptionAction(cancellationToken, fileEncryption, file, destinationPath, fileInfo, queue, maxBlockSize);
                    for (int i = 0; i < numThreads; i++)
                    {
                        encryptTasks.Add(Task.Factory.StartNew((action), cancellationToken));
                    }
                }
                try
                {
                    Task.WaitAll(encryptTasks.ToArray());
                }
                finally
                {
                    fileEncryption.Dispose();
                }
            }
        }
        private void AssetEncryptAction(string outputPath, bool overwriteExistingEncryptedFiles, CancellationToken cancellationToken, ConcurrentDictionary<string, IContentKey> keys, IIngestManifestAsset asset)
        {
            cancellationToken.ThrowIfCancellationRequested();

            List<Task> encryptTasks = new List<Task>();

            AssetCreationOptions assetCreationOptions = asset.Asset.Options;
            if (assetCreationOptions.HasFlag(AssetCreationOptions.StorageEncrypted))
            {
                IContentKey contentKeyData = keys[asset.Id];
                var fileEncryption = new FileEncryption(contentKeyData.GetClearKeyValue(), EncryptionUtils.GetKeyIdAsGuid(contentKeyData.Id));

                foreach (IngestManifestFileData file in asset.IngestManifestFiles)
                {
                    ulong iv = Convert.ToUInt64(file.InitializationVector, CultureInfo.InvariantCulture);
                    fileEncryption.SetInitializationVectorForFile(file.Name, iv);

                    FileInfo fileInfo = null;
                    fileInfo = TrackedFilesPaths.ContainsKey(file.Id) ? new FileInfo(TrackedFilesPaths[file.Id]) : new FileInfo(file.Name);

                    string destinationPath = Path.Combine(outputPath, fileInfo.Name);
                    if (File.Exists(destinationPath))
                    {
                        if (overwriteExistingEncryptedFiles)
                        {
                            File.Delete(destinationPath);
                        }
                        else
                        {
                            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, StringTable.BulkIngestFileExists, destinationPath));
                        }
                    }

                    long fileSize = fileInfo.Length;
                    int maxBlockSize = GetBlockSize(fileSize);

                    int numThreads = MaxNumberOfEncryptionThreadsForFilePerCore*Environment.ProcessorCount;
                    ConcurrentQueue<Tuple<int, int>> queue = PrepareUploadQueue(maxBlockSize, fileSize);
                    if (queue.Count < numThreads)
                    {
                        numThreads = queue.Count;
                    }

                    File.Create(destinationPath).Dispose();

                    Action action = GetEncryptionAction(cancellationToken, fileEncryption, file, destinationPath, fileInfo, queue, maxBlockSize);
                    for (int i = 0; i < numThreads; i++)
                    {
                        encryptTasks.Add(Task.Factory.StartNew((action), cancellationToken));
                    }

                }
                try
                {
                    Task.WaitAll(encryptTasks.ToArray());
                }
                finally
                {
                    fileEncryption.Dispose();
                }
            }
        }