| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- /*
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package conjur
- import (
- "context"
- "crypto/tls"
- "crypto/x509"
- "fmt"
- "net/http"
- "time"
- "github.com/cyberark/conjur-api-go/conjurapi"
- authenticationv1 "k8s.io/api/authentication/v1"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
- esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
- "github.com/external-secrets/external-secrets/pkg/utils/resolvers"
- )
- const JwtLifespan = 600 // 10 minutes
- // getJWTToken retrieves a JWT token either using the TokenRequest API for a specified service account, or from a jwt stored in a k8s secret.
- func (p *Client) getJWTToken(ctx context.Context, conjurJWTConfig *esv1beta1.ConjurJWT) (string, error) {
- if conjurJWTConfig.ServiceAccountRef != nil {
- // Should work for Kubernetes >=v1.22: fetch token via TokenRequest API
- jwtToken, err := p.getJwtFromServiceAccountTokenRequest(ctx, *conjurJWTConfig.ServiceAccountRef, nil, JwtLifespan)
- if err != nil {
- return "", err
- }
- return jwtToken, nil
- } else if conjurJWTConfig.SecretRef != nil {
- tokenRef := conjurJWTConfig.SecretRef
- if tokenRef.Key == "" {
- tokenRef = conjurJWTConfig.SecretRef.DeepCopy()
- tokenRef.Key = "token"
- }
- jwtToken, err := resolvers.SecretKeyRef(
- ctx,
- p.kube,
- p.StoreKind,
- p.namespace,
- tokenRef)
- if err != nil {
- return "", err
- }
- return jwtToken, nil
- }
- return "", fmt.Errorf("missing ServiceAccountRef or SecretRef")
- }
- // getJwtFromServiceAccountTokenRequest uses the TokenRequest API to get a JWT token for the given service account.
- func (p *Client) getJwtFromServiceAccountTokenRequest(ctx context.Context, serviceAccountRef esmeta.ServiceAccountSelector, additionalAud []string, expirationSeconds int64) (string, error) {
- audiences := serviceAccountRef.Audiences
- if len(additionalAud) > 0 {
- audiences = append(audiences, additionalAud...)
- }
- tokenRequest := &authenticationv1.TokenRequest{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: p.namespace,
- },
- Spec: authenticationv1.TokenRequestSpec{
- Audiences: audiences,
- ExpirationSeconds: &expirationSeconds,
- },
- }
- if (p.StoreKind == esv1beta1.ClusterSecretStoreKind) &&
- (serviceAccountRef.Namespace != nil) {
- tokenRequest.Namespace = *serviceAccountRef.Namespace
- }
- tokenResponse, err := p.corev1.ServiceAccounts(tokenRequest.Namespace).CreateToken(ctx, serviceAccountRef.Name, tokenRequest, metav1.CreateOptions{})
- if err != nil {
- return "", fmt.Errorf(errGetKubeSATokenRequest, serviceAccountRef.Name, err)
- }
- return tokenResponse.Status.Token, nil
- }
- // newClientFromJwt creates a new Conjur client using the given JWT Auth Config.
- func (p *Client) newClientFromJwt(ctx context.Context, config conjurapi.Config, jwtAuth *esv1beta1.ConjurJWT) (SecretsClient, error) {
- jwtToken, getJWTError := p.getJWTToken(ctx, jwtAuth)
- if getJWTError != nil {
- return nil, getJWTError
- }
- client, clientError := p.clientAPI.NewClientFromJWT(config, jwtToken, jwtAuth.ServiceID, jwtAuth.HostID)
- if clientError != nil {
- return nil, clientError
- }
- return client, nil
- }
- // newHTTPSClient creates a new HTTPS client with the given cert.
- func newHTTPSClient(cert []byte) (*http.Client, error) {
- pool := x509.NewCertPool()
- ok := pool.AppendCertsFromPEM(cert)
- if !ok {
- return nil, fmt.Errorf("can't append Conjur SSL cert")
- }
- tr := &http.Transport{
- TLSClientConfig: &tls.Config{RootCAs: pool, MinVersion: tls.VersionTLS12},
- }
- return &http.Client{Transport: tr, Timeout: time.Second * 10}, nil
- }
|