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

Webhook fix to allow service account user #8

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jayanth-tjvrr
Copy link

@jayanth-tjvrr jayanth-tjvrr commented Feb 25, 2025

This PR fixes KAAP-283 and includes fixes from previous branches regarding pushing the k8s bundle.

Fixes:

Edited create/update function to exclude the hostname check. This check is causing issues in our case. The previous code required the username to include the hostname in it for update/create. Since we don't follow that pattern, it was causing issues.

Changed the Installationsecret name to byoinstall-* so that it won't interfere with bootstrapSecret.

Testing:

published these changes in my private image and edited the byoh controller image to my private image.

Tried running the byoh agent with create/update calls in the webhookconfiguration. But still the byoh Host got registered successfully.

Webhook config:

  namespaceSelector: {}
  objectSelector: {}
  rules:
  - apiGroups:
    - infrastructure.cluster.x-k8s.io
    apiVersions:
    - v1beta1
    operations:
    - CREATE
    - UPDATE
    - DELETE
    resources:
    - byohosts
    scope: '*'

Byoh agent logs:

I0225 20:37:58.943998     180 host_registrar.go:46] Registering ByoHost
I0225 20:37:58.983516     180 host_registrar.go:81] Add Network Info
I0225 20:37:58.984713     180 host_registrar.go:89] Attach Host Platform details
I0225 20:37:59.195376     180 listener.go:44] controller-runtime/metrics "msg"="Metrics server is starting to listen" "addr"=":8080"
I0225 20:37:59.196402     180 internal.go:369]  "msg"="Starting server" "addr"={"IP":"::","Port":8080,"Zone":""} "kind"="metrics" "path"="/metrics"
I0225 20:37:59.196653     180 controller.go:186]  "msg"="Starting EventSource" "controller"="byohost" "controllerGroup"="infrastructure.cluster.x-k8s.io" "controllerKind"="ByoHost" "source"="kind source: *v1beta1.ByoHost"
I0225 20:37:59.196722     180 controller.go:194]  "msg"="Starting Controller" "controller"="byohost" "controllerGroup"="infrastructure.cluster.x-k8s.io" "controllerKind"="ByoHost"

Summary by Bito

This PR removes hostname validation in the webhook for service account resource management, updates installation secret naming to prevent bootstrap secret conflicts, adds sample configurations for RBAC and cluster setup, and revises Kubernetes package installation with new repository configurations. Several security-related changes are needed including re-enabling hostname validation and implementing dedicated service accounts.

Unit tests added: False

Estimated effort to review (1-5, lower is better): 4

Jayanth Reddy and others added 2 commits February 26, 2025 01:36
- bundled repo for 1.26 kube
- Sample yamls for byoh config with service account
- Webhook fix to allow sevice account user
- Fix to avoid same secret name for install & bootstrap
Copy link

Code Review Agent Run Status

  • Limitations and other issues: ❌ Failure - The AI Code Review Agent skipped reviewing this change because it is configured to exclude certain pull requests based on the source/target branch or the pull request status. You can change the settings here, or contact the agent instance creator at [email protected].

@jayanth-tjvrr jayanth-tjvrr changed the title Private/main/jayanth/kaap 283 2 Webhook fix to allow service account user Feb 25, 2025
@jayanth-tjvrr jayanth-tjvrr marked this pull request as ready for review February 25, 2025 20:43
Copy link
Member

@mithilarun mithilarun left a comment

Choose a reason for hiding this comment

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

@jayanth-tjvrr there are a lot of changes here that seem unrelated to the webhook change. Is all this needed?

@@ -109,7 +109,7 @@ run: manifests generate fmt vet ## Run a controller from your host.
go run ./main.go

docker-build: ## Build docker image with the manager.
docker build -t ${IMG} .
docker build --network host -t ${IMG} .
Copy link
Member

Choose a reason for hiding this comment

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

Why is this needed?

Comment on lines +59 to +61
//if !strings.Contains(byoHost.Name, substrs[2]) {
// return admission.Denied(fmt.Sprintf("%s cannot create/update resource %s", userName, byoHost.Name))
//}
Copy link
Member

Choose a reason for hiding this comment

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

Don't comment - remove the lines.

Copy link

