Exemple #1
0
        static void Main(string[] args)
        {
            KeyBundle            keyBundle            = null; // The key specification and attributes
            SecretBundle         secret               = null;
            CertificateBundle    certificateBundle    = null;
            CertificateOperation certificateOperation = null;
            string keyName               = string.Empty;
            string secretName            = string.Empty;
            string certificateName       = string.Empty;
            string certificateCreateName = string.Empty;

            inputValidator = new InputValidator(args);

            ServiceClientTracing.AddTracingInterceptor(new ConsoleTracingInterceptor());
            ServiceClientTracing.IsEnabled = inputValidator.GetTracingEnabled();

            var clientId             = ConfigurationManager.AppSettings["AuthClientId"];
            var cerificateThumbprint = ConfigurationManager.AppSettings["AuthCertThumbprint"];

            var certificate   = FindCertificateByThumbprint(cerificateThumbprint);
            var assertionCert = new ClientAssertionCertificate(clientId, certificate);

            keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(
                                                    (authority, resource, scope) => GetAccessToken(authority, resource, scope, assertionCert)),
                                                new InjectHostHeaderHttpMessageHandler());

            // SECURITY: DO NOT USE IN PRODUCTION CODE; FOR TEST PURPOSES ONLY
            //ServicePointManager.ServerCertificateValidationCallback += ( sender, cert, chain, sslPolicyErrors ) => true;

            List <KeyOperationType> successfulOperations = new List <KeyOperationType>();
            List <KeyOperationType> failedOperations     = new List <KeyOperationType>();

            foreach (var operation in inputValidator.GetKeyOperations())
            {
                try
                {
                    Console.Out.WriteLine("\n\n {0} is in process ...", operation);
                    switch (operation)
                    {
                    case KeyOperationType.CREATE_KEY:
                        keyBundle = CreateKey(keyBundle, out keyName);
                        break;

                    case KeyOperationType.IMPORT_KEY:
                        keyBundle = ImportKey(out keyName);
                        break;

                    case KeyOperationType.GET_KEY:
                        keyBundle = GetKey(keyBundle);
                        break;

                    case KeyOperationType.LIST_KEYVERSIONS:
                        ListKeyVersions(keyName);
                        break;

                    case KeyOperationType.UPDATE_KEY:
                        keyBundle = UpdateKey(keyName);
                        break;

                    case KeyOperationType.DELETE_KEY:
                        DeleteKey(keyName);
                        break;

                    case KeyOperationType.BACKUP_RESTORE:
                        keyBundle = BackupRestoreKey(keyName);
                        break;

                    case KeyOperationType.SIGN_VERIFY:
                        SignVerify(keyBundle);
                        break;

                    case KeyOperationType.ENCRYPT_DECRYPT:
                        EncryptDecrypt(keyBundle);
                        break;

                    case KeyOperationType.ENCRYPT:
                        Encrypt(keyBundle);
                        break;

                    case KeyOperationType.DECRYPT:
                        Decrypt(keyBundle);
                        break;

                    case KeyOperationType.WRAP_UNWRAP:
                        WrapUnwrap(keyBundle);
                        break;

                    case KeyOperationType.CREATE_SECRET:
                        secret = CreateSecret(out secretName);
                        break;

                    case KeyOperationType.GET_SECRET:
                        secret = GetSecret(secret.Id);
                        break;

                    case KeyOperationType.LIST_SECRETS:
                        ListSecrets();
                        break;

                    case KeyOperationType.DELETE_SECRET:
                        secret = DeleteSecret(secretName);
                        break;

                    case KeyOperationType.CREATE_CERTIFICATE:
                        certificateOperation = CreateCertificate(out certificateCreateName);
                        break;

                    case KeyOperationType.IMPORT_CERTIFICATE:
                        certificateBundle = ImportCertificate(out certificateName);
                        break;

                    case KeyOperationType.EXPORT_CERTIFICATE:
                        var x509Certificate = ExportCertificate(certificateBundle);
                        break;

                    case KeyOperationType.LIST_CERTIFICATEVERSIONS:
                        ListCertificateVersions(certificateName);
                        break;

                    case KeyOperationType.LIST_CERTIFICATES:
                        ListCertificates();
                        break;

                    case KeyOperationType.DELETE_CERTIFICATE:
                        certificateBundle = DeleteCertificate(certificateName);
                        certificateBundle = DeleteCertificate(certificateCreateName);
                        break;
                    }
                    successfulOperations.Add(operation);
                }
                catch (KeyVaultErrorException exception)
                {
                    // The Key Vault exceptions are logged but not thrown to avoid blocking execution for other commands running in batch
                    Console.Out.WriteLine("Operation failed: {0}", exception.Body.Error.Message);
                    failedOperations.Add(operation);
                }
            }

            Console.Out.WriteLine("\n\n---------------Successful Key Vault operations:---------------");
            foreach (KeyOperationType type in successfulOperations)
            {
                Console.Out.WriteLine("\t{0}", type);
            }

            if (failedOperations.Count > 0)
            {
                Console.Out.WriteLine("\n\n---------------Failed Key Vault operations:---------------");
                foreach (KeyOperationType type in failedOperations)
                {
                    Console.Out.WriteLine("\t{0}", type);
                }
            }

            Console.Out.WriteLine();
            Console.Out.Write("Press enter to continue . . .");
            Console.In.Read();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            KeyBundle keyBundle  = null; // The key specification and attributes
            Secret    secret     = null;
            string    keyName    = string.Empty;
            string    secretName = string.Empty;

            inputValidator = new InputValidator(args);

            TracingAdapter.AddTracingInterceptor(new ConsoleTracingInterceptor());
            TracingAdapter.IsEnabled = inputValidator.GetTracingEnabled();

            var clientId     = ConfigurationManager.AppSettings["AuthClientId"];
            var clientSecret = ConfigurationManager.AppSettings["AuthClientSecret"];

            clientCredential = new ClientCredential(clientId, clientSecret);

            keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetAccessToken), GetHttpClient());

            // SECURITY: DO NOT USE IN PRODUCTION CODE; FOR TEST PURPOSES ONLY
            //ServicePointManager.ServerCertificateValidationCallback += ( sender, cert, chain, sslPolicyErrors ) => true;

            List <KeyOperationType> successfulOperations = new List <KeyOperationType>();
            List <KeyOperationType> failedOperations     = new List <KeyOperationType>();

            foreach (var operation in inputValidator.GetKeyOperations())
            {
                try
                {
                    Console.Out.WriteLine(string.Format("\n\n {0} is in process ...", operation.ToString()));
                    switch (operation)
                    {
                    case KeyOperationType.CREATE_KEY:
                        keyBundle = CreateKey(keyBundle, out keyName);
                        break;

                    case KeyOperationType.IMPORT_KEY:
                        keyBundle = ImportKey(out keyName);
                        break;

                    case KeyOperationType.GET_KEY:
                        keyBundle = GetKey(keyBundle);
                        break;

                    case KeyOperationType.LIST_KEYVERSIONS:
                        ListKeyVersions(keyName);
                        break;

                    case KeyOperationType.UPDATE_KEY:
                        keyBundle = UpdateKey(keyName);
                        break;

                    case KeyOperationType.DELETE_KEY:
                        DeleteKey(keyName);
                        break;

                    case KeyOperationType.BACKUP_RESTORE:
                        keyBundle = BackupRestoreKey(keyName);
                        break;

                    case KeyOperationType.SIGN_VERIFY:
                        SignVerify(keyBundle);
                        break;

                    case KeyOperationType.ENCRYPT_DECRYPT:
                        EncryptDecrypt(keyBundle);
                        break;

                    case KeyOperationType.WRAP_UNWRAP:
                        WrapUnwrap(keyBundle);
                        break;

                    case KeyOperationType.CREATE_SECRET:
                        secret = CreateSecret(out secretName);
                        break;

                    case KeyOperationType.GET_SECRET:
                        secret = GetSecret(secret.Id);
                        break;

                    case KeyOperationType.LIST_SECRETS:
                        ListSecrets();
                        break;

                    case KeyOperationType.DELETE_SECRET:
                        secret = DeleteSecret(secretName);
                        break;
                    }
                    successfulOperations.Add(operation);
                }
                catch (KeyVaultClientException exception)
                {
                    // The Key Vault exceptions are logged but not thrown to avoid blocking execution for other commands running in batch
                    Console.Out.WriteLine("Operation failed: {0}", exception.Message);
                    failedOperations.Add(operation);
                }
            }

            Console.Out.WriteLine("\n\n---------------Successful Key Vault operations:---------------");
            foreach (KeyOperationType type in successfulOperations)
            {
                Console.Out.WriteLine("\t{0}", type);
            }

            if (failedOperations.Count > 0)
            {
                Console.Out.WriteLine("\n\n---------------Failed Key Vault operations:---------------");
                foreach (KeyOperationType type in failedOperations)
                {
                    Console.Out.WriteLine("\t{0}", type);
                }
            }

            Console.Out.WriteLine();
            Console.Out.Write("Press enter to continue . . .");
            Console.In.Read();
        }