diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..f542a53 --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,32 @@ +# This workflow will build a golang project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go + +name: Go + +on: + pull_request: + branches: [ "main" ] + +jobs: + + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.22.4' + + - name: Build + run: go build -o rh-api-linux -v . + + - name: debug + run: ls -lah + + - name: Upload artifact + uses: actions/upload-artifact@v4 + with: + name: rh api + path: rh-api-linux diff --git a/.gitignore b/.gitignore index e2a3910..a32d148 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ -#ignore .env file +#ignore .env file .env + +.idea/ +.vscode/ \ No newline at end of file diff --git a/api/GenerateLoopback.py b/api/GenerateLoopback.py deleted file mode 100644 index 89ef6c8..0000000 --- a/api/GenerateLoopback.py +++ /dev/null @@ -1,102 +0,0 @@ -import requests -import json -import random -import os -from dotenv import load_dotenv -import logging - -# Configuration de la journalisation -logging.basicConfig(level=logging.INFO) - -load_dotenv() -TOKEN = os.getenv("TOKEN") -NETBOX_API_URL = os.getenv("NETBOX_API_URL") -API_URL_IPAM = f"{NETBOX_API_URL}ipam/ip-addresses/" - -HEADERS = { - 'Authorization': f'Token {TOKEN}', - 'Content-Type': 'application/json', - 'Accept': 'application/json', -} - - -def generate_ipv6_suffix(prefix, byte_count): - """ - Génère un suffixe IPv6 aléatoire pour un préfixe donné. - :param prefix: Préfixe IPv6. - :param byte_count: Nombre de bytes à générer. - :return: Adresse IPv6 complète. - """ - suffix = "".join(random.choice("0123456789abcdef") for _ in range(byte_count)) - return f"{prefix[:-byte_count]}:{suffix}" - - -def make_netbox_request(url, method="get", data=None, params=None): - """ - Effectue une requête HTTP à NetBox. - :param url: URL de l'API NetBox. - :param method: Méthode HTTP ('get' ou 'post'). - :param data: Données pour la requête POST. - :param params: Paramètres pour la requête GET. - :return: Réponse de la requête. - """ - try: - if method == "get": - response = requests.get(url, headers=HEADERS, params=params) - else: - response = requests.post(url, headers=HEADERS, json=data) - - response.raise_for_status() - return response.json() - except requests.HTTPError as http_err: - logging.error(f"HTTP error occurred: {http_err}") - except Exception as err: - logging.error(f"An error occurred: {err}") - - return None - - -def check_ip_exist_in_netbox(ip_address): - """ - Vérifie si une adresse IP existe dans NetBox. - :param ip_address: Adresse IPv6 à vérifier. - :return: Booléen indiquant si l'adresse existe. - """ - params = {"address": ip_address} - response = make_netbox_request(API_URL_IPAM, params=params) - - if response and response["count"] > 0: - return True - return False - - -def create_ip_in_netbox(ip_address): - """ - Crée une adresse IP dans NetBox. - :param ip_address: Adresse IPv6 à créer. - """ - data = { - "address": ip_address, - "description": "Description de l'adresse IPv6", - "custom_fields": {"Pubkey": "Votre clé publique", "Userid": 5} - } - response = make_netbox_request(API_URL_IPAM, method="post", data=data) - - if response: - logging.info(f"L'adresse IPv6 {ip_address} a été créée avec succès dans NetBox.") - - -def create_loopback(): - """ - Crée une adresse loopback qui n'existe pas encore dans NetBox. - :return: Adresse loopback créée. - """ - while True: - temp_ip = generate_ipv6_suffix("2a13:79c0:ffff:fefe::/64", 4) - if not check_ip_exist_in_netbox(temp_ip): - create_ip_in_netbox(temp_ip) - return temp_ip - - -loopback = create_loopback() -print(f"{loopback}/128") diff --git a/api/util/netbox_utils.py b/api/util/netbox_utils.py deleted file mode 100644 index 556972c..0000000 --- a/api/util/netbox_utils.py +++ /dev/null @@ -1,109 +0,0 @@ -import requests -import random -import ipaddress - -NETBOX_API_URL = 'https://netbox.360p.kube.kittenconnect.net/api/' -HEADERS = { - 'Authorization': 'Token DANSTAMERELECODE ', - 'Content-Type': 'application/json', - 'Accept': 'application/json', -} - - -def GeneratePrefixIpV6(prefix, Byte): - # il faut le define prefix = "2a13:79c0:ffff:fefe::/64" - - # Générez un suffixe aléatoire de 4 caractères hexadécimaux pour /128 - suffix = "".join(random.choice("0123456789abcdef") for _ in range(4)) - - if Byte == 4: - ipv6_address = f"{prefix[:-Byte]}:{suffix}" - elif Byte == 5: - ipv6_address = f"{prefix[:-Byte]}:{suffix}:" - else: - raise NotImplementedError() - # Concaténez le préfixe et le suffixe pour former l'adresse IPv6 complète - - return ipv6_address - - -############ -# -# Fonction pour vérifier si l'adresse IPv6 existe déjà dans NetBox -# return True si l'adresse IPv6 existe déjà dans NetBox -# return False si l'adresse IPv6 n'existe pas dans NetBox -############ - -def CheckIpExistInNetbox(ip_address): - params = { - "address": ip_address, - } - apiurl = NETBOX_API_URL + "ipam/ip-addresses/" - try: - response = requests.get(apiurl, headers=HEADERS, params=params) - - if response.status_code == 200: - ip_data = response.json() - if ip_data["count"] > 0: - return True - else: - return False - else: - print("Erreur lors de la recherche de l'adresse IPv6 dans NetBox.") - print(response.text) - return False - except Exception as e: - print(f"Une erreur s'est produite : {str(e)}") - return False - - -############ - - -def generate_or_create_ipv6_address(): - while True: - ipv6_address = generate_short_ipv6_address() - if not checkIpinNetbox(ipv6_address): - CreateIpinNetbox(ipv6_address) - return ipv6_address - - -def get_available_address_prefix(prefix: str, length: int): - """ - Get all the IP addresses from Prefix IPv6 - :param prefix: - :param length: - :return: - """ - try: - ipv6_network = ipaddress.IPv6Network(f"{prefix}/{length}", strict=False) - network_address = ipv6_network.network_address - network_address_str = str(network_address) - - return network_address_str - except ValueError as e: - print(f"Error : {e}") - return None - - -test = GeneratePrefixIpV6("2a13:79c0:ffff:fefe::/64", 4) # generate loopback -test2 = GeneratePrefixIpV6("2a13:79c0:ffff:feff::/64", 4) + "/127" # generate prefix for wireguard -test3 = GeneratePrefixIpV6("2a13:79c0:ffff:feff:b00b::/80", 5) - -print(test + "/128") -print(test2) # + "/127") -print(test3 + "/96") -TAMMERE = ipaddress.IPv6Interface(test2).network - -test4fin = [*ipaddress.ip_network(TAMMERE).hosts()] -print(*test4fin) - - -# print(TAMERELALIST) -# 2a13:79c0:ffff:feff:b00b::/80 - - -## class NewConnection to allow Loopback IP address, and /127 subnet for wireguard and return public key -class NewConnection: - def __init__(): - return true diff --git a/api/util/wireguard_utils.py b/api/util/wireguard_utils.py deleted file mode 100644 index df7e342..0000000 --- a/api/util/wireguard_utils.py +++ /dev/null @@ -1,14 +0,0 @@ -class WireguardUtils: - - def __init__(self): - pass - - def get_tunnel_ip(self) -> list: - """ - Retourne la liste des ip qui vont constituer le tunnel - :return: - """ - pass - - def etc(self): - pass diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..2a3acf3 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,88 @@ +services: + netbox: &netbox + image: docker.io/netboxcommunity/netbox:${VERSION-v3.7.5} + depends_on: + - postgres + - redis + - redis-cache + env_file: docker/env/netbox.env + user: 'unit:root' + healthcheck: + start_period: 60s + timeout: 3s + interval: 15s + test: "curl -f http://localhost:8080/login/ || exit 1" + volumes: + - ./docker/configuration:/etc/netbox/config:z,ro + - netbox-media-files:/opt/netbox/netbox/media:rw + - netbox-reports-files:/opt/netbox/netbox/reports:rw + - netbox-scripts-files:/opt/netbox/netbox/scripts:rw + ports: + - "8888:8080" + netbox-worker: + <<: *netbox + depends_on: + netbox: + condition: service_healthy + command: + - /opt/netbox/venv/bin/python + - /opt/netbox/netbox/manage.py + - rqworker + healthcheck: + start_period: 20s + timeout: 3s + interval: 15s + test: "ps -aux | grep -v grep | grep -q rqworker || exit 1" + netbox-housekeeping: + <<: *netbox + depends_on: + netbox: + condition: service_healthy + command: + - /opt/netbox/housekeeping.sh + healthcheck: + start_period: 20s + timeout: 3s + interval: 15s + test: "ps -aux | grep -v grep | grep -q housekeeping || exit 1" + + # postgres + postgres: + image: docker.io/postgres:16-alpine + env_file: docker/env/postgres.env + volumes: + - netbox-postgres-data:/var/lib/postgresql/data + + # redis + redis: + image: docker.io/redis:7-alpine + command: + - sh + - -c # this is to evaluate the $REDIS_PASSWORD from the env + - redis-server --appendonly yes --requirepass $$REDIS_PASSWORD ## $$ because of docker-compose + env_file: docker/env/redis.env + volumes: + - netbox-redis-data:/data + redis-cache: + image: docker.io/redis:7-alpine + command: + - sh + - -c # this is to evaluate the $REDIS_PASSWORD from the env + - redis-server --requirepass $$REDIS_PASSWORD ## $$ because of docker-compose + env_file: docker/env/redis-cache.env + volumes: + - netbox-redis-cache-data:/data + +volumes: + netbox-media-files: + driver: local + netbox-postgres-data: + driver: local + netbox-redis-cache-data: + driver: local + netbox-redis-data: + driver: local + netbox-reports-files: + driver: local + netbox-scripts-files: + driver: local \ No newline at end of file diff --git a/docker/configuration/configuration.py b/docker/configuration/configuration.py new file mode 100644 index 0000000..2145a25 --- /dev/null +++ b/docker/configuration/configuration.py @@ -0,0 +1,339 @@ +#### +## We recommend to not edit this file. +## Create separate files to overwrite the settings. +## See `extra.py` as an example. +#### + +import re +from os import environ +from os.path import abspath, dirname, join +from typing import Any, Callable, Tuple + +# For reference see https://docs.netbox.dev/en/stable/configuration/ +# Based on https://github.com/netbox-community/netbox/blob/develop/netbox/netbox/configuration_example.py + +### +# NetBox-Docker Helper functions +### + +# Read secret from file +def _read_secret(secret_name: str, default: str | None = None) -> str | None: + try: + f = open('/run/secrets/' + secret_name, 'r', encoding='utf-8') + except EnvironmentError: + return default + else: + with f: + return f.readline().strip() + +# If the `map_fn` isn't defined, then the value that is read from the environment (or the default value if not found) is returned. +# If the `map_fn` is defined, then `map_fn` is invoked and the value (that was read from the environment or the default value if not found) +# is passed to it as a parameter. The value returned from `map_fn` is then the return value of this function. +# The `map_fn` is not invoked, if the value (that was read from the environment or the default value if not found) is None. +def _environ_get_and_map(variable_name: str, default: str | None = None, map_fn: Callable[[str], Any | None] = None) -> Any | None: + env_value = environ.get(variable_name, default) + + if env_value == None: + return env_value + + if not map_fn: + return env_value + + return map_fn(env_value) + +_AS_BOOL = lambda value : value.lower() == 'true' +_AS_INT = lambda value : int(value) +_AS_LIST = lambda value : list(filter(None, value.split(' '))) + +_BASE_DIR = dirname(dirname(abspath(__file__))) + +######################### +# # +# Required settings # +# # +######################### + +# This is a list of valid fully-qualified domain names (FQDNs) for the NetBox server. NetBox will not permit write +# access to the server via any other hostnames. The first FQDN in the list will be treated as the preferred name. +# +# Example: ALLOWED_HOSTS = ['netbox.example.com', 'netbox.internal.local'] +ALLOWED_HOSTS = environ.get('ALLOWED_HOSTS', '*').split(' ') +# ensure that '*' or 'localhost' is always in ALLOWED_HOSTS (needed for health checks) +if '*' not in ALLOWED_HOSTS and 'localhost' not in ALLOWED_HOSTS: + ALLOWED_HOSTS.append('localhost') + +# PostgreSQL database configuration. See the Django documentation for a complete list of available parameters: +# https://docs.djangoproject.com/en/stable/ref/settings/#databases +DATABASE = { + 'NAME': environ.get('DB_NAME', 'netbox'), # Database name + 'USER': environ.get('DB_USER', ''), # PostgreSQL username + 'PASSWORD': _read_secret('db_password', environ.get('DB_PASSWORD', '')), + # PostgreSQL password + 'HOST': environ.get('DB_HOST', 'localhost'), # Database server + 'PORT': environ.get('DB_PORT', ''), # Database port (leave blank for default) + 'OPTIONS': {'sslmode': environ.get('DB_SSLMODE', 'prefer')}, + # Database connection SSLMODE + 'CONN_MAX_AGE': _environ_get_and_map('DB_CONN_MAX_AGE', '300', _AS_INT), + # Max database connection age + 'DISABLE_SERVER_SIDE_CURSORS': _environ_get_and_map('DB_DISABLE_SERVER_SIDE_CURSORS', 'False', _AS_BOOL), + # Disable the use of server-side cursors transaction pooling +} + +# Redis database settings. Redis is used for caching and for queuing background tasks such as webhook events. A separate +# configuration exists for each. Full connection details are required in both sections, and it is strongly recommended +# to use two separate database IDs. +REDIS = { + 'tasks': { + 'HOST': environ.get('REDIS_HOST', 'localhost'), + 'PORT': _environ_get_and_map('REDIS_PORT', 6379, _AS_INT), + 'SENTINELS': [tuple(uri.split(':')) for uri in _environ_get_and_map('REDIS_SENTINELS', '', _AS_LIST) if uri != ''], + 'SENTINEL_SERVICE': environ.get('REDIS_SENTINEL_SERVICE', 'default'), + 'SENTINEL_TIMEOUT': _environ_get_and_map('REDIS_SENTINEL_TIMEOUT', 10, _AS_INT), + 'USERNAME': environ.get('REDIS_USERNAME', ''), + 'PASSWORD': _read_secret('redis_password', environ.get('REDIS_PASSWORD', '')), + 'DATABASE': _environ_get_and_map('REDIS_DATABASE', 0, _AS_INT), + 'SSL': _environ_get_and_map('REDIS_SSL', 'False', _AS_BOOL), + 'INSECURE_SKIP_TLS_VERIFY': _environ_get_and_map('REDIS_INSECURE_SKIP_TLS_VERIFY', 'False', _AS_BOOL), + }, + 'caching': { + 'HOST': environ.get('REDIS_CACHE_HOST', environ.get('REDIS_HOST', 'localhost')), + 'PORT': _environ_get_and_map('REDIS_CACHE_PORT', environ.get('REDIS_PORT', '6379'), _AS_INT), + 'SENTINELS': [tuple(uri.split(':')) for uri in _environ_get_and_map('REDIS_CACHE_SENTINELS', '', _AS_LIST) if uri != ''], + 'SENTINEL_SERVICE': environ.get('REDIS_CACHE_SENTINEL_SERVICE', environ.get('REDIS_SENTINEL_SERVICE', 'default')), + 'USERNAME': environ.get('REDIS_CACHE_USERNAME', environ.get('REDIS_USERNAME', '')), + 'PASSWORD': _read_secret('redis_cache_password', environ.get('REDIS_CACHE_PASSWORD', environ.get('REDIS_PASSWORD', ''))), + 'DATABASE': _environ_get_and_map('REDIS_CACHE_DATABASE', '1', _AS_INT), + 'SSL': _environ_get_and_map('REDIS_CACHE_SSL', environ.get('REDIS_SSL', 'False'), _AS_BOOL), + 'INSECURE_SKIP_TLS_VERIFY': _environ_get_and_map('REDIS_CACHE_INSECURE_SKIP_TLS_VERIFY', environ.get('REDIS_INSECURE_SKIP_TLS_VERIFY', 'False'), _AS_BOOL), + }, +} + +# This key is used for secure generation of random numbers and strings. It must never be exposed outside of this file. +# For optimal security, SECRET_KEY should be at least 50 characters in length and contain a mix of letters, numbers, and +# symbols. NetBox will not run without this defined. For more information, see +# https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-SECRET_KEY +SECRET_KEY = _read_secret('secret_key', environ.get('SECRET_KEY', '')) + + +######################### +# # +# Optional settings # +# # +######################### + +# # Specify one or more name and email address tuples representing NetBox administrators. These people will be notified of +# # application errors (assuming correct email settings are provided). +# ADMINS = [ +# # ['John Doe', 'jdoe@example.com'], +# ] + +if 'ALLOWED_URL_SCHEMES' in environ: + ALLOWED_URL_SCHEMES = _environ_get_and_map('ALLOWED_URL_SCHEMES', None, _AS_LIST) + +# Optionally display a persistent banner at the top and/or bottom of every page. HTML is allowed. To display the same +# content in both banners, define BANNER_TOP and set BANNER_BOTTOM = BANNER_TOP. +if 'BANNER_TOP' in environ: + BANNER_TOP = environ.get('BANNER_TOP', None) +if 'BANNER_BOTTOM' in environ: + BANNER_BOTTOM = environ.get('BANNER_BOTTOM', None) + +# Text to include on the login page above the login form. HTML is allowed. +if 'BANNER_LOGIN' in environ: + BANNER_LOGIN = environ.get('BANNER_LOGIN', None) + +# Maximum number of days to retain logged changes. Set to 0 to retain changes indefinitely. (Default: 90) +if 'CHANGELOG_RETENTION' in environ: + CHANGELOG_RETENTION = _environ_get_and_map('CHANGELOG_RETENTION', None, _AS_INT) + +# Maximum number of days to retain job results (scripts and reports). Set to 0 to retain job results in the database indefinitely. (Default: 90) +if 'JOB_RETENTION' in environ: + JOB_RETENTION = _environ_get_and_map('JOB_RETENTION', None, _AS_INT) +# JOBRESULT_RETENTION was renamed to JOB_RETENTION in the v3.5.0 release of NetBox. For backwards compatibility, map JOBRESULT_RETENTION to JOB_RETENTION +elif 'JOBRESULT_RETENTION' in environ: + JOB_RETENTION = _environ_get_and_map('JOBRESULT_RETENTION', None, _AS_INT) + +# API Cross-Origin Resource Sharing (CORS) settings. If CORS_ORIGIN_ALLOW_ALL is set to True, all origins will be +# allowed. Otherwise, define a list of allowed origins using either CORS_ORIGIN_WHITELIST or +# CORS_ORIGIN_REGEX_WHITELIST. For more information, see https://github.com/ottoyiu/django-cors-headers +CORS_ORIGIN_ALLOW_ALL = _environ_get_and_map('CORS_ORIGIN_ALLOW_ALL', 'False', _AS_BOOL) +CORS_ORIGIN_WHITELIST = _environ_get_and_map('CORS_ORIGIN_WHITELIST', 'https://localhost', _AS_LIST) +CORS_ORIGIN_REGEX_WHITELIST = [re.compile(r) for r in _environ_get_and_map('CORS_ORIGIN_REGEX_WHITELIST', '', _AS_LIST)] + +# Set to True to enable server debugging. WARNING: Debugging introduces a substantial performance penalty and may reveal +# sensitive information about your installation. Only enable debugging while performing testing. +# Never enable debugging on a production system. +DEBUG = _environ_get_and_map('DEBUG', 'False', _AS_BOOL) + +# This parameter serves as a safeguard to prevent some potentially dangerous behavior, +# such as generating new database schema migrations. +# Set this to True only if you are actively developing the NetBox code base. +DEVELOPER = _environ_get_and_map('DEVELOPER', 'False', _AS_BOOL) + +# Email settings +EMAIL = { + 'SERVER': environ.get('EMAIL_SERVER', 'localhost'), + 'PORT': _environ_get_and_map('EMAIL_PORT', 25, _AS_INT), + 'USERNAME': environ.get('EMAIL_USERNAME', ''), + 'PASSWORD': _read_secret('email_password', environ.get('EMAIL_PASSWORD', '')), + 'USE_SSL': _environ_get_and_map('EMAIL_USE_SSL', 'False', _AS_BOOL), + 'USE_TLS': _environ_get_and_map('EMAIL_USE_TLS', 'False', _AS_BOOL), + 'SSL_CERTFILE': environ.get('EMAIL_SSL_CERTFILE', ''), + 'SSL_KEYFILE': environ.get('EMAIL_SSL_KEYFILE', ''), + 'TIMEOUT': _environ_get_and_map('EMAIL_TIMEOUT', 10, _AS_INT), # seconds + 'FROM_EMAIL': environ.get('EMAIL_FROM', ''), +} + +# Enforcement of unique IP space can be toggled on a per-VRF basis. To enforce unique IP space within the global table +# (all prefixes and IP addresses not assigned to a VRF), set ENFORCE_GLOBAL_UNIQUE to True. +if 'ENFORCE_GLOBAL_UNIQUE' in environ: + ENFORCE_GLOBAL_UNIQUE = _environ_get_and_map('ENFORCE_GLOBAL_UNIQUE', None, _AS_BOOL) + +# By default, netbox sends census reporting data using a single HTTP request each time a worker starts. +# This data enables the project maintainers to estimate how many NetBox deployments exist and track the adoption of new versions over time. +# The only data reported by this function are the NetBox version, Python version, and a pseudorandom unique identifier. +# To opt out of census reporting, set CENSUS_REPORTING_ENABLED to False. +if 'CENSUS_REPORTING_ENABLED' in environ: + CENSUS_REPORTING_ENABLED = _environ_get_and_map('CENSUS_REPORTING_ENABLED', None, _AS_BOOL) + +# Exempt certain models from the enforcement of view permissions. Models listed here will be viewable by all users and +# by anonymous users. List models in the form `.`. Add '*' to this list to exempt all models. +EXEMPT_VIEW_PERMISSIONS = _environ_get_and_map('EXEMPT_VIEW_PERMISSIONS', '', _AS_LIST) + +# HTTP proxies NetBox should use when sending outbound HTTP requests (e.g. for webhooks). +# HTTP_PROXIES = { +# 'http': 'http://10.10.1.10:3128', +# 'https': 'http://10.10.1.10:1080', +# } + +# IP addresses recognized as internal to the system. The debugging toolbar will be available only to clients accessing +# NetBox from an internal IP. +INTERNAL_IPS = _environ_get_and_map('INTERNAL_IPS', '127.0.0.1 ::1', _AS_LIST) + +# Enable GraphQL API. +if 'GRAPHQL_ENABLED' in environ: + GRAPHQL_ENABLED = _environ_get_and_map('GRAPHQL_ENABLED', None, _AS_BOOL) + +# # Enable custom logging. Please see the Django documentation for detailed guidance on configuring custom logs: +# # https://docs.djangoproject.com/en/stable/topics/logging/ +# LOGGING = {} + +# Automatically reset the lifetime of a valid session upon each authenticated request. Enables users to remain +# authenticated to NetBox indefinitely. +LOGIN_PERSISTENCE = _environ_get_and_map('LOGIN_PERSISTENCE', 'False', _AS_BOOL) + +# Setting this to True will permit only authenticated users to access any part of NetBox. By default, anonymous users +# are permitted to access most data in NetBox (excluding secrets) but not make any changes. +LOGIN_REQUIRED = _environ_get_and_map('LOGIN_REQUIRED', 'False', _AS_BOOL) + +# The length of time (in seconds) for which a user will remain logged into the web UI before being prompted to +# re-authenticate. (Default: 1209600 [14 days]) +LOGIN_TIMEOUT = _environ_get_and_map('LOGIN_TIMEOUT', 1209600, _AS_INT) + +# Setting this to True will display a "maintenance mode" banner at the top of every page. +if 'MAINTENANCE_MODE' in environ: + MAINTENANCE_MODE = _environ_get_and_map('MAINTENANCE_MODE', None, _AS_BOOL) + +# Maps provider +if 'MAPS_URL' in environ: + MAPS_URL = environ.get('MAPS_URL', None) + +# An API consumer can request an arbitrary number of objects =by appending the "limit" parameter to the URL (e.g. +# "?limit=1000"). This setting defines the maximum limit. Setting it to 0 or None will allow an API consumer to request +# all objects by specifying "?limit=0". +if 'MAX_PAGE_SIZE' in environ: + MAX_PAGE_SIZE = _environ_get_and_map('MAX_PAGE_SIZE', None, _AS_INT) + +# The file path where uploaded media such as image attachments are stored. A trailing slash is not needed. Note that +# the default value of this setting is derived from the installed location. +MEDIA_ROOT = environ.get('MEDIA_ROOT', join(_BASE_DIR, 'media')) + +# Expose Prometheus monitoring metrics at the HTTP endpoint '/metrics' +METRICS_ENABLED = _environ_get_and_map('METRICS_ENABLED', 'False', _AS_BOOL) + +# Determine how many objects to display per page within a list. (Default: 50) +if 'PAGINATE_COUNT' in environ: + PAGINATE_COUNT = _environ_get_and_map('PAGINATE_COUNT', None, _AS_INT) + +# # Enable installed plugins. Add the name of each plugin to the list. +# PLUGINS = [] + +# # Plugins configuration settings. These settings are used by various plugins that the user may have installed. +# # Each key in the dictionary is the name of an installed plugin and its value is a dictionary of settings. +# PLUGINS_CONFIG = { +# } + +# When determining the primary IP address for a device, IPv6 is preferred over IPv4 by default. Set this to True to +# prefer IPv4 instead. +if 'PREFER_IPV4' in environ: + PREFER_IPV4 = _environ_get_and_map('PREFER_IPV4', None, _AS_BOOL) + +# The default value for the amperage field when creating new power feeds. +if 'POWERFEED_DEFAULT_AMPERAGE' in environ: + POWERFEED_DEFAULT_AMPERAGE = _environ_get_and_map('POWERFEED_DEFAULT_AMPERAGE', None, _AS_INT) + +# The default value (percentage) for the max_utilization field when creating new power feeds. +if 'POWERFEED_DEFAULT_MAX_UTILIZATION' in environ: + POWERFEED_DEFAULT_MAX_UTILIZATION = _environ_get_and_map('POWERFEED_DEFAULT_MAX_UTILIZATION', None, _AS_INT) + +# The default value for the voltage field when creating new power feeds. +if 'POWERFEED_DEFAULT_VOLTAGE' in environ: + POWERFEED_DEFAULT_VOLTAGE = _environ_get_and_map('POWERFEED_DEFAULT_VOLTAGE', None, _AS_INT) + +# Rack elevation size defaults, in pixels. For best results, the ratio of width to height should be roughly 10:1. +if 'RACK_ELEVATION_DEFAULT_UNIT_HEIGHT' in environ: + RACK_ELEVATION_DEFAULT_UNIT_HEIGHT = _environ_get_and_map('RACK_ELEVATION_DEFAULT_UNIT_HEIGHT', None, _AS_INT) +if 'RACK_ELEVATION_DEFAULT_UNIT_WIDTH' in environ: + RACK_ELEVATION_DEFAULT_UNIT_WIDTH = _environ_get_and_map('RACK_ELEVATION_DEFAULT_UNIT_WIDTH', None, _AS_INT) + +# Remote authentication support +REMOTE_AUTH_ENABLED = _environ_get_and_map('REMOTE_AUTH_ENABLED', 'False', _AS_BOOL) +REMOTE_AUTH_BACKEND = _environ_get_and_map('REMOTE_AUTH_BACKEND', 'netbox.authentication.RemoteUserBackend', _AS_LIST) +REMOTE_AUTH_HEADER = environ.get('REMOTE_AUTH_HEADER', 'HTTP_REMOTE_USER') +REMOTE_AUTH_AUTO_CREATE_USER = _environ_get_and_map('REMOTE_AUTH_AUTO_CREATE_USER', 'False', _AS_BOOL) +REMOTE_AUTH_DEFAULT_GROUPS = _environ_get_and_map('REMOTE_AUTH_DEFAULT_GROUPS', '', _AS_LIST) +# REMOTE_AUTH_DEFAULT_PERMISSIONS = {} + +# This repository is used to check whether there is a new release of NetBox available. Set to None to disable the +# version check or use the URL below to check for release in the official NetBox repository. +RELEASE_CHECK_URL = environ.get('RELEASE_CHECK_URL', None) +# RELEASE_CHECK_URL = 'https://api.github.com/repos/netbox-community/netbox/releases' + +# Maximum execution time for background tasks, in seconds. +RQ_DEFAULT_TIMEOUT = _environ_get_and_map('RQ_DEFAULT_TIMEOUT', 300, _AS_INT) + +# The name to use for the csrf token cookie. +CSRF_COOKIE_NAME = environ.get('CSRF_COOKIE_NAME', 'csrftoken') + +# Cross-Site-Request-Forgery-Attack settings. If Netbox is sitting behind a reverse proxy, you might need to set the CSRF_TRUSTED_ORIGINS flag. +# Django 4.0 requires to specify the URL Scheme in this setting. An example environment variable could be specified like: +# CSRF_TRUSTED_ORIGINS=https://demo.netbox.dev http://demo.netbox.dev +CSRF_TRUSTED_ORIGINS = _environ_get_and_map('CSRF_TRUSTED_ORIGINS', '', _AS_LIST) + +# The name to use for the session cookie. +SESSION_COOKIE_NAME = environ.get('SESSION_COOKIE_NAME', 'sessionid') + +# If true, the `includeSubDomains` directive will be included in the HTTP Strict Transport Security (HSTS) header. +# This directive instructs the browser to apply the HSTS policy to all subdomains of the current domain. +SECURE_HSTS_INCLUDE_SUBDOMAINS = _environ_get_and_map('SECURE_HSTS_INCLUDE_SUBDOMAINS', 'False', _AS_BOOL) + +# If true, the `preload` directive will be included in the HTTP Strict Transport Security (HSTS) header. +# This directive instructs the browser to preload the site in HTTPS. Browsers that use the HSTS preload list will force the +# site to be accessed via HTTPS even if the user types HTTP in the address bar. +SECURE_HSTS_PRELOAD = _environ_get_and_map('SECURE_HSTS_PRELOAD', 'False', _AS_BOOL) + +# If set to a non-zero integer value, the SecurityMiddleware sets the HTTP Strict Transport Security (HSTS) header on all +# responses that do not already have it. This will instruct the browser that the website must be accessed via HTTPS, +# blocking any HTTP request. +SECURE_HSTS_SECONDS = _environ_get_and_map('SECURE_HSTS_SECONDS', 0, _AS_INT) + +# If true, all non-HTTPS requests will be automatically redirected to use HTTPS. +SECURE_SSL_REDIRECT = _environ_get_and_map('SECURE_SSL_REDIRECT', 'False', _AS_BOOL) + +# By default, NetBox will store session data in the database. Alternatively, a file path can be specified here to use +# local file storage instead. (This can be useful for enabling authentication on a standby instance with read-only +# database access.) Note that the user as which NetBox runs must have read and write permissions to this path. +SESSION_FILE_PATH = environ.get('SESSION_FILE_PATH', environ.get('SESSIONS_ROOT', None)) + +# Time zone (default: UTC) +TIME_ZONE = environ.get('TIME_ZONE', 'UTC') + diff --git a/docker/configuration/extra.py b/docker/configuration/extra.py new file mode 100644 index 0000000..8bd1337 --- /dev/null +++ b/docker/configuration/extra.py @@ -0,0 +1,49 @@ +#### +## This file contains extra configuration options that can't be configured +## directly through environment variables. +#### + +## Specify one or more name and email address tuples representing NetBox administrators. These people will be notified of +## application errors (assuming correct email settings are provided). +# ADMINS = [ +# # ['John Doe', 'jdoe@example.com'], +# ] + + +## URL schemes that are allowed within links in NetBox +# ALLOWED_URL_SCHEMES = ( +# 'file', 'ftp', 'ftps', 'http', 'https', 'irc', 'mailto', 'sftp', 'ssh', 'tel', 'telnet', 'tftp', 'vnc', 'xmpp', +# ) + +## Enable installed plugins. Add the name of each plugin to the list. +# from netbox.configuration.configuration import PLUGINS +# PLUGINS.append('my_plugin') + +## Plugins configuration settings. These settings are used by various plugins that the user may have installed. +## Each key in the dictionary is the name of an installed plugin and its value is a dictionary of settings. +# from netbox.configuration.configuration import PLUGINS_CONFIG +# PLUGINS_CONFIG['my_plugin'] = { +# 'foo': 'bar', +# 'buzz': 'bazz' +# } + + +## Remote authentication support +# REMOTE_AUTH_DEFAULT_PERMISSIONS = {} + + +## By default uploaded media is stored on the local filesystem. Using Django-storages is also supported. Provide the +## class path of the storage driver in STORAGE_BACKEND and any configuration options in STORAGE_CONFIG. For example: +# STORAGE_BACKEND = 'storages.backends.s3boto3.S3Boto3Storage' +# STORAGE_CONFIG = { +# 'AWS_ACCESS_KEY_ID': 'Key ID', +# 'AWS_SECRET_ACCESS_KEY': 'Secret', +# 'AWS_STORAGE_BUCKET_NAME': 'netbox', +# 'AWS_S3_REGION_NAME': 'eu-west-1', +# } + + +## This file can contain arbitrary Python code, e.g.: +# from datetime import datetime +# now = datetime.now().strftime("%d/%m/%Y %H:%M:%S") +# BANNER_TOP = f'This instance started on {now}.' diff --git a/docker/configuration/ldap/extra.py b/docker/configuration/ldap/extra.py new file mode 100644 index 0000000..4505197 --- /dev/null +++ b/docker/configuration/ldap/extra.py @@ -0,0 +1,28 @@ +#### +## This file contains extra configuration options that can't be configured +## directly through environment variables. +## All vairables set here overwrite any existing found in ldap_config.py +#### + +# # This Python script inherits all the imports from ldap_config.py +# from django_auth_ldap.config import LDAPGroupQuery # Imported since not in ldap_config.py + +# # Sets a base requirement of membetship to netbox-user-ro, netbox-user-rw, or netbox-user-admin. +# AUTH_LDAP_REQUIRE_GROUP = ( +# LDAPGroupQuery("cn=netbox-user-ro,ou=groups,dc=example,dc=com") +# | LDAPGroupQuery("cn=netbox-user-rw,ou=groups,dc=example,dc=com") +# | LDAPGroupQuery("cn=netbox-user-admin,ou=groups,dc=example,dc=com") +# ) + +# # Sets LDAP Flag groups variables with example. +# AUTH_LDAP_USER_FLAGS_BY_GROUP = { +# "is_staff": ( +# LDAPGroupQuery("cn=netbox-user-ro,ou=groups,dc=example,dc=com") +# | LDAPGroupQuery("cn=netbox-user-rw,ou=groups,dc=example,dc=com") +# | LDAPGroupQuery("cn=netbox-user-admin,ou=groups,dc=example,dc=com") +# ), +# "is_superuser": "cn=netbox-user-admin,ou=groups,dc=example,dc=com", +# } + +# # Sets LDAP Mirror groups variables with example groups +# AUTH_LDAP_MIRROR_GROUPS = ["netbox-user-ro", "netbox-user-rw", "netbox-user-admin"] diff --git a/docker/configuration/ldap/ldap_config.py b/docker/configuration/ldap/ldap_config.py new file mode 100644 index 0000000..82fad72 --- /dev/null +++ b/docker/configuration/ldap/ldap_config.py @@ -0,0 +1,111 @@ +from importlib import import_module +from os import environ + +import ldap +from django_auth_ldap.config import LDAPSearch + + +# Read secret from file +def _read_secret(secret_name, default=None): + try: + f = open('/run/secrets/' + secret_name, 'r', encoding='utf-8') + except EnvironmentError: + return default + else: + with f: + return f.readline().strip() + +# Import and return the group type based on string name +def _import_group_type(group_type_name): + mod = import_module('django_auth_ldap.config') + try: + return getattr(mod, group_type_name)() + except: + return None + +# Server URI +AUTH_LDAP_SERVER_URI = environ.get('AUTH_LDAP_SERVER_URI', '') + +# The following may be needed if you are binding to Active Directory. +AUTH_LDAP_CONNECTION_OPTIONS = { + ldap.OPT_REFERRALS: 0 +} + +AUTH_LDAP_BIND_AS_AUTHENTICATING_USER = environ.get('AUTH_LDAP_BIND_AS_AUTHENTICATING_USER', 'False').lower() == 'true' + +# Set the DN and password for the NetBox service account if needed. +if not AUTH_LDAP_BIND_AS_AUTHENTICATING_USER: + AUTH_LDAP_BIND_DN = environ.get('AUTH_LDAP_BIND_DN', '') + AUTH_LDAP_BIND_PASSWORD = _read_secret('auth_ldap_bind_password', environ.get('AUTH_LDAP_BIND_PASSWORD', '')) + +# Set a string template that describes any user’s distinguished name based on the username. +AUTH_LDAP_USER_DN_TEMPLATE = environ.get('AUTH_LDAP_USER_DN_TEMPLATE', None) + +# Enable STARTTLS for ldap authentication. +AUTH_LDAP_START_TLS = environ.get('AUTH_LDAP_START_TLS', 'False').lower() == 'true' + +# Include this setting if you want to ignore certificate errors. This might be needed to accept a self-signed cert. +# Note that this is a NetBox-specific setting which sets: +# ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER) +LDAP_IGNORE_CERT_ERRORS = environ.get('LDAP_IGNORE_CERT_ERRORS', 'False').lower() == 'true' + +# Include this setting if you want to validate the LDAP server certificates against a CA certificate directory on your server +# Note that this is a NetBox-specific setting which sets: +# ldap.set_option(ldap.OPT_X_TLS_CACERTDIR, LDAP_CA_CERT_DIR) +LDAP_CA_CERT_DIR = environ.get('LDAP_CA_CERT_DIR', None) + +# Include this setting if you want to validate the LDAP server certificates against your own CA. +# Note that this is a NetBox-specific setting which sets: +# ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, LDAP_CA_CERT_FILE) +LDAP_CA_CERT_FILE = environ.get('LDAP_CA_CERT_FILE', None) + +AUTH_LDAP_USER_SEARCH_BASEDN = environ.get('AUTH_LDAP_USER_SEARCH_BASEDN', '') +AUTH_LDAP_USER_SEARCH_ATTR = environ.get('AUTH_LDAP_USER_SEARCH_ATTR', 'sAMAccountName') +AUTH_LDAP_USER_SEARCH_FILTER: str = environ.get( + 'AUTH_LDAP_USER_SEARCH_FILTER', f'({AUTH_LDAP_USER_SEARCH_ATTR}=%(user)s)' +) + +AUTH_LDAP_USER_SEARCH = LDAPSearch( + AUTH_LDAP_USER_SEARCH_BASEDN, ldap.SCOPE_SUBTREE, AUTH_LDAP_USER_SEARCH_FILTER +) + +# This search ought to return all groups to which the user belongs. django_auth_ldap uses this to determine group +# heirarchy. + +AUTH_LDAP_GROUP_SEARCH_BASEDN = environ.get('AUTH_LDAP_GROUP_SEARCH_BASEDN', '') +AUTH_LDAP_GROUP_SEARCH_CLASS = environ.get('AUTH_LDAP_GROUP_SEARCH_CLASS', 'group') + +AUTH_LDAP_GROUP_SEARCH_FILTER: str = environ.get( + 'AUTH_LDAP_GROUP_SEARCH_FILTER', f'(objectclass={AUTH_LDAP_GROUP_SEARCH_CLASS})' +) +AUTH_LDAP_GROUP_SEARCH = LDAPSearch( + AUTH_LDAP_GROUP_SEARCH_BASEDN, ldap.SCOPE_SUBTREE, AUTH_LDAP_GROUP_SEARCH_FILTER +) +AUTH_LDAP_GROUP_TYPE = _import_group_type(environ.get('AUTH_LDAP_GROUP_TYPE', 'GroupOfNamesType')) + +# Define a group required to login. +AUTH_LDAP_REQUIRE_GROUP = environ.get('AUTH_LDAP_REQUIRE_GROUP_DN') + +# Define special user types using groups. Exercise great caution when assigning superuser status. +AUTH_LDAP_USER_FLAGS_BY_GROUP = {} + +if AUTH_LDAP_REQUIRE_GROUP is not None: + AUTH_LDAP_USER_FLAGS_BY_GROUP = { + "is_active": environ.get('AUTH_LDAP_REQUIRE_GROUP_DN', ''), + "is_staff": environ.get('AUTH_LDAP_IS_ADMIN_DN', ''), + "is_superuser": environ.get('AUTH_LDAP_IS_SUPERUSER_DN', '') + } + +# For more granular permissions, we can map LDAP groups to Django groups. +AUTH_LDAP_FIND_GROUP_PERMS = environ.get('AUTH_LDAP_FIND_GROUP_PERMS', 'True').lower() == 'true' +AUTH_LDAP_MIRROR_GROUPS = environ.get('AUTH_LDAP_MIRROR_GROUPS', '').lower() == 'true' + +# Cache groups for one hour to reduce LDAP traffic +AUTH_LDAP_CACHE_TIMEOUT = int(environ.get('AUTH_LDAP_CACHE_TIMEOUT', 3600)) + +# Populate the Django user from the LDAP directory. +AUTH_LDAP_USER_ATTR_MAP = { + "first_name": environ.get('AUTH_LDAP_ATTR_FIRSTNAME', 'givenName'), + "last_name": environ.get('AUTH_LDAP_ATTR_LASTNAME', 'sn'), + "email": environ.get('AUTH_LDAP_ATTR_MAIL', 'mail') +} diff --git a/docker/configuration/logging.py b/docker/configuration/logging.py new file mode 100644 index 0000000..d786768 --- /dev/null +++ b/docker/configuration/logging.py @@ -0,0 +1,55 @@ +# # Remove first comment(#) on each line to implement this working logging example. +# # Add LOGLEVEL environment variable to netbox if you use this example & want a different log level. +# from os import environ + +# # Set LOGLEVEL in netbox.env or docker-compose.overide.yml to override a logging level of INFO. +# LOGLEVEL = environ.get('LOGLEVEL', 'INFO') + +# LOGGING = { + +# 'version': 1, +# 'disable_existing_loggers': False, +# 'formatters': { +# 'verbose': { +# 'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}', +# 'style': '{', +# }, +# 'simple': { +# 'format': '{levelname} {message}', +# 'style': '{', +# }, +# }, +# 'filters': { +# 'require_debug_false': { +# '()': 'django.utils.log.RequireDebugFalse', +# }, +# }, +# 'handlers': { +# 'console': { +# 'level': LOGLEVEL, +# 'filters': ['require_debug_false'], +# 'class': 'logging.StreamHandler', +# 'formatter': 'simple' +# }, +# 'mail_admins': { +# 'level': 'ERROR', +# 'class': 'django.utils.log.AdminEmailHandler', +# 'filters': ['require_debug_false'] +# } +# }, +# 'loggers': { +# 'django': { +# 'handlers': ['console'], +# 'propagate': True, +# }, +# 'django.request': { +# 'handlers': ['mail_admins'], +# 'level': 'ERROR', +# 'propagate': False, +# }, +# 'django_auth_ldap': { +# 'handlers': ['console',], +# 'level': LOGLEVEL, +# } +# } +# } diff --git a/docker/configuration/plugins.py b/docker/configuration/plugins.py new file mode 100644 index 0000000..c0b1a1f --- /dev/null +++ b/docker/configuration/plugins.py @@ -0,0 +1,13 @@ +# Add your plugins and plugin settings here. +# Of course uncomment this file out. + +# To learn how to build images with your required plugins +# See https://github.com/netbox-community/netbox-docker/wiki/Using-Netbox-Plugins + +# PLUGINS = ["netbox_bgp"] + +# PLUGINS_CONFIG = { +# "netbox_bgp": { +# ADD YOUR SETTINGS HERE +# } +# } diff --git a/docker/env/netbox.env b/docker/env/netbox.env new file mode 100644 index 0000000..9f6835a --- /dev/null +++ b/docker/env/netbox.env @@ -0,0 +1,38 @@ +CORS_ORIGIN_ALLOW_ALL=True +DB_HOST=postgres +DB_NAME=netbox +DB_PASSWORD=J5brHrAXFLQSif0K +DB_USER=netbox + +EMAIL_FROM=netbox@bar.com +EMAIL_PASSWORD= +EMAIL_PORT=25 +EMAIL_SERVER=localhost +EMAIL_SSL_CERTFILE= +EMAIL_SSL_KEYFILE= +EMAIL_TIMEOUT=5 +EMAIL_USERNAME=netbox +# EMAIL_USE_SSL and EMAIL_USE_TLS are mutually exclusive, i.e. they can't both be `true`! +EMAIL_USE_SSL=false +EMAIL_USE_TLS=false + +GRAPHQL_ENABLED=true +HOUSEKEEPING_INTERVAL=86400 +MEDIA_ROOT=/opt/netbox/netbox/media +METRICS_ENABLED=false + +REDIS_CACHE_DATABASE=1 +REDIS_CACHE_HOST=redis-cache +REDIS_CACHE_INSECURE_SKIP_TLS_VERIFY=false +REDIS_CACHE_PASSWORD=t4Ph722qJ5QHeQ1qfu36 +REDIS_CACHE_SSL=false +REDIS_DATABASE=0 +REDIS_HOST=redis +REDIS_INSECURE_SKIP_TLS_VERIFY=false +REDIS_PASSWORD=H733Kdjndks81 +REDIS_SSL=false + +RELEASE_CHECK_URL=https://api.github.com/repos/netbox-community/netbox/releases +SECRET_KEY='r(m)9nLGnz$(_q3N4z1k(EFsMCjjjzx08x9VhNVcfd%6RF#r!6DE@+V5Zk2X' +SKIP_SUPERUSER=true +WEBHOOKS_ENABLED=true diff --git a/docker/env/postgres.env b/docker/env/postgres.env new file mode 100644 index 0000000..bb7b53c --- /dev/null +++ b/docker/env/postgres.env @@ -0,0 +1,3 @@ +POSTGRES_DB=netbox +POSTGRES_PASSWORD=J5brHrAXFLQSif0K +POSTGRES_USER=netbox diff --git a/docker/env/redis-cache.env b/docker/env/redis-cache.env new file mode 100644 index 0000000..6285c33 --- /dev/null +++ b/docker/env/redis-cache.env @@ -0,0 +1 @@ +REDIS_PASSWORD=t4Ph722qJ5QHeQ1qfu36 diff --git a/docker/env/redis.env b/docker/env/redis.env new file mode 100644 index 0000000..44a1987 --- /dev/null +++ b/docker/env/redis.env @@ -0,0 +1 @@ +REDIS_PASSWORD=H733Kdjndks81 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..823b5df --- /dev/null +++ b/go.mod @@ -0,0 +1,40 @@ +module github.com/KittenConnect/rh-api + +go 1.22 + +require github.com/rabbitmq/amqp091-go v1.10.0 + +require ( + github.com/fatih/color v1.17.0 + github.com/joho/godotenv v1.5.1 + github.com/netbox-community/go-netbox v0.0.0-20230225105939-fe852c86b3d6 +) + +require ( + github.com/PuerkitoBio/purell v1.1.1 // indirect + github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect + github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect + github.com/go-openapi/analysis v0.21.2 // indirect + github.com/go-openapi/errors v0.20.2 // indirect + github.com/go-openapi/jsonpointer v0.19.5 // indirect + github.com/go-openapi/jsonreference v0.19.6 // indirect + github.com/go-openapi/loads v0.21.1 // indirect + github.com/go-openapi/runtime v0.23.3 // indirect + github.com/go-openapi/spec v0.20.4 // indirect + github.com/go-openapi/strfmt v0.21.2 // indirect + github.com/go-openapi/swag v0.21.1 // indirect + github.com/go-openapi/validate v0.21.0 // indirect + github.com/go-stack/stack v1.8.1 // indirect + github.com/josharian/intern v1.0.0 // indirect + github.com/mailru/easyjson v0.7.7 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mitchellh/mapstructure v1.4.3 // indirect + github.com/oklog/ulid v1.3.1 // indirect + github.com/opentracing/opentracing-go v1.2.0 // indirect + go.mongodb.org/mongo-driver v1.8.3 // indirect + golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect + golang.org/x/sys v0.18.0 // indirect + golang.org/x/text v0.3.7 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ca1e632 --- /dev/null +++ b/go.sum @@ -0,0 +1,208 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI= +github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= +github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M= +github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= +github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw= +github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ= +github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4= +github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI= +github.com/go-openapi/analysis v0.21.2 h1:hXFrOYFHUAMQdu6zwAiKKJHJQ8kqZs1ux/ru1P1wLJU= +github.com/go-openapi/analysis v0.21.2/go.mod h1:HZwRk4RRisyG8vx2Oe6aqeSQcoxRp47Xkp3+K6q+LdY= +github.com/go-openapi/errors v0.19.8/go.mod h1:cM//ZKUKyO06HSwqAelJ5NsEMMcpa6VpXe8DOa1Mi1M= +github.com/go-openapi/errors v0.19.9/go.mod h1:cM//ZKUKyO06HSwqAelJ5NsEMMcpa6VpXe8DOa1Mi1M= +github.com/go-openapi/errors v0.20.2 h1:dxy7PGTqEh94zj2E3h1cUmQQWiM1+aeCROfAr02EmK8= +github.com/go-openapi/errors v0.20.2/go.mod h1:cM//ZKUKyO06HSwqAelJ5NsEMMcpa6VpXe8DOa1Mi1M= +github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= +github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY= +github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= +github.com/go-openapi/jsonreference v0.19.6 h1:UBIxjkht+AWIgYzCDSv2GN+E/togfwXUJFRTWhl2Jjs= +github.com/go-openapi/jsonreference v0.19.6/go.mod h1:diGHMEHg2IqXZGKxqyvWdfWU/aim5Dprw5bqpKkTvns= +github.com/go-openapi/loads v0.21.1 h1:Wb3nVZpdEzDTcly8S4HMkey6fjARRzb7iEaySimlDW0= +github.com/go-openapi/loads v0.21.1/go.mod h1:/DtAMXXneXFjbQMGEtbamCZb+4x7eGwkvZCvBmwUG+g= +github.com/go-openapi/runtime v0.23.3 h1:/dxjx4KCOQI5ImBMz036F6v/DzZ2NUjSRvbLJs1rgoE= +github.com/go-openapi/runtime v0.23.3/go.mod h1:AKurw9fNre+h3ELZfk6ILsfvPN+bvvlaU/M9q/r9hpk= +github.com/go-openapi/spec v0.20.4 h1:O8hJrt0UMnhHcluhIdUgCLRWyM2x7QkBXRvOs7m+O1M= +github.com/go-openapi/spec v0.20.4/go.mod h1:faYFR1CvsJZ0mNsmsphTMSoRrNV3TEDoAM7FOEWeq8I= +github.com/go-openapi/strfmt v0.21.0/go.mod h1:ZRQ409bWMj+SOgXofQAGTIo2Ebu72Gs+WaRADcS5iNg= +github.com/go-openapi/strfmt v0.21.1/go.mod h1:I/XVKeLc5+MM5oPNN7P6urMOpuLXEcNrCX/rPGuWb0k= +github.com/go-openapi/strfmt v0.21.2 h1:5NDNgadiX1Vhemth/TH4gCGopWSTdDjxl60H3B7f+os= +github.com/go-openapi/strfmt v0.21.2/go.mod h1:I/XVKeLc5+MM5oPNN7P6urMOpuLXEcNrCX/rPGuWb0k= +github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk= +github.com/go-openapi/swag v0.19.15/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ= +github.com/go-openapi/swag v0.21.1 h1:wm0rhTb5z7qpJRHBdPOMuY4QjVUMbF6/kwoYeRAOrKU= +github.com/go-openapi/swag v0.21.1/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ= +github.com/go-openapi/validate v0.21.0 h1:+Wqk39yKOhfpLqNLEC0/eViCkzM5FVXVqrvt526+wcI= +github.com/go-openapi/validate v0.21.0/go.mod h1:rjnrwK57VJ7A8xqfpAOEKRH8yQSGUriMu5/zuPSQ1hg= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw= +github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4= +github.com/gobuffalo/attrs v0.0.0-20190224210810-a9411de4debd/go.mod h1:4duuawTqi2wkkpB4ePgWMaai6/Kc6WEz83bhFwpHzj0= +github.com/gobuffalo/depgen v0.0.0-20190329151759-d478694a28d3/go.mod h1:3STtPUQYuzV0gBVOY3vy6CfMm/ljR4pABfrTeHNLHUY= +github.com/gobuffalo/depgen v0.1.0/go.mod h1:+ifsuy7fhi15RWncXQQKjWS9JPkdah5sZvtHc2RXGlg= +github.com/gobuffalo/envy v1.6.15/go.mod h1:n7DRkBerg/aorDM8kbduw5dN3oXGswK5liaSCx4T5NI= +github.com/gobuffalo/envy v1.7.0/go.mod h1:n7DRkBerg/aorDM8kbduw5dN3oXGswK5liaSCx4T5NI= +github.com/gobuffalo/flect v0.1.0/go.mod h1:d2ehjJqGOH/Kjqcoz+F7jHTBbmDb38yXA598Hb50EGs= +github.com/gobuffalo/flect v0.1.1/go.mod h1:8JCgGVbRjJhVgD6399mQr4fx5rRfGKVzFjbj6RE/9UI= +github.com/gobuffalo/flect v0.1.3/go.mod h1:8JCgGVbRjJhVgD6399mQr4fx5rRfGKVzFjbj6RE/9UI= +github.com/gobuffalo/genny v0.0.0-20190329151137-27723ad26ef9/go.mod h1:rWs4Z12d1Zbf19rlsn0nurr75KqhYp52EAGGxTbBhNk= +github.com/gobuffalo/genny v0.0.0-20190403191548-3ca520ef0d9e/go.mod h1:80lIj3kVJWwOrXWWMRzzdhW3DsrdjILVil/SFKBzF28= +github.com/gobuffalo/genny v0.1.0/go.mod h1:XidbUqzak3lHdS//TPu2OgiFB+51Ur5f7CSnXZ/JDvo= +github.com/gobuffalo/genny v0.1.1/go.mod h1:5TExbEyY48pfunL4QSXxlDOmdsD44RRq4mVZ0Ex28Xk= +github.com/gobuffalo/gitgen v0.0.0-20190315122116-cc086187d211/go.mod h1:vEHJk/E9DmhejeLeNt7UVvlSGv3ziL+djtTr3yyzcOw= +github.com/gobuffalo/gogen v0.0.0-20190315121717-8f38393713f5/go.mod h1:V9QVDIxsgKNZs6L2IYiGR8datgMhB577vzTDqypH360= +github.com/gobuffalo/gogen v0.1.0/go.mod h1:8NTelM5qd8RZ15VjQTFkAW6qOMx5wBbW4dSCS3BY8gg= +github.com/gobuffalo/gogen v0.1.1/go.mod h1:y8iBtmHmGc4qa3urIyo1shvOD8JftTtfcKi+71xfDNE= +github.com/gobuffalo/logger v0.0.0-20190315122211-86e12af44bc2/go.mod h1:QdxcLw541hSGtBnhUc4gaNIXRjiDppFGaDqzbrBd3v8= +github.com/gobuffalo/mapi v1.0.1/go.mod h1:4VAGh89y6rVOvm5A8fKFxYG+wIW6LO1FMTG9hnKStFc= +github.com/gobuffalo/mapi v1.0.2/go.mod h1:4VAGh89y6rVOvm5A8fKFxYG+wIW6LO1FMTG9hnKStFc= +github.com/gobuffalo/packd v0.0.0-20190315124812-a385830c7fc0/go.mod h1:M2Juc+hhDXf/PnmBANFCqx4DM3wRbgDvnVWeG2RIxq4= +github.com/gobuffalo/packd v0.1.0/go.mod h1:M2Juc+hhDXf/PnmBANFCqx4DM3wRbgDvnVWeG2RIxq4= +github.com/gobuffalo/packr/v2 v2.0.9/go.mod h1:emmyGweYTm6Kdper+iywB6YK5YzuKchGtJQZ0Odn4pQ= +github.com/gobuffalo/packr/v2 v2.2.0/go.mod h1:CaAwI0GPIAv+5wKLtv8Afwl+Cm78K/I/VCm/3ptBN+0= +github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw= +github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= +github.com/karrick/godirwalk v1.8.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4= +github.com/karrick/godirwalk v1.10.3/go.mod h1:RoGL9dQei4vP9ilrpETWE8CLOZ1kiN0LhBygSwrAsHA= +github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= +github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= +github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= +github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE= +github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mitchellh/mapstructure v1.3.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs= +github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= +github.com/netbox-community/go-netbox v0.0.0-20230225105939-fe852c86b3d6 h1:vL8WkXAlu+rrmtWEbq+M9a8ZPaShDNpXu8nFrpek8IM= +github.com/netbox-community/go-netbox v0.0.0-20230225105939-fe852c86b3d6/go.mod h1:Tj90OQ0RNovdgAXEuVZtASdVhIGdZLvQtRd3p+upuuw= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4= +github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= +github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs= +github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= +github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw= +github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o= +github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/sirupsen/logrus v1.4.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= +github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= +github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= +github.com/xdg-go/scram v1.0.2/go.mod h1:1WAq6h33pAW+iRreB34OORO2Nf7qel3VV3fjBj+hCSs= +github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6+da4O5kxM= +github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= +go.mongodb.org/mongo-driver v1.7.3/go.mod h1:NqaYOwnXWr5Pm7AOpO5QFxKJ503nbMse/R79oO62zWg= +go.mongodb.org/mongo-driver v1.7.5/go.mod h1:VXEWRZ6URJIkUq2SCAyapmhH0ZLRBP+FT4xhp5Zvxng= +go.mongodb.org/mongo-driver v1.8.3 h1:TDKlTkGDKm9kkJVUOAXDK5/fkqKHJVwYQSpoRfB43R4= +go.mongodb.org/mongo-driver v1.8.3/go.mod h1:0sQWfOeY63QTntERDJJ/0SuKK0T1uVSgKCuAROlKEPY= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190422162423-af44ce270edf/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE= +golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM= +golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk= +golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190412183630-56d357773e84/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190419153524-e8e3143a4f4a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190531175056-4c3a928424d2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190329151228-23e29df326fe/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190416151739-9c9e1878f421/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190420181800-aa740d480789/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..b7e9db1 --- /dev/null +++ b/main.go @@ -0,0 +1,201 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "os" + "os/signal" + "strconv" + + "github.com/KittenConnect/rh-api/model" + "github.com/KittenConnect/rh-api/util" + "github.com/joho/godotenv" + amqp "github.com/rabbitmq/amqp091-go" +) + +func failWithError(err error, formatString string, args ...any) { + if err != nil { + util.Err(fmt.Errorf(fmt.Sprintf("%s: %w", formatString), append(args, err)...).Error()) + } +} + +var RETRY_DELAY = 5 + +func main() { + err := godotenv.Load() + failWithError(err, "Error loading .env file") + + conn, err := amqp.Dial(os.Getenv("RABBITMQ_URL")) + failWithError(err, "Failed to connect to broker") + + defer conn.Close() + + ch, err := conn.Channel() + failWithError(err, "Failed to open a channel") + + incomingQueue := os.Getenv("RABBITMQ_INCOMING_QUEUE") + outgoingQueue := os.Getenv("RABBITMQ_OUTGOING_QUEUE") + + if value, ok := os.LookupEnv("RABBITMQ_RETRY_DELAY"); ok { + if i, err := strconv.Atoi(value); err == nil { + RETRY_DELAY = i + } + } + + inQ, err := ch.QueueDeclare( + incomingQueue, + true, + false, + false, + false, + nil, + ) + failWithError(err, "Failed to declare queue %s", incomingQueue) + + outQ, err := ch.QueueDeclare( + outgoingQueue, + true, + false, + false, + false, + nil, + ) + failWithError(err, "Failed to declare queue %s", outgoingQueue) + + exchangeArgs := map[string]interface{}{ + "x-delayed-type": "direct", + } + + err = ch.ExchangeDeclare( + incomingQueue, + "x-delayed-message", + true, + false, + false, + false, + exchangeArgs, + ) + failWithError(err, "Failed to declare exchange %s", incomingQueue) + + err = ch.QueueBind( + incomingQueue, // queue name + incomingQueue, // routing key + incomingQueue, // exchange + false, + nil) + failWithError(err, "Failed to bind queue %s to exchange %s", incomingQueue, incomingQueue) + + // Consommation des messages + msgs, err := ch.Consume( + inQ.Name, // nom de la queue + "consumer", // consumer + true, // autoAck + false, // exclusive + false, // noLocal + false, // noWait + nil, // arguments + ) + failWithError(err, "Failed to register %s consumer", inQ.Name) + util.Info("Connected to message broker") + + netbox := model.NewNetbox() + err = netbox.Connect() + failWithError(err, "Failed to connect to netbox") + + if netbox.IsConnected() == false { + util.Err("Unable to connect to netbox") + os.Exit(-1) + } + + // cancel context for whole conde + foreverCtx, foreverCancel := context.WithCancel(context.Background()) + + // Canal pour signaler la fin du programme + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, os.Interrupt) + // catch signal + go func() { + <-sigs + fmt.Printf("You pressed ctrl + C. User interrupted infinite loop.") + foreverCancel() + }() + + go func() { + for d := range msgs { + go func() { + msg := model.Message{Timestamp: d.Timestamp, FailCount: 20} + err = json.Unmarshal(d.Body, &msg) + if err != nil { + util.Warn("Error unmarshalling message : %w", err) + return + } + + //Make request to the rest of API + err = netbox.CreateOrUpdateVM(msg) + if err != nil { + util.Warn("error creating or updating VM : %w", err) + + newMsg := msg + newMsg.FailCount-- + + if newMsg.FailCount <= 0 { + return + } + + newMsgJson, _ := json.Marshal(newMsg) + + headers := amqp.Table{ + "x-delay": RETRY_DELAY * 1000, + } + + chErr := ch.Publish( + incomingQueue, + inQ.Name, + false, + false, + amqp.Publishing{ + ContentType: "application/json", + Body: newMsgJson, + Headers: headers, + }) + + if chErr != nil { + util.Warn("Error re-publishing message: %s", chErr) + } else { + util.Warn("Re-sent message to RabbitMQ®️: %s", newMsgJson) + } + + return + } + + util.Success("VM %s is up to date", msg.Hostname) + + newMsg := msg + + newMsgJson, _ := json.Marshal(newMsg) + + chErr := ch.Publish( + "", + outQ.Name, + false, + false, + amqp.Publishing{ + ContentType: "application/json", + Body: newMsgJson, + }) + + if chErr != nil { + util.Warn("Error publishing success message: %s", chErr) + } else { + util.Success("sent success message to RabbitMQ®️: %s", newMsgJson) + } + }() + } + util.Info("End of queue reaches exit now !") + foreverCancel() + }() + + util.Info(" [*] Waiting for messages. To exit press CTRL+C") + <-foreverCtx.Done() +} diff --git a/model/Cluster.go b/model/Cluster.go new file mode 100644 index 0000000..0ef9afa --- /dev/null +++ b/model/Cluster.go @@ -0,0 +1,5 @@ +package model + +type Cluster struct { + ID int64 `json:"id"` +} diff --git a/model/VirtualMachine.go b/model/VirtualMachine.go new file mode 100644 index 0000000..be7dd7b --- /dev/null +++ b/model/VirtualMachine.go @@ -0,0 +1,347 @@ +package model + +import ( + "fmt" + "net" + "strconv" + + "github.com/KittenConnect/rh-api/util" + "github.com/netbox-community/go-netbox/netbox/client/ipam" + "github.com/netbox-community/go-netbox/netbox/client/virtualization" + "github.com/netbox-community/go-netbox/netbox/models" +) + +type VirtualMachine struct { + NetboxId int64 `json:"id"` + Cluster Cluster `json:"cluster"` + Name string `json:"name"` + Status string `json:"status"` + Serial string `json:"serial"` + + ManagementIP net.IP `json:"management_ip"` + n *Netbox +} + +var ( + mgmtInterfaceName = "mgmt" +) + +func NewVM(n *Netbox, msg Message) *VirtualMachine { + serial := msg.parseSerial() + + vm := &VirtualMachine{ + n: n, + NetboxId: -1, + + Name: msg.Hostname, + Serial: serial, + } + + return vm +} + +// dev chnage name ? +// Get: return tableVirtualMachineWithConfigContext for vm +func (vm *VirtualMachine) Get() *models.WritableVirtualMachineWithConfigContext { + // todo: implement netbox func + return &models.WritableVirtualMachineWithConfigContext{ + Cluster: &vm.Cluster.ID, + Name: &vm.Name, + Status: vm.Status, + + CustomFields: map[string]interface{}{ + "kc_serial_": vm.Serial, + }, + } +} + +func (vm *VirtualMachine) Create() (*virtualization.VirtualizationVirtualMachinesCreateCreated, error) { + params := virtualization.NewVirtualizationVirtualMachinesCreateParams().WithData(vm.Get()) + return vm.n.Client.Virtualization.VirtualizationVirtualMachinesCreate(params, nil) +} + +func (vm *VirtualMachine) CreateOrUpdate(msg Message) { + // +} + +// Update vm infos to netbox +func (vm *VirtualMachine) Update() error { + updateParams := &virtualization.VirtualizationVirtualMachinesPartialUpdateParams{ + Data: vm.Get(), + ID: vm.NetboxId, + } + + _, err := vm.n.Client.Virtualization. + VirtualizationVirtualMachinesPartialUpdate(updateParams.WithTimeout(vm.n.GetDefaultTimeout()), nil) + if err != nil { + return fmt.Errorf("error updating virtual machine interface: %w", err) + } + + return nil +} + +func (vm *VirtualMachine) GetInterfaces(name string) (*virtualization.VirtualizationInterfacesListOK, error) { + vmId := strconv.FormatInt(vm.NetboxId, 10) + + ipIfParam := &virtualization.VirtualizationInterfacesListParams{ + VirtualMachineID: &vmId, + Name: &name, + } + interfaces, err := vm.n.Client.Virtualization. + VirtualizationInterfacesList(ipIfParam.WithTimeout(vm.n.GetDefaultTimeout()), nil) + if err != nil { + return nil, fmt.Errorf("error listing virtual machine interfaces: %w", err) + } + + return interfaces, nil +} + +func (vm *VirtualMachine) GetInterfaceByID(id int64) (*models.VMInterface, error) { + vmId := strconv.FormatInt(vm.NetboxId, 10) + interfaceId := strconv.FormatInt(id, 10) + + ipIfParam := &virtualization.VirtualizationInterfacesListParams{ + VirtualMachineID: &vmId, + ID: &interfaceId, + } + i, err := vm.n.Client.Virtualization. + VirtualizationInterfacesList(ipIfParam.WithTimeout(vm.n.GetDefaultTimeout()), nil) + if err != nil { + return nil, fmt.Errorf("error listing virtual machine interfaces: %w", err) + } + + if *i.Payload.Count != 1 { + return nil, fmt.Errorf("error listing virtual machine interfaces: expected 1 item, got %d", i.Payload.Count) + } + + return i.Payload.Results[0], nil +} + +func (vm *VirtualMachine) GetManagementInterface() (*models.VMInterface, error) { + vmId := strconv.FormatInt(vm.NetboxId, 10) + + ipIfParam := &virtualization.VirtualizationInterfacesListParams{ + VirtualMachineID: &vmId, + Name: &mgmtInterfaceName, + } + in, err := vm.n.Client.Virtualization. + VirtualizationInterfacesList(ipIfParam.WithTimeout(vm.n.GetDefaultTimeout()), nil) + if err != nil { + return nil, fmt.Errorf("error listing virtual machine interfaces: %w", err) + } + + //If there are no management interface, create it + if *in.Payload.Count == 0 { + mgmtInterface, err := vm.CreateInterface("mgmt") + if err != nil { + return nil, fmt.Errorf("error creating virtual machine interface: %w", err) + } + + return mgmtInterface.Payload, nil + } + + return in.Payload.Results[0], nil +} + +func (vm *VirtualMachine) CreateInterface(ifName string) (*virtualization.VirtualizationInterfacesCreateCreated, error) { + ifParam := models.WritableVMInterface{ + Name: &ifName, + Enabled: true, + + TaggedVlans: []int64{}, + + VirtualMachine: &vm.NetboxId, + } + paramInterface := virtualization. + NewVirtualizationInterfacesCreateParams(). + WithData(&ifParam). + WithTimeout(vm.n.GetDefaultTimeout()) + res, err := vm.n.Client.Virtualization.VirtualizationInterfacesCreate(paramInterface, nil) + if err != nil { + return nil, fmt.Errorf("error creating virtual machine interface: %w", err) + } + util.Success("\tSuccessfully created vm interface %s", strconv.FormatInt(res.Payload.ID, 10)) + + return res, nil +} + +func (vm *VirtualMachine) UpdateInterfaceIP(address string, ifId int64, objectType NetboxVmObjectType) error { + objType := string(objectType) + + ip := vm.n.getIpAddress(address) + ip.AssignedObjectID = &ifId + ip.AssignedObjectType = &objType + + ifUpdateParam := &ipam.IpamIPAddressesPartialUpdateParams{ + Data: ip, + } + + _, err := vm.n.Client.Ipam. + IpamIPAddressesPartialUpdate(ifUpdateParam.WithID(ip.ID). + WithTimeout(vm.n.GetDefaultTimeout()), nil) + if err != nil { + return fmt.Errorf("error updating ip address: %w", err) + } + + util.Success("Update IP to VM interface") + return nil +} + +func (vm *VirtualMachine) CreateIP(n *Netbox, address string, status string, linkedObjectId int64, linkedObjectType NetboxVmObjectType) (*ipam.IpamIPAddressesCreateCreated, error) { + ip := &models.WritableIPAddress{ + Address: &address, + Status: status, + } + + if linkedObjectId != -1 && linkedObjectType != "" { + objectType := string(linkedObjectType) + + ip.AssignedObjectID = &linkedObjectId + ip.AssignedObjectType = &objectType + } + + ipCreateParams := &ipam.IpamIPAddressesCreateParams{ + Data: ip, + } + + res, err := n.Client.Ipam.IpamIPAddressesCreate(ipCreateParams.WithTimeout(n.GetDefaultTimeout()), nil) + if err != nil { + return nil, fmt.Errorf("error creating ip address: %w", err) + } + + return res, nil +} + +func (vm *VirtualMachine) UpdateManagementIP(msg Message) error { + //Get vm management interface + itf, err := vm.GetManagementInterface() + if err != nil { + return fmt.Errorf("error getting interfaces: %w", err) + } + + objectType := VmInterfaceType + + //Update management interface with latest IP + err = vm.UpdateInterfaceIP(msg.IpAddress, itf.ID, objectType) + if err != nil { + return err + } + + var mgmtInterfaceId = strconv.FormatInt(itf.ID, 10) + params := ipam.NewIpamIPAddressesListParams() + params.SetVminterfaceID(&mgmtInterfaceId) + + result, err := vm.n.Client.Ipam.IpamIPAddressesList(params, nil) + if err != nil { + return fmt.Errorf("error listing ip addresses: %w", err) + } + + var ipCount = result.Payload.Count + util.Info("There are actually %s IP(s) associated with the management interface", strconv.FormatInt(*ipCount, 10)) + + if *ipCount > 1 { + return fmt.Errorf("there are more than one management ip linked to the management interface") + } + + if *ipCount == 1 { + ip := result.Payload.Results[0] + if *ip.Address == msg.IpAddress { + //Nothing to do + return nil + } + + // 4. The management IP changed, so : + // - unlink the old ip and interface + // - set the new ip to the interface + + oldIpUpdateParams := models.WritableIPAddress{ + Address: &msg.IpAddress, + AssignedObjectType: nil, + AssignedObjectID: nil, + } + + paramUnlinkOldIp := ipam. + NewIpamIPAddressesPartialUpdateParams(). + WithID(ip.ID). + WithData(&oldIpUpdateParams). + WithTimeout(vm.n.GetDefaultTimeout()) + _, err = vm.n.Client.Ipam.IpamIPAddressesPartialUpdate(paramUnlinkOldIp, nil) + if err != nil { + return fmt.Errorf("error unlinking management ip addresses of VM #%d: %w", vm.NetboxId, err) + } + + // Set the ip to the new machine + objectType := string(objectType) + newIpUpdateParam := vm.n.getIpAddress(msg.IpAddress) + newIpUpdateParam.AssignedObjectID = &vm.NetboxId + newIpUpdateParam.AssignedObjectType = &objectType + + paramLinkNewIp := ipam. + NewIpamIPAddressesPartialUpdateParams(). + WithID(ip.ID). + WithData(newIpUpdateParam). + WithTimeout(vm.n.GetDefaultTimeout()) + _, err = vm.n.Client.Ipam.IpamIPAddressesPartialUpdate(paramLinkNewIp, nil) + if err != nil { + return fmt.Errorf("error linking ip with the new interface : %w", err) + } + + util.Success("Successfully updated management ip addresses of VM #%d with new IP: %s", vm.NetboxId, msg.IpAddress) + return nil + } + + // 5. No existing IP, but verify that she doesn't already exist in the netbox + ipSearchParams := ipam.NewIpamIPAddressesListParams() + ipSearchParams.Q = &msg.IpAddress + result, err = vm.n.Client.Ipam.IpamIPAddressesList(ipSearchParams, nil) + if err != nil { + return fmt.Errorf("error listing existing ip addresses: %w", err) + } + + existingIpCount := result.Payload.Count + newIpAddrId := int64(0) + if *existingIpCount == 0 { + util.Info("There is no IP registered in the netbox. Create him.") + var ipType = string(VmInterfaceType) + newIp := &ipam.IpamIPAddressesCreateParams{ + Data: &models.WritableIPAddress{ + Address: &msg.IpAddress, + AssignedObjectID: &itf.ID, + AssignedObjectType: &ipType, + }, + } + r, err := vm.n.Client.Ipam.IpamIPAddressesCreate(newIp.WithTimeout(vm.n.GetDefaultTimeout()), nil) + if err != nil { + return fmt.Errorf("error creating ip address: %w", err) + } + + newIpAddrId = r.Payload.ID + } else { + newIpAddrId = result.Payload.Results[0].ID + } + + var ipType = string(VmInterfaceType) + + ip := vm.n.getIpAddress(msg.IpAddress) + ip.ID = newIpAddrId + ip.AssignedObjectID = &itf.ID + ip.AssignedObjectType = &ipType + + ifUpdateParam := &ipam.IpamIPAddressesPartialUpdateParams{ + Data: ip, + } + _, err = vm.n.Client.Ipam.IpamIPAddressesPartialUpdate(ifUpdateParam.WithTimeout(vm.n.GetDefaultTimeout()), nil) + if err != nil { + return fmt.Errorf("error updating ip address: %w", err) + } + + return nil +} + +func (vm *VirtualMachine) Exists(hostname string, serial string) (bool, int64, error) { + if vm.NetboxId <= 0 && vm.n == nil { + return false, 0, nil + } + + return vm.n.VmExists(hostname, serial) +} diff --git a/model/message.go b/model/message.go new file mode 100644 index 0000000..3b8e804 --- /dev/null +++ b/model/message.go @@ -0,0 +1,29 @@ +package model + +import ( + "strings" + "time" +) + +type Message struct { + Hostname string `json:"hostname"` + IpAddress string `json:"ipaddress"` + serial string `json:"serial" binding:"optional"` + + //Make following json field optional with default 0 + FailCount int `json:"failcount" binding:"optional"` + + Timestamp time.Time `json:"-"` +} + +func (m *Message) parseSerial() string { + return strings.Join(strings.Split(m.Hostname, "-")[1:], "-") +} + +func (m *Message) GetSerial() string { + if m.serial == "" { + m.serial = m.parseSerial() + } + + return m.serial +} diff --git a/model/netbox.go b/model/netbox.go new file mode 100644 index 0000000..3526aa9 --- /dev/null +++ b/model/netbox.go @@ -0,0 +1,269 @@ +package model + +import ( + "context" + "errors" + "fmt" + "os" + "strconv" + "time" + + "github.com/KittenConnect/rh-api/util" + "github.com/netbox-community/go-netbox/netbox" + "github.com/netbox-community/go-netbox/netbox/client" + "github.com/netbox-community/go-netbox/netbox/client/ipam" + "github.com/netbox-community/go-netbox/netbox/client/virtualization" + "github.com/netbox-community/go-netbox/netbox/models" +) + +type NetboxVmObjectType string + +const ( + VmInterfaceType NetboxVmObjectType = "virtualization.vminterface" +) + +// Netbox structure +// For internal use ONLY ! +// To get an instance, call NewNetbox method +type Netbox struct { + ctx context.Context + + Client *client.NetBoxAPI + + _isConnected bool +} + +// NewNetbox return a fresh Netbox object +func NewNetbox() *Netbox { + nbx := Netbox{ + ctx: context.Background(), + Client: nil, + + _isConnected: false, + } + + return &nbx +} + +func (n *Netbox) IsConnected() bool { + return n._isConnected +} + +func (n *Netbox) Connect() error { + if n._isConnected { + return nil + } + + n.Client = netbox.NewNetboxWithAPIKey(os.Getenv("NETBOX_API_URL"), os.Getenv("NETBOX_API_TOKEN")) + n._isConnected = true + + return nil +} + +func (n *Netbox) GetDefaultTimeout() time.Duration { + return 30 * time.Second +} + +func (n *Netbox) getIpAddress(ip string) *models.WritableIPAddress { + return &models.WritableIPAddress{ + Address: &ip, + Status: models.IPAddressStatusValueActive, + } +} + +func (n *Netbox) CreateVM(msg Message) error { + if !n._isConnected { + return errors.New("netbox is not connected") + } + + vm := NewVM(n, msg) + res, err := vm.Create() + if err != nil { + if res != nil && res.Payload != nil { + return fmt.Errorf("error creating virtual machine: %w \n\t%s", err, res.Error()) + } + + return fmt.Errorf("error creating virtual machine: %w", err) + } + + util.Success("Created machine ID: %d", res.Payload.ID) + vm.NetboxId = res.Payload.ID + + //Create management interface + r, err := vm.CreateInterface("mgmt") + if err != nil { + return err + } + + var ( + ifId = r.Payload.ID + objectType = VmInterfaceType + ) + + //Verify if ip already exists + ipAlreadyExist := &ipam.IpamIPAddressesListParams{ + Address: &msg.IpAddress, + } + req, err := n.Client.Ipam. + IpamIPAddressesList(ipAlreadyExist.WithTimeout(n.GetDefaultTimeout()), nil) + if err != nil { + return fmt.Errorf("error checking ip addresses existance : %w", err) + } + + util.Info("Found #%d IPs in %v", *req.Payload.Count, *req) + //We don't have that ip registered on netbox, so let's create him + if *req.Payload.Count == 0 { + //Set ip to the interface + createdIP, err := vm.CreateIP(n, msg.IpAddress, models.IPAddressStatusValueActive, ifId, objectType) + if err != nil { + return err + } + + util.Success("\tSuccessfully created vm management ip: %s", strconv.FormatInt(createdIP.Payload.ID, 10)) + } else if *req.Payload.Count == 1 { + ip := req.Payload.Results[0] + + linkedInterfaceId := ip.AssignedObjectID + + //Si l'ip n'est pas liée à une interface + //On l'assigne à l'interface de la machine et zou + if linkedInterfaceId == nil { + return vm.UpdateInterfaceIP(msg.IpAddress, ifId, objectType) + } + + //Sinon on vérifie si la VM possède d'autres IP sur l'interface de management + interfaceId := *linkedInterfaceId + vmInterfaceParam := virtualization. + NewVirtualizationInterfacesReadParams(). + WithID(interfaceId). + WithTimeout(n.GetDefaultTimeout()) + + vmInterfaceResult, err := n.Client.Virtualization.VirtualizationInterfacesRead(vmInterfaceParam, nil) + if err != nil { + return fmt.Errorf("error reading virtual machine interface: %w", err) + } + + vmID := strconv.FormatInt(vmInterfaceResult.Payload.VirtualMachine.ID, 10) + + mgmtInterfaceName := "mgmt" + nestedVmParams := &virtualization.VirtualizationInterfacesListParams{ + Name: &mgmtInterfaceName, + VirtualMachineID: &vmID, + } + nestedVmInterfaces, err := n.Client.Virtualization. + VirtualizationInterfacesList(nestedVmParams.WithTimeout(n.GetDefaultTimeout()), nil) + if err != nil { + return fmt.Errorf("error listing virtual machine interfaces: %w", err) + } + + mgmtInterface := nestedVmInterfaces.Payload.Results[0] + if mgmtInterface.CountIpaddresses == 1 { + //L'interface possède d'autres IPs + //Du coup, on prend l'ip en question + util.Info("Remove the link ...") + err := vm.UpdateInterfaceIP(msg.IpAddress, ifId, objectType) + if err != nil { + return err + } + util.Success("IP changed of interface") + + return nil + } else { + //Sinon on laisse l'ip sur la VM + util.Info("L'IP %s reste sur l'interface n°%d", msg.IpAddress, mgmtInterface.ID) + } + + util.Warn("Trying to using existing IP on VM interface #%s", strconv.FormatInt(mgmtInterface.ID, 10)) + } + + return nil +} + +func (n *Netbox) UpdateVM(id int64, msg Message) error { + vm := NewVM(n, msg) + vm.NetboxId = id + + _, err := vm.Create() + if err != nil { + return err + } + + err = vm.Update() + if err != nil { + return err + } + + //Update management IP + return vm.UpdateManagementIP(msg) +} + +func (n *Netbox) CreateOrUpdateVM(msg Message) error { + if !n._isConnected { + return errors.New("netbox is not connected") + } + + var vmId int64 + var err error + + // Call netbox API with specific serial, then update his settings accordingly + //exist := contains(MachinesSerials, msg.Hostname) //TODO + //if !exist { + //If the vm don't exist in memory, fetch his details, if she exists in netbox + exist, vmId, err := n.VmExists(msg.Hostname, msg.GetSerial()) + if err != nil { + return fmt.Errorf("error checking if VM exists: %w", err) + } + + //Create VM if she doesn't exists in netbox + if !exist { + err = n.CreateVM(msg) + + if err != nil { + return fmt.Errorf("unable to create VM: %w", err) + } + } else { + err = n.UpdateVM(vmId, msg) + if err != nil { + return fmt.Errorf("unable to update VM: %w", err) + } + + //util.Success("VM updated successfully") + } + + return nil +} + +func (n *Netbox) VmExists(hostname string, serial string) (bool, int64, error) { + //Check if the vm exist in netbox + req := virtualization. + NewVirtualizationVirtualMachinesListParams(). + WithTimeout(n.GetDefaultTimeout()) + res, err := n.Client.Virtualization.VirtualizationVirtualMachinesList(req, nil) + if err != nil { + return false, 0, fmt.Errorf("unable to get list of machines from netbox: %w", err) + } + + for _, v := range res.Payload.Results { + if *v.Name == hostname { + return true, v.ID, nil + } + + var cf = v.CustomFields.(map[string]interface{}) + var serial = "" + + for k, v := range cf { + switch c := v.(type) { + case string: + if k == "kc_serial_" { + serial = c + } + } + } + + if serial != "" { + return true, v.ID, nil + } + } + + return false, 0, nil +} diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index da776d6..0000000 --- a/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -requests==2.31.0 -dotenv==0.19.1 diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..529b7f6 --- /dev/null +++ b/shell.nix @@ -0,0 +1,24 @@ +{ + pkgs ? import { }, + compile ? false, + ... +}: +let + inherit (pkgs) lib mkShell buildGoModule; + inherit (lib) fetchFromGitHub cleanSource; + + src = cleanSource ./.; + + package = buildGoModule { + name = "kittenMQ-consumer"; + inherit src; + # proxyVendor = true; + vendorHash = "sha256-XE5npxjcTRDmINM2IFS4C9NWfsAYiGs+h4sDIZX8AhU="; + + postInstall = '' + mv $out/bin/rh-api $out/bin/kittenMQ-consumer + ''; + + }; +in +mkShell { packages = (with pkgs; [ go ]) ++ lib.optional (compile) package; } diff --git a/util/log.go b/util/log.go new file mode 100644 index 0000000..6d11808 --- /dev/null +++ b/util/log.go @@ -0,0 +1,38 @@ +package util + +import ( + "fmt" + "os" + + "github.com/fatih/color" +) + +func Color(c color.Attribute, prefix string, s string, v ...any) { + colored := color.New(c).SprintFunc() + fmt.Printf(fmt.Sprintf(colored("[%s] %s\n"), prefix, s), v...) +} + +func Err(s string, v ...any) { + // red := color.New(color.FgRed).SprintFunc() + // fmt.Printf(fmt.Sprintf(red("%s %s\n"), "[ERROR]", s), v...) + Color(color.FgRed, "ERROR", s, v...) + os.Exit(-1) +} + +func Warn(s string, v ...any) { + // orange := color.New(color.FgYellow).SprintFunc() + // fmt.Printf("%s %s\n", orange("[WARN]"), orange(t)) + Color(color.FgYellow, "WARN", s, v...) +} + +func Info(s string, v ...any) { + // blue := color.New(color.FgBlue).SprintFunc() + // fmt.Printf("%s %s\n", blue("[INFO]"), blue(t)) + Color(color.FgBlue, "INFO", s, v...) +} + +func Success(s string, v ...any) { + // green := color.New(color.FgGreen).SprintFunc() + // fmt.Printf("%s %s\n", green("[SUCCESS]"), green(t)) + Color(color.FgGreen, "SUCCESS", s, v...) +}