bito-code-review bot commented Feb 25, 2025

Code Review Agent Run #021764

Actionable Suggestions - 11
  • installer/bundle_builder/Dockerfile - 1
    • Consider using specific version tag · Line 19-19
  • installer/bundle_builder/push-bundle.sh - 1
    • Shell debug mode may expose sensitive data · Line 6-6
  • docs/sample/generate-bootstrap-config.sh - 1
    • Consider making cluster server URL configurable · Line 6-6
  • installer/bundle_builder/ingredients/deb/download.sh - 3
    • Consider adding error handling for mkdir · Line 12-12
    • Fix typo in environment variable name · Line 18-18
    • Consider validating kubernetes version variable · Line 21-21
  • docs/sample/rolebinding.yaml - 1
    • Consider dedicated service account for cluster permissions · Line 8-34
  • docs/sample/rbac_csr.yaml - 1
    • Consider dedicated service account for certs · Line 17-19
  • apis/infrastructure/v1beta1/byohost_webhook.go - 2
    • Consider keeping host name validation check · Line 59-61
    • Consider adding space after comment marker · Line 59-61
  • docs/sample/cluster.yaml - 1
    • Consider revising null matchLabels selector · Line 45-46
Additional Suggestions - 3
  • docs/sample/secret.yaml - 1
    • Consider more descriptive secret name · Line 4-4
  • controllers/infrastructure/k8sinstallerconfig_controller.go - 1
  • docs/sample/kamaji_clusterroles.yaml - 1
    • Consider expanding RBAC verbs for byoclusters · Line 8-8
Review Details
  • Files reviewed - 21 · Commit Range: 8db3384..2c79821
    • Makefile
    • agent/cloudinit/cloudinit.go
    • agent/main.go
    • apis/infrastructure/v1beta1/byohost_webhook.go
    • apis/infrastructure/v1beta1/groupversion_info.go
    • controllers/infrastructure/k8sinstallerconfig_controller.go
    • docs/sample/cluster.yaml
    • docs/sample/generate-bootstrap-config.sh
    • docs/sample/kamaji_clusterrolebindings.yaml
    • docs/sample/kamaji_clusterroles.yaml
    • docs/sample/rbac_csr.yaml
    • docs/sample/rolebinding.yaml
    • docs/sample/roles.yaml
    • docs/sample/sa.yaml
    • docs/sample/secret.yaml
    • installer/bundle_builder/Dockerfile
    • installer/bundle_builder/build-bundle.sh
    • installer/bundle_builder/build-push-bundle.sh
    • installer/bundle_builder/ingredients/deb/Dockerfile
    • installer/bundle_builder/ingredients/deb/download.sh
    • installer/bundle_builder/push-bundle.sh
  • Files skipped - 0
  • Tools
    • Golangci-lint (Linter) - ✔︎ Successful
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

AI Code Review powered by Bito Logo

Copy link

Changelist by Bito

This pull request implements the following key changes.

Key Change Files Impacted
Bug Fix - Fix webhook validation and installation secret naming

byohost_webhook.go - Commented out hostname validation check in webhook

k8sinstallerconfig_controller.go - Modified installation secret naming to prevent conflicts

Documentation - Add sample configurations and documentation

cluster.yaml - Added sample cluster configuration file

generate-bootstrap-config.sh - Added script for generating bootstrap configuration

kamaji_clusterrolebindings.yaml - Added RBAC cluster role bindings for Kamaji

kamaji_clusterroles.yaml - Added RBAC cluster roles for Kamaji

rbac_csr.yaml - Added RBAC configuration for certificate signing

rolebinding.yaml - Added role bindings configuration

roles.yaml - Added roles configuration

sa.yaml - Added service account configuration

secret.yaml - Added secret configuration

Feature Improvement - Update Kubernetes package installation and Docker build

Makefile - Added network host flag to Docker build commands

Dockerfile - Updated base image source

build-bundle.sh - Added directory creation and file copying steps

Dockerfile - Updated Kubernetes version and added gnupg package

download.sh - Updated Kubernetes repository configuration

Other Improvements - Code formatting and style fixes

cloudinit.go - Fixed comment indentation

main.go - Fixed comment formatting

groupversion_info.go - Fixed comment spacing

