ヘッダー画像

【GitLab】CI/CDのSecure Files使ってみた

投稿 2025年6月1日 最終更新 2025年6月1日 専門用語多め

前置き

GitLab CI/CDにてセキュアな文字列をどう取り扱うか?
の答えがよくわからないですが、公式によるとCI/CD変数のマスクや非表示はセキュアじゃないと明言されています。

調べるとVaultAWS等、外部で管理するパターンもありましたが、
結局そこに接続するためのアクセスキー等をどうするのか問題もありますし、
何より面倒くさい…。

ということで、できるだけGitLabだけで完結したいなと思い、セキュアファイルを試してみます。

※いろいろ名前が揺れてますが、英語では「Secure Files」、日本語公式ドキュメントは「セキュアファイル」、GitLabの日本語翻訳は「安全なファイル」と記載されています

環境はDockerでGitLab EE v17.8.2を構築しています。

安全なファイルの追加方法

CI/CDをクリック追加したいプロジェクトを開き、サイドメニューの設定のCI/CDをクリックします。

セキュアファイル安全なファイルを展開して、「ファイルをアップロード」をクリックし、任意のファイルをアップロードします。

  • secure.txt
secure_file_test

※今回アップロードしたファイルは上記の通りです

ファイルをアップロードファイル一覧に追加されれば成功です。

使い方

通常

CI/CDにて、下記のように使います。
※途中にあるURLはどんなプロジェクトでも共通なので、変更しなくて大丈夫です

image: ruby

stages:
  - test

variables_sample:
  stage: test
  script:
    # カレントディレクトリパス
    - pwd
    # ディレクトリ内確認
    - ls -la
    # セキュアファイルダウンロード
    - curl -sS "https://gitlab.com/gitlab-org/incubation-engineering/mobile-devops/download-secure-files/-/raw/main/installer" | bash
    # ディレクトリ内確認
    - ls -la
    - ls -la ./.secure_files
    # ファイルの中身確認
    - cat ./.secure_files/secure.txt
Running with gitlab-runner 17.8.3 (690ce25c)
  on docker-runner t1_S5S4ik, system ID: r_9ntxuFB257bV
Preparing the "docker" executor 03:02
Using Docker executor with image ruby ...
Pulling docker image ruby ...
Using docker image sha256:4351ba9d9918f17a5c6ef03c76a6c1922bcb711d002e090fae90114da802be3f for ruby with digest ruby@sha256:4cf7641c6354e8f407afd2dbb0ab1968cd44ac443bd833c16bdf55cc074a3eb8 ...
Preparing environment 00:41
Running on runner-t1s5s4ik-project-6-concurrent-0 via 498eb2969af1...
Getting source from Git repository 00:50
Fetching changes with git depth set to 20...
Initialized empty Git repository in /builds/root/gitlab-ci-test/.git/
Created fresh repository.
Checking out 1258e1e6 as detached HEAD (ref is main)...
Skipping Git submodules setup
Executing "step_script" stage of the job script 01:06
Using docker image sha256:4351ba9d9918f17a5c6ef03c76a6c1922bcb711d002e090fae90114da802be3f for ruby with digest ruby@sha256:4cf7641c6354e8f407afd2dbb0ab1968cd44ac443bd833c16bdf55cc074a3eb8 ...
$ pwd
/builds/root/gitlab-ci-test
$ ls -la
total 4
drwxrwxrwx 1 root root  36 Jun  1 06:28 .
drwxrwxrwx 1 root root  64 Jun  1 06:28 ..
drwxrwxrwx 1 root root  86 Jun  1 06:28 .git
-rw-rw-rw- 1 root root 503 Jun  1 06:28 .gitlab-ci.yml
$ curl -sS "https://gitlab.com/gitlab-org/incubation-engineering/mobile-devops/download-secure-files/-/raw/main/installer" | bash
Downloading download-secure-files from https://gitlab.com/gitlab-org/incubation-engineering/mobile-devops/download-secure-files/-/releases/permalink/latest/downloads/download-secure-files-linux-amd64
Installing download-secure-files at /usr/bin/download-secure-files
Downloading Secure Files (v0.1.14) to /builds/root/gitlab-ci-test/.secure_files
secure.txt downloaded to .secure_files/secure.txt
$ ls -la
total 4
drwxrwxrwx 1 root root  62 Jun  1 06:29 .
drwxrwxrwx 1 root root  64 Jun  1 06:28 ..
drwxrwxrwx 1 root root  86 Jun  1 06:28 .git
-rw-rw-rw- 1 root root 503 Jun  1 06:28 .gitlab-ci.yml
drwxr-xr-x 1 root root  20 Jun  1 06:29 .secure_files
$ ls -la ./.secure_files
total 4
drwxr-xr-x 1 root root 20 Jun  1 06:29 .
drwxrwxrwx 1 root root 62 Jun  1 06:29 ..
-rw-r--r-- 1 root root 16 Jun  1 06:29 secure.txt
$ cat ./.secure_files/secure.txt
secure_file_test
Cleaning up project directory and file based variables 00:39
Job succeeded

