예제 #1
0
        /// <summary>
        /// Process input
        /// </summary>
        protected override void ProcessRecord()
        {
            if (!ShouldProcess(Name, VerbsLifecycle.Unregister))
            {
                return;
            }

            string vaultName;

            switch (ParameterSetName)
            {
            case NameParameterSet:
                vaultName = Name;
                break;

            case SecretVaultParameterSet:
                vaultName = SecretVault.Name;
                break;

            default:
                Dbg.Assert(false, "Invalid parameter set");
                vaultName = string.Empty;
                break;
            }

            if (vaultName.Equals(RegisterSecretVaultCommand.BuiltInLocalVault, StringComparison.OrdinalIgnoreCase))
            {
                var msg = string.Format(CultureInfo.InvariantCulture,
                                        "The {0} vault cannot be removed.",
                                        RegisterSecretVaultCommand.BuiltInLocalVault);

                WriteError(
                    new ErrorRecord(
                        new PSArgumentException(msg),
                        "RegisterSecretVaultInvalidVaultName",
                        ErrorCategory.InvalidArgument,
                        this));

                return;
            }

            var removedVaultInfo = RegisteredVaultCache.Remove(vaultName);

            if (removedVaultInfo == null)
            {
                var msg = string.Format(CultureInfo.InvariantCulture,
                                        "Unable to find secret vault {0} to unregister it.", vaultName);
                WriteError(
                    new ErrorRecord(
                        new ItemNotFoundException(msg),
                        "UnregisterSecretVaultObjectNotFound",
                        ErrorCategory.ObjectNotFound,
                        this));

                return;
            }

            // Remove any parameter secret from built-in local store.
            RemoveParamSecrets(removedVaultInfo, ExtensionVaultModule.VaultParametersStr);
        }
 protected override void EndProcessing()
 {
     try
     {
         RegisteredVaultCache.SetDefaultVault(Name);
     }
     catch (Exception ex)
     {
         ThrowTerminatingError(
             new ErrorRecord(
                 exception: ex,
                 errorId: "VaultNotFound",
                 errorCategory: ErrorCategory.ObjectNotFound,
                 this));
     }
 }
        /// <summary>
        /// Process input
        /// </summary>
        protected override void ProcessRecord()
        {
            if (!ShouldProcess(Name, VerbsLifecycle.Unregister))
            {
                return;
            }

            string vaultName;

            switch (ParameterSetName)
            {
            case NameParameterSet:
                vaultName = Name;
                break;

            case SecretVaultParameterSet:
                vaultName = SecretVault.VaultName;
                break;

            default:
                Dbg.Assert(false, "Invalid parameter set");
                vaultName = string.Empty;
                break;
            }

            var removedVaultInfo = RegisteredVaultCache.Remove(vaultName);

            if (removedVaultInfo == null)
            {
                var msg = string.Format(CultureInfo.InvariantCulture,
                                        "Unable to find secret vault {0} to unregister it.", vaultName);
                WriteError(
                    new ErrorRecord(
                        new ItemNotFoundException(msg),
                        "UnregisterSecretVaultObjectNotFound",
                        ErrorCategory.ObjectNotFound,
                        this));

                return;
            }
        }
