Hone logo
Hone
Problems

Secure Data Access Control with Row-Level and Column-Level Security

This challenge focuses on implementing advanced security features in a SQL database to protect sensitive information. You will be tasked with designing and implementing a system that restricts data access based on user roles and the sensitivity of individual data fields, ensuring that only authorized personnel can view specific information. This is crucial for compliance with data privacy regulations and for maintaining the integrity of confidential data.

Problem Description

You are responsible for a customer database containing personal identifiable information (PII) and order history. The company has strict policies regarding data access. Different user roles within the organization (e.g., "Customer Support," "Marketing," "Sales," "Management") should have varying levels of access to customer data. Furthermore, certain fields within the customer table are considered highly sensitive (e.g., "credit_card_number," "social_security_number") and should only be accessible by specific, high-privilege roles.

Your task is to implement a security mechanism that enforces these access controls using advanced SQL security features. Specifically, you need to implement:

  1. Row-Level Security (RLS): Restrict which rows (customers) a user can see based on their role. For instance, a "Sales" representative might only see customers within their assigned region, while "Management" can see all customers.
  2. Column-Level Security (CLS): Restrict which columns (data fields) a user can see. For example, "Customer Support" should see most customer details but not sensitive financial information like "credit_card_number."

You will need to define the roles, the security policies, and demonstrate how these policies affect data retrieval for different user types.

Examples

Example 1: Customer Support Role

Let's assume a Customers table with columns: customer_id, name, email, address, phone_number, credit_card_number, region.

And a Users table with user_id, username, role.

And a UserRoles table mapping users to specific roles (e.g., user_id, role_name).

  • Scenario: A user with the role "Customer Support" queries for all customer data.

  • Input Data (Conceptual):

    • Customers table:
      customer_idnameemailaddressphone_numbercredit_card_numberregion
      101Alice Smithalice@example.com123 Main St555-12341111-2222-3333-4444North
      102Bob Johnsonbob@example.com456 Oak Ave555-56785555-6666-7777-8888South
      103Carol Whitecarol@example.com789 Pine Ln555-90129999-0000-1111-2222North
    • Users table:
      user_idusername
      1support_user1
    • UserRoles table:
      user_idrole_name
      1Customer Support
  • Pseudocode Query:

    -- Assume 'support_user1' is currently logged in
    -- and the security policies are active.
    SELECT * FROM Customers;
    
  • Expected Output:

Example 2: Sales Role with Region Restriction

  • Scenario: A user with the role "Sales" queries for all customer data. The "Sales" role is restricted to only view customers in the "North" region.
  • Input Data: Same as Example 1.
  • Pseudocode Query:
    -- Assume 'sales_user1' is currently logged in
    -- and the security policies are active.
    SELECT * FROM Customers;
    
  • Expected Output:
    customer_idnameemailaddressphone_numberregion
    101Alice Smithalice@example.com123 Main St555-1234North
    103Carol Whitecarol@example.com789 Pine Ln555-9012North
  • Explanation: The "Sales" role is restricted by RLS to only see rows where region is 'North'. CLS is also applied, so credit_card_number is hidden.

Example 3: Management Role with Full Access (Except Sensitive Data)

  • Scenario: A user with the role "Management" queries for all customer data. This role can see all rows but not highly sensitive PII like credit_card_number or social_security_number (assuming a hypothetical social_security_number column).
  • Input Data: Same as Example 1 (and imagine social_security_number column exists and has values).
  • Pseudocode Query:
    -- Assume 'management_user1' is currently logged in
    -- and the security policies are active.
    SELECT * FROM Customers;
    
  • Expected Output:
    customer_idnameemailaddressphone_numberregion
    101Alice Smithalice@example.com123 Main St555-1234North
    102Bob Johnsonbob@example.com456 Oak Ave555-5678South
    103Carol Whitecarol@example.com789 Pine Ln555-9012North
  • Explanation: The "Management" role can see all rows (no RLS on region). However, CLS is applied to hide credit_card_number and social_security_number.

Constraints

  • The Customers table can contain up to 1,000,000 rows.
  • The Users table can contain up to 1,000 users.
  • The UserRoles table can contain up to 5,000 entries (a user can have multiple roles).
  • Queries should execute within 5 seconds on average for a dataset of 100,000 customers.
  • Your implementation must use SQL security features such as Row-Level Security (RLS) and Column-Level Security (CLS) or equivalent mechanisms provided by the target SQL dialect (e.g., Views with permissions, dynamic data masking).

Notes

  • Consider how you will manage user roles and their associated permissions. A dedicated table for roles and their mappings to users is recommended.
  • Think about how to dynamically apply these security policies. Many SQL dialects offer policy-based approaches.
  • The "equivalent mechanisms" mentioned in constraints allow for flexibility if your specific SQL dialect doesn't directly support the exact terms "RLS" and "CLS" but offers functional equivalents. For example, using carefully crafted views and granting permissions on those views can achieve similar results.
  • Your solution should demonstrate how to define the policies and how a query from a user of a specific role would be affected. You do not need to implement a full user authentication system, but you should show how a simulated user context would influence the query results.
Loading editor...
plaintext
customer_idnameemailaddressphone_numberregion
101Alice Smithalice@example.com123 Main St555-1234North
102Bob Johnsonbob@example.com456 Oak Ave555-5678South
103Carol Whitecarol@example.com789 Pine Ln555-9012North
  • Explanation: The "Customer Support" role has access to all rows (no RLS applied based on region in this example) but not to the credit_card_number column (CLS applied).