@@ -16,7 +16,7 @@
# // Build and store a BYOH bundle
# docker run --rm -v <INGREDIENTS_HOST_ABS_PATH>:/ingredients -v <BUNDLE_OUTPUT_ABS_PATH>:/bundle --env <THIS_IMAGE>

FROM harbor-repo.vmware.com/dockerhub-proxy-cache/k14s/image
FROM k14s/image

Choose a reason for hiding this comment

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

Consider using specific version tag

Consider using a specific version tag for the base image k14s/image instead of implicitly using latest to ensure reproducible builds and avoid potential breaking changes.

Code suggestion
Check the AI-generated fix before applying
Suggested change
FROM k14s/image
FROM k14s/image:v0.40.0

Code Review Run #021764


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

@@ -3,7 +3,7 @@
# Copyright 2021 VMware, Inc. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

set -e
set -ex

Choose a reason for hiding this comment

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

Shell debug mode may expose sensitive data

Consider if enabling shell debug mode with -x is intentional here. The -x flag will print each command before execution which could expose sensitive information in logs, especially when dealing with bundle operations that may contain credentials or sensitive paths.

Code suggestion
Check the AI-generated fix before applying
Suggested change
set -ex
set -e

Code Review Run #021764


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

NAMESPACE=byoh
SERVICE_ACCOUNT=default
CLUSTER_NAME=$(kubectl config view --minify -o jsonpath='{.clusters[0].name}')
CLUSTER_SERVER=https://10.149.106.220:443

Choose a reason for hiding this comment

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

Consider making cluster server URL configurable

Consider making the CLUSTER_SERVER URL configurable through an environment variable or command line argument instead of hardcoding it. This would make the script more flexible and reusable across different environments.

Code suggestion
Check the AI-generated fix before applying
Suggested change
CLUSTER_SERVER=https://10.149.106.220:443
CLUSTER_SERVER=${CLUSTER_SERVER:-$(kubectl config view --minify -o jsonpath='{.clusters[0].server}')}

Code Review Run #021764


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

@@ -9,14 +9,16 @@ echo Update the apt package index and install packages needed to use the Kubern
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl

mkdir -p /etc/apt/keyrings/

Choose a reason for hiding this comment

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

Consider adding error handling for mkdir

Consider adding error handling when creating the /etc/apt/keyrings/ directory. The mkdir command may fail if the script is not run with sufficient permissions.

Code suggestion
Check the AI-generated fix before applying
Suggested change
mkdir -p /etc/apt/keyrings/
if ! sudo mkdir -p /etc/apt/keyrings/; then
echo "Error: Failed to create /etc/apt/keyrings/ directory"
exit 1
fi

Code Review Run #021764


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

echo Download containerd
curl -LOJR https://github.com/containerd/containerd/releases/download/v${CONTAINERD_VERSION}/cri-containerd-cni-${CONTAINERD_VERSION}-linux-amd64.tar.gz

echo Download the Google Cloud public signing key
sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://dl.k8s.io/apt/doc/apt-key.gpg
curl -fsSL https://pkgs.k8s.io/core:/stable:/${KUBERENETES_MAJOR_VERSION}/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

Choose a reason for hiding this comment

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

Fix typo in environment variable name

There appears to be a typo in the environment variable name KUBERENETES_MAJOR_VERSION. It should be KUBERNETES_MAJOR_VERSION.

Code suggestion
Check the AI-generated fix before applying
Suggested change
curl -fsSL https://pkgs.k8s.io/core:/stable:/${KUBERENETES_MAJOR_VERSION}/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
curl -fsSL https://pkgs.k8s.io/core:/stable:/${KUBERNETES_MAJOR_VERSION}/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

Code Review Run #021764


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them


echo Add the Kubernetes apt repository
echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/${KUBERENETES_MAJOR_VERSION}/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list

Choose a reason for hiding this comment

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

Consider validating kubernetes version variable

Consider validating if ${KUBERENETES_MAJOR_VERSION} is defined before using it in the repository URL. The variable appears to have a typo ('KUBERENETES' instead of 'KUBERNETES') which could cause issues.

