Articles in this section

How to access Solr credentials on Upsun

Goal

Access credentials for Solr from within an Upsun application using Python.

Assumptions

  • an active Upsun project
  • properly configured .upsun/config.yaml for the given service
  • SSH key configured on account if developing locally
  • Solr installed if developing locally
  • properly configured Solr relationship like so, in .upsun/config.yaml:
relationships:
    solrsearch: "searchsolr:solr"

If developing locally, remember to first open a tunnel to the project environment using the Upsun CLI.

$ upsun tunnel:open && export PLATFORM_RELATIONSHIPS="$(upsun tunnel:info --encode)"

This will open an SSH tunnel to your current Upsun environment and expose a local environment variable that mimics the relationships array on Upsun. Check the Upsun documentation and How to develop locally on Upsun with a tethered connection for more information.

Problems

Upsun service credentials are made available to applications as the PLATFORM_RELATIONSHIPS environment variable, which is a base64-encoded JSON string that has to be decoded before it can be used.

There are two primary options for accessing service credentials on Upsun that can be used within an application:

  1. Using the Upsun Config Reader library
  2. Accessing environment variables manually

Steps (Config Reader)

1. Install the library

Install the configuration library. See the documentation for minimum requirements.

$ pip install platformshconfig

2. Create a Config object

Creating a Config object provides access to the Upsun environment.

from platformshconfig import Config

config = Config()

3. Read the credentials

# Get the credentials to connect to the Solr service.
credentials = config.credentials('solrsearch')

Steps (Manual)

1. Load the environment variable

import os, json, base64

relationships = json.loads(base64.b64decode(os.environ["PLATFORM_RELATIONSHIPS"]))

2. Read the credentials

credentials = relationships['solrsearch']

Use

In most cases, you will need only the host and port properties to connect to Solr. Pass those to your Solr library’s setup routine in your application. Most of the time the other values may be ignored.

In either case, credentials is a dictionary matching the relationship JSON object.

{
  "service": "searchsolr",
  "ip": "169.254.103.29",
  "hostname": "swhnppdg3ugcd5sktkga73wjoi.solr.service._.eu-3.platformsh.site",
  "cluster": "rjify4yjcwxaa-master-7rqtwti",
  "host": "solrsearh.internal",
  "rel": "solr",
  "path": "solr\/collection1",
  "scheme": "solr",
  "type": "solr:7.6",
  "port": 8080
}

Conclusions

Using either the language-specific Upsun Configuration library or direct access methods for environment variables, an application can get Solr credentials in Python.

Upsun supports configuration libraries for multiple languages. The Python configuration library can be useful for connecting to Solr without creating the full DSN string using FormattedCredentials:

# Define formatted credentials
formatted_url = config.formatted_credentials('solrsearch', 'pysolr')

The APIs for each language are written to be as consistent as possible, but seek out each library’s documentation for specific differences.

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.