API key configuration script content

Provides content of the configure-apikey.sh script file used for authentication and access control within the CML workspace.

configure-apikey.sh script file


#!/bin/bash

# Exit on the first error
set -e  

# Check for required argument (API Key File Path)
if [ $# -lt 1 ]; then
  echo "Error: Provide API Key file path as an argument. "
  exit 1
fi

file_path="$1"

# Check if the file exists
if [ ! -f "$file_path" ]; then
  echo "Error: File '$file_path' does not exist."
  exit 1
fi

# Read the API key value from the file
api_key=$(cat $file_path)
encoded_key=$(printf $api_key | base64 | tr -d '\n')
secret_name=dataservice-secret
secret_key=ds_api_auth_key

# Patch the secret using kubectl with base64 encoded value
echo "Patching secret: $secret_name (base64 encoded)"

kubectl patch secret $secret_name -n otel-collector --type=json \
  -p='[{
    "op" : "replace" ,
    "path" : "/data/'$secret_key'" ,
    "value" : "'$(printf "$encoded_key" | base64 | tr -d '\n')'"
  }]'

# Check for errors
if [ $? -ne 0 ]; then
  echo "Error creating secret!"
fi

#Restart the pod
kubectl rollout restart statefulset -n otel-collector otel-workloads-collector-collector