Code suggestion
Check the AI-generated fix before applying
Suggested change
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/${KUBERENETES_MAJOR_VERSION}/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list
if [ -z "${KUBERNETES_MAJOR_VERSION}" ]; then echo "Error: KUBERNETES_MAJOR_VERSION is not set"; exit 1; fi
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/${KUBERNETES_MAJOR_VERSION}/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list

Code Review Run #021764


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

Comment on lines +8 to +34
name: default
namespace: byoh
roleRef:
kind: Role
name: default
apiGroup: rbac.authorization.k8s.io
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: csr-manager-binding
subjects:
- kind: ServiceAccount
name: default
namespace: byoh
roleRef:
kind: ClusterRole
name: csr-manager
apiGroup: rbac.authorization.k8s.io
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: byohosts-clusterrolebinding
subjects:
- kind: ServiceAccount
name: default

Choose a reason for hiding this comment

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

Consider dedicated service account for cluster permissions

Consider using a more descriptive service account name than default for the byohosts-clusterrolebinding. Using the default service account for cluster-wide permissions could be a security concern.

Code suggestion
Check the AI-generated fix before applying
 -  name: default
 +  name: byoh-manager
    namespace: byoh
 @@ -20,8 +20,8 @@
 -  name: default
 +  name: byoh-manager
    namespace: byoh
 @@ -33,8 +33,8 @@
 -  name: default
 +  name: byoh-manager
    namespace: byoh

Code Review Run #021764


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

Comment on lines +17 to +19
- kind: ServiceAccount
name: default
namespace: byoh

Choose a reason for hiding this comment

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

Consider dedicated service account for certs

Consider creating a dedicated service account for certificate management operations instead of using the default service account. This would follow security best practices by providing more granular access control.

Code suggestion
Check the AI-generated fix before applying
 - kind: ServiceAccount
 -  name: default
 -  namespace: byoh
 + kind: ServiceAccount
 +  name: cert-manager
 +  namespace: byoh
 ---
 apiVersion: v1
 kind: ServiceAccount
 metadata:
   name: cert-manager
   namespace: byoh

Code Review Run #021764


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

Comment on lines +59 to +61
//if !strings.Contains(byoHost.Name, substrs[2]) {
// return admission.Denied(fmt.Sprintf("%s cannot create/update resource %s", userName, byoHost.Name))
//}

Choose a reason for hiding this comment

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

Consider keeping host name validation check

The commented out code block that checks host name validation appears to be important for security. Consider keeping this validation to ensure only authorized agents can create/update resources for their assigned hosts.

Code suggestion
Check the AI-generated fix before applying
Suggested change
//if !strings.Contains(byoHost.Name, substrs[2]) {
// return admission.Denied(fmt.Sprintf("%s cannot create/update resource %s", userName, byoHost.Name))
//}
if !strings.Contains(byoHost.Name, substrs[2]) {
return admission.Denied(fmt.Sprintf("%s cannot create/update resource %s", userName, byoHost.Name))
}

Code Review Run #021764


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

Comment on lines +59 to +61
//if !strings.Contains(byoHost.Name, substrs[2]) {
// return admission.Denied(fmt.Sprintf("%s cannot create/update resource %s", userName, byoHost.Name))
//}

Choose a reason for hiding this comment

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

Consider adding space after comment marker

Consider adding a space between the comment marker // and the comment text for better readability. The comment should be formatted as // if instead of //if.

Code suggestion
Check the AI-generated fix before applying
Suggested change
//if !strings.Contains(byoHost.Name, substrs[2]) {
// return admission.Denied(fmt.Sprintf("%s cannot create/update resource %s", userName, byoHost.Name))
//}
// if !strings.Contains(byoHost.Name, substrs[2]) {
// return admission.Denied(fmt.Sprintf("%s cannot create/update resource %s", userName, byoHost.Name))
// }

Code Review Run #021764


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

Comment on lines +45 to +46
matchLabels: null
template:

Choose a reason for hiding this comment

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

Consider revising null matchLabels selector

The matchLabels field is set to null which could cause issues with label selector matching. Consider either removing this field if not needed or specifying appropriate label selectors that match the template labels defined in lines 48-49.

Code suggestion
Check the AI-generated fix before applying
Suggested change
matchLabels: null
template:
matchLabels:
nodepool: pool1
template:

Code Review Run #021764


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

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.

3 participants