A CLI tool to restrict AWS IAM users and roles to specific Kubernetes namespaces by managing entries in the aws-auth ConfigMap and creating corresponding RBAC Roles and RoleBindings.
Before using this tool, please ensure you have:
- AWS Credentials:
- Configured AWS credentials (e.g., via
~/.aws/credentials, environment variables, or IAM roles for EC2/ECS). - The credentials must have IAM permissions to:
iam:ListUsersiam:ListRoles- (Note:
iam:ListGroupsmight be required if group functionality is expanded in the future).
- Configured AWS credentials (e.g., via
- Kubernetes Configuration:
- A valid
kubeconfigfile pointing to your Kubernetes cluster (typically an EKS cluster if usingaws-auth). - Permissions to:
- Read and write to the
aws-authConfigMap in thekube-systemnamespace. - Create, read, and delete namespaced
RoleandRoleBindingresources in the target namespaces.
- Read and write to the
- A valid
- Python Environment:
- Python 3.x
pipfor installation.
# For developers (from the repository root)
pip install --editable .
# ToDo: Add installation instructions once published to PyPI--version: Show the version of the tool.--help: Show help messages for commands.
k8s-iam list-entitiesThis command lists available IAM users and roles from your AWS account that can be targeted for namespace isolation.
k8s-iam createThis command will guide you through:
- Selecting the Kubernetes context.
- Choosing an AWS IAM User or Role.
- Specifying the Kubernetes namespace to grant access to.
- Selecting a predefined access role (e.g.,
viewer,editor,admin).
It then:
- Adds (or updates) a mapping for the IAM entity in the
aws-authConfigMap inkube-system. - Creates a namespaced
Rolewith the chosen permissions in the target namespace. - Creates a namespaced
RoleBindinglinking the IAM entity (as a Kubernetes user) to this Role.
k8s-iam deleteThis command will guide you through:
- Selecting the Kubernetes context.
- Choosing an AWS IAM User or Role whose access you want to remove.
- Specifying the Kubernetes namespace from which to revoke access.
- Selecting the predefined access role that was originally assigned (this is needed to identify the correct Role/RoleBinding).
It then:
- Removes the IAM entity's mapping from the
aws-authConfigMap. - Deletes the namespaced
RoleBindingassociated with the entity and role in that namespace. - Deletes the namespaced
Roleassociated with the entity and role in that namespace.
For both create and delete commands, you can use the --dry-run flag:
k8s-iam create --dry-run
k8s-iam delete --dry-runThis mode simulates the intended operations and prints out what actions would be taken without actually modifying your AWS or Kubernetes resources. It is highly recommended to use --dry-run before applying any changes, especially for the first time.
When using k8s-iam create, you'll be prompted to select a predefined role. These roles define the level of access the IAM entity will have within the specified Kubernetes namespace:
- viewer: Can view most resources in the namespace (pods, deployments, services, etc.). Cannot make changes.
- editor: Can view and modify most resources (create, update, delete pods, deployments, services, configmaps, etc.).
- admin: Extensive permissions within the namespace, including managing most resources and also managing RBAC (
Roles,RoleBindings) within that namespace. (Use with caution) - monitoring: View access to cluster components (nodes, namespaces) and application workloads, suitable for monitoring systems.
- deployment-manager: Permissions to manage deployments, ReplicaSets, and associated ConfigMaps/Services.
- job-manager: Permissions to manage Jobs and CronJobs.
- pod-executor: Allows creating pods and executing commands within them (
pods/exec). - pod-log-viewer: Allows viewing logs for pods.
- config-manager: Permissions to manage ConfigMaps and Secrets.
- network-admin: Permissions to manage NetworkPolicies and Ingresses.
- storage-admin: Permissions to manage PersistentVolumeClaims and PersistentVolumes.
- ci-cd-pipeline: Broad permissions suitable for CI/CD pipelines to deploy and manage applications.
- istio-manager: Permissions to manage Istio custom resources like VirtualServices and DestinationRules.
For the exact permissions, refer to the k8s_iam_isolation/predefined_rbac_rules.yaml file in the repository or your custom configuration (see below).
This tool uses a configuration file located at ~/.config/k8s_iam_isolation/config.yaml.
You can customize logging behavior by setting the following options in the config file:
log_level: The logging verbosity (e.g.,INFO,DEBUG,WARNING,ERROR). Defaults toINFO.log_file: Path to a file where logs should be written (e.g.,/var/log/k8s_iam_isolation/audit.log). If not specified, or if the directory cannot be created, logs will go to the console (stderr). The tool will attempt to create the log directory if it doesn't exist.
Example config.yaml:
log_level: DEBUG
log_file: /tmp/k8s-iam-isolation.logYou can override the default predefined RBAC rules. To do this:
- Copy the structure from the default
k8s_iam_isolation/predefined_rbac_rules.yamlfile (found in the installed package or repository). - Paste this structure into your
~/.config/k8s_iam_isolation/config.yamlunder a top-level key namedpredefined_rules:. - Modify the rules as needed.
The tool will then use your custom rules instead of the defaults.
Example config.yaml with custom rules:
log_level: INFO
predefined_rules:
viewer: # Overriding the default viewer
- apiGroups: [""]
resources: ["pods", "services"] # Only pods and services
verbs: ["get", "list", "watch"]
# ... other custom roles or modifications ...- This tool modifies critical Kubernetes resources, including the
aws-authConfigMap (which controls cluster-wide IAM authentication for EKS) and RBAC Roles/RoleBindings. - Ensure you are authorized to perform these operations on your cluster.
- Always understand the implications of the access levels you are granting. Use the principle of least privilege.
- The
--dry-runflag is your friend!
- Logs are generated based on your
config.yamlsettings (see Configuration section). Check the specifiedlog_fileor your console output for detailed information and errors. - Ensure your AWS credentials and
kubeconfigare correctly set up and have the necessary permissions.