Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Bearer Token authentication to Provider alertmanager #1021

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

d4rkfella
Copy link

@d4rkfella d4rkfella commented Jan 25, 2025

I'm not a programmer, but after examining the other providers, i think this should be correct. Please let me know :)

Closes: #1020

@d4rkfella d4rkfella changed the title Added option to pass optional Authorization: Bearer header for requests to Alertmanager provider Added option to pass optional Authorization: Bearer header for requests to Alertmanager Jan 25, 2025
Copy link
Member

@matheuscscp matheuscscp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this contribution, @d4rkfella! Left a few comments.

We also need to update the docs to mention this feature. Please update this file:

To configure a Provider for Prometheus Alertmanager, create a Secret with [the
`address`](#address-example) set to the Prometheus Alertmanager [HTTP API
URL](https://prometheus.io/docs/alerting/latest/https/#http-traffic)
including Basic Auth credentials, and a `alertmanager` Provider with a [Secret
reference](#secret-reference).
```yaml
---
apiVersion: notification.toolkit.fluxcd.io/v1beta3
kind: Provider
metadata:
name: alertmanager
namespace: default
spec:
type: alertmanager
secretRef:
name: alertmanager-address
---
apiVersion: v1
kind: Secret
metadata:
name: alertmanager-address
namespace: default
stringData:
address: https://username:password@<alertmanager-url>/api/v2/alerts/"
```

Please mention that the authentication for this provider can now be done through two options: basic auth (the one already documented), and bearer token. You can duplicate the example of basic auth and adapt it for the token. In this case, instead of setting the address field in the secret, this field would be set directly in the Provider object, .spec.address, and the secret would contain the token field instead.

internal/notifier/alertmanager.go Outdated Show resolved Hide resolved
internal/notifier/alertmanager.go Outdated Show resolved Hide resolved
internal/notifier/alertmanager.go Outdated Show resolved Hide resolved
@d4rkfella d4rkfella force-pushed the main branch 11 times, most recently from 27cf38a to cf64337 Compare January 26, 2025 15:24
@d4rkfella
Copy link
Author

Ok i made the changes you suggested, took me a while to figure out how to fix the Signed off by message hah .

@matheuscscp matheuscscp changed the title Added option to pass optional Authorization: Bearer header for requests to Alertmanager Add support for Bearer Token authentication to Provider alertmanager Jan 26, 2025
Copy link
Member

@matheuscscp matheuscscp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working through my comments 🙏

There's a final batch, after those I think we won't need any more changes 😁

Before we merge this, I'll ask you to please kindly test this change in your environment and post here evidence that it works, e.g. logs, screenshots, etc.

Here are the steps to test this:

  1. If you haven't done this already, please generate a GitHub Personal Access Token (Classic) with the write:packages permission in order to push images to the GitHub Container Registry (GHCR). Login locally in Docker using this token: docker login -u d4rkfella ghcr.io --password-stdin and paste the token in the prompt.
  2. Edit the project Makefile in the root directory changing the image like this:
IMG ?= fluxcd/notification-controller:latest

Becomes:

IMG ?= ghcr.io/d4rkfella/fluxcd/notification-controller:am-bearer
  1. Build an image using the following command: make docker-build docker-push
  2. After this you should see a package in your GitHub profile. Click on it and make it public, so your cluster can download the image without a secret.
  3. Update notification-controller in your test environment to use the image: ghcr.io/d4rkfella/fluxcd/notification-controller:am-bearer
  4. Configure the feature and test.

Thanks in advance!

docs/spec/v1beta3/providers.md Outdated Show resolved Hide resolved
docs/spec/v1beta3/providers.md Outdated Show resolved Hide resolved
docs/spec/v1beta3/providers.md Outdated Show resolved Hide resolved
docs/spec/v1beta3/providers.md Outdated Show resolved Hide resolved
@d4rkfella
Copy link
Author

d4rkfella commented Jan 26, 2025

Ok, i build the image locally and tested it by intentionally making a mistake in my kustomization resources and the event was successfully dispatched to alertmanager. I'm not sure why it includes Namespace/kubevirt configured, i have only set it up to send alerts with eventSeverity: error

{"level":"info","ts":"2025-01-26T17:40:20.874Z","logger":"event-server","msg":"dispatching event","eventInvolvedObject":{"kind":"Kustomization","namespace":"flux-system","name":"cluster-apps","uid":"a38a6e6b-ce01-4c64-9593-60e6f99e62ed","apiVersion":"kustomize.toolkit.fluxcd.io/v1","resourceVersion":"2768828"},"message":"Kustomization/vault/vault dry-run failed: failed to create typed patch object (vault/vault; kustomize.toolkit.fluxcd.io/v1, Kind=Kustomization): .spec.targetNamespacee: field not declared in schema\nNamespace/kubevirt configured\n"}

Request is authenticated to oauth2-proxy successfully and send to the upstream alertmanager service.

172.16.0.40 - 2b9be63a-45cf-4bf5-8b2d-dfc9ec9a4caf - [email protected] [2025/01/26 17:40:20] alertmanager-operated.observability.svc.cluster.local:8082 POST / "/api/v2/alerts/" HTTP/1.1 "Go-http-client/1.1" 200 0 0.001

Screenshot_2025-01-26-19-58-48-131_net superblock pushover

@d4rkfella
Copy link
Author

Does the fuzz_test need additional changes to test with token and with an empty string like how its setup for other providers that use token based authentication ?

func Fuzz_AlertManager(f *testing.F) {
	f.Add("token", "update", "", "", []byte{}, []byte("{}"))
	f.Add("", "something", "", "else", []byte{}, []byte(""))

	f.Fuzz(func(t *testing.T,
		token, commitStatus, urlSuffix, summary string, seed, response []byte) {

		ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.Write(response)
			io.Copy(io.Discard, r.Body)
			r.Body.Close()
		}))
		defer ts.Close()

		var cert x509.CertPool
		_ = fuzz.NewConsumer(seed).GenerateStruct(&cert)

		alertmanager, err := NewAlertmanager(fmt.Sprintf("%s/%s", ts.URL, urlSuffix), "", token, &cert)
		if err != nil {
			return
		}

Sorry if asking stupid questions, trying to get a grasp of things :P

@matheuscscp
Copy link
Member

matheuscscp commented Jan 26, 2025

Sorry to bug you with this @d4rkfella, the fuzz tests are flaky at the moment, don't worry about that.

The CI has passed 😀

Now the last thing before I merge this PR, I'll need you to please squash all the commits into one, with the message:

Add support for Bearer Token authentication to Provider alertmanager

(This will require a force push.)

@d4rkfella d4rkfella force-pushed the main branch 2 times, most recently from 6e6791e to c3a8185 Compare January 26, 2025 21:14
@d4rkfella
Copy link
Author

Oh man, i screwed that up

@matheuscscp
Copy link
Member

No need to panic, I'm writing specific instructions for you to recover your work.

@d4rkfella
Copy link
Author

I'm sorry for wasting more of your time.

@matheuscscp
Copy link
Member

You work is in the commit c3a8185465e3ebb5e0f346d1a0561e786c3cdc94.

  1. Reset your branch to the the Flux upstream latest commit: git reset --hard fa7d9f2. This will simply reset your local main branch to the exact same state of the main branch of github.com/fluxcd/notification-controller. If this command fails then it's because you probably need to add the Flux upstream remote to your local clone: git remote add upstream [email protected]:fluxcd/notification-controller.git && git fetch upstream. Then run the reset command again.
  2. Cherry-pick pick the commit with your work: git cherry-pick c3a8185465e3ebb5e0f346d1a0561e786c3cdc94.
  3. At this point, if you run git log you should see your commit on top of the latest commit of Flux upstream. If that's the case, now you can force push: git push --force

Signed-off-by: Georgi Panov <[email protected]>
Signed-off-by: Darkfella91 <[email protected]>

Update alertmanager_test.go

Signed-off-by: Georgi Panov <[email protected]>
Signed-off-by: Darkfella91 <[email protected]>

Update alertmanager_fuzz_test.go

Signed-off-by: Georgi Panov <[email protected]>
Signed-off-by: Darkfella91 <[email protected]>

Update factory.go

Signed-off-by: Georgi Panov <[email protected]>
Signed-off-by: Darkfella91 <[email protected]>

Update factory.go

Signed-off-by: Darkfella91 <[email protected]>

Fix a mistake with the last commit to update the docs

Signed-off-by: Darkfella91 <[email protected]>

Fix another formatting issue

Signed-off-by: Darkfella91 <[email protected]>

Screwed up my previous commit so implementing the suggested changes again and fixed formatting for the structs

Signed-off-by: Darkfella91 <[email protected]>

Tried to use better wording, to outline that authentication is optional

Signed-off-by: Darkfella91 <[email protected]>

Another small change to the explanation for bearer token authentication

Signed-off-by: Darkfella91 <[email protected]>

Fix incorrect article usage and the configured address example as suggested

Signed-off-by: Darkfella91 <[email protected]>
@d4rkfella
Copy link
Author

Thanks for your patience. That should be it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding headers to requests for Alertmanager provider.
2 participants