Beispiel #1
0
        public bool UpdateEnvironmentFilePath(string path, string envName, bool ignoreFilePathTranslation = false)
        {
            var environments = DeserializeBoxEnvironmentFile();
            var foundEnv     = new BoxHomeConfigModel();

            environments.Environments.TryGetValue(envName, out foundEnv);
            if (foundEnv == null || string.IsNullOrEmpty(foundEnv.Name))
            {
                throw new Exception("Couldn't find that environment");
            }
            if (this.VerifyBoxConfigFile(path))
            {
                var newEnv = this.TranslateConfigFileToEnvironment(path, ignoreFilePathTranslation: ignoreFilePathTranslation);
                newEnv.AdminAsUserId               = (string.IsNullOrEmpty(foundEnv.AdminAsUserId)) ? "" : foundEnv.AdminAsUserId;
                newEnv.DefaultAsUserId             = (string.IsNullOrEmpty(foundEnv.DefaultAsUserId)) ? "" : foundEnv.DefaultAsUserId;
                newEnv.UseDefaultAsUser            = foundEnv.UseDefaultAsUser;
                environments.Environments[envName] = newEnv;
                this.SerializeBoxEnvironmentFile(environments);
                return(true);
            }
            else
            {
                throw new Exception("Not a valid config file.");
            }
        }
Beispiel #2
0
        public bool ToggleUserSessionEnabledSetting(bool turnOn = false, bool turnOff = false)
        {
            var environments = DeserializeBoxEnvironmentFile();
            var foundEnv     = new BoxHomeConfigModel();
            var envName      = environments.DefaultEnvironment;

            environments.Environments.TryGetValue(envName, out foundEnv);
            if (foundEnv == null || string.IsNullOrEmpty(foundEnv.Name))
            {
                throw new Exception("Can't find the current environment");
            }
            if (turnOff)
            {
                foundEnv.UserSessionEnabled = false;
            }
            else if (turnOn)
            {
                foundEnv.UserSessionEnabled = true;
            }
            else
            {
                foundEnv.UserSessionEnabled = !foundEnv.UserSessionEnabled;
            }
            environments.Environments[envName] = foundEnv;
            this.SerializeBoxEnvironmentFile(environments);
            return(foundEnv.UserSessionEnabled);
        }
Beispiel #3
0
        public bool UpdateEnvironment(BoxHomeConfigModel newEnv, string envName)
        {
            var environments = DeserializeBoxEnvironmentFile();
            var foundEnv     = new BoxHomeConfigModel();

            environments.Environments.TryGetValue(envName, out foundEnv);
            if (foundEnv == null || string.IsNullOrEmpty(foundEnv.Name))
            {
                throw new Exception("Couldn't find that environment");
            }
            environments.Environments[envName] = newEnv;
            this.SerializeBoxEnvironmentFile(environments);
            return(true);
        }
Beispiel #4
0
        public bool UpdatePrivateKeyPath(string existingName, string newPath)
        {
            var environments = DeserializeBoxEnvironmentFile();
            var foundEnv     = new BoxHomeConfigModel();

            environments.Environments.TryGetValue(existingName, out foundEnv);
            if (foundEnv == null || string.IsNullOrEmpty(foundEnv.Name))
            {
                throw new Exception("Couldn't find that environment");
            }
            foundEnv.PrivateKeyPath = GeneralUtilities.TranslatePath(newPath);
            environments.Environments[existingName] = foundEnv;
            this.SerializeBoxEnvironmentFile(environments);
            return(true);
        }
Beispiel #5
0
        public bool SetUserSessionExpirationSetting(DateTime expires)
        {
            var environments = DeserializeBoxEnvironmentFile();
            var foundEnv     = new BoxHomeConfigModel();
            var envName      = environments.DefaultEnvironment;

            environments.Environments.TryGetValue(envName, out foundEnv);
            if (foundEnv == null || string.IsNullOrEmpty(foundEnv.Name))
            {
                throw new Exception("Couldn't find that environment");
            }
            foundEnv.UserSessionExpiration     = expires;
            environments.Environments[envName] = foundEnv;
            this.SerializeBoxEnvironmentFile(environments);
            return(true);
        }
Beispiel #6
0
        public bool RemoveTempAsUserIdSetting()
        {
            var environments = DeserializeBoxEnvironmentFile();
            var foundEnv     = new BoxHomeConfigModel();
            var envName      = environments.DefaultEnvironment;

            environments.Environments.TryGetValue(envName, out foundEnv);
            if (foundEnv == null || string.IsNullOrEmpty(foundEnv.Name))
            {
                throw new Exception("Can't find the current environment");
            }
            foundEnv.TempAsUserId = "";
            environments.Environments[envName] = foundEnv;
            this.SerializeBoxEnvironmentFile(environments);
            return(true);
        }
Beispiel #7
0
        public bool SetAdminAsUserIdSetting(string adminUserId, string envName = "")
        {
            var environments = DeserializeBoxEnvironmentFile();
            var foundEnv     = new BoxHomeConfigModel();

            if (string.IsNullOrEmpty(envName))
            {
                envName = environments.DefaultEnvironment;
            }
            environments.Environments.TryGetValue(envName, out foundEnv);
            if (foundEnv == null || string.IsNullOrEmpty(foundEnv.Name))
            {
                throw new Exception("Couldn't find that environment");
            }
            foundEnv.AdminAsUserId             = adminUserId;
            environments.Environments[envName] = foundEnv;
            this.SerializeBoxEnvironmentFile(environments);
            return(true);
        }
