Back to Apache Spark: Core
Q63Question 63 of 72

Spark security

Key Concepts
  • Spark security requires four layers working together: authentication, authorization, encryption, and network isolation.
  • Authentication uses Kerberos in Hadoop environments or IAM/OAuth in cloud, plus spark.authenticate for internal RPC.
  • Authorization is enforced through Apache Ranger, Hive table ACLs, or storage-level permissions for fine-grained access control.
  • Encryption at rest uses HDFS transparent encryption or S3 server-side encryption; in transit uses SSL/TLS for RPC and spark.network.crypto for shuffle.
  • Network isolation places the cluster in private subnets with security groups and firewalls, restricting access to necessary ports only.
  • spark.io.encryption.enabled encrypts intermediate data spilled to local disk by executors.
  • These layers are complementary: authentication without authorization lets any valid user read everything, and strong ACLs are undermined by an exposed unauthenticated port.

Definition

Secure a production Spark cluster across four layers – authentication (Kerberos/tokens), authorization (Ranger/RBAC/ACLs), encryption at rest and in transit (SSL/TLS for RPC and shuffle), and network isolation – defense in depth, not any single control.
  • Authentication – verifies who is connecting; Kerberos in Hadoop, IAM/OAuth in cloud, plus spark.authenticate for internal RPC.
  • Authorization – controls what an authenticated principal may do; Apache Ranger, table ACLs, and storage permissions.
  • Encryption at rest – HDFS transparent encryption or S3 server-side encryption makes stored files unreadable if media is stolen.
  • Encryption in transit – SSL/TLS for Spark RPC and UI, spark.network.crypto for shuffle and block transfers.
  • Network isolation – private subnets, security groups, and firewalls limit who can reach the cluster.
  • Defense in depth – all four layers together; any one alone leaves an obvious gap.

At the National Bank of India, securing a Spark cluster is like securing the vault building. Authentication is the ID check at the door (Kerberos). Authorization is the rule book saying which rooms each employee may open (Ranger). Encryption at rest is locked safes for stored ledgers (HDFS/S3 encryption). Encryption in transit is sealed envelopes between branches (SSL/TLS). Network isolation is the perimeter wall. A bank needs all of these.

Understanding the Concepts

What are the four layers of Spark security?

A production Spark cluster needs defense in depth across authentication, authorization, encryption, and network isolation. No single control is sufficient on its own.

PYTHON
from pyspark.sql import SparkSession

# Production SparkSession with security settings
spark = SparkSession.builder \
    .appName("SecureBankingPipeline") \
    .config("spark.authenticate", "true") \
    .config("spark.authenticate.secret", "${SPARK_SECRET}") \
    .config("spark.network.crypto.enabled", "true") \
    .config("spark.io.encryption.enabled", "true") \
    .config("spark.ssl.enabled", "true") \
    .getOrCreate()

How does authentication work?

Authentication establishes identity for both users and Spark's internal components.

PYTHON
# Kerberos authentication in Hadoop/YARN environments
# Users obtain tickets from the Key Distribution Center (KDC)
# Spark uses delegation tokens for executor access to secured HDFS

# spark-submit with Kerberos
# spark-submit --principal spark_user@NBI.COM \
#   --keytab /etc/security/keytabs/spark.keytab \
#   --conf spark.authenticate=true \
#   --conf spark.authenticate.secret=my_shared_secret \
#   banking_pipeline.py

# Internal RPC authentication prevents rogue processes from joining
spark.conf.set("spark.authenticate", "true")
spark.conf.set("spark.authenticate.secret", "secure_random_string")
# Without this, any process that knows the port can connect to your app

# Cloud/Kubernetes: IAM roles or service accounts instead of Kerberos
# spark.conf.set("spark.hadoop.fs.s3a.aws.credentials.provider",
#     "com.amazonaws.auth.InstanceProfileCredentialsProvider")

How does authorization work?

Authorization controls what an authenticated principal can access. Spark delegates this to the data and catalog layer.

PYTHON
# Apache Ranger: centralized RBAC with fine-grained policies
# Policy example:
#   Role: risk_analyst
#   Table: transactions
#   Columns: account_id, amount, txn_date (allowed)
#   Columns: ssn, pan_number (denied - PII)
#   Row filter: region = 'NORTH'

# Hive metastore table ACLs
# GRANT SELECT ON TABLE transactions TO ROLE risk_analyst;
# REVOKE SELECT ON TABLE transactions.ssn FROM ROLE risk_analyst;

# Storage-level permissions (HDFS ACLs or S3 bucket policies)
# hdfs dfs -setfacl -m user:spark_reader:r-x /data/transactions

How does encryption work?

Encryption protects data in two states: at rest (stored files) and in transit (network transfers).

PYTHON
# Encryption at rest: handled by the storage layer
# HDFS transparent encryption:
#   hdfs crypto -createZone -keyName bank_key -path /data/transactions
# S3 server-side encryption:
#   spark.conf.set("spark.hadoop.fs.s3a.server-side-encryption-algorithm",
#       "SSE-KMS")
#   spark.conf.set("spark.hadoop.fs.s3a.server-side-encryption.key",
#       "arn:aws:kms:...")

# Encryption in transit:
# RPC and UI: SSL/TLS
spark.conf.set("spark.ssl.enabled", "true")
spark.conf.set("spark.ssl.keyStore", "/etc/security/keystore.jks")
spark.conf.set("spark.ssl.keyStorePassword", "${KEYSTORE_PASS}")

# Shuffle and block transfers: spark.network.crypto
spark.conf.set("spark.network.crypto.enabled", "true")
spark.conf.set("spark.network.crypto.keyLength", "256")

