本文へスキップ

nonsensitive 関数

nonsensitive は、機密値を受け取り、機密マークを削除したその値のコピーを返します。これにより、機密値が公開されます。

通常、OpenTofuは、機密とマークされている値から新しい値を導出するために式を使用する場合に、その結果も機密としてマークできるように追跡します。

ただし、機密値から機密ではない結果を導出する式を書きたい場合があります。たとえば、特定のシステムとその脅威モデルの詳細に基づいて、特定の機密値のSHA256ハッシュをOpenTofuの出力に明確に含めても安全であることがわかっている場合、nonsensitive関数を使用してそれを示し、OpenTofuの通常の保守的な動作をオーバーライドできます。

コードブロック
output "sensitive_example_hash" {
value = nonsensitive(sha256(var.sensitive_example))
}

別の例としては、元の値が部分的にしか機密ではなく、機密の部分と機密ではない部分を分離する式を書いた場合が考えられます。

コードブロック
variable "mixed_content_json" {
description = "A JSON string containing a mixture of sensitive and non-sensitive values."
type = string
sensitive = true
}

locals {
# mixed_content is derived from var.mixed_content_json, so it
# is also considered to be sensitive.
mixed_content = jsondecode(var.mixed_content_json)

# password_from_json is derived from mixed_content, so it's
# also considered to be sensitive.
password_from_json = local.mixed_content["password"]

# username_from_json would normally be considered to be
# sensitive too, but system-specific knowledge tells us
# that the username is a non-sensitive fragment of the
# original document, and so we can override OpenTofu's
# determination.
username_from_json = nonsensitive(local.mixed_content["username"])
}

この関数を使用する場合は、引数として渡された式が、依存する機密値からすべての機密コンテンツを削除することを確認する責任があります。値をnonsensitiveに渡すことで、機密コンテンツから導出されたものであるにもかかわらず、結果の値に機密コンテンツが含まれていないことを確認するために必要なすべての操作を行ったことをOpenTofuに宣言しています。モジュール内のnonsensitiveへの不適切な呼び出しのために、OpenTofuの出力に機密値が表示された場合は、それはモジュールのバグであり、OpenTofu自体のバグではありません。**この関数は慎重に、必要最小限に留めて使用してください。**

nonsensitive は、機密とマークされていない値を渡すことを許可しますが、そのような呼び出しは冗長であり、モジュールの将来の保守担当者にとって混乱を招いたり、誤解を招いたりする可能性があります。

将来の保守担当者に対して、使用が安全である理由、および将来の変更において維持しなければならない不変量について説明するために、呼び出しに隣接するコメントを含めることを検討してください。

次の例は、上記の例でvariable "mixed_content_json"とローカル値mixed_contentを使用し、有効なJSON文字列がvar.mixed_content_jsonに割り当てられているコンテキストでtofu consoleを実行したものです。

コードブロック
> var.mixed_content_json
(sensitive value)
> local.mixed_content
(sensitive value)
> local.mixed_content["password"]
(sensitive value)
> nonsensitive(local.mixed_content["username"])
"zqb"
> nonsensitive("clear")
"clear"

ただし、nonsensitive を使用する際には、常に安全であることをご自身の責任で確認する必要があります。機密情報として扱うべきコンテンツに対して nonsensitive を使用した場合、そのコンテンツは公開されます。

コードブロック
> nonsensitive(var.mixed_content_json)
<<EOT
{
"username": "zqb",
"password": "p4ssw0rd"
}
EOT
> nonsensitive(local.mixed_content)
{
"password" = "p4ssw0rd"
"username" = "zqb"
}
> nonsensitive(local.mixed_content["password"])
"p4ssw0rd"