{"id":755,"date":"2018-07-14T10:27:10","date_gmt":"2018-07-14T10:27:10","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=755"},"modified":"2018-07-14T10:27:10","modified_gmt":"2018-07-14T10:27:10","slug":"setup-hashicorp-vault-beginners-guide","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=755","title":{"rendered":"How to Setup and Configure Hashicorp Vault Server &#8211; Detailed Beginners Guide"},"content":{"rendered":"<p>This Hashicorp vault beginners tutorial will walk you through the steps on how to setup and configure a Hashicorp vault server with detailed instructions.<\/p>\n<h3 id=\"introduction\">Introduction<\/h3>\n<p>Vault is a tool from <a href=\"https:\/\/www.hashicorp.com\/?ref=devopscube.com\" rel=\"noopener noreferrer\">HashiCorp<\/a> for securely storing and accessing secrets. Secret is nothing but all credentials like API Keys, passwords and certificates. Vault provides a unified interface to any secret while providing tight access control and recording a detailed audit log. Most of the organizations would keep their secrets in GitHub which can be seen by anyone who has access to the repo. Vault is designed in such a way that we can keep our database credentials, API keys for external services, credentials into vault and access directly from the application using APIs using various authentication mechanisms. HashiCorp Vault has more advantages than other similar services like HSMs, AWS KM, and keywhiz.<\/p>\n<h2 id=\"most-common-use-cases-of-vault\">Most Common Use Cases of Vault<\/h2>\n<p>Following are the common use cases for Vault<\/p>\n<ol>\n<li>A bare minimum vault can be used as a general secret storage, It is a great tool to store environment variables, DB credentials and API keys.<\/li>\n<li>Vault is a good fit for storing credentials that employees share to access web services. The audit log mechanism lets you know what secrets an employee accessed and when an employee leaves, it is easier to roll keys and understand which keys have and haven&#8217;t been rolled.<\/li>\n<li>The &#8220;dynamic secrets&#8221; feature of Vault is ideal for scripts: It can generate an access key for the duration of a script runtime which is like temporary access token.<\/li>\n<li>In addition to being able to store secrets, Vault can be used to encrypt\/decrypt data that is stored elsewhere. The primary use of this is to allow applications to encrypt their data being in the primary data store.<\/li>\n<\/ol>\n<h2 id=\"key-vault-features\">Key Vault Features<\/h2>\n<p><strong>Secure Secret Storage:<\/strong> Arbitrary key\/value secrets can be stored in Vault. It encrypts the secret and stores in a persistent backend storage. Vault supports multiple storage backends such as a local disk, <a href=\"https:\/\/devopscube.com\/setup-consul-cluster-guide\/\" rel=\"noopener noreferrer\">consul<\/a> or cloud storage like <a href=\"https:\/\/aws.amazon.com\/s3\/?ref=devopscube.com\" rel=\"noopener noreferrer\">AWS S3<\/a> or <a href=\"https:\/\/cloud.google.com\/storage\/docs\/creating-buckets?ref=devopscube.com\" rel=\"noopener noreferrer\">GCS bucket<\/a>.<\/p>\n<p><strong>Dynamic Secrets:<\/strong> Vault can generate secrets on-demand for some systems, such as AWS or SQL databases. For example, when an application needs to access an S3 bucket, it asks Vault for credentials, and Vault will generate an AWS keypair with valid permissions on demand. After creating these dynamic secrets, Vault will also automatically revoke them after the lease is up.<\/p>\n<p><strong>Data Encryption:<\/strong> Vault is capable of encrypting and decrypting data without storing it. This allows security teams to define encryption parameters and developers to store encrypted data in a location such as SQL without having to design their own encryption methods.<\/p>\n<p><strong>Leasing and Renewal:<\/strong> Secrets in vaults are associated with the lease, end of the lease vault will revoke the secrets, We can renew lease using renew APIs.<\/p>\n<p><strong>Revocation:<\/strong> Vault has built-in support for secret revocation.<\/p>\n<h2 id=\"setup-and-configure-vault-server-on-linux\">Setup and configure Vault Server on Linux<\/h2>\n<p>Follow the steps given below for setting up the vault server.<\/p>\n<p><strong>Step 1: <\/strong> Download the latest version of vault binary zip file from <a href=\"https:\/\/releases.hashicorp.com\/vault\/?ref=devopscube.com\">vault release page<\/a> and unzip it.<\/p>\n<pre><code>cd \/opt\/\nsudo wget https:\/\/releases.hashicorp.com\/vault\/0.10.3\/vault_0.10.3_linux_amd64.zip\nsudo unzip vault_0.10.3_linux_amd64.zip -d .<\/code><\/pre>\n<p><strong>Step 2:<\/strong> Copy vault binary into \/usr\/bin. This will allow us to execute vault binary systemwide.<\/p>\n<pre><code>sudo cp vault \/usr\/bin\/<\/code><\/pre>\n<p><strong>Step 3: <\/strong> Create a vault config directory under \/etc,  a vault data directory and logs directory.<\/p>\n<pre><code>sudo mkdir \/etc\/vault\nsudo mkdir \/vault-data\nsudo mkdir -p \/logs\/vault\/<\/code><\/pre>\n<p><strong>Step 4:<\/strong> Create a config.json file and add the vault configuration.<\/p>\n<pre><code>sudo vi \/etc\/vault\/config.json<\/code><\/pre>\n<p>Add the below configuration to the file. Vault supports both JSON and HCL formats. Here we are using JSON format.<\/p>\n<p><strong><em>Note:<\/em><\/strong> replace 10.128.0.2 with your vault host public\/private IP.<\/p>\n<pre><code>{\n\"listener\": [{\n\"tcp\": {\n\"address\" : \"0.0.0.0:8200\",\n\"tls_disable\" : 1\n}\n}],\n\"api_addr\": \"http:\/\/10.128.0.2:8200\",\n\"storage\": {\n    \"file\": {\n    \"path\" : \"\/vault-data\"\n    }\n },\n\"max_lease_ttl\": \"10h\",\n\"default_lease_ttl\": \"10h\",\n\"ui\":true\n}<\/code><\/pre>\n<p><strong>max_lease_ttl<\/strong> &#8211; Specifies the maximum possible lease duration for tokens and secrets. This is specified using a label suffix like &#8220;30s&#8221; or &#8220;1h&#8221;.<\/p>\n<p><strong>default_lease_ttl<\/strong> &#8211; Specifies the default lease duration for tokens and secrets. This is specified using a label suffix like &#8220;30s&#8221; or &#8220;1h&#8221;. This value cannot be larger than max_lease_ttl.<\/p>\n<p><em><strong>Note<\/strong>:<\/em> This config file is created specifically to use filesystem backend, You can even use c<a href=\"https:\/\/devopscube.com\/setup-consul-cluster-guide\/\" rel=\"noopener noreferrer\">onsul cluster<\/a> backend, S3 or GCS (Google cloud storage) backend like shown below,<\/p>\n<p><strong>Vault Consul Backend Config<\/strong><\/p>\n<pre><code>\"storage\": {\n   \"consul\" : {\n      \"address\" : \"127.0.0.1:8500\",\n      \"path\": \"vault\"\n    }\n }<\/code><\/pre>\n<p><strong>Vault Google Storage (GCS) Backend Config<\/strong><\/p>\n<pre><code>\"storage\": {\n\"gcs\": {\n\"bucket\" : \u201cdevopscube-demo\",\n\"ha_enabled\" : \"true\"\n}\n}<\/code><\/pre>\n<p><strong>Step 5:<\/strong> Create a vault service file.<\/p>\n<pre><code>sudo vi \/etc\/systemd\/system\/vault.service<\/code><\/pre>\n<p>Copy the following contents to the service file.<\/p>\n<pre><code>[Unit]\nDescription=vault service\nRequires=network-online.target\nAfter=network-online.target\nConditionFileNotEmpty=\/etc\/vault\/config.json\n\n[Service]\nEnvironmentFile=-\/etc\/sysconfig\/vault\nEnvironment=GOMAXPROCS=2\nRestart=on-failure\nExecStart=\/usr\/bin\/vault server -config=\/etc\/vault\/config.json\nStandardOutput=\/logs\/vault\/output.log\nStandardError=\/logs\/vault\/error.log\nLimitMEMLOCK=infinity\nExecReload=\/bin\/kill -HUP $MAINPID\nKillSignal=SIGTERM\n\n[Install]\nWantedBy=multi-user.target\n<\/code><\/pre>\n<p><strong>Step 6: <\/strong>Start the vault service.<\/p>\n<pre><code>sudo systemctl start vault.service<\/code><\/pre>\n<p>Check the service status.<\/p>\n<pre><code>sudo systemctl status vault.service<\/code><\/pre>\n<p>Enable vault service at boot up to make sure it starts automatically for reboots,<\/p>\n<pre><code>sudo systemctl enable vault.service<\/code><\/pre>\n<p><strong>Step 7:<\/strong> Login as root and Export VAULT_ADDR environment variable, don\u2019t forget to add this to ~\/.bashrc file. Change the IP to you vault server public\/private IP.<\/p>\n<pre><code>export VAULT_ADDR=http:\/\/10.128.0.2:8200\necho \"export VAULT_ADDR=http:\/\/10.128.0.2:8200\" &gt;&gt; ~\/.bashrc<\/code><\/pre>\n<p><strong>Step 8:<\/strong> Check the status using vault status<\/p>\n<pre><code>vault status<\/code><\/pre>\n<p>You will get an error server is not yet initialized as shown below. You could see vault is sealed by default. This is because of the default behavior of vault.<\/p>\n<pre><code>Error checking seal status: Error making API request.\n\nURL: GET http:\/\/104.198.185.234:8200\/v1\/sys\/seal-status\nCode: 400. Errors:\n\n* server is not yet initialized<\/code><\/pre>\n<p>Let&#8217;s initiate the vault server and store the initial tokens in a file.<\/p>\n<p><strong><em>Note<\/em><\/strong>: execute the following command by logging in as the root user.<\/p>\n<pre><code>vault operator init &gt; \/etc\/vault\/init.file<\/code><\/pre>\n<p>Noe vault is initiated but sealed. You can view the status using the following command.<\/p>\n<pre><code>vault status<\/code><\/pre>\n<p>Open the init file to get the unseal and root tokens. These tokens can be used to unseal the vault web UI during the first login.<\/p>\n<pre><code>cat \/etc\/vault\/init.file<\/code><\/pre>\n<p>An example, the output containing the root token is shown below.<\/p>\n<pre><code>Unseal Key 1: jsQ6ZshBCowoddwDhHTy7DgJU9To8YAprYToqPkMUrNg\nUnseal Key 2: 9PWznYV+uM+a1o6rMEGcuINeCtGnMRiV1a5xTe6EerSd\nUnseal Key 3: mavIFllXbQmo7QE2qmLuH9HfYEPQMLpCZNgT0QoRUkcE\nUnseal Key 4: VzXhuvnNuZkld4LnhPEjNyTEMJR3qIkq\/UsinwWWdv5l\nUnseal Key 5: ho23N6R2WGPOpijGsCMElv\/z4u9OxMw9AbEEMbePySU7\n\nInitial Root Token: d4dd0b96-4767-57a3-9081-aca03e530c8f\n\nVault initialized with 5 key shares and a key threshold of 3. Please securely\ndistribute the key shares printed above. When the Vault is re-sealed,\nrestarted, or stopped, you must supply at least 3 of these keys to unseal it\nbefore it can start servicing requests.\n\nVault does not store the generated master key. Without at least 3 key to\nreconstruct the master key, Vault will remain permanently sealed!\n\nIt is possible to generate new unseal keys, provided you have a quorum of\nexisting unseal keys shares. See \"vault operator rekey\" for more information.<\/code><\/pre>\n<p>Once a Vault is unsealed, it remains unsealed until one of two things happens:<\/p>\n<ol>\n<li>It is re-sealed via the API (see below).<\/li>\n<li>If vault service gets restarted or during a server restart.<\/li>\n<\/ol>\n<p><strong>Step 9:<\/strong> Unseal vault using unseal command. There are 5 unseal tokens. You need to execute the unseal command with a minimum of three unseal token to unseal vault.<\/p>\n<pre><code>vault operator unseal jsQ6ZshBCowoddwDhHTy7DgJU9To8YAprYToqPkMUrNg\nvault operator unseal 9PWznYV+uM+a1o6rMEGcuINeCtGnMRiV1a5xTe6EerSd\nvault operator unseal mavIFllXbQmo7QE2qmLuH9HfYEPQMLpCZNgT0QoRUkcE<\/code><\/pre>\n<p>Here you can see your standalone vault is up and running successfully, you can start by enabling authentication method and secret engine which you like,<\/p>\n<pre><code>[root@devopscube opt]# vault status\nKey Value\n--- -----\nSeal Type shamir\nSealed false\nTotal Shares 5\nThreshold 3\nVersion 0.10.3\nCluster Name vault-cluster-865eaf7d\nCluster ID 3164ab8f-d344-c835-af14-644cdc487a73\nHA Enabled true\nHA Cluster https:\/\/10.128.0.2:8201\nHA Mode active<\/code><\/pre>\n<p><strong>Step 10:<\/strong> Now you can also view vault console using the following URL,<\/p>\n<pre><code>http:\/\/IPADDRESS:8200\/ui<\/code><\/pre>\n<p>Finally, you can log in with root credentials which we created while initializing vault, in our case d4dd0b96-4767-57a3-9081-aca03e530c8f. Managing will be easy through UI<\/p>\n<p>Once you login you will see the following page,<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/screen-shot-2018-07-14-at-3-24-06-pm-1.jpg\" class=\"kg-image\" alt=\"vault UI setup\" loading=\"lazy\" width=\"1392\" height=\"618\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/screen-shot-2018-07-14-at-3-24-06-pm-1.jpg 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/screen-shot-2018-07-14-at-3-24-06-pm-1.jpg 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/screen-shot-2018-07-14-at-3-24-06-pm-1.jpg 1392w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<h3 id=\"vault-roles-and-policies\">Vault Roles and Policies<\/h3>\n<p>Once the setup is done, you can use vault by enabling AppRoles or some other auth methods with proper policies associated with it. Covering full roles and policies is out of the scope of this article.<\/p>\n<p>You can create AppRole and Policies through CLI as well as vault console.<\/p>\n<pre><code>vault auth enable approle\nvault write auth\/approle\/role\/demo bound_cidr_list=10.0.0.0\/16 bind_secret_id=false policies=default-policy<\/code><\/pre>\n<p><strong><em>Note:<\/em><\/strong> This same can be done from Vault console also<\/p>\n<p><strong>bound_cidr_list:<\/strong> If bound_cidr_list is set on the role, then the list of CIDR blocks listed here should be a subset of the CIDR blocks listed on the role.<\/p>\n<p><strong>bind_secret_id:<\/strong> Require secret_id to be presented when logging in using this AppRole.<\/p>\n<p>Configure secrets from the console as shown in below image, by default vault uses KV secret engine, we can even use AWS, RabbitMQ, Google cloud or any databases as a secret engine.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/vault-secret-creation-example-1.png\" class=\"kg-image\" alt=\"hashicorp vault - creating secrets\" loading=\"lazy\" width=\"2000\" height=\"570\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/vault-secret-creation-example-1.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/vault-secret-creation-example-1.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1600\/2025\/03\/vault-secret-creation-example-1.png 1600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w2400\/2025\/03\/vault-secret-creation-example-1.png 2400w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>Vault server which we have created is a standalone instance with HA Enabled configuration, If we disable HA Enabled option from config.json this will act as a cluster without HA.<\/p>\n<p>To enable HA we can have multiple machines where we configure api_addr parameter as the host IP, at a time only one of the node will act as the active node and rest of them are standby nodes.<\/p>\n<hr>\n<p><strong>ONLINE COURSE: <\/strong>Managing Secrets with Hashicorp Vault<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/vault-image-1.png\" class=\"kg-image\" alt=\"hashicorp vault online course\" loading=\"lazy\" width=\"527\" height=\"250\"><\/figure>\n<p>Everything you need to get started with Hashicorp Vault<\/p>\n<p><a href=\"https:\/\/devopscube.com\/recommends\/vault-course\/\" rel=\"noopener noreferrer\">Managing Secrets with Hashicorp Vault<\/a><\/p>\n<ul>\n<li>Learn concepts and architecture of vault<\/li>\n<li>Get started secret management<\/li>\n<li>Vault HA and TLS management<\/li>\n<li>Learn to manage secrets using CLI<\/li>\n<li>Learn to build PKI solution<\/li>\n<\/ul>\n<hr>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/setup-hashicorp-vault-beginners-guide\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Setup and Configure Hashicorp Vault Server &#8211; Detailed Beginners Guide \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/setup-hashicorp-vault-beginners-guide\/<\/p>\n","protected":false},"author":1,"featured_media":756,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-755","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/755","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=755"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/755\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/756"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}