Written by Sam McGeown on 4/11/2020 · Read in about 2 min (273 words)
Published under vRealize Automation

To generate a basic authentication header from a username and password in Code Stream you could use a CI task and execute echo -n username:password | base64 in the shell then export the result for use later on. A more repeatable way is to create a Custom Integration that takes the two inputs, and returns the encoded header as an output.

To create the Custom Integration:

  1. Create a new Custom Integration named “Create Basic Authentication Header”
  2. Select the Runtime - the examples below are shell and python3 respectively
  3. Replace the placeholder code with the example from below
  4. Save and version the Custom Integration, ensuring you eanble the “Release Version” toggle
Creating the Custom Integration
Creating the Custom Integration

To use the Custom Integration in a pipeline:

  1. Configure the Host (you’ll need a Docker endpoint configured for this) and Builder image URL settings in the Workspace tab of your pipeline (e.g. python:latest)
  2. Add a new Custom task, to your Stage, select the “Create Basic Authentication Header” integration and configure the inputs
  3. Use the tasks to refer to the header value in your later tasks - e.g. I’m using ${Stage0.Create Auth} to refer to the output in a REST task below:
Consuming the Custom Integration
Consuming the Custom Integration

Below are the shell and a Python3 examples, in the YAML format to paste into your custom integration:

 2runtime: shell
 3code: |
 4  export basicAuthHeader=$(echo -n $username:$password | base64)
 5inputProperties:      # Enter fields for input section of a task
 6  # Username input
 7  - name: 'username'
 8    type: text
 9    title: 'Username'
10    placeHolder: 'Enter basic authentication usename'
11    required: true
12    bindable: true
13  # Password input
14  - name: 'password'
15    type: password
16    title: 'Password'
17    placeHolder: 'Enter basic authentication password'
18    defaultValue: ''
19    required: true
20    bindable: true
22  - name: basicAuthHeader
23    type: label
24    title: Basic Authentication Header
 2runtime: "python3"
 3code: |
 4  from base64 import b64encode
 5  from context import getInput, setOutput
 6  username = getInput('username')
 7  password = getInput('password')
 8  usernameAndPassword = b64encode(bytes(f'{username}:{password}',encoding='ascii')).decode('ascii')
 9  setOutput('basicAuthHeader', "Basic "+usernameAndPassword)
10inputProperties:      # Enter fields for input section of a task
11  # Username input
12  - name: 'username'
13    type: text
14    title: 'Username'
15    placeHolder: 'Enter basic authentication usename'
16    required: true
17    bindable: true
18  # Password input
19  - name: 'password'
20    type: password
21    title: 'Password'
22    placeHolder: 'Enter basic authentication password'
23    defaultValue: ''
24    required: true
25    bindable: true
27  - name: basicAuthHeader
28    type: label
29    title: Basic Authentication Header
Share this post