Skip to content

Conversation

francescopagnamenta
Copy link

Improvement proposal: allow to defined preshared keys in a secret

Intro

Using OpenFGA with the "pre-shared" authentication mode requires defining API keys in the values file.

Proposal

Alternatively allow specifying a secret to define a comma-separated list of pre-shared keys.

{{- if .Values.authn.preshared.keysSecret }}
- name: OPENFGA_AUTHN_PRESHARED_KEYS
    valueFrom:
    secretKeyRef:
        name: "{{ .Values.authn.preshared.keysSecret }}"
        key: "presharedKeys"
{{- end }}

Testing

Tested locally on minikube as follows

Testing the configuration with preshared.keys (on clear)

helm install openfga ./openfga \
  --set datastore.engine=mysql \
  --set datastore.uri="root:password@tcp(openfga-mysql.default.svc.cluster.local:3306)/mysql?parseTime=true" \
  --set datastore.applyMigrations=true \
  --set datastore.waitForMigrations=true \
  --set datastore.migrationType=initContainer \
  --set mysql.enabled=true \
  --set mysql.auth.rootPassword=password \
  --set mysql.auth.database=mysql \
  --set authn.method=preshared \
  --set-json authn.preshared.keys='["key1"]'

Port forwarding for testing

  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=openfga,app.kubernetes.io/instance=openfga" -o jsonpath="{.items[0].metadata.name}")
  export CONTAINER_PORT=$(kubectl get pod --namespace default $POD_NAME -o jsonpath="{.spec.containers[0].ports[1].containerPort}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace default port-forward $POD_NAME 8080:$CONTAINER_PORT

Testing an endpoint

curl --location 'http://localhost:8080/stores' --header 'Authorization: Bearer key1'
{"stores":[], "continuation_token":""}
curl --location 'http://localhost:8080/stores' --header 'Authorization: Bearer key2'
{"code":"unauthenticated","message":"unauthenticated"}

Testing the configuration with preshared.keys (on clear)

Define the secret

kubectl create secret -n default generic openfga-secrets --from-literal=presharedKeys=key1,key2

Install openfga with presahred (run if you have the previous running instance helm uninstall openfga)

helm install openfga ./openfga \
  --set datastore.engine=mysql \
  --set datastore.uri="root:password@tcp(openfga-mysql.default.svc.cluster.local:3306)/mysql?parseTime=true" \
  --set datastore.applyMigrations=true \
  --set datastore.waitForMigrations=true \
  --set datastore.migrationType=initContainer \
  --set mysql.enabled=true \
  --set mysql.auth.rootPassword=password \
  --set mysql.auth.database=mysql  \
  --set authn.method=preshared  \
  --set authn.preshared.keysSecret=openfga-secrets

Testing an endpoint (user the port-forwarding statement defined before)

curl --location 'http://localhost:8080/stores' --header 'Authorization: Bearer key1'
{"stores":[], "continuation_token":""}
curl --location 'http://localhost:8080/stores' --header 'Authorization: Bearer key2'
{"stores":[], "continuation_token":""}
curl --location 'http://localhost:8080/stores' --header 'Authorization: Bearer key3'
{"code":"unauthenticated","message":"unauthenticated"}

@francescopagnamenta francescopagnamenta requested review from a team as code owners January 24, 2025 16:15
Copy link

linux-foundation-easycla bot commented Jan 24, 2025

CLA Missing ID CLA Not Signed

@rhamzeh
Copy link
Member

rhamzeh commented Jan 28, 2025

Thanks for your PR @francescopagnamenta! In order to review and merge this - may we ask you to sign the CLA posted by the bot above?

This is required for us to accept contributions under the CNCF and Linux Foundation rules

@francescopagnamenta francescopagnamenta requested a review from a team as a code owner January 29, 2025 10:08
@francescopagnamenta
Copy link
Author

Dear @rhamzeh, apologies for the very late reply! This PR might no longer be relevant.
I’ve signed the CLA, but I’m not sure if that’s sufficient to proceed with this PR in its current state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants