コード例 #1
0
def purge_static_cache(containers: list[str] = None):
    if (settings.AZURE_STATIC_CDN_ENDPOINT_NAME is None
            or len(settings.AZURE_STATIC_CDN_ENDPOINT_NAME) == 0):
        logger.warning("Azure settings not configured")
        return

    if containers is None:
        containers = STATIC_CONTAINERS

    credentials = ClientSecretCredential(
        tenant_id=settings.AZURE_TENANT_ID,
        client_id=settings.AZURE_CLIENT_ID,
        client_secret=settings.AZURE_CLIENT_SECRET,
    )

    client = CdnManagementClient(credentials, settings.AZURE_SUBSCRIPTION_ID)

    paths_group: list[str] = []
    for group in containers:
        paths_group.append(f"/{settings.AZURE_CONTAINER_PREFIX}{group}/*")
    logger.info(
        "Purging paths: %s %s %s %s",
        settings.AZURE_STATIC_CDN_RESOURCE_GROUP,
        settings.AZURE_STATIC_CDN_PROFILE_NAME,
        settings.AZURE_STATIC_CDN_ENDPOINT_NAME,
        paths_group,
    )

    poller = client.endpoints.begin_purge_content(
        settings.AZURE_STATIC_CDN_RESOURCE_GROUP,
        settings.AZURE_STATIC_CDN_PROFILE_NAME,
        settings.AZURE_STATIC_CDN_ENDPOINT_NAME,
        PurgeParameters(content_paths=paths_group),
    )
    poller.result()
コード例 #2
0
def handler(event, context):
    subscription_id = os.environ['AzureSubscriptionID']
    client_id = os.environ['AzureClientID']
    secret = os.environ['AzureSecretKey']
    tenant = os.environ['AzureTenantID']
    credentials = get_credentials(
        client=client_id,
        secret_key=secret,
        tenant_id=tenant,
    )
    cdn_client = CdnManagementClient(credentials, subscription_id)
    cdn_profiles = cdn_client.profiles.list()
    for i in cdn_profiles:
        profile_name = ' '.join(i.id.split('/')[-1:])
        group = ''.join(i.id.split('/')[4])
        endpoints_by_profile = cdn_client.endpoints.list_by_profile(
            resource_group_name=group,
            profile_name=profile_name,
        )
        for j in endpoints_by_profile:
            endpoint_hostname = j.host_name
            # origin_hostname = j.origin_host_header
            results = {
                'type': 'cdn',
                'sk': endpoint_hostname,
                'provider': 'azure'
            }
            table = dynamodb.Table(os.environ['DynamoDBTableAssets'])
            table.put_item(
                Item=results
            )
    print('Done')
コード例 #3
0
ファイル: azure_cdn.py プロジェクト: syslog-ng/syslog-ng
    def __init__(
        self,
        resource_group_name: str,
        profile_name: str,
        endpoint_name: str,
        endpoint_subscription_id: str,
        tenant_id: str,
        client_id: str,
        client_secret: str,
    ):
        credential = ClientSecretCredential(
            tenant_id=tenant_id,
            client_id=client_id,
            client_secret=client_secret,
        )
        self.__cdn = CdnManagementClient(
            credential=credential,
            subscription_id=endpoint_subscription_id,
        )

        self.__resource_group_name = resource_group_name
        self.__profile_name = profile_name
        self.__endpoint_name = endpoint_name

        super().__init__()
コード例 #4
0
def main():

    SUBSCRIPTION_ID = os.environ.get("SUBSCRIPTION_ID", None)
    TIME = str(time.time()).replace('.', '')
    GROUP_NAME = "testcdn" + TIME
    CDN = "cdn" + TIME
    LOCATION = 'WestUs'

    # Create client
    # # For other authentication approaches, please see: https://pypi.org/project/azure-identity/
    resource_client = ResourceManagementClient(
        credential=DefaultAzureCredential(), subscription_id=SUBSCRIPTION_ID)
    cdn_client = CdnManagementClient(credential=DefaultAzureCredential(),
                                     subscription_id=SUBSCRIPTION_ID)
    # - init depended client -
    # - end -

    # Create resource group
    resource_client.resource_groups.create_or_update(GROUP_NAME,
                                                     {"location": LOCATION})

    # - init depended resources -
    # - end -

    # Create cdn
    cdn = cdn_client.profiles.begin_create(GROUP_NAME, CDN, {
        "location": LOCATION,
        "sku": {
            "name": "Standard_Verizon"
        }
    }).result()
    print("Create cdn:\n{}\n".format(cdn))

    # Get cdn
    cdn = cdn_client.profiles.get(GROUP_NAME, CDN)
    print("Get cdn:\n{}\n".format(cdn))

    # Update cdn
    cdn = cdn_client.profiles.begin_update(GROUP_NAME, CDN, {
        "tags": {
            "additional_properties": "Tag1"
        }
    }).result()
    print("Update cdn:\n{}\n".format(cdn))

    # Delete cdn
    cdn = cdn_client.profiles.begin_delete(GROUP_NAME, CDN).result()
    print("Delete cdn.\n")

    # Delete Group
    resource_client.resource_groups.begin_delete(GROUP_NAME).result()
