Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] generate cert functionality #658

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/codeflare_sdk/utils/generate_cert.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import datetime
from ..cluster.auth import config_check, api_config_handler
from kubernetes import client, config
from .kube_api_helpers import _kube_api_error_handling


def generate_ca_cert(days: int = 30):
Expand Down Expand Up @@ -74,6 +75,24 @@ def generate_ca_cert(days: int = 30):
return key, certificate


def get_secret_name(cluster_name, namespace, api_instance):
label_selector = f"ray.openshift.ai/cluster-name={cluster_name}"
try:
secrets = api_instance.list_namespaced_secret(
namespace, label_selector=label_selector
)
for secret in secrets.items:
if (
f"{cluster_name}-ca-secret-" in secret.metadata.name
sutaakar marked this conversation as resolved.
Show resolved Hide resolved
): # Oauth secret share the same label this conditional is to make things more specific
return secret.metadata.name
else:
continue
raise KeyError(f"Unable to gather secret name for {cluster_name}")
except Exception as e: # pragma: no cover
return _kube_api_error_handling(e)


def generate_tls_cert(cluster_name, namespace, days=30):
# Create a folder tls-<cluster>-<namespace> and store three files: ca.crt, tls.crt, and tls.key
tls_dir = os.path.join(os.getcwd(), f"tls-{cluster_name}-{namespace}")
Expand All @@ -85,7 +104,11 @@ def generate_tls_cert(cluster_name, namespace, days=30):
# oc get secret ca-secret-<cluster-name> -o template='{{index .data "ca.crt"}}'|base64 -d > ${TLSDIR}/ca.crt
config_check()
v1 = client.CoreV1Api(api_config_handler())
secret = v1.read_namespaced_secret(f"ca-secret-{cluster_name}", namespace).data

# Secrets have a suffix appended to the end so we must list them and gather the secret that includes cluster_name-ca-secret-
secret_name = get_secret_name(cluster_name, namespace, v1)
secret = v1.read_namespaced_secret(secret_name, namespace).data

ca_cert = secret.get("ca.crt")
ca_key = secret.get("ca.key")

Expand Down
4 changes: 4 additions & 0 deletions tests/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2619,6 +2619,10 @@ def test_generate_tls_cert(mocker):
test the function codeflare_sdk.utils.generate_ca_cert generates the correct outputs
"""
mocker.patch("kubernetes.config.load_kube_config", return_value="ignore")
mocker.patch(
"codeflare_sdk.utils.generate_cert.get_secret_name",
return_value="ca-secret-cluster",
)
mocker.patch(
"kubernetes.client.CoreV1Api.read_namespaced_secret",
side_effect=secret_ca_retreival,
Expand Down