Skip to main content

Security Rules

The interfaces of Security Rules can be called via commonService .

Permission Categories (The first 4 types below are simple permissions, while CUSTOM is specific to security rules)

  • READONLY: Readable by all users, writable only by the creator and administrators
  • PRIVATE: Readable and writable only by the creator and administrators
  • ADMINWRITE: Readable by all users, writable only by administrators
  • ADMINONLY: Readable and writable only by administrators
  • CUSTOM: Custom security rules

Get database simple permissions

1. Interface Description

Interface function: Get database simple permissions

Interface declaration: manager.commonService().call({Action: 'DescribeDatabaseACL',Param: {}}): Promise<Object>

⚠️ Starting from version 3.0.0, commonService is used as a method with request parameters (service?: string, version?: string), which constitutes a breaking change.

2. Input Parameters

FieldRequiredTypeDescription
ActionYesStringInterface name
ParamYesObjectInterface parameters

Param Field Description

FieldRequiredTypeDescription
CollectionNameYesStringCollection name
EnvIdYesStringEnvironment ID

3. Response

FieldRequiredTypeDescription
RequestIdYesStringRequest unique identifier
AclTagNoStringPermission category

4. Sample Code

const cloudbaseConfig = {
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId", // CloudBase environment ID, obtain from the Tencent CloudBase Console
};

let manager = new CloudBase(cloudbaseConfig);

async function test() {
const res = await manager.commonService().call({
Action: "DescribeDatabaseACL",
Param: {
CollectionName: "xxx",
EnvId: cloudbaseConfig.envId,
},
});

console.log(res.AclTag); // Print permission category
}

test();

Update database simple permissions

1. Interface Description

Interface function: Update database simple permissions

Interface declaration: manager.commonService().call({Action: 'ModifyDatabaseACL',Param: {}}): Promise<Object>

⚠️ Starting from version 3.0.0, commonService is used as a method with request parameters (service?: string, version?: string), which constitutes a breaking change.

2. Input Parameters

FieldRequiredTypeDescription
ActionYesStringInterface name
ParamYesObjectInterface parameters

Param Field Description

FieldRequiredTypeDescription
CollectionNameYesStringCollection name
AclTagYesStringSimple database permissions: READONLY, PRIVATE, ADMINWRITE, ADMINONLY
EnvIdYesStringEnvironment ID

3. Response

FieldRequiredTypeDescription
RequestIdStringYesRequest unique identifier

4. Sample Code

const cloudbaseConfig = {
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId", // CloudBase environment ID, obtain from the Tencent CloudBase Console
};

let manager = new CloudBase(cloudBaseConfig);

async function test() {
const res = await manager.commonService().call({
Action: "ModifyDatabaseACL",
Param: {
CollectionName: "xxx",
EnvId: cloudbaseConfig.envId,
AclTag: "PRIVATE",
},
});
console.log(res);
}

test();

Set up Database Security Rules

1. Interface Description

Function: Set database security rules

Interface declaration: manager.commonService().call({Action: 'ModifySafeRule',Param: {}}): Promise<Object>

⚠️ Starting from version 3.0.0, commonService is used as a method with request parameters (service?: string, version?: string), which constitutes a breaking change.

2. Input Parameters

FieldRequiredTypeDescription
ActionYesStringInterface name
ParamYesObjectInterface parameters

Param Field Description

FieldRequiredTypeDescription
CollectionNameYesStringCollection name
EnvIdYesStringEnvironment ID
AclTagYesStringPermission category
RuleNoStringRequired when the permission category is set to CUSTOM (Database Security Rules Document)

3. Response

FieldRequiredTypeDescription
RequestIdYesStringRequest unique identifier

4. Sample Code

const cloudbaseConfig = {
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId", // CloudBase environment ID, obtain from the Tencent CloudBase Console
};

let manager = new CloudBase(cloudBaseConfig);

async function test() {
const res = await manager.commonService().call({
Action: "ModifySafeRule",
Param: {
CollectionName: "coll-1",
AclTag: "CUSTOM",
EnvId: "xxx",
Rule: JSON.stringify({
read: true,
write: "doc._openid == auth.openid",
}),
},
});
console.log(res);
}

