Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Using API keys securely in your OpenShift microservices and applications

<p>&nbsp;</p> <quillbot-extension-portal></quillbot-extension-portal>

October 7, 2016
Shane Boulden
Related topics:
Developer toolsDevOpsKubernetesMicroservices
Related products:
Red Hat OpenShift Container Platform

    In the microservices landscape, the API provides an essential form of communication between components. To allow secure communication between microservices components, as well as third-party applications, it's important to be able to consume API keys and other sensitive data in a manner that doesn't place the data at risk. Secret objects are specifically designed to hold sensitive information, and OpenShift makes exposing this information to the applications that need it easy.

    In this post, I'll demonstrate securely consuming API keys in OpenShift Enterprise 3. We'll deploy a Sinatra application that uses environment variables to interact with the Twitter API; create a Kubernetes Secret object to store the API keys; expose the secret to the application via environment variables; and then perform a rolling update of the environment variables across our pods.

    Prerequisites

    For this post, I'm assuming you have access to an OpenShift Enterprise deployment. If you don't have access to OpenShift Enterprise, you can get a local OpenShift 3 deployment with the Red Hat Container Development Kit (CDK)

    Application deployment

    We're going to use a pre-built Sinatra application to demonstrate interacting with the Twitter API, and you can find the source code on GitHub.  To deploy the app, simply create a new project in your OpenShift project from the GitHub repository:

    $ oc new-app https://github.com/jockey10/tripper

    Create a route to your application, and list the route in the terminal.

    $ oc expose service tripper
    $ oc get route
    
    NAME      HOST/PORT             PATH      SERVICE            TERMINATION   LABELS
    tripper   <url-to-app>                    tripper:8080-tcp                 app=tripper

     

    If you navigate to the application's URL, you'll see that it's up and running. However, any searches return the error "Unable to verify your credentials", as shown below. Time to grab some API keys!

    twitter-app-running

    Grab some API keys

    Our application is deployed and running, but it can't access the Twitter API without any keys. To get a key, navigate to https://apps.twitter.com, and sign in using your Twitter account (create one if required).

    Select "Create New App", and give your new app a name, description, and website. You can use a place-holder such as www.example.com for now if you don't have a domain name for your app.

    Read the Twitter Developer Agreement, select the checkbox, and then select "Create your Twitter Application".

    Once your application has been created you will be redirected to the application management page. Click the "Keys and Access Tokens" tab to view your API keys.

    You'll see two keys listed on this page: the consumer key, which identifies your app to the Twitter API, and the secret, which grants your application access. For now, just leave this page open, and we'll create a Kubernetes Secret object with this data.

    Create the Secret object

    We can use a YAML file to create the Secret object that will hold our API keys.

    Firstly, we need to encode our Twitter API keys into base64. We can do this easily using Ruby:

    $ irb
    
    irb(main):001:0> require 'base64'
    => true
    irb(main):002:0> Base64.strict_encode64('consumer-key-from-twitter')
    => "Y29uc3VtZXIta2V5LWZyb20tdHdpdHRlcg=="
    irb(main):003:0> Base64.strict_encode64('secret-key-from-twitter')
    => "c2VjcmV0LWtleS1mcm9tLXR3aXR0ZXI="
    

     

    We can then create a YAML file representing the secret object with these parameters:

    #twitter-secrets.yml
    apiVersion: v1
    kind: Secret
    metadata:
    name: twitter-secrets
    type: Opaque
    data:
    consumer-key: Y29uc3VtZXIta2V5LWZyb20tdHdpdHRlcg==
    secret-key: c2VjcmV0LWtleS1mcm9tLXR3aXR0ZXI=

     

    OpenShift objects can be created from a file using the "-f" switch, like so:

    $ oc create -f twitter-secrets.yml

    To check that the object has been successfully created, you can use the oc command to get a list of Secret objects:

    $ oc get secrets
    builder-dockercfg-anc66    kubernetes.io/dockercfg               1         15d
    builder-token-2fw2w        kubernetes.io/service-account-token   3         15d
    builder-token-g7o4y        kubernetes.io/service-account-token   3         15d
    default-dockercfg-ni33n    kubernetes.io/dockercfg               1         15d
    default-token-b8hh3        kubernetes.io/service-account-token   3         15d
    default-token-pwj6b        kubernetes.io/service-account-token   3         15d
    deployer-dockercfg-bb0rp   kubernetes.io/dockercfg               1         15d
    deployer-token-amtd3       kubernetes.io/service-account-token   3         15d
    deployer-token-uc4o0       kubernetes.io/service-account-token   3         15d
    twitter-secrets            Opaque                                2         21h
    

     

    Consume the Secret object in your application

    Our application is expecting the Twitter API consumer and secret keys to be available in environment variables. To deploy the secrets via environment variables, we need to edit the deployment configuration for our application.

    Firstly, we need the name of the deployment configuration.

    $ oc get dc
    
    NAME       REVISION   REPLICAS   TRIGGERED BY
    tripper    1          1          config,image(tripper:latest)

     

    Now that we have a handle to the deployment configuration, we can edit it:

    $ oc edit dc tripper

     

    We can use the following YAML syntax for the environment variables, indicating they are derived from a Secret object:

    env:
    - name: TWITTER_KEY
      valueFrom:
        secretKeyRef:
          name: twitter-secrets
          key: consumer-key
    - name: TWITTER_SECRET
      valueFrom:
        secretKeyRef:
          name: twitter-secrets
          key: secret-key

     

    Simply inject this text into the YAML for the tripper deployment configuration:

    apiVersion: v1
    kind: DeploymentConfig
    metadata:
    .
    .
    .
    spec:
    .
    .
     template:
     .
     .
     spec:
     .
     .
       containers:
       - env:
         - name: TWITTER_KEY
           valueFrom:
             secretKeyRef:
               name: twitter-secrets
               key: consumer-key
         - name: TWITTER_SECRET
           valueFrom:
             secretKeyRef:
               name: twitter-secrets
               key: secret-key
        image:
        imagePullPolicy:
    .
    .
    
    

     

    And that's it! Once you save and exit the deployment configuration file, OpenShift will automatically perform a rolling update of the deployment.

    Once the update has been performed, you can navigate back to your application see that it's now able to interact with the Twitter API.

    screenshot-from-2016-09-27-16-31-19

    Thanks for reading!

    Last updated: March 20, 2023

    Recent Posts

    • Debugging image mode with Red Hat OpenShift 4.20: A practical guide

    • EvalHub: Because "looks good to me" isn't a benchmark

    • SQL Server HA on RHEL: Meet Pacemaker HA Agent v2 (tech preview)

    • Deploy with confidence: Continuous integration and continuous delivery for agentic AI

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    What’s up next?

     

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.