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

fix keycloak secrets drift #507

Closed
wants to merge 3 commits into from
Closed

Conversation

kvaps
Copy link
Member

@kvaps kvaps commented Dec 4, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced management of Kubernetes secrets for Keycloak configuration, allowing for the reuse of existing client secrets and cookie secrets.
    • Improved password management logic for Keycloak deployment, enabling the retrieval of existing passwords or generating new ones if necessary.
  • Bug Fixes

    • Streamlined secret handling to avoid unnecessary random generation, improving security and maintainability.

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Dec 4, 2024
Copy link
Contributor

coderabbitai bot commented Dec 4, 2024

Walkthrough

The changes in this pull request primarily involve updates to the management of Kubernetes secrets and passwords within Keycloak configurations. The configure-kk.yaml file now checks for existing secrets for k8s-client and kubeapps-client, decoding them from base64 if found. A new variable for $cookieSecret is introduced. In the sts.yaml file, a conditional logic for password management is implemented, allowing the reuse of an existing password from a specified secret or generating a new one if it does not exist.

Changes

File Path Change Summary
packages/system/keycloak-configure/templates/configure-kk.yaml Removed random generation of client secrets; added logic to check for existing secrets and decode them. Introduced $cookieSecret variable based on existing authentication configuration. New Kubernetes Secret resources defined for k8s-client, kubeapps-client, and kubeapps-auth-config.
packages/system/keycloak/templates/sts.yaml Added logic to look up an existing password in {{ .Release.Name }}-credentials secret; if not found, generates a new random password. Updated password assignment in Secret resource and Keycloak container configuration.

Possibly related PRs

  • fix keycloak-configure secrets drift #506: The changes in this PR directly relate to the management of Kubernetes secrets in the configure-kk.yaml file, which is also the focus of the main PR. Both PRs involve the introduction of logic to check for existing secrets and the handling of client secrets in a similar manner.

Suggested labels

enhancement, lgtm

Suggested reviewers

  • klinch0

🐇 In the land of secrets, we hop and play,
Finding lost treasures in a clever way.
With passwords and tokens, all snug and tight,
Our Keycloak's secure, oh what a delight!
So let’s raise our ears and give a cheer,
For the magic of secrets, we hold so dear! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

@dosubot dosubot bot added the bug Something isn't working label Dec 4, 2024
Copy link
Contributor

@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: 1

🧹 Outside diff range and nitpick comments (2)
packages/system/keycloak/templates/sts.yaml (2)

4-8: Add validation for existing password

While reusing existing passwords is good for preventing drift, consider adding validation to ensure the decoded password meets minimum security requirements (length, complexity).

 {{- if $existingPassword }}
   {{- $password = index $existingPassword.data "password" | b64dec }}
+  {{- if not (regexMatch "^[A-Za-z0-9]{16,}$" $password) }}
+    {{- fail "Existing password does not meet minimum requirements (16+ alphanumeric characters)" }}
+  {{- end }}
 {{- end }}

Line range hint 67-68: Use secretKeyRef for admin password

The admin password is currently exposed in the pod spec. Consider using secretKeyRef to reference the password from the secret, similar to how other sensitive environment variables are handled.

 - name: KEYCLOAK_ADMIN_PASSWORD
-  value: {{ $password }}
+  valueFrom:
+    secretKeyRef:
+      name: {{ .Release.Name }}-credentials
+      key: password
🧰 Tools
🪛 yamllint (1.35.1)

[error] 1-1: syntax error: expected the node content, but found '-'

(syntax)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 3aa5f88 and f91cc56.

📒 Files selected for processing (2)
  • packages/system/keycloak-configure/templates/configure-kk.yaml (2 hunks)
  • packages/system/keycloak/templates/sts.yaml (1 hunks)
🧰 Additional context used
🪛 yamllint (1.35.1)
packages/system/keycloak-configure/templates/configure-kk.yaml

[warning] 17-17: wrong indentation: expected 0 but found 2

(indentation)


[warning] 22-22: wrong indentation: expected 0 but found 2

(indentation)

🔇 Additional comments (3)
packages/system/keycloak-configure/templates/configure-kk.yaml (3)

6-18: LGTM! Good secret management practices

The implementation correctly handles existing secrets while providing secure fallback for new deployments. The 32-character random strings provide good entropy.

🧰 Tools
🪛 yamllint (1.35.1)

[warning] 17-17: wrong indentation: expected 0 but found 2

(indentation)


27-56: LGTM! Well-structured secret definitions

The secret resources are properly defined with appropriate encoding and namespace placement.


205-205: LGTM! Proper integration with auth proxy

The cookie secret is correctly integrated into the auth proxy configuration.

Comment on lines +20 to +23
{{ $cookieSecret := randAlphaNum 16 }}
{{- if $existingAuthConfig }}
{{- $cookieSecret = index $existingAuthConfig.data "cookieSecret" | b64dec }}
{{- end }}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Increase cookie secret length

For cookie signing, 16 characters might not provide sufficient entropy. Consider increasing the length to 32 characters to align with security best practices.

-{{ $cookieSecret := randAlphaNum 16 }}
+{{ $cookieSecret := randAlphaNum 32 }}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
{{ $cookieSecret := randAlphaNum 16 }}
{{- if $existingAuthConfig }}
{{- $cookieSecret = index $existingAuthConfig.data "cookieSecret" | b64dec }}
{{- end }}
{{ $cookieSecret := randAlphaNum 32 }}
{{- if $existingAuthConfig }}
{{- $cookieSecret = index $existingAuthConfig.data "cookieSecret" | b64dec }}
{{- end }}
🧰 Tools
🪛 yamllint (1.35.1)

[warning] 22-22: wrong indentation: expected 0 but found 2

(indentation)

@kvaps
Copy link
Member Author

kvaps commented Dec 4, 2024

closed in favor #508

@kvaps kvaps closed this Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants