saedalavinia
2/7/2019 - 4:12 AM

Automate Concourse LDAP Login using fly

Automate Concourse LDAP Login using fly

## Variables required
CONCOURSE_URL="https://my.ci.com"
CONCOURSE_USER="a_user"
CONCOURSE_PASSWORD="a_password"
CONCOURSE_TEAM="some_team"
CONCOURSE_TARGET=mytarget

## Create a file named token that will be used to read and write tokens 
touch token 

## extract the LDAP authentication url and write to token file
LDAP_AUTH_URL=$CONCOURSE_URL$(curl -b token -c token -L "$CONCOURSE_URL/sky/login" -s | grep "/sky/issuer/auth/ldap?" | awk -F'"' '{print $2}')
# login using username and password while writing to the token file 
curl -s -o /dev/null -b token -c token  -L --data-urlencode "login=$CONCOURSE_USER" --data-urlencode "password=$CONCOURSE_PASSWORD" "$LDAP_AUTH_URL"

# Extract Bearer Token
ATC_BEARER_TOKEN=$(cat token | grep -o -P '(?<=Bearer\ ).*(?=\")')

# Build your .flyrc
cat <<ENDOFSCRIPT >> ~/.flyrc
targets:
  $CONCOURSE_TARGET:
    api: $CONCOURSE_URL
    team: $CONCOURSE_TEAM
    token:
      type: Bearer
      value: $ATC_BEARER_TOKEN
ENDOFSCRIPT

fly -t  $CONCOURSE_TARGET  <your_command