Add an initial version of the component

This commit is contained in:
Ahmed Hemdan 2023-06-13 13:37:30 +02:00
parent 5d254b062d
commit 66e7f4df74
No known key found for this signature in database
GPG key ID: 35C579218532E2FF
18 changed files with 307 additions and 78 deletions

49
.gitlab-ci.yml Normal file
View file

@ -0,0 +1,49 @@
include:
component: gitlab.com/$CI_PROJECT_PATH@$CI_COMMIT_SHA
stages: [test, release]
ensure-job-added:
stage: test
image: badouralix/curl-jq
script:
- echo "Expect that a job named 'secret_detection' is added to the pipeline"
- |
route="https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/pipelines/$CI_PIPELINE_ID/jobs"
count=`curl --silent $route | jq 'map(select(.name | contains("secret_detection"))) | length'`
if [ "$count" != "1" ]; then
exit 1
fi
# Ensure that a project description exists, because it will be important to display
# the resource in the catalog.
check-description:
image: badouralix/curl-jq
script:
- |
route="https://gitlab.com/api/v4/projects/$CI_PROJECT_ID"
desc=`curl --silent $route | jq '.description'`
if [ "$desc" = "null" ]; then
echo "Description not set. Please set a projet description"
exit 1
else
echo "Description set"
fi
# Ensure that a `README.md` exists in the root directory as it represents the
# documentation for the whole components repository.
check-readme:
image: busybox
script: ls README.md || (echo "Please add a README.md file" && exit 1)
# If we are tagging a release with a specific convention ("v" + number) and all
# previous checks succeeded, we proceed with creating a release automatically.
create-release:
stage: release
image: registry.gitlab.com/gitlab-org/release-cli:latest
rules:
- if: $CI_COMMIT_TAG =~ /\d+/
script: echo "Creating release $CI_COMMIT_TAG"
release:
tag_name: $CI_COMMIT_TAG
description: "Release $CI_COMMIT_TAG of components repository $CI_PROJECT_PATH"

111
README.md
View file

