# Buggy code
def start_okta_ingestion(neo4j_session, config):
    """
    Starts the OKTA ingestion process
    :param neo4j_session: The Neo4j session
    :param config: A `cartography.config` object
    :return: Nothing
    """

    logger.debug(f"Starting Okta sync on {config.okta_org_id}")

    common_job_parameters = {
        "UPDATE_TAG": config.update_tag,
        "OKTA_ORG_ID": config.okta_org_id,
    }

    state = OktaSyncState()

    organization.create_okta_organization(
        neo4j_session, config.okta_org_id, config.update_tag
    )
    users.sync_okta_users(
        neo4j_session, config.okta_org_id, config.update_tag, config.okta_api_key, state
    )
    groups.sync_okta_groups(
        neo4j_session, config.okta_org_id, config.update_tag, config.okta_api_key, state
    )
    applications.sync_okta_applications(
        neo4j_session, config.okta_org_id, config.update_tag, config.okta_api_key
    )
    factors.sync_users_factors(
        neo4j_session, config.okta_org_id, config.update_tag, config.okta_api_key, state
    )
    origins.sync_trusted_origins(
        neo4j_session, config.okta_org_id, config.update_tag, config.okta_api_key
    )
    awssaml.sync_okta_aws_saml(
        neo4j_session, config.okta_saml_role_regex, config.update_tag
    )

    # need creds with permission
    # soft fail as some won't be able to get such high priv token
    # when we get the E0000006 error
    # see https://developer.okta.com/docs/reference/error-codes/
    try:
        roles.sync_roles(
            neo4j_session,
            config.okta_org_id,
            config.update_tag,
            config.okta_api_key,
            state,
        )
    except OktaError as okta_error:
        logger.warning(f"Unable to pull admin roles got {okta_error}")

        # Getting roles requires super admin which most won't be able to get easily
        if okta_error.error_code == "E0000006":
            logger.warning(
                "Unable to sync admin roles - api token needs admin rights to pull admin roles data"
            )

    _cleanup_okta_organizations(neo4j_session, common_job_parameters)


# Traceback (AttributeError)
INFO:cartography.sync:Starting sync stage 'okta'
ERROR:cartography.sync:Unhandled exception during sync stage 'okta'
Traceback (most recent call last):
File "/home/ec2-user/venv/lib64/python3.6/site-packages/cartography/sync.py", line 69, in run
stage_func(neo4j_session, config)
File "/home/ec2-user/venv/lib64/python3.6/site-packages/cartography/intel/okta/__init__.py", line 48, in start_okta_ingestion
users.sync_okta_users(neo4j_session, config.okta_org_id, config.update_tag, config.okta_api_key, state)
AttributeError: 'Namespace' object has no attribute 'okta_api_key'
Traceback (most recent call last):
File "/home/ec2-user/venv/bin/cartography", line 11, in <module>
load_entry_point('cartography==0.13.0', 'console_scripts', 'cartography')()
File "/home/ec2-user/venv/lib64/python3.6/site-packages/cartography/cli.py", line 216, in main
return CLI(default_sync, prog='cartography').main(argv)
File "/home/ec2-user/venv/lib64/python3.6/site-packages/cartography/cli.py", line 196, in main
return cartography.sync.run_with_config(self.sync, config)
File "/home/ec2-user/venv/lib64/python3.6/site-packages/cartography/sync.py", line 135, in run_with_config
return sync.run(neo4j_driver, config)
File "/home/ec2-user/venv/lib64/python3.6/site-packages/cartography/sync.py", line 69, in run
stage_func(neo4j_session, config)
File "/home/ec2-user/venv/lib64/python3.6/site-packages/cartography/intel/okta/__init__.py", line 48, in start_okta_ingestion
users.sync_okta_users(neo4j_session, config.okta_org_id, config.update_tag, config.okta_api_key, state)
AttributeError: 'Namespace' object has no attribute 'okta_api_key'

# Fixed code
def start_okta_ingestion(neo4j_session, config):
    """
    Starts the OKTA ingestion process
    :param neo4j_session: The Neo4j session
    :param config: A `cartography.config` object
    :return: Nothing
    """
    if not config.okta_api_key:
        logger.warning(
            "No valid Okta credentials could be found. Exiting Okta sync stage.",
        )
        return

    logger.debug(f"Starting Okta sync on {config.okta_org_id}")

    common_job_parameters = {
        "UPDATE_TAG": config.update_tag,
        "OKTA_ORG_ID": config.okta_org_id,
    }

    state = OktaSyncState()

    organization.create_okta_organization(
        neo4j_session, config.okta_org_id, config.update_tag
    )
    users.sync_okta_users(
        neo4j_session, config.okta_org_id, config.update_tag, config.okta_api_key, state
    )
    groups.sync_okta_groups(
        neo4j_session, config.okta_org_id, config.update_tag, config.okta_api_key, state
    )
    applications.sync_okta_applications(
        neo4j_session, config.okta_org_id, config.update_tag, config.okta_api_key
    )
    factors.sync_users_factors(
        neo4j_session, config.okta_org_id, config.update_tag, config.okta_api_key, state
    )
    origins.sync_trusted_origins(
        neo4j_session, config.okta_org_id, config.update_tag, config.okta_api_key
    )
    awssaml.sync_okta_aws_saml(
        neo4j_session, config.okta_saml_role_regex, config.update_tag
    )

    # need creds with permission
    # soft fail as some won't be able to get such high priv token
    # when we get the E0000006 error
    # see https://developer.okta.com/docs/reference/error-codes/
    try:
        roles.sync_roles(
            neo4j_session,
            config.okta_org_id,
            config.update_tag,
            config.okta_api_key,
            state,
        )
    except OktaError as okta_error:
        logger.warning(f"Unable to pull admin roles got {okta_error}")

        # Getting roles requires super admin which most won't be able to get easily
        if okta_error.error_code == "E0000006":
            logger.warning(
                "Unable to sync admin roles - api token needs admin rights to pull admin roles data"
            )

    _cleanup_okta_organizations(neo4j_session, common_job_parameters)