# ========================================================================================= # Copyright (C) 2021 Orange # # This program is free software; you can redistribute it and/or modify it under the terms # of the GNU Lesser General Public License as published by the Free Software Foundation; # either version 3 of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # See the GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License along with this # program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth # Floor, Boston, MA 02110-1301, USA. # ========================================================================================= variables: # Default Docker image (can be overriden) GITLEAKS_IMAGE: "zricethezav/gitleaks:latest" GITLEAKS_ARGS: "--verbose" GITLEAKS_QUICK_ARGS: "--verbose --depth=10" # default production ref name (pattern) PROD_REF: '/^master$/' # default integration ref name (pattern) INTEG_REF: '/^develop$/' # allowed stages depend on your template type (see: orange-opensource.gitlab.io/tbc/doc/dev-guidelines/#stages) stages: - test .gitleaks-scripts: &gitleaks-scripts | # BEGSCRIPT set -e function log_info() { echo -e "[\\e[1;94mINFO\\e[0m] $*" } function log_warn() { echo -e "[\\e[1;93mWARN\\e[0m] $*" } function log_error() { echo -e "[\\e[1;91mERROR\\e[0m] $*" } function assert_defined() { if [[ -z "$1" ]] then log_error "$2" exit 1 fi } function install_ca_certs() { certs=$1 if [[ -z "$certs" ]] then return fi # import in system if echo "$certs" >> /etc/ssl/certs/ca-certificates.crt then log_info "CA certificates imported in \\e[33;1m/etc/ssl/certs/ca-certificates.crt\\e[0m" fi if echo "$certs" >> /etc/ssl/cert.pem then log_info "CA certificates imported in \\e[33;1m/etc/ssl/cert.pem\\e[0m" fi } function install_gitleaks_rules() { # If present, import gitleaks rules found inside the git repository (in place of the default + Orange rules) if [[ -f ".gitleaks.toml" ]] then log_info "Using custom Gitleaks rules from project (\\e[33;1m.gitleaks.toml\\e[0m)" cp .gitleaks.toml ./gitleaks/rules.toml export gitleaks_rule_opts="--config-path=./gitleaks/rules.toml" elif [[ "$GITLEAKS_RULES" ]] then log_info "Using Gitleaks rules from env (\\e[33;1m\$GITLEAKS_RULES\\e[0m)" echo "$GITLEAKS_RULES" > ./gitleaks/rules.toml export gitleaks_rule_opts="--config-path=./gitleaks/rules.toml" else log_info "No Gitleaks rules found: use default" fi } function get_latest_template_version() { tag_json=$(wget -T 5 -q -O - "$CI_API_V4_URL/projects/Orange-OpenSource%2Ftbc%2F$1/repository/tags?per_page=1" || echo "") echo "$tag_json" | sed -rn 's/^.*"name":"([^"]*)".*$/\1/p' } function check_for_update() { template="$1" actual="$2" latest=$(get_latest_template_version "$template") if [[ -n "$latest" ]] && [[ "$latest" != "$actual" ]] then log_warn "\\e[1;93m=======================================================================================================\\e[0m" log_warn "\\e[93mThe template \\e[32m$template\\e[93m:\\e[33m$actual\\e[93m you're using is not up-to-date: consider upgrading to version \\e[32m$latest\\e[0m" log_warn "\\e[93m(set \$TEMPLATE_CHECK_UPDATE_DISABLED to disable this message)\\e[0m" log_warn "\\e[1;93m=======================================================================================================\\e[0m" fi } if [[ -z "$TEMPLATE_CHECK_UPDATE_DISABLED" ]]; then check_for_update gitleaks "1.0.0"; fi # ENDSCRIPT # full analysis on master and develop branches gitleaks: image: name: $GITLEAKS_IMAGE entrypoint: [""] services: - name: "$CI_REGISTRY/orange-opensource/tbc/tools/tracking:master" command: ["--service", "gitleaks", "1.0.0"] stage: test before_script: - *gitleaks-scripts - install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}" - mkdir -p ./gitleaks - install_gitleaks_rules script: - gitleaks ${TRACE+--debug} --path=. $gitleaks_rule_opts --report=./gitleaks/gitleaks-report.json $GITLEAKS_ARGS artifacts: name: "$CI_JOB_NAME artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG" when: always paths: - gitleaks/ rules: # exclude merge requests - if: $CI_MERGE_REQUEST_ID when: never # on production and integration branch(es) - if: '$CI_COMMIT_REF_NAME =~ $INTEG_REF || $CI_COMMIT_REF_NAME =~ $PROD_REF' # quick analysis on dev branches gitleaks-quick: extends: gitleaks script: - gitleaks ${TRACE+--debug} --path=. $gitleaks_rule_opts --report=./gitleaks/gitleaks-report.json $GITLEAKS_QUICK_ARGS rules: # exclude merge requests - if: $CI_MERGE_REQUEST_ID when: never # only on non-production, non-integration branches - if: '$CI_COMMIT_REF_NAME !~ $PROD_REF && $CI_COMMIT_REF_NAME !~ $INTEG_REF' allow_failure: true