// Copyright Epic Games, Inc. All Rights Reserved.
namespace HordeServer.Utilities
{
///
/// Helper functions for interacting with Amazon Web Services (AWS)
///
public static class AwsHelper
{
///
/// Reads AWS credentials from 'credentials' file
///
/// The AWS SDK provided functionality was proven too much work, so this is a simplified version.
///
/// Name of the AWS profile
/// Override for the credentials file path, set to null for default path
/// Credentials as a tuple
///
public static (string AccessKey, string SecretAccessKey, string SecretToken) ReadAwsCredentials(string profileName, string? credentialsFilePath = null)
{
credentialsFilePath ??= Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".aws", "credentials");
string[] lines = File.ReadAllLines(credentialsFilePath);
static string ReadValue(string line, string expectedKey)
{
if (line.StartsWith(expectedKey, StringComparison.Ordinal))
{
return line.Split("=")[1].Trim();
}
throw new Exception($"Unable to read key/value on line {line} for key {expectedKey}");
}
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
if (line == $"[{profileName}]")
{
string accessKey = ReadValue(lines[i + 1], "aws_access_key_id");
string secretAccessKey = ReadValue(lines[i + 2], "aws_secret_access_key");
string sessionToken = ReadValue(lines[i + 3], "aws_session_token");
return (accessKey, secretAccessKey, sessionToken);
}
}
throw new Exception($"Unable to find profile {profileName} in file {credentialsFilePath}");
}
}
}