Пример #1
0
        protected override void ProcessRecord()
        {
            using (var vlt = Util.VaultHelper.GetVault(VaultProfile))
            {
                vlt.OpenStorage();
                var v = vlt.LoadVault();

                if (v.Certificates == null || v.Certificates.Count < 1)
                {
                    throw new InvalidOperationException("No certificates found");
                }

                var ci = v.Certificates.GetByRef(CertificateRef, throwOnMissing: false);
                if (ci == null)
                {
                    throw new Exception("Unable to find a Certificate for the given reference");
                }

                IssuerCertificateInfo ici = null;
                if (!string.IsNullOrEmpty(ci.IssuerSerialNumber))
                {
                    v.IssuerCertificates.TryGetValue(ci.IssuerSerialNumber, out ici);
                }

                PrivateKey pk     = null;
                Crt        crt    = null;
                Crt        issCrt = null;

                var keyAsset    = vlt.GetAsset(Vault.VaultAssetType.KeyPem, ci.KeyPemFile);
                var crtAsset    = vlt.GetAsset(Vault.VaultAssetType.CrtPem, ci.CrtPemFile);
                var issCrtAsset = ici != null
                        ? vlt.GetAsset(Vault.VaultAssetType.IssuerPem, ici.CrtPemFile)
                        : null;


                // Resolve details from inline or profile attributes
                string installerName = null;
                IReadOnlyDictionary <string, object> installerParams    = null;
                IReadOnlyDictionary <string, object> cliInstallerParams = null;

                if (InstallerParameters?.Count > 0)
                {
                    cliInstallerParams = (IReadOnlyDictionary <string, object>
                                          )PoshHelper.Convert <string, object>(InstallerParameters);
                }

                if (!string.IsNullOrEmpty(InstallerProfileRef))
                {
                    var ppi = v.InstallerProfiles.GetByRef(InstallerProfileRef, throwOnMissing: false);
                    if (ppi == null)
                    {
                        throw new ItemNotFoundException("no Installer profile found for the given reference")
                              .With(nameof(InstallerProfileRef), InstallerProfileRef);
                    }

                    var ppAsset = vlt.GetAsset(Vault.VaultAssetType.InstallerConfigInfo,
                                               ppi.Id.ToString());
                    InstallerProfile ip;
                    using (var s = vlt.LoadAsset(ppAsset))
                    {
                        ip = JsonHelper.Load <InstallerProfile>(s);
                    }

                    installerName   = ip.InstallerProvider;
                    installerParams = ip.InstanceParameters;
                    if (cliInstallerParams != null)
                    {
                        WriteVerbose("Override Installer parameters specified");
                        if (installerParams?.Count == 0)
                        {
                            WriteVerbose("Profile does not define any parameters, using override parameters only");
                            installerParams = cliInstallerParams;
                        }
                        else
                        {
                            WriteVerbose("Merging Installer override parameters with profile");
                            var mergedParams = new Dictionary <string, object>();

                            foreach (var kv in ip.InstanceParameters)
                            {
                                mergedParams[kv.Key] = kv.Value;
                            }
                            foreach (var kv in cliInstallerParams)
                            {
                                mergedParams[kv.Key] = kv.Value;
                            }

                            installerParams = mergedParams;
                        }
                    }
                }
                else
                {
                    installerName   = Installer;
                    installerParams = cliInstallerParams;
                }


                using (var pki = PkiHelper.GetPkiTool(v.PkiTool))
                {
                    // Load the Private Key
                    // TODO:  This is UGLY, but it works for now!
                    using (var s = vlt.LoadAsset(keyAsset))
                    {
                        try
                        {
                            pk = pki.ImportPrivateKey <RsaPrivateKey>(EncodingFormat.PEM, s);
                        }
                        catch { }
                    }
                    if (pk == null)
                    {
                        using (var s = vlt.LoadAsset(keyAsset))
                        {
                            try
                            {
                                pk = pki.ImportPrivateKey <EcKeyPair>(EncodingFormat.PEM, s);
                            }
                            catch { }
                        }
                    }
                    if (pk == null)
                    {
                        throw new NotSupportedException("unknown or unsupported private key format");
                    }

                    // Load the Certificate
                    using (var s = vlt.LoadAsset(crtAsset))
                    {
                        crt = pki.ImportCertificate(EncodingFormat.PEM, s);
                    }

                    // Load the Issuer Certificate
                    if (issCrtAsset != null)
                    {
                        using (var s = vlt.LoadAsset(issCrtAsset))
                        {
                            issCrt = pki.ImportCertificate(EncodingFormat.PEM, s);
                        }
                    }

                    // Finally, instantiate and invoke the installer
                    var installerProvider = InstallerExtManager.GetProvider(installerName);
                    using (var installer = installerProvider.GetInstaller(installerParams))
                    {
                        var chain = new Crt[0];
                        if (issCrt != null)
                        {
                            chain = new[] { issCrt }
                        }
                        ;
                        installer.Install(pk, crt, chain, pki);
                    }
                }


                //try
                //{
                //}
                //catch (AcmeClient.AcmeWebException ex)
                //{
                //    ThrowTerminatingError(PoshHelper.CreateErrorRecord(ex, ci));
                //    return;
                //}
            }
        }
    }
