You need to enable JavaScript to run this app.
Torch Log Service

Torch Log Service

Copy page
Download PDF
permission management
Access TLS via STS
Copy page
Download PDF
Access TLS via STS
You can use the Security Token Service (STS) to request temporary security tokens, allowing specified IAM users to access TLS resources within their assigned permissions for a specified period. This topic describes how to use temporary credentials issued by STS to access TLS.​
Background information​
STS is a temporary access permission management service provided by Volcano Engine, primarily used to grant third-party temporary access to cloud services. For example, when uploading log data directly to TLS using the SDK, for security reasons, it is not recommended to store the Volcano Engine account or IAM user access keys in client code. At this point, you can use STS on your application's server to obtain a temporary security token and issue it to the client. This enables you to create customizable, time-bound, and permission-scoped temporary credentials for third-party self-built applications or IAM users.​
Compared to using Volcano Engine access keys, temporary security tokens offer the following advantages:​
  • Using temporary security tokens reduces the risk of Volcano Engine access keys being leaked.​
  • Temporary security tokens allow you to set custom validity periods and expire automatically when the period ends.​
  • You can bind temporary security tokens to custom permission policies for more granular management, restricting the scope of permissions for token holders.​
​
Steps​
1 Create an IAM role and grant TLS permissions​
An IAM role is a virtual user and does not have permanent credentials. It can only obtain customizable, temporary security tokens through STS, and must be assumed by a trusted entity. In this example, you need to create an IAM role and authorize it, specifying the TLS resource permissions the IAM role will have when assumed by the IAM user.​
  1. Log in to the Access Control IAM Console with the primary Volcano Engine account.​
  1. In the left navigation bar, click Role Management.​
  1. On the Role management page, click Create New Role.​
  1. In the Create New Role panel, set Select trust identity type to Account, set Select identity to The current account, and then click Next.​
Tip​
When creating an IAM role, you can only set your own account as the authorized account. In 3 Grant IAM users permission to assume IAM roles., change the authorized account to the specified IAM user, so the IAM user can later assume this role.​
  1. Set the role name and then click Next.​
  1. Select the TLSReadOnlyAccess policy, and then click Submit.​
  • This example demonstrates how to grant the system default permission TLSReadOnlyAccess to an IAM role. After authorization, this IAM role has read-only access to all TLS resources and pages. You can also create custom permission policies based on your business requirements. For detailed steps, see Create custom policy. For information about available operations and resource authorization, see Authorized actions and Authorizable resources.​
Notice​
To follow the principle of least privilege, set IAM role permissions carefully. For passwordless login and embedded console scenarios, we recommend granting only read permissions for specific projects or topics, without any change permissions.​
  1. Save the TRN for the IAM role.​
  1. On the Role management page, click the name of the IAM role you created.​
  1. On the Role details page, obtain the role's Role TRN.​
  • When calling the AssumeRole API, enter the TRN.​
2. Create an IAM user​
An IAM user is an entity identity with a unique ID and credentials, an independent login password, and access keys. You need to create an IAM user to assume IAM roles and facilitate obtaining temporary security tokens. This guide uses Create by username as an example. For other methods, see Create user.​
Notice​
We recommend using IAM users to access and authenticate TLS in routine operations and data resource management, to reduce the risk of Volcano Engine account key leaks.​
  1. In the left navigation bar, select User management > users.​
  1. On the Users page, click New user.​
  1. On the Create user page, click Create by username.​
  1. Set the username, enable programmatic access in access method, and then click Next.​
  1. In the Permission settings step, click Next.​
  1. In the Review step, confirm the username information, click Next, and follow the on-screen instructions to complete creation.​
3 Grant IAM users permission to assume IAM roles.​
Once authorized, IAM users can assume IAM roles to obtain temporary security tokens and access TLS resources within the IAM role's permission scope.​
The authorization process consists of the following two required settings.​
  • Set the trust policy for the IAM role.​
  • When you create an IAM role, the default trusted entity is the Volcano Engine account. You need to modify the IAM role's trust policy and change the account configured under Principal to the IAM user you created in 2. Create an IAM user, allowing the specified IAM user to assume this IAM role.​
  • Grant the IAM user the STSAssumeRoleAccess permission policy.​
  • You need to grant the STSAssumeRoleAccess permission policy to IAM users before you can use their access keys to call the AssumeRole API and request temporary security tokens.​
