Beispiel #1
0
        public void ResourceGroupDeployment(string resourceGroupName, string templateFile, string?templateParameterFile, object?args)
        {
            IEnumerable <FormattableString> command = new FormattableString[] { $"az deployment group create --resource-group {resourceGroupName}" };

            command = command.Concat(SetupArgs(templateFile, templateParameterFile, args));
            shellRunner.RunProcessVoid(command, invalidExitCodeMessage: $"Error on resource group deploy for arm template '{templateFile}' to resource group '{resourceGroupName}'.");
        }
        public async Task <String> LoadPublicKey()
        {
            var publicKeyName = PublicKeySecretName;
            var publicKey     = UnpackKey(await keyVaultManager.GetSecret(config.InfraKeyVaultName, publicKeyName));

            if (publicKey == null)
            {
                logger.LogInformation("Need to create ssh keypair. Please press enter to the prompts below.");
                var outFile    = Path.GetTempFileName();
                var outPubFile = $"{outFile}.pub";
                File.Delete(outFile);
                try
                {
                    //Can't get -N '' to work so just send 2 newlines when making this
                    var newlines = $"{Environment.NewLine}{Environment.NewLine}";
                    shellRunner.RunProcessVoid($"cat {newlines} | ssh-keygen -t rsa -b 4096 -o -a 100 -f {outFile}",
                                               invalidExitCodeMessage: $"Error creating keys with ssh-keygen.");

                    var privateKey = File.ReadAllText(outFile);
                    //Clean up newlines in private key, this should work on any os
                    privateKey = PackKey(privateKey);
                    await keyVaultManager.SetSecret(config.InfraKeyVaultName, PrivateKeySecretName, privateKey);

                    //Set public key last since this is what satisfies the condition above, don't want to be in a half state.
                    publicKey = File.ReadAllText(outPubFile);
                    publicKey = PackKey(publicKey); //Pack the key to store it
                    if (publicKey.EndsWith(LFPlaceholder))
                    {
                        publicKey = publicKey.Substring(0, publicKey.Length - LFPlaceholder.Length);
                    }
                    await keyVaultManager.SetSecret(config.InfraKeyVaultName, publicKeyName, publicKey);

                    publicKey = UnpackKey(publicKey); //Unpack the key again to return it
                }
                finally
                {
                    if (File.Exists(outFile))
                    {
                        try
                        {
                            File.Delete(outFile);
                        }
                        catch (Exception ex)
                        {
                            logger.LogError(ex, $"{ex.GetType().Name} erasing file {outFile}. Please ensure this file is erased manually.");
                        }
                    }
                    if (File.Exists(outPubFile))
                    {
                        try
                        {
                            File.Delete(outPubFile);
                        }
                        catch (Exception ex)
                        {
                            logger.LogError(ex, $"{ex.GetType().Name} erasing file {outFile}. Please ensure this file is erased manually.");
                        }
                    }

                    //throw new InvalidOperationException($"You must create a key pair with \"ssh-keygen -t rsa -b 4096 -o -a 100 -f newazurevm\" and save it as '{publicKeyName}' and '{PrivateKeySecretName}' in the '{config.InfraKeyVaultName}' key vault. Also replace all the newlines with '**lf**'. This is needed to preserve them when they are reloaded. Then run this program again. There is no automation for this step at this time.");
                }

                logger.LogInformation("Finished creating new ssh key. Continuing.");
            }

            return(publicKey);
        }
 public void SetSubscription(String nameOrId)
 {
     powershellCoreRunner.RunProcessVoid($"az account set --subscription {nameOrId}", $"Error setting current subscription to '{nameOrId}'.");
 }