GitHub security Alerts

I really love everything about security and I’m really intrigued by GitHub security tab that is now present on you repository. In your project usually it is disabled by default.

image

Figure 1: GitHub Security tab on your repository

If you enable it you start receiving suggestion based on code that you check in on the repository, as an example, GitHub will scan your npm packages source to find dependencies with libraries that are insecure.

When GitHub found something that require your attention, it will put a nice warning header on your project, so the alert cannot really pass unnoticed.

image

Figure 2: Security alert warning banner

If you go to the security tab you got a detailed list of the analysis, so you can put a remediation plan in motion, or you can simply dismiss if you believe that you can live with them.

image

Figure 3: Summary of security issues for the repository

Clearly you can click on any issue to have a detailed description of the vulnerability, so you can decide if you are going to fix it or simple dismiss because that issue is not relevant to you or you cannot in anyway bypass the problem.

image

Figure 4: Detailed report of security issue

If you noticed in Figure 4, you have also a nice button “Create Automated Security Fix” in the upper right part of the page, this means that not only GitHub is telling me where the vulnerability is, it sometimes can fix the code for me. Pressing the button will simply create a new Pull Request to fix that error, how nice.

image

Figure 5: Pull request with the fix for the security issue

In this specific situation it is simply a vulnerable package that is donwloaded by npm install, the change is simply bumping a library to a version that removed this vulnerability.

Actually GitHub perform a security scan on project dependencies and can present a remediation simply with nice pull requests

Using Pull request is really nice, really in the spirit of GitHub. The overall experience is really nice, the only annoying stuff is that actually the analysis seems to be done on master branch and proposed solution creates pull requests for master branch. While this is perfectly fine, the only problem I have is that, closing that pull request from the UI, it will merge this commit on the master branch, effectively bypassing GitFlow flow.

Since I’m a big fan of command line, I prefer to close that Pull request manually, so I simply issue a fetch, identify the new branch (it has annoying long name Smile) and simply checkout it as an hotfix branch

$ git checkout -b hotfix/0.3.1 remotes/origin/dependabot/npm_and_yarn/CoreBasicSample/src/MyWonderfulApp.Service/UI/tar-2.2.2
Switched to a new branch 'hotfix/0.3.1'
Branch 'hotfix/0.3.1' set up to track remote branch 'dependabot/npm_and_yarn/CoreBasicSample/src/MyWonderfulApp.Service/UI/tar-2.2.2' from 'origin'.

With this commands I simply checkout the remote branch as hotfix/0.3.1, so I can simply issue a git flow hotfix finish and pushing everything back to the repository.

If you have a specific flow for hotfixes, like GitFlow, it is quite easy closing Pull Requests locally, following your process, GitHub will automatically detect that the PR is closed after the push.

Now branch is correctly merged

image

Figure 6: Pull request manually merged.

If you really like this process, you can simply ask GitHub to automatically create pull requests without your intervention. As soon as a security fix is present, a PR will be created.

image

Figure 7: You can ask to receive automated pull request for all vulnerabilities

Et voilà, it is raining pull requests

image

Figure 8: A series of Pull requests made to resolve security risks

This raise another little issue, we have a single PR for each vulnerability, so, if I want to apply all of them in a unique big hotfix, I only need to manually start the hotfix, then fetch all those branches from the repo and finally cherry-pick all the commits. This operation is easy because each Pull Request contains a single commit that fixes a single vulnerability issue. Sequence of command is:

git flow hotifx start 0.3.2
git cherry-pick commit1
git cherry-pick commit2
git cherry-pick commit3
git flow hotfix finish

Final result is an hotfix resulted from cherry-picking of three distinct PR.

image

Figure 9: Three of pull requests were closed using simple cherry-pick

GitHub is really good in understanding that I’ve cherry-picked all commits in yellow from pull requests, because all pull requests were automatically closed after the push.

image

Figure 10: My pull requests are correctly closed even if I cherry-picked all commits manually.

