Vault Agent Windows service
All authenticated requests to Vault must include a valid token, which means that Vault clients must first authenticate with Vault, and receive a token.
The Secure Introduction of Vault Clients tutorial details available approaches for solving the secret zero problem.
One comprehensive and robust way to solve this problem is to use Vault Agent. Vault Agent can also manage the lifecycle of additional secrets beyond secret zero.
Challenge
Vault Agent is most effective when available to the client application or user environment on a continuous basis and operated as a system service instead of a foreground user process.
Solution
You can configure Vault Agent to operate as a Windows service to automatically start when Windows starts, and become available for use when the client application or user needs it.
Scenario introduction
You can register Vault as a Windows service in different ways. You will use either the Service
Control tool, sc.exe
or the New-Service
cmdlet to register Vault Agent as a Windows service in this tutorial.
For this scenario, you will use PowerShell sessions and command line invocations to start a Vault dev mode server and configure the AppRole auth method for use by Vault Agent.
You will then configure Vault Agent and register it as a service.
You will also confirm that the Agent service can start and authenticate with the dev mode Vault server.
Prerequisites
You need the following to complete this section of the tutorial:
- A Windows host (Tutorial last tested with Windows Server 2019).
- An Administrator level user account on the Windows host that has the capability to register system services. Follow this tutorial while logged in as the local administrator account.
- Vault version 1.7 or later; you can follow the Install Vault tutorial to install Vault on Windows. The community edition is suitable for completing the example scenario.
- jq for consuming Vault JSON formatted output and capturing specific fields for output to files; you need
jq
to follow specific steps in the example scenario.
Lab setup
Vault Agent in production will typically use an external Vault cluster, but for the purposes of this example scenario, you will start and prepare a Vault dev mode server and the Vault Agent on the same host.
Tip
The dev mode server stores everything in memory, so you can just stop it when finished to clean up.
Start a PowerShell session. Start the Vault dev mode server, and specify
root
as the initial root token value.Insecure operation
Do not run a Vault dev server in production. You are using this approach to simplify the unsealing process for the example scenario.
Start a second PowerShell session and set an environment variable with the Vault server address.
Set an environment variable with the Vault token.
Verify the connection to the Vault server.
Leave the two PowerShell terminals open with the Vault server running. The Vault dev mode server is now ready for use.
Configure Vault
Vault Agent needs an enabled auth method for automatically authenticating to Vault. Enable the AppRole auth method.
The success message indicates that you enabled the AppRole auth method at the default path
approle/
.Create a role named
vault-agent-role
for later use by the Agent.Note
The restrictions on token use from this example role emphasize the importance of short lived secrets.
In the
vault-agent-role
, the AppRole secret ID has a 90 minute time to live (TTL) and you can use it 20 times. You can use tokens acquired from this role 10 times, with a maximum TTL of 2 hours.Vault Agent will use this role by passing a role ID and secret ID when it performs the automatic authentication.
Configure Vault Agent
In this scenario, Vault Agent will execute with an example configuration instructing it to connect to the Vault dev mode server and authenticate using the AppRole auth method.
You need to write the vault-agent-role
role ID and a matching secret ID to files in the Vault agent directory to authenticate with Vault using the AppRole auth method. When the Agent service starts, it will consume these files as part of its automatic authentication functionality.
Note
The default behavior for each automatic authentication that Agent tries when using AppRole is to delete the secret ID file regardless of whether authentication succeeds. You should expect the agent-secret-id
file to be missing after starting the service.
From your existing PowerShell session, create a top-level directory to contain the configuration and other data created when following the example scenario.
Note
These examples use the path
C:\vault-agent
, but you can use any name and have access to, so long as you substitute it for any reference of the scenario directory name.Write the role ID to file in the scenario directory called
agent-role-id
.Write the secret ID to file in the scenario directory called
agent-secret-id
.Check the file contents, keeping in mind that this will reveal the secret ID value.
The Vault Agent service uses a configuration file to connect to the Vault server. Assign the file content to a variable named
AgentConfiguration
.If you have Vault binary version 1.13.0 or later, you can specify a file to write Vault Agent log messages.
A log file named
vault-agent-{timestamp}.log
will be generated. To see the full list of available parameters, refer to the Vault Agent documentation.Write the
AgentConfiguration
variable contents to the Vault Agent configuration file.Confirm the Vault address in the Agent configuration file.
The first line is for your Vault server -
http://127.0.0.1.8200
. The second line is the listener address for the Vault Agent -127.0.0.1:8100
.Unset existing environment variables.
Verify removal of the environment variables.
You are now prepared to register the Agent service.
Register Vault Agent as a Windows service
One way to register the service is to use Service Control.
Service Control works best if the path to your Vault binary and the associated Agent configuration file do not contain space characters. Service Control can be difficult to configure if your path has spaces, as you must quote paths containing spaces, and escaping quotes is often non-trivial.
Another alternative is to use the New-Service cmdlet. New-Service is more tolerant about escaping quotes in paths containing spaces. It can sometimes be easier to configure if your path has spaces or you prefer not to use Service Control.
Choose the method you prefer to learn about, and register the Vault Agent service.
Heads up
When you use the Service Control tool, ensure that you explicitly invoke sc.exe
, and not just sc
so that you do not invoke the Set-Content
built-in alias by mistake.
The following example command registers a Vault Agent service that uses "Vault Agent" as the display name and automatically starts when Windows boots.
The binPath
argument should include the fully qualified path to the Vault binary executable, and all required arguments. In this case, it includes the -config
flag to specify the Agent configuration that you just created.
Note
The spacing after the equals sign (=
) in all arguments is intentional and required to register the service without error. The binPath
value here is for the vault.exe
location when Vault installed with Chocolatey, and you should adjust it to match your Vault executable path if you installed Vault another way.
If the output shows "[SC] CreateService SUCCESS", then service registration is complete.
If you observe an error, verify the path to the Vault binary, and check the arguments, by running the contents of binPath=
directly in a PowerShell session and observing the results.
Start Vault Agent service
You can start the service with Service Control, the Start-Service
cmdlet, or with the UI in the Windows Service Manager.
Start the Agent service with sc.exe
.
Verify the Agent token
If the Agent is operating properly, it will automatically authenticate with Vault using AppRole and get a token that it will write to the file sink at C:\vault-agent\agent-token
.
Using this token ID value and the Vault Agent address, you can further authenticate with Vault.
Set the Vault Agent address as the value to the VAULT_ADDR
environment variable so that you are communicating directly to the Agent and not the Vault dev mode server.
Capture the token contained in the agent-token
file as the VAULT_TOKEN
environment variable value.
Try a token lookup.
Example output:
Insecure operation
Displaying plaintext values in terminal sessions is typically discouraged; this command is for illustrative purposes.
Note that the role_name
contained in the meta
is vault-agent-role
, and the token has just the default policy attached. This is confirmation that the token originated from the AppRole auth method on the dev server.
IMPORTANT: VAULT_ADDR
usage
In this tutorial, you set the VAULT_ADDR
environment variable to the target Vault address. Keep in mind that Vault Agent has its own client for Vault server connections.
This is how Vault Agent transparently proxies connections from the CLI to the Vault server. If you set a VAULT_ADDR
environment variable, the Vault Agent will use that value to connect to Vault. This can create an infinite loop where Vault Agent uses the value of VAULT_ADDR
to make a connection, and ends up trying to connect to itself instead of the server.
When the connection fails, the Vault Agent increments the port and tries again. The agent repeats these attempts, which leads to port exhaustion.
This problem is a result of the precedence order of the 3 different ways to configure the Vault address. They are, in increasing order of priority:
- Configuration files
- Environment variables
- CLI flags
Warning
The configuration files have the lowest priority and CLI flags have the highest priority. A value of VAULT_ADDR
in the environment will override the Vault server address configured in the Vault Agent configuration file. A CLI
flag will override everything.
How to avoid infinite loop problem
Do NOT set
VAULT_ADDR
globally in the environment so that the Vault Agent will use the value in its configuration file to connect to the target Vault server instead. This means that you need to manually setVAULT_ADDR
for any sessions involving the Vault CLI.If
VAULT_ADDR
is set globally in the environment, add the-address="<server address>"
CLI flag to the command invocation when creating the Windows service (specified asbinPath
during service creation). This CLI flag will override both the configuration file and the environment variable, and ensure that Vault Agent is always talking to the correct server.Example command:
Next steps
You have learned how to configure Vault Agent as a Windows service and confirm that the service registers and starts.
You showed that the Vault Agent could connect to the Vault dev mode server, and authenticate by checking for a Vault token at the configured sink location.
You also used the Agent and token ID value to authenticate with Vault as a secondary confirmation that Agent acquired a token from the AppRole auth method.
From here, you can expand your Vault Agent knowledge, and learn about more advanced features, such as Agent Caching and Agent Templates.