Subscribe
AWS

Passwordless sign-in for an open-source BI tool: gate the UI at the ALB, leave the API to API keys

The free edition of our BI tool has no SSO. An ALB OIDC gate plus a small bridge that syncs the user, sets a random password and hands over the session gave us passwordless sign-in. Later we took /api/* out of the gate so automation uses API keys.

Passwordless sign-in for an open-source BI tool: gate the UI at the ALB, leave the API to API keys

The BI tool we self-host is open source, and its free edition has no SSO at all. OIDC, JWT and SAML sign-in are paid features. So every person had a local username and password inside the tool, separate from everything else, and removing someone meant remembering to remove them there too.

I had already put PgHero behind an ALB login (Putting internal tools behind your own login with ALB authenticate-oidc). The same gate works here, but only half way. PgHero doesn't care who you are. A BI tool does: it has users, groups and permissions on every dashboard. A gate in front lets the right people reach the login page, and then they still have to type a password. Not what we wanted.

The idea

Two pieces. First, the gate: the ALB listener rule for the tool's host runs authenticate-oidc against our internal admin console, same as PgHero, so only roles that have the BI product get through. Second, a bridge: one endpoint on the tool's own host, routed to the console's backend instead of the tool. When you open it, the bridge reads who the ALB signed in, makes sure that person has an active user in the tool with the right groups, sets a fresh random password, logs in with it through the tool's API and hands the browser the tool's own session cookies.

Nobody ever knows the password. It is used once and thrown away.

It sounds hacky, and honestly it is a bit hacky. But it uses only the tool's public admin API, so a version upgrade doesn't break it unless that API changes.

The ALB rule table

Rule order is where this design lives or dies. The ALB checks rules from the lowest priority number and stops at the first match. This is our table, with the tool's host on every rule:

Priority Match Action
5 header X-Bridge-Secret = the bridge secret forward to the tool, no login
6 POST on the tool's password login and reset paths fixed 403
7 /sso/* authenticate-oidc, then forward to the console backend
8 /auth/login* redirect to /sso/login keeping the query
9 /api/* forward to the tool, no login
13 everything else authenticate-oidc, then forward to the tool

Rule 5 is needed because the bridge calls the tool through the same public hostname. Without it, the bridge's own API calls would hit rule 13 and get a 302 to the login page. Rule 6 is what makes it passwordless for real: even if someone knows an old password, the tool's login endpoint answers 403 at the ALB and the request never reaches the tool. Rule 8 catches the tool's own login page (when your session expires, the tool sends you there) and turns it into a trip through the bridge.

Rule 9 came later. More on that below.

Here are rules 6 and 9 in Terraform. The full set is six aws_lb_listener_rule resources and I checked it with terraform validate (Terraform 1.17.1, AWS provider 6.74.0):

resource "aws_lb_listener_rule" "block_password_login" {
  listener_arn = var.listener_arn
  priority     = 6

  condition {
    host_header { values = [var.host] }
  }
  condition {
    http_request_method { values = ["POST"] }
  }
  condition {
    path_pattern { values = var.login_paths }
  }

  action {
    type = "fixed-response"
    fixed_response {
      content_type = "text/plain"
      message_body = "Password sign-in is disabled. Use the console."
      status_code  = "403"
    }
  }
}

resource "aws_lb_listener_rule" "api_open" {
  listener_arn = var.listener_arn
  priority     = 9

  condition {
    host_header { values = [var.host] }
  }
  condition {
    path_pattern { values = ["/api/*"] }
  }

  action {
    type             = "forward"
    target_group_arn = var.bi_tg_arn
  }
}

var.login_paths is whatever your tool uses for password login, forgotten password and reset password. Check its API docs; ours were three paths under /api/. That is exactly why rule 6 has to sit above rule 9. Swap them and the password login is open again.

One thing about the bypass header. Its value sits in the listener rule config, so anyone with elasticloadbalancing:DescribeRules can read it, and it is in Terraform state too. Treat it like a password with a smaller blast radius: it opens the tool's API, which still wants its own auth.

Who is calling: x-amzn-oidc-data

The bridge sits on a target group that is also reachable through the console's normal hostname. On that hostname anybody can send any header they like. So the bridge must not trust an x-amzn-oidc-data header just because it is present. It checks the ES256 signature with the ALB's public key, checks the signer is our ALB's ARN, checks the client is the BI tool's client id, checks expiry, and only then reads the email.

func Email(tok, albARN, clientID string, key KeyFunc, now time.Time) (string, error) {
    parts := strings.Split(tok, ".")
    if len(parts) != 3 {
        return "", ErrBad
    }
    rawHead, err := seg(parts[0])
    if err != nil {
        return "", ErrBad
    }
    var head struct {
        Alg    string `json:"alg"`
        Kid    string `json:"kid"`
        Signer string `json:"signer"`
        Client string `json:"client"`
        Exp    int64  `json:"exp"`
    }
    if json.Unmarshal(rawHead, &head) != nil || head.Alg != "ES256" ||
        head.Signer != albARN || head.Client != clientID || head.Exp <= now.Unix() {
        return "", ErrBad
    }
    sig, err := seg(parts[2])
    if err != nil || len(sig) != 64 {
        return "", ErrBad
    }
    pub, err := key(head.Kid)
    if err != nil {
        return "", ErrBad
    }
    digest := sha256.Sum256([]byte(parts[0] + "." + parts[1]))
    r := new(big.Int).SetBytes(sig[:32])
    s := new(big.Int).SetBytes(sig[32:])
    if !ecdsa.Verify(pub, digest[:], r, s) {
        return "", ErrBad
    }
    rawBody, err := seg(parts[1])
    if err != nil {
        return "", ErrBad
    }
    var body struct {
        Email string `json:"email"`
    }
    if json.Unmarshal(rawBody, &body) != nil || body.Email == "" {
        return "", ErrBad
    }
    return strings.ToLower(body.Email), nil
}

seg strips = before decoding, because the ALB pads its base64url and Go's RawURLEncoding refuses padding. KeyFunc fetches https://public-keys.auth.elb.ap-south-1.amazonaws.com/<kid> and caches the PEM per key id. When I wrote the test for this post, it signs tokens with a throwaway P-256 key and checks that a wrong client, a wrong signer, an expired header and a forged signature are all refused. All four failed closed on the first run itself, which was a small relief.

The sign-in itself

After the email is known, the bridge does this, in order:

  1. If the browser already sent a valid tool session for the same email, keep it.
  2. Otherwise find the user by email. Create it if missing, reactivate it if it was deactivated.
  3. Sync groups from the console role.
  4. Unless the session was kept, set a random password, log in with it and return the session cookies.

The password is 32 random bytes as base64url with -Aa1 stuck on the end, because the tool's complexity check wants a symbol, an uppercase letter, a lowercase letter and a digit. Base64 output doesn't promise any of them.

Group sync needed the most thought. The rule we landed on: console admins get the tool's admin group and everybody else is taken out of it. Other groups are added and removed only if some console role maps to them. A group nobody maps is left alone. That way the tool's admins can keep a hand-made group for a one-off project and the bridge won't wipe it at every sign-in.

This also has a side effect worth knowing before you switch it on. One person who was admin inside the tool directly, but not admin in the console, would lose admin at their first gated sign-in. That is correct by the rule. Still, tell people before, not after.

Afterwards: only the UI behind the gate

The first version gated everything on the host, /api/* too. That is fine for people and useless for automation. A script holding one of the tool's API keys has no browser and cannot do an OIDC redirect, so every call would get a 302.

On 20 November we added rule 9, because we actually wanted only the UI gated. Now /api/* goes straight to the tool, and the tool's own auth (API key or session cookie) decides. The browser UI still can't be reached without the gate, and the password login stays blocked by rule 6 above it. The UI's own calls to /api/* carry the session cookie the bridge gave, so they keep working.

Rule changes took about a minute to apply for us. Testing right after an edit and seeing the old behaviour is normal, don't revert back in a panic.

What I would check before calling it done

A curl without cookies to the UI path should give a 302 to the console. A curl with a valid API key to /api/ should give 200. A POST to the login path should give 403 from the ALB, and you can tell it's the ALB because the body is your fixed text. If the third one returns the tool's own error JSON, the order is wrong.

If you are setting up the role side as well, the RequireMFA post is a good reminder of what happens when a gate is stricter than the path to satisfy it.