The steps for authorization are as follows:​
  1. Edit the trust policy of the IAM role.​
  1. In the left navigation bar, click Role Management.​
  1. On the Role Management page, find and click the role you created in 1 Create an IAM role and grant TLS permissions.​
  1. On the Role detail page, click Trust relationship.​
  1. On the Trust relationship tab, click Edit trust policy.​
  1. Edit the IAM field in the trust policy, and then click Save.​
  • Replace the default root value in trn with user/${IAMUserName}, for example, user/TLSUser. For example, the following trust policy allows this IAM role to be assumed by the IAM user TLSUser under the Volcano Engine account 21000****.​
  • ​
    {​
    "Statement": [​
    {​
    "Effect": "Allow",​
    "Action": [​
    "sts:AssumeRole"​
    ],​
    "Principal": {​
    "IAM": [​
    "trn:iam::21000****:user/TLSUser"​
    ]​
    }​
    }​
    ]​
    }​
    ​
  • ​​​
  1. Grant the IAM user STSAssumeRoleAccess permission.​
  1. In the left navigation bar, select User management > users.​
  1. On the User page, find and click the user you created in 2. Create an IAM user.​
  1. On the User details page, select Permissions > Attach permissions directly, and then click Add permission.​
  1. In the Add permission panel, select the STSAssumeRoleAccess policy, and click Submit.​
4 Call AssumeRole to obtain a temporary security token​
The IAM user calls the AssumeRole API of STS by assuming the IAM role to obtain a temporary security token for the IAM role. The temporary security token includes a temporary access key (AccessKeyId and SecretAccessKey) and a temporary token (SessionToken).​
The following is a sample request, where the RoleTrn parameter should be specified as the TRN of the IAM role you created in 1 Create an IAM role and grant TLS permissions​
​
GET /?RoleTrn=trn:iam::200000****:role/TLSRole&RoleSessionName=ceshiassume&Action=AssumeRole&Version=2018-01-01 HTTP/1.1​
Accept: application/json​
Content-Type: application/x-www-form-urlencoded​
Host: iam.volcengineapi.com​
X-Date: 20180614T131357Z​
Authorization: HMAC-SHA256 Credential=AKLT4MQ/20180614/cn-north-1/sts/request, SignedHeaders=accept;content-type;host;x-date, Signature=83671ac2**8a5581f​
​
The following is a sample response of the AssumeRole API:​
​
{​
"ResponseMetadata": {​
"RequestId": "202104124922",​
"Action": "AssumeRole",​
"Version": "2018-01-01",​
"Service": "sts"​
},​
"Result": {​
"Credentials": {​
"CurrentTime": "2021-04-12T10:57:09+08:00",​
"ExpiredTime": "2021-04-12T11:57:09+08:00",​
"AccessKeyId": "AKTPODg0MzV**2ZDc***",​
"SecretAccessKey": "TVRNNVlqRmxPR1mRoTkdWbE1E****",​
"SessionToken": "STSeyJBY2NvdW50SWklkZW50aXR5VHlwZSI6NCwiSWRlbnRpdHlJZCI6MzAyMTcsIkNoYW5uZWwiOiJyZXNvdXJjZSIsIkFjY2Vzc0tleUlkIjoiQUtUUE9EZzBNelZrWlRNMVlqQXlOR05sWkdGbU0yVTNaVEl6T0RnMlpEY3hNRGciLCJTaWduZWRTZWNyZXRBY2Nlc3NLZXkiOiJHSHRuc2Q1b3BOOThUeUNrckVuMVAxTDd1ZzBXczZFMytzTktDMDJQT01lNzFMOUloNzhYaDB5VXE4Ni9JMW8reEZxTmtkK01tVUR0cjRHR0NodXJQdz09IiwiRXhwaXJlZFRpbWUiOjE2MTgxOTk4MjksIlBvbGljeVN0cmluZyI6IiIsIlNpZ25hdHVyZSI6IjdiZTEzNTU1MDM5NzcyM2VkMDQxYmVkNTIyNzU4YmMyNjQ3YzRmNzZiNmRjYTViNzVhZDUyOTMwYTg1NjE5ZGEiLCJTZXNzaW9uTmFtZSI6ImNlc2hpYXNzdW1****"​
},​
"AssumedRoleUser": {​
"Trn": "trn:sts::20**94:assumed-role/TLSRole/ceshiassume",​
"AssumedRoleId": "**:ceshiassume"​
}​
}​
}​
​
5 Use temporary security tokens to access TLS​
After obtaining a temporary security token, you can use it for SDK authentication, and then access TLS through the SDK. For the TLS SDK and related sample code, see SDK list. For example, you can refer to the following Python SDK sample to create a project, create a log topic, configure indexes, write logs, consume logs, and query logs.​
Tip​
  • The default validity period of temporary security tokens is one hour. You need to update the token promptly after it expires.​
  • You can use the DurationSeconds parameter in AssumeRole to set the validity period of the temporary security token. The unit for the DurationSeconds parameter is seconds, and it can be set for up to 12 hours.​
