def __init__(self, timeout=10, numRetries=3, verify=True, serverCertFile=os.path.join(os.path.dirname(__file__), 'certificates', 'devserver.cer'), clientPFXFile=os.path.join(os.path.dirname(__file__), 'certificates', 'devclient.pfx'), PFXPasswd='portknocking'): """ This function initializes the Port-Knocking client library \"knock\" Set context parameters and load required certificates timeout: Time in seconds to wait between retries. Default: 10 numRetries: Number of Retries. Default: 3 verify: Verify if the target Port was successfully opened. Only TCP is supported. Default: True serverCertFile: Path to the Server Certificate File encoded in DER. Default: certificates/devserver.cer clientPFXFile: Path to the Client Certificate with Private Key in PKCS#7 Format (.pfx). Default: certificates/devclient.pfx PFXPasswd: Password to decrypt @clientPFXFile """ self.connectionHandler = Connection(Security(clientPFXFile, PFXPasswd, serverCertFile), timeout, numRetries, verify)
def test_connection_get(self, setup_vcenter, rest_yaml, vmon_json): api_target = rest_yaml['vmonservice']['api_target'] con = Connection(setup_vcenter) response = con.get_request(api_target) assert response is False, 'Response fail expected, because there was no previous login (SessionID missing).' con.login() response = con.get_request(api_target) assert vmon_json == response, 'Identical vmon json data expected.'
def test_connection_post(self, setup_vcenter, rest_yaml, logging_json): api_target = rest_yaml['logging']['api_target'] data = rest_yaml['logging']['action'] con = Connection(setup_vcenter) response = con.post_request(api_target, data) assert response is False, 'Response fail expected, because there was no previous login (SessionID missing).' con.login() response = con.post_request(api_target, data) assert logging_json == response, 'Identical logging json data expected.'
class Vcenter: def __init__(self, name, mpw, user, password): self.user = user self.mpw = mpw self.pw = password if not password: self.pw_handle = self.generate_pw_handle() self.pw = self.generate_pw(name).replace('/', '') self.name = name @staticmethod def get_vcs_from_atlas(atlas_endpoint): response = requests.get(url=atlas_endpoint) netbox_json = response.json() vcenter_list = list() for target in netbox_json: if target['labels']['job'] == "vcenter": vcenter = target['labels']['server_name'] vcenter_list.append(vcenter) return vcenter_list def generate_pw_handle(self): return master_password.MPW(self.user, self.mpw) def generate_pw(self, url): if self.pw: return self.pw return self.pw_handle.password(url) def login(self): self.con = Connection(self) self.con.login() def logout(self): if self.con.session_id: self.con.logout()
def collect(self): for vc in self.vcenter.vcenter_list: g = GaugeMetricFamily('vcsa_service_status', 'Status of vCSA Services', labels=['service', 'vccluster']) rest_yaml = self.read_rest_yaml() api_target = rest_yaml['vmonservice']['api_target'] # TODO: move session handling to base class. implement reuse of sessions session_id = Connection.login(vc, self.vcenter.user, self.vcenter.generate_pw(vc)) if not session_id: print("skipping vc", vc, ", login not possible") continue fetched_data = Connection.get_request(vc, api_target, session_id) Connection.logout(vc, session_id) if not fetched_data: print("skipping vc", vc, "fetched data did not return anything") continue services = dict() for service in fetched_data['value']: service_name = service['key'] state = service['value']['state'] if state == "STOPPED": service_health = "STOPPED" else: service_health = service['value']['health'] g.add_metric(labels=[service_name, vc], value=self.health_states[service_health]) yield g
class ClientInterface: def __init__(self, timeout=10, numRetries=3, verify=True, serverCertFile=os.path.join(os.path.dirname(__file__), 'certificates', 'devserver.cer'), clientPFXFile=os.path.join(os.path.dirname(__file__), 'certificates', 'devclient.pfx'), PFXPasswd='portknocking'): """ This function initializes the Port-Knocking client library \"knock\" Set context parameters and load required certificates timeout: Time in seconds to wait between retries. Default: 10 numRetries: Number of Retries. Default: 3 verify: Verify if the target Port was successfully opened. Only TCP is supported. Default: True serverCertFile: Path to the Server Certificate File encoded in DER. Default: certificates/devserver.cer clientPFXFile: Path to the Client Certificate with Private Key in PKCS#7 Format (.pfx). Default: certificates/devclient.pfx PFXPasswd: Password to decrypt @clientPFXFile """ self.connectionHandler = Connection(Security(clientPFXFile, PFXPasswd, serverCertFile), timeout, numRetries, verify) def knockOnPort(self, host, port, protocol=PROTOCOL.TCP, knockPort = random.randint(MIN_PORT, MAX_PORT), forceIPv4 = False, clientIP = None): """ Actual port-knocking function Generate port-knocking packet for opening the requested @port on @host. Can be used to create TCP or UDP connections; Defaults to TCP connection if @protocol is not given. After sending the port-knocking request verifies that the target @port is open, and if necessary retries the port-knocking host: Target @host, on which the application is running port: Port to open on target @host protocol: Requested Target Protocol. Default: TCP knockPort: (Optional) Port to use for port-knocking request. Default: Random Port between MIN_PORT and MAX_PORT forceIPv4: (Optional) Force port-knocking via IPv4, even when IPv6 is available. Default: false For TCP, this function returns True if the port is successfully opened or False if it failed. For UDP, it always returns True """ LOG.debug('Knocking %s on port %s', host, port) return self.connectionHandler.knockOnPort(host, port, protocol, knockPort, forceIPv4, clientIP)
def login(self): self.con = Connection(self) self.con.login()
from modules.Connection import Connection c = Connection() c.create('airplanes') c.load_csv_data('airplanes', 'csv_data/airplanes.csv') c.create("planes") c.load_csv_data('flights', "csv_data/planes.csv") c.create('airports') c.load_csv_data('airports', "csv_data/airports.csv") c.create("flights") c.load_csv_data('flights', "csv_data/flights.csv") c.create("weather") c.load_csv_data('weather', "csv_data/weather.csv")
def test_logout_failure(self, setup_vcenter): connection = Connection(setup_vcenter) connection.session_id = 'False' response = connection.logout() assert response is False, 'Expected the response "False" for a failed logout.'
def test_login_failure(self, setup_vcenter): connection = Connection(setup_vcenter) connection.pw = 'False' response = connection.login() assert response is False, 'Expected the response "False" for a failed login.'
def test_logout_success(self, setup_vcenter): connection = Connection(setup_vcenter) connection.login() response = connection.logout() assert response is True, 'Expected the response "True" for a successful logout.'
from flask import Flask,jsonify from modules.Connection import Connection from flask_cors import CORS, cross_origin app = Flask(__name__) cors = CORS(app) app.config['CORS_HEADERS'] = 'Content-Type' conn = Connection() @app.route('/', methods=['GET']) def index(): return jsonify(conn.query("SELECT * FROM airports")) @app.route('/airport-number', methods=['GET']) def AirportNumber(): return conn.query("SELECT COUNT(faa) as `airport_number` FROM airports",multiple=False) @app.route('/carrier-number', methods=['GET']) def CarrierNumber(): return conn.query("SELECT COUNT(DISTINCT(carrier)) as `carrier_number` FROM airlines", multiple=False) @app.route('/destination-number', methods=['GET']) def DestinationNumber(): return conn.query("SELECT COUNT(DISTINCT(dest)) as `dest_number` FROM flights",multiple=False) @app.route('/plane-number', methods=['GET']) def PlaneNumber(): return conn.query("SELECT COUNT(tailnum) as `plane_number` FROM planes", multiple=False)