armosec / kubecop

Runtime detection and response for malicious events in Kubernetes workloads
Apache License 2.0
38 stars 5 forks source link

Feature/admission controller #189

Open amitschendel opened 7 months ago

amitschendel commented 7 months ago

Type

enhancement, documentation


Description


Changes walkthrough

Relevant files
Enhancement
main.go
Add Admission Controller Mode Support in Main Application

cmd/main.go
  • Added support for running in admission controller mode with new
    command line argument --mode-admission-controller.
  • Implemented starting of the admission controller when running in
    admission controller mode.
  • Added synchronization primitives to safely handle concurrent
    execution.
  • +39/-2   
    types.go
    Define Admission Control Data Structure                                   

    pkg/admission/types.go
  • Defined a new struct AdmissionControlData to hold data related to
    admission control alerts.
  • +25/-0   
    server.go
    Implement Admission Webhook Server                                             

    pkg/admission/webhook/server.go
  • Implemented the webhook server for admission control with methods to
    start, handle health checks, and validate requests.
  • Added logic to dynamically reload TLS certificates.
  • +396/-0 
    validator.go
    Implement Admission Validator Logic                                           

    pkg/admission/webhook/validator.go
  • Implemented admission validator with methods to validate pods and
    cluster role bindings.
  • +101/-0 
    *.go
    Implement Admission Control Alert in Exporters                     

    pkg/exporters/*.go
  • Added SendAdmissionControlAlert method implementation across various
    exporter types to handle admission control alerts.
  • Tests
    engine_test.go
    Update Engine Tests with Admission Control Alert Mock       

    pkg/engine/engine_test.go
  • Added a mock method SendAdmissionControlAlert to the MockExporter for
    testing purposes.
  • +4/-0     
    Configuration changes
    admission-*.yaml
    Add Kubernetes Manifests for Admission Controller Deployment

    chart/kubecop/templates/admission-*.yaml
  • Added Kubernetes manifests for deploying the admission controller,
    including a deployment, service, and validating webhook configuration.

  • Dependencies
    go.mod go.sum
    Update Go Module Dependencies                                                       

    go.mod go.sum
  • Updated module dependencies, including the addition of
    k8s.io/apiserver for admission webhook support.

  • ✨ PR-Agent usage: Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    codiumai-pr-agent-free[bot] commented 7 months ago

    PR Description updated to latest commit (https://github.com/armosec/kubecop/commit/27dc44a570e698247e4bb2804a021f44302c770c)

    codiumai-pr-agent-free[bot] commented 7 months ago

    PR Review

    ⏱️ Estimated effort to review [1-5] 4, due to the introduction of a new feature (admission controller), which involves multiple components and files, including main application logic, admission control data structures, webhook server implementation, and integration with exporters. The PR also includes Kubernetes manifests and updates to Go module dependencies. The complexity and breadth of changes require a thorough review to ensure correctness, security, and adherence to best practices.
    πŸ§ͺ Relevant tests No
    πŸ” Possible issues Possible Bug: The admission controller's validator logic (in `pkg/admission/webhook/validator.go`) is very basic and might not cover all necessary security validations. This could lead to security vulnerabilities if not properly addressed.
    Performance Concern: The webhook server implementation (in `pkg/admission/webhook/server.go`) includes file watching for TLS certificate reloading. Depending on the frequency of file changes and the efficiency of the implementation, this could introduce performance issues.
    Error Handling: The error handling in the webhook server (in `pkg/admission/webhook/server.go`) and the validator (in `pkg/admission/webhook/validator.go`) might not cover all failure scenarios comprehensively, potentially leading to unhandled errors or panics.
    πŸ”’ Security concerns - TLS Certificate Handling: The webhook server uses TLS certificates for secure communication. It's crucial to ensure that certificate handling, including reloading and error handling, is done securely to prevent any potential leakage of sensitive information. - Admission Control Logic: The admission controller's validation logic needs to be thoroughly reviewed to ensure it doesn't introduce any security vulnerabilities, such as allowing privileged operations or resources that should be restricted.

    ✨ Review tool usage guide:
    **Overview:** The `review` tool scans the PR code changes, and generates a PR review. The tool can be triggered [automatically](https://github.com/Codium-ai/pr-agent/blob/main/Usage.md#github-app-automatic-tools) every time a new PR is opened, or can be invoked manually by commenting on any PR. When commenting, to edit [configurations](https://github.com/Codium-ai/pr-agent/blob/main/pr_agent/settings/configuration.toml#L19) related to the review tool (`pr_reviewer` section), use the following template: ``` /review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=... ``` With a [configuration file](https://github.com/Codium-ai/pr-agent/blob/main/Usage.md#working-with-github-app), use the following template: ``` [pr_reviewer] some_config1=... some_config2=... ```
    Utilizing extra instructions
    The `review` tool can be configured with extra instructions, which can be used to guide the model to a feedback tailored to the needs of your project. Be specific, clear, and concise in the instructions. With extra instructions, you are the prompter. Specify the relevant sub-tool, and the relevant aspects of the PR that you want to emphasize. Examples for extra instructions: ``` [pr_reviewer] # /review # extra_instructions=""" In the 'possible issues' section, emphasize the following: - Does the code logic cover relevant edge cases? - Is the code logic clear and easy to understand? - Is the code logic efficient? ... """ ``` Use triple quotes to write multi-line instructions. Use bullet points to make the instructions more readable.
    How to enable\disable automation
    - When you first install PR-Agent app, the [default mode](https://github.com/Codium-ai/pr-agent/blob/main/Usage.md#github-app-automatic-tools) for the `review` tool is: ``` pr_commands = ["/review", ...] ``` meaning the `review` tool will run automatically on every PR, with the default configuration. Edit this field to enable/disable the tool, or to change the used configurations
    Auto-labels
    The `review` tool can auto-generate two specific types of labels for a PR: - a `possible security issue` label, that detects possible [security issues](https://github.com/Codium-ai/pr-agent/blob/tr/user_description/pr_agent/settings/pr_reviewer_prompts.toml#L136) (`enable_review_labels_security` flag) - a `Review effort [1-5]: x` label, where x is the estimated effort to review the PR (`enable_review_labels_effort` flag)
    Extra sub-tools
    The `review` tool provides a collection of possible feedbacks about a PR. It is recommended to review the [possible options](https://github.com/Codium-ai/pr-agent/blob/main/docs/REVIEW.md#enabledisable-features), and choose the ones relevant for your use case. Some of the feature that are disabled by default are quite useful, and should be considered for enabling. For example: `require_score_review`, `require_soc2_ticket`, and more.
    Auto-approve PRs
    By invoking: ``` /review auto_approve ``` The tool will automatically approve the PR, and add a comment with the approval. To ensure safety, the auto-approval feature is disabled by default. To enable auto-approval, you need to actively set in a pre-defined configuration file the following: ``` [pr_reviewer] enable_auto_approval = true ``` (this specific flag cannot be set with a command line argument, only in the configuration file, committed to the repository) You can also enable auto-approval only if the PR meets certain requirements, such as that the `estimated_review_effort` is equal or below a certain threshold, by adjusting the flag: ``` [pr_reviewer] maximal_review_effort = 5 ```
    More PR-Agent commands
    > To invoke the PR-Agent, add a comment using one of the following commands: > - **/review**: Request a review of your Pull Request. > - **/describe**: Update the PR title and description based on the contents of the PR. > - **/improve [--extended]**: Suggest code improvements. Extended mode provides a higher quality feedback. > - **/ask \**: Ask a question about the PR. > - **/update_changelog**: Update the changelog based on the PR's contents. > - **/add_docs** πŸ’Ž: Generate docstring for new components introduced in the PR. > - **/generate_labels** πŸ’Ž: Generate labels for the PR based on the PR's contents. > - **/analyze** πŸ’Ž: Automatically analyzes the PR, and presents changes walkthrough for each component. >See the [tools guide](https://github.com/Codium-ai/pr-agent/blob/main/docs/TOOLS_GUIDE.md) for more details. >To list the possible configuration parameters, add a **/config** comment.
    See the [review usage](https://github.com/Codium-ai/pr-agent/blob/main/docs/REVIEW.md) page for a comprehensive guide on using this tool.
    codiumai-pr-agent-free[bot] commented 7 months ago

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Enhancement
    Replace time.After with time.NewTicker for efficient periodic operations in loops. ___ **Consider using time.NewTicker instead of time.After in a loop for efficiency. time.After
    creates a new channel each time it's called, which can lead to memory leaks if not
    properly managed. time.NewTicker, on the other hand, reuses the same channel, making it
    more suitable for cases where you need to tick at regular intervals.** [pkg/admission/webhook/server.go [96]](https://github.com/armosec/kubecop/pull/189/files#diff-053b946850841ed54f8ca9db2d5ea6aef0f132123ee18681def83a599f63bdd5R96-R96) ```diff -case <-time.After(2 * time.Second): +ticker := time.NewTicker(2 * time.Second) +defer ticker.Stop() +case <-ticker.C: ```
    Initialize or remove ContainerID and ContainerName fields for AdmissionControlAlerts. ___ **Consider initializing ContainerID and ContainerName with actual values if available, or
    remove these fields if they are not applicable for AdmissionControlAlerts. Having empty
    strings might be misleading if the data is actually available but not being set.** [pkg/exporters/http_exporter.go [269-270]](https://github.com/armosec/kubecop/pull/189/files#diff-6e04fd42d767812ea2855370a21a524371930b320544c0ee0954e1500242fbbdR269-R270) ```diff -ContainerID: "", -ContainerName: "", +ContainerID: admissionControlData.ContainerID, // Assuming ContainerID is available in admissionControlData +ContainerName: admissionControlData.ContainerName, // Assuming ContainerName is available in admissionControlData ```
    Implement SendAdmissionControlAlert for SyslogExporter. ___ **Implement the SendAdmissionControlAlert function for the SyslogExporter to ensure feature
    parity across different exporter types.** [pkg/exporters/syslog_exporter.go [194-195]](https://github.com/armosec/kubecop/pull/189/files#diff-d84d67a80745d135f3f4af7c387c52361f506fc84f626e517ea8b37b150f8bb7R194-R195) ```diff -// not implemented +func (se *SyslogExporter) SendAdmissionControlAlert(admissionControlData admission.AdmissionControlData) { + // Implementation goes here +} ```
    Implement SendAdmissionControlAlert for CsvExporter. ___ **Similar to the SyslogExporter, implement the SendAdmissionControlAlert function for the
    CsvExporter to maintain consistency and feature completeness across exporters.** [pkg/exporters/csv_exporter.go [164-165]](https://github.com/armosec/kubecop/pull/189/files#diff-0a2d375b8879952f2c2f2dbfe3d1976f1b38645d958ad75633a9aca5d151aef3R164-R165) ```diff -// not implemented +func (ce *CsvExporter) SendAdmissionControlAlert(admissionControlData admission.AdmissionControlData) { + // CSV export logic for admission control alerts +} ```
    Best practice
    Implement graceful server shutdown with a timeout for better resource management. ___ **To improve error handling and server shutdown process, consider implementing a graceful
    shutdown with a timeout. This ensures that all active connections are properly closed
    before the server shuts down, preventing potential data loss or corruption.** [pkg/admission/webhook/server.go [164]](https://github.com/armosec/kubecop/pull/189/files#diff-053b946850841ed54f8ca9db2d5ea6aef0f132123ee18681def83a599f63bdd5R164-R164) ```diff -if err := currentServer.Close(); err != nil { +shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) +defer cancel() +if err := currentServer.Shutdown(shutdownCtx); err != nil { ```
    Handle errors when reading from the request body to prevent unexpected behavior. ___ **It's recommended to check for errors when reading from the request body. Ignoring errors
    can lead to unexpected behavior or security vulnerabilities. Always handle the error
    returned by ReadFrom.** [pkg/admission/webhook/server.go [378]](https://github.com/armosec/kubecop/pull/189/files#diff-053b946850841ed54f8ca9db2d5ea6aef0f132123ee18681def83a599f63bdd5R378-R378) ```diff -bodybuf.ReadFrom(r.Body) +_, err := bodybuf.ReadFrom(r.Body) +if err != nil { + return nil, fmt.Errorf("error reading request body: %v", err) +} ```
    Add exceptions for system namespaces to prevent blocking critical pods. ___ **To ensure that the admission controller does not inadvertently block critical system pods,
    consider adding an exception for pods in the kube-system and kube-public namespaces. This
    can prevent potential issues with cluster operation.** [pkg/admission/webhook/validator.go [62]](https://github.com/armosec/kubecop/pull/189/files#diff-b85dee05cc62171829a9ea04dde813d857587405cc16594c351c5833502ad512R62-R62) ```diff -if attrs.GetOperation() == admission.Create { +if attrs.GetOperation() == admission.Create && attrs.GetNamespace() != "kube-system" && attrs.GetNamespace() != "kube-public" { ```
    Replace fmt.Sprintf with structured logging. ___ **Use structured logging instead of fmt.Sprintf for constructing log messages to ensure
    consistent log format and easier parsing by log management tools.** [pkg/exporters/alert_manager.go [146]](https://github.com/armosec/kubecop/pull/189/files#diff-40568094a80f334c1148d539cedecc426f810a6e32e5eeab274195f4c80693d5R146-R146) ```diff -summary := fmt.Sprintf("Admission control alert in '%s' namespace", admissionControlData.Namespace) +summary := "Admission control alert" +log.WithFields(log.Fields{"namespace": admissionControlData.Namespace}).Info(summary) ```
    Change the failurePolicy from Ignore to Fail for stricter validation. ___ **Consider setting a more specific failurePolicy for the admission controller. Using Ignore
    might lead to resources being admitted that shouldn't be, in case the webhook is down or
    fails to respond. A more conservative approach would be to use Fail to ensure that no
    resource is admitted without passing the validation.** [chart/kubecop/templates/admission-deployment.yaml [36]](https://github.com/armosec/kubecop/pull/189/files#diff-38a554d1df87c59cc954f575665893d8645530bd4675937355d52c69af32df70R36-R36) ```diff -failurePolicy: Ignore +failurePolicy: Fail ```
    Add resource requests and limits to the admission controller container. ___ **It's recommended to specify resource limits and requests for the containers to ensure that
    the admission controller has enough resources to operate efficiently and to prevent it
    from consuming too much resource on the node. This can be done by adding limits and
    requests under the resources section.** [chart/kubecop/templates/admission-deployment.yaml [31-32]](https://github.com/armosec/kubecop/pull/189/files#diff-38a554d1df87c59cc954f575665893d8645530bd4675937355d52c69af32df70R31-R32) ```diff resources: - {{- toYaml .Values.kubecop.resources | nindent 12 }} + requests: + memory: "64Mi" + cpu: "250m" + limits: + memory: "128Mi" + cpu: "500m" ```
    Set imagePullPolicy to Always to ensure the latest image is used. ___ **The imagePullPolicy should be set to Always to ensure that the latest version of the image
    is always used, especially important in dynamic environments or when using mutable tags
    like latest.** [chart/kubecop/templates/admission-deployment.yaml [26]](https://github.com/armosec/kubecop/pull/189/files#diff-38a554d1df87c59cc954f575665893d8645530bd4675937355d52c69af32df70R26-R26) ```diff -imagePullPolicy: {{ .Values.image.pullPolicy }} +imagePullPolicy: Always ```
    Confirm and document the choice of service type if different from ClusterIP. ___ **For services that are meant to be exposed within the cluster only, it's recommended to
    stick with ClusterIP as the service type. If there's a specific requirement to expose the
    service outside of the cluster, consider using LoadBalancer or NodePort. Ensure that this
    choice is intentional and documented.** [chart/kubecop/templates/admission-service.yaml [16]](https://github.com/armosec/kubecop/pull/189/files#diff-f39618a3b418e6792532492563c0e705874f1dda34072f50c214283ed479488dR16-R16) ```diff -type: ClusterIP # Or use LoadBalancer or NodePort if needed +type: ClusterIP ```
    Maintainability
    Use constants for Kubernetes resources instead of hardcoded strings. ___ **Using hardcoded strings for resources like "Pod" or "pods" can lead to typos and
    maintenance issues. Consider using constants from the Kubernetes API, such as
    corev1.ResourcePods.String() for better maintainability and readability.** [pkg/admission/webhook/validator.go [24]](https://github.com/armosec/kubecop/pull/189/files#diff-b85dee05cc62171829a9ea04dde813d857587405cc16594c351c5833502ad512R24-R24) ```diff -if attrs.GetKind().GroupKind().Kind == "Pod" || attrs.GetResource().Resource == "pods" { +if attrs.GetKind().GroupKind().Kind == corev1.ResourcePods.String() || attrs.GetResource().Resource == corev1.ResourcePods.String() { ```
    Use a constant for severity levels. ___ **The severity level '7' seems arbitrary. Define a constant for severity levels to improve
    code readability and maintainability.** [pkg/exporters/stdout_exporter.go [62]](https://github.com/armosec/kubecop/pull/189/files#diff-720fbdf92dc87851c73298130b8a4ee2991b0e58e6498fddfe1e5bf74900c66eR62-R62) ```diff -"severity": 7, +"severity": SeverityLevelAlert, // Assuming SeverityLevelAlert is a constant defined elsewhere ```
    Security
    Increase the certificate key size to 2048 bits for enhanced security. ___ **The certificate key size of 1024 bits might not provide sufficient security. It's
    recommended to use at least 2048 bits for RSA keys to ensure a higher level of security.** [chart/kubecop/templates/admission-webhook.yaml [3]](https://github.com/armosec/kubecop/pull/189/files#diff-11a39967000bf754146d36a80f4aaa32ae4de7887fa0f901e730a705a1fe63c2R3-R3) ```diff -{{- $cert := genSignedCert $svcName nil (list $svcName) 1024 $ca -}} +{{- $cert := genSignedCert $svcName nil (list $svcName) 2048 $ca -}} ```

    ✨ Improve tool usage guide:
    **Overview:** The `improve` tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered [automatically](https://github.com/Codium-ai/pr-agent/blob/main/Usage.md#github-app-automatic-tools) every time a new PR is opened, or can be invoked manually by commenting on a PR. When commenting, to edit [configurations](https://github.com/Codium-ai/pr-agent/blob/main/pr_agent/settings/configuration.toml#L69) related to the improve tool (`pr_code_suggestions` section), use the following template: ``` /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=... ``` With a [configuration file](https://github.com/Codium-ai/pr-agent/blob/main/Usage.md#working-with-github-app), use the following template: ``` [pr_code_suggestions] some_config1=... some_config2=... ```
    Enabling\disabling automation
    When you first install the app, the [default mode](https://github.com/Codium-ai/pr-agent/blob/main/Usage.md#github-app-automatic-tools) for the improve tool is: ``` pr_commands = ["/improve --pr_code_suggestions.summarize=true", ...] ``` meaning the `improve` tool will run automatically on every PR, with summarization enabled. Delete this line to disable the tool from running automatically.
    Utilizing extra instructions
    Extra instructions are very important for the `improve` tool, since they enable to guide the model to suggestions that are more relevant to the specific needs of the project. Be specific, clear, and concise in the instructions. With extra instructions, you are the prompter. Specify relevant aspects that you want the model to focus on. Examples for extra instructions: ``` [pr_code_suggestions] # /improve # extra_instructions=""" Emphasize the following aspects: - Does the code logic cover relevant edge cases? - Is the code logic clear and easy to understand? - Is the code logic efficient? ... """ ``` Use triple quotes to write multi-line instructions. Use bullet points to make the instructions more readable.
    A note on code suggestions quality
    - While the current AI for code is getting better and better (GPT-4), it's not flawless. Not all the suggestions will be perfect, and a user should not accept all of them automatically. - Suggestions are not meant to be simplistic. Instead, they aim to give deep feedback and raise questions, ideas and thoughts to the user, who can then use his judgment, experience, and understanding of the code base. - Recommended to use the 'extra_instructions' field to guide the model to suggestions that are more relevant to the specific needs of the project, or use the [custom suggestions :gem:](https://github.com/Codium-ai/pr-agent/blob/main/docs/CUSTOM_SUGGESTIONS.md) tool - With large PRs, best quality will be obtained by using 'improve --extended' mode.
    More PR-Agent commands
    > To invoke the PR-Agent, add a comment using one of the following commands: > - **/review**: Request a review of your Pull Request. > - **/describe**: Update the PR title and description based on the contents of the PR. > - **/improve [--extended]**: Suggest code improvements. Extended mode provides a higher quality feedback. > - **/ask \**: Ask a question about the PR. > - **/update_changelog**: Update the changelog based on the PR's contents. > - **/add_docs** πŸ’Ž: Generate docstring for new components introduced in the PR. > - **/generate_labels** πŸ’Ž: Generate labels for the PR based on the PR's contents. > - **/analyze** πŸ’Ž: Automatically analyzes the PR, and presents changes walkthrough for each component. >See the [tools guide](https://github.com/Codium-ai/pr-agent/blob/main/docs/TOOLS_GUIDE.md) for more details. >To list the possible configuration parameters, add a **/config** comment.
    See the [improve usage](https://github.com/Codium-ai/pr-agent/blob/main/docs/IMPROVE.md) page for a more comprehensive guide on using this tool.