|
1 |
| ---- |
2 |
| -title: '3. Implement auto-scaling in template' |
3 |
| -layout: default |
4 |
| -nav_order: 3 |
5 |
| -parent: 'Exercise 05: Fix a performance issue using GitHub Copilot' |
6 |
| ---- |
7 |
| - |
8 |
| -# Task 03 - Implement auto-scaling in the template (20 minutes) |
9 |
| - |
10 |
| -## Introduction |
11 |
| - |
12 |
| -The Munson's Pickles and Preserves Team Messaging System is up and running! They even have a proper Git Flow to protect against unintended changes to the main branch, and are recording application telemetry into App Insights. Before we are truly production-ready, though, there is one topic we have to cover: security. |
13 |
| - |
14 |
| -One good DevOps practice is to enable protections against code-level vulnerabilities, and GitHub provides a number of useful features in this area. First, there are Issues, which allow developers or users to open 'tickets' indicating bugs to be fixed or potential vulnerabilities. If your organization prefers security flaws to be reported in a location other than GitHub, you have the option to provide a custom Security policy which describes the process for reporting. |
15 |
| - |
16 |
| -In addition to these manual processes, GitHub also provides automated tools for scanning code for common errors. In this task, you will utilize the built-in Dependabot, which provides alerts if your repository contains libraries, packages, or external dependencies with known vulnerabilities. You will also set up a workflow with CodeQL, which can scan your source code for common coding errors or basic security flaws. This will help to ensure that the Team Messaging System contains code without any known vulnerabilities. |
17 |
| - |
18 |
| -## Description |
19 |
| - |
20 |
| -In this task, you will improve the security of your repository using some of GitHub's built-in tools. |
21 |
| - |
22 |
| -1. Ask GitHub Copilot, "What do I need in a GitHub repository's security file?" |
23 |
| -2. Find the repository's Security policy. If there is an existing policy, make an edit and merge your change back into the main branch. Otherwise, create a policy using the template provided. GitHub Security policies are Markdown documents that indicate the preferred way to report security vulnerabilities for the repository. |
24 |
| -3. Ask GitHub Copilot, "How do I enable Dependabot alerts on a GitHub repository?" Then, enable Dependabot alerts for the repository. Dependabot is an automated tool that creates a pull request when any dependencies in the code base has a known vulnerability. |
25 |
| -4. Ask GitHub Copilot, "How do I create a code scanning workflow in a GitHub repository?" After that, set up and run a Code scanning workflow for the repository using GitHub's 'CodeQL Analysis.' This workflow can run either on each pull request or on a schedule, and it checks your code for common vulnerabilities or errors. |
26 |
| -5. Ask GitHub Copilot, "How can I view the results of a CodeQL analysis in GitHub?" Then, navigate to the results. |
27 |
| -6. You should have one security issue from code scanning. Review the security issue and read up on how to correct the issue. Create a new GitHub Issue and feature branch. Then, resolve the issue, using GitHub Copilot to assist you. |
28 |
| - |
29 |
| -## Success Criteria |
30 |
| - |
31 |
| -- In GitHub, you should be able to view the 'closed' pull request which either created or updated the Security policy (SECURITY.md file). |
32 |
| -- Additionally, you should be able to view a new 'open' pull request created by Dependabot requesting an update of a dependency. |
33 |
| -- Finally, you should be able to view the results of the CodeQL Analysis in the Security tab. |
34 |
| - |
35 |
| -## Learning Resources |
36 |
| - |
37 |
| -- [Learn more about adding a security policy to your repository](https://docs.github.com/en/github/managing-security-vulnerabilities/adding-a-security-policy-to-your-repository). |
38 |
| -- [Learn more about Dependabot and vulnerable dependencies](https://docs.github.com/en/github/managing-security-vulnerabilities/managing-vulnerabilities-in-your-projects-dependencies). |
39 |
| -- [Learn more about automated code scanning and understanding results](https://docs.github.com/en/github/finding-security-vulnerabilities-and-errors-in-your-code). |
40 |
| - |
41 |
| -## Tips |
42 |
| - |
43 |
| -- If you are stuck, check out the 'Security' tab of your repository on GitHub. |
44 |
| - |
45 |
| -## Solution |
46 |
| - |
47 |
| -<details markdown="block"> |
48 |
| -<summary>Expand this section to view the solution</summary> |
49 |
| - |
50 |
| -1. Select **Settings** in your repo, then **Code security and analysis**. Select **Enable** on "Dependabot alerts" and "Dependabot security updates." |
51 |
| - |
52 |
| -  |
53 |
| - |
54 |
| - **Note** This will also automatically enable "Dependency graph." |
55 |
| -2. Navigate to [https://github.com/electron/electron/blob/main/SECURITY.md](https://github.com/electron/electron/blob/main/SECURITY.md) for information about security policies. This is an example of a sample security policy that you could use for this exercise. |
56 |
| -3. In your GitHub repo, select **Security**, **Policy**, and **Start setup** |
57 |
| - |
58 |
| -  |
59 |
| - |
60 |
| -4. Paste the security policy into the Markdown file (you can overwrite what is there now) and update it for the Munson's Pickles and Preserves Team Messaging System and the GitHub repo your code is in. Then, commit the changes to the main branch. |
61 |
| - |
62 |
| -  |
63 |
| - |
64 |
| -5. Next, we need to enable CodeQL. Select **Settings** and then **Code security and analysis**. |
65 |
| -6. Scroll down if needed and select **Set up** in "Code scanning" for "CodeQL analysis." |
66 |
| - |
67 |
| -  |
68 |
| - |
69 |
| -7. If you select "Default", the code scan will immediately be run. For this exercise, select **Advanced**. |
70 |
| - |
71 |
| -  |
72 |
| - |
73 |
| -8. By choosing the advanced option, you can see the YAML for the pipeline that actually performs the code check. We don't need to make any changes here, but it's something you should be familiar with. An easy change to make in this file would be if you want to adjust the schedule of when the scan runs. |
74 |
| - |
75 |
| -  |
76 |
| - |
77 |
| - After you've reviewed the YAML, commit the change to main. |
78 |
| - |
79 |
| -  |
80 |
| - |
81 |
| -9. After you've committed the change, select **Actions** and you should see your CodeQL Scan workflow running. |
82 |
| - |
83 |
| -  |
84 |
| - |
85 |
| -10. After about 5 minutes, you should see the workflow has completed. |
86 |
| - |
87 |
| -  |
88 |
| - |
89 |
| -11. After it's complete, go back to **Settings** and **Code security and analysis**. Then, select the ellipsis **...** next to the "Set up" menu. From the ellipsis dropdown, explore each of the first two options: "View last scan log" and "View Code Scanning alerts." You will find one High-risk vulnerability around arbitrary file access during archive extraction. |
90 |
| - |
91 |
| - {: .note } |
92 |
| - > This page will still show "Set up" because we chose the Advanced option instead of Basic. |
93 |
| -
|
94 |
| -  |
95 |
| - |
96 |
| -12. Select the alert and then choose "Show more" to view details on the security issue. It turns out that an attacker could traverse to an arbitrary directory based on the way the MP&P staff wrote this function. In order to correct the function and prevent a directory traversal attack, replace `WriteToDirectory()` with the following code: |
97 |
| - |
98 |
| - ```csharp |
99 |
| - public static void WriteToDirectory(ZipArchiveEntry entry, string destDirectory) |
100 |
| - { |
101 |
| - string destFileName = Path.GetFullPath(Path.Combine(destDirectory, entry.FullName)); |
102 |
| - string fullDestDirPath = Path.GetFullPath(destDirectory + Path.DirectorySeparatorChar); |
103 |
| - if (!destFileName.StartsWith(fullDestDirPath)) { |
104 |
| - throw new System.InvalidOperationException("Entry is outside the target dir: " + destFileName); |
105 |
| - } |
106 |
| - entry.ExtractToFile(destFileName); |
107 |
| - } |
108 |
| - ``` |
109 |
| - |
110 |
| - Commit the code and create a pull request to the main branch. You should then see a CodeQL scan for the pull request, and after it succeeds, complete the pull request. Then, return to the code scanning results view and confirm that no issues remain on the list. |
111 |
| - |
112 |
| -</details> |
| 1 | +--- |
| 2 | +title: '3. Add support for Azure Cache for Redis' |
| 3 | +layout: default |
| 4 | +nav_order: 3 |
| 5 | +parent: 'Exercise 05: Fix a performance issue using GitHub Copilot' |
| 6 | +--- |
| 7 | + |
| 8 | +# Task 03 - Add support for Azure Cache for Redis (30 minutes) |
| 9 | + |
| 10 | +## Introduction |
| 11 | + |
| 12 | +The Munson's Pickles and Preserves development team has learned a considerable amount from you. They are running into one more problem they would like your assistance with. At certain points of the day, users overwhelm the Team Messaging System, causing performance issues at critical points in the day. They would like your assistance in deploying a caching tier to reduce some of the load on their databases. They would like to make this deployment in a way that supports their new infrastructure as code mode of operation. Their developers will handle making code changes to support this new caching layer. |
| 13 | + |
| 14 | +{: .note } |
| 15 | +> The actual Team Messaging System that we have used in this demo operates on a built-in memory cache on the web server. For that reason, this task is more in line with a standard database-driven web application than the one you have worked with so far. |
| 16 | +
|
| 17 | +## Description |
| 18 | + |
| 19 | +In this task, you will update the infrastructure as code Bicep script and create an instance of Azure Cache for Redis for the Team Messaging System web application. |
| 20 | + |
| 21 | +1. Create an Issue and a feature branch associated with your infrastructure as code changes. |
| 22 | +2. Use GitHub Copilot to add support for the Basic SKU (C0) of Azure Cache for Redis. |
| 23 | +3. Check in your changes and have GitHub Copilot assist on creating the commit message. |
| 24 | +4. Create a pull request to the main branch, using GitHub Copilot Enterprise to create a pull request summary. Have another team member review and approve your code before you merge the code into the main branch. |
| 25 | +5. Ensure that the deployment completes (including a manual execution of the Azure Bicep workflow) and review your resource group in the Azure Portal to ensure that Azure Cache for Redis exists. |
| 26 | + |
| 27 | +## Success Criteria |
| 28 | + |
| 29 | +- In GitHub, you should see a successful execution of the **Azure Bicep** GitHub Actions workflow. |
| 30 | +- In the Azure Portal, you should see your Azure Cache for Redis instance. |
| 31 | + |
| 32 | +## Learning Resources |
| 33 | + |
| 34 | +- [Quickstart: Create an Azure Cache for Redis using Bicep](https://learn.microsoft.com/azure/azure-cache-for-redis/cache-redis-cache-bicep-provision?tabs=CLI). |
| 35 | + |
| 36 | +## Tips |
| 37 | + |
| 38 | +- If you receive an error message like the following: |
| 39 | + |
| 40 | + > The subscription is not registered to use namespace 'Microsoft.Cache'. See https://aka.ms/rps-not-found for how to register subscriptions. |
| 41 | +
|
| 42 | + Be sure to enable the **Microsoft.Cache** namespace [in the Azure Portal](https://learn.microsoft.com/azure/azure-resource-manager/troubleshooting/error-register-resource-provider?tabs=azure-portal). |
| 43 | + |
| 44 | +## Solution |
| 45 | + |
| 46 | +<details markdown="block"> |
| 47 | +<summary>Expand this section to view the solution</summary> |
| 48 | + |
| 49 | +- The final Bicep script is [in the solution folder]((https://github.com/microsoft/TechExcel-Accelerate-developer-productivity-with-GitHub-Copilot-and-Dev-Box/blob/main/Solution/Exercise-05/Task-3/main.bicep)). Before looking at this script, try to use GitHub Copilot to generate the changes to your existing `/src/InfrastructureAsCode/main.bicep` file. You may wish to use a prompt such as "Please add in a Basic C0 instance of Azure Cache for Redis." Then, you may wish to compare what GitHub Copilot generated. |
| 50 | + |
| 51 | +</details> |
0 commit comments