Managed Alibaba Cloud STS injection

Updated at:

The platform obtains and automatically refreshes STS (Security Token Service) credentials through a Workload Identity.

In the sandbox, call Alibaba Cloud APIs with placeholder credentials. Managing STS credentials or putting real credentials into the sandbox is not required.

To provide your own credentials or use multiple accounts, see Inject your own Alibaba Cloud STS credentials.

Prerequisites

Complete the following preparations in the Agent Identity console:

  1. Create a Workload Identity.

  2. Associate it with a Resource Access Management (RAM) role used to access Alibaba Cloud resources.

  3. Grant the role only the permissions that your workloads require.

If multiple sandboxes access the same Object Storage Service (OSS) bucket, they can share a single RAM role. When you create each sandbox, use a Session Policy (fc.sandbox.identity.role.policy) to narrow the permissions to the dedicated path of each sandbox. A separate Workload Identity is not required for each sandbox.

Grant least privilege

Important

The temporary nature and automatic refresh of STS credentials do not narrow permissions automatically. Real credentials never enter the sandbox, but code in the sandbox can still access the allowed services through the egress gateway with the effective permissions of the associated RAM role. Grant permissions based on task requirements to prevent untrusted code from abusing permissions and causing data exposure, tampering, or deletion.

  • Configure a dedicated RAM role for the sandbox and grant only the actions that the task requires. If the service supports resource-level authorization, limit the permissions to specific resources, such as the task-specific path in an OSS bucket. Do not attach policies that exceed task requirements, such as AdministratorAccess, only to simplify access.

  • When you create a sandbox, use fc.sandbox.identity.role.policy to further limit the actions and resources of the current task. If the task only reads data, do not grant write or delete permissions. If you do not configure a Session Policy, STS permissions are not narrowed automatically, so the permissions of the role itself must still be minimal. For the permission narrowing rules of a Session Policy, see the Policy parameter of AssumeRole.

  • Use fc.sandbox.identity.role.domains to limit re-signing to the service domains actually in use. Domain restrictions do not replace resource-level permission control. Other resources under the same service domain must still be isolated by permission policies.

  • Do not grant the sandbox direct access to sensitive resources such as shared databases, and do not reuse high-privilege RAM roles from production systems. If business data is required, use a trusted business service to expose only the data or actions that the current task needs.

Configure the sandbox

When you create a sandbox, specify only a Workload Identity name in metadata and use the default values for the rest:

Node.js

import { Sandbox } from "e2b";

const sandbox = await Sandbox.create("code-interpreter-v1", {
  metadata: {
    "fc.sandbox.identity.name": "my-identity",
  },
});

Python

from e2b import Sandbox

sandbox = Sandbox.create(
    template="code-interpreter-v1",
    metadata={"fc.sandbox.identity.name": "my-identity"},
)

By default, the egress gateway re-signs recognizable Alibaba Cloud signed requests sent to *.aliyuncs.com.

Verify credential injection

An Alibaba Cloud request must first be signed by the SDK, so the verification uses a standard SDK and a regular endpoint. Pass placeholder credentials during initialization so that the SDK generates a complete signed request, and the egress gateway re-signs it with the real signature.

The following example uses the Function Compute (FC) SDK in the sandbox to call ListFunctions. The RAM role associated with the Workload Identity must have the fc:ListFunctions permission. The SDK dependencies can be pre-installed in the template.

Node.js

import assert from "node:assert/strict";
import FC from "@alicloud/fc20230330";
import OpenApi from "@alicloud/openapi-core";
import Darabonba from "@darabonba/typescript";

const config = new OpenApi.$OpenApiUtil.Config({
  accessKeyId: "fake-access-key-id",
  accessKeySecret: "fake-access-key-secret",
  securityToken: "fake-sts-token",
  endpoint: "<account-id>.cn-hangzhou-internal.fc.aliyuncs.com",
});
const client = new FC.default(config);
const result = await client.listFunctionsWithOptions(
  new FC.ListFunctionsRequest({ limit: 1 }),
  {},
  new Darabonba.RuntimeOptions({
    connectTimeout: 30_000,
    readTimeout: 30_000,
  }),
);
assert.equal(result.statusCode, 200);

