Skip to content

Commit

Permalink
Manage Gitlab certificate (#1249)
Browse files Browse the repository at this point in the history
* Manage gitlab certificate

* Build

* Manage GITLAB_CUSTOM_CERTIFICATE

quick build push

* Fix python issue

quick build push

* Error msg doc

* [MegaLinter] Apply linters fixes

* Update config json schema

quick build push

Co-authored-by: nvuillam <nvuillam@users.noreply.github.com>
  • Loading branch information
nvuillam and nvuillam committed Feb 6, 2022
1 parent 54dc5a5 commit 1e261f6
Show file tree
Hide file tree
Showing 20 changed files with 84 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased] (beta, main branch content)

- Fixes
- Manage to use local certificate with Gitlab comments reporter using GITLAB_SSL_CERTIFICATE_PATH ([#1239](https://github.com/megalinter/megalinter/issues/1239))

Note: Can be used with `megalinter/megalinter@beta` in your GitHub Action mega-linter.yml file, or with `megalinter/megalinter:beta` docker image

- Fixes
- Gitlab Comments Reporter: allow to use certificates with variable GITLAB_CUSTOM_CERTIFICATE (or GITLAB_CERTIFICATE_PATH only if [PRE_COMMANDS](https://megalinter.github.io/configuration/#pre-commands) are used) ([#1239](https://github.com/megalinter/megalinter/issues/1239))

- Doc
- Update images with screen records gifs
- Add publish artifact task in azure pipelines doc
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ ARG PSSA_VERSION='latest'
# APK Packages used by mega-linter core architecture
RUN apk add --update --no-cache \
bash \
ca-certificates \
coreutils \
curl \
file \
Expand Down
14 changes: 8 additions & 6 deletions docs/reporters/GitlabCommentReporter.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ Click on hyperlinks to access detailed logs (click on **Download** in **Artifact

![config-gitlab-access-token](https://user-images.githubusercontent.com/17500430/151674446-1bcb1420-d9aa-4ae1-aaae-dcf51afb36ab.gif)

| Variable | Description | Default value |
|--------------------------------|-------------------------------------------------------------------------------------------|--------------------------|
| GITLAB_COMMENT_REPORTER | Activates/deactivates reporter | true |
| GITLAB_API_URL | URL where the github API can be reached<br/>May be overridden if using self-hosted Gitlab | `https://api.gitlab.com` |
| GITLAB_SERVER_URL | URL of the Gitlab instance<br/>May be overridden if using self-hosted Gitlab | `https://gitlab.com` |
| GITLAB_ACCESS_TOKEN_MEGALINTER | Must contain a Gitlab private access token defined with api access | <!-- --> |
| Variable | Description | Default value |
|--------------------------------|--------------------------------------------------------------------------------------------------------|--------------------------|
| GITLAB_COMMENT_REPORTER | Activates/deactivates reporter | true |
| GITLAB_API_URL | URL where the github API can be reached<br/>May be overridden if using self-hosted Gitlab | `https://api.gitlab.com` |
| GITLAB_SERVER_URL | URL of the Gitlab instance<br/>May be overridden if using self-hosted Gitlab | `https://gitlab.com` |
| GITLAB_ACCESS_TOKEN_MEGALINTER | Must contain a Gitlab private access token defined with api access | <!-- --> |
| GITLAB_CUSTOM_CERTIFICATE | SSL certificate value to connect to Gitlab | <!-- --> |
| GITLAB_CERTIFICATE_PATH | Path to SSL certificate to connect to Gitlab (if SSL cert has been manually defined with PRE_COMMANDS) | <!-- --> |
1 change: 1 addition & 0 deletions flavors/ci_light/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ FROM python:3.9.7-alpine3.13
# APK Packages used by mega-linter core architecture
RUN apk add --update --no-cache \
bash \
ca-certificates \
coreutils \
curl \
file \
Expand Down
1 change: 1 addition & 0 deletions flavors/dart/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ ARG GLIBC_VERSION='2.31-r0'
# APK Packages used by mega-linter core architecture
RUN apk add --update --no-cache \
bash \
ca-certificates \
coreutils \
curl \
file \
Expand Down
1 change: 1 addition & 0 deletions flavors/documentation/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ FROM python:3.9.7-alpine3.13
# APK Packages used by mega-linter core architecture
RUN apk add --update --no-cache \
bash \
ca-certificates \
coreutils \
curl \
file \
Expand Down
1 change: 1 addition & 0 deletions flavors/dotnet/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ ARG PSSA_VERSION='latest'
# APK Packages used by mega-linter core architecture
RUN apk add --update --no-cache \
bash \
ca-certificates \
coreutils \
curl \
file \
Expand Down
1 change: 1 addition & 0 deletions flavors/go/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ FROM python:3.9.7-alpine3.13
# APK Packages used by mega-linter core architecture
RUN apk add --update --no-cache \
bash \
ca-certificates \
coreutils \
curl \
file \
Expand Down
1 change: 1 addition & 0 deletions flavors/java/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ FROM python:3.9.7-alpine3.13
# APK Packages used by mega-linter core architecture
RUN apk add --update --no-cache \
bash \
ca-certificates \
coreutils \
curl \
file \
Expand Down
1 change: 1 addition & 0 deletions flavors/javascript/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ FROM python:3.9.7-alpine3.13
# APK Packages used by mega-linter core architecture
RUN apk add --update --no-cache \
bash \
ca-certificates \
coreutils \
curl \
file \
Expand Down
1 change: 1 addition & 0 deletions flavors/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ FROM python:3.9.7-alpine3.13
# APK Packages used by mega-linter core architecture
RUN apk add --update --no-cache \
bash \
ca-certificates \
coreutils \
curl \
file \
Expand Down
1 change: 1 addition & 0 deletions flavors/python/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ FROM python:3.9.7-alpine3.13
# APK Packages used by mega-linter core architecture
RUN apk add --update --no-cache \
bash \
ca-certificates \
coreutils \
curl \
file \
Expand Down
1 change: 1 addition & 0 deletions flavors/ruby/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ FROM python:3.9.7-alpine3.13
# APK Packages used by mega-linter core architecture
RUN apk add --update --no-cache \
bash \
ca-certificates \
coreutils \
curl \
file \
Expand Down
1 change: 1 addition & 0 deletions flavors/rust/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ FROM python:3.9.7-alpine3.13
# APK Packages used by mega-linter core architecture
RUN apk add --update --no-cache \
bash \
ca-certificates \
coreutils \
curl \
file \
Expand Down
1 change: 1 addition & 0 deletions flavors/salesforce/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ FROM python:3.9.7-alpine3.13
# APK Packages used by mega-linter core architecture
RUN apk add --update --no-cache \
bash \
ca-certificates \
coreutils \
curl \
file \
Expand Down
1 change: 1 addition & 0 deletions flavors/scala/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ FROM python:3.9.7-alpine3.13
# APK Packages used by mega-linter core architecture
RUN apk add --update --no-cache \
bash \
ca-certificates \
coreutils \
curl \
file \
Expand Down
1 change: 1 addition & 0 deletions flavors/swift/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ FROM python:3.9.7-alpine3.13
# APK Packages used by mega-linter core architecture
RUN apk add --update --no-cache \
bash \
ca-certificates \
coreutils \
curl \
file \
Expand Down
1 change: 1 addition & 0 deletions flavors/terraform/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ FROM python:3.9.7-alpine3.13
# APK Packages used by mega-linter core architecture
RUN apk add --update --no-cache \
bash \
ca-certificates \
coreutils \
curl \
file \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3314,13 +3314,34 @@
"title": "GitHub Workspace",
"type": "string"
},
"GITLAB_ACCESS_TOKEN_MEGALINTER": {
"$id": "#/properties/GITLAB_ACCESS_TOKEN_MEGALINTER",
"default": true,
"description": "Gitlab access token with API access (can be user or project Access token)",
"title": "Gitlab Access Token",
"type": "string"
},
"GITLAB_CERTIFICATE_PATH": {
"$id": "#/properties/GITLAB_CERTIFICATE_PATH",
"default": true,
"description": "Path to SSL certificate if necessary",
"title": "Gitlab SSL Certificate path",
"type": "string"
},
"GITLAB_COMMENT_REPORTER": {
"$id": "#/properties/GITLAB_COMMENT_REPORTER",
"default": true,
"description": "Posts Mega-Linter results summary in the comments of the related merge request (if existing)",
"title": "Activate Gitlab MR Comments reporter",
"type": "boolean"
},
"GITLAB_CUSTOM_CERTIFICATE": {
"$id": "#/properties/GITLAB_CUSTOM_CERTIFICATE",
"default": true,
"description": "Store your SSL certificate string value in a CI env variable named GITLAB_CUSTOM_CERTIFICATE if you want it to be copied by MegaLinter in a local certificate file that will be used by python gitlab lib",
"title": "Gitlab SSL Certificate value",
"type": "string"
},
"GIT_FILTER_REGEX_EXCLUDE": {
"$id": "#/properties/GIT_FILTER_REGEX_EXCLUDE",
"title": "Excluding regex filter for GIT descriptor",
Expand Down
40 changes: 33 additions & 7 deletions megalinter/reporters/GitlabCommentReporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import gitlab
from megalinter import Reporter, config
from megalinter.pre_post_factory import run_command
from megalinter.utils_reporter import build_markdown_summary


Expand Down Expand Up @@ -47,16 +48,39 @@ def produce_report(self):
action_run_url = config.get("CI_JOB_URL", "")
p_r_msg = build_markdown_summary(self, action_run_url)

# Post comment on merge request if found
# Build gitlab options
gitlab_options = {}
# auth token
if config.get("GITLAB_ACCESS_TOKEN_MEGALINTER", "") != "":
gl = gitlab.Gitlab(
gitlab_server_url,
private_token=config.get("GITLAB_ACCESS_TOKEN_MEGALINTER"),
gitlab_options["private_token"] = config.get(
"GITLAB_ACCESS_TOKEN_MEGALINTER"
)
else:
gl = gitlab.Gitlab(
gitlab_server_url, job_token=config.get("CI_JOB_TOKEN")
gitlab_options["job_token"] = config.get("CI_JOB_TOKEN")
# Certificate management
gitlab_certificate_path = config.get("GITLAB_CERTIFICATE_PATH", "")
if config.get("GITLAB_CUSTOM_CERTIFICATE", "") != "":
# Certificate value defined in an ENV variable
cert_value = config.get("GITLAB_CUSTOM_CERTIFICATE")
gitlab_certificate_path = "/etc/ssl/certs/gitlab-cert.crt"
with open(gitlab_certificate_path, "w", encoding="utf-8") as cert_file:
cert_file.write(cert_value)
logging.debug(
f"Updated {gitlab_certificate_path} with certificate value {cert_value}"
)
if gitlab_certificate_path != "":
# Update certificates and set cert path in gitlab options
run_command(
{"cwd": "root", "command": "update-ca-certificates"},
"GitlabCommentReporter",
self.master,
)
gitlab_options["ssl_verify"] = gitlab_certificate_path
# Create gitlab connection
logging.debug(
f"[GitlabCommentReporter] Logging to {gitlab_server_url} with {str(gitlab_options)}"
)
gl = gitlab.Gitlab(gitlab_server_url, **gitlab_options)
# Get gitlab project
try:
project = gl.projects.get(gitlab_project_id)
Expand Down Expand Up @@ -143,5 +167,7 @@ def display_auth_error(self, e):
logging.error(
"[Gitlab Comment Reporter] You may need to define a masked Gitlab CI/CD variable "
"MEGALINTER_ACCESS_TOKEN containing a personal token with scope 'api'\n"
"(if already defined, your token is probably invalid)" + str(e)
"(if already defined, your token is probably invalid)\n"
"If you are using local certificate, you also may need to define variables "
"GITLAB_CUSTOM_CERTIFICATE or GITLAB_CERTIFICATE_PATH" + str(e)
)

0 comments on commit 1e261f6

Please sign in to comment.