ankane / authtrail

Track Devise login activity

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Extended documentation: where should it go?

mroach opened this issue · comments

I just got this setup in our environment and it's solving our needs perfectly!

I wanted to share a bit of setup that might be useful to others, but didn't want to open a PR until chatting about where it should go.

The situation: when running apps on Google Cloud, their load balancers/ingresses will provide client geocode information to your application so you don't have to do it yourself.

config/initializers/authtrail.rb

AuthTrail.geocode = false

AuthTrail.transform_method = lambda do |data, request|
  data[:country] = request.headers['HTTP_X_CLIENT_REGION']
  data[:region] = request.headers['HTTP_X_CLIENT_REGION_SUBDIVISION']
  data[:city] = request.headers['HTTP_X_CLIENT_CITY']

  if (value = request.headers['HTTP_X_CLIENT_CITY_LATLONG'])
    lat, long = value.split(',').map(&:to_f)
    data[:latitude] = lat
    data[:longitude] = long
  end
end

When using Kubernetes, you get these headers by creating a BackendConfig and wiring it up to your Service

---
apiVersion: cloud.google.com/v1
kind: BackendConfig
metadata:
  name: myapp-backend-beconf
  labels:
    app: myapp
    tier: backend
spec:
  customRequestHeaders:
    headers:
      - "X-Client-Region:{client_region}"
      - "X-Client-Region-Subdivision:{client_region_subdivision}"
      - "X-Client-City:{client_city}"
      - "X-Client-City-LatLong:{client_city_lat_long}"

---
apiVersion: v1
kind: Service
metadata:
  name: myapp-backend
  labels:
    app: myapp
    tier: backend
  annotations:
    cloud.google.com/backend-config: '{"default": "myapp-backend-beconf"}'

See https://cloud.google.com/load-balancing/docs/custom-headers#variables

Google puts the country code in the 'region' field, probably because of Hong Kong, Macau, Taiwan, Kosovo, etc...

Hey @mroach, thanks for sharing! Just added a section on load balancer geocoding to the readme with a link to this example (this seems like a good place for it).

Cool! I'll close this then as there's no action to be taken.