Example #1
0
        public bool SaveEncryptedFile(FilePartCache cache, string outFile)
        {
            if (string.IsNullOrEmpty(cache.Container))
            {
                throw new Exception("The container must be set");
            }

            var tenant = GetTenant(cache.TenantID);

            try
            {
                using (var q = new WriterLock(cache.TenantID, cache.Container + "|" + cache.FileName))
                {
                    //Create engine
                    var tenantKey = tenant.Key.Decrypt(MasterKey, IV);
                    using (var engine = new FileEngine(MasterKey, tenantKey, cache.TenantID, IV))
                    {
                        using (var context = new gFileSystemEntities(ConfigHelper.ConnectionString))
                        {
                            this.RemoveFile(tenant.UniqueKey, cache.Container, cache.FileName);
                            var containerItem = GetContainer(tenant, cache.Container);

                            var fiCipher = new FileInfo(outFile);
                            var stash    = new FileStash
                            {
                                Path             = cache.FileName,
                                TenantID         = tenant.TenantID,
                                Size             = cache.Size,
                                StorageSize      = fiCipher.Length,
                                ContainerId      = containerItem.ContainerId,
                                CrcPlain         = cache.CRC,
                                IsCompressed     = false,
                                FileCreatedTime  = cache.CreatedTime,
                                FileModifiedTime = cache.ModifiedTime,
                                UniqueKey        = cache.ID,
                            };
                            context.AddItem(stash);
                            context.SaveChanges();

                            //Move the cipher file to storage
                            var destFile = GetFilePath(tenant.UniqueKey, containerItem.UniqueKey, stash);
                            File.Move(outFile, destFile);
                        }
                    }

                    return(true);
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                throw;
            }
        }
Example #2
0
        /// <summary>
        /// Given a valid tenant, this will change the tenant key and ensure all files
        /// associated to that tenant are been re-keyed. This routine can be used if the
        /// tenant key has been compromised.
        /// </summary>
        public int RekeyTenant(Guid tenantID)
        {
            var tenant = GetTenant(tenantID);

            try
            {
                using (var q = new WriterLock(tenantID, ""))
                {
                    var count = 0;

                    //Create engine
                    var tenantKey = tenant.Key.Decrypt(MasterKey, IV);
                    var newKey    = FileUtilities.GenerateKey();
                    using (var engine = new FileEngine(MasterKey, tenantKey, tenantID, IV))
                    {
                        engine.WorkingFolder = ConfigHelper.WorkFolder;

                        using (var context = new gFileSystemEntities(ConfigHelper.ConnectionString))
                        {
                            var all = context.FileStash
                                      .Include(x => x.Container)
                                      .Where(x => x.TenantID == tenant.TenantID)
                                      .ToList();

                            //Loop through all files for this tenant and re-encrypt the data key for each file
                            //There is nothing to change in the database
                            foreach (var stash in all)
                            {
                                var existingFile = GetFilePath(tenant.UniqueKey, stash.Container.UniqueKey, stash);
                                if (File.Exists(existingFile))
                                {
                                    if (engine.RekeyFile(existingFile, newKey))
                                    {
                                        count++;
                                    }
                                }
                            }

                            //Save the new tenant key
                            tenant     = context.Tenant.FirstOrDefault(x => x.UniqueKey == tenantID);
                            tenant.Key = newKey.Encrypt(MasterKey, IV);
                            context.SaveChanges();
                        }
                    }
                    return(count);
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                throw;
            }
        }
Example #3
0
        /// <summary>
        /// Removes a file from storeage for a tenant in the specified container
        /// using the filenme as the lookup key
        /// </summary>
        /// <param name="tenantID"></param>
        /// <param name="container"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public int RemoveFile(Guid tenantID, string container, string fileName)
        {
            if (string.IsNullOrEmpty(container))
            {
                throw new Exception("The container must be set");
            }

            var tenant = GetTenant(tenantID);

            try
            {
                using (var q = new WriterLock(tenantID, ""))
                {
                    var count = 0;
                    using (var context = new gFileSystemEntities(ConfigHelper.ConnectionString))
                    {
                        var all = context.FileStash
                                  .Include(x => x.Container)
                                  .Where(x => x.TenantID == tenant.TenantID &&
                                         x.Container.Name == container &&
                                         x.Path == fileName)
                                  .ToList();

                        foreach (var stash in all)
                        {
                            var existingFile = GetFilePath(tenant.UniqueKey, stash.Container.UniqueKey, stash);
                            if (File.Exists(existingFile))
                            {
                                File.Delete(existingFile);
                            }
                            context.DeleteItem(stash);
                            count++;
                        }
                        context.SaveChanges();
                    }
                    return(count);
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                throw;
            }
        }
Example #4
0
        /// <summary>
        /// Creates a new tenant or retieves an existing one.
        /// The tenant ID is returned
        /// </summary>
        public Guid GetOrAddTenant(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new Exception("The name must be specified.");
            }

            try
            {
                using (var q = new WriterLock(GetOrAddTenantLockID, "GetOrAddTenant"))
                {
                    //Add/get a tenant in a transaction
                    var parameters = new List <SqlParameter>();
                    parameters.Add(new SqlParameter {
                        DbType = DbType.String, IsNullable = false, ParameterName = "@name", Value = name
                    });
                    parameters.Add(new SqlParameter {
                        DbType = DbType.Binary, IsNullable = false, ParameterName = "@key", Value = FileUtilities.GenerateKey().Encrypt(MasterKey, IV)
                    });
                    var tenantID = (Guid)SqlHelper.ExecuteWithReturn(ConfigHelper.ConnectionString, "[AddOrUpdateTenant] @name, @key", parameters);

                    //Create the tenant storage folder
                    var tFolder = Path.Combine(ConfigHelper.StorageFolder, tenantID.ToString());
                    if (!Directory.Exists(tFolder))
                    {
                        Directory.CreateDirectory(tFolder);
                    }

                    return(tenantID);
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                throw;
            }
        }