mds_plugin/user.py
def create_user_api_key(user_name=None, key_path="~/.oci/", user_id=None,
                        key_file_prefix="oci_api_key",
                        config=None, write_to_disk=True, return_object=False,
                        interactive=True):
    """Creates an API key for a user

    Args:
        user_name (str): The name of the user
        key_path (str): The path where the key files should be created
        user_id (str): The OCID of the user.
        key_file_prefix (str): A key_file_prefix to use
        config (dict): An OCI config object or None.
        write_to_disk (bool): Whether the keys should be written to disk
        return_object (bool): Whether the keys should be returned
        interactive (bool): Whether to query the user for input

    Returns:
        The user account details
    """

    # Get the current config
    try:
        if interactive or user_name is not None or user_id is not None:
            config = configuration.get_current_config(config=config)
    except ValueError as e:
        print(e)
        return

    import oci.identity
    import time
    import os.path
    from pathlib import Path

    # Get the user object
    user = None
    if interactive or user_name is not None or user_id is not None:
        user = get_user(user_name=user_name, user_id=user_id, config=config)
        if user is None:
            print("User not found.")
            return

    # Generate the API Keys
    from cryptography.hazmat.primitives import serialization
    from cryptography.hazmat.backends import default_backend
    from cryptography.hazmat.primitives.asymmetric import rsa

    key = rsa.generate_private_key(
        public_exponent=65537,
        key_size=2048,
        backend=default_backend()
    )

    private_key = key.private_bytes(
        serialization.Encoding.PEM,
        serialization.PrivateFormat.TraditionalOpenSSL,
        serialization.NoEncryption())

    public_key = key.public_key().public_bytes(
        serialization.Encoding.PEM,
        serialization.PublicFormat.SubjectPublicKeyInfo
    )

    # Create path
    # Convert Unix path to Windows
    key_path = os.path.abspath(os.path.expanduser(key_path))
    Path(key_path).mkdir(parents=True, exist_ok=True)

    # Create filenames for keys
    user_key_file_caption = f'_{user.name}' if user else ''
    private_key_path = os.path.join(
        key_path, f"{key_file_prefix}{user_key_file_caption}.pem")
    public_key_path = os.path.join(
        key_path, f"{key_file_prefix}{user_key_file_caption}_public.pem")

    # Write out keys
    if write_to_disk is True:
        with open(private_key_path, mode='wb') as file:
            file.write(private_key)
        with open(public_key_path, mode='wb') as file:
            file.write(public_key)

    # Get the fingerprint for the key
    fingerprint = get_fingerprint(public_key)

    # If a use was selected, upload the key
    if user:
        # Initialize the identity client
        identity = core.get_oci_identity_client(config=config)

        # Check to see if this key is already associated with the user.
        if is_key_already_uploaded(identity.list_api_keys(
                user.id).data, fingerprint):
            print(f"Key with fingerprint {fingerprint} has already been "
                  f"added to the user")
            return

        # Initialize the CreateApiKeyDetails model
        key_details = oci.identity.models.CreateApiKeyDetails(
            key=public_key.decode())

        # Upload the key
        identity.upload_api_key(user.id, key_details)

        i = 0
        key_uploaded = False
        while not key_uploaded and i < 30:
            key_uploaded = is_key_already_uploaded(identity.list_api_keys(
                user.id).data, fingerprint)
            if key_uploaded:
                break
            time.sleep(2)
            i += 1

        if not key_uploaded:
            print("The key uploaded failed.")
            return

    if return_object:
        return {
            "private_key": private_key,
            "public_key": public_key,
            "fingerprint": fingerprint}
Creates an API key for a user

    Args:
        user_name (str): The name of the user
        key_path (str): The path where the key files should be created
        user_id (str): The OCID of the user.
        key_file_prefix (str): A key_file_prefix to use
        config (dict): An OCI config object or None.
        write_to_disk (bool): Whether the keys should be written to disk
        return_object (bool): Whether the keys should be returned
        interactive (bool): Whether to query the user for input

    Returns:
        The user account details
    
