Skip to content

Commit

Permalink
Get secret for SM http client
Browse files Browse the repository at this point in the history
  • Loading branch information
szwedm committed May 9, 2024
1 parent 71a83d0 commit 88b58f5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
15 changes: 15 additions & 0 deletions internal/cluster-object/secret_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,18 @@ func (p *SecretProvider) secretExistsInList(secret *corev1.Secret, secrets *core
}
return false
}

func (p *SecretProvider) GetByNameAndNamespace(ctx context.Context, name, namespace string) (*corev1.Secret, error) {
p.logger.Info(fmt.Sprintf("fetching \"%s\" secret in \"%s\" namespace", name, namespace))
secret := &corev1.Secret{}
if err := p.Get(ctx, client.ObjectKey{Namespace: namespace, Name: name}, secret); err != nil {
if k8serrors.IsNotFound(err) {
p.logger.Warn(fmt.Sprintf("secret \"%s\" not found in \"%s\" namespace", name, namespace))
return nil, nil
}
p.logger.Error(fmt.Sprintf("failed to fetch \"%s\" secret in \"%s\" namespace", name, namespace), "error", err)
return nil, err
}

return secret, nil
}
18 changes: 18 additions & 0 deletions internal/service-manager/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package servicemanager
import (
"context"
"log/slog"
"net/http"

clusterobject "github.com/kyma-project/btp-manager/internal/cluster-object"
"golang.org/x/oauth2/clientcredentials"
corev1 "k8s.io/api/core/v1"
)

Expand All @@ -14,6 +16,7 @@ type Client struct {
ctx context.Context
logger *slog.Logger
secretProvider clusterobject.NamespacedProvider[*corev1.Secret]
httpClient *http.Client
}

func NewClient(ctx context.Context, logger *slog.Logger, secretProvider clusterobject.NamespacedProvider[*corev1.Secret]) *Client {
Expand All @@ -23,3 +26,18 @@ func NewClient(ctx context.Context, logger *slog.Logger, secretProvider clustero
secretProvider: secretProvider,
}
}

func (c *Client) getHttpClientForGivenSecret(ctx context.Context, secretName, secretNamespace string) (*http.Client, error) {
secret, err := c.secretProvider.GetByNameAndNamespace(ctx, secretName, secretNamespace)
if err != nil {
return nil, err
}

cfg := clientcredentials.Config{
ClientID: string(secret.Data["clientid"]),
ClientSecret: string(secret.Data["clientsecret"]),
TokenURL: string(secret.Data["tokenurl"]),
}

return cfg.Client(ctx), nil
}

0 comments on commit 88b58f5

Please sign in to comment.