Actually this functionality is really nice, in this simple repository I have really few lines of code but it helped me revealing some npm dependencies with vulnerabilities and, most important, it gave me the solution so I can immediately put a remediation in place.

Gian Maria.

GitHub Actions, second round

After being capable of running build and test in my GitHub action workflow, it is time to experiment with matrix to have the build run on multiple OSes. This can be tricky if you use (like me) some Docker Images (Mongodb, SqlServer). This because when you choose Windows machine, you are using Windows Container services, not standard Docker for Windows. This means that you are not able to run standard Docker container based on linux, but you need to use Windows Container based image.

GitHub actions Windows based machines are running Windows Container, not Linux ones.

This is especially annoying for me because it seems that there is no SQL Server image available for Windows Server 2019.

image

Figure 1: Docker image for Sql Server does not support Windows Server 2019.

Ok, I’m forced to use Windows server 2016, when I really have preferred to use Windows 2019, that has a much better support for containers.

Apart these difficulties, GitHub actions saved my day because it allows me to specify new variables depending on matrix values, thus allowing me to use different container commands for different operating systems, as you can see in Figure 2.

image 

 

Figure 2: Different mssSql and mongContainer variable values depending on operating system.

Thanks to the include directive, I’m able to give different value to job variables, I’m creating two new variables: msssql and mongoContainer, that contain the command line to start MsSql server and MongoDb containers. This is important because, in Windows, the image of MsSql container gaves me problem if I use ‘ instead of “. With include directive I’m able to specify a completely different run command line for different operating system.

This is also fundamental because I need to use two different container images for MsSql, in fact they are different for different operating systems. With linux I can use mcr.microsoft.com/mssql/server:2017-latest-ubuntu, while for Windows 2016 (but not for Windows 2019) I should use microsoft/mssql-server-windows-developer.

Thanks to include: condition, I can change value for jobs variable depending on Matrix combinations

The net result was that my action now runs in both operating systems.

image

Figure 3: Action now runs on both operating systems, I still got error from mongo test because of problem in container in Windows, but this is a different story.

If I want to run my build and test also against .NET Core 3 Rc, I can simply add another value on dotnet matrix, et voilà, now I got 4 different runs of my workflow.

image

Figure 3: Running actions with matrix, cross product between all variables allows me to run action in different combination of operating system and .NET  Core framework.

Thanks to the max-parallel: setting, I ask to the system to run only two build in parallel, but with fail-fast equal to false I’m asking to GitHub to always run all the combination, even if one previous combination fails. This allows me to always have all four actions run, regardless of the outcome of a previous action.

I can also use Exclude to remove some combination from matrix cross product, in Figure 4 I’m excluding running for .NET core 3 on windows machine

SNAGHTML181d66

Figure 4: Excluding a specific combination from matrix combination

This will generate a total of three runs, I will build both version of framework in linux machine, but only 2.2.401 .net Framework on windows machine.

Include and exclude are powerful action configurations, that allows to configure differently the job or completely exclude some matrix combination, allowing for fine grained control on job parallelism.

Everything is really good, but here is some problems that I encountered while using Actions, but it is completely understandable because it is still in beta.

Running on Windows Machine is slower than Linux, I do not know if this is a problem of docker images, but in Figure 5 you can see timing of the action in Linux (red square) and in Windows (blue square). I suspect that Windows machine runs in a much slower hardware. Nevertheless, pay attention at timing, if you are building .NET core, probably linux is the best choice (better container support and faster in GitHub actions). 

SNAGHTML1d0571

Figure 5: Timing running the action in Linux and Windows machine

Another area where actions need improvement is the concept of a partially failing action, like we had for Years in Azure DevOps (TFS). The concept is: when I’m running a series of tests, I do not want the entire action job to stop if one of the test run fails, I want it to be reported failed, continue to the next step, and the entire action should be marked as “partially failing” if one of the job marked with continue-on-error failed. This kind of CI workflow is standard, do not stop the script, just continue and mark the single step as failed.

