Skip to content

Ranger Policy Access

Overview

At checkout, the broker calls the Apache Ranger Admin REST API to create a policy granting the requesting user access to a specific resource — an HDFS path, Hive database/table, or HBase table. At checkin, the policy is deleted. No standing policies exist between sessions.

This pattern supports all Ranger-managed services in Cloudera: HDFS, Hive, HBase, Kafka, Ozone, and others.

Before You Begin

  • The broker is running and connected (see Getting Started)
  • curl and jq are installed on the broker host
  • The Ranger service account has the Admin role in Ranger Admin
  • The Ranger service name for the target component is known (e.g. mycluster_hive)

Checkout Routine

Environment variables:

VariableNotes
BRITIVE_USER_EMAILInjected — requesting user’s email; used as the Ranger user
ranger_urlRanger Admin base URL — set as a resource parameter
ranger_serviceRanger service name — set as a resource parameter
ranger_userRanger service account username
ranger_passwordRanger service account password — reference from Britive Secrets Store
resource_pathThe resource to grant access to (e.g. /data/finance for HDFS, mydb.transactions for Hive)
permissionsComma-separated Ranger permissions (e.g. read,write for HDFS, select for Hive)
TRXInjected — used to uniquely name the policy per session

Create a Ranger policy:

# Derive the Ranger username from the email (strip domain)
RANGER_USER_NAME=$(echo "$BRITIVE_USER_EMAIL" | cut -d'@' -f1)

# Build the policy name using TRX for uniqueness
POLICY_NAME="britive-jit-${RANGER_USER_NAME}-${TRX}"

# Convert comma-separated permissions to JSON array
PERMS_JSON=$(echo "$permissions" | tr ',' '\n' | jq -R . | jq -s .)

# Create the policy via the Ranger Admin API
RESPONSE=$(curl --silent --user "${ranger_user}:${ranger_password}" \
  --request POST \
  "${ranger_url}/service/public/v2/api/policy" \
  --header "Content-Type: application/json" \
  --header "Accept: application/json" \
  --data "{
    \"name\": \"${POLICY_NAME}\",
    \"service\": \"${ranger_service}\",
    \"isEnabled\": true,
    \"isAuditEnabled\": true,
    \"resources\": {
      \"path\": { \"values\": [\"${resource_path}\"], \"isRecursive\": true, \"isExcludes\": false }
    },
    \"policyItems\": [{
      \"users\": [\"${RANGER_USER_NAME}\"],
      \"accesses\": $(echo $PERMS_JSON | jq '[.[] | {\"type\": ., \"isAllowed\": true}]'),
      \"delegateAdmin\": false
    }]
  }")

POLICY_ID=$(echo "$RESPONSE" | jq -r '.id')
echo "POLICY_ID=${POLICY_ID}"
echo "POLICY_NAME=${POLICY_NAME}"

The resource structure differs per Ranger service type. For Hive, use database and table keys instead of path. See Ranger REST API reference for the schema for each service type.


Checkin Routine

Delete the Ranger policy using the stored policy ID:

POLICY_ID=$(echo "$BRITIVE_RESPONSE" | jq -r '.POLICY_ID // empty')

if [ -n "$POLICY_ID" ]; then
  curl --silent --user "${ranger_user}:${ranger_password}" \
    --request DELETE \
    "${ranger_url}/service/public/v2/api/policy/${POLICY_ID}"
  echo "Policy ${POLICY_ID} deleted."
else
  # Fallback: look up by name using TRX
  RANGER_USER_NAME=$(echo "$BRITIVE_USER_EMAIL" | cut -d'@' -f1)
  POLICY_NAME="britive-jit-${RANGER_USER_NAME}-${TRX}"

  POLICY_ID=$(curl --silent --user "${ranger_user}:${ranger_password}" \
    "${ranger_url}/service/public/v2/api/policy?policyName=${POLICY_NAME}" \
    -H "Accept: application/json" | jq -r '.[0].id // empty')

  if [ -n "$POLICY_ID" ]; then
    curl --silent --user "${ranger_user}:${ranger_password}" \
      --request DELETE \
      "${ranger_url}/service/public/v2/api/policy/${POLICY_ID}"
    echo "Policy ${POLICY_NAME} (ID: ${POLICY_ID}) deleted."
  else
    echo "Policy not found — may have already been deleted."
  fi
fi

Hive-Specific Policy

For Hive, the resource keys are database and table instead of path. Replace the resources block in the checkout routine:

# Hive policy resource block
"resources": {
  "database": { "values": ["mydb"], "isExcludes": false },
  "table":    { "values": ["transactions"], "isExcludes": false },
  "column":   { "values": ["*"], "isExcludes": false }
}

Configure in Britive

Create a permission

Go to Resource Manager → Resource Type Permissions → New Permission. Set Language to Shell.

Paste the checkout and checkin routines. Declare variables:

VariableSystem definedNotes
BRITIVE_USER_EMAILYesInjected automatically
TRXYesInjected automatically
ranger_urlNoSet per resource
ranger_serviceNoSet per resource
ranger_userNoRanger Admin service account
ranger_passwordNoReference from Britive Secrets Store
resource_pathNoSet on the permission or profile
permissionsNoe.g. read,write or select

Set show_orig_creds = true — no credential is returned to the user; checkin confirmation is the only output.

Create a profile

Go to Resource Manager → Profiles → New Profile. Set an expiration (e.g. 4h). Under Associations, select the resource label(s) for your Cloudera clusters. Under Permissions, add the permission above.

Add a policy

Under Policies, assign members and configure approval conditions.


Verify

Check out the profile

Navigate to My Access → find the profile → Check Out.

Confirm the policy in Ranger Admin

Log in to Ranger Admin and go to the relevant service (e.g. HivePolicies). A policy named britive-jit-<username>-<trx> should appear.

Access the resource

Connect to Hive, HDFS, or HBase using your existing credentials — the Ranger policy now allows the configured access.

# Hive example
beeline -u "jdbc:hive2://hiveserver:10000" -n jdoe -e "SELECT * FROM finance_db.transactions LIMIT 10;"

Check in

Return to My AccessCheck In.

Confirm the policy is deleted

In Ranger Admin, the britive-jit-<username>-<trx> policy should no longer appear. Repeat the data access command — it should fail with Permission denied.


Troubleshoot

SymptomCauseFix
HTTP 400 on policy createWrong resource key for service typeHive uses database/table, HDFS uses path — check the Ranger API schema for your service
Policy created but access still deniedRanger policy cache not refreshedRanger plugins sync on a schedule (default 30s) — wait and retry
HTTP 404 on checkinPolicy ID not found — may have expired or been manually deletedConfirm POLICY_ID is passed correctly via the response; the fallback name lookup handles this case
Policy not deleted at checkinCheckin routine ran with wrong TRXVerify TRX is declared as a system-defined variable — Britive injects the same value for checkout and checkin
Ranger Admin unreachable from brokerNetwork or firewall blockCheck that TCP port 6080 (or 6182 for TLS) is open between broker and Ranger Admin host
Last updated on