forked from MathieuLamiot/TechTeamBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from wp-media/develop
Release Slack command /wprocket-ips
- Loading branch information
Showing
8 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
""" | ||
This module defines the factory for OVH API | ||
""" | ||
from flask import current_app | ||
import sources.utils.IpAddress as IpAddress | ||
import sources.utils.Constants as cst | ||
import ovh | ||
|
||
|
||
class OvhApiFactory(): | ||
""" | ||
Class managing the API for OVH | ||
""" | ||
def __init__(self): | ||
""" | ||
The factory instanciates the objects it needed to complete the processing of the request. | ||
""" | ||
self.client = None | ||
|
||
def _get_ovh_client(self, app_context): | ||
""" | ||
Return the ovh client and creates it if needed | ||
""" | ||
if self.client is None: | ||
app_context.push() | ||
self.client = ovh.Client( | ||
endpoint='ovh-eu', # Endpoint of API OVH Europe (List of available endpoints) | ||
application_key=current_app.config[cst.APP_CONFIG_TOKEN_OVH_APP_KEY], | ||
application_secret=current_app.config[cst.APP_CONFIG_TOKEN_OVH_APP_SECRET], | ||
consumer_key=current_app.config[cst.APP_CONFIG_TOKEN_OVH_CONSUMER_KEY], | ||
) | ||
return self.client | ||
|
||
def get_dedicated_servers(self, app_context): | ||
""" | ||
Retrieves the list of dedicated servers available | ||
""" | ||
client = self._get_ovh_client(app_context) | ||
result = client.get('/dedicated/server', iamTags=None) | ||
return result | ||
|
||
def get_dedicated_server_display_name(self, app_context, server_name): | ||
""" | ||
Returns display_name of the dedicated server. | ||
""" | ||
client = self._get_ovh_client(app_context) | ||
service_info = client.get(f'/dedicated/server/{server_name}/serviceInfos') | ||
service_id = service_info["serviceId"] | ||
service = client.get(f'/service/{service_id}') | ||
display_name = service["resource"]["displayName"] | ||
return display_name | ||
|
||
def get_dedicated_server_ips(self, app_context, server_name): | ||
""" | ||
Return the IPv6 and IPv4 of a dedicated server | ||
""" | ||
client = self._get_ovh_client(app_context) | ||
raw_result = client.get(f'/dedicated/server/{server_name}/ips') | ||
result = dict() | ||
for ip in raw_result: | ||
ip_split = ip.split("/") | ||
result[IpAddress.validIPAddress(ip_split[0])] = ip | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
""" | ||
This module defines the handler for logic related to listing server IPs. | ||
""" | ||
from sources.factories.SlackMessageFactory import SlackMessageFactory | ||
from sources.factories.OvhApiFactory import OvhApiFactory | ||
import sources.utils.IpAddress as IpAddress | ||
|
||
|
||
class ServerListHandler(): | ||
""" | ||
Class managing the business logic related to listing servers WP Media uses. | ||
""" | ||
def __init__(self): | ||
""" | ||
The handler instanciates the objects it needed to complete the processing of the request. | ||
""" | ||
self.slack_message_factory = SlackMessageFactory() | ||
self.ovh_api_factory = OvhApiFactory() | ||
|
||
def send_wp_rocket_ips(self, app_context, slack_user): | ||
""" | ||
List all IPs used for WP Rocket and sends it in a Slack DM | ||
""" | ||
text = "List of IPs used for WP Rocket:\n\n" | ||
|
||
text += "License validation/activation, update check, plugin information:\n" | ||
# Defined in https://gitlab.one.com/systems/group.one-authdns/-/blob/main/octodns/wp-rocket.me.yaml?ref_type=heads | ||
text += "https://wp-rocket.me / 185.10.9.101\n" | ||
text += "\n" | ||
|
||
text += "Load CSS Asynchronously:\n" | ||
# Defined in https://gitlab.one.com/systems/group.one-authdns/-/blob/main/octodns/wp-rocket.me.yaml?ref_type=heads | ||
text += "https://cpcss.wp-rocket.me / 46.30.212.116\n" | ||
# Defined in k8s_sips: https://gitlab.one.com/systems/chef-repo/-/blob/master/roles/onecom-global-firewall-macros.json#L173 | ||
text += "46.30.212.64\n46.30.212.65\n46.30.212.66\n46.30.212.67\n46.30.212.68\n46.30.212.69\n46.30.211.85\n" | ||
text += "\n" | ||
|
||
text += "Remove Unused CSS:\n" | ||
# Defined in k8s_sips: https://gitlab.one.com/systems/chef-repo/-/blob/master/roles/onecom-global-firewall-macros.json#L173 | ||
text += "46.30.212.64\n46.30.212.65\n46.30.212.66\n46.30.212.67\n46.30.212.68\n46.30.212.69\n46.30.211.85\n" | ||
# OVH servers | ||
all_server_list = self.ovh_api_factory.get_dedicated_servers(app_context) | ||
for server_name in all_server_list: | ||
display_name = self.ovh_api_factory.get_dedicated_server_display_name(app_context, server_name) | ||
if 'worker' in display_name: | ||
server_ips = self.ovh_api_factory.get_dedicated_server_ips(app_context, server_name) | ||
text += server_ips[IpAddress.IP_ADDRESS_IPV4] + " / " + server_ips[IpAddress.IP_ADDRESS_IPV6] + "\n" | ||
text += "\n" | ||
|
||
text += "Dynamic exclusions and inclusions:\n" | ||
# Defined in https://gitlab.one.com/systems/group.one-authdns/-/blob/main/octodns/wp-rocket.me.yaml?ref_type=heads | ||
text += "https://b.rucss.wp-rocket.me / 46.30.212.116\n" | ||
text += "\n" | ||
|
||
text += "RocketCDN subscription:\n" | ||
text += "https://rocketcdn.me/api/\n" | ||
# Defined in k8s_sips: https://gitlab.one.com/systems/chef-repo/-/blob/master/roles/onecom-global-firewall-macros.json#L173 | ||
text += "46.30.212.64\n46.30.212.65\n46.30.212.66\n46.30.212.67\n46.30.212.68\n46.30.212.69\n46.30.211.85\n" | ||
|
||
self.slack_message_factory.post_message(app_context, slack_user, text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
""" | ||
Utility functions to handle IP Addresses. | ||
""" | ||
from ipaddress import ip_address, IPv4Address | ||
|
||
IP_ADDRESS_IPV4 = "IPv4" | ||
IP_ADDRESS_IPV6 = "IPv6" | ||
|
||
|
||
def validIPAddress(IP: str) -> str: | ||
""" | ||
Checks the IP address provided to identify if it is IPv4 or IPv6 or invalid. | ||
""" | ||
try: | ||
return IP_ADDRESS_IPV4 if type(ip_address(IP)) is IPv4Address else IP_ADDRESS_IPV6 | ||
except ValueError: | ||
return "Invalid" |