Autore Topic: Git-Secrets - Prevents You From Committing Secrets And Credentials Into Git Repositories  (Letto 157 volte)

0 Utenti e 1 Visitatore stanno visualizzando questo topic.

Offline Flavio58

Git-Secrets - Prevents You From Committing Secrets And Credentials Into Git Repositories

Prevents you from committing passwords and other sensitive information to a git repository.

git secrets --scan [-r|--recursive] [--cached] [--no-index] [--untracked] [<files>...]
git secrets --scan-history
git secrets --install [-f|--force] [<target-directory>]
git secrets --list [--global]
git secrets --add [-a|--allowed] [-l|--literal] [--global] <pattern>
git secrets --add-provider [--global] <command> [arguments...]
git secrets --register-aws [--global]
git secrets --aws-provider [<credentials-file>]

git-secrets scans commits, commit messages, and --no-ff merges to prevent adding secrets into your git repositories. If a commit, commit message, or any commit in a --no-ff merge history matches one of your configured prohibited regular expression patterns, then the commit is rejected.
Installing git-secrets
git-secrets must be placed somewhere in your PATH so that it is picked up by git when running git secrets. You can use install target of the provided Makefile to install git secrets and the man page. You can customize the install path using the PREFIX and MANPREFIX variables.
make install
Or, installing with Homebrew (for OS X users).
brew install git-secrets
You're not done yet! You MUST install the git hooks for every repo that you wish to use with git secrets --install.
Here's a quick example of how to ensure a git repository is scanned for secrets on each commit:
cd /path/to/my/repo
git secrets --install
git secrets --register-aws

Operation Modes
Each of these options must appear first on the command line.
Installs hooks for a repository. Once the hooks are installed for a git repository, commits and non-ff merges for that repository will be prevented from committing secrets.
Scans one or more files for secrets. When a file contains a secret, the matched text from the file being scanned will be written to stdout and the script will exit with a non-zero RC. Each matched line will be written with the name of the file that matched, a colon, the line number that matched, a colon, and then the line of text that matched. If no files are provided, all files returned by git ls-files are scanned.
Scans repository including all revisions. When a file contains a secret, the matched text from the file being scanned will be written to stdout and the script will exit with a non-zero RC. Each matched line will be written with the name of the file that matched, a colon, the line number that matched, a colon, and then the line of text that matched.
Lists the git-secrets configuration for the current repo or in the global git config.
Adds a prohibited or allowed pattern.
Registers a secret provider. Secret providers are executables that when invoked outputs prohibited patterns that git-secrets should treat as prohibited.
Adds common AWS patterns to the git config and ensures that keys present in ~/.aws/credentials are not found in any commit. The following checks are added:
  • AWS Access Key ID via [A-Z0-9]{20}
  • AWS Secret Access Key assignments via ":" or "=" surrounded by optional quotes
  • AWS account ID assignments via ":" or "=" surrounded by optional quotes
  • Allowed patterns for example AWS keys (AKIAIOSFODNN7EXAMPLE and wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY)
  • Enables using ~/.aws/credentials to scan for known credentials.
While the patterns registered by this command should catch most instances of AWS credentials, these patterns are not guaranteed to catch them all. git-secrets should be used as an extra means of insurance -- you still need to do your due diligence to ensure that you do not commit credentials to a repository.
Secret provider that outputs credentials found in an INI file. You can optionally provide the path to an ini file.
Options for --install
-f, --force
Overwrites existing hooks if present.
When provided, installs git hooks to the given directory. The current directory is assumed if <target-directory> is not provided.
If the provided <target-directory> is not in a Git repository, the directory will be created and hooks will be placed in <target-directory>/hooks. This can be useful for creating Git template directories using with git init --template <target-directory>.
You can run git init on a repository that has already been initialized. >From the git init documentation:
>From the git documentation: Running git init in an existing repository  is safe. It will not overwrite things that are already there. The  primary reason for rerunning git init is to pick up newly added  templates (or to move the repository to another place if  --separate-git-dir is given).
The following git hooks are installed:
  1. pre-commit: Used to check if any of the files changed in the commit use prohibited patterns.
  2. commit-msg: Used to determine if a commit message contains a prohibited patterns.
  3. prepare-commit-msg: Used to determine if a merge commit will introduce a history that contains a prohibited pattern at any point. Please note that this hook is only invoked for non fast-forward merges.
