Beispiel #1
0
        public async Task <object> Execute(ArgumentSyntax syntax)
        {
            var keyPath = syntax.GetParameter <string>(PrivateKeyOption, true);
            var pwd     = syntax.GetParameter <string>(PasswordParam, true);

            var(location, cert) = await DownloadCertificate(syntax);

            var pfxName = string.Format(CultureInfo.InvariantCulture, "[certes] {0:yyyyMMddhhmmss}", DateTime.UtcNow);
            var privKey = await syntax.ReadKey(PrivateKeyOption, "CERTES_CERT_KEY", File, environment, true);

            var pfx = cert.ToPfx(privKey).Build(pfxName, pwd);

            var outPath = syntax.GetOption <string>(OutOption);

            if (string.IsNullOrWhiteSpace(outPath))
            {
                return(new
                {
                    location,
                    pfx,
                });
            }
            else
            {
                logger.Debug("Saving certificate to '{0}'.", outPath);
                await File.WriteAllBytes(outPath, pfx);

                return(new
                {
                    location,
                });
            }
        }
Beispiel #2
0
        public async Task <object> Execute(ArgumentSyntax syntax)
        {
            var(serverUri, key) = await ReadAccountKey(syntax, true, false);

            var orderUri = syntax.GetParameter <Uri>(OrderIdParam, true);
            var domain   = syntax.GetParameter <string>(DomainParam, true);

            var azureCredentials = await ReadAzureCredentials(syntax);

            var resourceGroup = syntax.GetOption <string>(AzureResourceGroupOption, true);
            var appName       = syntax.GetParameter <string>(AppNameParam, true);
            var appSlot       = syntax.GetOption <string>(SlotOption, false);

            var privKey = await syntax.ReadKey(PrivateKeyOption, "CERTES_CERT_KEY", File, environment, true);

            var acme     = ContextFactory.Invoke(serverUri, key);
            var orderCtx = acme.Order(orderUri);

            var order = await orderCtx.Resource();

            if (order.Certificate == null)
            {
                throw new CertesCliException(string.Format(Strings.ErrorOrderIncompleted, orderCtx.Location));
            }

            var cert = await orderCtx.Download();

            var x509Cert   = new X509Certificate2(cert.Certificate.ToDer());
            var thumbprint = x509Cert.Thumbprint;

            using (var client = clientFactory.Invoke(azureCredentials))
            {
                client.SubscriptionId = azureCredentials.DefaultSubscriptionId;

                var certUploaded = await FindCertificate(client, resourceGroup, thumbprint);

                if (certUploaded == null)
                {
                    certUploaded = await UploadCertificate(
                        client, resourceGroup, appName, appSlot, cert.ToPfx(privKey), thumbprint);
                }

                var hostNameBinding = new HostNameBindingInner
                {
                    SslState   = SslState.SniEnabled,
                    Thumbprint = certUploaded.Thumbprint,
                };

                var hostName = string.IsNullOrWhiteSpace(appSlot) ?
                               await client.WebApps.CreateOrUpdateHostNameBindingAsync(
                    resourceGroup, appName, domain, hostNameBinding) :
                               await client.WebApps.CreateOrUpdateHostNameBindingSlotAsync(
                    resourceGroup, appName, domain, hostNameBinding, appSlot);

                return(new
                {
                    data = hostName
                });
            }
        }
        public async Task <object> Execute(ArgumentSyntax syntax)
        {
            var(serverUri, key) = await ReadAccountKey(syntax, true, false);

            var orderUri          = syntax.GetParameter <Uri>(OrderIdParam, true);
            var distinguishedName = syntax.GetOption <string>(DnOption);
            var outPath           = syntax.GetOption <string>(OutOption);
            var keyAlgorithmStr   = syntax.GetOption <string>(KeyAlgorithmOption);

            var keyAlgorithm =
                keyAlgorithmStr == null ? KeyAlgorithm.ES256 :
                Enum.TryParse <KeyAlgorithm>(keyAlgorithmStr, out var alg) ? alg :
                throw new ArgumentSyntaxException(string.Format(Strings.ErrorInvalidkeyAlgorithm, keyAlgorithmStr));


            var providedKey = await syntax.ReadKey(PrivateKeyOption, "CERTES_CERT_KEY", File, environment);

            var privKey = providedKey ?? KeyFactory.NewKey(keyAlgorithm);

            logger.Debug("Finalizing order from '{0}'.", serverUri);

            var acme     = ContextFactory.Invoke(serverUri, key);
            var orderCtx = acme.Order(orderUri);

            var csr = await orderCtx.CreateCsr(privKey);

            if (!string.IsNullOrWhiteSpace(distinguishedName))
            {
                csr.AddName(distinguishedName);
            }

            var order = await orderCtx.Finalize(csr.Generate());

            // output private key only if it is generated and not being saved
            if (string.IsNullOrWhiteSpace(outPath) && providedKey == null)
            {
                return(new
                {
                    location = orderCtx.Location,
                    privateKey = privKey.ToDer(),
                    resource = order,
                });
            }
            else
            {
                if (providedKey == null)
                {
                    await File.WriteAllText(outPath, privKey.ToPem());
                }

                return(new
                {
                    location = orderCtx.Location,
                    resource = order,
                });
            }
        }