test();

Query Database Security Rules

1. Interface Description

Function: Query database security rules

Interface declaration: manager.commonService().call({Action: 'DescribeSafeRule',Param: {}}): Promise<Object>

⚠️ Starting from version 3.0.0, commonService is used as a method with request parameters (service?: string, version?: string), which constitutes a breaking change.

2. Input Parameters

FieldRequiredTypeDescription
ActionYesStringInterface name
ParamYesObjectInterface parameters

Param Field Description

FieldRequiredTypeDescription
CollectionNameYesStringCollection name
EnvIdYesStringEnvironment ID

3. Response

FieldRequiredTypeDescription
RequestIdYesStringRequest unique identifier
AclTagYesStringPermission category
RuleYesString or nullSecurity rule

4. Sample Code

const cloudbaseConfig = {
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId", // CloudBase environment ID, obtain from the Tencent CloudBase Console
};

let manager = new CloudBase(cloudBaseConfig);

async function test() {
const res = await manager.commonService().call({
Action: "DescribeSafeRule",
Param: {
CollectionName: "coll-1",
EnvId: cloudbaseConfig.envId,
},
});
console.log(res.AclTag);
console.log(res.Rule);
}
test();

Set up Cloud Function Security Rules

1. Interface Description

Function: Set up Cloud Function Security Rules

Interface declaration: manager.commonService().call({Action: 'ModifySecurityRule',Param: {}}): Promise<Object>

⚠️ Starting from version 3.0.0, commonService is used as a method with request parameters (service?: string, version?: string), which constitutes a breaking change.

2. Input Parameters

FieldRequiredTypeDescription
ActionYesStringInterface name
ParamYesObjectInterface parameters

Param Field Description

FieldRequiredTypeDescription
AclTagYesStringEnter CUSTOM here
EnvIdYesStringEnvironment ID
ResourceTypeYesStringEnter FUNCTION here
RuleYesStringRefer to the Cloud Function Security Rules Documentation

3. Response

FieldRequiredTypeDescription
RequestIdYesStringRequest unique identifier

4. Sample Code

const manager = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId", // CloudBase environment ID, obtain from the Tencent CloudBase Console
});

const { env } = manager;

async function test() {
const res = await manager.commonService().call({
Action: "ModifySecurityRule",
Param: {
AclTag: "CUSTOM",
EnvId: envId,
ResourceType: "FUNCTION",
Rule: JSON.stringify({
"*": {
invoke: true,
},
}),
},
});
console.log(res);
}
test();

Query Cloud Function Security Rules

1. Interface Description

Function: Query Cloud Function Security Rules

Interface declaration: manager.commonService().call({Action: 'DescribeSecurityRule',Param: {}}): Promise<Object>

⚠️ Starting from version 3.0.0, commonService is used as a method with request parameters (service?: string, version?: string), which constitutes a breaking change.

2. Input Parameters

FieldRequiredTypeDescription
ActionYesStringInterface name
ParamYesObjectInterface parameters

Param Field Description

FieldRequiredTypeDescription
ResourceTypeYesStringEnter FUNCTION here
EnvIdYesStringEnvironment ID

3. Response

FieldRequiredTypeDescription
RequestIdYesStringRequest unique identifier
AclTagYesStringPermission category
RuleYesString or nullSecurity rule

4. Sample Code

const cloudbaseConfig = {
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId", // CloudBase environment ID, obtain from the Tencent CloudBase Console
};

let manager = new CloudBase(cloudBaseConfig);

async function test() {
const res = await manager.commonService().call({
Action: "DescribeSecurityRule",
Param: {
EnvId: "hello-tcb-9glo12vd5bd3bd0d",
ResourceType: "FUNCTION",
},
});
console.log(res.AclTag);
console.log(res.Rule);
}

test();

Set up Storage Security Rules

1. Interface Description

Function: Set storage security rules

Interface declaration: manager.commonService().call({Action: 'ModifyStorageSafeRule',Param: {}}): Promise<Object>

⚠️ Starting from version 3.0.0, commonService is used as a method with request parameters (service?: string, version?: string), which constitutes a breaking change.

