# 部署权限规则

## 应用场景

华为云弹性文件服务（SFS Turbo）的权限规则功能允许用户为SFS Turbo文件系统配置访问控制规则，定义哪些IP地址或网段可以访问文件系统，以及访问的权限级别。通过配置权限规则，您可以实现文件系统的安全访问控制、网络隔离和权限管理。

本最佳实践特别适用于需要控制SFS Turbo文件系统访问权限、实现网络安全隔离、管理多用户访问的场景，如多租户环境、安全敏感应用、企业级文件共享等。本最佳实践将介绍如何使用Terraform自动化部署SFS Turbo的权限规则，包括VPC网络、安全组、SFS Turbo文件系统和权限规则的创建，实现完整的文件系统访问控制解决方案。

## 相关资源/数据源

本最佳实践涉及以下主要资源和数据源：

### 数据源

* [可用区查询数据源（data.huaweicloud\_availability\_zones）](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs/data-sources/availability_zones)

### 资源

* [VPC资源（huaweicloud\_vpc）](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs/resources/vpc)
* [VPC子网资源（huaweicloud\_vpc\_subnet）](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs/resources/vpc_subnet)
* [安全组资源（huaweicloud\_networking\_secgroup）](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs/resources/networking_secgroup)
* [SFS Turbo文件系统资源（huaweicloud\_sfs\_turbo）](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs/resources/sfs_turbo)
* [SFS Turbo权限规则资源（huaweicloud\_sfs\_turbo\_perm\_rule）](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs/resources/sfs_turbo_perm_rule)

### 资源/数据源依赖关系

```
data.huaweicloud_availability_zones
    └── huaweicloud_sfs_turbo

huaweicloud_vpc
    ├── huaweicloud_vpc_subnet
    └── huaweicloud_sfs_turbo

huaweicloud_vpc_subnet
    └── huaweicloud_sfs_turbo

huaweicloud_networking_secgroup
    └── huaweicloud_sfs_turbo

huaweicloud_sfs_turbo
    └── huaweicloud_sfs_turbo_perm_rule
```

## 操作步骤

### 1. 脚本准备

