{
  "version": "v1",
  "variables": {
    "XTNA_TOKEN": {
      "type": "STRING",
      "label": "Asset creation token (XplicitTrust console)",
      "value": "",
      "system": false,
      "restricted": false
    },
    "XTNA_DOMAIN": {
      "type": "USER_TYPE",
      "label": "Tenant domain, e.g. example.com",
      "value": "",
      "system": false,
      "restricted": false,
      "userTypeName": "XTNA_Domain"
    },
    "XTNA_NAME": {
      "type": "STRING",
      "label": "Asset name; also sets -ignore-hostname",
      "value": "",
      "system": false,
      "restricted": false
    },
    "XTNA_BRIDGE": {
      "type": "USER_TYPE",
      "label": "Docker bridge, set PER DEVICE (Bridge Interfaces, e.g. dockereaecd76f)",
      "value": "",
      "system": false,
      "restricted": false,
      "userTypeName": "XTNA_Bridge"
    },
    "XTNA_INTERNET": {
      "type": "STRING",
      "label": "Internet object name, exactly as on the firewall (or its uniqueId)",
      "value": "WAN",
      "system": false,
      "restricted": false
    },
    "XTNA_CREATE_NETWORK": {
      "type": "USER_TYPE",
      "label": "Declare the docker network (off only if it is managed elsewhere)",
      "value": "on",
      "system": false,
      "restricted": false,
      "userTypeName": "XTNA_OnOff"
    },
    "XTNA_NETWORK": {
      "type": "STRING",
      "label": "Docker network name",
      "value": "Docker",
      "system": false,
      "restricted": false
    },
    "XTNA_SUBNET": {
      "type": "USER_TYPE",
      "label": "Docker network subnet",
      "value": "10.238.238.0/24",
      "system": false,
      "restricted": false,
      "userTypeName": "XTNA_CIDR"
    },
    "XTNA_WAN_ACCESS": {
      "type": "USER_TYPE",
      "label": "Create the desktop object and WAN connection",
      "value": "on",
      "system": false,
      "restricted": false,
      "userTypeName": "XTNA_OnOff"
    },
    "XTNA_SERVICES": {
      "type": "STRING",
      "label": "Outbound services (names or uuids), comma separated",
      "value": "internet.https,standard.dns",
      "system": false,
      "restricted": false
    },
    "XTNA_PORT_FORWARD": {
      "type": "USER_TYPE",
      "label": "Forward the WireGuard port to the container (direct peering)",
      "value": "off",
      "system": false,
      "restricted": false,
      "userTypeName": "XTNA_OnOff"
    },
    "XTNA_PORT": {
      "type": "USER_TYPE",
      "label": "Fixed WireGuard port, used with port forwarding",
      "value": "51899",
      "system": false,
      "restricted": false,
      "userTypeName": "XTNA_Port"
    },
    "XTNA_IP": {
      "type": "USER_TYPE",
      "label": "Container address for port forwarding (blank = 10th of the subnet)",
      "value": "",
      "system": false,
      "restricted": false,
      "userTypeName": "XTNA_IPv4"
    }
  },
  "applications": {
    "XTNA-Agent": {
      "script": {
        "lcos": false,
        "swos": false,
        "lcosFx": true,
        "lcosLx": false,
        "lcosSxXs": false,
        "lcosSxSdk4": false,
        "content": "/**\n * XplicitTrust Network Access - agent container deployment\n * LMC add-in for LANCOM R&S Unified Firewalls (LCOS FX 11.2+, SAG Basic/Full licence)\n *\n * ---------------------------------------------------------------------------\n * How the LCOS FX add-in API actually works\n * ---------------------------------------------------------------------------\n * Probed on a live UF: ufApi.raw() returns undefined for every URI, so there is\n * NO way to read device state from an add-in. ufApi.lookup() does not return a\n * value either - it returns a deferred reference whose string form is a JSONPath\n * expression resolved when the configuration is applied:\n *\n *     String(ufApi.lookup(\"internet\", { name: \"WAN\" }))\n *       -> \"\\x02/model/internet\\x05$[?(@.name==\\\"WAN\\\")].uniqueId\\x03\"\n *\n * So an add-in is write-only and declarative. It cannot check whether an object\n * exists, cannot diff, and cannot branch on anything the firewall holds. It\n * states the objects that should exist and lets the rollout reconcile them,\n * which is why LANCOM's own examples call createObject unconditionally and pass\n * lookup() straight into the payload.\n *\n * Everything below follows that model. The only branches are on add-in\n * variables, which are known at script time.\n *\n * ---------------------------------------------------------------------------\n * Add-in variables  (LMC: Add-ins > Variables)\n * ---------------------------------------------------------------------------\n *   XTNA_TOKEN       required  Asset creation token from the XplicitTrust console\n *   XTNA_DOMAIN      required  Tenant domain, e.g. example.com\n *   XTNA_NAME        optional  Asset name. When set, -ignore-hostname is added too.\n *\n *   XTNA_BRIDGE      required, PER DEVICE. Name of the docker bridge in\n *                    /model/interfaces, e.g. dockereaecd76f. LCOS FX names it\n *                    \"docker\" + the first 8 hex of the docker network's uniqueId,\n *                    which every firewall generates itself, so a value set at\n *                    project level is wrong on all but one firewall. It cannot be\n *                    derived at script time because the uniqueId is not\n *                    readable. The web client lists it under Network >\n *                    Interfaces > Bridge Interfaces. A value that is not\n *                    docker<8 hex> is ignored.\n *   XTNA_INTERNET    the firewall's Internet object, by exact name (default WAN,\n *                    seen in the field as \"WAN DSL\") or uniqueId. A name that\n *                    matches nothing resolves to null and the upload fails with\n *                    err.general.required on desktop-connections objb.\n *\n *   XTNA_CREATE_NETWORK  on | off (default on)\n *                    Declare the docker network. Turn off only to attach to a\n *                    network that exists and is managed elsewhere. Re-declaring\n *                    an object the firewall already has is reconciled as a PUT\n *                    rather than duplicated - a firewall kept its bridge over\n *                    several rollouts with this on - so the risk of leaving it on\n *                    is not a second network but reconfiguring an existing one\n *                    of the same name, e.g. one with another subnet. While\n *                    XTNA_BRIDGE is empty, only the network is declared - see\n *                    \"Two rollouts\" below.\n *   XTNA_NETWORK     optional  default Docker              (docker network name)\n *   XTNA_SUBNET      the docker network's subnet. Used for the network and the\n *                    desktop object.\n *\n *   XTNA_WAN_ACCESS  on | off (default on) - declare the desktop object and the\n *                    connection to the Internet object\n *   XTNA_SERVICES    optional  default \"internet.https,standard.dns\". Predefined\n *                              service names are category-prefixed. A uniqueId may\n *                              be given instead of a name.\n *\n *   XTNA_PORT_FORWARD  on | off (default off) - fix the agent's WireGuard port\n *                    and forward it from the Internet object to the container,\n *                    so peers connect directly instead of through a relay. See\n *                    \"Port forwarding\" below.\n *   XTNA_PORT        default 51899. The fixed WireGuard port, on the firewall\n *                    and in the container alike.\n *   XTNA_IP          the container's fixed address, needed with port\n *                    forwarding. Empty takes the 10th address of XTNA_SUBNET.\n *\n * No longer offered by the import, still honoured where an older import left\n * them set, so a rollout does not rename or reconfigure what they declared:\n *\n *   XTNA_IMAGE       default cr-public.xplicittrust.com/xtna-agent:latest\n *   XTNA_CONTAINER   default xtna-agent          (container object name)\n *   XTNA_DESKTOP     default \"XTNA container\"    (desktop object name)\n *   XTNA_GATEWAY     docker network gateway; empty lets the firewall pick one\n *                    inside the subnet\n *\n * If neither XTNA_TOKEN nor XTNA_DOMAIN is set, the add-in does nothing on that\n * device, so it is safe to allocate broadly.\n *\n * ---------------------------------------------------------------------------\n * Two rollouts on a firewall without a docker network\n * ---------------------------------------------------------------------------\n * The network and the container cannot arrive in the same rollout. The\n * container is validated against Docker before the network declared next to it\n * exists there, and is rejected with err.general.unspecified. The apply is\n * transactional, so the network goes down with it and the firewall is left\n * unchanged - every retry fails the same way.\n *\n * So XTNA_CREATE_NETWORK=on with XTNA_BRIDGE empty declares the network and\n * nothing else. The bridge name is unknown at that point anyway, which makes an\n * empty XTNA_BRIDGE a reliable sign that the network is not on the device yet:\n *\n *   rollout 1  XTNA_CREATE_NETWORK=on, XTNA_BRIDGE empty -> network only\n *   rollout 2  XTNA_BRIDGE set                          -> container, desktop\n *                                                          object, connection\n *\n * With XTNA_WAN_ACCESS=off the bridge is not otherwise needed, but setting it is\n * still what moves the add-in on to the container.\n *\n * ---------------------------------------------------------------------------\n * Port forwarding\n * ---------------------------------------------------------------------------\n * LCOS FX forwards a port per service rule, and only to a single host object -\n * not to the network object the outbound connection uses. So XTNA_PORT_FORWARD\n * declares, next to the rest:\n *\n *   - the container with a fixed address and -port XTNA_PORT\n *   - a userdefined UDP service to XTNA_PORT, and one from XTNA_PORT to any port\n *   - a host object for the container's address on the bridge\n *   - a connection host <-> Internet object with two rules: incoming\n *     (rightToLeft) to the port with its own NAT settings (useConnection false)\n *     and dmz on, and outgoing (leftToRight) from the port, masqueraded\n *\n * The shapes are copied from objects set up by hand in the web client of a UF\n * and read back from /model (CONTROLLER-3397). dmzport stays null: the external\n * and internal port are the same, which is what the broker announces.\n *\n * The outgoing rule is needed as well: with the forwarding alone a customer's\n * UF classified as \"inbound blocked\", and only once outbound UDP from the port\n * was allowed by hand did the broker see it as reachable. The container's other\n * outbound rule only carries XTNA_SERVICES (https, dns).\n *\n * Off by default because turning it on changes the container's command and\n * address, which updates the container - and an updated container has no\n * outbound access until its rule is saved again in the web client.\n *\n * -port only takes effect when the asset enrols. On an asset that is already\n * enrolled the launcher skips registration, so the port has to be set on the\n * asset itself (Admin Console: Fixate Port) before forwarding is switched on.\n *\n * ---------------------------------------------------------------------------\n * Notes\n * ---------------------------------------------------------------------------\n * - The volume at /etc/XplicitTrust is what makes the asset identity survive a\n *   restart. Without it every recreate enrols a brand new asset. The launcher\n *   only registers when /etc/XplicitTrust/config/selected_config is absent, so\n *   re-declaring -token on an enrolled container is harmless.\n * - XTNA_TOKEN ends up in the container command line, which is stored in the\n *   firewall configuration and included in configuration exports. Use a token\n *   scoped to asset creation and disable it once the rollout is done.\n * - With only https+dns outbound the agent works but every peer connection falls\n *   back to the relay. There is no predefined service covering the WireGuard and\n *   peering UDP ports, so direct peering needs a userdefined service.\n *\n * @param {Config} config\n * @param {Context} context\n * Do not edit this comment or parameter types. Required for code suggestions\n */\nexports.main = function (config, context) {\n\n    var DEFAULTS = {\n        image:     \"cr-public.xplicittrust.com/xtna-agent:latest\",\n        container: \"xtna-agent\",\n        network:   \"Docker\",\n        subnet:    \"10.238.238.0/24\",\n        desktop:   \"XTNA container\",\n        internet:  \"WAN\",\n        services:  \"internet.https,standard.dns\",\n        port:      \"51899\",\n        host:      \"XTNA agent\",\n        udp:       \"XTNA WireGuard\",\n        udpOut:    \"XTNA WireGuard out\"\n    };\n\n    var vars = (context && context.vars) || {};\n\n    function str(v) {\n        if (v === null || v === undefined) { return \"\"; }\n        return String(v).replace(/^\\s+|\\s+$/g, \"\");\n    }\n\n    function isOn(v, fallback) {\n        var text = str(v);\n        if (!text) { return fallback; }\n        return /^(on|true|yes|1)$/i.test(text);\n    }\n\n    function log(message) {\n        config.infoLog(\"xtna-agent: \" + message);\n    }\n\n    // Splits XTNA_SERVICES, dropping empty entries. A blank value - or one that\n    // is nothing but separators - falls back to the default rather than leaving\n    // the connection with no rules at all, which the firewall would reject and\n    // which would take the whole configuration upload down with it.\n    function servicesFrom(value) {\n        var names = [];\n        var parts = (value || \"\").split(\",\");\n        for (var i = 0; i < parts.length; i++) {\n            var part = str(parts[i]);\n            if (part) { names.push(part); }\n        }\n        return names.length ? names : DEFAULTS.services.split(\",\");\n    }\n\n    // IPv4 helpers for XTNA_SUBNET / XTNA_IP. Addresses as unsigned 32-bit\n    // numbers; null for anything that does not parse.\n    function ipToInt(text) {\n        var m = /^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$/.exec(text);\n        if (!m) { return null; }\n        var n = 0;\n        for (var i = 1; i <= 4; i++) {\n            var octet = Number(m[i]);\n            if (octet > 255) { return null; }\n            n = n * 256 + octet;\n        }\n        return n;\n    }\n\n    function intToIp(n) {\n        return [Math.floor(n / 16777216) % 256, Math.floor(n / 65536) % 256,\n                Math.floor(n / 256) % 256, n % 256].join(\".\");\n    }\n\n    // [first, last] address of a CIDR, network and broadcast included.\n    function cidrRange(cidr) {\n        var m = /^([0-9.]+)\\/(\\d{1,2})$/.exec(cidr);\n        var base = m ? ipToInt(m[1]) : null;\n        var bits = m ? Number(m[2]) : 33;\n        if (base === null || bits > 32) { return null; }\n        var size = Math.pow(2, 32 - bits);\n        var first = base - (base % size);\n        return [first, first + size - 1];\n    }\n\n    // The n-th address of the subnet, or \"\" when the subnet is too small to\n    // hold it as a usable host address.\n    function nthAddress(cidr, n) {\n        var range = cidrRange(cidr);\n        if (!range || range[0] + n >= range[1]) { return \"\"; }\n        return intToIp(range[0] + n);\n    }\n\n    function inSubnet(ip, cidr) {\n        var n = ipToInt(ip);\n        var range = cidrRange(cidr);\n        return n !== null && range !== null && n > range[0] && n < range[1];\n    }\n\n    var token  = str(vars.XTNA_TOKEN);\n    var domain = str(vars.XTNA_DOMAIN);\n    var name   = str(vars.XTNA_NAME);\n\n    if (!token && !domain) {\n        log(\"XTNA_TOKEN/XTNA_DOMAIN not assigned on this device - skipping.\");\n        return;\n    }\n    if (!token || !domain) {\n        config.warnLog(\"xtna-agent: XTNA_TOKEN and XTNA_DOMAIN must both be set (got token=\" +\n                       (token ? \"yes\" : \"no\") + \", domain=\" + (domain ? \"yes\" : \"no\") +\n                       \"). Declaring nothing on this device.\");\n        return;\n    }\n    if (/\\s/.test(token) || /\\s/.test(domain) || /\\s/.test(name)) {\n        config.warnLog(\"xtna-agent: XTNA_TOKEN, XTNA_DOMAIN and XTNA_NAME must not contain whitespace - \" +\n                       \"the container command line is split on spaces. Declaring nothing on this device.\");\n        return;\n    }\n\n    var image        = str(vars.XTNA_IMAGE)     || DEFAULTS.image;\n    var ctName       = str(vars.XTNA_CONTAINER) || DEFAULTS.container;\n    var netName      = str(vars.XTNA_NETWORK)   || DEFAULTS.network;\n    var deskName     = str(vars.XTNA_DESKTOP)   || DEFAULTS.desktop;\n    var internetName = str(vars.XTNA_INTERNET)  || DEFAULTS.internet;\n    var bridgeName   = str(vars.XTNA_BRIDGE);\n    var subnet       = str(vars.XTNA_SUBNET)    || DEFAULTS.subnet;\n\n    var createNetwork = isOn(vars.XTNA_CREATE_NETWORK, true);\n    var wantWan       = isOn(vars.XTNA_WAN_ACCESS, true);\n\n    // A uniqueId is accepted wherever a name is looked up, as the escape hatch\n    // when a name does not match what the firewall calls the object.\n    var UUID = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;\n\n    // Only the format can be checked here, not that the bridge exists on this\n    // firewall. A malformed value is treated as empty, so the add-in stops\n    // before the container instead of failing the whole upload on the desktop\n    // object with err.desktop.invalid_interface.\n    if (bridgeName && !/^docker[0-9a-f]{8}$/.test(bridgeName)) {\n        config.warnLog(\"xtna-agent: XTNA_BRIDGE '\" + bridgeName + \"' is not a docker bridge name \" +\n                       \"(docker followed by 8 lowercase hex characters, e.g. dockereaecd76f). Ignoring it.\");\n        bridgeName = \"\";\n    }\n\n    var forward = isOn(vars.XTNA_PORT_FORWARD, false);\n    var port    = str(vars.XTNA_PORT) || DEFAULTS.port;\n    var fixedIp = str(vars.XTNA_IP);\n\n    if (forward) {\n        if (!/^[0-9]{1,5}$/.test(port) || Number(port) < 1 || Number(port) > 65535) {\n            config.warnLog(\"xtna-agent: XTNA_PORT '\" + port + \"' is not a port between 1 and 65535. \" +\n                           \"Declaring nothing on this device.\");\n            return;\n        }\n        fixedIp = fixedIp || nthAddress(subnet, 10);\n        if (!fixedIp || !inSubnet(fixedIp, subnet)) {\n            config.warnLog(\"xtna-agent: port forwarding needs the container's fixed address inside \" +\n                           subnet + \", got XTNA_IP '\" + str(vars.XTNA_IP) + \"'. Declaring nothing on \" +\n                           \"this device.\");\n            return;\n        }\n    }\n\n    var argv = [\"-token\", token, \"-domain\", domain];\n    if (name) {\n        argv.push(\"-name\", name, \"-ignore-hostname\");\n    }\n    if (forward) {\n        argv.push(\"-port\", port);\n    }\n    var command = argv.join(\" \");\n\n    if (command.length > 1024) {\n        config.warnLog(\"xtna-agent: container command line is \" + command.length +\n                       \" characters, the firewall accepts at most 1024. Declaring nothing \" +\n                       \"on this device.\");\n        return;\n    }\n\n    var ufApi = config.getUfApi();\n\n    // A reference resolved when the configuration is applied, not now.\n    function ref(entity, criteria) {\n        return String(ufApi.lookup(entity, criteria));\n    }\n\n    /* ------------------------------------------------------------------ *\n     * 1. docker network - only when asked for\n     * ------------------------------------------------------------------ */\n\n    if (createNetwork) {\n        // Only name and subnet are required. A default gateway would silently\n        // contradict a changed subnet - .254 of a /24 that no longer exists -\n        // and the firewall would reject the network, discarding the whole\n        // upload. Left unset, the firewall picks one inside the subnet.\n        var gateway = str(vars.XTNA_GATEWAY);\n        var network = { name: netName, subnet: subnet };\n        if (gateway) { network.gateway = gateway; }\n\n        ufApi.createObject(\"docker-network-settings\", network);\n        log(\"declared docker network '\" + netName + \"' (\" + subnet +\n            (gateway ? \", gateway \" + gateway : \", gateway chosen by the firewall\") + \")\");\n\n        if (!bridgeName) {\n            config.warnLog(\"xtna-agent: XTNA_BRIDGE is empty, so this rollout declares only the docker \" +\n                           \"network - the container cannot be created in the same rollout as its network. \" +\n                           \"Once it is applied, set XTNA_BRIDGE to the bridge shown under Network > \" +\n                           \"Interfaces > Bridge Interfaces (docker<8 hex>) on this device only - every \" +\n                           \"firewall has its own - and roll out again.\");\n            return;\n        }\n    } else {\n        log(\"using existing docker network '\" + netName + \"' (XTNA_CREATE_NETWORK is off)\");\n    }\n\n    // Checked before the container, not after it: a container without its\n    // outbound rule starts with no internet, and the launcher does not reliably\n    // recover once access arrives later (CONTROLLER-3362). Not an abort either -\n    // aborting discards the whole config build for the device. Skipping leaves\n    // the device as it was, which the next rollout repairs.\n    if (wantWan && !bridgeName) {\n        config.warnLog(\"xtna-agent: XTNA_WAN_ACCESS is on but XTNA_BRIDGE is empty, so the container \" +\n                       \"would have no outbound rule and could not enrol. Declaring no container. Set \" +\n                       \"XTNA_BRIDGE to the bridge shown under Network > Interfaces > Bridge Interfaces \" +\n                       \"(docker<8 hex>) on this device only and roll out again. It cannot be derived \" +\n                       \"because add-ins cannot read device state.\");\n        return;\n    }\n    if (forward && !bridgeName) {\n        config.warnLog(\"xtna-agent: XTNA_PORT_FORWARD is on but XTNA_BRIDGE is empty, and the host object \" +\n                       \"the port is forwarded to needs the bridge. Declaring no container. Set XTNA_BRIDGE \" +\n                       \"on this device and roll out again.\");\n        return;\n    }\n\n    /* ------------------------------------------------------------------ *\n     * 2. agent container\n     * ------------------------------------------------------------------ */\n\n    var attachment = { uniqueId: ref(\"docker-network-settings\", { name: netName }) };\n    if (fixedIp) {\n        attachment.ip = fixedIp;\n    }\n\n    ufApi.createObject(\"docker-container-settings\", {\n        active:       true,\n        name:         ctName,\n        image:        image,\n        command:      command,\n        capabilities: [\"NET_ADMIN\", \"NET_RAW\"],\n        volumes:      [\"/etc/XplicitTrust\"],\n        networks:     [attachment],\n        policy:       { restart: { condition: \"always\" } }\n    });\n    log(\"declared container '\" + ctName + \"' from \" + image +\n        (name ? \" as asset '\" + name + \"'\" : \" (asset name from the container id)\") +\n        (fixedIp ? \" at \" + fixedIp : \"\") + (forward ? \" with WireGuard port \" + port : \"\"));\n\n    var internetRef = UUID.test(internetName) ? internetName : ref(\"internet\", { name: internetName });\n\n    /* ------------------------------------------------------------------ *\n     * 3. port forwarding to the container - only when asked for\n     * ------------------------------------------------------------------ */\n\n    if (forward) {\n        ufApi.createObject(\"userdefined-services\", {\n            name:        DEFAULTS.udp,\n            userdefined: true,\n            services:    [{\n                protocol:    [ref(\"predefined-protocols\", { name: \"UDP\" })],\n                portfrom:    Number(port),\n                portto:      Number(port),\n                srcportfrom: 0,\n                srcportto:   0,\n                noproxy:     true\n            }]\n        });\n\n        // From the WireGuard port to any port: peers listen on ports of their\n        // own, and the agent initiates handshakes too.\n        ufApi.createObject(\"userdefined-services\", {\n            name:        DEFAULTS.udpOut,\n            userdefined: true,\n            services:    [{\n                protocol:    [ref(\"predefined-protocols\", { name: \"UDP\" })],\n                portfrom:    1,\n                portto:      65535,\n                srcportfrom: Number(port),\n                srcportto:   Number(port),\n                noproxy:     true\n            }]\n        });\n\n        // The bridge NAME again, as for the network object below.\n        ufApi.createObject(\"hosts\", {\n            name:      DEFAULTS.host,\n            interface: bridgeName,\n            ipv4:      fixedIp,\n            icon:      \"host\"\n        });\n\n        // Incoming: useConnection false gives the rule its own NAT settings,\n        // where dmz is the \"Enable DMZ / Port Forwarding\" box. No masquerading\n        // towards the container: it should see the peer's real address.\n        // Outgoing: masqueraded like the container's other outbound rule.\n        ufApi.createObject(\"desktop-connections\", {\n            obja:           ref(\"hosts\", { name: DEFAULTS.host }),\n            objb:           internetRef,\n            natactive:      \"left\",\n            trafficShaping: {},\n            rules:          [{\n                uniqueId:      ref(\"userdefined-services\", { name: DEFAULTS.udp }),\n                action:        \"rightToLeft\",\n                useConnection: false,\n                natactive:     \"none\",\n                dmz:           true,\n                dmzport:       null,\n                dmzip:         \"\"\n            }, {\n                uniqueId:  ref(\"userdefined-services\", { name: DEFAULTS.udpOut }),\n                action:    \"leftToRight\",\n                natactive: \"left\"\n            }]\n        });\n        log(\"declared port forwarding of UDP \" + port + \" from '\" + internetName + \"' to host '\" +\n            DEFAULTS.host + \"' (\" + fixedIp + \") and outbound UDP from port \" + port + \". The asset \" +\n            \"must use port \" + port + \": -port only applies at enrolment, so use Fixate Port in the \" +\n            \"Admin Console for an asset that is already enrolled.\");\n    }\n\n    /* ------------------------------------------------------------------ *\n     * 4. outbound access for the container network\n     * ------------------------------------------------------------------ */\n\n    if (!wantWan) {\n        log(\"XTNA_WAN_ACCESS=off - not declaring desktop objects.\");\n        return;\n    }\n\n    // The interface NAME, deliberately - not a lookup reference like every other\n    // cross-object field here. The schema types this field as a bare string and\n    // accepts either form, so nothing rejects the wrong one; it simply produces\n    // an object that generates no rules, and the container silently has no\n    // outbound access.\n    //\n    // Two reasons the name is the right form. Setting a uniqueId here was\n    // reproduced on a directly-managed firewall as \"container has no rules and\n    // no internet\", where the same object with a name worked. And an interface's\n    // uniqueId is not stable - the same bridge name resolved to 828061ef... and\n    // then 4dfca796... two weeks apart - so a stored id goes stale whenever the\n    // bridge is recreated, which is exactly what happens when the docker network\n    // is rebuilt.\n    //\n    // Do not \"fix\" this into ref(\"interfaces\", { name: bridgeName }). That\n    // resolves to a uniqueId at apply time; the payload reaching the device was\n    // observed to carry one.\n    ufApi.createObject(\"networks\", {\n        name:      deskName,\n        interface: bridgeName,\n        ipv4:      subnet,\n        icon:      \"network\"\n    });\n    log(\"declared desktop object '\" + deskName + \"' on bridge '\" + bridgeName + \"' (\" + subnet + \")\");\n\n    var rules = [];\n    var named = [];\n\n    // An entry may be a service name or a literal uniqueId. Names are resolved by\n    // reference at apply time and silently become null if nothing matches, which\n    // fails validation with err.general.required.\n    var wantedServices = servicesFrom(str(vars.XTNA_SERVICES));\n\n    for (var i = 0; i < wantedServices.length; i++) {\n        var svcName = wantedServices[i];\n        if (!svcName) { continue; }\n        rules.push({\n            uniqueId:  UUID.test(svcName) ? svcName : ref(\"predefined-services\", { name: svcName }),\n            action:    \"leftToRight\",\n            natactive: \"left\"\n        });\n        named.push(svcName);\n    }\n\n    // natactive \"left\" masquerades the container side. The docker subnet is not\n    // routable upstream, so without it packets leave with a 10.x source and\n    // nothing comes back.\n    ufApi.createObject(\"desktop-connections\", {\n        obja:           ref(\"networks\", { name: deskName }),\n        objb:           internetRef,\n        natactive:      \"left\",\n        trafficShaping: {},\n        rules:          rules\n    });\n    log(\"declared connection '\" + deskName + \"' <-> '\" + internetName + \"' allowing \" +\n        named.join(\", \") + \" outbound. If the upload fails with err.general.required on objb, \" +\n        \"no Internet object is named '\" + internetName + \"': set XTNA_INTERNET to its exact name.\");\n};\n"
      },
      "application": {
        "type": "SCRIPT",
        "enabled": true,
        "resetToDefault": false,
        "usedForOneTimeExecution": false
      },
      "applicationVariables": {
        "XTNA_TOKEN": {
          "restricted": false
        },
        "XTNA_DOMAIN": {
          "restricted": false
        },
        "XTNA_NAME": {
          "restricted": false
        },
        "XTNA_BRIDGE": {
          "restricted": false
        },
        "XTNA_INTERNET": {
          "restricted": false
        },
        "XTNA_CREATE_NETWORK": {
          "restricted": false
        },
        "XTNA_NETWORK": {
          "restricted": false
        },
        "XTNA_SUBNET": {
          "restricted": false
        },
        "XTNA_WAN_ACCESS": {
          "restricted": false
        },
        "XTNA_SERVICES": {
          "restricted": false
        },
        "XTNA_PORT_FORWARD": {
          "restricted": false
        },
        "XTNA_PORT": {
          "restricted": false
        },
        "XTNA_IP": {
          "restricted": false
        }
      }
    }
  },
  "variableTypes": {
    "XTNA_OnOff": {
      "type": "OPTIONS",
      "options": [
        {
          "value": "on",
          "displayName": "on"
        },
        {
          "value": "off",
          "displayName": "off"
        }
      ]
    },
    "XTNA_Domain": {
      "type": "REGEX",
      "regex": "^$|^(?=.{1,253}$)([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,63}$"
    },
    "XTNA_CIDR": {
      "type": "REGEX",
      "regex": "^$|^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])/([1-9]|[12]\\d|3[01])$"
    },
    "XTNA_Bridge": {
      "type": "REGEX",
      "regex": "^$|^docker[0-9a-f]{8}$"
    },
    "XTNA_Port": {
      "type": "REGEX",
      "regex": "^$|^([1-9]\\d{0,3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])$"
    },
    "XTNA_IPv4": {
      "type": "REGEX",
      "regex": "^$|^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"
    }
  }
}
