OpenShift CI provides its own Hashicorp Vault instance that we can use. This makes the use of secrets standard for anyone who needs them. Most of everything that we do for secrets was discovered from the OpenShift CI documentation Adding a New Secret to CI
Each scenario being tested will have its own collection. This allows us to prevent secret sharing between members of other collections meant to hold secrets for other scenarios.
First go to selfservice.vault.ci.openshift.org and login.
If you do not see a collection in the table for the scenario that your testing it either hasn't been created yet or you don't have access to it.
Please reach out on slack at #forum-qe-cspi-ocp-ci to ask if the collection for the scenario you are testing has already been created. If it has go to the next section
If it hasn't you can create the collection by clicking the New Collection
button and adding the name. Follow the naming structure of {product short name}-qe
.
If you do not see a collection in the table, and you have verified that it exists then you just need to be added by one of the collection owners. Please reach out on slack at #forum-qe-cspi-ocp-ci and provide details about what and why you would like to be added to a specific collection.
This collection holds team specific information holding things like AWS creds, pull secrets, ..etc. If you need access to this collection reach out on slack at #forum-qe-cspi-ocp-ci and provide details about why you would like to be added to the cspi-qe collection.
Now that you are a member of a collection you can login to vault and access that collection.
Go to vault.ci.openshift.org and click Sign in with OIDC Provider
.
You'll be redirected to your secrets homepage.
cubbyhole/
is just your users personal space for any secrets you want to store.
We care about kv/
this is where you'll find secret directories that correspond to the collections that you are a member of.
From here you can use the UI or Vault CLI to view, create, edit, and delete secrets.
In order to use a secret in Vault in OpenShift CI, the secret must include two key/value pairs:
secretsync/target-name
: The name of the secret in OpenShift CIsecretsync/target-namespace
: The namespace of the secret in OpenShift CI
Outside of those two values, any key/value pair desired can be added to the same path for that secret making them available for use in OpenShift CI.
The name of a secret in OpenShift CI must be unique. When deciding on a name for a secret, make sure to be descriptive. Using a descriptive name will help with readability in OpenShift CI configuration files as well as making it less-likely that a name is used twice.
Best practice in naming these secrets would be something similar to scenario_name-name_of_secret
. As an example, for the MTR scenario, the FTP credentials used are named mtr-ftp-credentials
. This name is unique and descriptive.
The Namespace of your secret in the build clusters. Multiple namespaces can be targeted by using a comma-separated list. For the purpose of this document, test-credentials
will be used as the namespace. This namespace allows for the use of a secret in the step of a job within OpenShift CI.
From the OpenShift CI Adding a New Secret to CI documentation:
The most common case is to use secrets in a step of a job. In this case, we require the user to mirror secrets to test-credentials namespace. The pod which runs the step can access the secrets defined in the credentials stanza of the step definition.
- Create a new collection, if one doesn't already exist.
- Sign into vault and navigate to
kv/NAME_OF_COLLECTION
. - Click the "Create secret" button in the upper right-hand corner.
- Choose name for the secret in Vault and append that to the text already in the "Path for this secret" textbox.
- Note: This name is not the same as the name described in the OpenShift CI Secret Name section of this document. This name can be anything and does not need to be unique.
- Add a key of
secretsync/target-name
to the secret and choose a value. This value must be unique and descriptive.- Note: See the OpenShift CI Secret Name section of this document for additional information.
- Example:
some-test-secret
- Add a key of
secretsync/target-namespace
and set the value totest-credentials
.- Note: See the OpenShift CI Secret Namespace section of this document for more information.
- Add the desired key/value pair(s) to the secret for use during execution.
- Example: Add a
username
key with a value ofSomeUser
and apassword
key with a value ofCleverPassword
. These values will be available during OpenShift CI execution. -
IMPORTANT SECURITY INFORMATION:
- We'll be covering information from the official OpenShift CI secret docs here
- There are 3 different practices shown in that document, depending on which case you use, your secrets may or may not get redacted when being posted to public logs.
- Good Practice - This is when you have a single secret value per secret key. This allows for your secret value to be redacted when printed to a public log.
- Risky Practice - In some cases you may need a single secret key to hold multiple secret values. In this case its possible to store multiple secret values to a single key, but this comes with a massive security risk. The secret redaction process cannot make sense of this multiple secret value syntax and it runs the risk of your secrets being posted publicly.
- Acceptable Practice - This is a mix of both practices you see above. Because there are cases that require us to store multiple values in a single secret key we are sometimes forced to use the Risky Practice. This is ok so long as we also include each individual secret value in its own key following the Good Practice. These individual keys holding the individual secrets from your key with many secrets will not be used in your automated CI run. Your CI run will still use the key that holds many secrets. What we are doing here though is that when it comes time to post the public log, the individual secrets that you've plucked out of the larger secret will be read by the secret redaction process and this will ensure that any secret that you've placed in the larger secret key will be redacted and never shown in a public log.
- See the Acceptable Practice Section here for a visual representation of what your vault should look like. If you have any questions about this process please reach us at the Slack channel #forum-qe-layered-product so that we can work together to make sure that you are not at risk of leaking credentials.
- Example: Add a
- Click the "Save" button in the bottom left-hand side of the screen
Now that a secret has been created in Vault, it is available for use in OpenShift CI steps.
For the following steps, this document will refer to the configuration of a ref
step in OpenShift CI. For simplicity, here is the configuration that will be referred to:
some-test-ref.yaml
ref:
as: some-test-ref
from: cli
commands: some-test-ref-commands.sh
credentials:
- namespace: test-credentials
name: some-test-secret
mount_path: /tmp/secrets/test
documentation: |-
Show how to use secrets in OpenShift CI
- Create the step you would like to use your new secret in, if it doesn't already exist.
- Add a
credentials
stanza to the step's configuration (.yaml
) file. - Under the new
credentials
stanza, add a new list item for your secret. Multiple secrets can be used in a step, but for the sake of simplicity, there is only one in the configuration above. - In the new list object, add the following key/value pairs:
namespace
: The namespace specified in step 6 of the Create a New Secret section of this tutorial.- Note: This will usually be set to
test-credentials
- Note: This will usually be set to
name
: The name chosen in step 5 of the Create a New Secret section of this tutorial.mount_path
: This will be the directory where the secrets are stored for use during execution.- Note: If you add more than one secret to the
credentials
stanza, you must use a differentmount-path
value for each secret.
- Note: If you add more than one secret to the
- Use the new secret in your execution.
- Each key/value pair added to your secret in Vault ,other than
secretsync/target-namespace
andsecretsync/target-name
, will have have a file in themount_path
directory. The file will be named the same as the key in Vault and will contain a single clear-text line with the value from Vault.- Example:
- In step 7 of the Create a New Secret section of this tutorial, we created the following key/value pairs:
username
:SomeUser
password
:CleverPassword
- In this example, there would be two files found in
tmp/secrets/test
- one file namedusername
and one file namedpassword
. Each file would hold a single clear-text line - the value ("SomeUser
" and "CleverPassword
")
- In step 7 of the Create a New Secret section of this tutorial, we created the following key/value pairs:
- Example:
- Using these values can be as easy as setting the contents of each file to a variable:
USERNAME=$(cat /tmp/secrets/test/username)
- Each key/value pair added to your secret in Vault ,other than
➜ export VAULT_ADDR=https://vault.ci.openshift.org
➜ vault login -method=oidc
# List vault secrets that you have access to
➜ vault kv list kv/selfservice
Keys
----
/3scale-test-image/
/acm-qe/
/aws-managed-cspi-qe/
/cspi-qe/
/oadp-qe/
/sbo-qe/
# Get secrets
➜ vault kv get kv/selfservice/cspi-qe/cluster-secrets
In the metadata section we see the version of the secret
➜ vault kv get kv/selfservice/cspi-qe/cluster-secrets
=============== Secret Path ===============
kv/data/selfservice/cspi-qe/cluster-secrets
====== Metadata ======
Key Value
--- -----
created_time 2023-03-06T12:39:47.702500423Z
deletion_time n/a
destroyed false
version 19
This tells us that the secret version for cspi-qe/cluster-secrets is at version 19. We can get older versions of secrets like this
➜ vault kv get -version=12 kv/selfservice/cspi-qe/cluster-secrets
=============== Secret Path ===============
kv/data/selfservice/cspi-qe/cluster-secrets
====== Metadata ======
Key Value
--- -----
created_time 2022-11-02T12:40:14.129461007Z
deletion_time n/a
destroyed false
version 12
At this time we believe only the most recent 10 versions are saved.
If we want to recover from an older version of a vault secret then we can do the following:
IMPORTANT
Please do not run this rollback command against any active secrets just for learning purposes (I purposely included a typo so this command should not work but just wanted to emphasize this, please do not fix this command and run just to experiment)!
There is a cubbyhole (personal space) that you are given access to in vault that you can store secrets and experiment with them there.
➜ vault kv rollback -version=10 kv/selfservice/cspi-qe/clutser-secrets