Skip to content

Commit f5bbb50

Browse files
easyCZroboquat
authored andcommitted
[public-api] Parametrize connection URL based on token
1 parent 81b94dd commit f5bbb50

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

components/public-api-server/pkg/proxy/conn.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ func NewConnectionPool(address *url.URL, poolSize int) (*ConnectionPool, error)
102102
return nil, errors.New("unknown token type")
103103
}
104104

105-
conn, err := gitpod.ConnectToServer(address.String(), opts)
105+
endpoint, err := getEndpointBasedOnToken(token, address)
106+
if err != nil {
107+
return nil, fmt.Errorf("failed to construct endpoint: %w", err)
108+
}
109+
110+
conn, err := gitpod.ConnectToServer(endpoint, opts)
106111
if err != nil {
107112
return nil, fmt.Errorf("failed to create new connection to server: %w", err)
108113
}
@@ -140,3 +145,14 @@ func (p *ConnectionPool) Get(ctx context.Context, token auth.Token) (gitpod.APII
140145

141146
return conn, nil
142147
}
148+
149+
func getEndpointBasedOnToken(t auth.Token, u *url.URL) (string, error) {
150+
switch t.Type {
151+
case auth.AccessTokenType:
152+
return fmt.Sprintf("%s/api/v1", u.String()), nil
153+
case auth.CookieTokenType:
154+
return fmt.Sprintf("%s/api/gitpod", u.String()), nil
155+
default:
156+
return "", errors.New("unknown token type")
157+
}
158+
}

components/public-api-server/pkg/proxy/conn_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package proxy
66

77
import (
88
"context"
9+
"net/url"
910
"testing"
1011

1112
gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"
@@ -47,3 +48,16 @@ func TestConnectionPool(t *testing.T) {
4748
require.True(t, pool.cache.Contains(barToken))
4849
require.True(t, pool.cache.Contains(bazToken))
4950
}
51+
52+
func TestEndpointBasedOnToken(t *testing.T) {
53+
u, err := url.Parse("wss://gitpod.io")
54+
require.NoError(t, err)
55+
56+
endpointForAccessToken, err := getEndpointBasedOnToken(auth.NewAccessToken("foo"), u)
57+
require.NoError(t, err)
58+
require.Equal(t, "wss://gitpod.io/api/v1", endpointForAccessToken)
59+
60+
endpointForCookie, err := getEndpointBasedOnToken(auth.NewCookieToken("foo"), u)
61+
require.NoError(t, err)
62+
require.Equal(t, "wss://gitpod.io/api/gitpod", endpointForCookie)
63+
}

install/installer/pkg/components/public-api-server/configmap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func configmap(ctx *common.RenderContext) ([]runtime.Object, error) {
3535
})
3636

3737
cfg := config.Configuration{
38-
GitpodServiceURL: fmt.Sprintf("wss://%s/api/gitpod", ctx.Config.Domain),
38+
GitpodServiceURL: fmt.Sprintf("wss://%s", ctx.Config.Domain),
3939
StripeWebhookSigningSecretPath: stripeSecretPath,
4040
BillingServiceAddress: net.JoinHostPort(fmt.Sprintf("%s.%s.svc.cluster.local", usage.Component, ctx.Namespace), strconv.Itoa(usage.GRPCServicePort)),
4141
Server: &baseserver.Configuration{

install/installer/pkg/components/public-api-server/configmap_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestConfigMap(t *testing.T) {
3131
})
3232

3333
expectedConfiguration := config.Configuration{
34-
GitpodServiceURL: "wss://test.domain.everything.awesome.is/api/gitpod",
34+
GitpodServiceURL: "wss://test.domain.everything.awesome.is",
3535
BillingServiceAddress: fmt.Sprintf("usage.%s.svc.cluster.local:9001", ctx.Namespace),
3636
StripeWebhookSigningSecretPath: stripeSecretPath,
3737
Server: &baseserver.Configuration{

0 commit comments

Comments
 (0)