【问题标题】:Symfony 1.4 connect to mysql via SSLSymfony 1.4 通过 SSL 连接到 mysql
【发布时间】:2017-11-14 03:13:23
【问题描述】:

我需要更改旧的 Symfony 1.4 应用程序,以便它能够通过 ssl-connection 连接到 mysql。

我为 Symfony >= 2 找到了很多关于此的内容。但不幸的是,对于这个尘土飞扬的人来说不是。

出于验证目的,我已经通过编辑使其工作

./apps/frontend/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Connection.php

$this->dbh = new PDO($this->options['dsn'], $this->options['username'],
(!$this->options['password'] ? '':$this->options['password']), array(PDO::ATTR_PERSISTENT => true));

$this->dbh = new PDO($this->options['dsn'], $this->options['username'],
(!$this->options['password'] ? '':$this->options['password']),  
array(PDO::ATTR_PERSISTENT => true,                           
PDO::MYSQL_ATTR_SSL_KEY  => '/etc/my.cnf.d/ssl/client-key.pem',        
PDO::MYSQL_ATTR_SSL_CERT => '/etc/my.cnf.d/ssl/client-cert.pem',        
PDO::MYSQL_ATTR_SSL_CA   => '/etc/my.cnf.d/ssl/ca-cert.pem'));

但我想知道这个丑陋的 hack 是否真的是唯一的解决方案?

【问题讨论】:

    标签: php mysql ssl doctrine symfony-1.4


    【解决方案1】:

    我花了一段时间才发现这个连接类已经被覆盖了(apps/frontend/lib...)。

    所以我只需要使这些变量可配置。 databases.yml 配置中有一个选项称为 attributes (doctrine::param::attributes)。如果您传递非字符串键,您可以使用 getAttribute 获取它们。

    所以至少它有效(它在 connect 方法的 try 区域内)。

    $sslOptionKeys = array(PDO::MYSQL_ATTR_SSL_KEY, PDO::MYSQL_ATTR_SSL_CERT, PDO::MYSQL_ATTR_SSL_CA);
    
    foreach($sslOptionKeys as $sslOptionKey) {
       if(array_key_exists($sslOptionKey, $this->pendingAttributes)) {
           $pdoOptions[$sslOptionKey] = $this->getAttribute($sslOptionKey);
       }
    }
    
    $this->dbh = new PDO($this->options['dsn'], $this->options['username'],
                         (!$this->options['password'] ? '':$this->options['password']),
                         $pdoOptions);
    

    databases.yml 中,您必须输入以下内容(cmets 有助于理解这些数字)

    all:
      doctrine:
        class: sfDoctrineDatabase
        param:
          dsn:      mysql:host=localhost;dbname=db
          username: user
          password: pass
          encoding: utf8
          attributes:
            #PDO::MYSQL_ATTR_SSL_KEY
            1010: /etc/my.cnf.d/ssl/client-key.pem
            #PDO::MYSQL_ATTR_SSL_CERT
            1011: /etc/my.cnf.d/ssl/client-cert.pem
            #PDO::MYSQL_ATTR_SSL_CA
            1012: /etc/my.cnf.d/ssl/ca-cert.pem
    

    【讨论】:

      【解决方案2】:

      我们发现属性数组不起作用。我们必须添加一个事件侦听器来侦听教义“doctrine.configure_connection”事件并直接在连接上设置属性。

      class ProjectConfiguration extends sfProjectConfiguration
      {
        public function setup()
        {
             //existing code
             
             $this->dispatcher->connect('doctrine.configure_connection', array(
              'ProjectConfiguration','addConnectionSSL'
             ));  
        }
      
         static public function addConnectionSSL(sfEvent $event){
           $connection = $event->getParameters()['connection']; 
           /* @var $connection Doctrine_Manager */
           $other = $connection->getOption('other');
           if(!is_array($other)) $other=array();
           $other[PDO::MYSQL_ATTR_SSL_CA] = "PATH_TO_CERT_FILE"; //Set this to actual path. You can also set other properties in the same way.
           $connection->setOption('other',$other);
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-02
        • 1970-01-01
        • 2018-06-08
        • 2019-04-03
        相关资源
        最近更新 更多