{"id":34502,"date":"2022-03-28T08:39:27","date_gmt":"2022-03-28T12:39:27","guid":{"rendered":"https:\/\/centricconsulting.com\/?p=34502"},"modified":"2022-04-12T09:54:31","modified_gmt":"2022-04-12T13:54:31","slug":"the-benefit-of-using-git-pre-commit-hooks","status":"publish","type":"post","link":"https:\/\/centricconsulting.com\/blog\/the-benefit-of-using-git-pre-commit-hooks\/","title":{"rendered":"The Benefit of Using Git Pre-commit Hooks"},"content":{"rendered":"

We walk through using git pre-commit hooks in your git workflow to supercharge your software development lifecycle.<\/h2>\n
\n

Using git pre-commit hooks in your software delivery pipeline helps achieve better quality software with faster time to market. Developers have more access to define additional aspects around the infrastructure needed to run their applications. It is critical to lower the risk of changes completed in development to supply additional confidence and increase velocity.<\/p>\n

This is because implementing git pre-commit hooks is essentially executing local scripts to check for certain quality and policy items prior to pushing the commit. This is one less step you would normally do manually. The number of available checks is extensive and covers multiple programming languages. This simple addition to a Continuous Integration\/Continuous Development<\/a> (CI\/CD) pipeline is one way you can take steps towards \u201cshifting left\u201d with security.<\/p>\n

In this blog, we will talk more about how using git pre-commit hooks is one of the best practices in modern software development<\/a> lifecycles (SDLC).<\/strong> We will also discuss what it means to \u201cshift left\u201d as it applies to committing important files that may expose surface area or otherwise goes against policy.<\/p>\n

Git Pre-commit Hooks, Defined<\/h2>\n

Using git pre-commit hooks is comparable to using linting tools whose purpose is to check for correct syntax and formatting. The tool itself is a set of scripts that are kept along with the git information that is local to your PC. You can use them on multiple languages existing in the same repository. Its design allows for managing the installation and execution of the various hooks available without root access and on an as-needed basis.<\/strong><\/p>\n

While this is very much something that aids in code reviews, it is also an initial part of an end-to-end solution for automated software delivery. With Infrastructure as Code (IaC), any changes in the infrastructure of a project need to consider the downstream effects. In the case of a Kubernetes manifest, this means ensuring you don\u2019t commit something like private key data to the repository.<\/p>\n

Now let\u2019s look at how pre-commit hooks help you shift left.<\/strong><\/p>\n

Why Shifting Left is the Right Direction<\/h2>\n

Shifting left is a practice getting a lot of attention in the software industry. OWASP stated<\/a> shifting left is geared towards keeping a pipeline and its resulting artifacts more secure. Take a Kubernetes manifest file, for example. This configuration file can expose various ports or credentials you should not commit to code. You can check these against policy with tools like git pre-commit hooks, similar to how developers use code linting tools.<\/p>\n

By shifting left, teams can find and prevent issues much earlier in the SDLC process. This method is useful for finding quality, security and policy issues as early as possible.<\/strong> Enforcing these checks prior to reaching the codebase can prevent developers from using offending code in additional feature branches.<\/p>\n

Keeping Things in Check<\/h2>\n

If a developer is not careful, they may inadvertently create more attack surface area for an application due to modifications done to the manifest file. These files, along with other files that hold settings, control much of the infrastructure or permissions used by an application. In many cases, the negative effects of these changes are not found until further down the line. Sometimes, even after being deployed to a production environment.<\/strong><\/p>\n

This increased ability to have more surface area available for miscellaneous attacks is the drawback of giving those who may not have security at the top of mind more control. Using tools such as git pre-commit hooks allows for a quick pass over these critical configuration files to help with security. Injecting a git pre-commit hook into a development workflow can help surface these issues well before they are deployed to your various environments.<\/p>\n

A Look at Git Pre-commit Hooks in Action<\/h2>\n

Prior to using pre-commit hooks, you should choose one of the methods of installation based on your development environment. In this example, we will look at an application that we will run inside a Kubernetes cluster.<\/p>\n

To begin, we will install pre-commit using \u201cpip\u201d with the following command:<\/p>\n

$ pip install pre-commit<\/strong><\/p>\n

To verify it was installed, run the following:<\/p>\n

$ pre-commit \u2013version<\/strong><\/p>\n

An additional file called .pre-commit-config.yaml will be added to your project by running:<\/p>\n

$ pre-commit sample-config > .pre-commit-config.yaml<\/strong><\/p>\n

This basic configuration file has examples of commonly used hooks, but we want to ensure we install the hook that checks for the presence of a private key. Open the .pre-commit-config.yaml<\/strong> file and add the hook at the bottom:<\/p>\n

\"Pre<\/a><\/p>\n

Running pre-commit install results in creating the scripts to add the hooks to your project.<\/p>\n

$ pre-commit install<\/strong><\/p>\n

Once you complete these steps, a commit results in the pre-commit scripts running without any other actions needed by the developer.<\/p>\n

Let us look at an example k8s_manifest.yaml file with a private key we do not want committed to code.\"pre<\/a><\/p>\n

The next command will scan the entire codebase. This ensures all the files are checked against the implemented rules.<\/p>\n

In our example, since there is an RSA Private Key inside the manifest, our hook installed for \u201cdetect-private-key” will force a non-zero exit code.<\/p>\n

$ pre-commit run \u2013all-files -v<\/strong><\/p>\n

\"PRE-GIT<\/a><\/p>\n

Conclusion<\/h2>\n

Looking at this information, you should see the benefits of using git pre-commit hooks in your SDLC process. This simple step towards shifting left will accelerate the goal of achieving better code quality, security and velocity to help decrease time to market.<\/p>\n

Due to their simplicity, savvy teams are using git pre-commit hooks as a quick way to improve their git workflow and supercharge their development.<\/p>\n","protected":false},"excerpt":{"rendered":"

We walk through using git pre-commit hooks in your git workflow to supercharge your software development lifecycle.<\/p>\n","protected":false},"author":63,"featured_media":34514,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_oasis_is_in_workflow":0,"_oasis_original":0,"_oasis_task_priority":"","_relevanssi_hide_post":"","_relevanssi_hide_content":"","_relevanssi_pin_for_all":"","_relevanssi_pin_keywords":"","_relevanssi_unpin_keywords":"","_relevanssi_related_keywords":"","_relevanssi_related_include_ids":"","_relevanssi_related_exclude_ids":"","_relevanssi_related_no_append":"","_relevanssi_related_not_related":"","_relevanssi_related_posts":"","_relevanssi_noindex_reason":"","footnotes":""},"categories":[1],"tags":[18560],"coauthors":[15012],"acf":[],"publishpress_future_action":{"enabled":false,"date":"2024-08-02 09:18:12","action":"change-status","newStatus":"draft","terms":[],"taxonomy":"category"},"_links":{"self":[{"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/posts\/34502"}],"collection":[{"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/comments?post=34502"}],"version-history":[{"count":0,"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/posts\/34502\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/media\/34514"}],"wp:attachment":[{"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/media?parent=34502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/categories?post=34502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/tags?post=34502"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/coauthors?post=34502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}