def post(self, request): from awx.main.utils.common import get_licenser data = request.data.copy() if data.get('rh_password') == '$encrypted$': data['rh_password'] = settings.REDHAT_PASSWORD try: user, pw = data.get('rh_username'), data.get('rh_password') with set_environ(**settings.AWX_TASK_ENV): validated = get_licenser().validate_rh(user, pw) if user: settings.REDHAT_USERNAME = data['rh_username'] if pw: settings.REDHAT_PASSWORD = data['rh_password'] except Exception as exc: msg = _("Invalid License") if (isinstance(exc, requests.exceptions.HTTPError) and getattr(getattr(exc, 'response', None), 'status_code', None) == 401): msg = _("The provided credentials are invalid (HTTP 401).") elif isinstance(exc, requests.exceptions.ProxyError): msg = _("Unable to connect to proxy server.") elif isinstance(exc, requests.exceptions.ConnectionError): msg = _("Could not connect to subscription service.") elif isinstance(exc, (ValueError, OSError)) and exc.args: msg = exc.args[0] else: logger.exception(smart_text(u"Invalid license submitted."), extra=dict(actor=request.user.username)) return Response({"error": msg}, status=status.HTTP_400_BAD_REQUEST) return Response(validated)
def post(self, request): data = request.data.copy() pool_id = data.get('pool_id', None) if not pool_id: return Response({"error": _("No subscription pool ID provided.")}, status=status.HTTP_400_BAD_REQUEST) user = getattr(settings, 'SUBSCRIPTIONS_USERNAME', None) pw = getattr(settings, 'SUBSCRIPTIONS_PASSWORD', None) if pool_id and user and pw: from awx.main.utils.common import get_licenser data = request.data.copy() try: with set_environ(**settings.AWX_TASK_ENV): validated = get_licenser().validate_rh(user, pw) except Exception as exc: msg = _("Invalid Subscription") if isinstance(exc, requests.exceptions.HTTPError) and getattr(getattr(exc, 'response', None), 'status_code', None) == 401: msg = _("The provided credentials are invalid (HTTP 401).") elif isinstance(exc, requests.exceptions.ProxyError): msg = _("Unable to connect to proxy server.") elif isinstance(exc, requests.exceptions.ConnectionError): msg = _("Could not connect to subscription service.") elif isinstance(exc, (ValueError, OSError)) and exc.args: msg = exc.args[0] else: logger.exception(smart_text(u"Invalid subscription submitted."), extra=dict(actor=request.user.username)) return Response({"error": msg}, status=status.HTTP_400_BAD_REQUEST) for sub in validated: if sub['pool_id'] == pool_id: sub['valid_key'] = True settings.LICENSE = sub return Response(sub) return Response({"error": _("Error processing subscription metadata.")}, status=status.HTTP_400_BAD_REQUEST)
def ship(path): """ Ship gathered metrics to the Insights API """ if not path: logger.error('Automation Analytics TAR not found') return if not os.path.exists(path): logger.error('Automation Analytics TAR {} not found'.format(path)) return if "Error:" in str(path): return try: logger.debug('shipping analytics file: {}'.format(path)) url = getattr(settings, 'AUTOMATION_ANALYTICS_URL', None) if not url: logger.error('AUTOMATION_ANALYTICS_URL is not set') return rh_user = getattr(settings, 'REDHAT_USERNAME', None) rh_password = getattr(settings, 'REDHAT_PASSWORD', None) if not rh_user: return logger.error('REDHAT_USERNAME is not set') if not rh_password: return logger.error('REDHAT_PASSWORD is not set') with open(path, 'rb') as f: files = { 'file': (os.path.basename(path), f, settings.INSIGHTS_AGENT_MIME) } s = requests.Session() s.headers = get_awx_http_client_headers() s.headers.pop('Content-Type') with set_environ(**settings.AWX_TASK_ENV): response = s.post( url, files=files, verify="/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", auth=(rh_user, rh_password), headers=s.headers, timeout=(31, 31), ) # Accept 2XX status_codes if response.status_code >= 300: return logger.exception( 'Upload failed with status {}, {}'.format( response.status_code, response.text)) finally: # cleanup tar.gz if os.path.exists(path): os.remove(path)
def send(self, subject, body): for field in filter(lambda x: self.notification_class.init_parameters[x]['type'] == "password", self.notification_class.init_parameters): self.notification_configuration[field] = decrypt_field(self, 'notification_configuration', subfield=field) recipients = self.notification_configuration.pop(self.notification_class.recipient_parameter) if not isinstance(recipients, list): recipients = [recipients] sender = self.notification_configuration.pop(self.notification_class.sender_parameter, None) backend_obj = self.notification_class(**self.notification_configuration) notification_obj = EmailMessage(subject, backend_obj.format_body(body), sender, recipients) with set_environ(**settings.AWX_TASK_ENV): return backend_obj.send_messages([notification_obj])