2. Input Parameters

FieldRequiredTypeDescription
ActionYesStringInterface name
ParamYesObjectInterface parameters

Param Field Description

FieldRequiredTypeDescription
BucketYesStringBucket name
AclTagYesStringPermission category
EnvIdYesStringEnvironment ID
RuleNoStringSet this field when the permission category is CUSTOM. Storage Security Rules Documentation

3. Response

FieldRequiredTypeDescription
RequestIdYesStringRequest unique identifier

4. Sample Code

const manager = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId", // CloudBase environment ID, obtain from the Tencent CloudBase Console
});

const { env } = manager;

// First get the bucket name
const {
EnvInfo: { Storages },
} = await env.getEnvInfo();
const { Bucket } = Storages[0];

async function test() {
const res = await manager.commonService().call({
Action: "ModifyStorageSafeRule",
Param: {
Bucket,
AclTag: "CUSTOM",
EnvId: envId,
Rule: JSON.stringify({
read: true,
write: "resource.openid == auth.uid",
}),
},
});
console.log(res);
}
test();

Query Storage Security Rules

1. Interface Description

Function: Query storage security rules

Interface declaration: manager.commonService().call({Action: 'DescribeStorageSafeRule',Param: {}}): Promise<Object>

⚠️ Starting from version 3.0.0, commonService is used as a method with request parameters (service?: string, version?: string), which constitutes a breaking change.

2. Input Parameters

FieldRequiredTypeDescription
ActionYesStringInterface name
ParamYesObjectInterface parameters

Param Field Description

FieldRequiredTypeDescription
BucketYesStringBucket name
EnvIdYesStringEnvironment ID

3. Response

FieldRequiredTypeDescription
RequestIdYesStringRequest unique identifier
AclTagYesStringPermission category
RuleYesString or nullSecurity rule

4. Sample Code

const cloudbaseConfig = {
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId", // CloudBase environment ID, obtain from the Tencent CloudBase Console
};

let manager = new CloudBase(cloudBaseConfig);

async function test() {
const res = await manager.commonService().call({
Action: "DescribeStorageSafeRule",
Param: {
Bucket: "xxx",
EnvId: cloudBaseConfig.envId,
},
});
console.log(res.AclTag);
console.log(res.Rule);
}

test();

Querying the Status of Asynchronous Tasks for Modifying Storage Security Rules

1. Interface Description

Function: Query the status of asynchronous tasks for modifying storage security rules

Interface declaration: manager.commonService().call({Action: 'DescribeCDNChainTask',Param: {}}): Promise<Object>

⚠️ Starting from version 3.0.0, commonService is used as a method with request parameters (service?: string, version?: string), which constitutes a breaking change.

2. Input Parameters

FieldRequiredTypeDescription
ActionYesStringInterface name
ParamYesObjectInterface parameters

Param Field Description

FieldRequiredTypeDescription
BucketYesStringBucket name
EnvIdYesStringEnvironment ID

3. Response

FieldRequiredTypeDescription
RequestIdYesStringRequest unique identifier
StatusRequiredStringStatus of the enable/disable hotlink protection task. Values: WAITING, PENDING, FINISHED, ERROR

4. Sample Code

const cloudbaseConfig = {
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId", // CloudBase environment ID, obtain from the Tencent CloudBase Console
};

let manager = new CloudBase(cloudBaseConfig);

async function test() {
// Get environment information Retrieve bucket
const {
EnvInfo: { Storages },
} = await env.getEnvInfo();
console.log(Storages);
const { Bucket } = Storages[0];
const res = await manager.commonService().call({
Action: "ModifyStorageSafeRule",
Param: {
Bucket,
AclTag: "CUSTOM",
EnvId: envId,
Rule: JSON.stringify({
read: true,
write: "resource.openid == auth.uid",
}),
},
});
console.log(res);
expect(res.RequestId !== undefined).toBe(true);

let status = "";
do {
status = (
await commonService.call({
Action: "DescribeCDNChainTask",
Param: {
Bucket,
EnvId: envId,
},
})
).Status;
console.log(status);
} while (status !== "FINISHED" && status !== "ERROR");
}

test();