Creates an API key for a user
Creates an API key for a user
def create_user_api_key(user_name=None, key_path="~/.oci/", user_id=None,
                        key_file_prefix="oci_api_key",
                        config=None, write_to_disk=True, return_object=False,
                        interactive=True):
    try:
        if interactive or user_name is not None or user_id is not None:
            config = configuration.get_current_config(config=config)
    except ValueError as e:
        print(e)
        return
    import oci.identity
    import time
    import os.path
    from pathlib import Path
    user = None
    if interactive or user_name is not None or user_id is not None:
        user = get_user(user_name=user_name, user_id=user_id, config=config)
        if user is None:
            print("User not found.")
            return
    from cryptography.hazmat.primitives import serialization
    from cryptography.hazmat.backends import default_backend
    from cryptography.hazmat.primitives.asymmetric import rsa
    key = rsa.generate_private_key(
        public_exponent=65537,
        key_size=2048,
        backend=default_backend()
    )
    private_key = key.private_bytes(
        serialization.Encoding.PEM,
        serialization.PrivateFormat.TraditionalOpenSSL,
        serialization.NoEncryption())
    public_key = key.public_key().public_bytes(
        serialization.Encoding.PEM,
        serialization.PublicFormat.SubjectPublicKeyInfo
    )
    key_path = os.path.abspath(os.path.expanduser(key_path))
    Path(key_path).mkdir(parents=True, exist_ok=True)
    user_key_file_caption = f'_{user.name}' if user else ''
    private_key_path = os.path.join(
        key_path, f"{key_file_prefix}{user_key_file_caption}.pem")
    public_key_path = os.path.join(
        key_path, f"{key_file_prefix}{user_key_file_caption}_public.pem")
    if write_to_disk is True:
        with open(private_key_path, mode='wb') as file:
            file.write(private_key)
        with open(public_key_path, mode='wb') as file:
            file.write(public_key)
    fingerprint = get_fingerprint(public_key)
    if user:
        identity = core.get_oci_identity_client(config=config)
        if is_key_already_uploaded(identity.list_api_keys(
                user.id).data, fingerprint):
            print(f"Key with fingerprint {fingerprint} has already been "
                  f"added to the user")
            return
        key_details = oci.identity.models.CreateApiKeyDetails(
            key=public_key.decode())
        identity.upload_api_key(user.id, key_details)
        i = 0
        key_uploaded = False
        while not key_uploaded and i < 30:
            key_uploaded = is_key_already_uploaded(identity.list_api_keys(
                user.id).data, fingerprint)
            if key_uploaded:
                break
            time.sleep(2)
            i += 1
        if not key_uploaded:
            print("The key uploaded failed.")
            return
    if return_object:
        return {
            "private_key": private_key,
            "public_key": public_key,
            "fingerprint": fingerprint}
def create_user_api_key ( user_name = None , key_path = "~/.oci/" , user_id = None , key_file_prefix = "oci_api_key" , config = None , write_to_disk = True , return_object = False , interactive = True ) : try : if interactive or user_name is not None or user_id is not None : config = configuration . get_current_config ( config = config ) except ValueError as e : print ( e ) return import oci . identity import time import os . path from pathlib import Path user = None if interactive or user_name is not None or user_id is not None : user = get_user ( user_name = user_name , user_id = user_id , config = config ) if user is None : print ( "User not found." ) return from cryptography . hazmat . primitives import serialization from cryptography . hazmat . backends import default_backend from cryptography . hazmat . primitives . asymmetric import rsa key = rsa . generate_private_key ( public_exponent = 65537 , key_size = 2048 , backend = default_backend ( ) ) private_key = key . private_bytes ( serialization . Encoding . PEM , serialization . PrivateFormat . TraditionalOpenSSL , serialization . NoEncryption ( ) ) public_key = key . public_key ( ) . public_bytes ( serialization . Encoding . PEM , serialization . PublicFormat . SubjectPublicKeyInfo ) key_path = os . path . abspath ( os . path . expanduser ( key_path ) ) Path ( key_path ) . mkdir ( parents = True , exist_ok = True ) user_key_file_caption = f'_{user.name}' if user else '' private_key_path = os . path . join ( key_path , f"{key_file_prefix}{user_key_file_caption}.pem" ) public_key_path = os . path . join ( key_path , f"{key_file_prefix}{user_key_file_caption}_public.pem" ) if write_to_disk is True : with open ( private_key_path , mode = 'wb' ) as file : file . write ( private_key ) with open ( public_key_path , mode = 'wb' ) as file : file . write ( public_key ) fingerprint = get_fingerprint ( public_key ) if user : identity = core . get_oci_identity_client ( config = config ) if is_key_already_uploaded ( identity . list_api_keys ( user . id ) . data , fingerprint ) : print ( f"Key with fingerprint {fingerprint} has already been " f"added to the user" ) return key_details = oci . identity . models . CreateApiKeyDetails ( key = public_key . decode ( ) ) identity . upload_api_key ( user . id , key_details ) i = 0 key_uploaded = False while not key_uploaded and i < 30 : key_uploaded = is_key_already_uploaded ( identity . list_api_keys ( user . id ) . data , fingerprint ) if key_uploaded : break time . sleep ( 2 ) i += 1 if not key_uploaded : print ( "The key uploaded failed." ) return if return_object : return { "private_key" : private_key , "public_key" : public_key , "fingerprint" : fingerprint }
Creates an API key for a user
Creates an API key for a user
"""Creates an API key for a user

    Args:
        user_name (str): The name of the user
        key_path (str): The path where the key files should be created
        user_id (str): The OCID of the user.
        key_file_prefix (str): A key_file_prefix to use
        config (dict): An OCI config object or None.
        write_to_disk (bool): Whether the keys should be written to disk
        return_object (bool): Whether the keys should be returned
        interactive (bool): Whether to query the user for input

    Returns:
        The user account details
    """ # Get the current config
 # Get the user object
 # Generate the API Keys
 # Create path
 # Convert Unix path to Windows
 # Create filenames for keys
 # Write out keys
 # Get the fingerprint for the key
 # If a use was selected, upload the key
 # Initialize the identity client
 # Check to see if this key is already associated with the user.
 # Initialize the CreateApiKeyDetails model
 # Upload the key

The user account details
The user account details
user_name
The name of the user
The name of the user
key_path
The path where the key files should be created
The path where the key files should be created
user_id
The OCID of the user.
The OCID of the user .
key_file_prefix
A key_file_prefix to use
A key_file_prefix to use
config
An OCI config object or None.
An OCI config object or None .
write_to_disk
Whether the keys should be written to disk
Whether the keys should be written to disk
return_object
Whether the keys should be returned
Whether the keys should be returned
interactive
Whether to query the user for input
Whether to query the user for input