-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add services parameter to 3 more checks and separate K8s cluster cert…
… check (#1010) Co-authored-by: abhishek-unskript <[email protected]>
- Loading branch information
1 parent
5f8f6c4
commit 6b0afa9
Showing
14 changed files
with
205 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
27 changes: 27 additions & 0 deletions
27
Kubernetes/legos/k8s_get_expiring_cluster_certificate/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[<img align="left" src="https://unskript.com/assets/favicon.png" width="100" height="100" style="padding-right: 5px">](https://unskript.com/assets/favicon.png) | ||
<h1>Check the valifity of K8s certificate for a cluster. </h1> | ||
|
||
## Description | ||
This action checks if the certificate is expiring for a K8s cluster. | ||
|
||
|
||
## Lego Details | ||
|
||
k8s_get_expiring_cluster_certificate(handle, expiring_threshold: int = 7) | ||
|
||
handle: Object of type unSkript K8S Connector | ||
expiration_threshold (int): The threshold (in days) for considering a certificate as expiring soon. | ||
|
||
## Lego Input | ||
|
||
This Lego take three inputs handle, expiration_threshold. | ||
|
||
|
||
## Lego Output | ||
Here is a sample output. | ||
<img src="./1.png"> | ||
|
||
|
||
## See it in Action | ||
|
||
You can see this Lego in action following this link [unSkript Live](https://us.app.unskript.io) |
File renamed without changes.
6 changes: 3 additions & 3 deletions
6
...icates/k8s_get_expiring_certificates.json → ...k8s_get_expiring_cluster_certificate.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...rnetes/legos/k8s_get_expiring_cluster_certificate/k8s_get_expiring_cluster_certificate.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
## | ||
# Copyright (c) 2023 unSkript, Inc | ||
# All rights reserved. | ||
## | ||
from pydantic import BaseModel, Field | ||
from typing import Optional, Tuple | ||
import base64 | ||
import datetime | ||
from cryptography import x509 | ||
from cryptography.hazmat.backends import default_backend | ||
|
||
|
||
class InputSchema(BaseModel): | ||
expiring_threshold: Optional[int] = Field( | ||
default=7, | ||
title='Expiration Threshold (in days)', | ||
description='Expiration Threshold of certificates (in days). Default- 90 days') | ||
|
||
def k8s_get_expiring_cluster_certificate_printer(output): | ||
if output is None: | ||
return | ||
success, data = output | ||
if not success: | ||
print(data) | ||
else: | ||
print("K8s certificate is valid.") | ||
|
||
def get_expiry_date(pem_data: str) -> datetime.datetime: | ||
cert = x509.load_pem_x509_certificate(pem_data.encode(), default_backend()) | ||
return cert.not_valid_after | ||
|
||
def k8s_get_expiring_cluster_certificate(handle, expiring_threshold:int=7) -> Tuple: | ||
""" | ||
Check the validity for a K8s cluster certificate. | ||
Args: | ||
handle: Object of type unSkript K8S Connector | ||
expiration_threshold (int): The threshold (in days) for considering a certificate as expiring soon. | ||
Returns: | ||
tuple: Status, details of the certificate. | ||
""" | ||
result = [] | ||
try: | ||
# Fetch cluster CA certificate | ||
ca_cert = handle.run_native_cmd("kubectl get secret -o jsonpath=\"{.items[?(@.type=='kubernetes.io/service-account-token')].data['ca\\.crt']}\" --all-namespaces") | ||
if ca_cert.stderr: | ||
raise Exception(f"Error occurred while fetching cluster CA certificate: {ca_cert.stderr}") | ||
|
||
# Decode and check expiry date of the cluster's CA certificate | ||
ca_cert_decoded = base64.b64decode(ca_cert.stdout.strip()).decode("utf-8") | ||
ca_cert_exp = get_expiry_date(ca_cert_decoded) | ||
days_remaining = (ca_cert_exp - datetime.datetime.now()).days | ||
if days_remaining < 0: | ||
# Certificate has already expired | ||
result.append({ | ||
"certificate": "Kubeconfig Cluster certificate", | ||
"days_remaining": days_remaining, | ||
"status": "Expired" | ||
}) | ||
elif ca_cert_exp < datetime.datetime.now() + datetime.timedelta(days=expiring_threshold): | ||
result.append({ | ||
"certificate": "Kubeconfig Cluster certificate", | ||
"days_remaining": days_remaining, | ||
"status": "Expiring Soon" | ||
}) | ||
except Exception as e: | ||
print(f"Error occurred while checking cluster CA certificate: {e}") | ||
raise e | ||
|
||
if len(result) != 0: | ||
return (False, result) | ||
return (True, None) |
8 changes: 4 additions & 4 deletions
8
...s/k8s_get_expiring_certificates/README.md → ...xpiring_tls_secret_certificates/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
14 changes: 14 additions & 0 deletions
14
...os/k8s_get_expiring_tls_secret_certificates/k8s_get_expiring_tls_secret_certificates.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"action_title": "Get expiring secret certificates", | ||
"action_description": "Get the expiring secret certificates for a K8s cluster.", | ||
"action_type": "LEGO_TYPE_K8S", | ||
"action_entry_function": "k8s_get_expiring_tls_secret_certificates", | ||
"action_is_check": true, | ||
"action_needs_credential": true, | ||
"action_supports_poll": true, | ||
"action_supports_iteration": true, | ||
"action_output_type": "ACTION_OUTPUT_TYPE_LIST", | ||
"action_categories": [ "CATEGORY_TYPE_CLOUDOPS", "CATEGORY_TYPE_DEVOPS", "CATEGORY_TYPE_SRE" ,"CATEGORY_TYPE_K8S"], | ||
"action_next_hop": [""], | ||
"action_next_hop_parameter_mapping": {} | ||
} |
Oops, something went wrong.