前提条件
下载安装HCloud CLI
HCloud CLI的下载安装请参考安装指南。
若已安装HCloud CLI,请执行 "hcloud update" 升级至2.3.7及以上版本。
获取认证相关的信息
开发者在配置HCloud CLI前需先获取认证相关的信息。
1.AK/SK:访问密钥
请在华为云控制台“我的凭证-访问密钥”页面上创建和查看您的AK/SK。更多信息请查看访问密钥。
2.region:当前可调用的区域
华为云各服务应用区域和各服务的终端节点请查看地区和终端节点。
获取管理虚拟私有云VPC安全组相关的参数
在执行命令时可能需获取如下参数:project_id、security_group_id、security_group_rule_id、name、remote_group_id、remote_ip_prefix、ethertype、direction。
1.project_id:云服务所在项目ID
根据需要操作的项目所属区域选择对应的项目ID。若未在命令中传入此参数,华为云CLI会在运行过程中根据用户认证信息自行获取所属区域的父级项目ID。
添加HCloud CLI配置项
认证用的ak和sk直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
请根据实际情况替换命令中的 "{your_xxx}" 参数内容:
hcloud configure set --profile=cliProfile --project-id={your_project_id} --region={your_region} --mode=AKSK --access-key={your_AK} --secret-key={your_SK} --read-timeout=60 --connect-timeout=30
脚本执行完成后可使用此命令删除配置项:
hcloud configure delete --profile=cliProfile
2.security_group_id:安全组ID,安全组唯一标识
添加配置项后,可通过执行hcloud VPC ListSecurityGroups/v2 --profile=cliProfile --json-filter="security_groups[].{Id:id,Name:name}"
命令获取安全组列表,从中选取目标安全组ID。
3.security_group_rule_id:安全组规则ID
添加配置项后,可通过执行hcloud VPC ListSecurityGroupRules/v2 --profile=cliProfile --json-filter="security_group_rules[].{Id:id,Direction:direction,Description:description}"
命令获取安全组规则列表,从中选取目标安全组规则ID。
4.name:安全组名称。取值范围:1-64个字符,支持数字、字母、中文、_(下划线)、-(中划线)、.(点)
5.remote_ip_prefix:远端IP地址,当direction是egress时为虚拟机访问端的地址,当direction是ingress时为访问虚拟机的地址。取值范围:IP地址,或者cidr格式。与remote_group_id、remote_address_group_id互斥。
6.remote_group_id:远端安全组ID,表示该安全组内的流量允许或拒绝。取值范围:租户下存在的安全组ID。与remote_ip_prefix、remote_address_group_id功能互斥。
7.remote_address_group_id:远端地址组ID。取值范围:租户下存在的地址组ID。与remote_ip_prefix、remote_group_id功能互斥。
8.ethertype:IP地址协议类型。取值范围:IPv4,IPv6。
9.direction:出入控制方向。取值范围: egress:出方向;ingress:入方向。
运行结果
脚本执行成功后,控制台打印虚拟私有云VPC安全组或安全组规则信息。
创建安全组
Copied!
{
"security_group": {
"id": "be89b1b2-****-****-****-e873253dc9b6",
"name": "test-amber",
"project_id": "4ff018c31************4936df31948",
"description": "",
"enterprise_project_id": "0",
"security_group_rules": [
{
"id": "52135499-****-****-****-16c0c53dc36e",
"project_id": "4ff018c31************4936df31948",
"security_group_id": "be89b1b2-****-****-****-e873253dc9b6",
"remote_group_id": null,
"direction": "egress",
"protocol": null,
"description": "",
"created_at": "2021-08-16T06:44:10Z",
"updated_at": "2021-08-16T06:44:10Z",
"ethertype": "IPv4",
"remote_ip_prefix": null,
"multiport": null,
"remote_address_group_id": null,
"action": "allow",
"priority": 100
},
... ...
],
"created_at": "2021-08-16T06:44:10Z",
"updated_at": "2021-08-16T06:44:10Z"
},
"request_id": "bea88f90************50bcb7124f36"
}
查询安全组列表
Copied!
{
"security_groups": [
{
"id": "3cc837c5-****-****-****-d395e80066a6",
"name": "Sys-default",
"vpc_id": "default",
"description": "default",
"enterprise_project_id": "0",
"security_group_rules": [
{
"description": null,
"direction": "ingress",
"ethertype": "IPv6",
"id": "b64fad20-****-****-****-9ff810ec35f3",
"port_range_max": null,
"port_range_min": null,
"protocol": null,
"remote_group_id": "3cc837c5-****-****-****-d395e80066a6",
"remote_ip_prefix": null,
"security_group_id": "3cc837c5-****-****-****-d395e80066a6",
"tenant_id": "08e09a6e2************01d57af62b9",
"remote_address_group_id": null
}
... ...
]
}
]
}
查询安全组
Copied!
{
"security_group": {
"id": "3cc837c5-****-****-****-d395e80066a6",
"name": "Sys-default",
"vpc_id": "default",
"description": "default",
"enterprise_project_id": "0",
"security_group_rules": [
{
"description": null,
"direction": "ingress",
"ethertype": "IPv6",
"id": "b64fad20-****-****-****-9ff810ec35f3",
"port_range_max": null,
"port_range_min": null,
"protocol": null,
"remote_group_id": "3cc837c5-****-****-****-d395e80066a6",
"remote_ip_prefix": null,
"security_group_id": "3cc837c5-****-****-****-d395e80066a6",
"tenant_id": "08e09a6e2************01d57af62b9",
"remote_address_group_id": null
},
... ...
]
}
}
删除安全组
Copied!
{
"X-Frame-Options": "SAMEORIGIN",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains;",
"Server": "api-gateway",
"X-Request-Id": "07d13ca8b6************828d3ce30d",
"X-Content-Type-Options": "nosniff",
"Connection": "keep-alive",
"X-Openstack-Request-Id": "Req-07d13ca8b6************828d3ce30d",
"X-Download-Options": "noopen",
"X-XSS-Protection": "1; mode=block;",
"Date": "Mon, 16 Aug 2021 06:53:38 GMT",
"Content-Type": "application/json; charset=utf-8",
"X-Handler-Trace": "C->Q"
}
创建安全组规则
Copied!
{
"security_group_rule": {
"id": "c91bc597-****-****-****-cf42b8fc777b",
"tenant_id": "4ff018c31************4936df31948",
"project_id": "4ff018c31************4936df31948",
"security_group_id": "be89b1b2-****-****-****-e873253dc9b6",
"remote_group_id": "be89b1b2-****-****-****-e873253dc9b6",
"direction": "ingress",
"protocol": null,
"description": "测试",
"created_at": "2021-08-16T06:49:16",
"updated_at": "2021-08-16T06:49:16",
"ethertype": "IPv6",
"remote_ip_prefix": null,
"port_range_max": null,
"port_range_min": null,
"remote_address_group_id": null
}
}
查询安全组规则列表
Copied!
{
"security_group_rules": [
{
"id": "0eca4df6-****-****-****-0835ea331513",
"tenant_id": "08e09a6e2************01d57af62b9",
"project_id": "08e09a6e2************01d57af62b9",
"security_group_id": "3cc837c5-****-****-****-d395e80066a6",
"remote_group_id": "3cc837c5-****-****-****-d395e80066a6",
"direction": "ingress",
"protocol": null,
"description": "",
"created_at": "2021-08-16T06:38:17",
"updated_at": "2021-08-16T06:38:17",
"ethertype": "IPv4",
"remote_ip_prefix": null,
"port_range_max": null,
"port_range_min": null,
"remote_address_group_id": null
},
... ...
],
"security_group_rules_links": [
{
"href": "https://network.region.cn-east-3.hwclouds.com:443/v2.0/security-group-rules?limit=2000&marker=0eca4df6-****-****-****-0835ea331513&page_reverse=true",
"rel": "previous"
}
]
}
查询安全组规则
Copied!
{
"security_group_rule": {
"description": "",
"direction": "ingress",
"ethertype": "IPv6",
"id": "b64fad20-****-****-****-9ff810ec35f3",
"port_range_max": null,
"port_range_min": null,
"protocol": null,
"remote_group_id": "3cc837c5-****-****-****-d395e80066a6",
"remote_ip_prefix": null,
"security_group_id": "3cc837c5-****-****-****-d395e80066a6",
"tenant_id": "08e09a6e2************01d57af62b9",
"remote_address_group_id": null
}
}
删除安全组规则
Copied!
{
"X-Frame-Options": "SAMEORIGIN",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains;",
"Server": "api-gateway",
"X-Request-Id": "cafb2c3b6************942a4a071b3",
"X-Content-Type-Options": "nosniff",
"Connection": "keep-alive",
"X-Openstack-Request-Id": "Req-cafb2c3b6************942a4a071b3",
"X-Download-Options": "noopen",
"X-XSS-Protection": "1; mode=block;",
"Date": "Mon, 16 Aug 2021 06:48:39 GMT",
"Content-Type": "application/json; charset=utf-8",
"X-Handler-Trace": "C->Q"
}
描述
基于华为云CLI,以shell脚本模板形式实现对虚拟私有云VPC安全组的管理。
关于华为云CLI更多使用帮助请参考用户指南。
场景介绍
本示例实现使用CLI便捷管理虚拟私有云VPC安全组,创建安全组,并向其中添加新创建的安全组规则。
涉及相关功能如下:
创建安全组;
查询安全组列表;
查询指定安全组;
删除安全组;
创建安全组规则:分为命令中可传入以下参数之一(非必填):1. 远端IP地址(remote_ip_prefix);2. 远端安全组ID(remote_group_id);3. 远端地址组ID(remote_address_group_id);
查询安全组规则列表;
查询指定安全组规则;
删除安全组规则;
前提条件
下载安装HCloud CLI
HCloud CLI的下载安装请参考安装指南。 若已安装HCloud CLI,请执行 "hcloud update" 升级至2.3.7及以上版本。
获取认证相关的信息
开发者在配置HCloud CLI前需先获取认证相关的信息。
1.AK/SK:访问密钥
请在华为云控制台“我的凭证-访问密钥”页面上创建和查看您的AK/SK。更多信息请查看访问密钥。
2.region:当前可调用的区域
华为云各服务应用区域和各服务的终端节点请查看地区和终端节点。
获取管理虚拟私有云VPC安全组相关的参数
在执行命令时可能需获取如下参数:project_id、security_group_id、security_group_rule_id、name、remote_group_id、remote_ip_prefix、ethertype、direction。
1.project_id:云服务所在项目ID
根据需要操作的项目所属区域选择对应的项目ID。若未在命令中传入此参数,华为云CLI会在运行过程中根据用户认证信息自行获取所属区域的父级项目ID。
添加HCloud CLI配置项
认证用的ak和sk直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
请根据实际情况替换命令中的 "{your_xxx}" 参数内容:
hcloud configure set --profile=cliProfile --project-id={your_project_id} --region={your_region} --mode=AKSK --access-key={your_AK} --secret-key={your_SK} --read-timeout=60 --connect-timeout=30
脚本执行完成后可使用此命令删除配置项:
hcloud configure delete --profile=cliProfile
2.security_group_id:安全组ID,安全组唯一标识
添加配置项后,可通过执行
hcloud VPC ListSecurityGroups/v2 --profile=cliProfile --json-filter="security_groups[].{Id:id,Name:name}"
命令获取安全组列表,从中选取目标安全组ID。3.security_group_rule_id:安全组规则ID
添加配置项后,可通过执行
hcloud VPC ListSecurityGroupRules/v2 --profile=cliProfile --json-filter="security_group_rules[].{Id:id,Direction:direction,Description:description}"
命令获取安全组规则列表,从中选取目标安全组规则ID。4.name:安全组名称。取值范围:1-64个字符,支持数字、字母、中文、_(下划线)、-(中划线)、.(点)
5.remote_ip_prefix:远端IP地址,当direction是egress时为虚拟机访问端的地址,当direction是ingress时为访问虚拟机的地址。取值范围:IP地址,或者cidr格式。与remote_group_id、remote_address_group_id互斥。
6.remote_group_id:远端安全组ID,表示该安全组内的流量允许或拒绝。取值范围:租户下存在的安全组ID。与remote_ip_prefix、remote_address_group_id功能互斥。
7.remote_address_group_id:远端地址组ID。取值范围:租户下存在的地址组ID。与remote_ip_prefix、remote_group_id功能互斥。
8.ethertype:IP地址协议类型。取值范围:IPv4,IPv6。
9.direction:出入控制方向。取值范围: egress:出方向;ingress:入方向。
示例代码
添加配置项后,使用如下代码管理虚拟私有云VPC安全组,调用前请根据实际情况替换其中的 "{your_xxx}" 参数内容;方括号中的参数为非必填参数,可删除。
添加配置项后,使用如下代码管理虚拟私有云VPC安全组规则,调用前请根据实际情况替换其中的 "{your_xxx}" 参数内容;命令中方括号 "[]" 中的参数为非必填参数,可删除。
运行示例
1.运行时首先通过“sudo ln -s CLI工具所在目录 /usr/local/bin/”命令配置华为云CLI工具环境变量,或将脚本模板放置在华为云CLI工具所在目录,并在hcloud开头的相关命令前加“./”。
2.添加HCloud CLI配置项后,根据实际情况修改命令中的部分参数内容,替换其中的 "{your_xxx}" 参数内容,或注释无关的命令;命令中方括号 "[]" 中的参数为非必填参数,若不需要该参数请将该参数与方括号一同删除;最后执行“bash vpc-security-groups-manage.sh”命令。
运行结果
脚本执行成功后,控制台打印虚拟私有云VPC安全组或安全组规则信息。
创建安全组
{ "security_group": { "id": "be89b1b2-****-****-****-e873253dc9b6", "name": "test-amber", "project_id": "4ff018c31************4936df31948", "description": "", "enterprise_project_id": "0", "security_group_rules": [ { "id": "52135499-****-****-****-16c0c53dc36e", "project_id": "4ff018c31************4936df31948", "security_group_id": "be89b1b2-****-****-****-e873253dc9b6", "remote_group_id": null, "direction": "egress", "protocol": null, "description": "", "created_at": "2021-08-16T06:44:10Z", "updated_at": "2021-08-16T06:44:10Z", "ethertype": "IPv4", "remote_ip_prefix": null, "multiport": null, "remote_address_group_id": null, "action": "allow", "priority": 100 }, ... ... ], "created_at": "2021-08-16T06:44:10Z", "updated_at": "2021-08-16T06:44:10Z" }, "request_id": "bea88f90************50bcb7124f36" }
查询安全组列表
{ "security_groups": [ { "id": "3cc837c5-****-****-****-d395e80066a6", "name": "Sys-default", "vpc_id": "default", "description": "default", "enterprise_project_id": "0", "security_group_rules": [ { "description": null, "direction": "ingress", "ethertype": "IPv6", "id": "b64fad20-****-****-****-9ff810ec35f3", "port_range_max": null, "port_range_min": null, "protocol": null, "remote_group_id": "3cc837c5-****-****-****-d395e80066a6", "remote_ip_prefix": null, "security_group_id": "3cc837c5-****-****-****-d395e80066a6", "tenant_id": "08e09a6e2************01d57af62b9", "remote_address_group_id": null } ... ... ] } ] }
查询安全组
{ "security_group": { "id": "3cc837c5-****-****-****-d395e80066a6", "name": "Sys-default", "vpc_id": "default", "description": "default", "enterprise_project_id": "0", "security_group_rules": [ { "description": null, "direction": "ingress", "ethertype": "IPv6", "id": "b64fad20-****-****-****-9ff810ec35f3", "port_range_max": null, "port_range_min": null, "protocol": null, "remote_group_id": "3cc837c5-****-****-****-d395e80066a6", "remote_ip_prefix": null, "security_group_id": "3cc837c5-****-****-****-d395e80066a6", "tenant_id": "08e09a6e2************01d57af62b9", "remote_address_group_id": null }, ... ... ] } }
删除安全组
{ "X-Frame-Options": "SAMEORIGIN", "Strict-Transport-Security": "max-age=31536000; includeSubdomains;", "Server": "api-gateway", "X-Request-Id": "07d13ca8b6************828d3ce30d", "X-Content-Type-Options": "nosniff", "Connection": "keep-alive", "X-Openstack-Request-Id": "Req-07d13ca8b6************828d3ce30d", "X-Download-Options": "noopen", "X-XSS-Protection": "1; mode=block;", "Date": "Mon, 16 Aug 2021 06:53:38 GMT", "Content-Type": "application/json; charset=utf-8", "X-Handler-Trace": "C->Q" }
创建安全组规则
{ "security_group_rule": { "id": "c91bc597-****-****-****-cf42b8fc777b", "tenant_id": "4ff018c31************4936df31948", "project_id": "4ff018c31************4936df31948", "security_group_id": "be89b1b2-****-****-****-e873253dc9b6", "remote_group_id": "be89b1b2-****-****-****-e873253dc9b6", "direction": "ingress", "protocol": null, "description": "测试", "created_at": "2021-08-16T06:49:16", "updated_at": "2021-08-16T06:49:16", "ethertype": "IPv6", "remote_ip_prefix": null, "port_range_max": null, "port_range_min": null, "remote_address_group_id": null } }
查询安全组规则列表
{ "security_group_rules": [ { "id": "0eca4df6-****-****-****-0835ea331513", "tenant_id": "08e09a6e2************01d57af62b9", "project_id": "08e09a6e2************01d57af62b9", "security_group_id": "3cc837c5-****-****-****-d395e80066a6", "remote_group_id": "3cc837c5-****-****-****-d395e80066a6", "direction": "ingress", "protocol": null, "description": "", "created_at": "2021-08-16T06:38:17", "updated_at": "2021-08-16T06:38:17", "ethertype": "IPv4", "remote_ip_prefix": null, "port_range_max": null, "port_range_min": null, "remote_address_group_id": null }, ... ... ], "security_group_rules_links": [ { "href": "https://network.region.cn-east-3.hwclouds.com:443/v2.0/security-group-rules?limit=2000&marker=0eca4df6-****-****-****-0835ea331513&page_reverse=true", "rel": "previous" } ] }
查询安全组规则
{ "security_group_rule": { "description": "", "direction": "ingress", "ethertype": "IPv6", "id": "b64fad20-****-****-****-9ff810ec35f3", "port_range_max": null, "port_range_min": null, "protocol": null, "remote_group_id": "3cc837c5-****-****-****-d395e80066a6", "remote_ip_prefix": null, "security_group_id": "3cc837c5-****-****-****-d395e80066a6", "tenant_id": "08e09a6e2************01d57af62b9", "remote_address_group_id": null } }
删除安全组规则
{ "X-Frame-Options": "SAMEORIGIN", "Strict-Transport-Security": "max-age=31536000; includeSubdomains;", "Server": "api-gateway", "X-Request-Id": "cafb2c3b6************942a4a071b3", "X-Content-Type-Options": "nosniff", "Connection": "keep-alive", "X-Openstack-Request-Id": "Req-cafb2c3b6************942a4a071b3", "X-Download-Options": "noopen", "X-XSS-Protection": "1; mode=block;", "Date": "Mon, 16 Aug 2021 06:48:39 GMT", "Content-Type": "application/json; charset=utf-8", "X-Handler-Trace": "C->Q" }
参考
关于华为云CLI的更多信息请参考:https://support.huaweicloud.com/productdesc-hcli/hcli_01.html
修订记录