Something went wrong. Try again.
Hands pods short-lived GitHub, Gitea, and atproto credentials for their ServiceAccount tokens
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113// Package githubapp keeps each GitHubApp's Ready condition, which says// whether GitHub accepts the App's key, and its slug.//// Each pass signs a statement with the key and asks GitHub who the App// is. A pass runs when the GitHubApp changes and again on a slow clock,// because a key that someone deletes on GitHub, or a Secret that// changes, is nothing that the watch of GitHubApps sees.package githubapp
import ( "context" "errors" "reflect" "slices"
"github.com/liken-sh/liken/kubernetes/conditions" "github.com/liken-sh/liken/kubernetes/events"
"tangled.org/guid.foo/lanyard/api/v1alpha1" "tangled.org/guid.foo/lanyard/cluster" "tangled.org/guid.foo/lanyard/github")
// Reconciler runs one pass over one GitHubApp.type Reconciler struct { // namespace is lanyard's own, where each App's private key is. namespace string cluster *cluster.Cluster github *github.Client events *events.Recorder}
// NewReconciler answers a Reconciler that reads keys from namespace and// posts each transition through recorder.func NewReconciler(namespace string, c *cluster.Cluster, g *github.Client, recorder *events.Recorder) *Reconciler { return &Reconciler{namespace: namespace, cluster: c, github: g, events: recorder}}
// Reconcile brings one GitHubApp's status up to date. A pass that// cannot reach GitHub fails and leaves the condition as it was, because// an outage says nothing about the key.func (r *Reconciler) Reconcile(ctx context.Context, _, name string) error { app, err := r.cluster.GitHubApp(ctx, name) if errors.Is(err, cluster.ErrNotFound) { return nil } if err != nil { return err } ready, slug, err := r.judge(ctx, app) if err != nil { return err } ready.Type, ready.ObservedGeneration = v1alpha1.ConditionReady, app.Metadata.Generation judged := app.Metadata.Generation transitioned := false written, err := r.cluster.SettleGitHubAppStatus(ctx, app, func(a *v1alpha1.GitHubApp) bool { if a.Metadata.Generation != judged { // The fresh copy carries a newer spec, which this pass did // not read. The pass that its watch event wakes judges it. return false } before := a.Status before.Conditions = slices.Clone(before.Conditions) transitioned = conditions.Set(&a.Status.Conditions, ready) a.Status.ObservedGeneration = judged if slug != "" { a.Status.Slug = slug } return !reflect.DeepEqual(before, a.Status) }) if written && transitioned { r.events.Transition(cluster.GitHubAppReference(app), ready, conditions.False) } return err}
// judge answers the Ready condition's status, reason, and message, and// the slug when GitHub accepted the key.func (r *Reconciler) judge(ctx context.Context, app *v1alpha1.GitHubApp) (conditions.Condition, string, error) { no := func(reason, message string) (conditions.Condition, string, error) { return conditions.Condition{Status: conditions.False, Reason: reason, Message: message}, "", nil } ref := app.Spec.PrivateKey.SecretKeyRef where := "the key " + ref.Key + " of the Secret " + r.namespace + "/" + ref.Name data, err := r.cluster.Secret(ctx, r.namespace, ref.Name) if errors.Is(err, cluster.ErrNotFound) { return no(v1alpha1.ReasonKeyMissing, "There is no Secret "+r.namespace+"/"+ref.Name+".") } if err != nil { return conditions.Condition{}, "", err } text, ok := data[ref.Key] if !ok { return no(v1alpha1.ReasonKeyMissing, "The Secret "+r.namespace+"/"+ref.Name+" has no key "+ref.Key+".") } key, err := github.ParseKey(text) if err != nil { return no(v1alpha1.ReasonKeyInvalid, "GitHub Apps sign with an RSA private key in PEM, and "+where+" is not one: "+err.Error()+".") } slug, err := r.github.Slug(ctx, github.App{ClientID: app.Spec.ClientID, Key: key}) if errors.Is(err, github.ErrUnknownApp) { return no(v1alpha1.ReasonAppUnknown, "GitHub knows no App with the client ID "+app.Spec.ClientID+".") } if errors.Is(err, github.ErrRefused) { return no(v1alpha1.ReasonRefused, "GitHub refused a statement that the client ID "+app.Spec.ClientID+" signed with "+where+".") } if err != nil { return conditions.Condition{}, "", err } return conditions.Condition{Status: conditions.True, Reason: v1alpha1.ReasonAccepted, Message: "GitHub accepts the key of the App " + slug + "."}, slug, nil}