# This file is part of cloud-init. See LICENSE file for license information.
"""Datasource for Oracle (OCI/Oracle Cloud Infrastructure)

OCI provides a OpenStack like metadata service which provides only
'2013-10-17' and 'latest' versions..

Notes:
 * This datasource does not support the OCI-Classic. OCI-Classic
   provides an EC2 lookalike metadata service.
 * The uuid provided in DMI data is not the same as the meta-data provided
   instance-id, but has an equivalent lifespan.
 * Previous versions of this datasource used openstack. This is no longer the
   case, as the Oracle IMDS endopoints are used. Upgrading from a version of
   cloud-init that used openstack should not be an issue.
 * Both bare-metal and vms use iscsi root
 * Both bare-metal and vms provide chassis-asset-tag of OracleCloud.com
"""

from cloudinit.url_helper import combine_url, readurl, UrlError
from cloudinit.net import dhcp, get_interfaces_by_mac
from cloudinit.event import EventType
from cloudinit import net
from cloudinit import sources
from cloudinit import util
from cloudinit.net import cmdline
from cloudinit import log as logging

import base64
import glob
import json
import os

LOG = logging.getLogger(__name__)

CHASSIS_ASSET_TAG = "OracleCloud.com"
OPC_METADATA_ENDPOINT = 'http://169.254.169.254/opc/'
OPC_V2_METADATA_HEADERS = {'Authorization': 'Bearer Oracle'}
OPC_METADATA_RETRIES = 9
# https://docs.cloud.oracle.com/iaas/Content/Network/Troubleshoot/connectionhang.htm#Overview,
# indicates that an MTU of 9000 is used within OCI
MTU = 9000

BUILTIN_DS_CONFIG = {
    # Don't use IMDS to configure secondary NICs by default
    'configure_secondary_nics': False,
    'mtu': MTU,
}

def _add_network_config_from_opc_imds(network_config, vnics, mtu=MTU):
    """
    Fetch data from Oracle's IMDS, generate secondary NIC config, merge it.

    The primary NIC configuration should not be modified based on the IMDS
    values, as it should continue to be configured for DHCP.  As such, this
    takes an existing network_config dict which is expected to have the primary
    NIC configuration already present.  It will mutate the given dict to
    include the secondary VNICs.

    :param network_config:
        A v1 or v2 network config dict with the primary NIC already configured.
        This dict will be mutated.

    :param vnics:
        Raw vnics metadata crawled from OCI IMDS

    :param mtu:
        Default MTU value if specified by system config.

    :raises:
        Exceptions are not handled within this function.  Likely exceptions are
        those raised by url_helper.readurl (if communicating with the IMDS
        fails), ValueError/JSONDecodeError (if the IMDS returns invalid JSON),
        and KeyError/IndexError (if the IMDS returns valid JSON with unexpected
        contents).
    """
    if 'nicIndex' in vnics[0]:
        # TODO: Once configure_secondary_nics defaults to True, lower the level
        # of this log message.  (Currently, if we're running this code at all,
        # someone has explicitly opted-in to secondary VNIC configuration, so
        # we should warn them that it didn't happen.  Once it's default, this
        # would be emitted on every Bare Metal Machine launch, which means INFO
        # or DEBUG would be more appropriate.)
        LOG.warning(
            'VNIC metadata indicates this is a bare metal machine; skipping'
            ' secondary VNIC configuration.'
        )
        return

    interfaces_by_mac = get_interfaces_by_mac()

    for vnic_dict in vnics[1:]:
        # We skip the first entry in the response because the primary interface
        # is already configured by iSCSI boot; applying configuration from the
        # IMDS is not required.
        mac_address = vnic_dict['macAddr'].lower()
        if mac_address not in interfaces_by_mac:
            LOG.debug('Interface with MAC %s not found; skipping', mac_address)
            continue
        name = interfaces_by_mac[mac_address]
        privateIp = vnic_dict['privateIp']
        netPrefix = vnic_dict['subnetCidrBlock'].split('/')[1]
        ipAddr = '{ip}/{prefix}'.format(
               ip=privateIp, prefix=netPrefix)

        if network_config['version'] == 1:
            subnet = {
                'type': 'static',
                'address': ipAddr,
            }
            network_config['config'].append({
                'name': name,
                'type': 'physical',
                'mac_address': mac_address,
                'mtu': mtu,
                'subnets': [subnet],
            })
        elif network_config['version'] == 2:
            network_config['ethernets'][name] = {
                'addresses': [ipAddr],
                'mtu': mtu, 'dhcp4': False, 'dhcp6': False,
                'match': {'macaddress': mac_address}}


