def set_user(iam: BaseClient, account_id: str, user: str): """Summary Args: account_id (str): Description user (str): Description """ key_path = (f"credentials/{os.environ['ENV']}/users/" f"{user}/aws/{account_id}.csv") if not os.path.exists(key_path): try: iam.get_user(f'{user}-sftp') print(f"User {user}-sftp already exists !") except iam.meta.client.exceptions.NoSuchEntityException: print(f'Creating user : '******'arn:aws:iam::{account_id}:user/{user}-sftp', UserName=f'{user}-sftp', PermissionsBoundary= f"arn:aws:iam::{account_id}:policy/sftp-users") print(f"Created user : {user}") print(f'Creating access key for user : '******'{user}-sftp !') created_key = iam.create_access_key(UserName=f"{user}-sftp") key_directory = os.path.join(*key_path.split("/")[:-1]) if not os.path.exists(key_directory): os.mkdir(key_directory) with open(key_path, 'w') as file: writer = csv.writer(file) for row in [["Access key ID", "Secret access key"], [ created_key['AccessKey']['AccessKeyId'], created_key['AccessKey']['SecretAccessKey'] ]]: writer.writerow(row) print(f'Done creating key at path {key_path}') else: print(f"Key already found for project {account_id} and user {user}")
def create_access_key(username: str, iam: BaseClient) -> Tuple[str, str]: option = {"UserName": username} resp = iam.create_access_key(**option) return resp["AccessKey"]["AccessKeyId"], resp["AccessKey"][ "SecretAccessKey"]