Beispiel #8
0
        public bool AddNewEnvironment(BoxHomeConfigModel env, bool isDefault = false)
        {
            var update = DeserializeBoxEnvironmentFile();

            if (isDefault || string.IsNullOrEmpty(update.DefaultEnvironment))
            {
                update.DefaultEnvironment = env.Name;
            }
            if (!CheckForDistinctEnvironments(update.Environments, env.Name))
            {
                update.Environments.Add(env.Name, env);
                SerializeBoxEnvironmentFile(update);
                return(true);
            }
            else
            {
                Reporter.WriteWarning("This environment already exists.");
                return(false);
            }
        }
Beispiel #9
0
        public bool ChangeBoxEnvironmentName(string existingName, string newName)
        {
            var environments = DeserializeBoxEnvironmentFile();
            var foundEnv     = new BoxHomeConfigModel();

            environments.Environments.TryGetValue(existingName, out foundEnv);
            if (foundEnv == null || string.IsNullOrEmpty(foundEnv.Name))
            {
                throw new Exception("Couldn't find that environment");
            }
            environments.Environments.Remove(existingName);

            if (environments.DefaultEnvironment == existingName)
            {
                environments.DefaultEnvironment = newName;
            }
            environments.Environments[newName] = foundEnv;
            this.SerializeBoxEnvironmentFile(environments);
            return(true);
        }
Beispiel #10
0
        public bool UpdateConfigFilePath(string existingName, string newPath, string newPemPath = "",
                                         bool ignoreFilePathTranslation = false)
        {
            var environments = DeserializeBoxEnvironmentFile();
            var foundEnv     = new BoxHomeConfigModel();

            environments.Environments.TryGetValue(existingName, out foundEnv);
            if (foundEnv == null || string.IsNullOrEmpty(foundEnv.Name))
            {
                throw new Exception("Couldn't find that environment");
            }
            if (!ignoreFilePathTranslation)
            {
                newPath = GeneralUtilities.TranslatePath(newPath);
            }
            if (this.VerifyBoxConfigFile(newPath))
            {
                BoxHomeConfigModel env;
                if (!string.IsNullOrEmpty(newPemPath))
                {
                    if (!ignoreFilePathTranslation)
                    {
                        newPemPath = GeneralUtilities.TranslatePath(newPemPath);
                    }
                    env = this.TranslateConfigFileToEnvironment(newPath, newPemPath, ignoreFilePathTranslation: ignoreFilePathTranslation);
                }
                else
                {
                    env = this.TranslateConfigFileToEnvironment(newPath, ignoreFilePathTranslation: ignoreFilePathTranslation);
                }
                foundEnv.BoxConfigFilePath   = env.BoxConfigFilePath;
                foundEnv.ClientId            = env.ClientId;
                foundEnv.EnterpriseId        = env.EnterpriseId;
                foundEnv.HasInLinePrivateKey = env.HasInLinePrivateKey;
                foundEnv.PrivateKeyPath      = env.PrivateKeyPath;
            }
            environments.Environments[existingName] = foundEnv;
            this.SerializeBoxEnvironmentFile(environments);
            return(true);
        }
Beispiel #11
0
        public BoxHomeConfigModel TranslateConfigFileToEnvironment(string filePath, string privateKeyPath = "", bool ignoreFilePathTranslation = false)
        {
            if (!ignoreFilePathTranslation)
            {
                filePath = GeneralUtilities.TranslatePath(filePath);
            }
            var translatedConfig = new BoxHomeConfigModel();

            if (File.Exists(filePath))
            {
                var config = DeserializeBoxConfigFile(filePath);
                if (!string.IsNullOrEmpty(privateKeyPath))
                {
                    if (!ignoreFilePathTranslation)
                    {
                        privateKeyPath = GeneralUtilities.TranslatePath(privateKeyPath);
                    }
                    if (File.Exists(privateKeyPath))
                    {
                        translatedConfig.PrivateKeyPath = privateKeyPath;
                    }
                    else
                    {
                        throw new Exception($"Couldn't access the private key file from the path provided: {privateKeyPath}.");
                    }
                }
                else if (!string.IsNullOrEmpty(config.AppSettings.AppAuth.PrivateKey))
                {
                    Reporter.WriteInformation("Detected private key value in config...");
                    Reporter.WriteInformation("Calculating between in-line private key or separate private key file...");
                    var pattern = @"^-----BEGIN ENCRYPTED PRIVATE KEY-----\n";
                    var regex   = new Regex(pattern);
                    if (regex.IsMatch(config.AppSettings.AppAuth.PrivateKey))
                    {
                        Reporter.WriteInformation("Detected in-line private key.");
                        translatedConfig.HasInLinePrivateKey = true;
                    }
                    else
                    {
                        Reporter.WriteInformation("Attempting to resolve file path for private key.");
                        var privateKeyPathInline = config.AppSettings.AppAuth.PrivateKey;
                        if (!ignoreFilePathTranslation)
                        {
                            privateKeyPathInline = GeneralUtilities.TranslateDependentPath(privateKeyPathInline, filePath);
                        }
                        Reporter.WriteInformation($"Path to private key file identified: {privateKeyPathInline}.");
                        if (File.Exists(privateKeyPathInline))
                        {
                            translatedConfig.PrivateKeyPath = privateKeyPathInline;
                        }
                        else
                        {
                            throw new Exception($"Unable to open private key file at {privateKeyPathInline}");
                        }
                    }
                }
                else
                {
                    throw new Exception("No in-line private key or private key file path provided.");
                }
                translatedConfig.ClientId          = config.AppSettings.ClientId;
                translatedConfig.EnterpriseId      = config.EnterpriseId;
                translatedConfig.BoxConfigFilePath = filePath;
            }
            else
            {
                throw new Exception($"Couldn't open config file at {filePath}");
            }
            return(translatedConfig);
        }