-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(metrics-operator): add basicauth to prometheus provider (#2154)
Signed-off-by: realanna <[email protected]> Signed-off-by: RealAnna <[email protected]> Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Signed-off-by: Florian Bacher <[email protected]> Co-authored-by: odubajDT <[email protected]> Co-authored-by: Florian Bacher <[email protected]> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
- Loading branch information
1 parent
d7fce2a
commit bab605e
Showing
14 changed files
with
518 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
metrics-operator/controllers/common/providers/prometheus/common.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package prometheus | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
|
||
metricsapi "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha3" | ||
promapi "github.com/prometheus/client_golang/api" | ||
"github.com/prometheus/common/config" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
const secretKeyUserName = "user" | ||
const secretKeyPassword = "password" | ||
|
||
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"` | ||
} | ||
|
||
//go:generate moq -pkg fake -skip-ensure -out ./fake/roundtripper_mock.go . IRoundTripper | ||
type IRoundTripper interface { | ||
GetRoundTripper(context.Context, metricsapi.KeptnMetricsProvider, client.Client) (http.RoundTripper, error) | ||
} | ||
|
||
type RoundTripperRetriever struct { | ||
} | ||
|
||
func (r RoundTripperRetriever) GetRoundTripper(ctx context.Context, provider metricsapi.KeptnMetricsProvider, k8sClient client.Client) (http.RoundTripper, error) { | ||
secret, err := getPrometheusSecret(ctx, provider, k8sClient) | ||
if err != nil { | ||
if errors.Is(err, ErrSecretKeyRefNotDefined) { | ||
return promapi.DefaultRoundTripper, nil | ||
} | ||
return nil, err | ||
} | ||
return config.NewBasicAuthRoundTripper(secret.User, secret.Password, "", promapi.DefaultRoundTripper), nil | ||
} | ||
|
||
func getPrometheusSecret(ctx context.Context, provider metricsapi.KeptnMetricsProvider, k8sClient client.Client) (*SecretData, error) { | ||
if !provider.HasSecretDefined() { | ||
return nil, ErrSecretKeyRefNotDefined | ||
} | ||
secret := &corev1.Secret{} | ||
if err := k8sClient.Get(ctx, types.NamespacedName{Name: provider.Spec.SecretKeyRef.Name, Namespace: provider.Namespace}, secret); err != nil { | ||
return nil, err | ||
} | ||
|
||
var secretData SecretData | ||
user, ok := secret.Data[secretKeyUserName] | ||
pw, yes := secret.Data[secretKeyPassword] | ||
if !ok || !yes { | ||
return nil, ErrInvalidSecretFormat | ||
} | ||
secretData.User = string(user) | ||
secretData.Password = config.Secret(pw) | ||
return &secretData, nil | ||
} |
193 changes: 193 additions & 0 deletions
193
metrics-operator/controllers/common/providers/prometheus/common_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
package prometheus | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
|
||
metricsapi "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha3" | ||
"github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" | ||
promapi "github.com/prometheus/client_golang/api" | ||
"github.com/prometheus/common/config" | ||
"github.com/stretchr/testify/require" | ||
corev1 "k8s.io/api/core/v1" | ||
v1 "k8s.io/api/core/v1" | ||
k8serrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
const prometheusPayload = "test" | ||
|
||
func TestGetSecret_NoKeyDefined(t *testing.T) { | ||
|
||
fakeClient := fake.NewClient() | ||
|
||
p := metricsapi.KeptnMetricsProvider{ | ||
Spec: metricsapi.KeptnMetricsProviderSpec{ | ||
TargetServer: "svr.URL", | ||
}, | ||
} | ||
r1, e := getPrometheusSecret(context.TODO(), p, fakeClient) | ||
require.NotNil(t, e) | ||
require.ErrorIs(t, ErrSecretKeyRefNotDefined, e) | ||
require.Empty(t, r1) | ||
|
||
} | ||
|
||
func TestGetSecret_NoSecretDefined(t *testing.T) { | ||
|
||
secretName := "testSecret" | ||
|
||
fakeClient := fake.NewClient() | ||
|
||
b := true | ||
p := metricsapi.KeptnMetricsProvider{ | ||
Spec: metricsapi.KeptnMetricsProviderSpec{ | ||
SecretKeyRef: v1.SecretKeySelector{ | ||
Key: "apiKey", | ||
LocalObjectReference: v1.LocalObjectReference{ | ||
Name: secretName, | ||
}, | ||
Optional: &b, | ||
}, | ||
TargetServer: "svr", | ||
}, | ||
} | ||
r1, e := getPrometheusSecret(context.TODO(), p, fakeClient) | ||
require.NotNil(t, e) | ||
t.Log(e.Error()) | ||
require.True(t, k8serrors.IsNotFound(e)) | ||
require.Empty(t, r1) | ||
|
||
} | ||
|
||
func TestGetSecret_HappyPath(t *testing.T) { | ||
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
_, err := w.Write([]byte(prometheusPayload)) | ||
require.Nil(t, err) | ||
})) | ||
defer svr.Close() | ||
|
||
secretName := "mySecret" | ||
apiToken := &corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: secretName, | ||
Namespace: "default", | ||
}, | ||
Data: map[string][]byte{ | ||
"user": []byte("myuser"), | ||
"password": []byte("mytoken"), | ||
}, | ||
} | ||
fakeClient := fake.NewClient(apiToken) | ||
|
||
p := metricsapi.KeptnMetricsProvider{ | ||
ObjectMeta: metav1.ObjectMeta{Namespace: "default"}, | ||
Spec: metricsapi.KeptnMetricsProviderSpec{ | ||
SecretKeyRef: v1.SecretKeySelector{ | ||
Key: "login", | ||
LocalObjectReference: v1.LocalObjectReference{ | ||
Name: secretName, | ||
}, | ||
}, | ||
TargetServer: svr.URL, | ||
}, | ||
} | ||
r1, e := getPrometheusSecret(context.TODO(), p, fakeClient) | ||
require.Nil(t, e) | ||
require.Equal(t, "myuser", r1.User) | ||
require.Equal(t, config.Secret("mytoken"), r1.Password) | ||
|
||
} | ||
|
||
func Test_GetRoundtripper(t *testing.T) { | ||
goodsecret := &corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test", | ||
Namespace: "default", | ||
}, | ||
Data: map[string][]byte{ | ||
"user": []byte("myuser"), | ||
"password": []byte("mytoken"), | ||
}, | ||
} | ||
tests := []struct { | ||
name string | ||
provider metricsapi.KeptnMetricsProvider | ||
k8sClient client.Client | ||
want http.RoundTripper | ||
wantErr bool | ||
errorStr string | ||
}{ | ||
{ | ||
name: "TestSuccess", | ||
provider: metricsapi.KeptnMetricsProvider{ | ||
ObjectMeta: metav1.ObjectMeta{Namespace: "default"}, | ||
Spec: metricsapi.KeptnMetricsProviderSpec{ | ||
Type: "", | ||
TargetServer: "", | ||
SecretKeyRef: v1.SecretKeySelector{ | ||
LocalObjectReference: v1.LocalObjectReference{ | ||
Name: "test", | ||
}, | ||
Key: "", | ||
Optional: nil, | ||
}, | ||
}, | ||
}, | ||
k8sClient: fake.NewClient(goodsecret), | ||
want: config.NewBasicAuthRoundTripper("myuser", "mytoken", "", promapi.DefaultRoundTripper), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "TestSecretNotDefined", | ||
provider: metricsapi.KeptnMetricsProvider{}, | ||
k8sClient: fake.NewClient(), | ||
want: promapi.DefaultRoundTripper, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "TestErrorFromGetPrometheusSecretNotExists", | ||
provider: metricsapi.KeptnMetricsProvider{ | ||
ObjectMeta: metav1.ObjectMeta{Namespace: "default"}, | ||
Spec: metricsapi.KeptnMetricsProviderSpec{ | ||
Type: "", | ||
TargetServer: "", | ||
SecretKeyRef: v1.SecretKeySelector{ | ||
LocalObjectReference: v1.LocalObjectReference{ | ||
Name: "test", | ||
}, | ||
Key: "", | ||
Optional: nil, | ||
}, | ||
}, | ||
}, | ||
k8sClient: fake.NewClient(), | ||
want: nil, | ||
wantErr: true, | ||
errorStr: "not found", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := RoundTripperRetriever{}.GetRoundTripper(context.TODO(), tt.provider, tt.k8sClient) | ||
t.Log(err) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("getRoundtripper() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if tt.errorStr != "" && !strings.Contains(err.Error(), tt.errorStr) { | ||
t.Errorf("getRoundtripper() error = %s, wantErr %s", err.Error(), tt.errorStr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("getRoundtripper() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.