Resource object change control method and device, equipment and medium
By receiving API requests in Kubernetes and matching them with resource change configuration information, using webhooks to determine the source of API requests, ensuring that only authorized users can change the resource object, solving the problem that resource objects are easily changed by malicious changes and improving the stability of the application.
Patent Information
- Application Number
- CN202411063824.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-05
- Publication Date
- 2024-11-01
- Estimated Expiration
- 2044-08-05
AI Technical Summary
The resource objects of third-party applications in Kubernetes are easily changed maliciously, causing the resource objects to run in an unexpected state, bringing risks to the application.
By receiving the resource information in the API request, match the resource change configuration information. If the match is successful, it is sent to the network hook webhook to determine whether the API request comes from the authorized change user. The API request is processed based on the webhook's response to ensure that only the authorized user can handle it. Changes to the target resource object.
It effectively reduces the probability that the resource object is maliciously changed by unauthorized users, avoids the resource object running in an unexpected state, and ensures the stability of the application in K8S.
Smart Images

Figure CN118890191A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a method, device, equipment and medium for controlling changes to resource objects. Background Art
[0002] Helm is a native package management tool for Kubernetes (K8s for short). Helm allows third parties to provide a software package called Chart, which contains relevant K8s resource templates and other files for creating or maintaining related resource objects.
[0003] In the related technology, Helm adopts an open approach to support users' deployment and subsequent changes to third-party applications, that is, a third-party application deployed through a Chart can subsequently change the corresponding resource objects of the third-party application in K8s in any way. For example, users can update the corresponding resource objects of third-party applications in K8s through Chart, and can also directly manage the corresponding resource objects of third-party applications in K8s through the kubectl binary tool, such as changing the corresponding resource objects of third-party applications in K8s through operations such as kubectledit and kubectl apply. In this open approach, it is easy for the resource objects corresponding to third-party applications to be maliciously changed, causing the resource objects to run in an unexpected state, thereby causing various usage problems for the application. Summary of the invention
[0004] The embodiments of the present application provide a resource object change control method, apparatus, device and medium, which are used to solve the problem in the related art that the resource objects corresponding to the applications in K8s are easily maliciously changed, causing the resource objects to run in an unexpected state, bringing usage risks to the applications.
[0005] In a first aspect, the present application provides a method for controlling changes to a resource object, comprising:
[0006] Receive an application program interface (API) request, wherein the API request includes resource information;
[0007] Matching the resource information with each resource change configuration information;
[0008] If the resource information successfully matches any resource change configuration information, determining that the API request is used to request a change to a target resource object corresponding to the resource change configuration information;
[0009] Sending the API request to a webhook corresponding to the resource change configuration information, and determining by the webhook whether the API request comes from an authorized change user of the target resource object;
[0010] Processing the API request based on an indication sent by the webhook of whether to respond to the API request, wherein the indication is sent by the webhook based on a result of determining whether the API request comes from an authorized change user of the target resource object.
[0011] In some embodiments, the authorized change user includes an authorized Helm user, and
[0012] The webhook compares the request user information of the API request with the user information of the Helm user to determine whether the API request comes from the Helm user;
[0013] If it is determined that the API request comes from the Helm user, an instruction to respond to the API request is sent.
[0014] In some embodiments, the user information of the Helm user is a user name or user group set by the service account ServiceAccount, and the permission corresponding to the user information of the Helm user is the cluster administrator permission of Kubernetes;
[0015] Before matching the resource information with each resource change configuration information, the method further includes:
[0016] Determine whether the user who initiates the API request has the corresponding resource operation authority for the requested resource object.
[0017] In some embodiments, the user information of the Helm user and the cluster administrator permissions are applied for and created through a Helm subcommand before the Helm user calls the Kubernetes API to change resources for the first time, or the user information of the Helm user and the cluster administrator permissions are applied for and created when the Helm user calls the Kubernetes API to change resources for the first time.
[0018] In some embodiments, the authorized change user includes an authorized non-Helm user, and
[0019] The webhook obtains the annotation content corresponding to the annotation keyword of the target indicating resource change permission from the resource information, wherein the annotation content is obtained by encoding the permission description information of the current change according to a preset encoding method;
[0020] If the annotation is obtained, it is determined that the API request comes from the non-Helm user;
[0021] Decoding the acquired annotation content according to the preset decoding method corresponding to the preset encoding method to obtain the permission description information of the current change;
[0022] Compare the permission description information with the preset permission range information of the authorized kubectl non-Helm user to determine whether the API request for this change is within the change permission range of the authorized kubectl non-Helm user;
[0023] If it is determined that the API request for this change is within the change permission scope of the non-Helm user and that it comes from the authorized kubectl, an indication of responding to the API request is sent.
[0024] In some embodiments, the authorized change user includes each system built-in user of Kubernetes, and
[0025] The webhook compares the requesting user information of the API request with the user information of each system built-in user to determine whether the API request comes from any system built-in user;
[0026] If it is determined that the API request comes from any system built-in user, an instruction to respond to the API request is sent.
[0027] In some embodiments, processing the API request based on the indication sent by the webhook of whether to respond to the API request includes:
[0028] If the webhook sends an instruction in response to the API request, then based on the API request, the target resource object is changed;
[0029] If the webhook sends an indication of not responding to the API request, the API request is rejected and the reason for the rejection is returned.
[0030] In a second aspect, the present application provides a resource object change control device, comprising:
[0031] A receiving module, used for receiving an application program interface (API) request, wherein the API request includes resource information;
[0032] A matching module, used for matching the resource information with each resource change configuration information;
[0033] A determination module, configured to determine that the API request is used to request a change to a target resource object corresponding to the resource change configuration information if the resource information successfully matches any resource change configuration information;
[0034] A sending module, used for sending the API request to a webhook corresponding to the resource change configuration information, and the webhook determines whether the API request comes from an authorized change user of the target resource object;
[0035] A processing module is used to process the API request based on an indication sent by the webhook of whether to respond to the API request, wherein the indication is sent by the webhook based on a determination result of whether the API request comes from an authorized change user of the target resource object.
[0036] In some embodiments, the authorized change user includes an authorized Helm user, and
[0037] The webhook compares the request user information of the API request with the user information of the Helm user to determine whether the API request comes from the Helm user;
[0038] If it is determined that the API request comes from the Helm user, an instruction to respond to the API request is sent.
[0039] In some embodiments, the user information of the Helm user is a user name or user group set by the service account ServiceAccount, and the permission corresponding to the user information of the Helm user is the cluster administrator permission of Kubernetes; further comprising:
[0040] The permission module is used to determine whether the user who initiates the API request has the corresponding resource operation permission for the requested resource object before matching the resource information with each resource change configuration information.
[0041] In some embodiments, the user information of the Helm user and the cluster administrator permissions are applied for and created through a Helm subcommand before the Helm user calls the Kubernetes API to change resources for the first time, or the user information of the Helm user and the cluster administrator permissions are applied for and created when the Helm user calls the Kubernetes API to change resources for the first time.
[0042] In some embodiments, the authorized change user includes an authorized non-Helm user, and
[0043] The webhook obtains the annotation content corresponding to the annotation keyword of the target indicating resource change permission from the resource information, wherein the annotation content is obtained by encoding the permission description information of the current change according to a preset encoding method;
[0044] If the annotation is obtained, it is determined that the API request comes from the non-Helm user;
[0045] Decoding the acquired annotation content according to the preset decoding method corresponding to the preset encoding method to obtain the permission description information of the current change;
[0046] Compare the permission description information with the preset permission range information of the authorized kubectl non-Helm user to determine whether the API request for this change is within the change permission range of the authorized kubectl non-Helm user;
[0047] If it is determined that the API request for this change is within the change permission scope of the non-Helm user and that it comes from the authorized kubectl, an indication of responding to the API request is sent.
[0048] In some embodiments, the authorized change user includes each system built-in user of Kubernetes, and
[0049] The webhook compares the requesting user information of the API request with the user information of each system built-in user to determine whether the API request comes from any system built-in user;
[0050] If it is determined that the API request comes from any system built-in user, an instruction to respond to the API request is sent.
[0051] In some embodiments, the processing module is specifically used to:
[0052] If the webhook sends an instruction in response to the API request, then based on the API request, the target resource object is changed;
[0053] If the webhook sends an indication of not responding to the API request, the API request is rejected and the reason for the rejection is returned.
[0054] In a third aspect, the present application provides an electronic device, including:
[0055] A memory for storing program instructions;
[0056] The processor is used to call the program instructions stored in the memory, and execute the steps included in any one of the methods in the first aspect according to the obtained program instructions.
[0057] In a fourth aspect, the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, wherein the computer program includes program instructions, and when the program instructions are executed by a computer, the computer executes any one of the methods described in the first aspect.
[0058] In a fifth aspect, the present application provides a computer program product, comprising: a computer program code, when the computer program code is run on a computer, the computer executes any one of the methods described in the first aspect.
[0059] In an embodiment of the present application, the resource information in the received API request is matched with each resource change configuration information. If the resource information successfully matches any resource change configuration information, it is determined that the API request is used to request a change to the target resource object. Then, the API request can be sent to the webhook corresponding to the resource change configuration information, and the webhook determines whether the API request comes from an authorized change user of the target resource object, and then processes the API request based on the indication sent by the webhook on whether to respond to the API request. In this way, when an API request to change the target resource object is received, it can be determined whether the API request comes from an authorized change user of the target resource object, and it can be ensured that only authorized change users can change the target resource object, reducing the probability of the target resource object being maliciously changed by unauthorized users and the probability of the target resource object running in an unexpected state, thereby ensuring the stability of the use of the corresponding application deployed in K8S. BRIEF DESCRIPTION OF THE DRAWINGS
[0060] Figure 1 A possible application scenario diagram provided for an embodiment of the present application;
[0061] Figure 2 A flowchart of a resource object change control method provided in an embodiment of the present application;
[0062] Figure 3 A structural diagram of a resource object change control device provided in an embodiment of the present application;
[0063] Figure 4 A structural diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0064] In order to make the purpose, technical scheme and advantages of the present application clearer, the technical scheme in the embodiment of the present application will be clearly and completely described below in conjunction with the drawings in the embodiment of the present application. Obviously, the described embodiment is only a part of the embodiment of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of the present application. In the absence of conflict, the embodiments in the present application and the features in the embodiments can be arbitrarily combined with each other. In addition, although the logical order is shown in the flow chart, in some cases, the steps shown or described can be performed in an order different from that here.
[0065] The terms "first" and "second" in the specification and claims of the present application and the above-mentioned drawings are used to distinguish different objects, rather than to describe a specific order. In addition, the term "comprising" and any of their variations are intended to cover non-exclusive protection. For example, a process, method, system, product or device comprising a series of steps or units is not limited to the listed steps or units, but optionally also includes steps or units that are not listed, or optionally also includes other steps or units inherent to these processes, methods, products or devices. "Multiple" in the present application can mean at least two, for example, two, three or more, and the embodiments of the present application are not limited.
[0066] The following is a description of exemplary embodiments of the present application in conjunction with the accompanying drawings, including various details of the embodiments of the present application to facilitate understanding, which should be considered as merely exemplary. Therefore, those of ordinary skill in the art should recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope of the present application. Similarly, for the sake of clarity and conciseness, the description of well-known functions and structures is omitted in the following description. It should be noted that in the embodiments of the present application, certain software, components, models and other existing solutions in the industry may be mentioned, which should be considered as exemplary, and their purpose is only to illustrate the feasibility of the implementation of the technical solution of the present application, but it does not mean that the applicant has or will necessarily use the solution.
[0067] In the technical solution of this application, the acquisition, transmission, storage, and use of data are in compliance with the requirements of relevant national laws and regulations.
[0068] Before introducing the change control method for resource objects provided in the embodiment of the present application, in order to facilitate understanding, the technical background of the embodiment of the present application is first introduced in detail below.
[0069] Helm is a native package management tool for Kubernetes (K8s for short). Helm allows third parties to provide a software package called Chart, which contains relevant K8s resource templates and other files for creating or maintaining related resource objects.
[0070] In related technologies, Helm adopts an open approach to support users' deployment and subsequent changes of third-party applications, that is, a third-party application deployed through a Chart can subsequently change the corresponding resource objects of the third-party application in K8s in any way. For example, users can update the corresponding resource objects of third-party applications in K8s through Chart, and can also directly manage the corresponding resource objects of third-party applications in K8s through the kubectl binary tool, such as changing the corresponding resource objects of third-party applications in K8s through operations such as kubectledit and kubectl apply.
[0071] This open approach places high demands on application operation and maintenance, because operation and maintenance personnel need to understand the application's architectural design, operating logic, configuration relationships, etc. Operation and maintenance personnel need to be clear about what they are changing, how to change it, and what results it will bring.
[0072] In addition, the open approach cannot regulate the development of application release charts. During the development phase, developers may modify a release-related K8s resource or configuration in the development and test environment through kubectlizedit or other methods for convenience. However, such modifications may not be merged into the release chart due to negligence, and subsequent developers need to investigate the reasons, which leads to reduced development quality and efficiency.
[0073] In addition, for applications that have already been released, opening up change permissions cannot play a restrictive role. Any authorized person or third-party management system may change the application configuration for any purpose or simply due to operational errors, causing the application operation to deviate from the original design.
[0074] To this end, an embodiment of the present application provides a change control scheme for resource objects. In this scheme, when an API request for changing a target resource object (referring to a resource object under change control) is received, it is possible to determine whether the API request comes from an authorized change user of the target resource object, thereby ensuring that only authorized change users can change the target resource object, and other users are not allowed to change the target resource object, so as to reduce the probability of the target resource object being maliciously changed by unauthorized users and the probability of the target resource object running in an unexpected state, thereby ensuring the stability of use of the corresponding application deployed in K8S.
[0075] See also Figure 1, Figure 1 A scenario diagram of a change control method for a resource object provided in an embodiment of the present application includes Helm and K8s, wherein Helm is a package manager for K8s, a client tool under a command line, and is mainly used for the creation, packaging, and publishing of K8s application charts, as well as the creation and management of local and remote chart repositories. K8s is an open source container orchestration engine that supports automated deployment, large-scale scalability, and application container management, and usually includes Master nodes and Node nodes, which are distributed and deployed on multiple services. The Master node has an application program interface service (APIServer) and a verification network hook webhook, and the Node node is deployed with resources of a third-party application. Among them, webhook is an admission control webhook supported by K8s, which usually runs in a K8s cluster in a Pod mode, and can be written, created, updated, and deleted by a third party on demand, and the content of a specific resource object submitted to the K8s APIServer in a specific scenario is changed and the legality is verified.
[0076] It should be noted that helm chart can be used to deploy applications as well as to change (i.e. upgrade) applications. A helm chart usually contains several k8s resources that make up an application, and the update of any k8s resource can be considered as an application update.
[0077] The following describes the resource object change control method provided by the exemplary embodiment of the present application in combination with the above application scenarios and with reference to the accompanying drawings. It should be noted that the above application scenarios are only shown to facilitate understanding of the spirit and principles of the present application, and the implementation methods of the present application are not limited in this regard.
[0078] Please refer to Figure 2 , which is a flow chart of a resource object change control method provided in an embodiment of the present application, includes the following steps.
[0079] In step 201, the API Server receives an API request, which includes resource information.
[0080] Among them, the API request comes from the client, and the resource information includes the resource type (such as API group, API version number, resource type name), namespace, resource content, etc. The operation performed on the resource, such as update, delete, etc., and the label corresponding to the resource can be learned from the resource content.
[0081] In step 202, the API Server matches the resource information with each resource change configuration information.
[0082] Generally speaking, if a K8s has issued permissions to multiple Helms, that is, a K8s corresponds to multiple authorized Helm users, then each authorized Helm user corresponds to a resource change configuration information, and a resource change configuration information corresponds to a webhook. The resource change configuration information is used to describe what kind of target resource objects need to be checked for changes and what kind of API requests need to be sent to the corresponding webhook.
[0083] In addition, a resource change configuration information may include: at least one of: a wildcard matching rule of the resource, an operation type of the resource, a namespace of the resource, and a tag of the resource, wherein the wildcard matching rule of the resource is used to indicate the resource type of the target resource, the operation type of the resource is used to indicate the operation type of the target resource, the namespace of the resource is used to indicate the namespace of the target resource, and the tag of the resource is used to indicate the tag of the target resource.
[0084] In step 203, if the API Server determines that the resource information successfully matches any resource change configuration information, it determines that the API request is used to request a change to the target resource object corresponding to the resource change configuration information.
[0085] In step 204, the API Server sends the API request to the webhook corresponding to the resource change configuration information.
[0086] In step 205, the webhook determines whether the API request comes from a user who is authorized to change the target resource object.
[0087] In case 1, the authorized change user may include the authorized Helm user. The webhook may compare the request user information of the API request with the user information of the authorized Helm user to determine whether the API request comes from the authorized Helm user. If it is determined that the API request comes from the authorized Helm user, an instruction to respond to the API request may be sent. The comparison is successful if the request user information of the API request is the same as the user information of the authorized Helm user.
[0088] In actual applications, the user information of the authorized Helm user can be the user name or user group set by the service account ServiceAccount, and the permissions corresponding to the user information of the authorized Helm user can be the cluster administrator permissions of K8s. In addition, before matching the resource information with the resource change configuration information, the APIServer can also determine whether the user who initiates the API request has the corresponding resource operation permissions for the requested resource object. If not, the API request can be rejected. If so, the subsequent steps can be executed.
[0089] In addition, the user information and cluster administrator permissions of the authorized Helm user can be created by applying for Helm subcommands before the authorized Helm user calls the K8s API to change resources for the first time, or they can be created when the authorized Helm user calls the K8s API to change resources for the first time. The former method is created in advance through a separate Helm subcommand, and the subsequent change process remains unchanged. The latter method automatically applies for creation with the first resource change, which is imperceptible to users. Both methods have their own advantages and can be selected by technical personnel according to actual needs.
[0090] In the above situation 1, it can be ensured that only authorized Helm users can make changes to the target resource objects. Considering that in the enterprise release process, if an emergency problem needs to be repaired, the response speed of emergency processing is often required, and the change process control through helm cannot be handled in time and effectively in an emergency. On the one hand, it takes extra time to write a new helm chart package, and on the other hand, the transfer of the helm chart package from the development environment to the production environment may also encounter the release process constraints in general enterprises, resulting in a longer processing time.
[0091] To this end, the embodiment of the present application designs an emergency processing mechanism for authorization codes based on the webhook plus authorized Helm users. Unlike the method of limiting the helm client in situation 1, the authorization code method does not limit the client. As long as the API request initiated by the client to change the target resource object contains specific annotation content, the target resource object is allowed to be changed.
[0092] Specifically, the administrator (i.e., the user who changes the target resource) can use the authorization code issuance tool to encode the permission description information of an emergency change, and obtain the encoding result such as a string of characters, where the permission description information is as follows: a) which authorized operator issued the code; b) the coding date; c) the serial number of the code within the day of issuance; d) the coding confusion value and the verification value. Then, through kubectl or a client that can directly send API requests, the encoding result is added to the API request for requesting to change the target resource object in the form of (key, value), where key is the annotation keyword indicating the change permission, which can be pre-specified, and value is the annotation content, i.e., the encoding result. Since the authorization code issuance tool and webhook are jointly released, both use the same preset encoding and decoding method and preset the same permission information, so the encoding result in the API request can be parsed and identified by the webhook. That is, the following situation 2.
[0093] In case 2, the authorized change users may also include authorized non-Helm users, such as authorized kubectl users or authorized client users who can directly send API requests. Webhook can also obtain the annotation content corresponding to the target annotation keyword (i.e., the annotation keyword indicating the change permission) from the resource information. If obtained, it is determined that the API request comes from an authorized non-Helm user, and then the annotation content can be decoded according to the preset decoding method to obtain the permission description information of this change, and the permission description information is compared with the preset permission range information of the authorized non-Helm user to determine whether this change is within the change permission range of the non-Helm user. After determining that this change is within the change permission range of the non-Helm user, an indication of responding to the API request can be sent.
[0094] For example, the number of changes that an authorized non-Helm user can make to the target resource object cannot exceed three times a day. If this change is the second change made by the non-Helm user to the target resource object, then this change is within the non-Helm user's change permission scope. If this change is the fourth change made by the non-Helm user to the target resource object, then this change is not within the non-Helm user's change permission scope. For another example, the coding validity period is one day, and the permission description information indicates that the coding date of the coding result is 2024.7.1. If the current date is 2024.7.1, then this change is within the non-Helm user's change permission scope. If the current date is 2024.7.2, then this change is not within the non-Helm user's change permission scope.
[0095] In this way, when an authorized non-Helm user changes the target resource object, he can encode the permission description information of this change according to the preset encoding method agreed with the Webhook, and add the encoding result to the API request for changing the target resource object in the form of an annotation. Subsequently, the target resource object can be quickly changed and the application can be repaired through the permission check of the Webhook.
[0096] In addition, in actual applications, some API requests that match resource change configuration information may come from K8s's built-in system users. These users are usually used to maintain resource logic status, etc. They are robot users that run the logic inside K8s. In order for the relevant resources to be processed normally by K8s, these user names need to be allowed. This is the case 3 below.
[0097] In case 3, the authorized change user may also include each system built-in user of K8s. Webhook may also compare the request user information of the API request with the user information of each system built-in user to determine whether the API request comes from any system built-in user. If it is determined that the API request comes from any system built-in user, an indication of responding to the API request may be sent, where the comparison is successful if the request user information of the API request meets the user naming rules of any system built-in user.
[0098] In step 206, the webhook sends an indication of whether to respond to the API request based on the result of determining whether the API request comes from an authorized change user of the target resource object.
[0099] In actual applications, the above situations 1-3 can be combined arbitrarily. If the Webhook determines that the API request comes from an authorized Helm user, an authorized non-Helm user, or any system built-in user, it can send an instruction to respond to the API request. If it determines that the API request does not come from an authorized Helm user, an authorized non-Helm user, or any system built-in user, it can send an instruction to reject the API request. At this time, you can also include the rejection reason, such as you are not an authorized Helm user, you are not an authorized kubectl user, the number of changes has exceeded the upper limit for today, etc.
[0100] In step 207, the API Server processes the API request based on the instruction sent by the webhook.
[0101] If the indication is to respond to an API request, subsequent processing may be performed to change the target resource object, thereby upgrading the corresponding application; if the indication is to reject the API request, the API request may be directly rejected.
[0102] In this way, the API request for changing the target resource object can determine whether the API request comes from the authorized change user of the target resource object, ensuring that only the authorized change user can change the target resource object, reducing the probability of errors when changing resources and the probability of application problems.
[0103] The scheme of the embodiment of the present application is introduced below in conjunction with specific embodiments.
[0104] In the embodiment of the present application, the feature of Helm that allows the user name to be specified when submitting resources to K8s, as well as the convention of labeling resource objects in the Helm Chart with Helm-related labels, is mainly utilized. By building a specific user name into the secondary developed Helm, the user who uses the secondary developed Helm is the authorized Helm user. Subsequently, each time a version is released through the secondary developed Helm, the resource change request submitted to K8s will carry a specific user name. Then, in combination with the supporting verification webhook (i.e., Validating Webhook), the resource change request is verified for resources with relevant labels, mainly verifying whether the user name that issued the request is a specific user name, thereby distinguishing whether the resource change request comes from the secondary developed Helm, and releasing the resource change request from the secondary developed Helm.
[0105] First, we'll explain how to create a specific user name and the permissions that the specific user name should have.
[0106] Helm provides a parameter --kube-as-user to specify the user name when submitting application releases. This parameter belongs to the category of global flags, so all Helm subcommands will inherit this parameter, including helminstall, helm upgrade, etc.
[0107] In order to make the specific user name more universal and not tied to a specific user, the specific user name can be a Kubernetes service account ServiceAccount class user name to serve the purpose of a robot user. Considering that HelmChart may create resources in any namespace or create cluster-level resources, the specific user name of the ServiceAccount class can also have cluster administrator permissions.
[0108] Next, we will show you how to build a specific user name into Helm for secondary development.
[0109] There are many ways to build a specific user name into the Helm for secondary development. One method provided in the embodiment of the present application is as follows:
[0110] After the Helm command line completes parameter collection, check the specified user name parameter before further registering or calling subcommands;
[0111] If it is found that the username of the --kube-as-user parameter has been provided by the parameter, no processing is done; otherwise, the specific username of the ServiceAccoun class is set to the username of the --kube-as-user parameter.
[0112] This logic allows users to use the --kube-as-user parameter originally provided by Helm when using the secondary development of Helm, to support users' compatibility with traditional Helm Chart usage.
[0113] In addition to the --kube-as-user parameter, Helm also provides the --kube-as-group parameter, which can also be used to provide user information when submitting K8s API requests, that is, the group to which the user belongs. And the group information to which the user belongs can also be extracted in the verification webhook to verify which user group the request comes from. That is, the above-mentioned specific user name can also be a specific user group name. And when the specific user name is replaced with the specific user group name, the other processing procedures are similar.
[0114] After that, bind the specific user name and the permissions of the specific user name in K8s.
[0115] Here, the specific user name is a ServiceAccount, and the permissions of the specific user name can be granted through the cluster role binding ClusterRoleBinding. In order to make the specific user name known to as few technical personnel or management personnel as possible, the creation of ServiceAccount and ClusterRoleBinding can be included in the subcommand of Helm in secondary development, that is, using the subcommand of Helm, apply to K8s from the helm side to create ServiceAccount and the corresponding ClusterRoleBinding. This subcommand always attempts to create ServiceAccount and ClusterRoleBinding resources, and ignores existing errors until the creation is successful.
[0116] Different from the aforementioned method of specifying built-in user names for scenarios where no user name is specified, when you need to create a ServiceAccount and ClusterRoleBinding, you generally need to use the cluster administrator user, that is, Kubernetes-admin. This user name is generally read by Helm based on the environment information when the user name is not specified through parameters or environment variables. In order to allow the Kubernetes-admin user to be read normally, the subcommands used to create ServiceAccount and ClusterRoleBinding can set the user name to empty.
[0117] To reduce complexity, the ServiceAccount and ClusterRoleBinding to be created can be composed in the form of a golang language byte array, which will be organized in a Chart Template way and then created as parameters of the helminstall subcommand.
[0118] In addition, packaging the creation of ServiceAccount and ClusterRoleBinding through the secondary developed Helm subcommand is only one of the feasible methods. Of course, there are similar methods. For example, when it is determined that a specific user name is to be used to call K8s APISercer to change resources, pre-processing is performed to confirm whether ServiceAccount and ClusterRoleBinding exist. If it is confirmed that they do not exist, ServiceAccount and ClusterRoleBinding are created. In other words, there is no need to create ServiceAccount and ClusterRoleBinding in advance, and ServiceAccount and ClusterRoleBinding are created when a specific user name is used.
[0119] Finally, let’s introduce the verification webhook.
[0120] After having the above secondary development Helm, you also need a matching verification webhook. For wehbook:
[0121] Because it is impossible to predict what types of resources may be included in the Helm Chart, for each webhook, the APIServer will match resources using the wildcard rules allowed by Kubernetes ValidatingWebhookConfiguration, including apiGroups, apiVersions, and resources that specify resources in a wildcard manner, that is, describing what kind of resource objects need to be matched;
[0122] Because the addition, deletion and modification of related resources are all completed through Helm, the API Server configures CREATE, UPDATE, and DELETE as the matching rules of operations for each webhook, that is, describing which operations on resource objects need to be matched;
[0123] In order to match all possible API requests to create, modify, or delete Helm Charts, the namespace filter of the webhook can be set to empty to match requests occurring in all namespaces, that is, to describe which resource objects in which namespaces need to be matched;
[0124] In order to distinguish which Helm Charts expect Helm to be responsible for subsequent changes and releases, and which Helm Charts allow non-Helm methods to make changes and releases, the webhook will configure an object filter to filter resources with specific labels, that is, it is considered that resources with these labels are expected to be fully responsible for subsequent changes and releases by Helm.
[0125] The above-mentioned information can be configured in ValidatingWebhookConfiguration, and a resource change configuration information is formed after the configuration is completed. ValidatingWebhookConfiguration describes, for K8sAPIServer, what conditions must be met for API requests to be forwarded to the webhook for filtering. The object filter objectSelector of ValidatingWebhookConfiguration is used to list label matching rules, such as the "managed-by" rule, which is inherent in the Helm convention, and the specific value is "Helm" under Helm V3; the "all-in-helm" rule is required to be added in the embodiment of the present application to distinguish the Chart that is expected to be subsequently released and changed by Helm. Label rules. Here, it is only necessary to state that such a label is required for distinguishing and matching. As for the key-value pair content of the specific label, it can be set according to actual needs, and will not be repeated here.
[0126] In the general enterprise release process, if an emergency problem occurs and the application needs to be repaired, there is often a requirement for the response speed of emergency processing. Release process control only through helm may not be able to handle it in a timely and effective manner in an emergency. On the one hand, it takes extra time to write a new helm chart package. On the other hand, the transfer of the helmchart package from the development environment to the production environment may also encounter the release process constraints in general enterprises, resulting in a longer processing time.
[0127] To this end, the embodiment of the present application designs an emergency processing mechanism for authorization coding based on webhook plus secondary development of helm. Its design logic is as follows:
[0128] Allows application operators or system administrators to add a specific annotation to the K8s resource objects generated after submission through helm chat. For example, relevant personnel can add annotations through kubectl or the client (annotation annotations are part of the metadata of K8s resource objects, and all resources can add annotation annotations in their metadata).
[0129] An annotation is essentially a string key-value pair, where the key part can be hard-coded by the developer when compiling the webhook, or allowed to be injected externally through environment variables, startup parameters, etc.; while the value part of the annotation can be encoded. The encoding information is generated by a sufficiently long random seed group, and the encoding information (i.e. the above-mentioned permission description information) at least includes: a) which specific authorized operator issued the code; b) the encoding date; c) the serial number of the code on the day of issuance; d) the code obfuscation value and the checksum value.
[0130] The coding information can be generated by a specific command line issuing tool (i.e. the above-mentioned authorization code issuing tool), and different issuing tools can be distributed to different specific operators with different permissions. The issuing tool and webhook use the same random seed, so that the code issued by the issuing tool can be decoded and verified by the webhook, and the part contained in the above-mentioned coding information can be parsed out from it.
[0131] After the webhook decodes the coded information, it uses the operator information in the coded information, such as ID, for identity authentication, to check whether the operator is registered and has the authority to operate the current resource; the coded date and coded serial number information in the coded information are used to check the validity of the code. For example, the code on June 18th is invalid on June 19th. On June 19th, if the code with serial number 3 has been used, the code with serial number less than 3 is invalid. When the webhook parses and verifies, the relevant operator is allowed to process the current resource. Otherwise, it is considered an illegal modification and the modification request is rejected.
[0132] In addition, because webhook only verifies the annotation information in the metadata of resource objects and does not erase the code after verification, different administrators with operation permissions can view the annotations added to resource objects for emergency processing before making new emergency processing. By decoding the annotations in the annotations through the command line issuance tool, you can know which person with operation permissions made an emergency processing last time. This facilitates communication between different operators and records and reviews of the emergency matters handled.
[0133] In addition, in addition to the aforementioned specific user names built into the secondary development Helm and the authorized user information injected into the annotations by encoding, user names prefixed with "system:serviceaccount:kube-system:" are also allowed. This is because such requests come from the ServiceAccount users of the relevant K8s components under the kube-system namespace. Such requests are usually used to maintain the logical status of resources, etc., and belong to robot users who run the internal logic of K8s. In order for the relevant resources to be processed normally by K8s, API requests with such user names can be allowed to pass.
[0134] Based on the same inventive concept, the present application embodiment provides a resource object change control device, please refer to Figure 3 , the device comprises:
[0135] The receiving module 301 is used to receive an application program interface API request, wherein the API request includes resource information;
[0136] A matching module 302, used to match the resource information with each resource change configuration information;
[0137] The determination module 303 is used to determine that the API request is used to request a change to a target resource object corresponding to the resource change configuration information if the resource information successfully matches any resource change configuration information;
[0138] A sending module 304 is used to send the API request to a webhook corresponding to the resource change configuration information, and the webhook determines whether the API request comes from an authorized change user of the target resource object;
[0139] The processing module 305 is used to process the API request based on the indication sent by the webhook of whether to respond to the API request, where the indication is sent by the webhook based on the determination result of whether the API request comes from the authorized change user of the target resource object.
[0140] In some embodiments, the authorized change user includes an authorized Helm user, and
[0141] The webhook compares the request user information of the API request with the user information of the Helm user to determine whether the API request comes from the Helm user;
[0142] If it is determined that the API request comes from the Helm user, an instruction to respond to the API request is sent.
[0143] In some embodiments, the user information of the Helm user is a user name or user group set by the service account ServiceAccount, and the permission corresponding to the user information of the Helm user is the cluster administrator permission of Kubernetes; further comprising:
[0144] The permission module 306 is used to determine whether the user who initiates the API request has the corresponding resource operation permission for the requested resource object before matching the resource information with each resource change configuration information.
[0145] In some embodiments, the user information of the Helm user and the cluster administrator permissions are applied for and created through a Helm subcommand before the Helm user calls the Kubernetes API to change resources for the first time, or the user information of the Helm user and the cluster administrator permissions are applied for and created when the Helm user calls the Kubernetes API to change resources for the first time.
[0146] In some embodiments, the authorized change user includes an authorized non-Helm user, and
[0147] The webhook obtains annotation content corresponding to the target annotation keyword from the resource information, where the annotation content is obtained by encoding the permission description information of the current change according to a preset encoding method;
[0148] If obtained, it is determined that the API request comes from the non-Helm user;
[0149] Decode the obtained annotation content according to the preset decoding method corresponding to the preset encoding method to obtain the permission description information of this change;
[0150] Compare the permission description information with the preset permission range information of the non-Helm user to determine whether the change is within the change permission range of the non-Helm user;
[0151] If it is determined that the change is within the change permission scope of the non-Helm user, an instruction to respond to the API request is sent.
[0152] In some embodiments, the authorized change user includes each system built-in user of Kubernetes, and
[0153] The webhook compares the requesting user information of the API request with the user information of each system built-in user to determine whether the API request comes from any system built-in user;
[0154] If it is determined that the API request comes from any system built-in user, an instruction to respond to the API request is sent.
[0155] In some embodiments, the processing module 305 is specifically used to:
[0156] If the webhook sends an instruction in response to the API request, then based on the API request, the target resource object is changed;
[0157] If the webhook sends an indication of not responding to the API request, the API request is rejected and the reason for the rejection is returned.
[0158] Based on the same inventive concept, the embodiment of the present application provides an electronic device, which can realize the function of the resource object change control device discussed above, please refer to Figure 4 , the device includes a memory 401 , one or more processors 402 , and a bus 403 .
[0159] The memory 401 is used to store computer programs executed by the processor 402. The memory 401 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system and programs required for running the instant messaging function, etc.; the data storage area may store various instant messaging information and operation instruction sets, etc.
[0160] The memory 401 may be a volatile memory, such as a random-access memory (RAM); the memory 401 may also be a non-volatile memory, such as a read-only memory, a flash memory, a hard disk drive (HDD) or a solid-state drive (SSD), or the memory 401 may be any other medium that can be used to carry or store the desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited thereto. The memory 401 may be a combination of the above memories.
[0161] The processor 402 may include one or more central processing units (CPU) or a digital processing unit, etc. The processor 402 is used to implement the resource object change control method in the above embodiment when calling the computer program stored in the memory 401 .
[0162] The specific connection medium between the memory 401 and the processor 402 is not limited in the embodiment of the present application. Figure 4 In the embodiment, the memory 401 and the processor 402 are connected via a bus 403. The bus 403 is connected to the processor 402 via a bus 403. Figure 4 The connections between other components are shown in bold lines, which are only for illustration and are not intended to be limiting. Bus 403 can be divided into address bus, data bus, control bus, etc. For ease of representation, Figure 4 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.
[0163] Based on the same inventive concept, the embodiment of the present application provides a computer-readable storage medium, and the computer program product includes: computer program code, when the computer program code is executed on a computer, the computer executes any of the resource object change control methods discussed above. Since the principle of solving the problem by the above-mentioned computer-readable storage medium is similar to that of the resource object change control method, the implementation of the above-mentioned computer-readable storage medium can refer to the implementation of the method, and the repeated parts will not be repeated.
[0164] Based on the same inventive concept, the embodiment of the present application also provides a computer program product, which includes: computer program code, when the computer program code is run on a computer, the computer executes any of the resource object change control methods discussed above. Since the principle of solving the problem by the above computer program product is similar to that of the resource object change control method, the implementation of the above computer program product can refer to the implementation of the method, and the repeated parts will not be repeated.
[0165] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.
[0166] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0167] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0168] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of user-operated steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for executing on the computer or other programmable device to implement the process. Figure 1 A process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.
[0169] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.
Claims
1. A method for controlling changes to resource objects, characterized in that: include: Receive an application program interface (API) request, wherein the API request includes resource information; Matching the resource information with each resource change configuration information; If the resource information successfully matches any resource change configuration information, determining that the API request is used to request a change to a target resource object corresponding to the resource change configuration information; Sending the API request to a webhook corresponding to the resource change configuration information, and determining by the webhook whether the API request comes from an authorized change user of the target resource object; Processing the API request based on an indication sent by the webhook of whether to respond to the API request, wherein the indication is sent by the webhook based on a result of determining whether the API request comes from an authorized change user of the target resource object.
2. The method according to claim 1, characterized in that The authorized change user includes the authorized Helm user, and The webhook compares the request user information of the API request with the user information of the Helm user to determine whether the API request comes from the Helm user; If it is determined that the API request comes from the Helm user, an instruction to respond to the API request is sent.
3. The method according to claim 2, characterized in that The user information of the Helm user is the user name or user group set by the service account ServiceAccount, and the permission corresponding to the user information of the Helm user is the cluster administrator permission of Kubernetes; Before matching the resource information with each resource change configuration information, the method further includes: Determine whether the user who initiates the API request has the corresponding resource operation authority for the requested resource object.
4. The method according to claim 3, characterized in that The user information of the Helm user and the cluster administrator permissions are applied for and created by the Helm user through the Helm subcommand before the Helm user calls the Kubernetes API to change resources for the first time, or the user information of the Helm user and the cluster administrator permissions are applied for and created when the Helm user calls the Kubernetes API to change resources for the first time.
5. The method according to claim 1, characterized in that The authorized change users include authorized non-Helm users, and The webhook obtains annotation content corresponding to the target annotation keyword from the resource information, where the annotation content is obtained by encoding the permission description information of the current change according to a preset encoding method; If obtained, it is determined that the API request comes from the non-Helm user; Decode the obtained annotation content according to the preset decoding method corresponding to the preset encoding method to obtain the permission description information of this change; Compare the permission description information with the preset permission range information of the non-Helm user to determine whether the change is within the change permission range of the non-Helm user; If it is determined that the change is within the change permission scope of the non-Helm user, an instruction to respond to the API request is sent.
6. The method according to any one of claims 1 to 5, characterized in that: The authorized change users include Kubernetes's built-in system users, as well as The webhook compares the requesting user information of the API request with the user information of each system built-in user to determine whether the API request comes from any system built-in user; If it is determined that the API request comes from any system built-in user, an instruction to respond to the API request is sent.
7. The method according to claim 1, characterized in that Processing the API request based on the indication sent by the webhook of whether to respond to the API request includes: If the webhook sends an instruction in response to the API request, then based on the API request, the target resource object is changed; If the webhook sends an indication of not responding to the API request, the API request is rejected and the reason for the rejection is returned.
8. A resource object change control device, characterized in that: include: A receiving module, used for receiving an application program interface (API) request, wherein the API request includes resource information; A matching module, used for matching the resource information with each resource change configuration information; A determination module, configured to determine that the API request is used to request a change to a target resource object corresponding to the resource change configuration information if the resource information successfully matches any resource change configuration information; A sending module, used for sending the API request to a webhook corresponding to the resource change configuration information, and the webhook determines whether the API request comes from an authorized change user of the target resource object; A processing module is used to process the API request based on an indication sent by the webhook of whether to respond to the API request, wherein the indication is sent by the webhook based on a determination result of whether the API request comes from an authorized change user of the target resource object.
9. The device according to claim 8, characterized in that The authorized change user includes the authorized Helm user, and The webhook compares the request user information of the API request with the user information of the Helm user to determine whether the API request comes from the Helm user; If it is determined that the API request comes from the Helm user, an instruction to respond to the API request is sent.
10. The device according to claim 9, characterized in that The user information of the Helm user is the user name or user group set by the service account ServiceAccount, and the permission corresponding to the user information of the Helm user is the cluster administrator permission of Kubernetes; it also includes: The permission module is used to determine whether the user who initiates the API request has the corresponding resource operation permission for the requested resource object before matching the resource information with each resource change configuration information.
11. The device according to claim 10, characterized in that The user information of the Helm user and the cluster administrator permissions are applied for and created by the Helm user through the Helm subcommand before the Helm user calls the Kubernetes API to change resources for the first time, or the user information of the Helm user and the cluster administrator permissions are applied for and created when the Helm user calls the Kubernetes API to change resources for the first time.
12. The device according to claim 8, characterized in that The authorized change users include authorized non-Helm users, and The webhook obtains annotation content corresponding to the target annotation keyword from the resource information, where the annotation content is obtained by encoding the permission description information of the current change according to a preset encoding method; If obtained, it is determined that the API request comes from the non-Helm user; Decode the obtained annotation content according to the preset decoding method corresponding to the preset encoding method to obtain the permission description information of this change; Compare the permission description information with the preset permission range information of the non-Helm user to determine whether the change is within the change permission range of the non-Helm user; If it is determined that the change is within the change permission scope of the non-Helm user, an instruction to respond to the API request is sent.
13. The device according to any one of claims 8 to 12, characterized in that: The authorized change users include Kubernetes's built-in system users, as well as The webhook compares the requesting user information of the API request with the user information of each system built-in user to determine whether the API request comes from any system built-in user; If it is determined that the API request comes from any system built-in user, an instruction to respond to the API request is sent.
14. The device according to claim 8, characterized in that The processing module is specifically used for: If the webhook sends an instruction in response to the API request, then based on the API request, the target resource object is changed; If the webhook sends an indication of not responding to the API request, the API request is rejected and the reason for the rejection is returned.
15. An electronic device, characterized in that: include: A memory for storing program instructions; A processor is used to call the program instructions stored in the memory, and execute the steps included in any one of claims 1-7 according to the obtained program instructions.
16. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, wherein the computer program includes program instructions, and when the program instructions are executed by a computer, the computer executes the method according to any one of claims 1 to 7.
17. A computer program product, characterized in that The computer program product comprises: a computer program code, and when the computer program code is run on a computer, the computer is enabled to execute the method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Resource object protection method
CN116432218A
Network resource change method and device, equipment and storage medium
CN117176576A
K8s system authority management method and device and readable storage medium
CN117938461A
Techniques for overriding libraries for workloads in a cloud-computing environment
US20230388369A1
