import { podOwnerLookup, toShortResourceName } from './Utils.js';
import BaseTable from '../BaseTable.jsx';
import Grid from '@material-ui/core/Grid';
import { Link } from 'react-router-dom';
import OpenInNewIcon from '@material-ui/icons/OpenInNew';
import Popover from '../Popover.jsx';
import PropTypes from 'prop-types';
import React from 'react';
import TapLink from '../TapLink.jsx';
import Tooltip from '@material-ui/core/Tooltip';
import _each from 'lodash/each';
import _get from 'lodash/get';
import _has from 'lodash/has';
import _isEmpty from 'lodash/isEmpty';
import _isNil from 'lodash/isNil';
import _map from 'lodash/map';
import _merge from 'lodash/merge';
import _size from 'lodash/size';
import _take from 'lodash/take';
export const httpMethods = ["GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH"];
export const defaultMaxRps = "100.0";
export const setMaxRps = query => {
if (!_isEmpty(query.maxRps)) {
query.maxRps = parseFloat(query.maxRps);
} else {
query.maxRps = 0; // golang unset value for maxRps
}
};
// use a generator to get this object, to prevent it from being overwritten
export const emptyTapQuery = () => ({
resource: "",
namespace: "",
toResource: "",
toNamespace: "",
method: "",
path: "",
scheme: "",
authority: "",
maxRps: ""
});
export const tapQueryProps = {
resource: PropTypes.string,
namespace: PropTypes.string,
toResource: PropTypes.string,
toNamespace: PropTypes.string,
method: PropTypes.string,
path: PropTypes.string,
scheme: PropTypes.string,
authority: PropTypes.string,
maxRps: PropTypes.string
};
export const tapQueryPropType = PropTypes.shape(tapQueryProps);
// from https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
export const wsCloseCodes = {
1000: "Normal Closure",
1001: "Going Away",
1002: "Protocol Error",
1003: "Unsupported Data",
1004: "Reserved",
1005: "No Status Recvd",
1006: "Abnormal Closure",
1007: "Invalid frame payload data",
1008: "Policy Violation",
1009: "Message too big",
1010: "Missing Extension",
1011: "Internal Error",
1012: "Service Restart",
1013: "Try Again Later",
1014: "Bad Gateway",
1015: "TLS Handshake"
};
/*
Use tap data to figure out a resource's unmeshed upstreams/downstreams
*/
export const processNeighborData = (source, labels, resourceAgg, resourceType) => {
if (_isEmpty(labels)) {
return resourceAgg;
}
let neighb = {};
if (_has(labels, resourceType)) {
neighb = {
type: resourceType,
name: labels[resourceType],
namespace: labels.namespace
};
} else if (_has(labels, "pod")) {
neighb = {
type: "pod",
name: labels.pod,
namespace: labels.namespace
};
} else {
neighb = {
type: "ip",
name: source.str
};
}
// keep track of pods under this resource to display the number of unmeshed source pods
neighb.pods = {};
if (labels.pod) {
neighb.pods[labels.pod] = true;
}
let key = neighb.type + "/" + neighb.name;
if (_has(labels, "control_plane_ns")) {
delete resourceAgg[key];
} else {
if (_has(resourceAgg, key)) {
_merge(neighb.pods, resourceAgg[key].pods);
}
resourceAgg[key] = neighb;
}
return resourceAgg;
};
export const processTapEvent = jsonString => {
let d = JSON.parse(jsonString);
d.source.str = publicAddressToString(_get(d, "source.ip.ipv4"));
d.source.pod = _get(d, "sourceMeta.labels.pod", null);
d.source.owner = extractPodOwner(d.sourceMeta.labels);
d.source.namespace = _get(d, "sourceMeta.labels.namespace", null);
d.destination.str = publicAddressToString(_get(d, "destination.ip.ipv4"));
d.destination.pod = _get(d, "destinationMeta.labels.pod", null);
d.destination.owner = extractPodOwner(d.destinationMeta.labels);
d.destination.namespace = _get(d, "destinationMeta.labels.namespace", null);
switch (d.proxyDirection) {
case "INBOUND":
d.tls = _get(d, "sourceMeta.labels.tls", "");
break;
case "OUTBOUND":
d.tls = _get(d, "destinationMeta.labels.tls", "");
break;
default:
// too old for TLS
}
if (_isNil(d.http)) {
this.setState({ error: "Undefined request type"});
} else {
if (!_isNil(d.http.requestInit)) {
d.eventType = "requestInit";
} else if (!_isNil(d.http.responseInit)) {
d.eventType = "responseInit";
} else if (!_isNil(d.http.responseEnd)) {
d.eventType = "responseEnd";
}
d.id = tapEventKey(d, d.eventType);
}
return d;
};
/*
Use this key to associate the corresponding response with the request
so that we can have one single event with reqInit, rspInit and rspEnd
current key: (src, dst, stream)
*/
const tapEventKey = (d, eventType) => {
return `${d.source.str},${d.destination.str},${_get(d, ["http", eventType, "id", "stream"])}`;
};
/*
produce octets given an ip address
*/
const decodeIPToOctets = ip => {
ip = parseInt(ip, 10);
return [
ip >> 24 & 255,
ip >> 16 & 255,
ip >> 8 & 255,
ip & 255
];
};
/*
converts an address to an ipv4 formatted host
*/
const publicAddressToString = ipv4 => {
let octets = decodeIPToOctets(ipv4);
return octets.join(".");
};
/*
display more human-readable information about source/destination
*/
const resourceShortLink = (resourceType, labels, ResourceLink) => (