akuity / kargo

Application lifecycle orchestration
https://kargo.akuity.io/
Apache License 2.0
1.38k stars 114 forks source link

Convenience for generating API keys for automation #2107

Open jessesuen opened 3 weeks ago

jessesuen commented 3 weeks ago

Proposed Feature

Kargo should have a convenience for creating long-lived "API keys" for interacting with Kargo API. This should be manageable from the UI either as a separate tab, or incorporated into the existing Roles tab.

Motivation

It will be desired for some automated systems to interface with Kargo API. For example, I would like for my CI system to refresh a Warehouse as soon as it pushed an image is pushed so that Kargo knows about it right away.

Suggested Implementation

"API Keys" would really just be Kubernetes ServiceAccount Bearer Tokens under the covers. The tokens would be bound to Kargo Roles (which are just K8s ServiceAccounts). This feature is simply a UI & API convenience for creating a ServiceAccount token Secret:

https://kubernetes.io/docs/concepts/configuration/secret/#serviceaccount-token-secrets

apiVersion: v1
kind: Secret
metadata:
  name: secret-sa-sample
  annotations:
    kubernetes.io/service-account.name: "sa-name"
type: kubernetes.io/service-account-token

And then returning data.token to the user.

Other considerations

krancour commented 3 weeks ago

@jessesuen if the token we issue is a k8s bearer token, then we're talking about "client auth" to the API Server?

https://github.com/akuity/kargo/blob/08b394f6a060e20b90e29499a8717cf9094c8a5c/internal/api/option/auth.go#L397-L406

One possible issue with this is that we don't really get any information about the principal in that case, which makes audit logs incomplete, but we may be able to figure something out.

jessesuen commented 3 weeks ago

if the token we issue is a k8s bearer token, then we're talking about "client auth" to the API Server?

That's correct. Yes, service account namespace/name is in the JWT and is available to the webhook, and API server, so I think we have sufficient information to enrich the audit events.

jessesuen commented 3 weeks ago

Another requirement: since I intend to use this from my CI system, kargo CLI should accept an env var to use this token.

krancour commented 3 weeks ago

That's correct. Yes, service account namespace/name is in the JWT and is available to the webhook, and API server, so I think we have sufficient information to enrich the audit events.

I should have been more clear. I knew the info would be there. The thing that's a little bit of a problem is unless we have a way to validate the token ourselves, we can't trust its claims. But I still think we'll be able to figure something out.