Git only allows a single script to be executed per hook. If the repository contains Debian style subdirectories like pre-commit.d and commit-msg.d, then the git hooks will be installed into these directories, which assumes that you've configured the corresponding hooks to execute all of the scripts found in these directories. If these git subdirectories are not present, then the git hooks will be installed to the git repo's .git/hooks directory.
Install git hooks to the current directory:
cd /path/to/my/repository
git secrets --install
Install git hooks to a repository other than the current directory:
git secrets --install /path/to/my/repository
Create a git template that has git-secrets installed, and then copy that template into a git repository:
git secrets --install ~/.git-templates/git-secrets
git init --template ~/.git-templates/git-secrets
Overwrite existing hooks if present:
git secrets --install -f
Options for --scan
-r, --recursive
Scans the given files recursively. If a directory is encountered, the directory will be scanned. If -r is not provided, directories will be ignored.
-r cannot be used alongside --cached, --no-index, or --untracked.
Searches blobs registered in the index file.
Searches files in the current directory that is not managed by Git.
In addition to searching in the tracked files in the working tree, --scan also in untracked files.
The path to one or more files on disk to scan for secrets.
If no files are provided, all files returned by git ls-files are scanned.
Scan all files in the repo:
git secrets --scan
Scans a single file for secrets:
git secrets --scan /path/to/file
Scans a directory recursively for secrets:
git secrets --scan -r /path/to/directory
Scans multiple files for secrets:
git secrets --scan /path/to/file /path/to/other/file
You can scan by globbing:
git secrets --scan /path/to/directory/*
Scan from stdin:
echo 'hello!' | git secrets --scan -
Options for --list
Lists only git-secrets configuration in the global git config.
Options for --add
Adds patterns to the global git config
-l, --literal
Escapes special regular expression characters in the provided pattern so that the pattern is searched for literally.
-a, --allowed
Mark the pattern as allowed instead of prohibited. Allowed patterns are used to filter our false positives.
The regex pattern to search.
Adds a prohibited pattern to the current repo:
git secrets --add '[A-Z0-9]{20}'
Adds a prohibited pattern to the global git config:
git secrets --add --global '[A-Z0-9]{20}'
Adds a string that is scanned for literally (+ is escaped):
git secrets --add --literal 'foo+bar'
Add an allowed pattern:
git secrets --add -a 'allowed pattern'
Options for --register-aws
Adds AWS specific configuration variables to the global git config.
Options for --aws-provider
If provided, specifies the custom path to an INI file to scan. If not provided, ~/.aws/credentials is assumed.
Options for --add-provider
Adds the provider to the global git config.
Provider command to invoke. When invoked the command is expected to write prohibited patterns separated by new lines to stdout. Any extra arguments provided are passed on to the command.
Registers a secret provider with arguments:
git secrets --add-provider -- git secrets --aws-provider
Cats secrets out of a file:
git secrets --add-provider -- cat /path/to/secret/file/patterns

Defining prohibited patterns
egrep compatible regular expressions are used to determine if a commit or commit message contains any prohibited patterns. These regular expressions are defined using the git config command. It is important to note that different systems use different versions of egrep. For example, when running on OS X, you will use a different version of egrep than when running on something like Ubuntu (BSD vs GNU).
You can add prohibited regular expression patterns to your git config using git secrets --add <pattern>.

Ignoring false-positives
Sometimes a regular expression might match false positives. For example, git commit SHAs look a lot like AWS access keys. You can specify many different regular expression patterns as false positives using the following command:
git secrets --add --allowed 'my regex pattern'
You can also add regular expressions patterns to filter false positives to a .gitallowed file located in the repository's root directory. Lines starting with # are skipped (comment line) and empty lines are also skipped.
First, git-secrets will extract all lines from a file that contain a prohibited match. Included in the matched results will be the full path to the name of the file that was matched, followed ':', followed by the line number that was matched, followed by the entire line from the file that was matched by a secret pattern. Then, if you've defined allowed regular expressions, git-secrets will check to see if all of the matched lines match at least one of your registered allowed regular expressions. If all of the lines that were flagged as secret are canceled out by an allowed match, then the subject text does not contain any secrets. If any of the matched lines are not matched by an allowed regular expression, then git-secrets will fail the commit/merge/message.
Just as it is a bad practice to add prohibited patterns that are too greedy, it is also a bad practice to add allowed patterns that are too forgiving. Be sure to test out your patterns using ad-hoc calls to git secrets --scan $filename to ensure they are working as intended.

Secret providers
Sometimes you want to check for an exact pattern match against a set of known secrets. For example, you might want to ensure that no credentials present in ~/.aws/credentials ever show up in a commit. In these cases, it's better to leave these secrets in one location rather than spread them out across git repositories in git configs. You can use "secret providers" to fetch these types of credentials. A secret provider is an executable that when invoked outputs prohibited patterns separated by new lines.
You can add secret providers using the --add-provider command:
git secrets --add-provider -- git secrets --aws-provider
Notice the use of --. This ensures that any arguments associated with the provider are passed to the provider each time it is invoked when scanning for secrets.

Example walkthrough
Let's take a look at an example. Given the following subject text (stored in /tmp/example):
This is a test!
More test...
And the following registered patterns:
git secrets --add 'password\s*=\s*.+'
git secrets --add --allowed --literal 'ex@mplepassword'
Running git secrets --scan /tmp/example, the result will result in the following error output:

[ERROR] Matched prohibited pattern

Possible mitigations:
- Mark false positives as allowed using: git config --add secrets.allowed ...
- List your configured patterns: git config --get-all secrets.patterns
- List your configured allowed patterns: git config --get-all secrets.allowed
- Use --no-verify if this is a one-time false positive
Breaking this down, the prohibited pattern value of password\s*=\s*.+ will match the following lines:
...But the first match will be filtered out due to the fact that it matches the allowed regular expression of ex@mplepassword. Because there is still a remaining line that did not match, it is considered a secret.
Because that matching lines are placed on lines that start with the filename and line number (e.g., /tmp/example:3:...), you can create allowed patterns that take filenames and line numbers into account in the regular expression. For example, you could whitelist an entire file using something like:
git secrets --add --allowed '/tmp/example:.*'
git secrets --scan /tmp/example && echo $?
# Outputs: 0
Alternatively, you could whitelist a specific line number of a file if that line is unlikely to change using something like the following:
git secrets --add --allowed '/tmp/example:3:.*'
git secrets --scan /tmp/example && echo $?
# Outputs: 0
Keep this in mind when creating allowed patterns to ensure that your allowed patterns are not inadvertantly matched due to the fact that the filename is included in the subject text that allowed patterns are matched against.

Skipping validation
Use the --no-verify option in the event of a false-positive match in a commit, merge, or commit message. This will skip the execution of the git hook and allow you to make the commit or merge.

Source: Git-Secrets - Prevents You From Committing Secrets And Credentials Into Git Repositories

Consulente in Informatica dal 1984

Software automazione, progettazione elettronica, computer vision, intelligenza artificiale, IoT, sicurezza informatica, tecnologie di sicurezza militare, SIGINT. 

Twitter :

Cell:  +39 366 3416556

#deeplearning #computervision #embeddedboard #iot #ai


Related Topics

  Oggetto / Aperto da Risposte Ultimo post
0 Risposte
160 Visite
Ultimo post Aprile 18, 2018, 08:58:11 am
da Ruggero Respigo
0 Risposte
63 Visite
Ultimo post Ottobre 13, 2018, 06:11:01 pm
da Flavio58
0 Risposte
22 Visite
Ultimo post Luglio 21, 2019, 02:26:08 am
da Ruggero Respigo
0 Risposte
6 Visite
Ultimo post Dicembre 29, 2019, 02:16:46 pm
da Flavio58
0 Risposte
3 Visite
Ultimo post Gennaio 20, 2020, 01:29:59 am
da Flavio58

Sitemap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326