Using Secrets Safely: A Pattern for Using HashiCorp Vault

HashiCorp Vault is an open source tool that provides a safe and secure way to store and distribute secrets such as API keys, access tokens and passwords. Software such as Vault can be critical when deploying applications that require secrets or sensitive data.







According to a recent study by researchers at North Carolina State University, over 100,000 public GitHub repositories contain open-source application secrets directly in source code. This study - from private API tokens to cryptographic keys - only scanned about 13% of the public GitHub repositories - shows that properly securing application secrets is one of the most overlooked methods of protecting information in software.







While the magnitude of the impact is surprising, it is important to note that this issue is not limited to open source projects. Even private source code repositories can reveal secrets if not properly secured. Take the security breach at Buffer Inc. in 2013. What started out as illegal access to Buffer's own source code leaked the company's Twitter API credentials, eventually spamming countless customers' Twitter accounts.







I'm not going to oppress Buffer right now. Companies are hacked every day, and Buffer gave a top-notch answer. Their unfiltered transparency and incident reporting provided an interesting example of the importance of secrecy management as a fundamental principle of information security. But it also raises the question of how best to manage secrets in a growing, scalable organization.







Introduction to HashiCorp Vault



I'm a huge HashiCorp fan. Their vendor-agnostic approach to DevOps tools provides excellent portable solutions that abstract from individual cloud providers and focus on solving real-world problems. Their secret management tool, Vault , is no exception.







, Vault , .







Vault



Vault β€” Vault HashiCorp







Vault, . HashiCorp, Vault , macOS, Windows, Linux, Solaris BSD. Raspberry Pi.









Vault . Vault. , , , . HashiCorp:







Vault, : , , . .







, vault server -dev



(-dev



, , ):







$ vault server -dev
==> Vault server configuration:

        Api Address: http://127.0.0.1:8200
                Cgo: disabled
    Cluster Address: https://127.0.0.1:8201
        Listener 1: tcp (addr: "127.0.0.1:8200", cluster address: "127.0.0.1:8201", max_request_duration: "1m30s", max_request_size: "33554432", tls: "disabled")
        Log Level: info
            Mlock: supported: false, enabled: false
            Storage: inmem
            Version: Vault v1.2.1

WARNING! dev mode is enabled! In this mode, Vault runs entirely in-memory
and starts unsealed with a single unseal key. The root token is already
authenticated to the CLI, so you can immediately begin using Vault.

You may need to set the following environment variable:

    $ export VAULT_ADDR='http://127.0.0.1:8200'

The unseal key and root token are displayed below in case you want to seal/unseal the Vault or re-authenticate.

Unseal Key: p8MumXfy57bh2T1FxdvZSmHhxqr7aQAByPpfE4PLujk=
Root Token: s.aSQmpEYEi5MKelf5TDLPC6r9

Development mode should NOT be used in production installations!

==> Vault server started! Log data will stream in below:
      
      





, , . , ( , ). , , VAULT_ADDR, Vault , .







Unseal Key Root Token. , Root Token , / Vault Vault .









Vault . , Vault , , , . Vault (unsealed). , , (Unseal Key), (unseal) . Vault , .







Vault . , - :







$ vault operator init

Unseal Key 1: 4jYbl2CBIv6SpkKj6Hos9iD32k5RfGkLzlosrrq/JgOm
Unseal Key 2: B05G1DRtfYckFV5BbdBvXq0wkK5HFqB9g2jcDmNfTQiS
Unseal Key 3: Arig0N9rN9ezkTRo7qTB7gsIZDaonOcc53EHo83F5chA
Unseal Key 4: 0cZE0C/gEk3YHaKjIWxhyyfs8REhqkRW/CSXTnmTilv+
Unseal Key 5: fYhZOseRgzxmJCmIqUdxEm9C3jB5Q27AowER9w4FC2Ck

Initial Root Token: s.KkNJYWF5g0pomcCLEmDdOVCW

Vault initialized with 5 key shares and a key threshold of 3. Please securely distribute the key shares printed above. When the Vault is re-sealed, restarted, or stopped, you must supply at least 3 of these keys to unseal it before it can start servicing requests.

Vault does not store the generated master key. Without at least 3 keys to reconstruct the master key, Vault will remain permanently sealed!

It is possible to generate new unseal keys, provided you have a quorum of existing unseal keys shares. See "vault operator rekey" for more information.
      
      







, , , . vault login



, . Root Token (. ). , , :







$ vault login
Token (will be hidden):
Success! You are now authenticated. The token information displayed below is already stored in the token helper. You do NOT need to run "vault login" again. Future Vault requests will automatically use this token.

Key                 Value
---                 -----
token               s.aSQmpEYEi5MKelf5TDLPC6r9
token_accessor      MaJhao2R54EdV9fDq7sL11d4
token_duration      ∞
token_renewable     false
token_policies      ["root"]
identity_policies   []
policies            ["root"] 
      
      







Vault HashiCorp , Vault . vault kv put



:







$ vault kv put secret/foo bar=baz
Key             Value
---             -----
created_time    2019-08-09T16:43:10.604124Z
deletion_time   n/a
destroyed       false
version         1
      
      





, foo



secret



bar=baz



, . created_time



, deletion_time



destroyed



, version



, , .







, , , :







$ vault kv put secret/foo bat=ball
Key             Value
---             -----
created_time    2019-08-09T16:43:32.638788Z
deletion_time   n/a
destroyed       false
version         2
      
      





, ? , , , .









$ vault kv list secret
Keys
----
foo
      
      





β€” . β€” . , :







, , , . , vault kv get



:







$ vault kv get secret/foo
====== Metadata ======
Key             Value
---             -----
created_time    2019-08-09T16:43:32.638788Z
deletion_time   n/a
destroyed       false
version         2

=== Data ===
Key Value
--- -----
bat ball
      
      





Vault , , -version



:







$ vault kv get -version=1 secret/foo
====== Metadata ======
Key             Value
---             -----
created_time    2019-08-09T16:43:10.604124Z
deletion_time   n/a
destroyed       false   
version         1

=== Data ===
Key Value
--- -----
bar baz
      
      





, , , (release) , .









, ( ). , , «» , : delete



destroy



. , foo



:







$ vault kv delete -versions=1 secret/foo
Success! Data deleted (if it existed) at: secret/foo
      
      





(deleted



) GET, :







$ vault kv get -version=1 secret/foo
====== Metadata ======
Key             Value
---             -----
created_time    2019-08-09T16:43:10.604124Z
deletion_time   2019-08-09T16:45:39.664577Z
destroyed       false
version         1
      
      





, destroy:







$ vault kv destroy -versions=1 secret/foo
Success! Data written to: secret/destroy/foo
      
      





, , destroy , :







$ vault kv get -version=1 secret/foo
====== Metadata ======
Key             Value
---             -----
created_time    2019-08-09T16:43:10.604124Z
deletion_time   2019-08-09T16:45:39.664577Z
destroyed       true
version         1
      
      





HashiCorp Vault



Vault β€” , β€” , . Vault , , Vault .







Secrets engines



$ vault secrets enable database
Success! Enabled the database secrets engine at: database/
      
      





Vault ( , kv



). β€” . , - . , (database



) MySQL MariaDB, root .









$ vault auth enable github
Success! Enabled github auth method at: github/
      
      





Vault . GitHub, Vault , GitHub β€” GitHub β€” . , LDAP Okta, Vault.









$ vault write auth/userpass/users/test policies="dev-readonly,logs"
      
      





. GitHub , . Vault RBAC, CRUD- . .







Vault



Vault, . , Vault, . , API, , Vault, Vault HashiCorp – .







In addition to securing your applications and infrastructure, you need a rapid incident response plan. Check out our free guide, From Reactive to Proactive: 6 Ways to Transform Your Monitoring and Incident Response, to create highly collaborative, transparent incident management workflows.








All Articles