Пример #2
0
        protected override void ProcessRecord()
        {
            // We have to invoke this here because we *may not* invoke
            // any Vault access but we do rely on Ext mechanism access.
            Util.PoshHelper.BeforeExtAccess();

            if (ReloadProviders)
            {
                InstallerExtManager.Reload();
            }
            else if (!string.IsNullOrEmpty(GetInstaller))
            {
                WriteVerbose("Getting details of Installer");
                var pInfo = InstallerExtManager.GetProviderInfos()
                            .FirstOrDefault(_ => _.Name == GetInstaller);
                var p = InstallerExtManager.GetProvider(GetInstaller);
                if (ParametersOnly)
                {
                    WriteVerbose("Showing parameter details only");
                    WriteObject(p.DescribeParameters().Select(_ => new {
                        _.Name,
                        _.Label,
                        _.Type,
                        _.IsRequired,
                        _.IsMultiValued,
                        _.Description,
                    }), true);
                }
                else
                {
                    WriteObject(new
                    {
                        pInfo.Name,
                        pInfo.Info.Label,
                        pInfo.Info.IsUninstallSupported,
                        pInfo.Info.Description,
                        Parameters = p.DescribeParameters().Select(_ => new {
                            _.Name,
                            _.Label,
                            _.Type,
                            _.IsRequired,
                            _.IsMultiValued,
                            _.Description,
                        }),
                    });
                }
            }
            else if (ListInstallers)
            {
                WriteVerbose("Listing all Installers");
                WriteObject(InstallerExtManager.GetProviderInfos().Select(_ => _.Name), true);
            }
            else
            {
                WriteVerbose("Getting details of preconfigured Installer Profile");
                using (var vlt = Util.VaultHelper.GetVault(VaultProfile))
                {
                    vlt.OpenStorage();
                    var v = vlt.LoadVault();

                    if (ListProfiles)
                    {
                        WriteObject(v.InstallerProfiles?.Values, true);
                    }
                    else
                    {
                        var ipi = v.InstallerProfiles?.GetByRef(ProfileRef, throwOnMissing: false);
                        if (ipi == null)
                        {
                            WriteObject(ipi);
                        }
                        else
                        {
                            var asset = vlt.GetAsset(Vault.VaultAssetType.InstallerConfigInfo,
                                                     ipi.Id.ToString());
                            using (var s = vlt.LoadAsset(asset))
                            {
                                WriteObject(JsonHelper.Load <InstallerProfile>(s), false);
                            }
                        }
                    }
                }
            }
        }
Пример #3
0
        protected override void ProcessRecord()
        {
            using (var vlt = Util.VaultHelper.GetVault(VaultProfile))
            {
                vlt.OpenStorage();
                var v = vlt.LoadVault();

                if (v.InstallerProfiles == null)
                {
                    WriteVerbose("Initializing Installer Profile collection");
                    v.InstallerProfiles = new Vault.Util.EntityDictionary <Vault.Model.InstallerProfileInfo>();
                }

                WriteVerbose($"Searching for existing Installer Profile for reference [{ProfileName}]");
                var ipi = v.InstallerProfiles.GetByRef(ProfileName, throwOnMissing: false);
                if (ipi == null)
                {
                    WriteVerbose("No existing Profile found");
                }
                else
                {
                    WriteVerbose($"Existing Profile found [{ipi.Id}][{ipi.Alias}]");
                }

                if (!string.IsNullOrEmpty(Rename))
                {
                    if (ipi == null)
                    {
                        throw new KeyNotFoundException("no existing profile found that can be renamed");
                    }

                    v.InstallerProfiles.Rename(ProfileName, Rename);
                    ipi.Alias = Rename;
                }
                else if (Remove)
                {
                    WriteVerbose($"Removing named Installer Profile for name [{ProfileName}]");
                    if (ipi == null)
                    {
                        WriteVerbose("No Installer Profile found for given name");
                        return;
                    }
                    else
                    {
                        v.InstallerProfiles.Remove(ipi.Id);
                        WriteVerbose("Installer Profile removed");
                    }
                }
                else
                {
                    if (ipi != null)
                    {
                        if (!Force)
                        {
                            throw new InvalidOperationException("existing profile found;"
                                                                + " specify -Force to overwrite");
                        }

                        WriteVerbose("Removing existing Profile");
                        v.InstallerProfiles.Remove(ipi.Id);
                    }

                    if (InstallerExtManager.GetProviderInfo(Installer) == null)
                    {
                        throw new ArgumentException("Unknown or invalid Installer provider name")
                              .With(nameof(Installer), Installer);
                    }

                    WriteVerbose("Adding new Installer Profile Info");
                    ipi = new Vault.Model.InstallerProfileInfo
                    {
                        Id    = Guid.NewGuid(),
                        Alias = ProfileName,
                        Label = Label,
                        Memo  = Memo,
                    };
                    var pp = new InstallerProfile
                    {
                        InstallerProvider  = Installer,
                        InstanceParameters = (IReadOnlyDictionary <string, object>
                                              )InstallerParameters.Convert <string, object>(),
                    };

                    var asset = vlt.CreateAsset(Vault.VaultAssetType.InstallerConfigInfo, ipi.Id.ToString());
                    using (var s = vlt.SaveAsset(asset))
                    {
                        JsonHelper.Save(s, pp);
                    }
                    v.InstallerProfiles.Add(ipi);
                }

                vlt.SaveVault(v);
            }
        }