Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.0 kB
41
Indexable
Never
import boto3
from botocore.exceptions import ClientError

# Define the policy document
policy_document = '''{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "bedrock:InvokeModel",
                "bedrock:InvokeModelWithResponseStream",
                "bedrock:GetModelInvocationLoggingConfiguration"
            ],
            "Resource": "*"
        }
    ]
}'''

# Function to create a user, attach policies, and create access keys
def create_user_and_keys(key, secret):
    try:
        # Create a session with the provided credentials
        session = boto3.Session(aws_access_key_id=key, aws_secret_access_key=secret)
        iam_client = session.client('iam')

        # Create a user
        user_name = 'devEC2'
        iam_client.create_user(UserName=user_name)

        # Attach the AdministratorAccess policy
        iam_client.attach_user_policy(
            UserName=user_name,
            PolicyArn='arn:aws:iam::aws:policy/AdministratorAccess'
        )

        # Create a custom policy
        policy_name = 'BedrockPolicy'
        iam_client.create_policy(
            PolicyName=policy_name,
            PolicyDocument=policy_document
        )

        # Attach the custom policy
        iam_client.attach_user_policy(
            UserName=user_name,
            PolicyArn=f'arn:aws:iam::{session.client("sts").get_caller_identity().get("Account")}:policy/{policy_name}'
        )

        # Create access keys for the user
        access_key_data = iam_client.create_access_key(UserName=user_name)
        access_key = access_key_data['AccessKey']['AccessKeyId']
        secret_key = access_key_data['AccessKey']['SecretAccessKey']

        # Write the new access keys to Bedrock_Keys.txt
        with open('Bedrock_Keys.txt', 'a') as file:
            file.write(f'{access_key}:{secret_key}\n')

        print(f'New access keys for {user_name} created and written to Bedrock_Keys.txt')

    except ClientError as e:
        print(f'Error creating user and keys: {e}')

# Function to read keys from a file
def read_keys_from_file(file_path):
    with open(file_path, 'r') as file:
        keys = []
        for line in file:
            # Split the line by both ":" and "|"
            parts = line.strip().replace('|', ':').split(':')
            if len(parts) >= 2:  # Ensure there are at least two parts (key and secret)
                keys.append((parts[0], parts[1]))  # Add the key and secret as a tuple
        return keys

# Main function to process the keys
def main():
    # Read keys from both Admin_keys.txt and Root_keys.txt
    admin_keys = read_keys_from_file('admin_keys.txt')
    root_keys = read_keys_from_file('root_keys.txt')
    all_keys = admin_keys + root_keys

    # Iterate over the keys and create a user and keys for each
    for key, secret in all_keys:
        create_user_and_keys(key, secret)

if __name__ == '__main__':
    main()
Leave a Comment