github.com/hugorut/terraform@v1.1.3/website/docs/internals/credentials-helpers.mdx (about)

     1  ---
     2  page_title: Credentials Helpers
     3  description: >-
     4    Credentials helpers are external programs that know how to store and retrieve
     5    API tokens for remote Terraform services.
     6  ---
     7  
     8  # Credentials Helpers
     9  
    10  For Terraform-specific features that interact with remote network services,
    11  such as [module registries](/registry) and
    12  [remote operations](/cloud-docs/run/cli), Terraform by default looks for
    13  API credentials to use in these calls in
    14  [the CLI configuration](/cli/config/config-file).
    15  
    16  Credentials helpers offer an alternative approach that allows you to customize
    17  how Terraform obtains credentials using an external program, which can then
    18  directly access an existing secrets management system in your organization.
    19  
    20  This page is about how to write and install a credentials helper. To learn
    21  how to configure a credentials helper that was already installed, see
    22  [the CLI config Credentials Helpers section](/cli/config/config-file#credentials-helpers).
    23  
    24  ## How Terraform finds Credentials Helpers
    25  
    26  A credentials helper is a normal executable program that is installed in a
    27  particular location and whose name follows a specific naming convention.
    28  
    29  A credentials helper called "credstore", for example, would be implemented as
    30  an executable program named `terraform-credentials-credstore` (with an `.exe`
    31  extension on Windows only), and installed in one of the
    32  [default plugin search locations](/plugin/how-terraform-works#plugin-locations).
    33  
    34  ## How Terraform runs Credentials Helpers
    35  
    36  Once Terraform has located the configured credentials helper, it will execute
    37  it once for each credentials request that cannot be satisfied by a `credentials`
    38  block in the CLI configuration.
    39  
    40  For the following examples, we'll assume a "credstore" credentials helper
    41  configured as follows:
    42  
    43  ```
    44  credentials_helper "credstore" {
    45    args = ["--host=credstore.example.com"]
    46  }
    47  ```
    48  
    49  Terraform runs the helper program with each of the arguments given in `args`,
    50  followed by an _verb_ and then the hostname that the verb will apply to.
    51  The current set of verbs are:
    52  
    53  * `get`: retrieve the credentials for the given hostname
    54  * `store`: store new credentials for the given hostname
    55  * `forget`: delete any stored credentials for the given hostname
    56  
    57  To represent credentials, the credentials helper protocol uses a JSON object
    58  whose contents correspond with the contents of
    59  [`credentials` blocks in the CLI configuration](/cli/config/config-file#credentials).
    60  To represent an API token, the object contains a property called "token" whose
    61  value is the token string:
    62  
    63  ```json
    64  {
    65    "token": "example-token-value"
    66  }
    67  ```
    68  
    69  The following sections describe the specific expected behaviors for each of the
    70  three verbs.
    71  
    72  ## `get`: retrieve the credentials for the given hostname
    73  
    74  To retrieve credentials for `app.terraform.io`, Terraform would run the
    75  "credstore" helper as follows:
    76  
    77  ```
    78  terraform-credentials-credstore --host=credstore.example.com get app.terraform.io
    79  ```
    80  
    81  If the credentials helper is able to provide credentials for the given host
    82  then it must print a JSON credentials object to its stdout stream and then
    83  exit with status code zero to indicate success.
    84  
    85  If the credentials helper definitively has no credentials for the given host,
    86  then it must print an empty JSON object to stdout and exit with status zero.
    87  
    88  If the credentials helper is unable to provide the requested credentials for
    89  any other reason, it must print an end-user-oriented plain text error message
    90  to its stderr stream and then exit with a _non-zero_ status code.
    91  
    92  ## `store`: store new credentials for the given hostname
    93  
    94  To store new credentials for `app.terraform.io`, Terraform would run the
    95  "credstore" helper as follows:
    96  
    97  ```
    98  terraform-credentials-credstore --host=credstore.example.com store app.terraform.io
    99  ```
   100  
   101  Terraform then writes a JSON credentials object to the helper program's stdin
   102  stream. If the helper is able to store the given credentials then it must do
   103  so and then exit with status code zero and no output on stdout or stderr to
   104  indicate success.
   105  
   106  If it is unable to store the given credentials for any reason, it _must_ still
   107  fully read its stdin until EOF and then print an end-user-oriented plain text
   108  error message to its stderr stream before exiting with a non-zero status
   109  code.
   110  
   111  The new credentials must fully replace any existing credentials stored for the
   112  given hostname.
   113  
   114  ## `forget`: delete any stored credentials for the given hostname
   115  
   116  To forget any existing credentials for `app.terraform.io`, Terraform would run
   117  the "credstore" helper as follows:
   118  
   119  ```
   120  terraform-credentials-credstore --host=credstore.example.com forget app.terraform.io
   121  ```
   122  
   123  No JSON credentials objects are used for the `forget` verb.
   124  
   125  If the helper program is able to delete its stored credentials for the given
   126  hostname or if there are no such credentials stored already then it must
   127  exist with status code zero and produce no output on stdout or stderr.
   128  
   129  If it is unable to forget the stored credentials for any reason, particularly
   130  if the helper cannot be sure that the credentials are no longer available for
   131  retrieval, the helper program must print an end-user-oriented plain text error
   132  message to its stderr stream and then exit with a non-zero status code.
   133  
   134  ## Handling Other Commands
   135  
   136  The credentials helper protocol may be extended with additional verbs in future,
   137  so for forward-compatibility a credentials helper must react to any unsupported
   138  verb by printing an end-user-oriented plain text error message to its stderr
   139  stream and then exiting with a non-zero status code.
   140  
   141  ## Handling Unsupported Credentials Object Properties
   142  
   143  Currently Terraform defines only the `token` property within JSON credentials
   144  objects, but this format might be extended in future.
   145  
   146  If a credentials helper is asked to store an object that has any properties
   147  other than `token` and if it is not able to faithfully retain them then it
   148  must behave as if the object is unstorable, returning an error. It must _not_
   149  store the `token` value in isolation and silently drop other properties, as
   150  that might change the meaning of the credentials object.
   151  
   152  If technically possible within the constraints of the target system, a
   153  credentials helper should prefer to store the whole JSON object as-is for
   154  later retrieval. For systems that are more constrained, it's acceptable to
   155  store only the `token` string so long as the program rejects objects containing
   156  other properties as described above.
   157  
   158  ## Installing a Credentials Helper
   159  
   160  Terraform does not have any automatic installation mechanism for credentials
   161  helpers. Instead, the user must extract the helper program executable into
   162  one of the [default plugin search locations](/plugin/how-terraform-works#plugin-locations).
   163  
   164  If you are packaging a credentials helper for distribution, place it in an
   165  named with the expected naming scheme (`terraform-credentials-example`) and,
   166  if the containing archive format supports it and it's meaningful for the
   167  target operating system, mark the file as executable to increase the chances
   168  that it will work immediately after extraction.
   169  
   170  Terraform does _not_ honor the `-plugin-dir` argument to `terraform init` when
   171  searching for credentials helpers, because credentials are also used by other
   172  commands that can be run prior to `terraform init`. Only the default search
   173  locations are supported.