Skip to content

Authentication for metrics and version endpoint #1607

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

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

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.

Copy link

coderabbitai bot commented Jun 12, 2025

Walkthrough

This change introduces metrics and version endpoint authorization to the system. It adds two new environment variables to the core StatefulSet and endpoint Deployment, manages a new Kubernetes secret for metrics authorization, and updates the reconciler logic to create and inject a metrics token into Prometheus ServiceMonitors for secure endpoint access.

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 to core container and endpoint Deployment; updated manifest checksums.
pkg/system/phase2_creating.go Added default values for NOOBAA_METRICS_AUTH_ENABLED and NOOBAA_VERSION_AUTH_ENABLED in CoreAppConfig data.
pkg/system/phase4_configuring.go Added metrics token secret creation, ServiceMonitor authorization setup, and related helper methods.
pkg/system/reconciler.go Added SecretMetricsAuth field to reconciler and initialized it with correct namespace and name.

Sequence Diagram(s)

sequenceDiagram
    participant Operator
    participant Reconciler
    participant Kubernetes API
    participant NBClient
    participant Prometheus

    Operator->>Reconciler: Start reconciliation
    Reconciler->>Kubernetes API: Ensure SecretMetricsAuth exists
    alt Secret does not exist
        Reconciler->>NBClient: CreateAuthAPI(metrics role)
        NBClient-->>Reconciler: Return metrics token
        Reconciler->>Kubernetes API: Create Secret with metrics token
    end
    Reconciler->>Kubernetes API: Patch StatefulSet and Deployment with METRICS_AUTH_ENABLED and VERSION_AUTH_ENABLED
    Reconciler->>Kubernetes API: Patch ServiceMonitors with Bearer token ref to SecretMetricsAuth
    Prometheus->>Kubernetes API: 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 9c2144c and e4c340c.

📒 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
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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:

✅ 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.

Comment on lines 5152 to 5171
- name: METRICS_AUTH_ENABLED
valueFrom:
configMapKeyRef:
name: noobaa-config
key: METRICS_AUTH_ENABLED
- name: VERSION_AUTH_ENABLED
valueFrom:
configMapKeyRef:
name: noobaa-config
key: VERSION_AUTH_ENABLED
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

❓ Verification inconclusive

Validate ConfigMap defaults and indentation for new flags

  • Ensure that noobaa-config defines default entries for METRICS_AUTH_ENABLED and VERSION_AUTH_ENABLED (e.g., "false"). Missing keys will result in empty env values in the pod.
  • Double-check that the - name: and valueFrom: lines align with existing env entries (same two-space YAML indentation per level) to prevent parsing errors.

🏁 Script executed:

#!/bin/bash
# Check that defaults exist in the internal ConfigMap manifest
grep -E "METRICS_AUTH_ENABLED|VERSION_AUTH_ENABLED" deploy/internal/configmap*.yaml

Length of output: 174


Action Required: Add default entries for METRICS_AUTH_ENABLED and VERSION_AUTH_ENABLED in noobaa-config

  • No occurrences of these keys were found in the internal ConfigMap manifests (e.g., deploy/internal/configmap-*.yaml), so pods will receive empty values at runtime.
  • Please update each deploy/internal/configmap-*.yaml under the data: section with defaults, for example:
    data:
      METRICS_AUTH_ENABLED: "false"
      VERSION_AUTH_ENABLED: "false"
    
  • Verify that in pkg/bundle/deploy.go (around lines 5152–5161) the new env entries use the same two-space indentation per YAML level as existing entries to avoid parsing errors.
🤖 Prompt for AI Agents
In pkg/bundle/deploy.go around lines 5152 to 5161, ensure the new environment
variables METRICS_AUTH_ENABLED and VERSION_AUTH_ENABLED have the same two-space
YAML indentation as existing env entries to avoid parsing errors. Additionally,
update all deploy/internal/configmap-*.yaml files by adding default values for
these keys under the data: section, for example, METRICS_AUTH_ENABLED: "false"
and VERSION_AUTH_ENABLED: "false", so pods receive proper default values at
runtime.

Comment on lines +128 to 129
SecretMetricsAuth *corev1.Secret

Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Hard-coded secret name can collide when multiple NooBaa CRs share a namespace

SecretMetricsAuth is always named metrics-auth-secret.
If two NooBaa CRs are deployed in the same namespace (common in test/CI scenarios) the second reconcile will fail with “already exists” error.

- r.SecretMetricsAuth.Name = "metrics-auth-secret"
+// keep name stable for ServiceMonitors but unique per-system
+ r.SecretMetricsAuth.Name = r.Request.Name + "-metrics-auth"

Remember to update all references (setServiceMonitorAuthorization, RBAC, docs).

Also applies to: 199-200, 248-249, 294-295


🏁 Script executed:

#!/bin/bash
# Locate reconciler.go
fd --max-depth 4 --name reconciler.go

# Show relevant snippet around lines 128–130
sed -n '120,150p' pkg/system/reconciler.go

# Find all occurrences of the hard-coded secret name
rg -n '"metrics-auth-secret"' .

# Find all assignments to SecretMetricsAuth.Name
rg -n 'SecretMetricsAuth\.Name' -n pkg/system/reconciler.go

Length of output: 1725


🏁 Script executed:

#!/bin/bash
# Show the reconciler logic around SecretMetricsAuth and ServiceMonitor setup
sed -n '240,360p' pkg/system/reconciler.go

# Locate where setServiceMonitorAuthorization is invoked
rg -n 'setServiceMonitorAuthorization' -n pkg/system/reconciler.go

Length of output: 5312


Use a per-CR unique name for the metrics-auth secret

The reconciler currently hard-codes

r.SecretMetricsAuth.Name = "metrics-auth-secret"

which will collide if you deploy more than one NooBaa CR in the same namespace. Please change it to derive the name from the CR’s name, for example:

- r.SecretMetricsAuth.Name = "metrics-auth-secret"
+ // keep name stable for ServiceMonitors but unique per-CR
+ r.SecretMetricsAuth.Name = r.Request.Name + "-metrics-auth"

• pkg/system/reconciler.go: replace the hard-coded assignment at line 294.
• Verify and update any ServiceMonitor specs, RBAC Role/RoleBinding rules, helper functions or documentation that refer to "metrics-auth-secret".

🤖 Prompt for AI Agents
In pkg/system/reconciler.go around lines 128-129 and also at lines 199-200,
248-249, and 294-295, the SecretMetricsAuth.Name is hard-coded as
"metrics-auth-secret", which causes conflicts when multiple NooBaa CRs are
deployed in the same namespace. To fix this, change the secret name assignment
to derive it uniquely from the CR's name, for example by appending or prefixing
the CR name to "metrics-auth-secret". Also update all references to this secret
name in setServiceMonitorAuthorization, RBAC rules, and documentation to use the
new dynamic naming scheme to avoid collisions.

@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
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.

1 participant