<commit_before>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/*
 * Copyright 2016, Joyent, Inc.
 */

var assert = require('assert-plus');
var format = require('util').format;
var mod_fwrule = require('fwrule');
var net = require('net');
var restify = require('restify');
var triton_tags = require('triton-tags');

var common = require('../../common');
var constants = require('../../constants');
var Link = require('../../models/link');



function vmUuidToShortDockerId(uuid) {
    return uuid.replace(/-/g, '').slice(0, 12);
}

/**
 * Docker containers are stored on the VM "tags" field
 * (https://github.com/joyent/sdc-vmapi/blob/master/docs/index.md#vm-metadata).
 * General docker labels are prefixed with 'docker:label:' (AFAIK the only
 * need for that separation is the use of the 'sdc_docker' tag for fwrules).
 *
 * In addition, the special structured 'triton.'-prefixed tags are exposed
 * as Docker labels (see DOCKER-728).
 *
 * Note: Docker label values must be strings otherwise the docker 1.10 client
 * will blow up. See https://github.com/docker/docker/issues/20881 and
 * DOCKER-721. Currently VM.tags are limited to string, boolean or number,
 * so we can simply `.toString()`.
 */
function dockerLabelsFromVmTags(tags) {
    const expectedTypes = {string: true, boolean: true, number: true};
    var labels = {};
    const LABELTAG_PREFIX = common.LABELTAG_PREFIX;

    var strFromLabelVal = function (val) {
        if (!expectedTypes[typeof (val)]) {
            throw new Error(format(
                'unexpected VM tag value type: val=%j, type=%j',
                val, typeof (val)));
        }
        return val.toString();
    };

    Object.keys(tags).forEach(function (key) {
        if (key.substr(0, LABELTAG_PREFIX.length) === LABELTAG_PREFIX) {
            labels[key.substr(LABELTAG_PREFIX.length)]
                = strFromLabelVal(tags[key]);
        }
    });

    /*
     * Add the 'triton.' tags last so they 'win' over (obsolete, legacy)
     * 'docker:label:triton.'.
     */
    Object.keys(tags).forEach(function (key) {
        if (triton_tags.isTritonTag(key)) {
            labels[key] = strFromLabelVal(tags[key]);
        }
    });

    return labels;
}


function getPublishingRules(opts, fwrules) {
    var log = opts.log;
    var uuid = opts.vm.uuid;
    return fwrules.reduce(function (published, rule) {
        var fwrule;
        try {
            fwrule = new mod_fwrule.FwRule(rule);
        } catch (e) {
            log.error(e, {
                rule: rule
            }, 'Failed to parse rule while producing inspect info');
            return published;
        }

        // We only process open TCP and UDP ports that go to our VM
        if (fwrule.protocol !== 'tcp' && fwrule.protocol !== 'udp') {
            return published;
        }

        if (fwrule.action !== 'allow'
            || fwrule.to.vms.indexOf(uuid) === -1
            || fwrule.from.wildcards.indexOf('any') === -1) {
            return published;
        }

        published.push(fwrule);
        return published;
    }, []);
}

/*
 * With Docker 1.10 (API version 1.22) the filters changed from an array to an
 * object with the value true.
 *
 * In 1.9.1:
 *
 *     filters={"label":["com.joyent.package=sample-2G", "foo=bar"]}
 *
 * In 1.10.0:
 *
 *     filters={"label":{"com.joyent.package=sample-2G":true, "foo=bar":true}}
 *
 * This function accepts either format and returns an object in the 1.9.1
 * (array) format.
 */
function getNormalizedFilters(filters) {
    assert.object(filters, 'filters');

    var i;
    var name;<commit_msg>DOCKER-760 Firewall rules with PORT ALL don't behave well with Docker Reviewed by: <NAME> <<EMAIL>><commit_after>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/*
 * Copyright 2016, Joyent, Inc.
 */

var assert = require('assert-plus');
var format = require('util').format;
var mod_fwrule = require('fwrule');
var net = require('net');
var restify = require('restify');
var triton_tags = require('triton-tags');

var common = require('../../common');
var constants = require('../../constants');
var Link = require('../../models/link');



function vmUuidToShortDockerId(uuid) {
    return uuid.replace(/-/g, '').slice(0, 12);
}

/**
 * Docker containers are stored on the VM "tags" field
 * (https://github.com/joyent/sdc-vmapi/blob/master/docs/index.md#vm-metadata).
 * General docker labels are prefixed with 'docker:label:' (AFAIK the only
 * need for that separation is the use of the 'sdc_docker' tag for fwrules).
 *
 * In addition, the special structured 'triton.'-prefixed tags are exposed
 * as Docker labels (see DOCKER-728).
 *
 * Note: Docker label values must be strings otherwise the docker 1.10 client
 * will blow up. See https://github.com/docker/docker/issues/20881 and
 * DOCKER-721. Currently VM.tags are limited to string, boolean or number,
 * so we can simply `.toString()`.
 */
function dockerLabelsFromVmTags(tags) {
    const expectedTypes = {string: true, boolean: true, number: true};
    var labels = {};
    const LABELTAG_PREFIX = common.LABELTAG_PREFIX;

    var strFromLabelVal = function (val) {
        if (!expectedTypes[typeof (val)]) {
            throw new Error(format(
                'unexpected VM tag value type: val=%j, type=%j',
                val, typeof (val)));
        }
        return val.toString();
    };

    Object.keys(tags).forEach(function (key) {
        if (key.substr(0, LABELTAG_PREFIX.length) === LABELTAG_PREFIX) {
            labels[key.substr(LABELTAG_PREFIX.length)]
                = strFromLabelVal(tags[key]);
        }
    });

    /*
     * Add the 'triton.' tags last so they 'win' over (obsolete, legacy)
     * 'docker:label:triton.'.
     */
    Object.keys(tags).forEach(function (key) {
        if (triton_tags.isTritonTag(key)) {
            labels[key] = strFromLabelVal(tags[key]);
        }
    });

    return labels;
}


function getPublishingRules(opts, fwrules) {
    var log = opts.log;
    var uuid = opts.vm.uuid;
    return fwrules.reduce(function (published, rule) {
        var fwrule;
        try {
            fwrule = new mod_fwrule.FwRule(rule);
        } catch (e) {
            log.error(e, {
                rule: rule
            }, 'Failed to parse rule while producing inspect info');
            return published;
        }

        // We only process open TCP and UDP ports that go to our VM
        if (fwrule.protocol !== 'tcp' && fwrule.protocol !== 'udp') {
            return published;
        }

        if (fwrule.action !== 'allow'
            || fwrule.to.vms.indexOf(uuid) === -1
            || fwrule.ports.indexOf('all') !== -1
            || fwrule.from.wildcards.indexOf('any') === -1) {
            return published;
        }

        published.push(fwrule);
        return published;
    }, []);
}

/*
 * With Docker 1.10 (API version 1.22) the filters changed from an array to an
 * object with the value true.
 *
 * In 1.9.1:
 *
 *     filters={"label":["com.joyent.package=sample-2G", "foo=bar"]}
 *
 * In 1.10.0:
 *
 *     filters={"label":{"com.joyent.package=sample-2G":true, "foo=bar":true}}
 *
 * This function accepts either format and returns an object in the 1.9.1
 * (array) format.
 */
function getNormalizedFilters(filters) {
    assert.object(filters, 'filters');

    var i;
    var name;
{'fraction_non_alphanumeric': 0.14190426366727701, 'fraction_numerical': 0.012555584619408842, 'mean_word_length': 2.5683621091927207, 'pattern_counts': {'":': 8, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3275885', 'n_tokens_mistral': 2474, 'n_tokens_neox': 2338, 'n_words': 679}