Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: Make 'cloudflared access token' return with a non-zero exit code when token has expired #1429

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ento
Copy link

@ento ento commented Mar 1, 2025

I've been working on a helper script for a project that automatically fetches an access token and updates a local .env file with the token. Sometimes the script would fail to detect that cloudflared failed to fetch a token by looking at its exit code.

token=$(cloudflared access token -app=$cf_access_domain)
code=$?
echo "exit code $code"
if [ $code -ne 0 ] || [ -z "$token" ]; then
    echo "'cloudflared access token' failed. Attempting to log in..."
    cloudflared access login $cf_access_domain
    token=$(cloudflared access token -app=$cf_access_domain)
fi

I needed to add the [ -z "$token" ] test to catch these cases.

Looking at cloudflared's source, my guess is the command prints an empty token and exits with zero when GetAppTokenIfExists detects that the token has been expired and there was no error removing the file that the token was saved to.

cloudflared/token/token.go

Lines 435 to 438 in 3f6b1f2

if payload.isExpired() {
err := os.Remove(path)
return "", err
}

@ento ento force-pushed the fix-access-token-exit-code-when-token-expired branch 2 times, most recently from f1c76a7 to 6dc71af Compare March 1, 2025 17:26
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant