Skip to content

Commit

Permalink
modified GetRoundTripper function
Browse files Browse the repository at this point in the history
Signed-off-by: Bharadwajshivam28 <[email protected]>
  • Loading branch information
Bharadwajshivam28 committed Oct 9, 2024
1 parent e109150 commit f4c29ac
Showing 1 changed file with 14 additions and 113 deletions.
127 changes: 14 additions & 113 deletions metrics-operator/controllers/common/providers/prometheus/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/tls"
"errors"
"net/http"
"sync"

metricsapi "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1"
promapi "github.com/prometheus/client_golang/api"
Expand All @@ -15,112 +14,24 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
secretKeyUserName = "user"
secretKeyPassword = "password"
secretKeyCAFile = "caFile"
secretKeyCertFile = "certFile"
secretKeyKeyFile = "keyFile"
)
const secretKeyUserName = "user"
const secretKeyPassword = "password"

var (
ErrSecretKeyRefNotDefined = errors.New("the SecretKeyRef property with the Prometheus API Key is missing")
ErrInvalidSecretFormat = errors.New("secret key does not contain required fields")
)
var ErrSecretKeyRefNotDefined = errors.New("the SecretKeyRef property with the Prometheus API Key is missing")
var ErrInvalidSecretFormat = errors.New("secret key does not contain user and password")

type SecretData struct {
User string `json:"user"`
Password config.Secret `json:"password"`
CAFile string `json:"caFile"`
CertFile string `json:"certFile"`
KeyFile string `json:"keyFile"`
}

// IRoundTripper interface defines the method to get the RoundTripper
type IRoundTripper interface {
GetRoundTripper(context.Context, metricsapi.KeptnMetricsProvider, client.Client) (http.RoundTripper, error)
}

type RoundTripperRetriever struct{}

type TLSRoundTripperSettings struct {
CAFile string
CertFile string
KeyFile string
ServerName string
InsecureSkipVerify bool
}

type tlsRoundTripper struct {
settings TLSRoundTripperSettings
newRT func(*tls.Config) (http.RoundTripper, error)
mtx sync.RWMutex
rt http.RoundTripper
tlsConfig *tls.Config
}

func NewTLSRoundTripper(settings TLSRoundTripperSettings) (*tlsRoundTripper, error) {
return &tlsRoundTripper{
settings: settings,
newRT: func(config *tls.Config) (http.RoundTripper, error) {
return &http.Transport{
TLSClientConfig: config,
}, nil
},
}, nil
}

func (t *tlsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
t.mtx.RLock()
rt := t.rt
t.mtx.RUnlock()

if rt == nil {
t.mtx.Lock()
defer t.mtx.Unlock()
if t.rt == nil {
var err error
t.tlsConfig, err = newTLSConfig(&t.settings)
if err != nil {
return nil, err
}
rt, err = t.newRT(t.tlsConfig)
if err != nil {
return nil, err
}
t.rt = rt
}
rt = t.rt
}

return rt.RoundTrip(req)
}

func newTLSConfig(settings *TLSRoundTripperSettings) (*tls.Config, error) {
tlsConfig := &tls.Config{
InsecureSkipVerify: settings.InsecureSkipVerify,
ServerName: settings.ServerName,
}

if settings.CAFile != "" {
caConfig := &config.TLSConfig{
CAFile: settings.CAFile,
}
ca, err := config.NewTLSConfig(caConfig)
if err != nil {
return nil, err
}
tlsConfig.RootCAs = ca.RootCAs
}

if settings.CertFile != "" && settings.KeyFile != "" {
cert, err := tls.LoadX509KeyPair(settings.CertFile, settings.KeyFile)
if err != nil {
return nil, err
}
tlsConfig.Certificates = []tls.Certificate{cert}
}

return tlsConfig, nil
// RoundTripperRetriever implements the IRoundTripper interface
type RoundTripperRetriever struct {
}

func (r RoundTripperRetriever) GetRoundTripper(ctx context.Context, provider metricsapi.KeptnMetricsProvider, k8sClient client.Client) (http.RoundTripper, error) {
Expand All @@ -132,19 +43,13 @@ func (r RoundTripperRetriever) GetRoundTripper(ctx context.Context, provider met
return nil, err
}

settings := TLSRoundTripperSettings{
CAFile: secret.CAFile,
CertFile: secret.CertFile,
KeyFile: secret.KeyFile,
ServerName: provider.Spec.TargetServer,
}

tlsRT, err := NewTLSRoundTripper(settings)
if err != nil {
return nil, err
transport := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: provider.Spec.InsecureSkipTlsVerify,
},
}

return config.NewBasicAuthRoundTripper(secret.User, secret.Password, "", "", tlsRT), nil
return config.NewBasicAuthRoundTripper(secret.User, secret.Password, "", "", transport), nil
}

func getPrometheusSecret(ctx context.Context, provider metricsapi.KeptnMetricsProvider, k8sClient client.Client) (*SecretData, error) {
Expand All @@ -159,16 +64,12 @@ func getPrometheusSecret(ctx context.Context, provider metricsapi.KeptnMetricsPr

var secretData SecretData
user, ok := secret.Data[secretKeyUserName]
pw, pwOk := secret.Data[secretKeyPassword]
if !ok || !pwOk {
pw, yes := secret.Data[secretKeyPassword]
if !ok || !yes {
return nil, ErrInvalidSecretFormat
}

secretData.User = string(user)
secretData.Password = config.Secret(pw)
secretData.CAFile = string(secret.Data[secretKeyCAFile])
secretData.CertFile = string(secret.Data[secretKeyCertFile])
secretData.KeyFile = string(secret.Data[secretKeyKeyFile])

return &secretData, nil
}

0 comments on commit f4c29ac

Please sign in to comment.