예제 #4
0
        protected override void EndProcessing()
        {
            if (!ShouldProcess(Name, "Set vault as default"))
            {
                return;
            }

            string vaultName;

            switch (ParameterSetName)
            {
            case NameParameterSet:
                vaultName = Name;
                break;

            case SecretVaultParameterSet:
                vaultName = SecretVault.Name;
                break;

            default:
                Dbg.Assert(false, "Invalid parameter set");
                vaultName = string.Empty;
                break;
            }

            try
            {
                RegisteredVaultCache.SetDefaultVault(vaultName);
            }
            catch (Exception ex)
            {
                ThrowTerminatingError(
                    new ErrorRecord(
                        exception: ex,
                        errorId: "VaultNotFound",
                        errorCategory: ErrorCategory.ObjectNotFound,
                        this));
            }
        }
        protected override void EndProcessing()
        {
            var vaultInfo = new Hashtable();

            // Validate mandatory parameters.
            var vaultItems = RegisteredVaultCache.GetAll();

            if (vaultItems.ContainsKey(Name))
            {
                ThrowTerminatingError(
                    new ErrorRecord(
                        new InvalidOperationException("Provided Name for vault is already being used."),
                        "RegisterSecretVaultInvalidVaultName",
                        ErrorCategory.InvalidArgument,
                        this));
            }

            if (!ShouldProcess(Name, VerbsLifecycle.Register))
            {
                return;
            }

            // Resolve the module name path in calling context, if it is a path and not a name.
            var results = InvokeCommand.InvokeScript(
                script: "param([string] $path) (Resolve-Path -Path $path -EA Silent).Path",
                args: new object[] { ModuleName });
            string resolvedPath     = (results.Count == 1 && results[0] != null) ? (string)results[0].BaseObject : null;
            string moduleNameOrPath = resolvedPath ?? ModuleName;

            results = InvokeCommand.InvokeScript(
                script: "(Get-Module -Name Microsoft.PowerShell.SecretManagement).ModuleBase");
            string secretMgtModulePath = (results.Count == 1 && results[0] != null) ? (string)results[0].BaseObject : string.Empty;

            secretMgtModulePath = System.IO.Path.Combine(secretMgtModulePath, "Microsoft.PowerShell.SecretManagement.psd1");

            var moduleInfo = GetModuleInfo(
                modulePath: moduleNameOrPath,
                secretMgtModulePath: secretMgtModulePath,
                error: out ErrorRecord moduleLoadError);

            if (moduleInfo == null)
            {
                var msg = string.Format(CultureInfo.InvariantCulture,
                                        "Could not load and retrieve module information for module: {0} with error : {1}.",
                                        ModuleName, moduleLoadError?.ToString() ?? string.Empty);

                ThrowTerminatingError(
                    new ErrorRecord(
                        new PSInvalidOperationException(msg),
                        "RegisterSecretVaultCantGetModuleInfo",
                        ErrorCategory.InvalidOperation,
                        this));
            }

            if (!CheckForImplementingModule(
                    dirPath: moduleInfo.ModuleBase,
                    moduleName: moduleInfo.Name,
                    secretMgtModulePath: secretMgtModulePath,
                    error: out Exception error))
            {
                var invalidException = new PSInvalidOperationException(
                    message: "Could not find a SecretManagement extension implementing script module.",
                    innerException: error);

                ThrowTerminatingError(
                    new ErrorRecord(
                        invalidException,
                        "RegisterSecretVaultCantFindImplementingScriptModule",
                        ErrorCategory.ObjectNotFound,
                        this));
            }

            // Find base path of module without version folder, to store in vault registry.
            string dirPath;

            if (System.IO.Path.GetFileName(moduleInfo.ModuleBase).Equals(moduleInfo.Name, StringComparison.OrdinalIgnoreCase))
            {
                dirPath = moduleInfo.ModuleBase;
            }
            else
            {
                var parent = System.IO.Directory.GetParent(moduleInfo.ModuleBase);
                while (parent != null && !parent.Name.Equals(moduleInfo.Name, StringComparison.OrdinalIgnoreCase))
                {
                    parent = parent.Parent;
                }
                dirPath = parent?.FullName ?? moduleInfo.ModuleBase;
            }

            // Store module information.
            vaultInfo.Add(
                key: ExtensionVaultModule.ModulePathStr,
                value: dirPath);
            vaultInfo.Add(
                key: ExtensionVaultModule.ModuleNameStr,
                value: moduleInfo.Name);

            // Store optional vault parameters.
            vaultInfo.Add(
                key: ExtensionVaultModule.VaultParametersStr,
                value: VaultParameters);

            // Register new secret vault information.
            RegisteredVaultCache.Add(
                keyName: Name,
                vaultInfo: vaultInfo,
                defaultVault: DefaultVault);
        }
예제 #6
0
        protected override void EndProcessing()
        {
            var vaultInfo = new Hashtable();

            // Validate mandatory parameters.
            var vaultItems = RegisteredVaultCache.GetAll();

            if (vaultItems.ContainsKey(Name))
            {
                ThrowTerminatingError(
                    new ErrorRecord(
                        new InvalidOperationException("Provided Name for vault is already being used."),
                        "RegisterSecretVaultInvalidVaultName",
                        ErrorCategory.InvalidArgument,
                        this));
            }

            if (!ShouldProcess(Name, VerbsLifecycle.Register))
            {
                return;
            }

            var moduleInfo = GetModuleInfo(ModuleName);

            if (moduleInfo == null)
            {
                var msg = string.Format(CultureInfo.InvariantCulture,
                                        "Could not load and retrieve module information for module: {0}.",
                                        ModuleName);

                ThrowTerminatingError(
                    new ErrorRecord(
                        new PSInvalidOperationException(msg),
                        "RegisterSecretVaultCantGetModuleInfo",
                        ErrorCategory.InvalidOperation,
                        this));
            }

            var modulePath = moduleInfo.Path;
            var dirPath    = System.IO.File.Exists(modulePath) ? System.IO.Path.GetDirectoryName(modulePath) : modulePath;

            // Check module required modules for implementing type of SecretManagementExtension class.
            Type implementingType = GetImplementingTypeFromRequiredAssemblies(moduleInfo);

            // Check if module supports implementing functions.
            var haveScriptFunctionImplementation = CheckForImplementingModule(
                dirPath: dirPath,
                error: out Exception error);

            if (implementingType == null && !haveScriptFunctionImplementation)
            {
                var invalidException = new PSInvalidOperationException(
                    message: "Could not find a SecretManagementExtension implementing type, or a valid implementing script module.",
                    innerException: error);

                ThrowTerminatingError(
                    new ErrorRecord(
                        invalidException,
                        "RegisterSecretVaultCantFindImplementingTypeOrScriptModule",
                        ErrorCategory.ObjectNotFound,
                        this));
            }

            vaultInfo.Add(ExtensionVaultModule.ModulePathStr, dirPath);
            vaultInfo.Add(ExtensionVaultModule.ModuleNameStr, moduleInfo.Name);

            vaultInfo.Add(
                key: ExtensionVaultModule.ImplementingTypeStr,
                value: new Hashtable()
            {
                { "AssemblyName", implementingType != null ? implementingType.Assembly.GetName().Name : string.Empty },
                { "TypeName", implementingType != null ? implementingType.FullName: string.Empty }
            });

            vaultInfo.Add(
                key: ExtensionVaultModule.ImplementingFunctionsStr,
                value: haveScriptFunctionImplementation);

            // Store the optional secret parameters
            StoreVaultParameters(
                vaultInfo: vaultInfo,
                vaultName: Name,
                parameters: VaultParameters);

            // Register new secret vault information.
            RegisteredVaultCache.Add(
                keyName: Name,
                vaultInfo: vaultInfo);
        }