#!/usr/bin/perl
 
use strict;
use warnings;
use Crypt::OpenSSL::X509;
use Time::Piece;

my $cert_path = '/etc/certes/ca_cert.pem';

# Read the certificate
my $x509;
eval {
	$x509 = Crypt::OpenSSL::X509->new_from_file($cert_path);
} or do {
	print('{"certes_ca_status": "nocert"}' . "\n");
	exit(0);
};

my $notafter;
eval {
	$notafter = Time::Piece->strptime($x509->notAfter(),
		 '%B %d %H:%M:%S %Y %Z');
} or do {
	print('{"certes_ca_status": "invalid"}' . "\n");
	exit(0);
};
my $now = gmtime();

my $v = 'ready';
if ($now > $notafter) {
	$v = 'expired';
}

print("{\"certes_ca_status\": \"$v\"}\n");
 
exit(0);