It is true that GitHub actions have a continue-on-error property, but it simply report the step as succeded even if it fails, this is a real annoying missing feature.

SNAGHTML41e080

Figure 6: Continue-on-error actually mark the step as succeeded even if it fails.

As you can see from Figure 6, step failed (2 test failed), but the it is marked as successful (due to continue-on-error) and the overall execution is green. This is a real missing feature for complex project where you want to execute every steps and visualize which steps failed.

This second wave of test confirmed me that GitHub actions is a powerful build system, but it still need in its early day and need more work to be really usable in complex projects.

Gian Maria.

GitHub Actions Error pushing with workflow modified

After creating a workflow for GitHub Action, if you try to modify the workflow locally then push to GitHub you can incur in strange error.

refusing to allow an integration to create or update .github/workflows/ci.yml

image

Figure 1: Error in pushing to Git Repository

The reason seems to be a different permission in auth token used for authentication, then to solve the problem you need to clear credentials then try again the operation. In Windows you need to use Credential Manager as I described in that old post. Just delete every entry for GitHub, then try to push again, you will be asked again for credentials and then you should be able to push.

image

Figure 2: I got the error, then clear credentials in Credential Manager, finally I was able to push again.

Let me know if you still have the error.

Gian Maria.

First Experience with GitHub Actions

GitHub actions is the new CI/CD system created by GitHub that allows you to build and release your software with a simple workflow defined in YAML file. Actually it is in beta, but you can simply request to be enlisted and your account will be enabled so you can try it in preview.

Actions engine is based on a yaml definition that is stored directly in code, there are lots of predefined actions made by GitHub team as well as custom actions that can be developed by the community.The real power rely on the fact that you can use simply use command line and docker commands, making the creation of a release a simple and smooth process.

Adding a new workflow is really simple, just open the Actions tab of the repository, then ask to create a new worfklow:

image

Figure 1: Create new workflow for GitHub action directly from repository page.

This will simply create a new yml file in a directory called .github and you can immediately start editing the build. The syntax is really simple and it aims to simplicity rather than complexity. The vast majority of tasks can be simple accomplished inserting command line arguments.

My first impression is that the strongest point of GitHub actions is simplicity and easy to use.

Here is the first part of workflow definition:

name: NStore CI

on: [push]

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        dotnet: [ '2.2.401', '3.0.100-preview9-014004'] 
        os: ['ubuntu-latest']
    name: Build for .NET ${{ matrix.dotnet }}
    steps:

You can find complete workflow syntax at this page, but here is the explanation of my workflow. First of all on: [push] directive asks for continuous integration (run action for each push), then a list of jobs follows.

First and only job for this example is called build and it could run on different operating system. This is a nice feature of actions called matrix: you can define array of values and use those arrays in workflow definition to have it run multiple time, once for each parameter combination. Array of values are defined inside the strategy.matrix section, where I defined two distinct set of parameters, dotnet (version of dotnet core used to build) and os (type of machine where my action should be run). For this example I’m going to use only framework version as matrix value.

Runs-on step define OS, for this example I’m using ubuntu-latest. Finally I give a name to the job: Build for .NET following the actual version of matrix.dotnet value. When I push the code I can verify that two distinct jobs are scheduled.

image

Figure 2: Two distinct job where scheduled, one for each matrix version.

This is a really nice feature because we can specify a single workflow and have GitHub action engine run it with different configuration.

Thanks to Matrix configuration a single job can be run for many different combination of input parameters.

A job is simply composed by different steps, for my solution, I wants only to build my solution and run some tests agains Microsoft Sql Server and MongoDb