class DataSourceOracle(sources.DataSource):

    dsname = 'Oracle'
    system_uuid = None
    vendordata_pure = None
    network_config_sources = (
        sources.NetworkConfigSource.cmdline,
        sources.NetworkConfigSource.ds,
        sources.NetworkConfigSource.initramfs,
        sources.NetworkConfigSource.system_cfg,
    )

    _network_config = sources.UNSET
    _vnic_metadata = sources.UNSET

    def __init__(self, sys_cfg, *args, **kwargs):
        super(DataSourceOracle, self).__init__(sys_cfg, *args, **kwargs)

        self.ds_cfg = util.mergemanydict([
            util.get_cfg_by_path(sys_cfg, ['datasource', self.dsname], {}),
            BUILTIN_DS_CONFIG])

    def _is_platform_viable(self):
        """Check platform environment to report if this datasource may run."""
        return _is_platform_viable()

    def _get_data(self):
        if not self._is_platform_viable():
            return False

        # network may be configured if iscsi root.  If that is the case
        # then read_initramfs_config will return non-None.
        if _is_iscsi_root():
            data, vnic_md = self.crawl_metadata()
        else:
            with dhcp.EphemeralDHCPv4(net.find_fallback_nic()):
                data, vnic_md = self.crawl_metadata()

        self.userdata_raw = data.get('user_data')
        self.vendordata_pure = data.get('vendor_data')
        self.system_uuid = data['system_uuid']
        self.metadata = data['meta_data']
        self._vnic_metadata = vnic_md

        self.ds_cfg = util.mergemanydict([
            util.get_cfg_by_path(self.sys_cfg, ['datasource', self.dsname], {}),
            BUILTIN_DS_CONFIG])

        try:
            self.vendordata_raw = \
                sources.convert_vendordata(data.get('vendor_data'))
        except ValueError as e:
            LOG.warning("Invalid content in vendor-data: %s", e)
            self.vendordata_raw = None


        return True

    def crawl_metadata(self):
        return read_metadata(), read_vnic_metadata()

    def _get_subplatform(self):
        """Return the subplatform metadata source details."""
        return 'metadata (%s)' % OPC_METADATA_ENDPOINT

    def check_instance_id(self, sys_cfg):
        """quickly check (local only) if self.instance_id is still valid

        On Oracle, the dmi-provided system uuid differs from the instance-id
        but has the same life-span."""
        return sources.instance_id_matches_system_uuid(self.system_uuid)

    def get_public_ssh_keys(self):
        return sources.normalize_pubkey_data(self.metadata.get('public_keys'))

    @property
    def update_events(self):
        events = {'network': set([EventType.BOOT_NEW_INSTANCE,
                                  EventType.BOOT]),
                  'storage': set([])}

        self.ds_cfg = util.mergemanydict([
            util.get_cfg_by_path(self.sys_cfg, ['datasource', self.dsname], {}),
            BUILTIN_DS_CONFIG])

        if self.ds_cfg.get('configure_secondary_nics', False):
            events['network'].add(EventType.HOTPLUG)

        LOG.debug('Oracle ds update events: %s', events)

        return events

    @property
    def network_config(self):
        """Network config is read from initramfs provided files, plus
        those got from Instance MetaData Service for secondary NICs.
        If none is present, then we fall back to fallback configuration.

        One thing to note here is that this method is not currently
        considered at all if there is is kernel bootline provided
        data.  In that case, stages considers that the cmdline data
        overrides datasource provided data and does not consult here.

        The primary NIC is always configured through initramfs provided
        data. If initramfs provided data is unavailable, attempt will
        still be made to get metadata from IMDS for secondary NICs, once
        network for primary NIC is brought up by system. If no network
        is availabe at all, it has to generate a fallback config."""

        if self._network_config == sources.UNSET:
            # this is v1
            self._network_config = cmdline.read_initramfs_config()

            if not self._network_config:
                # this is now v2
                self._network_config = self.distro.generate_fallback_config()

            # Even if initramfs data is not provided, we still try to
            # configure secondary NICs when network is available through
            # ephemeral DHCP during _get_data().
            # We can't poll IMDS right here for VNIC metadata, since
            # network or route might not have been configured properly.
            if self.ds_cfg.get('configure_secondary_nics', False):
                if self._vnic_metadata != sources.UNSET:
                    # Mutate netcfg to include secondary VNICs
                    _add_network_config_from_opc_imds(
                        self._network_config,
                        self._vnic_metadata,
                        mtu=self.ds_cfg.get('mtu', MTU))
                else:
                    LOG.warning('Unexpected call to network_config when '
                        '_vnic_metadata is None.')

        return self._network_config

