protected override void uxMenuSecretKind_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            var sk = (SecretKind)e.ClickedItem;

            if (sk.Checked)
            {
                return;             // Same item was clicked
            }
            foreach (var item in uxMenuSecretKind.Items)
            {
                ((SecretKind)item).Checked = false;
            }
            PropertyObject.SecretKind = sk;
            PropertyObject.PopulateCustomTags();
            // Populate default expiration and value template in case this is a new secret
            if (_mode == ItemDialogBaseMode.New)
            {
                PropertyObject.PopulateExpiration();
                uxTextBoxValue.Text = sk.ValueTemplate;
            }
            sk.Checked = true;
            uxLinkLabelSecretKind.Text = sk.ToString();
            uxToolTip.SetToolTip(uxLinkLabelSecretKind, sk.Description);
            RefreshCertificate(_certificateObj);
            uxTextBoxName_TextChanged(sender, null);
            uxTextBoxValue_TextChanged(sender, null);
            uxPropertyGridSecret.Refresh();
        }
        private void CopyToClipboard(Vault vault)
        {
            PropertyObject po = null;

            switch (Collection)
            {
            case VaultUriCollection.Keys:
                return;

            case VaultUriCollection.Certificates:
                var cb   = vault.GetCertificateAsync(ItemName, Version, CancellationToken.None).GetAwaiter().GetResult();
                var cert = vault.GetCertificateWithExportableKeysAsync(ItemName, Version, CancellationToken.None).GetAwaiter().GetResult();
                po = new PropertyObjectCertificate(cb, cb.Policy, cert, null);
                break;

            case VaultUriCollection.Secrets:
                var s = vault.GetSecretAsync(ItemName, Version, CancellationToken.None).GetAwaiter().GetResult();
                po = new PropertyObjectSecret(s, null);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(Collection), $"Invalid endpoint {Collection}");
            }
            po.CopyToClipboard(true);
        }
        private async void uxButtonCopy_Click(object sender, EventArgs e)
        {
            var item = uxListViewSecrets.FirstSelectedItem;

            if (null != item)
            {
                using (var op = NewUxOperationWithProgress(uxButtonCopy, uxMenuItemCopy))
                {
                    PropertyObject po = null;
                    await op.Invoke($"get {item.Kind} from", async() => po = await item.GetAsync(op.CancellationToken));

                    po.CopyToClipboard(false); // Always execute on single thread apartment (STA) - UI thread, because of OLE limitations
                }
            }
        }
        public void FillTagsAndExpiration(PropertyObject obj)
        {
            ObservableTagItemsCollection tags = obj.Tags;

            tags.AddOrReplace(new TagItem("Thumbprint", Certificate.Thumbprint.ToLowerInvariant()));
            tags.AddOrReplace(new TagItem("Expiration", Certificate.GetExpirationDateString()));
            tags.AddOrReplace(new TagItem("Subject", Certificate.GetNameInfo(X509NameType.SimpleName, false)));
            var sans =
                from X509Extension ext in Certificate.Extensions
                where ext.Oid.Value == "2.5.29.17" // Subject Alternative Name
                select ext.Format(false).Replace("DNS Name=", "");

            tags.AddOrReplace(new TagItem("SAN", string.Join(";", sans)));
            obj.NotBefore = Certificate.NotBefore;
            obj.Expires   = Certificate.NotAfter;
        }
Exemplo n.º 5
0
        private void SecretObject_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            _changed = true;
            if (e.PropertyName == nameof(PropertyObject.ContentType)) // ContentType changed, refresh
            {
                AutoDetectSecretKind();
                RefreshCertificate(_certificateObj);
                uxTextBoxValue_TextChanged(sender, null);
            }

            string tagsExpirationError = PropertyObject.AreCustomTagsValid();

            if (false == PropertyObject.IsExpirationValid)
            {
                tagsExpirationError += $"Expiration values are invalid: 'Valid from time' must be less then 'Valid until time' and expiration period must be less or equal to {Utils.ExpirationToString(PropertyObject.SecretKind.MaxExpiration)}";
            }
            uxErrorProvider.SetError(uxPropertyGridSecret, string.IsNullOrEmpty(tagsExpirationError) ? null : tagsExpirationError);

            InvalidateOkButton();
        }
        private async void uxButtonSave_Click(object sender, EventArgs e)
        {
            var item = uxListViewSecrets.FirstSelectedItem;

            if (null != item)
            {
                PropertyObject po = null;
                using (var op = NewUxOperationWithProgress(uxButtonSave, uxMenuItemSave)) await op.Invoke($"get {item.Kind} from", async() =>
                    {
                        po = await item.GetAsync(op.CancellationToken);
                    });
                uxSaveFileDialog.FileName    = po.GetFileName();
                uxSaveFileDialog.DefaultExt  = po.GetContentType().ToExtension();
                uxSaveFileDialog.FilterIndex = po.GetContentType().ToFilterIndex();
                if (uxSaveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    po.SaveToFile(uxSaveFileDialog.FileName);
                }
            }
        }
