Skip to content

Authentication for metrics and version endpoint#1607

Merged
naveenpaul1 merged 1 commit intonoobaa:masterfrom
naveenpaul1:metrics-auth-token
Jul 2, 2025
Merged

Authentication for metrics and version endpoint#1607
naveenpaul1 merged 1 commit intonoobaa:masterfrom
naveenpaul1:metrics-auth-token

Conversation

@naveenpaul1
Copy link
Contributor

@naveenpaul1 naveenpaul1 commented May 16, 2025

Explain the changes

  1. Call CreateAuthAPI rpc with a role metrics to get the JWT token in the configuration phase, And the response token will be saved as secret(metrics-auth-secret)
  2. JWT is signed using the existing signature key JWT_SECRET that used to sign other admin and operator credentials.
  3. Secret will be used for authorization for both noobaa management and s3 ServiceMonitor.

Issues: Fixed #xxx / Gap #xxx

  1. RHSTOR-7202
    DFBUGS-1802

Testing Instructions:

containerized deployment

  1. The customer should be able to access the bearer token from the secret metrics-auth-secret, secret can be used for accessing noobaa management and endpoint metrics/version.
JWT_TOKEN=$(oc get secret/{token-secret-name} -n {namespace} -o jsonpath={.data.metrics_token} | base64 -d)    
curl -k -H "Authorization: Bearer ${JWT_TOKEN "https://{endpoint-loadbalancer-ingress-ip}:{endpoint-port}
curl -k -H "Authorization: Bearer ${JWT_TOKEN}" https://$(oc -n {namespace} get route noobaa-mgmt -o jsonpath='{.status.ingress[*].host}/version or metrics endpoints')
  1. Verify all the noobaa specific targets are up and scraping metrics from those tragets in kubernates/ODF

Design doc : https://ibm.ent.box.com/notes/1853310270159

  • Doc added/updated
  • Tests added

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features
    • Added support for metrics and version endpoint authentication, configurable via environment variables.
    • Introduced automatic creation and management of a metrics authorization token for secure Prometheus monitoring integration.
  • Chores
    • Updated internal configuration to support new authentication options for metrics and version endpoints.

@coderabbitai
Copy link

coderabbitai bot commented Jun 12, 2025

Walkthrough

This change adds two new environment variables for metrics and version authorization to the core StatefulSet and endpoint Deployment. It introduces a new Kubernetes secret for metrics authorization, updates the reconciler to create and manage this secret, and configures Prometheus ServiceMonitors to use the secret for bearer token authentication.

Changes

Files/Paths Change Summary
deploy/internal/statefulset-core.yaml, deploy/internal/deployment-endpoint.yaml, pkg/bundle/deploy.go Added NOOBAA_METRICS_AUTH_ENABLED and NOOBAA_VERSION_AUTH_ENABLED env vars sourced from ConfigMap; updated manifest checksums.
pkg/system/phase2_creating.go Added default "true" values for NOOBAA_METRICS_AUTH_ENABLED and NOOBAA_VERSION_AUTH_ENABLED in CoreAppConfig data.
pkg/system/phase4_configuring.go Added creation and reconciliation of metrics auth secret; updated ServiceMonitors to use bearer token authorization; added helper methods.
pkg/system/reconciler.go Added SecretMetricsAuth field to Reconciler struct; initialized with empty secret and set namespace and name.

Sequence Diagram(s)

sequenceDiagram
    participant Operator
    participant Reconciler
    participant KubernetesAPI
    participant NBClient
    participant Prometheus

    Operator->>Reconciler: Start reconciliation
    Reconciler->>KubernetesAPI: Ensure SecretMetricsAuth exists
    alt Secret does not exist
        Reconciler->>NBClient: CreateAuthAPI(metrics role)
        NBClient-->>Reconciler: Return metrics token
        Reconciler->>KubernetesAPI: Create Secret with metrics token
    end
    Reconciler->>KubernetesAPI: Patch StatefulSet and Deployment with METRICS_AUTH_ENABLED and VERSION_AUTH_ENABLED
    Reconciler->>KubernetesAPI: Patch ServiceMonitors with Bearer token referencing SecretMetricsAuth
    Prometheus->>KubernetesAPI: Scrape endpoints using Bearer token
Loading

Poem