​
# coding=utf-8 ​
from __future__ import absolute_import ​
from __future__ import division ​
from __future__ import print_function ​
​
import time ​
​
from volcengine.tls.TLSService import TLSService ​
from volcengine.tls.tls_requests import * ​
​
​
if __name__ == "__main__": ​
# When you initialize the client, we recommend that you use environment variables to dynamically get identity authentication information such as Volcano Engine AccessKey. Otherwise, hard-coded AccessKey information leads to data security risks. For details, see https://www.volcengine.com/docs/6470/1166455.​
# When you use STS,VOLCENGINE_ACCESS_KEY_ID and VOLCENGINE_ACCESS_KEY_SECRET both require temporary access keys and VOLCENGINE_TOKEN.​
endpoint = os.environ["VOLCENGINE_ENDPOINT"] ​
region = os.environ["VOLCENGINE_REGION"] ​
access_key_id = os.environ["VOLCENGINE_ACCESS_KEY_ID"] ​
access_key_secret = os.environ["VOLCENGINE_ACCESS_KEY_SECRET"] ​
​
# Initialize TLS client​
tls_service = TLSService(endpoint, access_key_id, access_key_secret, region) ​
​
# Create log project​
create_project_request = CreateProjectRequest(project_name="project-name", region=region, ​
description="project-description") ​
create_project_response = tls_service.create_project(create_project_request) ​
project_id = create_project_response.project_id ​
​
# Create log topic​
create_topic_request = CreateTopicRequest(topic_name="topic-name", project_id=project_id, ​
ttl=3650, description="topic-description", shard_count=2) ​
create_topic_response = tls_service.create_topic(create_topic_request) ​
topic_id = create_topic_response.topic_id ​
​
# Configure index​
full_text = FullTextInfo(case_sensitive=False, delimiter=",-;", include_chinese=False) ​
value_info_a = ValueInfo(value_type="text", delimiter="", case_sensitive=True, ​
include_chinese=False, sql_flag=False) ​
value_info_b = ValueInfo(value_type="long", delimiter="", case_sensitive=False, ​
include_chinese=False, sql_flag=True) ​
key_value_info_a = KeyValueInfo(key="key1", value=value_info_a) ​
key_value_info_b = KeyValueInfo(key="key2", value=value_info_b) ​
key_value = [key_value_info_a, key_value_info_b] ​
create_index_request = CreateIndexRequest(topic_id, full_text, key_value) ​
create_index_response = tls_service.create_index(create_index_request) ​
​
# Write logs.​
logs = PutLogsV2Logs(source="192.168.1.1", filename="sys.log") ​
for i in range(100): ​
logs.add_log(contents={"key1": "value1-" + str(i + 1), "key2": "value2-" + str(i + 1)}, ​
log_time=int(round(time.time()))) ​
tls_service.put_logs_v2(PutLogsV2Request(topic_id, logs)) ​
time.sleep(30) ​
​
# Query the consumption cursor.​
describe_cursor_request = DescribeCursorRequest(topic_id, shard_id=0, from_time="begin") ​
describe_cursor_response = tls_service.describe_cursor(describe_cursor_request) ​
​
# Consume logs. ​
consume_logs_request = ConsumeLogsRequest(topic_id, shard_id=0, cursor=describe_cursor_response.cursor) ​
consume_logs_response = tls_service.consume_logs(consume_logs_request) ​
​
# If you query and analyze logs, we recommend using search_logs_v2 method provided by Python SDK. For call example, see the follwing code. ​
# Query logs (full-text query). ​
search_logs_request = SearchLogsRequest(topic_id, query="error", limit=10, ​
start_time=1346457600000, end_time=1630454400000) ​
search_logs_response = tls_service.search_logs_v2(search_logs_request) ​
​
# Query logs (key-value query). ​
search_logs_request = SearchLogsRequest(topic_id, query="key1:error", limit=10, ​
start_time=1346457600000, end_time=1630454400000) ​
search_logs_response = tls_service.search_logs_v2(search_logs_request) ​
​
# Query and analyze logs.​
search_logs_request = SearchLogsRequest(topic_id, query="* | select key1, key2", limit=10, ​
start_time=1346457600000, end_time=1630454400000) ​
search_logs_response = tls_service.search_logs_v2(search_logs_request) ​
​
# Query and analuze logs.​
search_logs_request = SearchLogsRequest(topic_id, query="* | select key1, key2", limit=10, ​
start_time=1346457600000, end_time=1630454400000) ​
search_logs_response = tls_service.search_logs(search_logs_request)​
​
​
Last updated: 2026.09.01 17:38:32