Skip to content

Commit

Permalink
feat: expose https port from kong (#2487)
Browse files Browse the repository at this point in the history
* feat: expose https port from kong

* chore: add tls config block for future extension

* fix: init hostname with docker client

* fix: use modern tls certificate
  • Loading branch information
sweatybridge authored Jul 5, 2024
1 parent 90c8d90 commit beba533
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 54 deletions.
2 changes: 1 addition & 1 deletion internal/db/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func initSchema15(ctx context.Context, host string) error {
return err
}
return utils.DockerRunOnceWithStream(ctx, utils.Config.Auth.Image, []string{
fmt.Sprintf("API_EXTERNAL_URL=http://%s:%d", host, utils.Config.Api.Port),
"API_EXTERNAL_URL=" + utils.GetApiUrl(""),
"GOTRUE_LOG_LEVEL=error",
"GOTRUE_DB_DRIVER=postgres",
fmt.Sprintf("GOTRUE_DB_DATABASE_URL=postgresql://supabase_auth_admin:%s@%s:5432/postgres", utils.Config.Db.Password, host),
Expand Down
7 changes: 2 additions & 5 deletions internal/functions/new/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ var (
)

type indexConfig struct {
Port uint16
Slug string
URL string
Token string
}

Expand All @@ -46,13 +45,11 @@ func Run(ctx context.Context, slug string, fsys afero.Fs) error {
}
defer f.Close()
// Templatize index.ts by config.toml if available
utils.Config.Api.Port = 54321
if err := utils.LoadConfigFS(fsys); err != nil {
utils.CmdSuggestion = ""
}
config := indexConfig{
Port: utils.Config.Api.Port,
Slug: slug,
URL: utils.GetApiUrl("/functions/v1/" + slug),
Token: utils.Config.Auth.AnonKey,
}
if err := indexTemplate.Execute(f, config); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/functions/new/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func TestNewCommand(t *testing.T) {
assert.NoError(t, Run(context.Background(), "test-func", fsys))
// Validate output
funcPath := filepath.Join(utils.FunctionsDir, "test-func", "index.ts")
contains, err := afero.FileContainsBytes(fsys, funcPath, []byte(
`curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/test-func'`,
))
content, err := afero.ReadFile(fsys, funcPath)
assert.NoError(t, err)
assert.True(t, contains)
assert.Contains(t, string(content),
"curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/test-func'",
)
})

t.Run("throws error on malformed slug", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/functions/new/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Deno.serve(async (req) => {
1. Run `supabase start` (see: https://supabase.com/docs/reference/cli/supabase-start)
2. Make an HTTP request:
curl -i --location --request POST 'http://127.0.0.1:{{ .Port }}/functions/v1/{{ .Slug }}' \
curl -i --location --request POST '{{ .URL }}' \
--header 'Authorization: Bearer {{ .Token }}' \
--header 'Content-Type: application/json' \
--data '{"name":"Functions"}'
Expand Down
42 changes: 28 additions & 14 deletions internal/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ EOF
binds = append(binds, fmt.Sprintf("%s:%s:rw", hostPath, dockerPath))
}

dockerPort := uint16(8000)
if utils.Config.Api.Tls.Enabled {
dockerPort = 8443
}
if _, err := utils.DockerStart(
ctx,
container.Config{
Expand All @@ -373,17 +377,30 @@ EOF
"KONG_NGINX_PROXY_PROXY_BUFFER_SIZE=160k",
"KONG_NGINX_PROXY_PROXY_BUFFERS=64 160k",
"KONG_NGINX_WORKER_PROCESSES=1",
// Use modern TLS certificate
"KONG_SSL_CERT=/home/kong/localhost.crt",
"KONG_SSL_CERT_KEY=/home/kong/localhost.key",
},
Entrypoint: []string{"sh", "-c", `cat <<'EOF' > /home/kong/kong.yml && cat <<'EOF' > /home/kong/custom_nginx.template && ./docker-entrypoint.sh kong docker-start --nginx-conf /home/kong/custom_nginx.template
Entrypoint: []string{"sh", "-c", `cat <<'EOF' > /home/kong/kong.yml && \
cat <<'EOF' > /home/kong/custom_nginx.template && \
cat <<'EOF' > /home/kong/localhost.crt && \
cat <<'EOF' > /home/kong/localhost.key && \
./docker-entrypoint.sh kong docker-start --nginx-conf /home/kong/custom_nginx.template
` + kongConfigBuf.String() + `
EOF
` + nginxConfigEmbed + `
EOF
` + status.KongCert + `
EOF
` + status.KongKey + `
EOF
`},
},
container.HostConfig{
Binds: binds,
PortBindings: nat.PortMap{"8000/tcp": []nat.PortBinding{{HostPort: strconv.FormatUint(uint64(utils.Config.Api.Port), 10)}}},
Binds: binds,
PortBindings: nat.PortMap{nat.Port(fmt.Sprintf("%d/tcp", dockerPort)): []nat.PortBinding{{
HostPort: strconv.FormatUint(uint64(utils.Config.Api.Port), 10)},
}},
RestartPolicy: container.RestartPolicy{Name: "always"},
},
network.NetworkingConfig{
Expand All @@ -408,7 +425,7 @@ EOF
}

env := []string{
fmt.Sprintf("API_EXTERNAL_URL=http://%s:%d", utils.Config.Hostname, utils.Config.Api.Port),
"API_EXTERNAL_URL=" + utils.GetApiUrl(""),

"GOTRUE_API_HOST=0.0.0.0",
"GOTRUE_API_PORT=9999",
Expand All @@ -425,7 +442,7 @@ EOF
"GOTRUE_JWT_DEFAULT_GROUP_NAME=authenticated",
fmt.Sprintf("GOTRUE_JWT_EXP=%v", utils.Config.Auth.JwtExpiry),
"GOTRUE_JWT_SECRET=" + utils.Config.Auth.JwtSecret,
fmt.Sprintf("GOTRUE_JWT_ISSUER=http://%s:%d/auth/v1", utils.Config.Hostname, utils.Config.Api.Port),
"GOTRUE_JWT_ISSUER=" + utils.GetApiUrl("/auth/v1"),

fmt.Sprintf("GOTRUE_EXTERNAL_EMAIL_ENABLED=%v", utils.Config.Auth.Email.EnableSignup),
fmt.Sprintf("GOTRUE_MAILER_SECURE_EMAIL_CHANGE_ENABLED=%v", utils.Config.Auth.Email.DoubleConfirmChanges),
Expand All @@ -441,13 +458,10 @@ EOF
fmt.Sprintf("GOTRUE_SMTP_SENDER_NAME=%s", utils.Config.Auth.Email.Smtp.SenderName),
fmt.Sprintf("GOTRUE_SMTP_MAX_FREQUENCY=%v", utils.Config.Auth.Email.MaxFrequency),

// TODO: To be reverted to `/auth/v1/verify` once
// https://github.com/supabase/supabase/issues/16100
// is fixed on upstream GoTrue.
fmt.Sprintf("GOTRUE_MAILER_URLPATHS_INVITE=http://%s:%d/auth/v1/verify", utils.Config.Hostname, utils.Config.Api.Port),
fmt.Sprintf("GOTRUE_MAILER_URLPATHS_CONFIRMATION=http://%s:%d/auth/v1/verify", utils.Config.Hostname, utils.Config.Api.Port),
fmt.Sprintf("GOTRUE_MAILER_URLPATHS_RECOVERY=http://%s:%d/auth/v1/verify", utils.Config.Hostname, utils.Config.Api.Port),
fmt.Sprintf("GOTRUE_MAILER_URLPATHS_EMAIL_CHANGE=http://%s:%d/auth/v1/verify", utils.Config.Hostname, utils.Config.Api.Port),
"GOTRUE_MAILER_URLPATHS_INVITE=" + utils.GetApiUrl("/auth/v1/verify"),
"GOTRUE_MAILER_URLPATHS_CONFIRMATION=" + utils.GetApiUrl("/auth/v1/verify"),
"GOTRUE_MAILER_URLPATHS_RECOVERY=" + utils.GetApiUrl("/auth/v1/verify"),
"GOTRUE_MAILER_URLPATHS_EMAIL_CHANGE=" + utils.GetApiUrl("/auth/v1/verify"),
"GOTRUE_RATE_LIMIT_EMAIL_SENT=360000",

fmt.Sprintf("GOTRUE_EXTERNAL_PHONE_ENABLED=%v", utils.Config.Auth.Sms.EnableSignup),
Expand Down Expand Up @@ -591,7 +605,7 @@ EOF
)
} else {
env = append(env,
fmt.Sprintf("GOTRUE_EXTERNAL_%s_REDIRECT_URI=http://%s:%d/auth/v1/callback", strings.ToUpper(name), utils.Config.Hostname, utils.Config.Api.Port),
fmt.Sprintf("GOTRUE_EXTERNAL_%s_REDIRECT_URI=%s", strings.ToUpper(name), utils.GetApiUrl("/auth/v1/callback")),
)
}

Expand Down Expand Up @@ -910,7 +924,7 @@ EOF
"STUDIO_PG_META_URL=http://" + utils.PgmetaId + ":8080",
"POSTGRES_PASSWORD=" + dbConfig.Password,
"SUPABASE_URL=http://" + utils.KongId + ":8000",
fmt.Sprintf("SUPABASE_PUBLIC_URL=%s:%v/", utils.Config.Studio.ApiUrl, utils.Config.Api.Port),
"SUPABASE_PUBLIC_URL=" + utils.Config.Studio.ApiUrl,
"AUTH_JWT_SECRET=" + utils.Config.Auth.JwtSecret,
"SUPABASE_ANON_KEY=" + utils.Config.Auth.AnonKey,
"SUPABASE_SERVICE_KEY=" + utils.Config.Auth.ServiceRoleKey,
Expand Down
6 changes: 3 additions & 3 deletions internal/start/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ func TestDatabaseStart(t *testing.T) {
},
}})
}
gock.New("127.0.0.1").
gock.New(utils.GetApiUrl("")).
Head("/rest-admin/v1/ready").
Reply(http.StatusOK)
gock.New("127.0.0.1").
gock.New(utils.GetApiUrl("")).
Head("/functions/v1/_internal/health").
Reply(http.StatusOK)
// Seed tenant services
Expand All @@ -183,7 +183,7 @@ func TestDatabaseStart(t *testing.T) {
Health: &types.Health{Status: "healthy"},
},
}})
gock.New("127.0.0.1").
gock.New(utils.GetApiUrl("")).
Get("/storage/v1/bucket").
Reply(http.StatusOK).
JSON([]storage.BucketResponse{})
Expand Down
30 changes: 30 additions & 0 deletions internal/status/kong.local.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-----BEGIN CERTIFICATE-----
MIIFJTCCAw2gAwIBAgIURUQAFIOhttOg0D12xZvx+pScX4cwDQYJKoZIhvcNAQEL
BQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTI0MDcwNTE0MjI1NFoXDTM0MDcw
MzE0MjI1NFowFDESMBAGA1UEAwwJbG9jYWxob3N0MIICIjANBgkqhkiG9w0BAQEF
AAOCAg8AMIICCgKCAgEAyL79RrCl78/XutDm7tVNyIJNZqbNSfX+3AsnP1dQc37Q
7QkxmNJ2EmfklTLyvY1A+CY/o+3S1blK+O8lm40yTHP+GC30InKS7lSnCUNqSrHy
DaGNShtphqJoao9vo69/anI6I2tKc8VQ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bUEwovDEcPUaLc3d5gCk/dvH9aR8mVLEcs7lMyHSsiG1Co1QXg6UjpMCrIV
Tvk+MrSQVVxw0rKS4H7ZEhfC2PfEVe1az24HrrPWJDZKnRhv1Ohj4jsxkBSprXsu
3qxcMCr7yr4hjsu9BsKtmvRfHTrMOwY1WtZBcl7hjgAItfMs29Fd4Tkn0aZs5f+n
GSwG5BeJL3kfEKwOjw8j+EAqRBu/7mbcoUyErnuLK7FimU4jtL8VX03n6nTgLJzS
xjUTaplgerrazAvV31vMaiINoaGM0RDnGjSCgZT4Va33noqo3qVJJOnWpheFmaXq
bUMQtwrAWTt12NECJt1nWAT4VWhfDoBE7A==
-----END CERTIFICATE-----
52 changes: 52 additions & 0 deletions internal/status/kong.local.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-----BEGIN PRIVATE KEY-----
MIIJQwIBADANBgkqhkiG9w0BAQEFAASCCS0wggkpAgEAAoICAQDIvv1GsKXvz9e6
0Obu1U3Igk1mps1J9f7cCyc/V1BzftDtCTGY0nYSZ+SVMvK9jUD4Jj+j7dLVuUr4
7yWbjTJMc/4YLfQicpLuVKcJQ2pKsfINoY1KG2mGomhqj2+jr39qcjoja0pzxVDa
15u8xXOaOuy8a1ZxThyLLNtS16spBqwiKfwDDmAMIuzBU1meXrBS+VyBsuaJMs9o
kqc2xthwEnFJPZIXFjmrlhbOW6Cb1DTf3XdcHritWgoDJDzphpuXhRfJVeSfIlxu
l+eyafo5HGdkl5ue5TQ73LzKz8T2EbDmuRVObtCPOxtzMxg6C+k9tHWaLINBpnVs
bKP1KYmjUfl7XbE7RE2L5rC4yem3dwHw15Oxly06CcRAHUaJ2rZdxhXjx7Y4YlM2
1VVU+7VQoI14m6vUB8s09HPx8mrToD5fLnF5E0YWxp5hG38g778JlQ4dxj+9HnmL
w955OP1wHV/0qN+ZBV463bnCJ3GuEUpGSlGADWh9Dg7z+A9I5yeuhCBKzZaGv1Fc
gXvW5IsnJlTQgHGu7RzOe4SlfGSHug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OvtYDnj
3QhvRrUpxIEgimXjm8g5JYKYGHODL7LpME5yrk0m1FjVUB1yncojeETkVXxxeUP2
+9kCggEBAMcsbVgvNVwt7bBf1zuw/AiXX4e3Lc2019M/z9SFsu6WfA+OgNMM90lQ
Eqq6XGhlNSDw+Zz8JDPq5RIM3rGlKFWAFSQ+h/4q67LgIDTOKjk7EXpbt/bJIFud
xyiG6uvMSX35OZ5vLsBvv2AzkzR5x2v9kcKD1b5eWn6haEXMOX/X0yGHux9vRylw
zEW1IOA5+2FZvZhwLy+F3XxY3Vrf04mWGyCxiBy4aMvbmp0BcWBg+tRLOmc6h7rX
2Bi8TfeE+GnmJkezD44MH/S+EEfafOkiDqoDCxxgYpJzXiE6DZNEUGdpUm87hdx4
MviBG5FCtAAJp+5btxy72dIKBYBVUJ8=
-----END PRIVATE KEY-----
51 changes: 43 additions & 8 deletions internal/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package status

import (
"context"
"crypto/tls"
"crypto/x509"
_ "embed"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -38,8 +41,8 @@ func (c *CustomName) toValues(exclude ...string) map[string]string {
c.DbURL: fmt.Sprintf("postgresql://%s@%s:%d/postgres", url.UserPassword("postgres", utils.Config.Db.Password), utils.Config.Hostname, utils.Config.Db.Port),
}
if utils.Config.Api.Enabled && !utils.SliceContains(exclude, utils.RestId) && !utils.SliceContains(exclude, utils.ShortContainerImageName(utils.Config.Api.Image)) {
values[c.ApiURL] = fmt.Sprintf("http://%s:%d", utils.Config.Hostname, utils.Config.Api.Port)
values[c.GraphqlURL] = fmt.Sprintf("http://%s:%d/graphql/v1", utils.Config.Hostname, utils.Config.Api.Port)
values[c.ApiURL] = utils.GetApiUrl("")
values[c.GraphqlURL] = utils.GetApiUrl("/graphql/v1")
}
if utils.Config.Studio.Enabled && !utils.SliceContains(exclude, utils.StudioId) && !utils.SliceContains(exclude, utils.ShortContainerImageName(utils.Config.Studio.Image)) {
values[c.StudioURL] = fmt.Sprintf("http://%s:%d", utils.Config.Hostname, utils.Config.Studio.Port)
Expand All @@ -53,7 +56,7 @@ func (c *CustomName) toValues(exclude ...string) map[string]string {
values[c.InbucketURL] = fmt.Sprintf("http://%s:%d", utils.Config.Hostname, utils.Config.Inbucket.Port)
}
if utils.Config.Storage.Enabled && !utils.SliceContains(exclude, utils.StorageId) && !utils.SliceContains(exclude, utils.ShortContainerImageName(utils.Config.Storage.Image)) {
values[c.StorageS3URL] = fmt.Sprintf("http://%s:%d/storage/v1/s3", utils.Config.Hostname, utils.Config.Api.Port)
values[c.StorageS3URL] = utils.GetApiUrl("/storage/v1/s3")
values[c.StorageS3AccessKeyId] = utils.Config.Storage.S3Credentials.AccessKeyId
values[c.StorageS3SecretAccessKey] = utils.Config.Storage.S3Credentials.SecretAccessKey
values[c.StorageS3Region] = utils.Config.Storage.S3Credentials.Region
Expand All @@ -69,7 +72,6 @@ func Run(ctx context.Context, names CustomName, format string, fsys afero.Fs) er
if err := assertContainerHealthy(ctx, utils.DbId); err != nil {
return err
}

stopped, err := checkServiceHealth(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -130,20 +132,53 @@ func IsServiceReady(ctx context.Context, container string) error {
return assertContainerHealthy(ctx, container)
}

var (
//go:embed kong.local.crt
KongCert string
//go:embed kong.local.key
KongKey string
)

// To regenerate local certificate pair:
//
// openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 \
// -nodes -keyout kong.local.key -out kong.local.crt -subj "/CN=localhost" \
// -addext "subjectAltName=DNS:localhost,IP:127.0.0.1"
func NewKongClient() *http.Client {
client := &http.Client{
Timeout: 10 * time.Second,
}
if t, ok := http.DefaultTransport.(*http.Transport); ok {
pool, err := x509.SystemCertPool()
if err != nil {
fmt.Fprintln(utils.GetDebugLogger(), err)
pool = x509.NewCertPool()
}
// No need to replace TLS config if we fail to append cert
if pool.AppendCertsFromPEM([]byte(KongCert)) {
rt := t.Clone()
rt.TLSClientConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
RootCAs: pool,
}
client.Transport = rt
}
}
return client
}

var (
healthClient *fetcher.Fetcher
healthOnce sync.Once
)

func checkHTTPHead(ctx context.Context, path string) error {
healthOnce.Do(func() {
server := fmt.Sprintf("http://%s:%d", utils.Config.Hostname, utils.Config.Api.Port)
server := utils.GetApiUrl("")
header := func(req *http.Request) {
req.Header.Add("apikey", utils.Config.Auth.AnonKey)
}
client := &http.Client{
Timeout: 10 * time.Second,
}
client := NewKongClient()
healthClient = fetcher.NewFetcher(
server,
fetcher.WithHTTPClient(client),
Expand Down
Loading

0 comments on commit beba533

Please sign in to comment.