steps:     
    - uses: actions/checkout@v1
    
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: ${{ matrix.dotnet }}
      
    - name: Build with dotnet
      run: dotnet build src/NStore.sln --configuration Release
    
    - name: Start Docker for MSSSql
      run: docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=sqlPw3$secure' -e 'MSSQL_PID=Developer' -p 1433:1433 --name msssql -d mcr.microsoft.com/mssql/server:2017-latest-ubuntu
      
    - name: Start Docker for Mongodb
      run: docker run -d -p 27017:27017 mongo
      
    - name: Dump mssql docker logs
      run: docker logs msssql
      
    - name: Run Tests - Core
      run: dotnet test src/NStore.Core.Tests/NStore.Core.Tests.csproj --configuration Release --no-build

    - name: Run Tests - Domain
      run: dotnet test src/NStore.Domain.Tests/NStore.Domain.Tests.csproj --configuration Release --no-build
    
    - name: Run Tests - MongoDb
      env:
        NSTORE_MONGODB: mongodb://localhost/nstore
      run: dotnet test src/NStore.Persistence.Mongo.Tests/NStore.Persistence.Mongo.Tests.csproj --configuration Release --no-build
           
    - name: Run Tests - MsSql
      env:
        NSTORE_MSSQL: Server=localhost;user id=sa;password=sqlPw3$secure
      run: dotnet test src/NStore.Persistence.MsSql.Tests/NStore.Persistence.MsSql.Tests.csproj --configuration Release --no-build
    
    - name: Dump mssql docker logs after tests
      run: docker logs msssql
      
    - name: Run Tests - Sql Lite
      run: dotnet test src/NStore.Persistence.Sqlite.Tests/NStore.Persistence.Sqlite.Tests.csproj --configuration Release --no-build 

Workflows starts with actions/checkout@v1 a really standard action that simply clone and checkout the code, it is followed by another action that ensure that a specific version of .NET core SDK is installed and configured in the system. It is declared with the syntax uses: actions/setup-dotnet@v1 and allows me to use a specific version of .NET core; this action supports parameters, and is followed by a with: section used to pass parameters. This is another strong point of GitHub actions, it is really simple to declare and use actions, there is no need to install or reference anything, just reference the action in the right repository and the game is done.

The rest of the repository is a series of steps composed only by a name and a command line instruction. This allows me to simply issue dotnet command to restore, build, test my solution.

Another cool aspect of Actions is that Docker is available inside the machine, this allows me to run a couple of containers: SQL Server and MongoDb, to run my tests during a build. This is super cool, because it allows me to use Docker to create all prerequisites that I need for my build.

Having Docker inside the machine that runs actions is a real blessing because it allows to run integration tests.

My first impression is quite positive, with just a bunch of Yaml code I was able to create a workflow to build and run tests for my project (I spent a quite good amount of time to have MsSql container work, but this is another story).

Another good aspect of Actions is the ability to see real-time log of your run directly from a browser, without the need of installing anything.

A final real nice aspect of Actions is that they are defined by conventions inside a special folder .github/workflows; I’ve developed this build in a fork of the original project, then I issued a pull request and when the pull request was accepted, this new workflows appears in the original repository.

image

Figure 3: After pull request was merged, immediately the workflow is up and running on target repository..

Clearly this is still a beta and there are still part that should be improved. First of all, if a test run fails, the build is marked as failed and you need to look at test logs to understand which tests failed.

image

Figure 3: Build failed, but to understand why it failed you need to check the logs.

This is the reason why I included a distinct test step for each test assembly, instead of a simple dotnet run on the entire solution. Using this little trick I can at least understand which test run failed.

image

Figure 4: Action run result, each failed step is marked with a red cross

Clicking on failed step, you can find the output log of the step, needed to understand which tests failed and why. For those of you used to Azure DevOps pipeline, you will surely miss the nice Test Result page, but I’m expecting GitHub actions to close the gap in this area.

image

Figure 5: Action step run detail.

Another problem I found (but I need to investigate more) is that docker seems not to be available on MacOS Machine. If I run previous build on MacOS I got a docker command not found.

You only need to enlist in beta and start playing with Actions, you will surely find a good use for them.

Gian Maria.