Skip to content

Commit b152969

Browse files
feat: add tls information into info command output
Signed-off-by: Ben G <[email protected]>
1 parent de408df commit b152969

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/facade/dragonfly_listener.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,33 @@ void OverriddenSSLFree(void* addr, const char* file, int line) {
133133
mi_free(addr);
134134
}
135135

136+
TLSCertificateInfo GetCertInfoFromCtx(const SSL_CTX* ctx) {
137+
TLSCertificateInfo info;
138+
139+
X509* cert = SSL_CTX_get0_certificate(ctx);
140+
if (!cert) {
141+
info.commonName = "none";
142+
info.issueDate = 0;
143+
info.expirationDate = 0;
144+
return info;
145+
}
146+
147+
X509_NAME* subject = X509_get_subject_name(cert);
148+
int loc = X509_NAME_get_index_by_NID(subject, NID_commonName, -1);
149+
if (loc >= 0) {
150+
X509_NAME_ENTRY* entry = X509_NAME_get_entry(subject, loc);
151+
X509_NAME_get_text_ex(entry->value, info.commonName.data(), 256, 0);
152+
}
153+
154+
ASN1_TIME* notBefore = X509_get_notBefore(cert);
155+
ASN1_TIME* notAfter = X509_get_notAfter(cert);
156+
157+
info.issueDate = ASN1_GetTimeT(notBefore);
158+
info.expirationDate = ASN1_GetTimeT(notAfter);
159+
160+
return info;
161+
}
162+
136163
} // namespace
137164

138165
Listener::Listener(Protocol protocol, ServiceInterface* si, Role role)
@@ -221,9 +248,11 @@ bool Listener::ReconfigureTLS() {
221248
if (!ctx) {
222249
return false;
223250
}
251+
cert_info_ = GetCertInfoFromCtx(ctx);
224252
ctx_ = ctx;
225253
} else {
226254
ctx_ = nullptr;
255+
cert_info_ = TLSCertificateInfo{};
227256
}
228257

229258
if (prev_ctx) {

src/facade/dragonfly_listener.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ namespace facade {
2626
class ServiceInterface;
2727
class Connection;
2828

29+
struct TLSCertificateInfo {
30+
string commonName;
31+
int64_t expirationDate;
32+
int64_t issueDate;
33+
};
34+
2935
class Listener : public util::ListenerInterface {
3036
public:
3137
// The Role PRIVILEGED is for admin port/listener
@@ -55,6 +61,13 @@ class Listener : public util::ListenerInterface {
5561
return protocol_;
5662
}
5763

64+
const TLSCertificateInfo& GetCertInfo() const {
65+
return cert_info_;
66+
}
67+
bool HasTLS() const {
68+
return ctx_ != nullptr;
69+
}
70+
5871
private:
5972
util::Connection* NewConnection(ProactorBase* proactor) final;
6073
ProactorBase* PickConnectionProactor(util::FiberSocketBase* sock) final;
@@ -79,6 +92,7 @@ class Listener : public util::ListenerInterface {
7992

8093
Protocol protocol_;
8194
SSL_CTX* ctx_ = nullptr;
95+
TLSCertificateInfo cert_info_;
8296
};
8397

8498
// Dispatch tracker allows tracking the dispatch state of connections and blocking until all

src/server/server_family.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2978,6 +2978,16 @@ string ServerFamily::FormatInfoMetrics(const Metrics& m, std::string_view sectio
29782978
append("executable", base::kProgramName);
29792979
absl::CommandLineFlag* flagfile_flag = absl::FindCommandLineFlag("flagfile");
29802980
append("config_file", flagfile_flag->CurrentValue());
2981+
2982+
for (const facade::Listener* listener : listeners_) {
2983+
if (listener->HasTLS()) {
2984+
const auto& cert = listener->GetCertInfo();
2985+
append("tls_cert_common_name", cert.commonName);
2986+
append("tls_cert_issue_date", std::to_string(cert.issueDate));
2987+
append("tls_cert_expiration_date", std::to_string(cert.expirationDate));
2988+
break;
2989+
}
2990+
}
29812991
};
29822992

29832993
auto add_clients_info = [&] {

0 commit comments

Comments
 (0)