Forum Discussion
NirBY
3 months agoNew Member
I have used this Script Assertion for verify SSL, it verify certificate has >10 days. hope this help..
import java.security.cert.Certificate
import java.security.cert.X509Certificate
import javax.net.ssl.HttpsURLConnection
import java.net.URL
import java.time.LocalDate
import java.time.ZoneId
import java.time.temporal.ChronoUnit
try {
// Dynamically get the endpoint URL from the current TestRequest
def endpointUrl = messageExchange.modelItem.endpoint // Gets the endpoint URL of the current request
// Check if the endpoint URL is a dynamic property expression
if (endpointUrl.contains('${')) {
// Expand the property expression to get the actual URL
endpointUrl = context.expand(endpointUrl)
}
log.info("Verify SSL of current URL:" + endpointUrl)
// Check if the expanded endpoint URL is not empty
if (!endpointUrl) {
log.error("Expanded endpoint URL is empty or not defined. Please provide a valid URL.")
return false
}
// Check if the URL starts with the correct protocol (e.g., "https://")
if (!endpointUrl.startsWith("https://")) {
log.error("Invalid URL format. The URL must start with 'https://'. Provided URL: ${endpointUrl}")
return false
}
// Open connection to the endpoint
def url = new URL(endpointUrl)
def connection = url.openConnection() as HttpsURLConnection
connection.connect()
// Retrieve the server certificates
Certificate[] certs = connection.getServerCertificates()
X509Certificate x509Cert = certs[0] as X509Certificate
// Get the expiration date of the certificate
Date expiryDate = x509Cert.getNotAfter()
LocalDate expiryLocalDate = expiryDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()
LocalDate currentDate = LocalDate.now()
// Calculate the days remaining until the certificate expires
long daysUntilExpiry = ChronoUnit.DAYS.between(currentDate, expiryLocalDate)
// Check if the certificate expires in more than 10 days
if (daysUntilExpiry > 10) {
log.info("The certificate expires in $daysUntilExpiry days, which is more than 10 days.")
return true
} else {
log.error("The certificate expires in $daysUntilExpiry days, which is less than or equal to 10 days.")
return false
}
} catch (MalformedURLException e) {
log.error("Malformed URL: ${e.message}")
return false
} catch (Exception e) {
// Handle any other errors that occur during the execution
log.error("An error occurred while verifying the certificate: ${e.message}")
return false
}
Related Content
- 3 years ago
- 7 years ago
- 13 years ago