class OracleImdsHelper:

    _version = None
    _headers = None

    def get_latest_supported_version(self):
        """checks for existence of v2 and falls back to v1 if not available"""
        try:
            resp = readurl(
                combine_url(OPC_METADATA_ENDPOINT, 'v2', 'instance', 'id'),
                headers=OPC_V2_METADATA_HEADERS,
                timeout=20,
                retries=OPC_METADATA_RETRIES)
            LOG.debug("OracleImdsHelper.get_latest_supported_version() -> %s",
                      resp)
            return 'v2' if resp.ok() else 'v1'
        except UrlError as e:
            LOG.warning(
                "Error while looking for OCI IMDS v2 support: %s", e)
            if e.code in [403, 404]:
                LOG.warning("Falling back to OCI IMDS v1")
                return 'v1'
            else:
                raise e

    def get_metadata(self, path):
        """reads requested metadata from latest version available
        and returns a string"""
        try:
            endpoint = combine_url(self.get_base_uri(), path)
            resp = readurl(endpoint, headers=self._headers,
                           timeout=20,
                           retries=OPC_METADATA_RETRIES)

            if not resp.ok():
                raise sources.BrokenMetadata(
                    "Bad response from %s: %s" % (endpoint, resp.code))
            LOG.debug("OracleImdsHelper.get_metadata(%s) -> %s", path, resp)

            return resp.contents.decode('utf-8')
        except UrlError as e:
            raise sources.BrokenMetadata(
                "Failed to read index at %s: %s" % (path, e))

    def get_metadata_json(self, path):
        """returns requested metadata parsed as a json object"""
        if not path.endswith('/'):
            path += '/'
        return json.loads(self.get_metadata(path))

    def get_base_uri(self):
        """returns the base metadata uri including latest supported version"""
        if self._version is None:
            LOG.debug("OracleImdsHelper.get_base_uri() -> "
                      "Priming version cache")
            self._version = self.get_latest_supported_version()
            self._headers = OPC_V2_METADATA_HEADERS \
                if self._version is "v2" else None
        return combine_url(OPC_METADATA_ENDPOINT, self._version)


_imds_helper = OracleImdsHelper()


def _read_system_uuid():
    sys_uuid = util.read_dmi_data('system-uuid')
    return None if sys_uuid is None else sys_uuid.lower()


def _is_platform_viable():
    asset_tag = util.read_dmi_data('chassis-asset-tag')
    return asset_tag == CHASSIS_ASSET_TAG


def _is_iscsi_root():
    return bool(cmdline.read_initramfs_config())


def read_metadata(sys_uuid=None, imds_helper=_imds_helper):
    """Read metadata, return a dictionary

    The system uuid (/sys/class/dmi/id/product_uuid) is also populated.
    Example: given instance metadata = {
        "id": "foo",
        "displayName": "Bar",
        "ociAdName": "PHX-1",
        "hostname": "bar.oracle.com",
        "metadata": {
            "ssh_authorized_keys": "key1\nkey2",
            "user_data": base64.b64encode("userdata"),
            "vendor_data": base64.b64encode("vendordata")
        }
     }
     This would return:
     {
       "system_uuid": "uuid",
       "user_data": base64.b64decode(...),
       "vendor_data": base64.b64decode(...),
       "meta_data": {
           "launch-index": 0,
           "files": [],
           "meta": {},
           "instance-id": "foo",
           "name": "Bar",
           "availability-zone": "PHX-1",
           "local-hostname": "bar.oracle.com",
           "public_keys": {
               "0": "key1",
               "1": "key2"
           }
       }
     }
     """
    if sys_uuid is None:
        sys_uuid = _read_system_uuid()
    if not sys_uuid:
        raise sources.BrokenMetadata("Failed to read system uuid.")

    # launch-index, files, and meta are not supported by OCI
    md = {'launch-index': 0, 'files': [], 'meta': {}}
    instance = imds_helper.get_metadata_json('instance')

    try:
        md['instance-id'] = instance['id']
        md['name'] = instance['displayName']
        md['availability-zone'] = instance['ociAdName']
        md['local-hostname'] = instance['hostname']

        imd = instance['metadata']

        # keys are expected to be a '0' indexed map while
        # OCI provides a new-line delimited string
        authorized_keys = imd['ssh_authorized_keys'].split('\n') \
            if 'ssh_authorized_keys' in imd else []
        md['public_keys'] = dict(
            zip(map(str, range(0, len(authorized_keys))), authorized_keys))

        ret = {'system_uuid': sys_uuid, 'meta_data': md}
        ret['user_data'] = base64.b64decode(imd['user_data']) \
            if 'user_data' in imd else None
        vd = base64.b64decode(imd['vendor_data']) \
            if 'vendor_data' in imd else None
        try:
            ret['vendor_data'] = json.loads(vd)
        except (TypeError, ValueError):
            # return just the base64 decoded string if
            # failed to parse it as JSON
            ret['vendor_data'] = vd

        LOG.debug("read_metadata() -> cloud-init formatted metadata: %s", ret)
        return ret
    except KeyError as e:
        raise sources.BrokenMetadata("Bad or missing metadata %s" % (e))


def read_vnic_metadata(imds_helper=_imds_helper):
    return imds_helper.get_metadata_json("vnics")


# Used to match classes to dependencies
datasources = [
    (DataSourceOracle, (sources.DEP_FILESYSTEM,)),
]


# Return a list of data sources that match this set of dependencies
def get_datasource_list(depends):
    return sources.list_from_depends(depends, datasources)


if __name__ == "__main__":
    import os
    sys_uuid = "uuid-not-available-not-root" if os.geteuid() != 0 else None

    data = read_metadata(sys_uuid=sys_uuid)
    data['is_platform_viable'] = _is_platform_viable()
    print(util.json_dumps(data))

# vi: ts=4 expandtab
