sodcalculator

10 Tech How-To

Least Privilege Access for AI Agents: Setup Guide

Guide 10-21682 · Updated · Step by step

This guide shows you how to configure least privilege access for AI agents in your software development workflow. By granting agents only the permissions they absolutely need—and nothing more—you reduce the risk of accidental or malicious damage, data leaks, and costly mistakes. You’ll learn a practical, step-by-step method to define roles, scope credentials, and audit agent activity, so your AI tools can work effectively without becoming a security liability.

What You’ll Need

  • Access to your CI/CD platform (e.g., GitHub Actions, GitLab CI, Jenkins) or the tool where your AI agents run.
  • Ability to create and manage service accounts or API tokens (admin access to your cloud provider or version control system).
  • A basic understanding of your repository structure and the commands your agents need to execute.
  • A list of the AI agents you use (e.g., code review bots, automated test runners, dependency updaters) and the tasks they perform.

Step-by-Step

  1. Inventory your AI agents and their tasks. List every AI agent in your workflow. For each, note what it does: pull requests, run tests, deploy, update dependencies, or scan code. Be specific—an agent that only reads code should never have write access.
  2. Map each task to the minimum permissions required. For each agent, write down the exact API calls, file paths, or system resources it touches. For example, a code review bot might need read-only access to the repository and the ability to comment on pull requests, but not the ability to merge or push.
  3. Create dedicated service accounts or identities. Avoid using your personal credentials or a shared admin token. Instead, create a separate account or bot identity for each agent (or at least one per distinct permission set). This makes auditing and revocation much easier.
  4. Assign scoped permissions, not blanket roles. Use the principle of least privilege: grant the smallest set of permissions that still allows the agent to function. For cloud services, use IAM roles with specific actions (e.g., s3:GetObject but not s3:DeleteObject). For GitHub, use fine-grained PATs (Personal Access Tokens) with only the required repo scopes, or use GitHub Apps with precise permissions.
  5. Restrict network access and resources. If your agent runs in a container or VM, limit its network access to only the endpoints it needs. Use firewalls or security groups to block everything else. Also, set resource limits (CPU, memory) to prevent runaway processes.
  6. Use environment variables for secrets. Never hardcode credentials in code or configuration files. Store API keys, tokens, and passwords in your CI/CD platform’s secret manager or your cloud provider’s secrets service, and reference them via environment variables.
  7. Implement approval gates for high-risk actions. For actions like merging code, deploying to production, or modifying infrastructure, require human approval. Most CI/CD tools support manual approval steps. Even if the agent proposes a change, a human should review and approve it.
  8. Log and monitor agent activity. Enable detailed logging for all agent actions. Use your cloud provider’s audit logs or your CI/CD platform’s logs to track what each agent does. Set up alerts for unusual behavior, such as an agent accessing files it shouldn’t.
  9. Regularly review and rotate credentials. Schedule periodic audits (e.g., quarterly) to review each agent’s permissions. Remove any that are no longer needed. Rotate API keys and tokens on a regular basis, and immediately revoke any that are compromised.
  10. Document your access policy. Write down the permissions for each agent, the rationale, and the review cadence. This helps onboard new team members and ensures consistency.

Common Mistakes

  • Using a single over-privileged token. One token that can do everything is a single point of failure. If it leaks, an attacker gets full access.
  • Granting write access when read-only is enough. An agent that only reads code doesn’t need to push changes. Over-privileged agents can accidentally corrupt the repository or introduce vulnerabilities.
  • Ignoring the principle of least privilege for dependencies. A dependency updater that can also delete packages or modify the build system is dangerous. Scope it to only update dependency files.
  • Hardcoding secrets in scripts. This is a classic mistake. Secrets in code can be exposed in logs, version control history, or to other agents.
  • Not revoking access when an agent is decommissioned. Old agents with active tokens become forgotten backdoors.
  • Forgetting about human users. Least privilege applies to humans too, but this guide focuses on AI agents. Still, ensure your own account isn’t overly privileged.

Tips

  • Start small and expand as needed. Begin with the most restrictive permissions and add more only when you find the agent can’t do its job.
  • Use infrastructure as code (IaC). Define IAM roles and permissions in Terraform or CloudFormation. This makes them version-controlled and reviewable.
  • Leverage conditional access. In cloud platforms, you can restrict access based on IP, time, or resource tags. For example, allow an agent to access a staging bucket only from the CI/CD IP range.
  • Separate environments. Use different credentials for development, staging, and production. An agent that works in dev should not have access to prod.
  • Use short-lived credentials. Where possible, use tokens that expire quickly (e.g., AWS STS, GitHub App installation tokens). This reduces the impact of a leak.
  • Test your permissions. After setting up an agent, run a few test tasks to ensure it works, then try to do something it shouldn’t—like deleting a file—to confirm it’s blocked.

Troubleshooting

Agent fails with permission denied. Check the logs to see exactly which API call failed. Then, review the permissions you granted and add the missing one. Sometimes the agent needs an additional scope you didn’t anticipate.

Agent works but is too slow. This is usually not a permissions issue but a resource limit. Increase CPU/memory limits or optimize the agent’s code. If you suspect network restrictions are causing timeouts, verify that the required endpoints are reachable.

Agent can’t access a secret. Ensure the secret is correctly defined in your secret manager and that the agent’s environment has access to it. Check for typos in the environment variable name.

Audit logs show unexpected activity. Immediately revoke the agent’s credentials and investigate. It could be a compromised token or a misconfigured agent. Review the logs to understand what happened.

Agent is blocked by an approval gate. If you set up manual approval, make sure the right people are notified. If the gate is too restrictive, adjust it, but only after reviewing the risk.

Can’t create a service account due to admin restrictions. If you don’t have admin rights, ask your infrastructure team to create the account and grant you the ability to manage its permissions. Alternatively, use a more restricted token type that you can create yourself.

Frequently Asked Questions

Q: Can I use the same token for multiple agents? A: It’s not recommended. If one agent is compromised, the token is compromised for all. Use separate tokens per agent to isolate risk.

Q: How often should I rotate credentials? A: A good practice is every 90 days, or immediately if you suspect a leak. Some platforms support automatic rotation.

Q: What’s the difference between a service account and a bot user? A: They’re similar. A service account is a non-human identity with permissions. A bot user is often a regular user account used by a bot. Service accounts are usually cleaner for automation.

Q: My agent needs to create temporary files. Is that a security risk? A: It’s fine as long as the agent has write access only to a temporary directory, not to the entire file system. Use a dedicated temp folder and clean it up.

Q: Can I use OAuth2 for agent authentication? A: Yes, but it’s often more complex. Service accounts or API tokens are simpler for server-to-server communication. OAuth2 is better for user-facing apps.

Q: What if my AI agent uses a third-party API that requires a key? A: Store that key in your secret manager and pass it to the agent via environment variables. Do not embed it in the agent’s code.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →