Skip to content

Integration with Authzed#375

Merged
guicassolato merged 3 commits intomainfrom
authzed
Feb 16, 2023
Merged

Integration with Authzed#375
guicassolato merged 3 commits intomainfrom
authzed

Conversation

@guicassolato
Copy link
Collaborator

@guicassolato guicassolato commented Feb 6, 2023

Adds built-in integration with Authzed.

Authorino will send check permission requests to Authzed/SpiceDB via GRPC.
Subject, resource and permission parameters can be set to static values or read from the Authorization JSON.

Closes #372.

Verification steps

Setup locally:

make local-setup
kubectl port-forward service/envoy 8000:8000 2>&1 >/dev/null &

Deploy a SpiceDB instance:

kubectl apply -f -<<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: spicedb
  labels:
    app: spicedb
spec:
  selector:
    matchLabels:
      app: spicedb
  template:
    metadata:
      labels:
        app: spicedb
    spec:
      containers:
      - name: spicedb
        image: authzed/spicedb
        args:
        - serve
        - "--grpc-preshared-key"
        - secret
        - "--http-enabled"
        ports:
        - containerPort: 50051
        - containerPort: 8443
  replicas: 1
---
apiVersion: v1
kind: Service
metadata:
  name: spicedb
spec:
  selector:
    app: spicedb
  ports:
    - name: grpc
      port: 50051
      protocol: TCP
    - name: http
      port: 8443
      protocol: TCP
EOF
kubectl port-forward service/spicedb 8443:8443 2>&1 >/dev/null &

Create the permission schema:

curl -X POST http://localhost:8443/v1/schema/write \
  -H 'Authorization: Bearer secret' \
  -H 'Content-Type: application/json' \
  -d @- << EOF
{
  "schema": "definition blog/user {}\ndefinition blog/post {\n\trelation reader: blog/user\n\trelation writer: blog/user\n\n\tpermission read = reader + writer\n\tpermission write = writer\n}"
}
EOF

Store relationships:

curl -X POST http://localhost:8443/v1/relationships/write \
  -H 'Authorization: Bearer secret' \
  -H 'Content-Type: application/json' \
  -d @- << EOF
{
  "updates": [
    {
      "operation": "OPERATION_CREATE",
      "relationship": {
        "resource": {
          "objectType": "blog/post",
          "objectId": "1"
        },
        "relation": "writer",
        "subject": {
          "object": {
            "objectType": "blog/user",
            "objectId": "emilia"
          }
        }
      }
    },
    {
      "operation": "OPERATION_CREATE",
      "relationship": {
        "resource": {
          "objectType": "blog/post",
          "objectId": "1"
        },
        "relation": "reader",
        "subject": {
          "object": {
            "objectType": "blog/user",
            "objectId": "beatrice"
          }
        }
      }
    }
  ]
}
EOF

Create the AuthConfig and Secrets:

kubectl apply -f -<<EOF
apiVersion: authorino.kuadrant.io/v1beta1
kind: AuthConfig
metadata:
  name: talker-api-protection
spec:
  hosts:
  - talker-api-authorino.127.0.0.1.nip.io
  identity:
  - name: friends
    apiKey:
      selector:
        matchLabels:
          app: talker-api
    credentials:
      in: authorization_header
      keySelector: APIKEY
  authorization:
  - name: authzed
    authzed:
      endpoint: spicedb:50051
      insecure: true
      sharedSecretRef:
        name: spicedb
        key: grpc-preshared-key
      subject:
        kind:
          value: blog/user
        name:
          valueFrom:
            authJSON: auth.identity.metadata.annotations.username
      resource:
        kind:
          value: blog/post
        name:
          valueFrom:
            authJSON: context.request.http.path.@extract:{"sep":"/","pos":2}
      permission:
        valueFrom:
          authJSON: context.request.http.method.@replace:{"old":"GET","new":"read"}.@replace:{"old":"POST","new":"write"}
---
apiVersion: v1
kind: Secret
metadata:
  name: spicedb
  labels:
    app: spicedb
stringData:
  grpc-preshared-key: secret
---
apiVersion: v1
kind: Secret
metadata:
  name: api-key-writer
  labels:
    authorino.kuadrant.io/managed-by: authorino
    app: talker-api
  annotations:
    username: emilia
stringData:
  api_key: IAMEMILIA
---
apiVersion: v1
kind: Secret
metadata:
  name: api-key-reader
  labels:
    authorino.kuadrant.io/managed-by: authorino
    app: talker-api
  annotations:
    username: beatrice
stringData:
  api_key: IAMBEATRICE
EOF

Send requests:

curl -H 'Authorization: APIKEY IAMEMILIA' -X GET  http://talker-api-authorino.127.0.0.1.nip.io:8000/posts/1 -i
# HTTP/1.1 200 OK
curl -H 'Authorization: APIKEY IAMEMILIA' -X POST http://talker-api-authorino.127.0.0.1.nip.io:8000/posts/1 -i
# HTTP/1.1 200 OK
curl -H 'Authorization: APIKEY IAMBEATRICE' -X GET  http://talker-api-authorino.127.0.0.1.nip.io:8000/posts/1 -i
# HTTP/1.1 200 OK
curl -H 'Authorization: APIKEY IAMBEATRICE' -X POST http://talker-api-authorino.127.0.0.1.nip.io:8000/posts/1 -i
# HTTP/1.1 403 Forbidden
# x-ext-auth-reason: PERMISSIONSHIP_NO_PERMISSION | token: GhUKEzE2NzU3MDE3MjAwMDAwMDAwMDA=

@guicassolato guicassolato self-assigned this Feb 6, 2023
@guicassolato guicassolato marked this pull request as ready for review February 7, 2023 12:38
@guicassolato guicassolato requested a review from a team February 7, 2023 13:48
@guicassolato guicassolato force-pushed the authzed branch 3 times, most recently from cc9290e to 87efa4b Compare February 9, 2023 16:48
@eguzki
Copy link
Collaborator

eguzki commented Feb 15, 2023

verification steps working 👍

eguzki
eguzki previously approved these changes Feb 15, 2023
@guicassolato guicassolato merged commit 4ab127f into main Feb 16, 2023
@guicassolato guicassolato deleted the authzed branch February 16, 2023 10:16
alechenninger added a commit to alechenninger/awesome-spicedb that referenced this pull request May 5, 2023
@guicassolato added this integration a few months back in Kuadrant/authorino#375
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Integration with Authzed

2 participants