Init Consul Client - Using k8s Service account
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 30.1k
- Forks
- 4.6k
- Avg merge
- 1d 18h
- Merged PRs (30d)
- 39
Description
#### Feature Description
I want to set up a Consul client by authenticating with my Kubernetes service account.
I don't understand at all why the `ACL().Login()` method doesn't update the Vault client with the Secret, and why I have to declare a new one using the newly created secret.
I suggest using the same authentication method as your other tools, including Vault.
#### Use Case(s)
```golang
package client
import (
"os"
"github.com/hashicorp/consul/api"
)
func newConsulClient() (*api.Client, error) {
consulConfig := newConfig()
client, err := api.NewClient(consulConfig)
if err != nil {
return nil, err
}
bearerToken, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token")
if err != nil {
return nil, err
}
auth := &api.ACLLoginParams{
AuthMethod: "k8s",
BearerToken: string(bearerToken),
}
aclToken, _, err := client.ACL().Login(auth, nil)
if err != nil {
return nil, err
}
newConfig := newConfig()
newConfig.Token = aclToken.SecretID
client, err = api.NewClient(newConfig)
if err != nil {
return nil, err
}
return client, nil
}
```
Where for vault, we just need to do this:
```go
package client
import (
"context"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/api/auth/kubernetes"
)
func provideK8sAuth(ctx context.Context, client *api.Client, roleName string) error {
k8sAuth, err := kubernetes.NewKubernetesAuth(roleName)
if err != nil {
return err
}
authInfo, err := client.Auth().Login(ctx, k8sAuth)
if err != nil {
return err
}
return nil
}
```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the Go entry points shown in the issue: api.NewClient, ACL().Login(), and ACLLoginParams, then compare the requested flow with the Vault Kubernetes authentication example. Determine how a successful login should update or reuse the client configuration. Done means a Kubernetes service-account login authenticates the Consul client without requiring callers to construct a second client.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, kubernetes
- Domain
- authentication, backend-api-design
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100