Skip to content

fix: call TokenRequest API when service account token secret is missing #3377

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

Merged
merged 4 commits into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion pkg/lib/scoped/token_retriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
"github.com/sirupsen/logrus"
authv1 "k8s.io/api/authentication/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -36,7 +37,14 @@ func (r *BearerTokenRetriever) Retrieve(reference *corev1.ObjectReference) (toke
}

if secret == nil {
err = fmt.Errorf("the service account does not have any API secret sa=%s/%s", sa.GetNamespace(), sa.GetName())
token, err = requestSAToken(r.kubeclient, sa)
if err != nil {
err = fmt.Errorf("creating service account token from TokenRequest API for sa=%s/%s; %v",
sa.GetNamespace(),
sa.GetName(),
err,
)
}
return
}

Expand All @@ -48,6 +56,20 @@ func (r *BearerTokenRetriever) Retrieve(reference *corev1.ObjectReference) (toke
return
}

// requestSAToken requests for a service account token from the Kubernetes API server whenever the Operator
// Lifecycle manager is unable to find a service account token secret
func requestSAToken(kubeclient operatorclient.ClientInterface, sa *corev1.ServiceAccount) (string, error) {
req := new(authv1.TokenRequest)
req, err := kubeclient.KubernetesInterface().
CoreV1().ServiceAccounts(sa.GetNamespace()).
CreateToken(context.Background(), sa.GetName(), req, metav1.CreateOptions{})
if err != nil {
return "", err
}

return req.Status.Token, nil
}

func getAPISecret(logger logrus.FieldLogger, kubeclient operatorclient.ClientInterface, sa *corev1.ServiceAccount) (APISecret *corev1.Secret, err error) {
seList, err := kubeclient.KubernetesInterface().CoreV1().Secrets(sa.GetNamespace()).List(context.TODO(), metav1.ListOptions{})
if err != nil {
Expand Down