在指定工作空间中准备好用于编写当前最佳实践脚本的TF文件（如main.tf），确保其中（也可以是其他同级目录下的TF文件）包含部署资源所需的provider版本声明和华为云鉴权信息。 配置介绍参考[部署华为云资源前的准备工作](https://hcbp.gitbook.io/hua-wei-yun-provider/chan-pin-jie-shao/prepare_before_deploy)一文中的介绍。

### 2. 查询可用区信息

在TF文件（如main.tf）中添加以下脚本以告知Terraform查询可用区信息：

```hcl
# 获取指定region（region参数缺省时默认继承当前provider块中所指定的region）下所有可用的可用区信息，用于创建SFS Turbo文件系统
data "huaweicloud_availability_zones" "test" {}
```

**参数说明**：

* 此数据源无需额外参数，会自动查询当前区域的所有可用区

### 3. 创建VPC网络

在TF文件中添加以下脚本以告知Terraform创建VPC资源：

```hcl
variable "vpc_name" {
  description = "The name of the VPC"
  type        = string
}

variable "vpc_cidr" {
  description = "The CIDR block of the VPC"
  type        = string
  default     = "192.168.0.0/16"
}

# 在指定region（region参数缺省时默认继承当前provider块中所指定的region）下创建VPC资源
resource "huaweicloud_vpc" "test" {
  name = var.vpc_name
  cidr = var.vpc_cidr
}
```

**参数说明**：

* **name**：VPC的名称，通过引用输入变量vpc\_name进行赋值
* **cidr**：VPC的CIDR块，通过引用输入变量vpc\_cidr进行赋值，默认值为"192.168.0.0/16"

### 4. 创建VPC子网

在TF文件中添加以下脚本以告知Terraform创建VPC子网资源：

```hcl
variable "subnet_name" {
  description = "The name of the subnet"
  type        = string
}

variable "subnet_cidr" {
  description = "The CIDR block of the subnet"
  type        = string
  default     = ""
  nullable    = false
}

variable "subnet_gateway_ip" {
  description = "The gateway IP of the subnet"
  type        = string
  default     = ""
  nullable    = false
}

# 在指定region（region参数缺省时默认继承当前provider块中所指定的region）下创建VPC子网资源
resource "huaweicloud_vpc_subnet" "test" {
  vpc_id     = huaweicloud_vpc.test.id
  name       = var.subnet_name
  cidr       = var.subnet_cidr == "" ? cidrsubnet(huaweicloud_vpc.test.cidr, 8, 0) : var.subnet_cidr
  gateway_ip = var.subnet_gateway_ip == "" ? cidrhost(cidrsubnet(huaweicloud_vpc.test.cidr, 8, 0), 1) : var.subnet_gateway_ip
}
```

**参数说明**：

* **vpc\_id**：子网所属的VPC ID，引用前面创建的VPC资源的ID
* **name**：子网的名称，通过引用输入变量subnet\_name进行赋值
* **cidr**：子网的CIDR块，如果subnet\_cidr为空则自动计算，否则使用subnet\_cidr的值
* **gateway\_ip**：子网的网关IP，如果subnet\_gateway\_ip为空则自动计算，否则使用subnet\_gateway\_ip的值

### 5. 创建安全组

在TF文件中添加以下脚本以告知Terraform创建安全组资源：

```hcl
variable "security_group_name" {
  description = "The name of the security group"
  type        = string
}

# 在指定region（region参数缺省时默认继承当前provider块中所指定的region）下创建安全组资源
resource "huaweicloud_networking_secgroup" "test" {
  name                 = var.security_group_name
  delete_default_rules = true
}
```

**参数说明**：

* **name**：安全组的名称，通过引用输入变量security\_group\_name进行赋值
* **delete\_default\_rules**：是否删除默认规则，设置为true表示删除默认安全组规则

### 6. 创建SFS Turbo文件系统

在TF文件中添加以下脚本以告知Terraform创建SFS Turbo文件系统资源：

```hcl
variable "turbo_availability_zone" {
  description = "The availability zone to which the SFS Turbo belongs"
  type        = string
  default     = ""
  nullable    = false
}

variable "turbo_name" {
  description = "The name of the SFS Turbo file system"
  type        = string
}

variable "turbo_size" {
  description = "The capacity of the SFS Turbo file system"
  type        = number
  default     = 500
}

variable "turbo_share_proto" {
  description = "The protocol of the SFS Turbo file system"
  type        = string
  default     = "NFS"
}

variable "turbo_share_type" {
  description = "The type of the SFS Turbo file system"
  type        = string
  default     = "STANDARD"
}

variable "turbo_hpc_bandwidth" {
  description = "The bandwidth specification of the SFS Turbo file system"
  type        = string
  default     = ""
}

# 在指定region（region参数缺省时默认继承当前provider块中所指定的region）下创建SFS Turbo文件系统资源
resource "huaweicloud_sfs_turbo" "test" {
  vpc_id            = huaweicloud_vpc.test.id
  subnet_id         = huaweicloud_vpc_subnet.test.id
  security_group_id = huaweicloud_networking_secgroup.test.id
  availability_zone = var.turbo_availability_zone != "" ? var.turbo_availability_zone : try(data.huaweicloud_availability_zones.test.names[0], null)
  name              = var.turbo_name
  size              = var.turbo_size
  share_proto       = var.turbo_share_proto
  share_type        = var.turbo_share_type
  hpc_bandwidth     = var.turbo_hpc_bandwidth
}
```

**参数说明**：

* **vpc\_id**：VPC ID，引用前面创建的VPC资源的ID
* **subnet\_id**：子网ID，引用前面创建的VPC子网资源的ID
* **security\_group\_id**：安全组ID，引用前面创建的安全组资源的ID
* **availability\_zone**：可用区，优先使用turbo\_availability\_zone变量，如果为空则使用查询到的第一个可用区
* **name**：SFS Turbo文件系统的名称，通过引用输入变量turbo\_name进行赋值
* **size**：文件系统容量，通过引用输入变量turbo\_size进行赋值，默认值为500（GB）
* **share\_proto**：共享协议，通过引用输入变量turbo\_share\_proto进行赋值，默认值为"NFS"
* **share\_type**：共享类型，通过引用输入变量turbo\_share\_type进行赋值，默认值为"STANDARD"
* **hpc\_bandwidth**：HPC带宽规格，通过引用输入变量turbo\_hpc\_bandwidth进行赋值，默认值为空字符串

### 7. 创建SFS Turbo权限规则

在TF文件中添加以下脚本以告知Terraform创建SFS Turbo权限规则资源：

```hcl
variable "rule_ip_cidr" {
  description = "The IP address or network segment of the authorized object"
  type        = string
  default     = "192.168.0.0/16"
}

variable "rule_rw_type" {
  description = "The read and write permissions for the authorized object"
  type        = string
  default     = "rw"
}

variable "rule_user_type" {
  description = "The access permissions of the system users of the authorized object to the file system"
  type        = string
  default     = "no_root_squash"
}

# 在指定region（region参数缺省时默认继承当前provider块中所指定的region）下创建SFS Turbo权限规则资源
resource "huaweicloud_sfs_turbo_perm_rule" "test" {
  share_id  = huaweicloud_sfs_turbo.test.id
  ip_cidr   = var.rule_ip_cidr
  rw_type   = var.rule_rw_type
  user_type = var.rule_user_type
}
```

**参数说明**：

* **share\_id**：SFS Turbo文件系统ID，引用前面创建的SFS Turbo文件系统资源的ID
* **ip\_cidr**：授权对象的IP地址或网段，通过引用输入变量rule\_ip\_cidr进行赋值，默认值为"192.168.0.0/16"
* **rw\_type**：授权对象的读写权限，通过引用输入变量rule\_rw\_type进行赋值，默认值为"rw"（读写权限）
* **user\_type**：授权对象系统用户对文件系统的访问权限，通过引用输入变量rule\_user\_type进行赋值，默认值为"no\_root\_squash"（不限制root用户）

### 8. 预设资源部署所需的入参（可选）

本实践中，部分资源、数据源使用了输入变量对配置内容进行赋值，这些输入参数在后续部署时需要手工输入。 同时，Terraform提供了通过`.tfvars`文件预设这些配置的方法，可以避免每次执行时重复输入。

在工作目录下创建`terraform.tfvars`文件，示例内容如下：

```hcl
# 华为云认证信息
region_name = "cn-north-4"
access_key  = "your-access-key"
secret_key  = "your-secret-key"

# VPC网络配置
vpc_name = "tf_test_perm_rule"

# 子网配置
subnet_name = "tf_test_perm_rule"

# 安全组配置
security_group_name = "tf_test_perm_rule"

# SFS Turbo文件系统配置
turbo_name = "tf_test_perm_rule_demo"
```

**使用方法**：

1. 将上述内容保存为工作目录下的`terraform.tfvars`文件（该文件名可使用户在执行terraform命令时自动导入该`tfvars`文件中的内容，其他命名则需要在tfvars前补充`.auto`定义，如`variables.auto.tfvars`）
2. 根据实际需要修改参数值
3. 执行`terraform plan`或`terraform apply`时，Terraform会自动读取该文件中的变量值

除了使用`terraform.tfvars`文件外，还可以通过以下方式设置变量值：

1. 命令行参数：`terraform apply -var="vpc_name=my-vpc" -var="turbo_name=my-turbo"`
2. 环境变量：`export TF_VAR_vpc_name=my-vpc`
3. 自定义命名的变量文件：`terraform apply -var-file="custom.tfvars"`

> 注意：如果同一个变量通过多种方式进行设置，Terraform会按照以下优先级使用变量值：命令行参数 > 变量文件 > 环境变量 > 默认值。

### 9. 初始化并应用Terraform配置

完成以上脚本配置后，执行以下步骤来创建资源：

1. 运行 `terraform init` 初始化环境
2. 运行 `terraform plan` 查看资源创建计划
3. 确认资源计划无误后，运行 `terraform apply` 开始创建权限规则
4. 运行 `terraform show` 查看已创建的权限规则详情

## 参考信息

* [华为云弹性文件服务产品文档](https://support.huaweicloud.com/sfsturbo/index.html)
* [华为云Provider文档](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs)
* [SFS Turbo权限规则最佳实践源码参考](https://github.com/huaweicloud/terraform-provider-huaweicloud/tree/master/examples/sfs-turbo/permission-rule)