# Local disk spill: spark.io.encryption
spark.conf.set("spark.io.encryption.enabled", "true")
# Protects intermediate data written to executor local disk

How does network isolation work?

Network isolation reduces the attack surface so the other security controls are not even reachable by outsiders.

DIAGRAM
# Production network architecture:
┌──────────────────────────────────────────────┐
│  Private Subnet (VPC)                        │
│  ┌───────────┐ ┌───────────┐ ┌───────────┐  │
│  │  Driver    │ │ Executor 1│ │ Executor 2│  │
│  │  port 4040 │ │           │ │           │  │
│  └───────────┘ └───────────┘ └───────────┘  │
│                                              │
│  Security Group Rules:                       │
│  - Allow: internal traffic between Spark     │
│  - Allow: bastion host for admin access      │
│  - Deny: all public internet access          │
│  - Deny: all other inbound traffic           │
└──────────────────────────────────────────────┘

Why are these layers complementary?

Each layer addresses a different attack vector. Removing any one leaves a gap.

| Missing Layer | Vulnerability |
|---|---|
| No authentication | Anyone can submit jobs and access data |
| No authorization | Any authenticated user can read all tables including PII |
| No encryption at rest | Stolen disks or S3 objects expose raw data |
| No encryption in transit | Network sniffing captures shuffle data and credentials |
| No network isolation | Attackers reach unauthenticated ports from the internet |

PYTHON
# Complete security baseline for a banking Spark cluster
security_config = {
    # Authentication
    "spark.authenticate": "true",
    "spark.authenticate.secret": "${SPARK_SECRET}",
    # Encryption in transit
    "spark.ssl.enabled": "true",
    "spark.network.crypto.enabled": "true",
    # Encryption at rest (local spill)
    "spark.io.encryption.enabled": "true",
    # Event log security
    "spark.eventLog.enabled": "true",
    "spark.history.ui.acls.enable": "true",
}

for key, value in security_config.items():
    spark.conf.set(key, value)

What about audit logging?

In regulated banking, every data access must be traceable. Audit logging records who accessed what and when.

PYTHON
# Apache Ranger audit logging captures:
# - Who: authenticated principal
# - What: table, columns, rows accessed
# - When: timestamp
# - Whether: access was allowed or denied

# Spark event logs capture:
# - Queries executed (SQL text)
# - Job and stage metrics
# - Read and write paths

# At the National Bank of India, audit logs from Ranger
# were retained for 7 years per regulatory requirement

What should a senior engineer know?

  • Apply all four layers as a baseline verified by audit, not configured ad hoc per cluster.
  • Manage secrets in a vault (HashiCorp Vault, AWS Secrets Manager), not in configuration files or environment variables.
  • Treat the Spark UI and History Server as sensitive because they can expose query text, data samples, and environment variables.
  • At the National Bank of India, the security baseline was applied to every cluster via a shared configuration template, and compliance audits verified all four layers were active.
  • Enable user impersonation (doAs) so each user's queries run with their own permissions rather than the service account's permissions.

Visual Overview

DIAGRAM
  Four Layers of Spark Security
  ┌──────────────────────────────────────────────┐
  │  Layer 4: Network Isolation                  │
  │  Private subnets, security groups, firewalls │
  │  ┌──────────────────────────────────────────┐│
  │  │  Layer 3: Encryption                     ││
  │  │  At rest (HDFS/S3) + In transit (TLS)    ││
  │  │  ┌──────────────────────────────────────┐││
  │  │  │  Layer 2: Authorization              │││
  │  │  │  Ranger, table ACLs, storage perms   │││
  │  │  │  ┌──────────────────────────────────┐│││
  │  │  │  │  Layer 1: Authentication         ││││
  │  │  │  │  Kerberos, IAM, spark.authenticate││││
  │  │  │  │  ┌──────────────────────────────┐││││
  │  │  │  │  │  Data and Computation        │││││
  │  │  │  │  └──────────────────────────────┘││││
  │  │  │  └──────────────────────────────────┘│││
  │  │  └──────────────────────────────────────┘││
  │  └──────────────────────────────────────────┘│
  └──────────────────────────────────────────────┘

Key Takeaway

Spark security is defense in depth across four dimensions. Authentication verifies identity with Kerberos in Hadoop environments or IAM in cloud, plus Spark's own internal RPC authentication so rogue processes cannot join. Authorization controls what an authenticated principal may access, enforced through Apache Ranger, Hive table ACLs, and storage permissions. Encryption protects data at rest with HDFS or S3 encryption and in transit with TLS for RPC and spark.network.crypto for shuffle. Network isolation places the cluster in private subnets with security groups restricting access.

These layers are complementary and must be applied together. Authentication without authorization lets any valid user read everything. Encryption in transit without encryption at rest leaves stored data exposed. Strong ACLs are undermined if the network exposes an unauthenticated port. The mature approach is a security baseline that turns on all four layers, is verified by audit, and is applied consistently to every cluster.

At the National Bank of India, the baseline was enforced on every cluster: Kerberos authentication, Ranger authorization with column-level policies blocking PII from analysts, S3 encryption at rest and TLS in transit, and the cluster in a private subnet with no public exposure. Audit logs from Ranger were retained for seven years per regulatory requirement, and secrets were managed in a vault rather than configuration files.

The senior perspective is that security is not optional for banking data platforms, and the four-layer model provides a complete framework that addresses every attack vector from unauthorized access to data interception.

Remember one sentence: secure a production Spark cluster with all four layers together – authentication, authorization, encryption, and network isolation – because any single layer alone leaves an obvious gap.

---

Share
Use ← → arrow keys to navigate