This class uses a symmetric key algorithm (Rijndael/AES) to encrypt and decrypt data. As long as encryption and decryption routines use the same parameters to generate the keys, the keys are guaranteed to be the same. The class uses static functions with duplicate code to make it easier to demonstrate encryption and decryption logic. In a real-life application, this may not be the most efficient way of handling encryption, so - as soon as you feel comfortable with it - you may want to redesign this class.
Example #1
0
        public StringBuilder Execute(Dictionary <string, StringBuilder> values, IWorkspace theWorkspace)
        {
            if (File.Exists(ServerSecurityService.FileName))
            {
                string encryptedData;
                using (var inStream = new FileStream(ServerSecurityService.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    using (var reader = new StreamReader(inStream))
                    {
                        encryptedData = reader.ReadToEnd();
                    }
                }

                try
                {
                    var decryptData = SecurityEncryption.Decrypt(encryptedData);
                    var currentSecuritySettingsTo = JsonConvert.DeserializeObject <SecuritySettingsTO>(decryptData);
                    var permissionGroup           = currentSecuritySettingsTo.WindowsGroupPermissions;

                    // We need to change BuiltIn\Administrators to -> Warewolf Administrators ;)
                    if (permissionGroup.Count > 0)
                    {
                        var adminGrp = permissionGroup[0].WindowsGroup;
                        if (adminGrp == "BuiltIn\\Administrators")
                        {
                            permissionGroup[0].WindowsGroup = WindowsGroupPermission.BuiltInAdministratorsText;
                            decryptData = JsonConvert.SerializeObject(currentSecuritySettingsTo);
                        }
                    }

                    var hasGuestPermission = permissionGroup.Any(permission => permission.IsBuiltInGuests);
                    var hasAdminPermission = permissionGroup.Any(permission => permission.IsBuiltInAdministrators);
                    if (!hasAdminPermission)
                    {
                        permissionGroup.Add(WindowsGroupPermission.CreateAdministrators());
                        permissionGroup.Sort(QuickSortForPermissions);
                        decryptData = JsonConvert.SerializeObject(currentSecuritySettingsTo);
                    }
                    if (!hasGuestPermission)
                    {
                        permissionGroup.Add(WindowsGroupPermission.CreateGuests());
                        permissionGroup.Sort(QuickSortForPermissions);
                        decryptData = JsonConvert.SerializeObject(currentSecuritySettingsTo);
                    }
                    return(new StringBuilder(decryptData));
                }
                catch (Exception e)
                {
                    Dev2Logger.Log.Error("SecurityRead", e);
                }
            }

            var serializer         = new Dev2JsonSerializer();
            var securitySettingsTo = new SecuritySettingsTO(DefaultPermissions)
            {
                CacheTimeout = _cacheTimeout
            };

            return(serializer.SerializeToBuilder(securitySettingsTo));
        }
Example #2
0
        static void DoFileEncryption(string permissions)
        {
            var byteConverter = new ASCIIEncoding();
            var encryptedData = SecurityEncryption.Encrypt(permissions);

            byte[] dataToEncrypt = byteConverter.GetBytes(encryptedData);
            using (var outStream = new FileStream(EnvironmentVariables.ServerSecuritySettingsFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
            {
                outStream.SetLength(0);
                outStream.Write(dataToEncrypt, 0, dataToEncrypt.Length);
                outStream.Flush();
            }
        }
Example #3
0
        StringBuilder Execute(string encryptedData)
        {
            var decryptData = SecurityEncryption.Decrypt(encryptedData);

            Dev2Logger.Debug(decryptData, GlobalConstants.WarewolfDebug);
            var currentSecuritySettingsTo = JsonConvert.DeserializeObject <SecuritySettingsTO>(decryptData);

            if (currentSecuritySettingsTo.WindowsGroupPermissions.Any(a => a.ResourceID != Guid.Empty))
            {
                foreach (var perm in currentSecuritySettingsTo.WindowsGroupPermissions.Where(a => a.ResourceID != Guid.Empty))
                {
                    perm.ResourceName = Catalog.GetResourcePath(GlobalConstants.ServerWorkspaceID, perm.ResourceID);
                }
            }
            decryptData = JsonConvert.SerializeObject(currentSecuritySettingsTo);
            var permissionGroup = currentSecuritySettingsTo.WindowsGroupPermissions;

            // We need to change BuiltIn\Administrators to -> Warewolf Administrators ;)
            if (permissionGroup.Count > 0)
            {
                var adminGrp = permissionGroup[0].WindowsGroup;
                if (adminGrp == "BuiltIn\\Administrators")
                {
                    permissionGroup[0].WindowsGroup = WindowsGroupPermission.BuiltInAdministratorsText;
                    decryptData = JsonConvert.SerializeObject(currentSecuritySettingsTo);
                }
            }

            var hasGuestPermission = permissionGroup.Any(permission => permission.IsBuiltInGuests);
            var hasAdminPermission = permissionGroup.Any(permission => permission.IsBuiltInAdministrators);

            if (!hasAdminPermission)
            {
                permissionGroup.Add(WindowsGroupPermission.CreateAdministrators());
                permissionGroup.Sort(QuickSortForPermissions);
                decryptData = JsonConvert.SerializeObject(currentSecuritySettingsTo);
            }
            if (!hasGuestPermission)
            {
                permissionGroup.Add(WindowsGroupPermission.CreateGuests());
                permissionGroup.Sort(QuickSortForPermissions);
                decryptData = JsonConvert.SerializeObject(currentSecuritySettingsTo);
            }
            return(new StringBuilder(decryptData));
        }
Example #4
0
        public StringBuilder Execute(Dictionary <string, StringBuilder> values, IWorkspace theWorkspace)
        {
            Dev2JsonSerializer serializer = new Dev2JsonSerializer();

            try
            {
                Dev2Logger.Info("Fetch Tests for deploy Service");

                StringBuilder resourceIdString;
                values.TryGetValue("resourceID", out resourceIdString);
                if (resourceIdString == null)
                {
                    throw new InvalidDataContractException("resourceID is missing");
                }
                Guid resourceId;
                if (!Guid.TryParse(resourceIdString.ToString(), out resourceId))
                {
                    throw new InvalidDataContractException("resourceID is not a valid GUID.");
                }
                var tests = TestCatalog.Fetch(resourceId);
                foreach (var serviceTestModelTO in tests.Where(to => !string.IsNullOrEmpty(to.Password)))
                {
                    serviceTestModelTO.Password = SecurityEncryption.Encrypt(serviceTestModelTO.Password);
                }
                CompressedExecuteMessage message = new CompressedExecuteMessage();
                message.SetMessage(serializer.Serialize(tests));
                message.HasError = false;

                return(serializer.SerializeToBuilder(message));
            }
            catch (Exception err)
            {
                Dev2Logger.Error(err);
                var res = new CompressedExecuteMessage {
                    HasError = true, Message = new StringBuilder(err.Message)
                };
                return(serializer.SerializeToBuilder(res));
            }
        }
        public StringBuilder Execute(Dictionary <string, StringBuilder> values, IWorkspace theWorkspace)
        {
            Dev2Logger.Debug("Start Security Read");
            var serverSecuritySettingsFile = EnvironmentVariables.ServerSecuritySettingsFile;

            if (File.Exists(serverSecuritySettingsFile))
            {
                string encryptedData;
                using (var inStream = new FileStream(serverSecuritySettingsFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    using (var reader = new StreamReader(inStream))
                    {
                        encryptedData = reader.ReadToEnd();
                    }
                }
                Dev2Logger.Debug("Security Data Read");
                try
                {
                    var decryptData = SecurityEncryption.Decrypt(encryptedData);
                    Dev2Logger.Debug(decryptData);
                    var currentSecuritySettingsTo = JsonConvert.DeserializeObject <SecuritySettingsTO>(decryptData);
                    if (currentSecuritySettingsTo.WindowsGroupPermissions.Any(a => a.ResourceID != Guid.Empty))
                    {
                        foreach (var perm in currentSecuritySettingsTo.WindowsGroupPermissions.Where(a => a.ResourceID != Guid.Empty))
                        {
                            perm.ResourceName = Catalog.GetResourcePath(GlobalConstants.ServerWorkspaceID, perm.ResourceID);
                        }
                    }
                    decryptData = JsonConvert.SerializeObject(currentSecuritySettingsTo);
                    var permissionGroup = currentSecuritySettingsTo.WindowsGroupPermissions;

                    // We need to change BuiltIn\Administrators to -> Warewolf Administrators ;)
                    if (permissionGroup.Count > 0)
                    {
                        var adminGrp = permissionGroup[0].WindowsGroup;
                        if (adminGrp == "BuiltIn\\Administrators")
                        {
                            permissionGroup[0].WindowsGroup = WindowsGroupPermission.BuiltInAdministratorsText;
                            decryptData = JsonConvert.SerializeObject(currentSecuritySettingsTo);
                        }
                    }

                    var hasGuestPermission = permissionGroup.Any(permission => permission.IsBuiltInGuests);
                    var hasAdminPermission = permissionGroup.Any(permission => permission.IsBuiltInAdministrators);
                    if (!hasAdminPermission)
                    {
                        permissionGroup.Add(WindowsGroupPermission.CreateAdministrators());
                        permissionGroup.Sort(QuickSortForPermissions);
                        decryptData = JsonConvert.SerializeObject(currentSecuritySettingsTo);
                    }
                    if (!hasGuestPermission)
                    {
                        permissionGroup.Add(WindowsGroupPermission.CreateGuests());
                        permissionGroup.Sort(QuickSortForPermissions);
                        decryptData = JsonConvert.SerializeObject(currentSecuritySettingsTo);
                    }
                    return(new StringBuilder(decryptData));
                }
                catch (Exception e)
                {
                    Dev2Logger.Error("SecurityRead", e);
                }
            }

            var serializer         = new Dev2JsonSerializer();
            var securitySettingsTo = new SecuritySettingsTO(DefaultPermissions)
            {
                CacheTimeout = _cacheTimeout
            };

            return(serializer.SerializeToBuilder(securitySettingsTo));
        }