Monitor SSL certificate expiration and publish data to Oracle Cloud Infrastructure (OCI) Monitoring.
This OCI Function calculates the number of days remaining until an SSL certificate expires for a specified endpoint and publishes the metric to OCI Monitoring.
-
OCI Tenancy: Access to an OCI tenancy with required permissions.
- Resource Creation: Before proceeding with group creation and policies, you must first create the required resource (e.g., an OCI Function or a Resource Scheduler) and obtain its OCID.
-
OCI Function Creation:
- Deploy the function using OCI's Functions service.
- Obtain the function's OCID.
-
Dynamic Group Setup:
- Create a dynamic group (
CertMonitoringFunc-DG
) to include your function's OCID:ANY {resource.id = '<ocid1.fnfunc.oc1...>'}
- Create dynamic group (
ResourceScheduler-DG
) including your resource scheduler OCID:ALL {resource.type='resourceschedule', resource.id ='ocid1.resourceschedule.oc1>'}
- Create a dynamic group (
-
IAM Policies:
- Grant permissions to the dynamic group:
Allow dynamic-group CertMonitoringFunc-DG to manage metrics in compartment <compartment_name> Allow dynamic-group CertMonitoringFunc-DG to read functions-family in compartment <compartment_name>
- Add a policy to enable the dynamic group to trigger OCI function
Allow dynamic-group ResourceScheduler-DG to manage functions-family in compartment <compartment_name>
- Grant permissions to the dynamic group:
-
Environment Variables:
- Configure the following variables for the function:
ENDPOINTS
: A comma-separated list of endpoints (e.g.,example.com,service.com
).NAMESPACE
: The custom namespace for OCI Monitoring metrics (e.g.,CertificateMonitoring
).METRIC_NAME
: The name of the metric to be published (e.g.,CertificateExpiryDays
).
- Configure the following variables for the function:
-
View Custom Metrics: After the function runs for the first time and pushes custom metrics to Monitoring, you can view the results in the Metrics Explorer by selecting the relevant
Compartment
,Metric Namespace
, andMetric Name
. -
Create Alarms: As the next step, create an appropriate alarm to monitor the custom metrics and configure the delivery method, such as email, to receive notifications.
- SSL Certificate Monitoring:
- Checks the number of days remaining until the SSL certificate expires for specified endpoints.
- Ensures proactive monitoring to avoid service disruption.
- Logs with timestamps and error categories for better debugging.
- Errors are clearly classified into:
- Connection failures
- TLS handshake issues
- Certificate retrieval failures
- OCI Monitoring Integration:
- Publishes custom metrics to OCI Monitoring for real-time analysis.
- Retry Mechanism:
- Uses exponential backoff with jitter to improve reliability.
- If a TLS handshake fails, the function retries up to 3 times with increasing wait times.
- Concurrent Processing:
- Processes multiple endpoints in parallel to improve efficiency.
- Timeout Handling:
- Uses context-based timeout to prevent long-running operations on unresponsive endpoints.
- Graceful Handling of Partial Failures
- Even if some endpoints fail, the function continues processing other endpoints.
- Metrics Publication:
- Publishes the metric data for each endpoint's certificate expiration.
-
Clone the repository:
git clone <repository_url> cd <repository_directory>
-
Ensure Go 1.23+ is installed.
-
Set environment variables:
export ENDPOINTS=example.com,service.com export NAMESPACE=CertificateMonitoring export METRIC_NAME=CertificateExpiryDays
-
Execute the function locally:
go run main.go
-
Build and deploy with Docker:
docker build --platform=linux/amd64 -t <region_code>.ocir.io/<namespace>/certificate-check:v1.0.0 . docker push <region_code>.ocir.io/<namespace>/certificate-check:v1.0.0
-
Deploy the function to OCI Functions:
- Specify the Docker image (
<region_code>.ocir.io/<namespace>/certificate-check:v1.0.0
) in the function configuration.
- Specify the Docker image (
-
Invoke the function:
oci fn function invoke --function-id <function_ocid> --body ""
- Endpoint Certificate Check:
- The function connects to each endpoint over TLS/SSL.
- It retrieves the SSL certificate and calculates the days remaining until expiration.
- Metrics Client Initialization:
- A monitoring client is initialized using OCI's
ResourcePrincipalConfigurationProvider
.
- A monitoring client is initialized using OCI's
- Metric Publishing:
- Metrics, including the number of days remaining for each endpoint's certificate expiry, are optionally published to OCI Monitoring.
- Logs and Results:
- Logs provide a summary of successful and failed operations, including any errors encountered.
- Metric Name:
CertificateExpiryDays
(or specified via theMETRIC_NAME
environment variable). - Namespace: As specified by the
NAMESPACE
environment variable (e.g.,CertificateMonitoring
). - Dimensions:
- Each metric includes the
resourceId
dimension, identifying the monitored endpoint.
- Each metric includes the
-
Common Issues:
- Ensure
ENDPOINTS
are in the correct format (hostname1,hostaname2,hostname3
). - Confirm IAM policies are properly configured and propagated.
- Check OCI Function logs for environment variable or permission errors.
- Ensure
-
Timeouts and Errors:
- If a timeout occurs or the endpoint returns an error, results and logs will indicate the failure:
Failed to process endpoint: <endpoint>, Error: <error_message>
- If a timeout occurs or the endpoint returns an error, results and logs will indicate the failure:
-
Security:
- Avoid using
InsecureSkipVerify
for production deployments. Update the TLS configuration accordingly.
- Avoid using