Skip to content

Commit 8e37e61

Browse files
authored
Git secrets windows and linux (#276)
* Rename deny rules file * Added Linux and Windows install scripts and docs * Reinstated changes
1 parent 732a84c commit 8e37e61

12 files changed

+253
-25
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Setup (Linux/WSL only)
2+
3+
## Setup
4+
5+
Make sure to copy the `nhd-git-secrets` folder into the root of the project repository, and then navigate the terminal to the repo root
6+
7+
* `cd nhsd-git-secrets`
8+
* `cp .gitallowed-base ../.gitallowed`
9+
* `./install-linux.sh`
10+
11+
Next time you do a commit the git secrets hook should be invoked.
12+
13+
### Troubleshooting
14+
15+
You should have 3 new files in the `.git/hooks` folder in the repository. If these are not present, then make sure you have ran the install script and that this script ran successfully.
16+
If you get an output containing:
17+
18+
```bash
19+
[3/5] Adding Git Hooks
20+
./install-linux.sh: line 18: git-secrets: command not found
21+
```
22+
23+
* Run this command anywhere: `export PATH="$HOME/git-secrets/bin":$PATH`
24+
* Then re-run the install script (`./nhsd-git-secrets/install-linux.sh`)
25+
26+
### Custom configuration (per repo / per service team)
27+
28+
* Add individual regex expressions to the existing `repo_root/nhsd-git-secrets-nhsd-rules-deny.txt` file
29+
* Or, create your own file for regex rules and add it as a provider within the [pre-commit script](pre-commit.sh) e.g.:
30+
`./nhsd-git-secrets/git-secrets --add-provider -- cat nhsd-git-secrets/nhsd-rules-deny.txt`
31+
32+
* Add file/dir excludes within the `repo_root/.gitallowed`, e.g. `.*terraform.tfstate.*:*`
33+
34+
* Control full scan vs staged files scan within [pre-commit script](pre-commit.sh) by commenting/uncommenting the mode to run e.g.:
35+
36+
```bash
37+
# Just scan the files changed in this commit
38+
# ./nhsd-git-secrets/git-secrets --pre_commit_hook
39+
40+
# Scan all files within this repo for this commit
41+
./nhsd-git-secrets/git-secrets --scan
42+
```
43+
44+
## Testing and Usage
45+
46+
To test that the hooks have been enabled correctly:
47+
48+
* make sure you have done git add if you have changed anything within git-Secrets
49+
* create a file containing one or more patterns from the `git-secrets/nhsd-rules-deny.txt` file (e.g.: `password = “test”`)
50+
* stage and commit the file
51+
52+
You should see an output similar to: `“[ERROR] Matched one or more prohibited patterns…”`.
53+
54+
**Note** This message may appear differently depending on the tools used.
55+
56+
> If you have a *false-positive* match, and your changes do not contain sensitive credentials then you can add the `--no-verify` flag to the commit command to **skip the checking**.
57+
58+
## Docker version
59+
60+
Alternatively, you might find this [dockerfile](nhsd-git-secrets.dockerfile) convenient, which:
61+
62+
1. Copies your source code into a docker image
63+
1. Downloads latest version of the secret scanner tool
64+
1. Downloads latest regex patterns from software-engineering-quality-framework
65+
1. Runs a scan

tools/nhsd-git-secrets/README-mac-workstation.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Setup (Mac only)
22

3-
Ensure you have the pre-commit framework set up first:
4-
5-
https://pre-commit.com/
3+
Ensure you have the [pre-commit framework](https://pre-commit.com/) set up first.
64

75
## TL;DR
86

@@ -33,33 +31,34 @@ Then:
3331

3432
Next time you do a commit the git secrets hook should be invoked.
3533

36-
# Custom configuration (per repo / per service team)
34+
## Custom configuration (per repo / per service team)
3735

38-
* Add individual regex expressions to nhsd-rules.txt
39-
* Add regex rules files within wrapper.sh e.g.
36+
* Add individual regex expressions to the existing `repo_root/nhsd-git-secrets-nhsd-rules-deny.txt` file
37+
* Or, create your own file for regex rules and add it as a provider within the [pre-commit script](pre-commit.sh) e.g.:
4038

41-
`git secrets --add-provider -- cat git-secrets/nhsd-rules.txt`
39+
`git secrets --add-provider -- cat git-secrets/nhsd-rules-deny.txt`
4240

43-
* Add file/dir excludes within .gitallowed, e.g. `.*terraform.tfstate.*:*`
41+
* Add file/dir excludes within the `repo_root/.gitallowed` file, e.g. `.*terraform.tfstate.*:*`
4442

45-
* Control full scan vs staged files scan within wrapper.sh by commenting/uncommenting the mode to run e.g.:
43+
* Control full scan vs staged files scan within [pre-commit (mac) script](pre-commit-mac.sh) by commenting/uncommenting the mode to run e.g.:
4644

47-
```
45+
```bash
4846
# Just scan the files changed in this commit
4947
# git secrets --pre_commit_hook
5048

5149
# Scan all files within this repo for this commit
5250
git secrets --scan
5351
```
5452

55-
# Testing-a
53+
## Testing-a
5654

5755
* make sure you have done git add if you have changed anything within git-Secrets
5856
* Run: `pre-commit run git-secrets`
5957

60-
# Docker version
58+
## Docker version
6159

6260
Alternatively, you might find this [dockerfile](nhsd-git-secrets.dockerfile) convenient, which:
61+
6362
1. Copies your source code into a docker image
6463
1. Downloads latest version of the secret scanner tool
6564
1. Downloads latest regex patterns from software-engineering-quality-framework
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Setup (Windows only)
2+
3+
## Prerequisites
4+
5+
Ensure you have permissions to exceute powershell scripts. If this is not possible, then you may be able to use WSL instead and setup git-secrets following the [guide for linux](README-linux-workstation.md).
6+
7+
## Setup
8+
9+
Make sure to copy the `nhd-git-secrets` folder into the root of the project repository, and then navigate the terminal (powershell) to the repo root
10+
11+
* `cd .\nhsd-git-secrets\`
12+
* `cp .\gitallowed-base ../.gitallowed`
13+
* `.\install-windows.ps1`
14+
15+
Next time you do a commit the git secrets hook should be invoked.
16+
17+
### Troubleshooting
18+
19+
You should have 3 new files in the `.git/hooks` folder in the repository. If these are not present, then make sure you have ran the install script.
20+
21+
### Custom configuration (per repo / per service team)
22+
23+
* Add individual regex expressions to the existing `repo_root/nhsd-git-secrets-nhsd-rules-deny.txt` file
24+
* Or, create your own file for regex rules and add it as a provider within the [pre-commit script](pre-commit.sh) e.g.:
25+
26+
`./nhsd-git-secrets/git-secrets --add-provider -- cat nhsd-git-secrets/nhsd-rules-deny.txt`
27+
28+
* Add file/dir excludes within the `repo_root/.gitallowed`, e.g. `.*terraform.tfstate.*:*`
29+
30+
* Control full scan vs staged files scan within [pre-commit script](pre-commit.sh) by commenting/uncommenting the mode to run e.g.:
31+
32+
```bash
33+
# Just scan the files changed in this commit
34+
# ./nhsd-git-secrets/git-secrets --pre_commit_hook
35+
36+
# Scan all files within this repo for this commit
37+
./nhsd-git-secrets/git-secrets --scan
38+
```
39+
40+
## Testing and Usage
41+
42+
To test that the hooks have been enabled correctly:
43+
44+
* make sure you have done git add if you have changed anything within git-Secrets
45+
* create a file containing one or more patterns from the `git-secrets/nhsd-rules-deny.txt` file (e.g.: `password = “test”`)
46+
* stage and commit the file
47+
48+
You should see an output similar to: `“[ERROR] Matched one or more prohibited patterns…”`.
49+
50+
**Note** This message may appear differently depending on the tools used.
51+
52+
> If you have a *false-positive* match, and your changes do not contain sensitive credentials then you can add the `--no-verify` flag to the commit command to **skip the checking**.
53+
54+
## Docker version
55+
56+
Alternatively, you might find this [dockerfile](nhsd-git-secrets.dockerfile) convenient, which:
57+
58+
1. Copies your source code into a docker image
59+
1. Downloads latest version of the secret scanner tool
60+
1. Downloads latest regex patterns from software-engineering-quality-framework
61+
1. Runs a scan

tools/nhsd-git-secrets/README.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,44 @@ Essentially, we want to avoid the NHS facing [potentially dire consequences](htt
1313

1414
## How to get started
1515

16-
If your team isn't doing secrets scanning at all yet, the fundamental first step is to understand the current state of the art. Use the [Macbook](README-mac-workstation.md) or Windows (coming soon...) guides to set up and run Git-Secrets for a nominated team member. Run the tooling, and ascertain whether there's any immediate actions to be taken.
16+
If your team isn't doing secrets scanning at all yet, the fundamental first step is to understand the current state of the art. Use the following guides to set up and run Git-Secrets for a nominated team member:
1717

18-
## Getting to green
18+
* [macOS](README-mac-workstation.md)
19+
* [Linux/WSL](README-linux-workstation.md)
20+
* [Windows](README-windows-workstation.md)
21+
22+
Run the tooling, and ascertain whether there's any immediate actions to be taken.
23+
24+
## Ongoing checks
1925

2026
Once you've verified there's no urgent actions on your code, the next steps towards getting to green are:
2127

2228
1. Ensure every team member is doing local scans. Stopping secrets before code has been committed is cheap, removing them from git history is expensive.
2329
2. Run these same scripts as part of your deployment pipelines as a second line of defence.
2430

25-
## Consider using OIDC instead of secrets
31+
## Other ways of keeping credentials out of your code
32+
33+
### Consider using OIDC Authentication instead of passwords
2634

2735
OpenID Connect allows federated authentication from pipeline workflows to AWS and Azure without storing credentials in repository secrets at all, so no expiry to manage. GitHub documentation for achieving this for:
2836

29-
- [AWS](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services)
30-
- [Azure](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-azure)
31-
- [Google](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-google-cloud-platform)
37+
* [AWS](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services)
38+
* [Azure](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-azure)
39+
* [Google](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-google-cloud-platform)
3240

3341
Configuration for OIDC is light. See the below example GitHub Actions workflow excerpt which connects to both AWS and Azure:
3442

3543
```yaml
3644
steps:
3745
- name: Checkout
3846
uses: actions/checkout@v3
39-
47+
4048
- name: Configure AWS STS credentials via OIDC
4149
uses: aws-actions/configure-aws-credentials@v1
4250
with:
4351
role-to-assume: ${{ secrets.AWS_ROLE_ID }}
4452
aws-region: eu-west-2
45-
53+
4654
- name: Configure Azure identity token via OIDC
4755
uses: azure/login@v1
4856
with:

tools/nhsd-git-secrets/full-history-scan.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export PATH=$PATH:.
66

77
# # These only need to be run once per workstation/slave/agent but are included to try and ensure they are present
88
./git-secrets --register-aws
9-
./git-secrets --add-provider -- cat nhsd-git-secrets/nhsd-rules-linux.txt
9+
./git-secrets --add-provider -- cat nhsd-git-secrets/nhsd-rules-deny.txt
1010
./git-secrets --add --allowed '.*git-secrets/.*:*'
1111
./git-secrets --add --allowed '.*terraform.tfstate.*:*'
1212

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
echo "[1/5] Cloning Git Secrets"
2+
installPath="$HOME/git-secrets-temp"
3+
if [ -d "$installPath" ];
4+
then
5+
echo "Git secrets already cloned"
6+
else
7+
git clone https://github.com/awslabs/git-secrets.git $installPath
8+
fi
9+
10+
echo "" && echo "[2/5] Installing Git Secrets and Adding to PATH"
11+
pushd $installPath
12+
make install PREFIX="$HOME/git-secrets"
13+
echo 'export PATH="$HOME/git-secrets/bin":$PATH' >> ~/.bashrc
14+
source ~/.bashrc
15+
popd
16+
17+
echo "" && echo "[3/5] Adding Git Hooks"
18+
git-secrets --install -f
19+
20+
echo "" && echo "[4/5] Removing Temp Git Secrets Repo"
21+
rm -rf $installPath
22+
23+
echo "" && echo "[5/5] Updating Pre-Commit Hook"
24+
projectRoot=$(dirname $PWD)
25+
preCommitHook="$projectRoot/.git/hooks/pre-commit"
26+
hookScript="nhsd-git-secrets/pre-commit.sh"
27+
replaceString='git secrets --pre_commit_hook -- "$@"'
28+
sed -i -e "s,$replaceString,./$hookScript," "$preCommitHook"
29+
chmod +x "$projectRoot/$hookScript"
30+
31+
echo "" && echo "Git Secrets Installation Complete"
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Write-Host "[1/6] Cloning Git Secrets"
2+
$installPath = $($Env:USERPROFILE + "\git-secrets-temp")
3+
if (-not (Test-Path $installPath))
4+
{
5+
git clone https://github.com/awslabs/git-secrets.git $installPath
6+
}
7+
else
8+
{
9+
Write-Host "Git secrets already cloned"
10+
}
11+
12+
Write-Host "`n[2/6] Installing Git Secrets"
13+
Push-Location $installPath
14+
& ".\install.ps1"
15+
Pop-Location
16+
17+
Write-Host "`n[3/6] Adding Git Hooks"
18+
git secrets --install
19+
20+
Write-Host "`n[4/6] Removing Temp Git Secrets Repo"
21+
Remove-Item $installPath -Recurse -Force
22+
23+
Write-Host "`n[5/6] Adding Git Bash to PATH"
24+
Write-Host "Checking if git bash already exists in path..."
25+
$currentPath = [Environment]::GetEnvironmentVariable("PATH", "User")
26+
$gitInstallDirectory = "C:\Program Files\Git\bin"
27+
if ($currentPath -notlike "*$gitInstallDirectory*")
28+
{
29+
Write-Host "Adding to path..."
30+
$newPath = $currentPath
31+
if (-not ($newPath.EndsWith(";")))
32+
{
33+
$newPath = $newPath + ";"
34+
}
35+
$newPath = $newPath + $gitInstallDirectory
36+
[Environment]::SetEnvironmentVariable("PATH", $newPath, "User")
37+
Write-Host "Added to path"
38+
}
39+
else
40+
{
41+
Write-Host "Already in Path"
42+
}
43+
44+
Write-Host "`n[6/6] Updating Pre-Commit Hook"
45+
$projectRoot = Split-Path -Path $PSScriptRoot -Parent
46+
$preCommitHook = $projectRoot + '\.git\hooks\pre-commit'
47+
(Get-Content $preCommitHook) | ForEach-Object {
48+
if ($_.ReadCount -eq 2) {
49+
$_ -replace '^.*$','./nhsd-git-secrets/pre-commit.sh'
50+
} else {
51+
$_
52+
}
53+
} | Set-Content $preCommitHook
54+
55+
Write-Host "`nGit Secrets Installation Complete"

tools/nhsd-git-secrets/nhsd-git-secrets.dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ WORKDIR /secrets-scanner/source
4444
RUN git init
4545

4646
RUN echo "Downloading regex files from engineering-framework"
47-
RUN curl https://codeload.github.com/NHSDigital/software-engineering-quality-framework/tar.gz/main | tar -xz --strip=3 software-engineering-quality-framework-main/tools/nhsd-git-secrets/nhsd-rules-linux-mac.txt
47+
RUN curl https://codeload.github.com/NHSDigital/software-engineering-quality-framework/tar.gz/main | tar -xz --strip=3 software-engineering-quality-framework-main/tools/nhsd-git-secrets/nhsd-rules-deny.txt
4848

4949
RUN echo "Copying allowed secrets list"
5050
COPY .gitallowed .
@@ -53,7 +53,7 @@ RUN echo .gitallowed
5353
# Register additional providers: adds AWS by default
5454
RUN echo "Configuring secrets scanner"
5555
RUN /secrets-scanner/git-secrets --register-aws
56-
RUN /secrets-scanner/git-secrets --add-provider -- cat nhsd-rules-linux-mac.txt
56+
RUN /secrets-scanner/git-secrets --add-provider -- cat nhsd-rules-deny.txt
5757

5858
# build will fail here, if secrets are found
5959
RUN echo "Running scan..."

tools/nhsd-git-secrets/pre-commit-mac.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Note that this will be invoked by the git hook from the repo root, so cd .. isn't required
44

55
# These only need to be run once per workstation but are included to try and ensure they are present
6-
./git-secrets --add-provider -- cat nhsd-git-secrets/nhsd-rules-linux-mac.txt
6+
./git-secrets --add-provider -- cat nhsd-git-secrets/nhsd-rules-deny.txt
77

88
# Just scan the files changed in this commit
99
./git-secrets --pre_commit_hook

0 commit comments

Comments
 (0)