How to Use CodeWhisperer to Identify Issues and Use Suggestions to Improve Code Security in your IDE
Tags: aws, codewhisper, codesecurity, bestpractice
Published: 2024-01-17 21:12:34

<!-- wp:paragraph -->
<p>It is a common practice adopted by beginner developers to start writing the code without thinking about other things. This practice continues to grow when there is lack of guidance from expert resources. A similar situation has been stated by a fellow developer who used to code blindly without taking security concerns into consideration. As the time passed, he got to know the advantages of considering security concerns at the start of the project. It can definitely save a lot of time and can reduce frustration up to a significant level.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You will be happy to know that the security which we consider at the early development phases goes by the name <a href="https://aws.amazon.com/what-is/devsecops/">DevSecOps.</a> An AWS approach which helps in identifying and improving security issues at an early stage of development is what DevSecOps is created for. Instead of wasting a ton of money on security and reworking, it is a better idea to consider all the crucial security issues at the very start.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Importance of CodeWhisperer</strong><strong></strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The mention of <a href="https://aws.amazon.com/codewhisperer/">CodeWhisperer</a> always comes when we talk about coding in our respective IDEs. CodeWhisperer possesses scanning capabilities which help us in identifying security issues while we code in our IDEs.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The most important features that CodeWhisperer has to offer are as follows:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li><strong>Providing Suggestions</strong></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Whenever you code within your IDE CodeWhisperer starts offering refactoring suggestions which eventually improves the code security and helps in avoiding last-minute panic situations if there is a change in your code.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li><strong>Increasing Productivity</strong></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>CodeWhisperer is capable of understanding English language comments. Due to this understanding, it generates real time code suggestions which increase the developer’s productivity. All this magic is made possible due to the presence of Large Language Models. These LLMs are trained on available codes in which potential security vulnerabilities are identified and removed during the training process.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>AWS Toolkit for JetBrains</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There are security scans available in JetBrains for Python, Java, JavaScript, TypeScript, and VS code as well. <a href="https://aws.amazon.com/codeguru/">AWS CodeGuru Security</a> is another amazing security tool that takes the assistance from detection engine. Detector Library is an important component of detection engine which is responsible in making you understand why your code was highlighted by CodeWhisperer and whether an action is to be taken or not. Along with this the relationship and understanding the paths through code is made possible through a machine learning model that uses neural networks and regression.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Installing AWS Toolkit for JetBrains</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In order to install the AWS toolkit for JetBrains you will have to start by installing PyCharm first and then configuring it to use AWS Toolkit. Next you will be needing an AWS Builder ID which will authenticate the extension with AWS. You must have a weak hashing algorithm, once you type this code in PyCharm, the CodeWhisperer will label it as a weak hashing algorithm and start giving suggestions to improve the code security.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":33127,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image size-full"><img src="https://sudoconsultants.com/wp-content/uploads/2024/01/image-8.png" alt="" class="wp-image-33127"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>Prerequisites Needed</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To experience a hurdle-free procedure, it is recommended to have the following prerequisites installed.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>PyCharm</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Python (Version 3.10 or latest)</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Pip3</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Step-by-Step Procedure</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Providing step-by-step guidance for installing AWS Toolkit for JetBrains is easier to follow for the users. In a nutshell, the procedure goes as follows; you install the latest version of AWS toolkit, make a new project in PyCharm, do a signup using AWS Builder ID, and authenticate the toolkit with AWS using this ID. In case you don’t have AWS Builder ID, you can use the details of AWS IAM Identity Center or simple AWS IAM credentials.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong><u>Installing AWS Toolkit Plugin</u></strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Install PyCharm (Community Version – Preferred)</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Choose <strong><em>Plugins</em></strong> option present on the left pane</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Search for <strong><em>AWS Toolkit</em></strong> in the search box</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select it and click <strong><em>Install</em></strong></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:image {"id":33129,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image size-full"><img src="https://sudoconsultants.com/wp-content/uploads/2024/01/image-10.png" alt="" class="wp-image-33129"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong><u>Creating a new PyCharm Project</u></strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Open PyCharm IDE</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Go to <strong><em>Menu Bar</em></strong> and choose <strong><em>File</em></strong></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select the option of <strong><em>New Project</em></strong> and then <strong><em>Create</em></strong></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong><u>Authenticating CodeWhisperer with AWS</u></strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Now that you have created the project, search for the <strong><em>AWS icon</em></strong> in the left pane and click on it</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:image {"id":33130,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image size-full"><img src="https://sudoconsultants.com/wp-content/uploads/2024/01/image-11.png" alt="" class="wp-image-33130"/></figure>
<!-- /wp:image -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Choose the <strong><em>Developers Tool</em></strong> tab</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:image {"id":33131,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image size-full"><img src="https://sudoconsultants.com/wp-content/uploads/2024/01/image-12.png" alt="" class="wp-image-33131"/></figure>
<!-- /wp:image -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Under CodeWhisperer option, select <strong><em>Sign in to get started</em></strong></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Choose the option <strong><em>Connect</em></strong> and you will be given a code</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:image {"id":33132,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image size-full"><img src="https://sudoconsultants.com/wp-content/uploads/2024/01/image-13.png" alt="" class="wp-image-33132"/></figure>
<!-- /wp:image -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Select the option <strong><em>Proceed to Browser</em></strong> and you will be redirected to it</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:image {"id":33133,"width":"1134px","height":"auto","sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image size-full is-resized"><img src="https://sudoconsultants.com/wp-content/uploads/2024/01/image-14.png" alt="" class="wp-image-33133" style="width:1134px;height:auto"/></figure>
<!-- /wp:image -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>If the code matches then select <strong><em>Confirm and Continue</em></strong> option</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:image {"id":33134,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image size-full"><img src="https://sudoconsultants.com/wp-content/uploads/2024/01/image-15.png" alt="" class="wp-image-33134"/></figure>
<!-- /wp:image -->

<!-- wp:image {"id":33135,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image size-full"><img src="https://sudoconsultants.com/wp-content/uploads/2024/01/image-16.png" alt="" class="wp-image-33135"/></figure>
<!-- /wp:image -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Enter the email address or select the option <strong><em>Already have AWS Builder ID</em></strong> if you have one</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>After providing your email, you will have to provide your name and then proceed further</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>You will receive a verification code on your provided email. Copy the code and paste it in the provided space and select <strong><em>Verify</em></strong></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>After a successful verification, now is the time to create a password for your account</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Choose <strong><em>Create AWS Builder ID</em></strong> option</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>You will be directed to a page asking you to “Allow AWS Toolkit for JetBrains to access your data?”, choose <strong><em>Allow</em></strong></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>To confirm the authentication, select the AWS icon in the left pane of PyCharm. Under AWS Toolkit window “Connected with AWS Builder ID” should be displayed</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Using CodeWhisperer Security Scans to Identify a Weak Hashing Algorithm</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We will be intentionally using a weak hashing algorithm, SHA-224. The reason for using this algorithm is because CodeWhisperer considers it as a weak algorithm. So instead of using the recommended algorithm, we will prefer using a weaker one.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong><u>Creating a New File in PyCharm Project</u></strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>First you need to create a new file in the previously created project</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Name the file as <strong><em>app.py</em></strong></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Copy the following code snippet in the newly created file (app.py)</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code>import hashlib

import os

salt = os.urandom(8)

password = ‘secret’.encode()

# Noncompliant: potentially weak algorithm used.

derivedkey = hashlib.pbkdf2_hmac('sha224', password, salt, 100000)

derivedkey.hex()</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p><strong><u>Initiating a Security Scan</u></strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>In the AWS Toolkit section, just under the <strong><em>Developer Tools</em></strong> tab click the run button present there</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>This will open <strong><em>CodeWhisperer Security Issues</em></strong> tab confirming that the scan was initiated successfully</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong><u>Interpreting the CodeWhisperer Results</u></strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>After a successful security scan, CodeWhisperer will highlight a particular code row where it has found some issues</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Hover over the highlighted line to know more about its description</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>In this case, the description shows that the hashing algorithm which is used is weak</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Now that you have come across the fact that there lies an issue, how to know whether to take an action or not? For this, you need to use Detector Library which takes assistance from the scanning capabilities of CodeWhisperer. You will be able to gather some additional information.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The additional information includes a truncated output which is an easy target to collision attacks. If you don’t know about a collision attack then allow me to explain. It is basically a way to find another input that produces or yields another identical hash.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>CodeWhisperer Suggestions</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>CodeWhisperer suggestions include code as well as function completion. The suggestions are real-time based, it means the suggestions are automatically generated as you start typing the code. Due to the training of LLM on vast data, the suggestions might be different every time you type your code. Now it is your duty to select the suggestion that appears smart to you.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>CodeWhisperer also gives you an option to replace the entire algorithm. You just need to delete the arguments from the function. The CodeWhisperer suggestion will say to replace the previous algorithm with a new one.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Validating CodeWhisperer Suggestions</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There is no 100% trust when it comes to security vulnerabilities. The suggestions by CodeWhisperer might still contain some vulnerabilities so it is important to cross check them. It is really important to get familiar with the code suggestion before you actually use it in your code. It also depends on the algorithm that you choose. By accepting a certain suggestion you might be reducing the computing as well as the execution speed of the algorithm. It is really important to validate the suggestions generated by CodeWhisperer.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can rerun the scan by choosing <strong><em>Run Security Scan</em></strong> option. A notification will pop up when the scan has been completed successfully with no issues found.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Wrap Up</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>All in all, it was an informative experience to know about the usage process of CodeWhisperer with PyCharm. They both help in scanning the code for potential security threats, getting to know about the potential issues and understanding them. You followed a really simple procedure by accepting the suggestions by CodeWhisperer and then rerunning the scan again in order to validate the suggestions.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Apart from this, CodeWhisperer has the potential to detect other stuff as well including top-ten web application security risks, top 25 most dangerous software weaknesses, insecure use of AWS SDKs and APIs. All this is possible with the help of a Detector Library. It contains references, descriptions as well as examples to provide additional information.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Have you heard about <a href="https://sudoconsultants.com/aws-cloud-native-codepipeline-ci-cd-automation/">CI/CD pipeline</a>? Well, it is an important concept in DevOps which stands for Continuous Integration and Continuous Delivery. By combining CodeWhisperer and CI/CD pipeline you can detect vulnerabilities throughout the delivery process. It is a great practice to search for the security vulnerabilities early in the development process.</p>
<!-- /wp:paragraph -->