Skip to content

Commit

Permalink
fix: use helm compatible template for secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
karlderkaefer committed Jan 30, 2024
1 parent ff9d48e commit c06ff6e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 52 deletions.
4 changes: 2 additions & 2 deletions docs/pingdom-transcation-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ spec:
args:
input: textarea[name=q]
# this will replaced with the value of the secret before sending the request to pingdom
value: '{{secret:my-secret-name:my-secret-key}}'
value: '{secret:my-secret-name:my-secret-key}'
- function: basic_auth
args:
user: admin
password: '{{secret:my-secret-name:admin-password}}'
password: '{secret:my-secret-name:admin-password}'
- function: submit
args:
form: form
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (service *PingdomTransactionMonitorService) createTransactionCheck(monitor
providerConfig, _ := monitor.Config.(*endpointmonitorv1alpha1.PingdomTransactionConfig)
if providerConfig == nil {
// ignore monitor if type is not PingdomTransaction
log.Info("Monitor is not PingdomTransaction type" + monitor.Name)
log.Info("Monitor is not PingdomTransaction type " + monitor.Name)
return nil
}
transactionCheck.Name = monitor.Name
Expand Down Expand Up @@ -283,7 +283,7 @@ func (service *PingdomTransactionMonitorService) NewStepArgsByMap(input map[stri
}

// ReplaceSecretValuesInArgs replaces secrets in StepArgs with actual secret values from Kubernetes.
// It expects secrets to be formatted as {{secret:secret-name:key}} in the args.
// It expects secrets to be formatted as {secret:secret-name:key} in the args.
// Returns an error if the secret or defined secret key cannot be retrieved.
func replaceSecretValuesInArgs(args *pingdomNew.StepArgs, kubeClient KubernetesInterface, namespace string) error {
if args == nil {
Expand Down Expand Up @@ -318,7 +318,7 @@ func replaceSecretValuesInArgs(args *pingdomNew.StepArgs, kubeClient KubernetesI

// parseSecretTemplate extracts secret name and key from a string template.
func parseSecretTemplate(content string) (secretName string, secretKey string) {
const secretPattern = `{{secret:(.*):(.*)}}`
const secretPattern = `{secret:(.*):(.*)}`
re := regexp.MustCompile(secretPattern)

matches := re.FindStringSubmatch(content)
Expand Down
69 changes: 22 additions & 47 deletions pkg/monitors/pingdomtransaction/pingdom-transaction-monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

pingdomNew "github.com/karlderkaefer/pingdom-golang-client/pkg/pingdom/openapi"
"github.com/karlderkaefer/pingdom-golang-client/pkg/pingdom/openapi/ptr"
endpointmonitorv1alpha1 "github.com/stakater/IngressMonitorController/v2/api/v1alpha1"
"github.com/stakater/IngressMonitorController/v2/pkg/config"
"github.com/stakater/IngressMonitorController/v2/pkg/models"
"github.com/stakater/IngressMonitorController/v2/pkg/util"
Expand Down Expand Up @@ -34,66 +35,40 @@ func TestAddMonitorWithCorrectValues(t *testing.T) {
log.Error(nil, "Failed to find provider")
return
}

service.Setup(*provider)
m := models.Monitor{Name: "google-test", URL: "https://google1.com"}

service.Add(m)

mRes, err := service.GetByName("google-test")
assert.NilError(t, err)

defer func() {
// Cleanup
service.Remove(*mRes)
}()

if err != nil {
t.Error("Error: " + err.Error())
spec := &endpointmonitorv1alpha1.PingdomTransactionConfig{
Steps: []endpointmonitorv1alpha1.PingdomStep{
{
Function: "go_to",
Args: map[string]string{
"url": "https://google.com",
},
},
},
}

assert.Equal(t, mRes.Name, m.Name)
assert.Equal(t, mRes.URL, "https://google1.com")
}

func TestUpdateMonitorWithCorrectValues(t *testing.T) {
config := config.GetControllerConfigTest()

service := PingdomTransactionMonitorService{}

provider := util.GetProviderWithName(config, "Pingdom")
if provider == nil {
// TODO: Currently forcing to pass the test as we dont have Pingdom account to test
// Fail this case in future when have a valid Pingdom account
log.Error(nil, "Failed to find provider")
return
}
service.Setup(*provider)
m := models.Monitor{
Name: "google-test",
URL: "https://google.com",
Config: spec,
}

// Create initial record
m := models.Monitor{Name: "google-update-test", URL: "https://google.com"}
service.Add(m)

mRes, err := service.GetByName("google-update-test")
mRes, err := service.GetByName("google-test")
assert.NilError(t, err)

defer func() {
// Cleanup
service.Remove(*mRes)
}()

// Update the record
mRes.URL = "https://facebook.com"

service.Update(*mRes)

mRes, err = service.GetByName("google-update-test")
if err != nil {
t.Error("Error: " + err.Error())
}

assert.Equal(t, mRes.Name, m.Name)
assert.Equal(t, mRes.URL, "https://facebook.com")
assert.Equal(t, mRes.URL, "https://google.com")
}

func TestGetSecretFromTemplate(t *testing.T) {
Expand All @@ -105,7 +80,7 @@ func TestGetSecretFromTemplate(t *testing.T) {
}{
{
name: "With Secret",
content: "This is a sample content with {{secret:my-secret:my-key}} embedded in it.",
content: "This is a sample content with {secret:my-secret:my-key} embedded in it.",
expectedSecretName: "my-secret",
expectedSecretKey: "my-key",
},
Expand All @@ -117,7 +92,7 @@ func TestGetSecretFromTemplate(t *testing.T) {
},
{
name: "Invalid Format",
content: "This is a sample content with invalid secret format {{secret:my-secret}}",
content: "This is a sample content with invalid secret format {secret:my-secret}",
expectedSecretName: "",
expectedSecretKey: "",
},
Expand Down Expand Up @@ -163,8 +138,8 @@ func TestReplaceSecrets(t *testing.T) {
}{
{
name: "Password field with secret",
password: "{{secret:my-secret:password}}",
value: "{{secret:my-secret:username}}",
password: "{secret:my-secret:password}",
value: "{secret:my-secret:username}",
expectedPassword: "simple-password",
expectedValue: "admin",
expectError: false,
Expand All @@ -179,7 +154,7 @@ func TestReplaceSecrets(t *testing.T) {
},
{
name: "Password field with invalid secret",
password: "{{secret:my-secret:invalidkey}}",
password: "{secret:my-secret:invalidkey}",
value: "no-secret",
expectedPassword: "",
expectedValue: "no-secret",
Expand Down

0 comments on commit c06ff6e

Please sign in to comment.