Labelgate

Access Labels

Put a Cloudflare Zero Trust Access login in front of a hostname with labels.

Access labels add a Cloudflare Access login in front of a hostname. It takes two steps: define a policy, then use it from a tunnel or DNS label.

services:
  admin:
    image: nginx:alpine
    labels:
      # 1. Define a policy named "team"
      labelgate.access.team.policy.include.emails: "alice@example.com,bob@example.com"
      # 2. Use it
      labelgate.tunnel.admin.hostname: "admin.example.com"
      labelgate.tunnel.admin.service: "http://admin:80"
      labelgate.tunnel.admin.access: "team"

Only Alice and Bob can now open admin.example.com, after logging in with Cloudflare Access.

Policy labels

Every label has the form labelgate.access.<name>.<property>. The <name> is the policy name you use in access: "<name>". It follows the name rules.

PropertyRequiredDefaultExampleMeaning
policy.include.<selector>for allow and block-policy.include.emailsWho the policy applies to. Any one rule is enough.
policy.require.<selector>no-policy.require.countryEveryone must also match all of these.
policy.exclude.<selector>no-policy.exclude.emailsNever applies to these.
policy.decisionnoallowbypassWhat to do. See Decisions.
policy.namenoapp nameTeam onlyPolicy name shown in Cloudflare.
app_namenolabelgate-<name>Admin panelAccess application name shown in Cloudflare.
session_durationno24h12hHow long a login lasts. Use m or h (30m, 720h); 7d is not accepted.

Decisions

policy.decisionMeaning
allowMatching users get in after logging in. Needs an include rule.
blockMatching users are blocked. Needs an include rule.
bypassMatching requests get in without logging in.
service_authFor machines: a service token or client certificate, no login.

Selectors

Separate several values with commas.

SelectorValueExample
emailsEmail addressesalice@example.com,bob@example.com
emails_ending_inEmail domainsexample.com
ip_rangesIP addresses or CIDR ranges203.0.113.0/24,198.51.100.7
countryCountry codesUS,GB
everyoneNo value (use "")""
service_tokenService token IDs<token-id>
access_groupsAccess group IDs<group-id>
certificateNo value (use ""); any valid client certificate""
login_methodsLogin method (identity provider) IDs<idp-id>

Recipes

Everyone with a company email

labels:
  labelgate.access.staff.policy.include.emails_ending_in: "example.com"
  labelgate.tunnel.wiki.hostname: "wiki.example.com"
  labelgate.tunnel.wiki.service: "http://wiki:80"
  labelgate.tunnel.wiki.access: "staff"

Office network only

labels:
  labelgate.access.office.policy.include.ip_ranges: "203.0.113.0/24"
  labelgate.tunnel.intranet.hostname: "intranet.example.com"
  labelgate.tunnel.intranet.service: "http://intranet:80"
  labelgate.tunnel.intranet.access: "office"

Public, no login

labels:
  labelgate.access.public.policy.decision: "bypass"
  labelgate.access.public.policy.include.everyone: ""
  labelgate.tunnel.docs.hostname: "docs.example.com"
  labelgate.tunnel.docs.service: "http://docs:80"
  labelgate.tunnel.docs.access: "public"

Machines with a service token

labels:
  labelgate.access.machine.policy.decision: "service_auth"
  labelgate.access.machine.policy.include.service_token: "<token-id>"
  labelgate.tunnel.api.hostname: "api.example.com"
  labelgate.tunnel.api.service: "http://api:8080"
  labelgate.tunnel.api.access: "machine"

Company emails, US only, minus contractors

labels:
  labelgate.access.secure.policy.include.emails_ending_in: "example.com"
  labelgate.access.secure.policy.require.country: "US"
  labelgate.access.secure.policy.exclude.emails: "contractor@example.com"
  # use it with: labelgate.tunnel.<name>.access: "secure"

One policy for several hostnames

Define the policy once. Any container on the same host can use it.

services:
  policies:
    image: busybox
    command: sleep infinity    # must keep running
    labels:
      labelgate.access.team.policy.include.emails_ending_in: "example.com"

  app:
    image: nginx:alpine
    labels:
      labelgate.tunnel.app.hostname: "app.example.com"
      labelgate.tunnel.app.service: "http://app:80"
      labelgate.tunnel.app.access: "team"

Good to know

  • Where a policy can live. In the same container, or in any running container on the same host. On an agent, only containers on that agent count.
  • Same name twice. A policy in the container itself wins. Otherwise, if two other containers define different policies with that name, you get access policy "name" is defined differently on several containers of this host. Rename one.
  • Cleanup. A policy has no cleanup label. Its Access app follows the tunnel or DNS label that uses it.
  • Changes apply. Edit a label, e.g. remove an email, and Cloudflare is updated on the next sync.
  • Existing apps are not touched. If Cloudflare already has an Access app for the hostname that Labelgate did not create, you get conflict: existing record not owned by labelgate. See Ownership & Conflicts.

On this page