LCOV - code coverage report
Current view: top level - src/impl - ssl_options.cc (source / functions) Hit Total Coverage
Test: vlink Lines: 52 52 100.0 %
Date: 2026-06-27 19:56:04 Functions: 3 3 100.0 %
Branches: 35 36 97.2 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (C) 2026 by Thun Lu. All rights reserved.
       3                 :            :  * Author: Thun Lu <thun.lu@zohomail.cn>
       4                 :            :  * Repo:   https://github.com/thun-res/vlink
       5                 :            :  *  _    __   __      _           __
       6                 :            :  * | |  / /  / /     (_) ____    / /__
       7                 :            :  * | | / /  / /     / / / __ \  / //_/
       8                 :            :  * | |/ /  / /___  / / / / / / / ,<
       9                 :            :  * |___/  /_____/ /_/ /_/ /_/ /_/|_|
      10                 :            :  *
      11                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
      12                 :            :  * you may not use this file except in compliance with the License.
      13                 :            :  * You may obtain a copy of the License at
      14                 :            :  *
      15                 :            :  *     http://www.apache.org/licenses/LICENSE-2.0
      16                 :            :  *
      17                 :            :  * Unless required by applicable law or agreed to in writing, software
      18                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      19                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      20                 :            :  * See the License for the specific language governing permissions and
      21                 :            :  * limitations under the License.
      22                 :            :  */
      23                 :            : 
      24                 :            : #include "./impl/ssl_options.h"
      25                 :            : 
      26                 :            : #include <string>
      27                 :            : 
      28                 :            : #include "./base/helpers.h"
      29                 :            : #include "./base/utils.h"
      30                 :            : #include "./impl/conf.h"
      31                 :            : 
      32                 :            : namespace vlink {
      33                 :            : 
      34                 :            : // SslOptions
      35   [ +  +  +  + ]:        222 : bool SslOptions::is_valid() const noexcept { return !ca_file.empty() || !cert_file.empty(); }
      36                 :            : 
      37                 :        217 : SslOptions SslOptions::parse_from(const Conf::PropertiesMap& properties) noexcept {
      38                 :        217 :   SslOptions options;
      39                 :            : 
      40                 :        434 :   const std::string env_verify = Utils::get_env("VLINK_SSL_VERIFY");
      41                 :        434 :   const std::string env_ca = Utils::get_env("VLINK_SSL_CA");
      42                 :        434 :   const std::string env_cert = Utils::get_env("VLINK_SSL_CERT");
      43                 :        434 :   const std::string env_key = Utils::get_env("VLINK_SSL_KEY");
      44                 :        434 :   const std::string env_key_pass = Utils::get_env("VLINK_SSL_KEY_PASS");
      45                 :        434 :   const std::string env_sni = Utils::get_env("VLINK_SSL_SNI");
      46                 :        434 :   const std::string env_ciphers = Utils::get_env("VLINK_SSL_CIPHERS");
      47                 :            : 
      48                 :        217 :   options.verify_peer = (env_verify != "0");
      49                 :            : 
      50                 :        217 :   options.ca_file = env_ca;
      51                 :        217 :   options.cert_file = env_cert;
      52                 :        217 :   options.key_file = env_key;
      53                 :        217 :   options.key_password = env_key_pass;
      54                 :        217 :   options.server_name = env_sni;
      55                 :        217 :   options.ciphers = env_ciphers;
      56                 :            : 
      57         [ +  + ]:        320 :   for (const auto& [prop, value] : properties) {
      58         [ +  + ]:        103 :     if VLIKELY (!Helpers::has_startwith(prop, "ssl.")) {
      59                 :         69 :       continue;
      60                 :            :     }
      61                 :            : 
      62         [ +  + ]:         34 :     if (prop == "ssl.ca") {
      63                 :         10 :       options.ca_file = value;
      64         [ +  + ]:         24 :     } else if (prop == "ssl.cert") {
      65                 :          4 :       options.cert_file = value;
      66         [ +  + ]:         20 :     } else if (prop == "ssl.key") {
      67                 :          4 :       options.key_file = value;
      68         [ +  + ]:         16 :     } else if (prop == "ssl.key_password") {
      69                 :          4 :       options.key_password = value;
      70         [ +  + ]:         12 :     } else if (prop == "ssl.verify") {
      71                 :          6 :       options.verify_peer = (value != "0");
      72         [ +  + ]:          6 :     } else if (prop == "ssl.server_name") {
      73                 :          3 :       options.server_name = value;
      74         [ +  - ]:          3 :     } else if (prop == "ssl.ciphers") {
      75                 :          3 :       options.ciphers = value;
      76                 :            :     }
      77                 :            :   }
      78                 :            : 
      79                 :        217 :   return options;
      80                 :        217 : }
      81                 :            : 
      82                 :          8 : void SslOptions::parse_to(Conf::PropertiesMap& properties) const noexcept {
      83         [ +  + ]:          8 :   if (!verify_peer) {
      84                 :          3 :     properties["ssl.verify"] = "0";
      85                 :            :   }
      86                 :            : 
      87         [ +  + ]:          8 :   if (!ca_file.empty()) {
      88                 :          7 :     properties["ssl.ca"] = ca_file;
      89                 :            :   }
      90                 :            : 
      91         [ +  + ]:          8 :   if (!cert_file.empty()) {
      92                 :          3 :     properties["ssl.cert"] = cert_file;
      93                 :            :   }
      94                 :            : 
      95         [ +  + ]:          8 :   if (!key_file.empty()) {
      96                 :          3 :     properties["ssl.key"] = key_file;
      97                 :            :   }
      98                 :            : 
      99         [ +  + ]:          8 :   if (!key_password.empty()) {
     100                 :          3 :     properties["ssl.key_password"] = key_password;
     101                 :            :   }
     102                 :            : 
     103         [ +  + ]:          8 :   if (!server_name.empty()) {
     104                 :          4 :     properties["ssl.server_name"] = server_name;
     105                 :            :   }
     106                 :            : 
     107         [ +  + ]:          8 :   if (!ciphers.empty()) {
     108                 :          2 :     properties["ssl.ciphers"] = ciphers;
     109                 :            :   }
     110                 :          8 : }
     111                 :            : 
     112                 :            : }  // namespace vlink

Generated by: LCOV version 1.14