summaryrefslogtreecommitdiff
path: root/cloudinit/sources/azure/imds.py
blob: 54fc9a05e9ac656338d94e5d293e2b97e16ef435 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# Copyright (C) 2022 Microsoft Corporation.
#
# This file is part of cloud-init. See LICENSE file for license information.

import functools
from typing import Dict

import requests

from cloudinit import log as logging
from cloudinit import util
from cloudinit.sources.helpers.azure import report_diagnostic_event
from cloudinit.url_helper import UrlError, readurl, retry_on_url_exc

LOG = logging.getLogger(__name__)

IMDS_URL = "http://169.254.169.254/metadata"

_readurl_exception_callback = functools.partial(
    retry_on_url_exc,
    retry_codes=(
        404,  # not found (yet)
        410,  # gone / unavailable (yet)
        429,  # rate-limited/throttled
        500,  # server error
    ),
    retry_instances=(
        requests.ConnectionError,
        requests.Timeout,
    ),
)


def _fetch_url(
    url: str, *, log_response: bool = True, retries: int = 10, timeout: int = 2
) -> bytes:
    """Fetch URL from IMDS.

    :raises UrlError: on error fetching metadata.
    """

    try:
        response = readurl(
            url,
            exception_cb=_readurl_exception_callback,
            headers={"Metadata": "true"},
            infinite=False,
            log_req_resp=log_response,
            retries=retries,
            timeout=timeout,
        )
    except UrlError as error:
        report_diagnostic_event(
            "Failed to fetch metadata from IMDS: %s" % error,
            logger_func=LOG.warning,
        )
        raise

    return response.contents


def _fetch_metadata(
    url: str,
) -> Dict:
    """Fetch IMDS metadata.

    :raises UrlError: on error fetching metadata.
    :raises ValueError: on error parsing metadata.
    """
    metadata = _fetch_url(url)

    try:
        return util.load_json(metadata)
    except ValueError as error:
        report_diagnostic_event(
            "Failed to parse metadata from IMDS: %s" % error,
            logger_func=LOG.warning,
        )
        raise


def fetch_metadata_with_api_fallback() -> Dict:
    """Fetch extended metadata, falling back to non-extended as required.

    :raises UrlError: on error fetching metadata.
    :raises ValueError: on error parsing metadata.
    """
    try:
        url = IMDS_URL + "/instance?api-version=2021-08-01&extended=true"
        return _fetch_metadata(url)
    except UrlError as error:
        if error.code == 400:
            report_diagnostic_event(
                "Falling back to IMDS api-version: 2019-06-01",
                logger_func=LOG.warning,
            )
            url = IMDS_URL + "/instance?api-version=2019-06-01"
            return _fetch_metadata(url)
        raise


def fetch_reprovision_data() -> bytes:
    """Fetch extended metadata, falling back to non-extended as required.

    :raises UrlError: on error.
    """
    url = IMDS_URL + "/reprovisiondata?api-version=2019-06-01"

    logging_threshold = 1
    poll_counter = 0

    def exception_callback(msg, exception):
        nonlocal logging_threshold
        nonlocal poll_counter

        poll_counter += 1
        if not isinstance(exception, UrlError):
            report_diagnostic_event(
                "Polling IMDS failed with unexpected exception: %r"
                % (exception),
                logger_func=LOG.warning,
            )
            return False

        log = True
        retry = False
        if exception.code in (404, 410):
            retry = True
            if poll_counter >= logging_threshold:
                # Exponential back-off on logging.
                logging_threshold *= 2
            else:
                log = False

        if log:
            report_diagnostic_event(
                "Polling IMDS failed with exception: %r count: %d"
                % (exception, poll_counter),
                logger_func=LOG.info,
            )
        return retry

    response = readurl(
        url,
        exception_cb=exception_callback,
        headers={"Metadata": "true"},
        infinite=True,
        log_req_resp=False,
        timeout=2,
    )

    report_diagnostic_event(
        f"Polled IMDS {poll_counter+1} time(s)",
        logger_func=LOG.debug,
    )
    return response.contents