Skip to content

fix keycloak secrets drift - #508

Merged
Andrei Kvapil (kvaps) merged 1 commit into
mainfrom
bugfix/fix-draft
Dec 4, 2024
Merged

Andrei Kvapil (kvaps) merged 1 commit into
mainfrom
bugfix/fix-draft

Conversation

@kvaps

@kvaps Andrei Kvapil (kvaps) commented Dec 4, 2024 •

Copy link
Copy Markdown
Member

Summary by CodeRabbit

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/XS This PR changes 0-9 lines, ignoring generated files label Dec 4, 2024
@coderabbitai

coderabbitai Bot commented Dec 4, 2024 •

Copy link
Copy Markdown
Contributor

Warning

Rate limit exceeded

Andrei Kvapil (@kvaps) has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 18 minutes and 26 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 72efd30 and b5262ea.

Walkthrough

The changes in this pull request modify the sts.yaml file to enhance the handling of Keycloak credentials. It introduces a mechanism to check for an existing Kubernetes Secret named {{ .Release.Name }}-credentials in the cozy-keycloak namespace. If the Secret exists, the password is retrieved and decoded; if not, a new random password is generated. This password is then used in the Secret's stringData section and as the KEYCLOAK_ADMIN_PASSWORD in the Keycloak container specification.

Changes

File Path Change Summary
packages/system/keycloak/templates/sts.yaml Added logic to check for an existing Kubernetes Secret for Keycloak credentials; retrieves or generates a password accordingly.

Possibly related PRs

  • fix keycloak secrets drift #508: This PR directly modifies the same sts.yaml file and implements a similar mechanism for managing Keycloak credentials by checking for an existing password in a Kubernetes Secret, aligning closely with the changes made in the main PR.

Suggested reviewers

  • kvaps

🐰 In the cozy nook where secrets dwell,
A password's tale I have to tell.
If one is found, we'll use it right,
If not, a new one springs to light!
Keycloak smiles, all set to go,
With credentials strong, our confidence will grow! 🌟


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 kind/bug Categorizes issue or PR as related to a bug kind/feature Categorizes issue or PR as related to a new feature labels Dec 4, 2024

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

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)

Line range hint 12-17: Add metadata labels and namespace

The Secret should include proper labels for management and explicit namespace specification.

 kind: Secret
 metadata:
   name: {{ .Release.Name }}-credentials
+  namespace: cozy-keycloak
+  labels:
+    app: keycloak-ha
+    app.kubernetes.io/managed-by: {{ .Release.Service }}
+    app.kubernetes.io/instance: {{ .Release.Name }}
 stringData:
🧰 Tools
🪛 yamllint (1.35.1)

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

(syntax)


Line range hint 1-143: Consider enhancing secret management strategy

While this implementation fixes the immediate secret drift issue, consider these long-term improvements:

  1. Integrate with an external secret management solution (HashiCorp Vault, AWS Secrets Manager, etc.)
  2. Implement proper secret rotation mechanisms with version tracking
  3. Add annotations for secret rotation timestamps and version information

This would provide better security, auditability, and operational management of credentials.

🧰 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 72efd30.

📒 Files selected for processing (1)
  • packages/system/keycloak/templates/sts.yaml (1 hunks)
🔇 Additional comments (2)
packages/system/keycloak/templates/sts.yaml (2)

Line range hint 20-143: StatefulSet configuration looks good

The StatefulSet configuration follows best practices with proper:

  • Security context and capabilities
  • Resource management
  • Health checks
  • Database configuration
  • JGroups clustering setup
🧰 Tools
🪛 yamllint (1.35.1)

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

(syntax)


6-8: 🛠️ Refactor suggestion

Add validation for existing password

The code should validate the format and length of an existing password before using it. A corrupted or invalid password could cause Keycloak authentication issues.

Consider adding validation:

 {{- if $existingPassword }}
+  {{- $decodedPassword := index $existingPassword.data "password" | b64dec }}
+  {{- if and (regexMatch "^[a-zA-Z0-9]+$" $decodedPassword) (ge (len $decodedPassword) 8) }}
+    {{- $password = $decodedPassword }}
+  {{- end }}
-  {{- $password = index $existingPassword.data "password" | b64dec }}
 {{- end }}

{{- $cozyConfig := lookup "v1" "ConfigMap" "cozy-system" "cozystack" }}
{{- $host := index $cozyConfig.data "root-host" }}

{{- $existingPassword := lookup "v1" "Secret" "cozy-keycloak" "{{ .Release.Name }}-credentials" }}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix template syntax in lookup path

The lookup path contains literal template markers which won't be evaluated correctly:

-{{- $existingPassword := lookup "v1" "Secret" "cozy-keycloak" "{{ .Release.Name }}-credentials" }}
+{{- $existingPassword := lookup "v1" "Secret" "cozy-keycloak" (printf "%s-credentials" .Release.Name) }}
📝 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
{{- $existingPassword := lookup "v1" "Secret" "cozy-keycloak" "{{ .Release.Name }}-credentials" }}
{{- $existingPassword := lookup "v1" "Secret" "cozy-keycloak" (printf "%s-credentials" .Release.Name) }}

@kvaps
Andrei Kvapil (kvaps) merged commit d43b8fd into main Dec 4, 2024
@kvaps
Andrei Kvapil (kvaps) deleted the bugfix/fix-draft branch December 4, 2024 18:40
klinch0 added a commit to klinch0/cozystack that referenced this pull request Dec 5, 2024
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

## Summary by CodeRabbit

- **New Features**
- Enhanced management of Keycloak credentials by checking for existing
passwords stored in Kubernetes Secrets.
- Improved password management logic, allowing for the reuse of existing
passwords or the generation of new ones as needed.

- **Bug Fixes**
- Streamlined secret handling to avoid unnecessary random password
generation, improving security and maintainability.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Co-authored-by: Floppy Disk <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Categorizes issue or PR as related to a bug kind/feature Categorizes issue or PR as related to a new feature size/XS This PR changes 0-9 lines, ignoring generated files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants