/// <summary>
        /// Creates (recursively) a group from a downloaded folder
        /// </summary>
        /// <param name="groupName">The group name of the created group.</param>
        /// <param name="secrets">The downloaded secret folder.</param>
        /// <param name="icon">The icon</param>
        /// <returns>The created keepass group</returns>
        private PwGroup CreateGroup(string groupName, SecretFolder secrets, PwIcon icon)
        {
            var group = new PwGroup(true, true, groupName, icon);

            group.CreationTime = DateTime.Now;

            // Create subfolder to recreate tree structure
            foreach (var subFolder in secrets.Folders)
            {
                // Create a subfolder only if there's something in it
                // Avoids having a lot of empty folders we don't have access to in Vault
                if (subFolder.Folders.Count() > 0 || subFolder.Secrets.Count() > 0)
                {
                    this.syncStatus.AddLog(string.Format("Create group {0} in {1}", subFolder.Name, groupName));
                    var subGroup = this.CreateGroup(subFolder.Name, subFolder, icon);
                    group.AddGroup(subGroup, true);
                }
            }

            // Create entries at this level
            foreach (var secret in secrets.Secrets)
            {
                this.syncStatus.AddLog(string.Format("Create entry {0} in {1}", secret.Name, groupName));
                var entry = this.CreateEntry(secret);
                group.AddEntry(entry, true);
            }

            return(group);
        }
        /// <summary>
        /// Download the secrets from a path (following the '.../v1/secret/')
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>The secret tree structure.</returns>
        public async Task <SecretFolder> GetSecrets(string path)
        {
            var folder = new SecretFolder(this.GetLastFolder(path));

            // Gets the list of secrets for this path
            IEnumerable <string> keyList = new List <string>();;

            try
            {
                keyList = await this.GetSecretList(path);
            }
            catch (VaultRequestException ex)
            {
                // May be connection issue, or forbidden. We just continue.
            }

            foreach (var key in keyList)
            {
                if (SecretFolder.IsFolder(key))
                {
                    // Resursive call
                    folder.AddFolder(await this.GetSecrets(path + "/" + key));
                }
                else
                {
                    // Adds secret
                    folder.AddSecret(this.GetSecret(path + "/" + key));
                }
            }

            return(folder);
        }
Beispiel #3
0
        /// <summary>
        /// Get the last folder of a path
        /// </summary>
        /// <param name="fullpath">The path</param>
        /// <returns>The last folder</returns>
        private string GetLastFolder(string fullpath)
        {
            var name = fullpath;

            if (SecretFolder.IsFolder(name))
            {
                // Remove the trailing /
                name = name.Substring(0, name.Length - 1);
            }

            return(name.Substring(name.LastIndexOf("/") + 1));
        }
Beispiel #4
0
        /// <summary>
        /// Download the secrets from a path (following the '.../v1/secret/')
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>The secret tree structure.</returns>
        public async Task <SecretFolder> GetSecrets(string path)
        {
            var folder = new SecretFolder(this.GetLastFolder(path));

            // Gets the list of secrets for this path
            IEnumerable <string> keyList = new List <string>();;

            try
            {
                keyList = await this.GetSecretList(path);
            }
            catch (VaultRequestException ex)
            {
                // May be connection issue, or forbidden. We just continue.
            }

            if (keyList.Count() < 1)
            {
                KeePassLib.Utility.MessageService.ShowWarning("No Secrets could be retrieved for: " + path);
                return(null);
            }

            foreach (var key in keyList)
            {
                try
                {
                    if (SecretFolder.IsFolder(key))
                    {
                        // Resursive call
                        folder.AddFolder(await this.GetSecrets(path + "/" + key));
                    }
                    else
                    {
                        folder.AddSecret(this.GetSecret(path + "/" + key));
                    }
                }
                catch (Exception ex)
                {
                    // we are unable to retrieve the secret.
                    // cause could be ACL prohibiting READ access to secret
                }
            }

            return(folder);
        }
Beispiel #5
0
        /// <summary>
        /// Download the secrets from a path (following the '.../v1/secret/')
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>The secret tree structure.</returns>
        public async Task <SecretFolder> GetSecrets(string path)
        {
            var folder = new SecretFolder(this.GetLastFolder(path));

            // Gets the list of secrets for this path
            SecretKeys keyList;

            try
            {
                keyList = await this.GetSecretList(path);
            }
            catch (VaultRequestException ex)
            {
                // May be connection issue, or forbidden. We just continue, but we store the error for future display
                this.syncStatus.AddLog(string.Format("Exception for {0}: {1}{2}{3}", path, ex.Message, Environment.NewLine, ex.StackTrace));
                return(folder);
            }

            if (keyList.Keys.Count() > 0)
            {
                this.syncStatus.AddLog(string.Format("{0} keys fetched in {1}", keyList.Keys.Count(), path));
            }

            foreach (var key in keyList.Keys)
            {
                if (SecretFolder.IsFolder(key))
                {
                    // Resursive call
                    folder.AddFolder(await this.GetSecrets(path + "/" + key));
                }
                else
                {
                    // Adds secret
                    folder.AddSecret(this.GetSecret(path + "/" + key));
                }
            }

            if (folder.Secrets.Count() > 0)
            {
                this.syncStatus.AddLog(string.Format("{0} secrets fetched in {1}", folder.Secrets.Count(), path));
            }

            return(folder);
        }
Beispiel #6
0
 /// <summary>
 /// Adds a sub folder in the folder
 /// </summary>
 /// <param name="folder">The folder</param>
 public void AddFolder(SecretFolder folder)
 {
     this.folders.Add(folder);
 }