ダウンロード先を変更

環境変数としてSECURE_FILES_DOWNLOAD_PATHにパスを指定してあげると、その指定したディレクトリにダウンロードされます。

image: ruby

stages:
  - test

variables_sample:
  stage: test
  variables:
    SECURE_FILES_DOWNLOAD_PATH: './work/'
  script:
    # カレントディレクトリパス
    - pwd
    # ディレクトリ内確認
    - ls -la
    # セキュアファイルダウンロード
    - curl -sS "https://gitlab.com/gitlab-org/incubation-engineering/mobile-devops/download-secure-files/-/raw/main/installer" | bash
    # ディレクトリ内確認
    - ls -la
    - ls -la ./work
    # ファイルの中身確認
    - cat ./work/secure.txt
Running with gitlab-runner 17.8.3 (690ce25c)
  on docker-runner t1_S5S4ik, system ID: r_9ntxuFB257bV
Preparing the "docker" executor 02:15
Using Docker executor with image ruby ...
Pulling docker image ruby ...
Using docker image sha256:4351ba9d9918f17a5c6ef03c76a6c1922bcb711d002e090fae90114da802be3f for ruby with digest ruby@sha256:4cf7641c6354e8f407afd2dbb0ab1968cd44ac443bd833c16bdf55cc074a3eb8 ...
Preparing environment 00:34
Running on runner-t1s5s4ik-project-6-concurrent-0 via 498eb2969af1...
Getting source from Git repository 00:35
Fetching changes with git depth set to 20...
Reinitialized existing Git repository in /builds/root/gitlab-ci-test/.git/
Checking out 38ebcbba as detached HEAD (ref is main)...
Removing .secure_files/
Skipping Git submodules setup
Executing "step_script" stage of the job script 01:06
Using docker image sha256:4351ba9d9918f17a5c6ef03c76a6c1922bcb711d002e090fae90114da802be3f for ruby with digest ruby@sha256:4cf7641c6354e8f407afd2dbb0ab1968cd44ac443bd833c16bdf55cc074a3eb8 ...
$ pwd
/builds/root/gitlab-ci-test
$ ls -la
total 4
drwxrwxrwx 1 root root  36 Jun  1 06:34 .
drwxrwxrwx 1 root root  64 Jun  1 06:28 ..
drwxrwxrwx 1 root root  86 Jun  1 06:34 .git
-rw-rw-rw- 1 root root 540 Jun  1 06:34 .gitlab-ci.yml
$ curl -sS "https://gitlab.com/gitlab-org/incubation-engineering/mobile-devops/download-secure-files/-/raw/main/installer" | bash
Downloading download-secure-files from https://gitlab.com/gitlab-org/incubation-engineering/mobile-devops/download-secure-files/-/releases/permalink/latest/downloads/download-secure-files-linux-amd64
Installing download-secure-files at /usr/bin/download-secure-files
Downloading Secure Files (v0.1.14) to /builds/root/gitlab-ci-test/work
secure.txt downloaded to work/secure.txt
$ ls -la
total 4
drwxrwxrwx 1 root root  44 Jun  1 06:36 .
drwxrwxrwx 1 root root  64 Jun  1 06:28 ..
drwxrwxrwx 1 root root  86 Jun  1 06:34 .git
-rw-rw-rw- 1 root root 540 Jun  1 06:34 .gitlab-ci.yml
drwxr-xr-x 1 root root  20 Jun  1 06:36 work
$ ls -la ./work
total 4
drwxr-xr-x 1 root root 20 Jun  1 06:36 .
drwxrwxrwx 1 root root 44 Jun  1 06:36 ..
-rw-r--r-- 1 root root 16 Jun  1 06:36 secure.txt
$ cat ./work/secure.txt
secure_file_test
Cleaning up project directory and file based variables 00:26
Job succeeded

まとめ

セキュアな文字列を扱うベストプラクティスは…よくわかりませんが、
変数のマスクよりはましかなと思いました。

以上、ここまで見ていただきありがとうございます。

皆さまの快適な開発ライフに、ほんの少しでもお役に立てれば幸いです。

コメント

この記事のコメントはありません。