Python

from alibabacloud_fc20230330.client import Client as FCClient
from alibabacloud_fc20230330 import models as fc_models
from alibabacloud_tea_openapi.models import Config
from alibabacloud_tea_util.models import RuntimeOptions

config = Config(
    access_key_id="fake-access-key-id",
    access_key_secret="fake-access-key-secret",
    security_token="fake-sts-token",
    endpoint="<account-id>.cn-hangzhou-internal.fc.aliyuncs.com",
)
client = FCClient(config)
response = client.list_functions_with_options(
    fc_models.ListFunctionsRequest(limit=1),
    {},
    RuntimeOptions(connect_timeout=30_000, read_timeout=30_000),
)
assert response.status_code == 200

The endpoint in the preceding example is within the default re-signing scope *.aliyuncs.com, so configuring additional domains is not required. Replace <account-id> and the region with your actual values.

Optional fields

To control the scope more finely, add the following fields. The values of metadata must be strings, so objects and arrays must be serialized into JSON strings.

Node.js

import { Sandbox } from "e2b";

const rolePolicy = {
  Version: "1",
  Statement: [{
    Effect: "Allow",
    Action: ["oss:GetObject", "oss:PutObject"],
    Resource: "acs:oss:*:*:agent-data/sandboxes/task-123/*",
  }],
};

const sandbox = await Sandbox.create("code-interpreter-v1", {
  metadata: {
    "fc.sandbox.identity.name": "my-identity",
    "fc.sandbox.identity.role.policy": JSON.stringify(rolePolicy),
    "fc.sandbox.identity.role.domains": JSON.stringify([
      "agent-data.oss-cn-hangzhou.aliyuncs.com",
    ]),
  },
});

Python

import json

from e2b import Sandbox

role_policy = {
    "Version": "1",
    "Statement": [{
        "Effect": "Allow",
        "Action": ["oss:GetObject", "oss:PutObject"],
        "Resource": "acs:oss:*:*:agent-data/sandboxes/task-123/*",
    }],
}

sandbox = Sandbox.create(
    template="code-interpreter-v1",
    metadata={
        "fc.sandbox.identity.name": "my-identity",
        "fc.sandbox.identity.role.policy": json.dumps(role_policy),
        "fc.sandbox.identity.role.domains": json.dumps([
            "agent-data.oss-cn-hangzhou.aliyuncs.com"
        ]),
        },
)

Field

Description

fc.sandbox.identity.role.policy

Session Policy. This policy can only further narrow permissions, not expand them. Effective permissions = RAM role permissions ∩ Session Policy.

fc.sandbox.identity.role.domains

The list of domains that can be re-signed with this identity.

Rules for re-sign domain values:

  • Not configured: The default value is *.aliyuncs.com.

  • Configured as a non-empty array: Only the domains in the list are recognized, and the default scope is no longer included.

  • Configured as an empty array []: Re-signing is disabled.

  • Exact domains and *.suffix wildcards are supported (*.example.com does not match example.com itself).

  • An exact domain prefixed with ! is an exclusion (blacklist). Matched domains are never re-signed. This rule takes precedence over other matches, so you can exclude individual domains from a wildcard scope (for example, configure both *.aliyuncs.com and !oss-cn-hangzhou.aliyuncs.com). The ! prefix supports only exact domains, not !*.suffix.

  • Specify only the domain name, without a protocol, port, or path.

  • The list cannot be changed after the sandbox is created.

These fields are configured only when the sandbox is created. Query operations do not return them afterwards, and they cannot be modified by updateNetwork. Save the configuration used to create the sandbox on the backend.

Update the configuration

allowOut and denyOut are examples of common network settings that can be updated while the sandbox is running. For more information, see Update credential configuration. The Workload Identity, Session Policy, and re-sign domains cannot be modified by updateNetwork. To change them, create a new sandbox.

Limits

For the common limits, security requirements, and supported re-signing protocols, see Credential usage limits.