Skip to content

Appendix I: Credentials Setup

Martin Olveyra edited this page Nov 18, 2026 · 3 revisions

Previous Chapter: Appendix H: Glossary


Introduction

shub-workflow library depends on scrapinghub python bindings library. For operation on ScrapyCloud (SC), the client from scrapinghub library needs either an explicit passing of SC credentials in its constructor, or to setup it via the environment variable SH_APIKEY. shub-workflow chooses the second alternative, so the only requirement is that this environment variable is set wherever your scripts run: in ScrapyCloud and in your local environment.

How to provide it is a decision of each project and team, and many approaches are valid: a secrets manager, variables of your CI/CD pipeline, environment files rendered from templates and never committed, etc. This appendix describes two approaches, as examples.

From the ScrapyCloud project settings

A simple approach is to add a setting with the ScrapyCloud apikey in zyte dash settings and set it as environment variable in the project settings.py file, e.g (assuming the zyte project setting is named SC_APIKEY, edit according to your case):

import os
(...)
from shub_workflow.utils import kumo_settings

(...)

settings_from_kumo = kumo_settings()
if "SC_APIKEY" in settings_from_kumo:
    os.environ["SH_APIKEY"] = settings_from_kumo["SC_APIKEY"]

This approach avoids to hard code the SC apikey directly on settings.py. Of course, if you need to run shub-workflow scripts in your local environment, you will not have access to kumo settings, so you need to set up SH_APIKEY environment variable locally.

From a Docker build argument

There are even safer alternatives, that difficult visibility of this credential. For example, if your project is based on a Dockerfile image, and assuming you have the SH_APIKEY environment variable set locally, you can add the following lines to Dockerfile:

ARG SHUB_APIKEY
ENV SH_APIKEY $SHUB_APIKEY

and, at the deploy time with shub:

$ shub image upload <target> -b SHUB_APIKEY="$SH_APIKEY"

The -b option of shub passes building arguments to docker build. In this case, the building argument SHUB_APIKEY is set with the value of the local SH_APIKEY environment variable. The ARG directive in Dockerfile declares that SHUB_APIKEY is an accepted building argument. The ENV line then sets the variable environment SH_APIKEY using the value of the passed argument. The final effect is that the SH_APIKEY is only visible within the docker container once instantiated. This method ensures that only the developers involved in the project need to know the project api key. For extra safety you may rely on bitbucket buildbot. The shub image upload line above will be specified in the bitbucket-pipelines.yml file, and the local SH_APIKEY environment variable set up via bitbucket repository settings->repository variables. In this case, only the repository admin need to have access to the apikey.

May be a more practical alternative, without using a custom docker image, would be to include the mentioned Dockerfile lines in the scrapinghub-scrapy-stack Dockerfile and make available the '-b' option to 'shub deploy' command. At the moment of writing these lines, this feature is not available, though.

Google Cloud Storage credentials

Operations on gs:// paths (through FSHelper or the gcstorage module) need a Google service account key. Two environment variables are accepted:

Variable Holds Typical use
GOOGLE_APPLICATION_CREDENTIALS the path of the key file a local environment, a Docker image with the key file inside
GOOGLE_APPLICATION_CREDENTIALS_JSON the key itself: its JSON, raw or base64-encoded ScrapyCloud jobs and other environments where secrets come as environment variables and no key file exists

With GOOGLE_APPLICATION_CREDENTIALS_JSON, the storage client is built directly from the key, so no file is written anywhere. It takes precedence over GOOGLE_APPLICATION_CREDENTIALS when both are set. The variable is read when the first gs:// operation runs, so it may be set at any point before that, e.g. from your project settings.py as shown above for SH_APIKEY.

Never store the key itself in GOOGLE_APPLICATION_CREDENTIALS. That variable belongs to the Google libraries, which expect a path and repeat its value verbatim in their errors (File <value> was not found.) and debug logs, so any code that reads it with the key inside, e.g. a quick script that doesn't import your project settings, prints the private key. For backward compatibility, shub-workflow itself detects a key stored there and builds the client from it without passing it to the Google libraries, but other code in your project can't be expected to do the same.

A common pattern to avoid is converting such a variable into a key file in settings.py with tempfile.mktemp(): every process that imports the settings leaves a world-readable copy of the key in the temporary folder, and nothing deletes it. Put the key in GOOGLE_APPLICATION_CREDENTIALS_JSON instead and drop the conversion.

Errors about the key in either variable (not JSON, not a JSON object, a key that can't be loaded) are raised as shub_workflow.utils.gcstorage.InvalidCredentialsError, whose message names the variable but never includes its value.

Alerting before a GCS key expires

A service account key can also be generated on your side (e.g. with openssl) and its public certificate uploaded to the service account, typically by whoever administers the bucket you write to. Such a key inherits the validity of its certificate: Google stops accepting it at the certificate's expiration date, and from then on every gs:// operation fails. Renewing it needs that administrator to act, so you want to know well in advance.

The certificate module of gcstorage checks it for the key in use, read from the same variables as the storage client. The certificate doesn't need to be kept anywhere: Google publishes the public certificates of every service account at the url the key carries in client_x509_cert_url, and the one of your key is selected by its private_key_id.

get_expiration_alert() returns an alert message when the certificate expires within a window (30 days by default) or already expired, and None otherwise. It also returns an alert when the date can't be verified at all (no key, network error, a key id the service account doesn't publish), since a check that fails silently is the same as no check; such a message names the exception type only, never anything from the key. A natural place to call it is the beginning of a daily monitor, sending the message at once instead of queueing it, so the alert goes out even if the rest of the monitor fails:

from shub_workflow.contrib.slack import SlackMixin
from shub_workflow.utils.gcstorage.certificate import get_expiration_alert
from shub_workflow.utils.monitor import BaseMonitor


class MyMonitor(SlackMixin, BaseMonitor):

    def run(self):
        message = get_expiration_alert(
            "the README section 'Credentials rotation'",  # where your rotation procedure is described
            note="The key is also used by project B.",  # optional, appended to the expiration messages
        )
        if message is not None:
            self.sender.send_slack_messages([message], self.args.subject)
        super().run()

get_certificate_expiration() returns the service account email and the expiration date, if you prefer to build your own message. The module needs the cryptography package, included in the with-gcs-tools extra.

Clone this wiki locally