コード例 #5
0
    def _get_client_for_hostname(self, hostname: str):
        from azure.mgmt.cdn import CdnManagementClient

        azure_credentials = self._get_setting_for_hostname(
            hostname, "credentials")
        azure_subscription_id = self._get_setting_for_hostname(
            hostname, "subscription_id")
        azure_cdn_service_url = self._get_setting_for_hostname(
            hostname, "cdn_service_url", raise_exception=False)
        return CdnManagementClient(
            credential=azure_credentials()
            if callable(azure_credentials) else azure_credentials,
            subscription_id=azure_subscription_id,
            base_url=azure_cdn_service_url,
        )
コード例 #6
0
def purge_cache(all_paths=False):
    # run after Azure has had a change to purge
    _reschedule(
        timezone.now() + timedelta(minutes=5),
        PURGE_DJ_CACHE,
        "Purge Django Cached Views",
    )

    if all_paths:
        paths = PURGE_GROUPS["__all__"]
    else:
        if not settings.AZURE_CDN_DYNAMIC_PURGE:
            logger.info("Dynamic purging disabled")
            return

        with cache.lock(PURGE_CACHE_LOCK, expire=10, auto_renewal=False):
            paths = cache.get(PURGE_CACHE_PATHS)

            if paths is not None:
                cache.delete(PURGE_CACHE_PATHS)

        if paths is None or len(paths) == 0:
            logger.warning("No paths to purge")
            return

        paths = list(paths)

    if (settings.AZURE_CDN_ENDPOINT_NAME is None
            or len(settings.AZURE_CDN_ENDPOINT_NAME) == 0):
        logger.warning("Azure settings not configured")
        return

    try:
        credentials = ClientSecretCredential(
            tenant_id=settings.AZURE_TENANT_ID,
            client_id=settings.AZURE_CLIENT_ID,
            client_secret=settings.AZURE_CLIENT_SECRET,
        )

        client = CdnManagementClient(credentials,
                                     settings.AZURE_SUBSCRIPTION_ID)

        for paths_group in _path_chunks(paths, MAX_SINGLE_PURGE):
            logger.info(
                "Purging paths: %s %s %s %s",
                settings.AZURE_CDN_RESOURCE_GROUP,
                settings.AZURE_CDN_PROFILE_NAME,
                settings.AZURE_CDN_ENDPOINT_NAME,
                paths_group,
            )

            poller = client.endpoints.begin_purge_content(
                settings.AZURE_CDN_RESOURCE_GROUP,
                settings.AZURE_CDN_PROFILE_NAME,
                settings.AZURE_CDN_ENDPOINT_NAME,
                PurgeParameters(content_paths=paths_group),
            )
            poller.result()
    except (Exception, socket.gaierror) as ex:  # pylint: disable=broad-except
        logger.info("Rescheduling paths: %s", paths)
        queue_purge_paths(paths)

        if "No address associated with hostname" not in str(ex):
            raise
コード例 #7
0
def azure_add(**kwargs):
    configs = get_configs()

    domain = get_final_domain(f"http://{kwargs['domain']}")
    # Tenant ID for your Azure subscription
    TENANT_ID = configs['azure_tenant_id']

    # Your service principal App ID
    CLIENT = configs['azure_app']

    # Your service principal password
    KEY = configs['azure_key']

    logger.debug("Authenticating...")
    credentials = ServicePrincipalCredentials(client_id=CLIENT,
                                              secret=KEY,
                                              tenant=TENANT_ID)

    endpoint_name = kwargs['domain'][0:6] + '1'
    endpoint_name_confirm = input(
        f"Azure CDN name/subdomain ({endpoint_name}.azureedge.net)?")
    if endpoint_name_confirm:
        endpoint_name = endpoint_name_confirm

    endpoint_full_name = endpoint_name + '.azureedge.net'
    my_resource_group = 'bypasscensorship'
    region = 'West India'
    tier = 'Standard_Akamai'

    cdn_client = CdnManagementClient(credentials, configs['azure_sub_id'])
    cdn_list = cdn_client.profiles.list()

    print("List of Azure CDNs:")
    count = 0
    names = []
    for profile in cdn_list:
        pf_vars = vars(profile)
        print(f"{count}: Name: {pf_vars['name']}")
        names.append(pf_vars['name'])
        endpoints_list = cdn_client.endpoints.list_by_profile(
            my_resource_group, pf_vars['name'])
        ep_count = 0
        for endpoint in endpoints_list:
            ep_count += 1
        print(f"Number of endpoints: {ep_count}")

        count += 1

    cdn_choice = input(f"Which CDN to add {endpoint_full_name} to?")
    if not cdn_choice:
        return False
    else:
        cdn_name = names[int(cdn_choice)]

    cdn_confirm = input(f"Add to {cdn_name} (Y/n)?")
    if cdn_confirm.lower() == 'n':
        return False

    logger.debug("Adding...")
    endpoint_poller = cdn_client.endpoints.create(
        my_resource_group, cdn_name, endpoint_name, {
            "location": region,
            "origin_host_header": domain,
            "origins": [{
                "name": cdn_name,
                "host_name": domain
            }]
        })
    endpoint = endpoint_poller.result()

    logger.debug("Done!")
    return endpoint_full_name