PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 13, 2022

[FIXED] How to configure SASL enabled memcached username and password on mac

 November 13, 2022     memcached, memcachedb, sasl, spymemcached     No comments   

Issue

I installed memcached version 1.4.34 on my mac using homebrew. I wanted to configure a username and password to enable SASL support when interacting with memcache. Can you point me the right direction for this? Ran below command to install memcache on mac.

brew install memcached --enable-sasl-pwdb

Above command installed memcache with sasl support.

echo "mech_list: plain" > memcached.conf
echo "myuser:mypass" > /tmp/memcached-sasl-db
export MEMCACHED_SASL_PWDB=/tmp/memcached-sasl-db
export SASL_CONF_PATH=`pwd`/memcached.conf
memcached -u myuser -m 1024 -p 8010 -S -B binary -vvv

Initialized SASL.

Now when I connect via memcache client it says Password verification failed on the terminal.

mech:  ``PLAIN'' with 15 bytes of data
INFO: User <myuser@mylocal-macbook.local> failed to authenticate
SASL (severity 2): Password verification failed
sasl result code:  -20
Unknown sasl response:  -20

Here's the java code I'm using:

public class MemcacheTest {

public static void main(String[] args) {
    System.setProperty("net.spy.memcached.auth.AuthThreshold", "10");

    AuthDescriptor ad = new AuthDescriptor(new String[] { "PLAIN" },
            new PlainCallbackHandler(
                    "myuser", "mypass"));

    ConnectionFactory connFactory = new ConnectionFactoryBuilder()
            .setProtocol(ConnectionFactoryBuilder.Protocol.BINARY)
            .setAuthWaitTime(10000)
            .setOpTimeout(10000)
            .setShouldOptimize(true)
            .setAuthDescriptor(ad).build();

    List<InetSocketAddress> servers = AddrUtil
            .getAddresses("localhost:8010");
    MemcachedClient cacheClient = null;
    try {
        cacheClient = new MemcachedClient(connFactory, servers);
        cacheClient.set("foo", 50000, "bar");
        System.out.println("Value: " + cacheClient.get("foo"));
    } catch (IOException iox) {
        iox.printStackTrace();
    }
}

}

Here are my intellij logs:

    2017-02-23 15:19:04.223 INFO net.spy.memcached.MemcachedConnection:  Reconnection due to exception handling a memcached operation on {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=1}. This may be due to an authentication failure.
OperationException: SERVER: Auth failure.
    at net.spy.memcached.protocol.BaseOperationImpl.handleError(BaseOperationImpl.java:192)
    at net.spy.memcached.protocol.binary.OperationImpl.finishedPayload(OperationImpl.java:204)
    at net.spy.memcached.protocol.binary.SASLBaseOperationImpl.finishedPayload(SASLBaseOperationImpl.java:98)
    at net.spy.memcached.protocol.binary.OperationImpl.readPayloadFromBuffer(OperationImpl.java:196)
    at net.spy.memcached.protocol.binary.OperationImpl.readFromBuffer(OperationImpl.java:139)
    at net.spy.memcached.MemcachedConnection.readBufferAndLogMetrics(MemcachedConnection.java:861)
    at net.spy.memcached.MemcachedConnection.handleReads(MemcachedConnection.java:840)
    at net.spy.memcached.MemcachedConnection.handleReadsAndWrites(MemcachedConnection.java:720)
    at net.spy.memcached.MemcachedConnection.handleIO(MemcachedConnection.java:683)
    at net.spy.memcached.MemcachedConnection.handleIO(MemcachedConnection.java:436)
    at net.spy.memcached.MemcachedConnection.run(MemcachedConnection.java:1446)

Solution

Yay! I got the issue. Couch base client automatically adds hostname at the end of the username. In my case when I set the username as myuser:mypassword but when I pass myuser in my java code, memcacheclient pass that information as myuser@mylocalhost-mac and that's where the mismatch was happening. To solve that I added the complete username with local host of the ssl db.

Instead of below line

echo "myuser:mypass" > /tmp/memcached-sasl-db

is replaced by

echo "myuser@mylocalhostMacBook-Pro-2.local:mypass" > /tmp/memcached-sasl-db

Now in java client I just pass myuser and client add the hostname automatically.



Answered By - User5817351
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing