async public Task <IActionResult> OnPostAsync()
        {
            string secretName = String.Empty;

            return(await SecureHandlerAsync(async() =>
            {
                await LoadCurrentLockerAsync(Input.LockerName);

                if (!ModelState.IsValid)
                {
                    throw new StatusMessageException($"Type a valid secret name.");
                }

                var secret = new VaultSecret()
                {
                    Name = Input.Name,
                    Description = Input.Description
                };
                await _secretsVaultDb.CreateVaultSecretAsync(Input.LockerName, secret, CancellationToken.None);

                secretName = secret.Name;
            }
                                            , onFinally : () => RedirectToPage("EditVaultSecret/Index", new { id = secretName, locker = Input.LockerName })
                                            , successMessage : ""
                                            , onException : (ex) => Page()));
        }
Esempio n. 2
0
        async public Task <bool> CreateVaultSecretAsync(string lockerName, VaultSecret vaultSecret, CancellationToken cancellationToken)
        {
            if (String.IsNullOrWhiteSpace(lockerName))
            {
                throw new StatusMessageException("Invalid locker name");
            }

            if (String.IsNullOrWhiteSpace(vaultSecret?.Name))
            {
                throw new StatusMessageException("Invalid secret name");
            }

            var diLocker = new DirectoryInfo($"{ _rootPath }/{ lockerName }");

            if (!diLocker.Exists)
            {
                throw new StatusMessageException($"Locker { lockerName } not exists");
            }

            var di = new DirectoryInfo($"{ diLocker.FullName }/{ vaultSecret.Name }");

            if (di.Exists)
            {
                throw new StatusMessageException($"Secret { vaultSecret.Name } already exists in locker { lockerName }");
            }

            di.Create();

            byte[] buffer = Encoding.UTF8.GetBytes(
                _cryptoService.EncryptText(_blobSerializer.SerializeObject(vaultSecret)));

            using (var fs = new FileStream($"{ di.FullName }/_item.meta", FileMode.OpenOrCreate,
                                           FileAccess.Write, FileShare.None, buffer.Length, true))
            {
                await fs.WriteAsync(buffer, 0, buffer.Length);
            }

            return(true);
        }
Esempio n. 3
0
        async public Task <bool> UpadteVaultSecretAsync(string lockerName, VaultSecret vaultSecret, CancellationToken cancellationToken)
        {
            var fi = new FileInfo($"{ _rootPath }/{ lockerName }/{ vaultSecret.Name }/_item.meta");

            if (!fi.Exists)
            {
                throw new StatusMessageException($"Secret { lockerName }/{ vaultSecret.Name } not exists");
            }
            ;

            byte[] buffer = Encoding.UTF8.GetBytes(
                _cryptoService.EncryptText(_blobSerializer.SerializeObject(vaultSecret)));

            fi.Delete();

            using (var fs = new FileStream($"{ fi.FullName }", FileMode.OpenOrCreate,
                                           FileAccess.Write, FileShare.None, buffer.Length, true))
            {
                await fs.WriteAsync(buffer, 0, buffer.Length);
            }

            return(true);
        }