Secrets in a pipeline
A secret is not a file
The Ruby release from lesson 13.9 needs an API key,
and a key in the repository
is a key anyone who clones the repository owns. The rule is simple: the
repository holds code, the CI settings hold secrets, and the job receives
them as environment variables.
Both systems call the same idea something different:
GitHub Actions has
secrets, GitLab CI has masked
variables. In the job they arrive as
ENV, exactly like the variables from lesson 11.3.
Read a secret without printing it
The first thing a script does with a token is check that it exists. Print its length, not its value: the length is enough to know the variable arrived, and a length in a log is not a leaked key.
API_TOKEN=abc123 ruby -e 'token = ENV["API_TOKEN"]; puts token ? token.length : "missing"'
6
ruby -e 'token = ENV["API_TOKEN"]; puts token ? token.length : "missing"'
missing
The same check belongs at the start of the release task: a missing token should stop the job in the first second, not after a build that cannot publish.
Both pipelines pass a variable
A GitHub workflow reads the secret into the step environment:
- name: Publish the gem run: gem push *.gem env: GEM_HOST_API_KEY: ${{ secrets.RUBYGEMS_API_KEY }}
GitLab does the same with a protected variable named RUBYGEMS_API_KEY; the job needs no YAML for it, because the variable is already in the environment:
publish: stage: release script: - gem push *.gem
Parse either file the usual way; the secret is never in the file, only its name:
ruby -ryaml -e 'p YAML.load_file(ARGV[0])["jobs"]["release"]["steps"][-1]["env"]' .github/workflows/ci.yml
{"GEM_HOST_API_KEY" => "${{ secrets.RUBYGEMS_API_KEY }}"}
What to watch for
A secret that reaches a log is a secret that must be rotated. Masking in
the CI hides the exact value, but a script that prints
ENV.to_h or a failing HTTP request with headers
can still write it to the job output. Read tokens by length or by a
boolean, never by value.
Keep .env out of the repository. Locally a
.env file is convenient; in the pipeline the
variables come from the CI settings, and the two should never be mixed in
one commit.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Where does the API key of a release job belong?
Answer
In the CI settings, passed to the job as an environment variable
The repository holds code; a key committed there belongs to anyone who clones it, so the CI settings hold the secret.
-
The variable API_TOKEN is not set. What does this program print?
token = ENV["API_TOKEN"] puts token ? token.length : "missing"Answer
missing
A missing variable reads as nil, and the check prints one word instead of a length or a value.
-
What does this program print?
token = "abc123" puts token.gsub(/.(?=.)/, "*")Answer
*****3
The lookahead keeps the last character visible, which is a mask a person can still compare with the key they pasted.
-
Why is printing
ENV.to_hdangerous in a job?Answer
It writes every variable the job has, secrets included, into the log
Masking hides the exact value, but a whole hash or a failed request with headers can still print it.
-
What does this program print?
require "yaml" text = "env:\n GEM_HOST_API_KEY: ${{ secrets.RUBYGEMS_API_KEY }}\n" p YAML.load(text)["env"]["GEM_HOST_API_KEY"]Answer
"${{ secrets.RUBYGEMS_API_KEY }}"
The file holds the name of the secret, not its value; the host replaces the placeholder at run time.
-
What does this program print?
ENV["DEMO_TOKEN"] = "abc123" puts ENV.to_h.key?("DEMO_TOKEN")Answer
true
A set variable is a key of the environment hash, which is exactly why dumping the hash leaks it.
-
How do GitHub and GitLab hand a secret to a job?
Answer
GitLab as a masked variable, GitHub as a secret; both arrive as ENV
The two names describe the same mechanism, and the script reads the value with ENV either way.
-
What does this program print?
puts "length: #{ENV["DEMO_TOKEN"].to_s.length}"Answer
length: 0
nil.to_s is the empty string, so an unset variable prints a length of zero without printing a value.
-
What is the rule for a local
.envfile?Answer
It stays out of the repository, because the pipeline gets the values from the CI settings
A local convenience file and a CI secret are two sources that should never be mixed in one commit.
-
A release task starts by checking that the token exists. Why is that the first step?
Answer
A missing token stops the job in the first second, not after a build that cannot publish
The early check turns a long wait and a confusing failure at the push into one clear message at the start.
Article author: Arthur Isaev