Skip to content

Commit

Permalink
fix: common method to get tls certificate
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabh6790 committed Aug 20, 2024
1 parent 29e64ba commit 6bfdae8
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 52 deletions.
5 changes: 1 addition & 4 deletions press/press/doctype/analytics_server/analytics_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ def validate_plausible_password(self):

def _setup_server(self):
agent_repository_url = self.get_agent_repository_url()
certificate_name = frappe.db.get_value(
"TLS Certificate", {"wildcard": True, "domain": self.domain}, "name"
)
certificate = frappe.get_doc("TLS Certificate", certificate_name)
certificate = self.get_certificate()

log_server = frappe.db.get_single_value("Press Settings", "log_server")
if log_server:
Expand Down
6 changes: 1 addition & 5 deletions press/press/doctype/database_server/database_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,7 @@ def _setup_server(self):
self.save()

def _get_config(self):
certificate_name = frappe.db.get_value(
"TLS Certificate", {"wildcard": True, "domain": self.domain}, "name"
)
certificate = frappe.get_doc("TLS Certificate", certificate_name)

certificate = self.get_certificate()
log_server = frappe.db.get_single_value("Press Settings", "log_server")
if log_server:
kibana_password = frappe.get_doc("Log Server", log_server).get_password(
Expand Down
6 changes: 2 additions & 4 deletions press/press/doctype/log_server/log_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ def _setup_server(self):
agent_repository_url = self.get_agent_repository_url()
kibana_password = self.get_password("kibana_password")
monitoring_password = self.get_password("monitoring_password")
certificate_name = frappe.db.get_value(
"TLS Certificate", {"wildcard": True, "domain": self.domain}, "name"
)
certificate = frappe.get_doc("TLS Certificate", certificate_name)
certificate = self.get_certificate()

try:
ansible = Ansible(
playbook="log.yml",
Expand Down
5 changes: 1 addition & 4 deletions press/press/doctype/monitor_server/monitor_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ def _setup_server(self):
agent_password = self.get_password("agent_password")
agent_repository_url = self.get_agent_repository_url()
monitoring_password = self.get_password("monitoring_password")
certificate_name = frappe.db.get_value(
"TLS Certificate", {"wildcard": True, "domain": self.domain}, "name"
)
certificate = frappe.get_doc("TLS Certificate", certificate_name)
certificate = self.get_certificate()

registries = []
for registry in frappe.get_all("Registry Server"):
Expand Down
6 changes: 2 additions & 4 deletions press/press/doctype/proxy_server/proxy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,8 @@ def get_wildcard_domains(self):
if domain.domain == self.domain:
# self.domain certs are symlinks
continue
certificate_name = frappe.db.get_value(
"TLS Certificate", {"wildcard": True, "domain": domain.domain}, "name"
)
certificate = frappe.get_doc("TLS Certificate", certificate_name)

certificate = self.get_certificate()
wildcard_domains.append(
{
"domain": domain.domain,
Expand Down
6 changes: 2 additions & 4 deletions press/press/doctype/registry_server/registry_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@ def _setup_server(self):
agent_password = self.get_password("agent_password")
agent_repository_url = self.get_agent_repository_url()
monitoring_password = self.get_password("monitoring_password")
certificate_name = frappe.db.get_value(
"TLS Certificate", {"wildcard": True, "domain": self.domain}, "name"
)
certificate = frappe.get_doc("TLS Certificate", certificate_name)
certificate = self.get_certificate()

try:
ansible = Ansible(
playbook="registry.yml",
Expand Down
47 changes: 24 additions & 23 deletions press/press/doctype/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,29 +779,6 @@ def run_press_job(self, job_name, arguments=None):
}
).insert()

def get_certificate(self):
certificate_name = frappe.db.get_value(
"TLS Certificate", {"wildcard": True, "domain": self.domain}, "name"
)

if not certificate_name and self.is_self_hosted:
certificate_name = frappe.db.get_value(
"TLS Certificate", {"domain": f"{self.name}"}, "name"
)

if not certificate_name:
self_hosted_server = frappe.db.get_value(
"Self Hosted Server", {"server": self.name}, ["hostname", "domain"], as_dict=1
)

certificate_name = frappe.db.get_value(
"TLS Certificate",
{"domain": f"{self_hosted_server.hostname}.{self_hosted_server.domain}"},
"name",
)

return frappe.get_doc("TLS Certificate", certificate_name)

def get_log_server(self):
log_server = frappe.db.get_single_value("Press Settings", "log_server")
if log_server:
Expand Down Expand Up @@ -1114,6 +1091,30 @@ def get_primary_frappe_public_key(self):
return frappe.db.get_value(self.doctype, self.primary, "frappe_public_key")
frappe.throw(f"Failed to fetch {primary.name}'s Frappe public key")

def get_certificate(self):
certificate_name = frappe.db.get_value(
"TLS Certificate", {"wildcard": True, "domain": self.domain}, "name"
)

if not certificate_name:
if hasattr(self, "is_self_hosted") and self.is_self_hosted:
certificate_name = frappe.db.get_value(
"TLS Certificate", {"domain": {self.name}}, "name"
)

if not certificate_name:
self_hosted_server = frappe.db.get_value(
"Self Hosted Server", {"server": self.name}, ["hostname", "domain"], as_dict=1
)

certificate_name = frappe.db.get_value(
"TLS Certificate",
{"domain": f"{self_hosted_server.hostname}.{self_hosted_server.domain}"},
"name",
)

return frappe.get_doc("TLS Certificate", certificate_name)


class Server(BaseServer):
# begin: auto-generated types
Expand Down
5 changes: 1 addition & 4 deletions press/press/doctype/trace_server/trace_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ def validate_sentry_admin_password(self):

def _setup_server(self):
agent_repository_url = self.get_agent_repository_url()
certificate_name = frappe.db.get_value(
"TLS Certificate", {"wildcard": True, "domain": self.domain}, "name"
)
certificate = frappe.get_doc("TLS Certificate", certificate_name)
certificate = self.get_certificate()

log_server = frappe.db.get_single_value("Press Settings", "log_server")
if log_server:
Expand Down

0 comments on commit 6bfdae8

Please sign in to comment.