In the warren where secrets are kept,
Two new envs in the core have crept.
A token for metrics, secure and neat,
Prometheus now knocks with a secret treat.
With paws on the pulse and ears to the ground,
This bunny ensures metrics are safe and sound! 🐰✨


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d92013c and 0674d87.

📒 Files selected for processing (6)
  • deploy/internal/deployment-endpoint.yaml (1 hunks)
  • deploy/internal/statefulset-core.yaml (1 hunks)
  • pkg/bundle/deploy.go (4 hunks)
  • pkg/system/phase2_creating.go (1 hunks)
  • pkg/system/phase4_configuring.go (3 hunks)
  • pkg/system/reconciler.go (4 hunks)
✅ Files skipped from review due to trivial changes (1)
  • pkg/bundle/deploy.go
🚧 Files skipped from review as they are similar to previous changes (5)
  • deploy/internal/deployment-endpoint.yaml
  • deploy/internal/statefulset-core.yaml
  • pkg/system/phase2_creating.go
  • pkg/system/reconciler.go
  • pkg/system/phase4_configuring.go
⏰ Context from checks skipped due to timeout of 90000ms (13)
  • GitHub Check: run-kms-key-rotate-test
  • GitHub Check: run-azure-vault-test
  • GitHub Check: run-core-config-map-tests
  • GitHub Check: run-operator-tests
  • GitHub Check: run-kms-kmip-test
  • GitHub Check: golangci-lint
  • GitHub Check: run-cli-tests
  • GitHub Check: cnpg-deployment-test
  • GitHub Check: run-kms-dev-test
  • GitHub Check: run-admission-test
  • GitHub Check: run-hac-test
  • GitHub Check: run-kms-tls-sa-test
  • GitHub Check: run-kms-tls-token-test
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (1)
pkg/system/phase4_configuring.go (1)