@ -1,92 +1,47 @@
# Secret Detection
Read more about this feature here: https://docs.gitlab.com/ee/user/application_security/secret_detection.
Configure Secret Detection with CI/CD variables (https://docs.gitlab.com/ee/ci/variables/index.html).
## Getting started
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
```
cd existing_repo
git remote add origin https://gitlab.com/gitlab-components/secret-detection.git
git branch -M main
git push -uf origin main
```
## Integrate with your tools
- [ ] [Set up project integrations](https://gitlab.com/gitlab-components/secret-detection/-/settings/integrations)
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
List of available variables: https://docs.gitlab.com/ee/user/application_security/secret_detection/#available-cicd-variables
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
You should add this component to an existing `.gitlab-ci.yml` file by using the `include:`
keyword.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
```yaml
include:
- component: gitlab.com/gitlab-components/secret-detection@<VERSION>
```
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
where `<VERSION>` is the latest released tag or `main`.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
If you want to leverage the `$SECRET_DETECTION_DISABLED` variable to run the `secret_detection` job conditionally use `include:rules`. Otherwise the job will run when applicable:
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
```yaml
include:
- component: gitlab.com/gitlab-components/secret-detection@main
rules:
- if: $SECRET_DETECTION_DISABLED != "true"
```
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
### Inputs
## License
For open source projects, say how it is licensed.
| Input | Default value | Description |
| ----- | ------------- | ----------- |
| `stage` | `test` | The stage where you want the job to be added. |
| `image_prefix` | `$CI_TEMPLATE_REGISTRY_HOST/security-products` | Override the name of the Docker registry providing the default images (proxy). |
| `image_tag` | `5` | Override the default version of the `secrets` analyzer image. |
| `image_suffix` | `""` | Suffix added to the image name. If set to -fips, [FIPS-enabled images](https://docs.gitlab.com/ee/user/application_security/secret_detection/#use-fips-enabled-images) are used for scan. [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/355519) in GitLab 14.10. |
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
### Variables
You can customize secret detection by defining the following CI/CD variables:
| CI/CD variable | Description |
| -------------- | ----------- |
| `SECRET_DETECTION_EXCLUDED_PATHS` | Exclude vulnerabilities from output based on the paths. The paths are a comma-separated list of patterns. Patterns can be globs (see [doublestar.Match](https://pkg.go.dev/github.com/bmatcuk/doublestar/v4@v4.0.2#Match) for supported patterns), or file or folder paths (for example, `doc,spec`). Parent directories also match patterns. [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/225273) in GitLab 13.3. |
| `SECRET_DETECTION_HISTORIC_SCAN` | Flag to enable a historic Gitleaks scan. |
| `SECRET_DETECTION_LOG_OPTIONS` | [`git log`](https://git-scm.com/docs/git-log) options used to define commit ranges. [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/350660) in GitLab 15.1. |

11
src/ruby_gem/.gitignore vendored Normal file
View file

@ -0,0 +1,11 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
# rspec failure tracking
.rspec_status

View file

@ -0,0 +1,10 @@
default:
image: ruby:3.0.5
before_script:
- gem install bundler -v 2.4.12
- bundle install
example_job:
script:
- bundle exec rake

3
src/ruby_gem/.rspec Normal file
View file

@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper

13
src/ruby_gem/.rubocop.yml Normal file
View file

@ -0,0 +1,13 @@
AllCops:
TargetRubyVersion: 2.6
Style/StringLiterals:
Enabled: true
EnforcedStyle: double_quotes
Style/StringLiteralsInInterpolation:
Enabled: true
EnforcedStyle: double_quotes
Layout/LineLength:
Max: 120

12
src/ruby_gem/Gemfile Normal file
View file

@ -0,0 +1,12 @@
# frozen_string_literal: true
source "https://rubygems.org"
# Specify your gem's dependencies in ruby_gem.gemspec
gemspec
gem "rake", "~> 13.0"
gem "rspec", "~> 3.0"
gem "rubocop", "~> 1.21"

31
src/ruby_gem/README.md Normal file
View file

@ -0,0 +1,31 @@
# RubyGem
TODO: Delete this and the text below, and describe your gem
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ruby_gem`. To experiment with that code, run `bin/console` for an interactive prompt.
## Installation
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
Install the gem and add to the application's Gemfile by executing:
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
If bundler is not being used to manage dependencies, install the gem by executing:
$ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
## Usage
TODO: Write usage instructions here
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruby_gem.

12
src/ruby_gem/Rakefile Normal file
View file

@ -0,0 +1,12 @@
# frozen_string_literal: true
require "bundler/gem_tasks"
require "rspec/core/rake_task"
RSpec::Core::RakeTask.new(:spec)
require "rubocop/rake_task"
RuboCop::RakeTask.new
task default: %i[spec rubocop]

11
src/ruby_gem/bin/console Executable file
View file

@ -0,0 +1,11 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require "bundler/setup"
require "ruby_gem"
# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
require "irb"
IRB.start(__FILE__)

8
src/ruby_gem/bin/setup Executable file
View file

@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx
bundle install
# Do any other automated setup that you need to do here

View file

@ -0,0 +1,8 @@
# frozen_string_literal: true
require_relative "ruby_gem/version"
module RubyGem
class Error < StandardError; end
# Your code goes here...
end

View file

@ -0,0 +1,5 @@
# frozen_string_literal: true
module RubyGem
VERSION = "0.1.0"
end

View file

@ -0,0 +1,38 @@
# frozen_string_literal: true
require_relative "lib/ruby_gem/version"
Gem::Specification.new do |spec|
spec.name = "ruby_gem"
spec.version = RubyGem::VERSION
spec.authors = ["Fabio Pitino"]
spec.email = ["fpitino@gitlab.com"]
spec.summary = "TODO: Write a short summary, because RubyGems requires one."
spec.description = "TODO: Write a longer description or delete this line."
spec.homepage = "TODO: Put your gem's website or public repo URL here."
spec.required_ruby_version = ">= 2.6.0"
spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(__dir__) do
`git ls-files -z`.split("\x0").reject do |f|
(File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
end
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
# Uncomment to register a new dependency of your gem
# spec.add_dependency "example-gem", "~> 1.0"
# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
end

View file

@ -0,0 +1,4 @@
module RubyGem
VERSION: String
# See the writing guide of rbs: https://github.com/ruby/rbs#guides
end

View file

@ -0,0 +1,11 @@
# frozen_string_literal: true
RSpec.describe RubyGem do
it "has a version number" do
expect(RubyGem::VERSION).not_to be nil
end
it "does something useful" do
expect(false).to eq(true)
end
end

View file

@ -0,0 +1,15 @@
# frozen_string_literal: true
require "ruby_gem"
RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"
# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!
config.expect_with :rspec do |c|
c.syntax = :expect
end
end

33
template.yml Normal file
View file

@ -0,0 +1,33 @@
spec:
inputs:
stage:
default: test
image_prefix:
default: "$CI_TEMPLATE_REGISTRY_HOST/security-products"
image_tag:
default: '5'
image_suffix:
default: ""
---
variables:
SECRET_DETECTION_EXCLUDED_PATHS: ""
.secret-analyzer:
stage: $[[ inputs.stage ]]
image: "$[[ inputs.image_prefix ]]/secrets:$[[ inputs.image_tag ]]$[[ inputs.image_suffix ]]"
services: []
allow_failure: true
variables:
GIT_DEPTH: "50"
# `rules` must be overridden explicitly by each child job
# see https://gitlab.com/gitlab-org/gitlab/-/issues/218444
artifacts:
reports:
secret_detection: gl-secret-detection-report.json
secret_detection:
extends: .secret-analyzer
rules:
- if: $CI_COMMIT_BRANCH
script:
- /analyzer run