public async Task SaveSshKnownHostsSecret()
        {
            var sshHost = await sshHostLookup.Value;
            var ip      = await machineIpManager.GetExternalIp();

            await vmManager.SetSecurityRuleAccess(config.NsgName, config.ResourceGroup, SshRuleName, "Allow", ip);

            String key;
            int    retry = 0;

            do
            {
                logger.LogInformation($"Trying key scan connection to '{sshHost}'. Retry '{retry}'.");
                var builder = shellRunner.CreateCommandBuilder();
                builder.AddCommand($"$key = ssh-keyscan -t rsa {sshHost}");
                builder.AddResultCommand($"$key | ConvertTo-Json -Depth 10");
                key = shellRunner.RunProcess <String>(builder);

                if (++retry > 100)
                {
                    throw new InvalidOperationException($"Retried ssh-keyscan '{retry}' times. Giving up.");
                }
            } while (String.IsNullOrEmpty(key));

            var existing = await keyVaultManager.GetSecret(config.InfraKeyVaultName, config.SshKnownHostKey);

            if (existing != null && existing != key)
            {
                logger.LogInformation($"Current saved server key (top) does not match current key on server (bottom). \n'{existing}'\n{key}");
                logger.LogInformation("If this is because the vm was recreated please enter y below. Otherwise this will be considered an error and the provisioning will stop.");

                if (!"y".Equals(Console.ReadLine(), StringComparison.InvariantCultureIgnoreCase))
                {
                    throw new InvalidOperationException("The ssh keys did not match and were rejected by the user. Key vault not updated.");
                }
            }

            await keyVaultManager.SetSecret(config.InfraKeyVaultName, config.SshKnownHostKey, key);
        }
Beispiel #2
0
        public string FindLatestImage(string image, string baseTag, string currentTag)
        {
            //Get the tags from docker
            var args      = $"";
            var searchTag = $"{image}:{currentTag}";
            var format    = "{{json .RepoTags}}";
            var tags      = shellRunner.RunProcess <List <String> >($"docker inspect --format={format} {searchTag}");

            //Remove any tags that weren't set by this software
            tags.Remove($"{image}:{currentTag}");
            var tagFilter = $"{image}:{baseTag}";

            tags = tags.Where(i => i.StartsWith(tagFilter)).ToList();
            tags.Sort(); //Docker seems to store these in order, but sort them by their names, the tags are date based and the latest will always be last

            var latestDateTag = tags.LastOrDefault();

            if (latestDateTag == null)
            {
                throw new InvalidOperationException($"Cannot find a tag in the format '{tagFilter}' on image '{image}'.");
            }

            return(latestDateTag);
        }
 public AccountShowOutput?Show()
 {
     return(powershellCoreRunner.RunProcess <AccountShowOutput>($"az account show", "Error getting account info."));
 }