Apache Cassandra

Cassandra List Users

“Apache Cassandra, commonly known as Cassandra, is a free and open-source NoSQL database. Cassandra is well known for its simplicity, high availability, and scalability. In addition, it can handle large amounts of data with security and fault tolerance. As a result, it is a popular choice in environments where data is critical.

Popular environments where Apache Cassandra is used include Netflix, Apple, eBay, Discord, Hulu, Spotify, Yelp, Uber, and many more.

User management is one of the most fundamental roles of a database administrator. When working with Apache Cassandra, you will encounter an instance where you need to gather information about various users in the cluster.”

In this tutorial, you will learn how to view information about users in a Cassandra cluster using Cassandra CQL Shell.

Requirements

To follow along with this tutorial, you will require:

    1. A running Apache Cassandra cluster
    2. Sufficient privileges on the server
    3. Terminal Access.

With the above requirements met, we can proceed.

Apache Cassandra Create User

Before viewing information about users in the cluster, it’s best to ensure we have at least one user on the server.

In Apache Cassandra, we can create a user using the CREATE USER statement.

Start by logging into your CQL Shell. The command syntax is as shown:

$ cqlsh -u <username> -p <password>

 
For example, to log in as a superuser:

$ cqlsh -u cassandra -p cassandra

 

Once logged in, we can create a user using the statement with the syntax as shown:

CREATE USER [ IF NOT EXISTS ] role_name
                          [ WITH [ HASHED ] PASSWORD string ]
                          [ user_option ]

 
For to create a user account under the username “linuxhint,” we can run a query as shown:

CREATE USER linuxhint WITH PASSWORD 'password' SUPERUSER;

 
We can also create a regular user by setting the role as NOSUPERUSER. An example query is shown:

CREATE USER elastic WITH PASSWORD 'elastic' NOSUPERUSER;

 

Apache Cassandra List Users

There are two methods of showing information about users in the cluster depending on the Cassandra version.

Cassandra Version < 2.2

In Apache Cassandra version 2.2 and below, you can use the LIST USERS statement to show the list of users in the cluster.

cassandra@cqlsh> LIST USERS;

 
The query above should return information as shown:

name      | super | datacenters
-----------+-------+-------------
 cassandra |  True |         ALL
   elastic | False |         ALL
 linuxhint |  True |         ALL

 

This returns information such as the username, superuser state, and the data center to which that username belongs.

Cassandra Version >= 2.2

For Cassandra version 2.2 and above, the LIST USERS; statement is deprecated. Therefore, to show user information, use the LIST ROLES; instead:

cassandra@cqlsh> LIST ROLES;

 
An example output is as shown:

role      | super | login | options | datacenters
-----------+-------+-------+---------+-------------
 cassandra |  True |  True |        {} |         ALL
   elastic | False |  True |        {} |         ALL
 linuxhint |  True |  True |        {} |         ALL

 

In this case, the command returns the username, superuser state, login state, various options, and the corresponding data center.

CQL > 3.x

In CQL version 3.x and above, you can query the system_auth table to retrieve user information. An example is as shown:

cassandra@cqlsh> select * from system_auth.roles;

 
An example output:

role      | can_login | is_superuser | member_of | salted_hash
-----------+-----------+--------------+-----------+-----------
   elastic |      True |        False |      null | $2a$10$ouaJz0U
 cassandra |      True |         True |      null | $2a$10$7U2/y/oIpjl
 linuxhint |      True |         True |      null | $2a$10$gsGThUcfMDY

 

Conclusion

In this article, you learned how to fetch user information in your Cassandra cluster using the CQL Shell.

Stay tuned for more!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list