1672-1684: Nil-slice guard & namespace explicitness

  1. endpoints may be nil if a user prunes endpoints from the CRD; add a quick nil-check to avoid panic.
  2. You rely on defaulting to the ServiceMonitor’s namespace. Being explicit increases readability and avoids surprises if cross-namespace ServiceMonitor support lands.
 func (r *Reconciler) setServiceMonitorAuthorization(endpoints []monitoringv1.Endpoint) {
-    for i := range endpoints {
+    if endpoints == nil {
+        return
+    }
+    for i := range endpoints {
         endpoints[i].Authorization = &monitoringv1.SafeAuthorization{
             Type: "Bearer",
             Credentials: &corev1.SecretKeySelector{
                 LocalObjectReference: corev1.LocalObjectReference{
-                    Name: r.SecretMetricsAuth.Name,
+                    Name:      r.SecretMetricsAuth.Name,
+                    Namespace: r.SecretMetricsAuth.Namespace, // be explicit
                 },
                 Key: metricsAuthKey,
             },
         }
     }
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8e707ef and d49700f.

📒 Files selected for processing (5)
  • deploy/internal/statefulset-core.yaml (1 hunks)
  • pkg/bundle/deploy.go (2 hunks)
  • pkg/system/phase2_creating.go (1 hunks)
  • pkg/system/phase4_configuring.go (3 hunks)
  • pkg/system/reconciler.go (4 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
pkg/system/reconciler.go (3)
pkg/util/util.go (1)
  • KubeObject (276-286)
pkg/bundle/deploy.go (1)
  • File_deploy_internal_secret_empty_yaml (4858-4865)
pkg/options/options.go (1)
  • Namespace (60-60)
pkg/system/phase4_configuring.go (4)
pkg/system/reconciler.go (1)
  • Reconciler (59-133)
pkg/util/util.go (1)
  • SecretResetStringDataFromData (958-968)
pkg/nb/types.go (1)
  • CreateAuthParams (320-325)
pkg/options/options.go (1)
  • OperatorAccountEmail (47-47)
⏰ Context from checks skipped due to timeout of 90000ms (13)
  • GitHub Check: run-kms-key-rotate-test
  • GitHub Check: cnpg-deployment-test
  • GitHub Check: run-kms-tls-sa-test
  • GitHub Check: run-core-config-map-tests
  • GitHub Check: run-kms-kmip-test
  • GitHub Check: run-kms-tls-token-test
  • GitHub Check: run-azure-vault-test
  • GitHub Check: run-kms-dev-test
  • GitHub Check: golangci-lint
  • GitHub Check: run-cli-tests
  • GitHub Check: run-admission-test
  • GitHub Check: run-hac-test
  • GitHub Check: run-operator-tests
🔇 Additional comments (4)
pkg/system/phase2_creating.go (1)

1420-1422: Set default auth flags for metrics and version endpoints
Ensures METRICS_AUTH_ENABLED and VERSION_AUTH_ENABLED default to "true" in the core ConfigMap, matching the new StatefulSet environment variables.

deploy/internal/statefulset-core.yaml (1)

104-113: Enable runtime auth flags for metrics and version endpoints
Added METRICS_AUTH_ENABLED and VERSION_AUTH_ENABLED env vars sourced from the noobaa-config ConfigMap. This aligns with the reconciler defaults and allows toggling endpoint auth without redeploying the pod.

pkg/bundle/deploy.go (1)

5047-5047:

Details

✅ Verification successful

Verify updated SHA256 checksum
The checksum constant was bumped after adding environment variables to the core StatefulSet manifest. Please confirm that this value exactly matches the SHA256 of deploy/internal/statefulset-core.yaml to avoid bundle mismatches.


🏁 Script executed:

#!/bin/bash
# Verify the Sha256_deploy_internal_statefulset_core_yaml matches the file content.
expected="94af9a850a7bf063f236f7ff3c8a23630a882329137bd854e33b040cec52de44"
actual=$(sha256sum deploy/internal/statefulset-core.yaml | awk '{print $1}')
if [ "$expected" != "$actual" ]; then
  echo "❌ SHA mismatch: expected $expected, got $actual"
  exit 1
fi
echo "✅ SHA matches"

Length of output: 549


🏁 Script executed:

#!/bin/bash
# Re-verify the SHA256 checksum using shasum since sha256sum is unavailable.
expected="94af9a850a7bf063f236f7ff3c8a23630a882329137bd854e33b040cec52de44"
actual=$(shasum -a 256 deploy/internal/statefulset-core.yaml | awk '{print $1}')
if [ "$expected" != "$actual" ]; then
  echo "❌ SHA mismatch: expected $expected, got $actual"
  exit 1
fi
echo "✅ SHA matches"

Length of output: 550


🏁 Script executed:

#!/usr/bin/env bash
# Use Python to compute SHA256 since sha256sum and shasum are unavailable.
expected="94af9a850a7bf063f236f7ff3c8a23630a882329137bd854e33b040cec52de44"
actual=$(python3 - << 'EOF'
import hashlib, sys
try:
    data = open('deploy/internal/statefulset-core.yaml', 'rb').read()
    print(hashlib.sha256(data).hexdigest())
except Exception as e:
    sys.exit(1)
EOF
)
if [ -z "$actual" ]; then
  echo "❌ Failed to compute SHA256. Please verify manually. "
  exit 1
fi
if [ "$expected" != "$actual" ]; then
  echo "❌ SHA mismatch: expected $expected, got $actual"
  exit 1
fi
echo "✅ SHA matches"

Length of output: 422


Checksum Verified for StatefulSet Manifest
The SHA256 constant in pkg/bundle/deploy.go (line 5047) matches the computed SHA256 of deploy/internal/statefulset-core.yaml (94af9a850a7bf063f236f7ff3c8a23630a882329137bd854e33b040cec52de44). No further action is needed.

pkg/system/phase4_configuring.go (1)

49-50: 👍 Dedicated key constant

Using a single, file-local metricsAuthKey constant prevents typos in multiple call-sites.

@noobaa noobaa deleted a comment from coderabbitai bot Jun 12, 2025
@pull-request-size pull-request-size bot added size/L and removed size/M labels Jun 12, 2025
@naveenpaul1 naveenpaul1 force-pushed the metrics-auth-token branch from e4c340c to d92013c Compare July 2, 2025 04:57
@naveenpaul1 naveenpaul1 requested a review from dannyzaken July 2, 2025 05:06
Copy link
Member

@tangledbytes tangledbytes left a comment

Choose a reason for hiding this comment

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

LGTM. Just some minor comments.

Signed-off-by: naveenpaul1 <napaul@redhat.com>
@naveenpaul1 naveenpaul1 force-pushed the metrics-auth-token branch from d92013c to 0674d87 Compare July 2, 2025 06:53
@naveenpaul1 naveenpaul1 merged commit 3fb66bc into noobaa:master Jul 2, 2025
16 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Jul 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants