Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vault: support k8s auth method #1580

Merged
merged 5 commits into from
Jun 8, 2022
Merged

Conversation

Kryvchun
Copy link
Contributor

Issue: hashicorp/envconsul#274

This PR implements support of K8S auth method:

  1. Add new fields to CreateConsulClientInput:
    K8SAuthRoleName            string
    K8SServiceAccountMountPath string
    K8SServiceAccountToken     string
    K8SServiceMountPath        string
  2. If a token is set in the function CreateVaultClient then ignore k8s auth method.
  3. Execute client.Auth().Login(...) with vault.auth.KubernetesAuth auth method,
  4. set the token.

This can't be integration tested, so I mocked one endpoint (/v1/auth/kubernetes/login) with an *httptest.Server, all other calls are proxied to Vault

@Kryvchun Kryvchun requested a review from a team May 12, 2022 08:37
@hashicorp-cla
Copy link

hashicorp-cla commented May 12, 2022

CLA assistant check
All committers have signed the CLA.

@Kryvchun Kryvchun force-pushed the feat/k8s-vault-auth branch from 6928d41 to aa8c510 Compare May 12, 2022 08:43
Kryvchun added 2 commits May 12, 2022 11:46
Cosmetic change

Improve docs
@Kryvchun Kryvchun force-pushed the feat/k8s-vault-auth branch from f9ad2fa to 54cf5e7 Compare May 12, 2022 08:59
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Kryvchun Kryvchun force-pushed the feat/k8s-vault-auth branch from 9ff66bf to 2d1f6fb Compare May 13, 2022 07:51
Define k8s params in main config

Define mapstructure tags
Copy link

@yellowmegaman yellowmegaman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
tested with auth role

config/vault.go Outdated Show resolved Hide resolved
config/vault.go Outdated Show resolved Hide resolved
dependency/client_set.go Outdated Show resolved Hide resolved
@Kryvchun
Copy link
Contributor Author

Kryvchun commented Jun 6, 2022

@eikenb Hi. Is everything okay with the implementation? Or do I need to fix something?

@eikenb
Copy link
Contributor

eikenb commented Jun 8, 2022

Hey @Kryvchun, sorry for the silence. I was planning on working on this when I got to working on Envconsul which took a little longer than expected but I did start working on a new release for it this week and I plan on reviewing this as part of that (so I can loop it into the Envconsul release).

@eikenb eikenb added the hashicat-update-required Changes that need to be ported to hashicat label Jun 8, 2022
Copy link
Contributor

@eikenb eikenb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good and checks all the required boxes, thanks @Kryvchun!

Thanks also for the help reviewing and testing @ygworldr, @yellowmegaman!!! Super appreciate that as I have limited time to validate the k8ts side of things.

@eikenb eikenb merged commit c106eba into hashicorp:main Jun 8, 2022
@eikenb eikenb added this to the v0.30.0 milestone Jun 8, 2022
@Kryvchun
Copy link
Contributor Author

Kryvchun commented Sep 8, 2022

How to use this with consul-template ? Is there any doc ?

You can configure (inside vault {} config):

  • k8s_auth_role_name: string (role name, it enables this auth method)
  • k8s_service_mount_path: string (a part of k8s login path, auth/:value/login, default: kubernetes)

Token:

  • k8s_service_account_token_path: string (path to secret, default: /run/secrets/kubernetes.io/serviceaccount/token)
    Or
  • k8s_service_account_token: string (value of an account token for k8s auth method).

There is no docs for this in consul-template, only in envconsul.

@solomon2201
Copy link

Good day. @Kryvchun and @eikenb I have a problem with your update. Previously we are using VaultClient in a init_container for take a token from vault and consul-template for took a secret and rotate access to vault. But we need connect to vault always because we are have dynamic env.

I made update to your commit. Its working well, consul take all secret from vault but we are lose token in 1h. I turned on the trace log for debug it. consul-template don`t rotate the token.
I understand, its just working with variable only ( but for token we need use VaultClient)
#vault_agent_token_file = "/tmp/vault/agent/token"
Can you add rotating token in consul-template for your new option?? We are really need that.

its update block code

vault {
  - vault_agent_token_file = "/var/run/secrets/vaultproject.io/.vault-token"  
 + k8s_service_account_token_path = "/var/run/secrets/kubernetes.io/serviceaccount/token"
 + k8s_service_mount_path =  "kubernetes"
 + k8s_auth_role_name = "app"
  unwrap_token           = false
  renew_token            = true
  retry {
    enabled     = true
    attempts    = 10
    attempts    = 3
    backoff     = "250ms"
    max_backoff = "1m"
  }

and vault accessor. You can see its not rotating.

---                 -----
accessor            bW8hQQQQaKSVxIR
creation_time       1674067534
creation_ttl        1h
display_name        kubernetes-app-app
entity_id           qqqqq-bcaa-55df-43ba-qqqqqqq
expire_time         2023-01-18T19:45:34.181388978Z
explicit_max_ttl    0s
id                  n/a
issue_time          2023-01-18T18:45:34.181398288Z
meta                map[role:app service_account_name:app service_account_namespace:app service_account_secret_name: service_account_uid:qqqq-qqq-490a-qqqqq-76d2863696d1]
num_uses            0
orphan              true
path                auth/kubernetes/login
policies            [app-database-creds-kubernetes app-secrets-rw default]
renewable           true
ttl                 29s
type                service

Thanks for your time

@Kryvchun Kryvchun deleted the feat/k8s-vault-auth branch May 15, 2023 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement hashicat-update-required Changes that need to be ported to hashicat
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants