// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 // GameKit using AWS.GameKit.Runtime.Core; using AWS.GameKit.Runtime.Models; using AWS.GameKit.Runtime.Utils; namespace AWS.GameKit.Editor.Core { /// /// Provides read/write access for persisting the user's AWS credentials to the standardized ~/.aws/credentials file.

/// /// To learn more about this file, please see . ///
public class CredentialsManager { private string _gameName = string.Empty; private string _envCode = string.Empty; /// /// Combines the gamename, environment code and the 'GameKit' phase to create a profile name /// /// Profile name that will be used to save/find credentials in the user's AWS credentials ini file private string GetProfileName() { return $"GameKit-{_gameName}-{_envCode}"; } /// /// Sets the _gameName variable for the instance of the credentials manager /// /// The name of the game public void SetGameName(string name) { _gameName = name; } /// /// Sets the _envCode variable for the instance of the credentials manager /// /// A 2 to 3 character environment code public void SetEnv(string environmentCode) { _envCode = environmentCode; } /// /// Save a new profile to the AWS credentials file. /// /// The access key of the AWS IAM role we are saving. /// The secret key of the AWS IAM role we are saving. public void SaveCredentials(string accessKey, string secretKey) { uint result = CoreWrapper.Get().SaveAWSCredentials(GetProfileName(), accessKey, secretKey, Logging.LogCb); if (result != GameKitErrors.GAMEKIT_SUCCESS) { Logging.LogError($"CredentialsManager::SaveCredentials() was unable to save profile: {GetProfileName()} to aws credentials file. Error Code: {result}"); } } /// /// Checks the AWS profile exists. /// /// The game name we are checking an aws profile for. /// The environment code we are checking an aws profile for. public bool CheckAwsProfileExists(string gameName, string environmentCode) { SetGameName(gameName); SetEnv(environmentCode); return CoreWrapper.Get().AwsProfileExists(GetProfileName()); } /// /// Sets the AWS access key of an existing profile. /// /// /// If the profile retrieved with GetProfileName does not exist, will not automatically create the profile and will log an error. /// /// The new access key of the AWS IAM role we are saving. public void SetAccessKey(string accessKey) { uint result = CoreWrapper.Get().SetAWSAccessKey(GetProfileName(), accessKey, Logging.LogCb); if (result != GameKitErrors.GAMEKIT_SUCCESS) { Logging.LogError($"CredentialsManager::SetAccessKey() was unable to set access key for profile: {GetProfileName()}. Error Code: {result}"); } } /// /// Sets the AWS secret key of an existing profile. /// /// /// If the profile retrieved with GetProfileName does not exist, will not automatically create the profile and will log an error. /// /// The new secret key that will be assigned to this profile. public void SetSecretKey(string secretKey) { uint result = CoreWrapper.Get().SetAWSSecretKey(GetProfileName(), secretKey, Logging.LogCb); if (result != GameKitErrors.GAMEKIT_SUCCESS) { Logging.LogError($"CredentialsManager::SetSecretKey() was unable to set secret key for profile: {GetProfileName()}. Error Code: {result}"); } } /// /// Gets the access key corresponding to a pre-existing profile in the AWS credentials file. /// /// The access key found in the .aws/credentials file for the corresponding profile public string GetAccessKey() { // KeyValueStringCallbackResult is a struct containing two strings ResponseKey and ResponseValue and and error code, which can be parsed in GameKitErrors.cs. // In this method ResponseKey corresponds to the Access Key and ResponseValue corresponds to the Secret Key KeyValueStringCallbackResult result = CoreWrapper.Get().GetAWSProfile(GetProfileName(), Logging.LogCb); if (result.ResultCode != GameKitErrors.GAMEKIT_SUCCESS) { Logging.LogError($"CredentialsManager::GetAccessKey() was unable to get access key for profile: {GetProfileName()}. Error Code: {result}"); } return result.ResponseKey; } /// /// Gets the secret key corresponding to a pre-existing profile in the AWS credentials file. /// /// The secret key found in the .aws/credentials file for the corresponding profile public string GetSecretAccessKey() { // KeyValueStringCallbackResult is a struct containing two strings ResponseKey and ResponseValue and and error code, which can be parsed in GameKitErrors.cs. // In this method ResponseKey corresponds to the Access Key and ResponseValue corresponds to the Secret Key KeyValueStringCallbackResult result = CoreWrapper.Get().GetAWSProfile(GetProfileName(), Logging.LogCb); if (result.ResultCode != GameKitErrors.GAMEKIT_SUCCESS) { Logging.LogError($"CredentialsManager::GetSecretAccessKey() was unable to get secret key for profile: {GetProfileName()}. Error Code: {result}"); } return result.ResponseValue; } } }