Exemplo n.º 7
0
        public static bool VerifyDuplication(ISession session, string oldName, PropertyObject soNew)
        {
            string newMd5 = soNew.Md5;

            // Check if we already have *another* secret with the same name
            if ((oldName != soNew.Name) && (session.ListViewSecrets.Items.ContainsKey(soNew.Name) &&
                                            (MessageBox.Show($"Are you sure you want to replace existing item '{soNew.Name}' with new value?", Utils.AppName, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.Yes)))
            {
                return(false);
            }

            // Detect dups by Md5
            var sameSecretsList = from slvi in session.ListViewSecrets.Items.Cast <ListViewItemBase>() where (slvi.Md5 == newMd5) && (slvi.Name != oldName) && (slvi.Name != soNew.Name) select slvi.Name;

            if ((sameSecretsList.Count() > 0) &&
                (MessageBox.Show($"There are {sameSecretsList.Count()} other item(s) in the vault which has the same Md5: {newMd5}.\nHere the name(s) of the other items:\n{string.Join(", ", sameSecretsList)}\nAre you sure you want to add or update item {soNew.Name} and have a duplication of secrets?", Utils.AppName, MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) != DialogResult.Yes))
            {
                return(false);
            }
            return(true);
        }
        private async void uxListViewSecrets_ItemDrag(object sender, ItemDragEventArgs e)
        {
            using (var op = NewUxOperation(uxButtonSave, uxMenuItemSave))
            {
                _ctrlKeyPressed  = (ModifierKeys & Keys.Control) != 0;
                _shiftKeyPressed = (ModifierKeys & Keys.Shift) != 0;
                List <string> filesList = new List <string>();
                foreach (var item in uxListViewSecrets.SelectedItems.Cast <ListViewItemBase>())
                {
                    PropertyObject po = null;
                    await op.Invoke("get item from", async() => po = await item.GetAsync(op.CancellationToken));

                    // Pick .kv-secret or .kv-certificate or .url extension if CTRL and SHIFT are pressed
                    var filename = po.Name + (_ctrlKeyPressed & _shiftKeyPressed ? ContentType.KeyVaultLink.ToExtension() : _ctrlKeyPressed ? po.GetKeyVaultFileExtension() : po.GetContentType().ToExtension());
                    var fullName = Path.Combine(Path.GetTempPath(), filename);
                    po.SaveToFile(fullName);
                    filesList.Add(fullName);
                }
                var dataObject = new DataObject(DataFormats.FileDrop, filesList.ToArray());
                uxListViewSecrets.DoDragDrop(dataObject, DragDropEffects.Move);
            }
        }
        public override async Task <ListViewItemBase> UpdateAsync(object originalObject, PropertyObject newObject, CancellationToken cancellationToken)
        {
            CertificateBundle         cb      = (CertificateBundle)originalObject;
            PropertyObjectCertificate certNew = (PropertyObjectCertificate)newObject;
            await Session.CurrentVault.UpdateCertificatePolicyAsync(certNew.Name, certNew.CertificatePolicy, cancellationToken);

            cb = await Session.CurrentVault.UpdateCertificateAsync(certNew.Name, null, null, certNew.ToCertificateAttributes(), certNew.ToTagsDictionary(), cancellationToken);

            return(new ListViewItemCertificate(Session, cb));
        }
        public static async Task <ListViewItemCertificate> NewAsync(ISession session, PropertyObject newObject, CancellationToken cancellationToken)
        {
            PropertyObjectCertificate certNew = (PropertyObjectCertificate)newObject;
            var certCollection = new X509Certificate2Collection();

            certCollection.Add(certNew.Certificate);
            CertificateBundle cb = await session.CurrentVault.ImportCertificateAsync(certNew.Name, certCollection, certNew.CertificatePolicy, certNew.CertificateBundle.Attributes, certNew.ToTagsDictionary(), cancellationToken);

            return(new ListViewItemCertificate(session, cb));
        }
Exemplo n.º 11
0
 public abstract Task <ListViewItemBase> UpdateAsync(object originalObject, PropertyObject newObject, CancellationToken cancellationToken);
        private static async Task <ListViewItemSecret> NewOrUpdateAsync(ISession session, object originalObject, PropertyObject newObject, CancellationToken cancellationToken)
        {
            SecretBundle         sOriginal = (SecretBundle)originalObject;
            PropertyObjectSecret posNew    = (PropertyObjectSecret)newObject;
            SecretBundle         s         = null;

            // New secret, secret rename or new value
            if ((sOriginal == null) || (sOriginal.SecretIdentifier.Name != posNew.Name) || (sOriginal.Value != posNew.RawValue))
            {
                s = await session.CurrentVault.SetSecretAsync(posNew.Name, posNew.RawValue, posNew.ToTagsDictionary(), ContentTypeEnumConverter.GetDescription(posNew.ContentType), posNew.ToSecretAttributes(), cancellationToken);
            }
            else // Same secret name and value
            {
                s = await session.CurrentVault.UpdateSecretAsync(posNew.Name, null, posNew.ToTagsDictionary(), ContentTypeEnumConverter.GetDescription(posNew.ContentType), posNew.ToSecretAttributes(), cancellationToken);
            }
            string oldSecretName = sOriginal?.SecretIdentifier.Name;

            if ((oldSecretName != null) && (oldSecretName != posNew.Name)) // Delete old secret
            {
                await session.CurrentVault.DeleteSecretAsync(oldSecretName, cancellationToken);
            }
            return(new ListViewItemSecret(session, s));
        }
 public static Task <ListViewItemSecret> NewAsync(ISession session, PropertyObject newObject, CancellationToken cancellationToken)
 {
     return(NewOrUpdateAsync(session, null, newObject, cancellationToken));
 }
 public override async Task <ListViewItemBase> UpdateAsync(object originalObject, PropertyObject newObject, CancellationToken cancellationToken)
 {
     return(await NewOrUpdateAsync(Session, originalObject, newObject, cancellationToken));
 }