Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Libmemcached do not setup setSaslAuthData on Memcached #13394

Closed
jyyan opened this issue May 31, 2018 · 2 comments

Comments

@jyyan
Copy link

commented May 31, 2018

Questions should go to https://forum.phalconphp.com YES
Documentation issues should go to https://github.com/phalcon/docs/issues

Just try to deploy my app example to Heroku, and using heroku addon memcachedcloud the memcached service to store my session data.

from the DOC of memcachedcloud service. we know to have using setSaslAuthData method to setup the user_name & user_password

such like :

$mc = new Memcached();
$mc->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$mc->addServers(array_map(function($server) { return explode(':', $server, 2); }, explode(',', $_ENV['MEMCACHEDCLOUD_SERVERS'])));
$mc->setSaslAuthData($_ENV['MEMCACHEDCLOUD_USERNAME'], $_ENV['MEMCACHEDCLOUD_PASSWORD']);

but during looking on the source code of /phalcon/cache/backend/libmemcached.zep#L115
in the _connect method, the memcached object was not try to setup with setSaslAuthData .

In current phalcon version 3.4.0 & 3.3.2 , i can not using heroku memcachedcloud addon. without setSaslAuthData method.

here is my temporary solution :

/*
step 1 : 
extent current Phalcon\Cache\Backend\Libmemcached class 
and override the _connect method
*/
class MyLibmemcached extends Phalcon\Cache\Backend\Libmemcached{
  function __construct($frontend, $options){
    parent::__construct($frontend, $options);
  }

  /**
   * Create internal connection to memcached
   */
  public function _connect()
  {
    $options = $this->_options;

    /* 
      ....
    */
    $memcache = new \Memcached($persistentId);
    /* 
      ....
    */
    // ADD the setSaslAuthData
    if(!empty($options['saslAuthData']['username']) && !empty($options['saslAuthData']['password'])) {
      $memcache->setSaslAuthData($options['saslAuthData']['username'], $options['saslAuthData']['password']);
    }
    $this->_memcache = $memcache;
  }

}
/*
step 2 : 
extent current Phalcon\Session\Adapter\Libmemcached class 
and override the __construct method
*/
use Phalcon\Cache\Frontend\Data as FrontendData;

class MySession extends Phalcon\Session\Adapter\Libmemcached{
  function __construct($options){

    parent::__construct($options);
    /* 
      ....
    */
    $saslAuthData = (!isset($options['saslAuthData']) ? [
      'username' => null,
      'password' => null,
    ] : $options['saslAuthData']);
    if (!isset($options['saslAuthData'])) {
      throw new Exception('No servers given in options');
    }

    $this->_libmemcached = new MyLibmemcached(
      new FrontendData(['lifetime' => $this->_lifetime]),
      [
        'servers' =>  $servers,
        'client' =>   $client,
        'prefix' =>   $prefix,
        'statsKey' => $statsKey,
        'persistent_id' => $persistentId,
        'saslAuthData' => $saslAuthData,
      ]
    );

  }
}

/*
 step 3 :
 at service session bootstrap, just passing the username / password from Heroku $_ENV
*/
  if(isset($_ENV['MEMCACHEDCLOUD_SERVERS'])) {
    $mc_servers = array_map(function($server) { return explode(':', $server, 2); }, explode(',', $_ENV['MEMCACHEDCLOUD_SERVERS']));
  } else {
    $mc_servers =[[
      "host"   => "127.0.0.1",
      "port"   => 11211,
      "weight" => 1,
    ]];
  }

  $session = new MySession(
    [
      "servers" => $mc_servers,
      "client" => [
        \Memcached::OPT_HASH       => \Memcached::HASH_MD5,
        \Memcached::OPT_PREFIX_KEY => "prefix.",
        \Memcached::OPT_BINARY_PROTOCOL => true,
      ],
      "lifetime" => 3600,
      "prefix"   => "my_",
      "saslAuthData" => [
        'username' => (isset($_ENV['MEMCACHEDCLOUD_USERNAME']) ? $_ENV['MEMCACHEDCLOUD_USERNAME'] : null),
        'password' => (isset($_ENV['MEMCACHEDCLOUD_PASSWORD']) ? $_ENV['MEMCACHEDCLOUD_PASSWORD'] : null)
      ],
    ]
  );

now, i can using the heroku memcachedcloud addon as my session service.

Details

  • Phalcon version: (local = 3.4.0 & heroku = 3.3.2)
  • PHP Version: (7.0.30)
  • Operating System: ubunut / heroku
  • Installation type: installing via package manager
  • Zephir version (if any):
  • Server: Nginx
  • Other related info (Database, table schema):

@sergeyklay sergeyklay added this to the 4.0.0 milestone Jun 1, 2018

@niden niden self-assigned this Dec 18, 2018

@niden

This comment has been minimized.

Copy link
Member

commented Feb 23, 2019

Closing in favor of #13855. Will revisit if the community votes for it, or in later versions.

@niden niden closed this Feb 23, 2019

@niden niden added the Enhancement label May 13, 2019

@niden niden added this to To do in 4.0 Release via automation May 13, 2019

@niden niden reopened this May 13, 2019

4.0 Release automation moved this from To do to In progress May 13, 2019

@niden niden removed this from the 4.0.0 milestone May 14, 2019

@niden niden referenced this issue May 14, 2019

Closed

T13394 libmemcached sasl #14069

3 of 4 tasks complete

@niden niden referenced this issue May 23, 2019

Merged

Added sasl authentication #14120

3 of 5 tasks complete
@niden

This comment has been minimized.

Copy link
Member

commented Jun 20, 2019

Resolved in #14120

@niden niden closed this Jun 20, 2019

4.0 Release automation moved this from In progress to Done Jun 20, 2019

@niden niden added the 4.0 label Jun 21, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
3 participants
You can’t perform that action at this time.