ory / kratos

The most scalable and customizable identity server on the market. Replace your Homegrown, Auth0, Okta, Firebase with better UX and DX. Has all the tablestakes: Passkeys, Social Sign In, Multi-Factor Auth, SMS, SAML, TOTP, and more. Written in Go, cloud native, headless, API-first. Available as a service on Ory Network and for self-hosters.
https://www.ory.sh/?utm_source=github&utm_medium=banner&utm_campaign=kratos
Apache License 2.0
11.23k stars 961 forks source link

Send Email via HTTP and not SMTP to Sendgrid #3693

Closed hiltpold closed 6 months ago

hiltpold commented 9 months ago

Preflight checklist

Ory Network Project

No response

Describe the bug

I am using Ory Kratos v1.0.0 and intend to send my emails over HTTP. As per the documentation, this is a viable option. This becomes necessary for me since I am working on Digital Ocean, where SMTP traffic is blocked. I am interested in integrating SendGrid into my Ory Kratos setup

kratos.yaml

courier:
  delivery_strategy: http
  http:
    request_config:
      url: https://api.sendgrid.com/v3/mail/send
      method: POST
      body: base64://ZnVuY3Rpb24oY3R4KSB7CiAgewogICAgcGVyc29uYWxpemF0aW9uczogWwogICAgICB7CiAgICAgICAgdG86IFsKICAgICAgICAgIHsKICAgICAgICAgICAgZW1haWw6ICJ0ZXN0QGRvbWFpbi5jb20iLAogICAgICAgICAgfSwKICAgICAgICBdLAogICAgICAgIHN1YmplY3Q6ICJMYWtlbGFuZGxhYiBBY2NvdW50IFZlcmlmaWNhdGlvbiIsCiAgICAgIH0sCiAgICBdLAogICAgZnJvbTogewogICAgICBlbWFpbDogImV4YW1wbGVAZG9tYWluLmNvbSIsCiAgICB9LAogICAgY29udGVudDogWwogICAgICB7CiAgICAgICAgdHlwZTogInRleHQvcGxhaW4iLAogICAgICAgIHZhbHVlOiAiT3J5IEtyYXRvcyBTZW5kZ3JpZCBpbnRlZ3JhdGlvbiIsCiAgICAgIH0sCiAgICBdLAogIH0KfQ==
      headers:
        "Content-Type": "application/json"
      auth:
        type: api_key
        config:
          name: Authorization
          value: Bearer API_KEY
          in: header

The base64 encoded String is given by:

function(ctx) {
  {
    personalizations: [
      {
        to: [
          {
            email: "test@domain.com",
          },
        ],
        subject: "Lakelandlab Account Verification",
      },
    ],
    from: {
      email: "example@domain.com",
    },
    content: [
      {
        type: "text/plain",
        value: "Ory Kratos Sendgrid integration",
      },
    ],
  }
}

Im not yet sure this will work, but it does not seem to be a problem yet.

Reproducing the bug

docker-compose up

Relevant log output

kratos-1          | time=2024-01-14T15:49:15Z level=error msg=Unable to instantiate configuration. audience=application error=map[message:I[#/courier/smtp] S[#/properties/courier/properties/smtp/required] missing properties: "connection_uri" stack_trace:stack trace could not be recovered from error type *jsonschema.ValidationError] service_name=Ory Kratos service_version=v1.0.0
kratos-1          | Error: I[#/courier/smtp] S[#/properties/courier/properties/smtp/required] missing properties: "connection_uri"
kratos-1          | Usage:
kratos-1          |   kratos serve [flags]
kratos-1          | 
kratos-1          | Flags:
kratos-1          |   -c, --config strings   Path to one or more .json, .yaml, .yml, .toml config files. Values are loaded in the order provided, meaning that the last config file overwrites values from the previous config file.
kratos-1          |       --dev              Disables critical security features to make development easier
kratos-1          |   -h, --help             help for serve
kratos-1          |       --sqa-opt-out      Disable anonymized telemetry reports - for more information please visit https://www.ory.sh/docs/ecosystem/sqa
kratos-1          |       --watch-courier    Run the message courier as a background task, to simplify single-instance setup
kratos-1          | 
kratos-1          | I[#/courier/smtp] S[#/properties/courier/properties/smtp/required] missing properties: "connection_uri"

Relevant configuration

version: '3.7'

services:
  db:
    image: postgres:latest
    volumes:
      - postgres:/var/lib/postgresql/data
    env_file: ./.env
    ports:
      - '5432:5432'
    networks:
      - intranet

  kratos-migrate:
    depends_on:
      - db
    image: oryd/kratos:v1.0.0
    env_file:
      - ./.env
    volumes:
      - type: bind
        source: ./kratos
        target: /etc/config/kratos
    command: -c /etc/config/kratos/kratos.yml migrate sql -e --yes
    restart: on-failure
    networks:
      - intranet

  kratos-cli:
    image: oryd/kratos:v1.0.0
    environment:
      - KRATOS_ADMIN_URL=http://kratos:4434

  kratos:
    depends_on:
      - kratos-migrate
    image: oryd/kratos:latest
    ports:
      - '4433:4433'
      - '4434:4434'
    env_file:
      - ./.env
    volumes:
      - type: bind
        source: ./kratos
        target: /etc/config/kratos
    # do not use --dev in production
    command: serve -c /etc/config/kratos/kratos.yml --dev --watch-courier
    restart: unless-stopped
    networks:
      - intranet

  mailslurper:
    image: oryd/mailslurper:latest-smtps
    ports:
      - '4436:4436'
      - '4437:4437'
    networks:
      - intranet

networks:
  intranet:

volumes:
  postgres:
    name: orystarter

Version

v1.0.0

On which operating system are you observing this issue?

macOS

In which environment are you deploying?

Docker Compose

Additional Context

Having a look at the schema iit appears that the connection_uri is a required field, even though we are not utilizing the smtp configuratio at all."

Gromitmugs commented 9 months ago

Hey man, I got the same error as well. Looks like the value for connection_uri in smtp is still required even though we choose to use http. The way to fix this is to simply put the dummy value for it.

Edit:

I believe they also fixed it already as well. https://github.com/ory/kratos/issues/3559

matthiastz commented 7 months ago

did you guys try it with v1.1.0 instead of 1.0 as well? Because I doubt that the issue is fixed with any of these versions, unfortunately. -> https://github.com/ory/kratos/issues/3559 was not fixed and https://github.com/ory/kratos/pull/3649 was a feature PR about sms, seems not related to me...

when I try to run kratos with docker-compose and kratos v1.1.0

kratos.yml

...
courier:
  delivery_strategy: http
  http:
    request_config:
      url: http://localhost:8080/api/someUri

I still get this error:

The configuration contains values or keys which are invalid:
courier.smtp.connection_uri: <nil>
                             ^-- one or more required properties are missing

time=2024-03-19T15:22:01Z level=error msg=Unable to instantiate configuration. audience=application error=map[message:I[#/courier/smtp] S[#/properties/courier/properties/smtp/required] missing properties: "connection_uri"] service_name=Ory Kratos service_version=v1.1.0
Error: I[#/courier/smtp] S[#/properties/courier/properties/smtp/required] missing properties: "connection_uri"

same issue occurs when I try to deploy it on k8s with v1.1.0 (tried both chart version 0.35.0 and 0.40.0):

kratos config:

....
        courier:
          delivery_strategy: http
          http:
            request_config:
              url: <REDACTED>
              headers:
                ...

kratos pod log:

The configuration contains values or keys which are invalid:
courier.smtp.connection_uri: <nil>
^-- one or more required properties are missing

time=2024-03-19T14:54:59Z level=error msg=Unable to instantiate configuration. audience=application error=map[message:I[#/courier/smtp] S[#/properties/courier/properties/smtp/required] missing properties: "connection_uri"] service_name=Ory Kratos service_version=v1.1.0
Error: I[#/courier/smtp] S[#/properties/courier/properties/smtp/required] missing properties: "connection_uri"
Usage:
kratos serve [flags]

Flags:
-c, --config strings Path to one or more .json, .yaml, .yml, .toml config files. Values are loaded in the order provided, meaning that the last config file overwrites values from the previous config file.
--dev Disables critical security features to make development easier
-h, --help help for serve
--sqa-opt-out Disable anonymized telemetry reports - for more information please visit https://www.ory.sh/docs/ecosystem/sqa
--watch-courier Run the message courier as a background task, to simplify single-instance setup

I[#/courier/smtp] S[#/properties/courier/properties/smtp/required] missing properties: "connection_uri"