Skip to content

Commit

Permalink
}}} This is a combination of 2 commits.
Browse files Browse the repository at this point in the history
[stripe] Inject stripe-webhook-secret into public-api
  • Loading branch information
easyCZ authored and roboquat committed Aug 26, 2022
1 parent 8d00926 commit 3037016
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 35 deletions.
4 changes: 1 addition & 3 deletions .werft/jobs/build/installer/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,7 @@ EOF`);
}

private configurePublicAPIServer(slice: string) {
exec(`yq w -i ${this.options.installerConfigPath} experimental.webapp.publicApi.enabled true`, {
slice: slice,
});
exec(`yq w -i ${this.options.installerConfigPath} experimental.webapp.publicApi.enabled true`, { slice: slice });
}

private configureUsage(slice: string) {
Expand Down
7 changes: 7 additions & 0 deletions components/public-api/go/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,12 @@ type Configuration struct {

BillingServiceAddress string `json:"billingServiceAddress,omitempty"`

// StripeWebhookSigningSecretPath is a filepath to a secret used to validate incoming webhooks from Stripe
StripeWebhookSigningSecretPath string `json:"stripeWebhookSigningSecretPath"`

Server *baseserver.Configuration `json:"server,omitempty"`
}

type StripeSecret struct {
WebhookSigningKey string `json:"signingKey"`
}
47 changes: 45 additions & 2 deletions install/installer/pkg/components/public-api-server/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ package public_api_server

import (
"fmt"
"github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental"
"k8s.io/utils/pointer"
"net"
"path/filepath"
"strconv"

"github.com/gitpod-io/gitpod/common-go/baseserver"
Expand All @@ -24,9 +27,17 @@ const (
)

func configmap(ctx *common.RenderContext) ([]runtime.Object, error) {
var stripeSecretPath string

_ = ctx.WithExperimental(func(cfg *experimental.Config) error {
_, _, stripeSecretPath, _ = getStripeConfig(cfg)
return nil
})

cfg := config.Configuration{
GitpodServiceURL: fmt.Sprintf("wss://%s/api/v1", ctx.Config.Domain),
BillingServiceAddress: net.JoinHostPort(usage.Component, strconv.Itoa(usage.GRPCServicePort)),
GitpodServiceURL: fmt.Sprintf("wss://%s/api/v1", ctx.Config.Domain),
StripeWebhookSigningSecretPath: stripeSecretPath,
BillingServiceAddress: net.JoinHostPort(usage.Component, strconv.Itoa(usage.GRPCServicePort)),
Server: &baseserver.Configuration{
Services: baseserver.ServicesConfiguration{
GRPC: &baseserver.ServerConfiguration{
Expand Down Expand Up @@ -59,3 +70,35 @@ func configmap(ctx *common.RenderContext) ([]runtime.Object, error) {
},
}, nil
}

func getStripeConfig(cfg *experimental.Config) (corev1.Volume, corev1.VolumeMount, string, bool) {
var volume corev1.Volume
var mount corev1.VolumeMount
var path string

if cfg == nil || cfg.WebApp == nil || cfg.WebApp.PublicAPI == nil || cfg.WebApp.PublicAPI.StripeSecretName == "" {
return volume, mount, path, false
}

stripeSecret := cfg.WebApp.PublicAPI.StripeSecretName

volume = corev1.Volume{
Name: "stripe-secret",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: stripeSecret,
Optional: pointer.Bool(true),
},
},
}

mount = corev1.VolumeMount{
Name: "stripe-secret",
MountPath: stripeSecretMountPath,
ReadOnly: true,
}

path = filepath.Join(secretsDirectory, stripeSecretMountPath)

return volume, mount, path, true
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package public_api_server

import (
"fmt"
"github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental"
"testing"

"github.com/gitpod-io/gitpod/common-go/baseserver"
Expand All @@ -22,9 +23,16 @@ func TestConfigMap(t *testing.T) {

require.Len(t, objs, 1, "must only render one configmap")

var stripeSecretPath string
_ = ctx.WithExperimental(func(ucfg *experimental.Config) error {
_, _, stripeSecretPath, _ = getStripeConfig(ucfg)
return nil
})

expectedConfiguration := config.Configuration{
GitpodServiceURL: "wss://test.domain.everything.awesome.is/api/v1",
BillingServiceAddress: "usage:9001",
GitpodServiceURL: "wss://test.domain.everything.awesome.is/api/v1",
BillingServiceAddress: "usage:9001",
StripeWebhookSigningSecretPath: stripeSecretPath,
Server: &baseserver.Configuration{
Services: baseserver.ServicesConfiguration{
GRPC: &baseserver.ServerConfiguration{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ const (
HTTPContainerPort = 9002
HTTPServicePort = 9002
HTTPPortName = "http"

secretsDirectory = "secrets"
stripeSecretMountPath = "stripe-secret"
)
55 changes: 35 additions & 20 deletions install/installer/pkg/components/public-api-server/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package public_api_server

import (
"fmt"
"github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental"

"github.com/gitpod-io/gitpod/common-go/baseserver"

Expand All @@ -31,6 +32,38 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
return nil, err
}

volumes := []corev1.Volume{
{
Name: configmapVolume,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: Component,
},
},
},
},
}
volumeMounts := []corev1.VolumeMount{
{
Name: configmapVolume,
ReadOnly: true,
MountPath: configMountPath,
SubPath: configJSONFilename,
},
}

_ = ctx.WithExperimental(func(cfg *experimental.Config) error {
volume, mount, _, ok := getStripeConfig(cfg)
if !ok {
return nil
}

volumes = append(volumes, volume)
volumeMounts = append(volumeMounts, mount)
return nil
})

labels := common.CustomizeLabel(ctx, Component, common.TypeMetaDeployment)
return []runtime.Object{
&appsv1.Deployment{
Expand Down Expand Up @@ -115,29 +148,11 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
SuccessThreshold: 1,
TimeoutSeconds: 1,
},
VolumeMounts: []corev1.VolumeMount{
{
Name: configmapVolume,
ReadOnly: true,
MountPath: configMountPath,
SubPath: configJSONFilename,
},
},
VolumeMounts: volumeMounts,
},
*common.KubeRBACProxyContainerWithConfig(ctx),
},
Volumes: []corev1.Volume{
{
Name: configmapVolume,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: Component,
},
},
},
},
},
Volumes: volumes,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package public_api_server

import (
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/pointer"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -42,14 +43,25 @@ func TestDeployment_ServerArguments(t *testing.T) {
`--json-log=true`,
}, apiContainer.Args)

require.Equal(t, []corev1.Volume{{
Name: configmapVolume,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: Component,
require.Equal(t, []corev1.Volume{
{
Name: configmapVolume,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: Component,
},
},
},
},
}}, dpl.Spec.Template.Spec.Volumes, "must bind config as a volume")
{
Name: "stripe-secret",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: "stripe-webhook-secret",
Optional: pointer.Bool(true),
},
},
},
}, dpl.Spec.Template.Spec.Volumes, "must bind config as a volume")
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ func renderContextWithPublicAPIEnabled(t *testing.T) *common.RenderContext {
Domain: "test.domain.everything.awesome.is",
Experimental: &experimental.Config{
WebApp: &experimental.WebAppConfig{
PublicAPI: &experimental.PublicAPIConfig{Enabled: true},
PublicAPI: &experimental.PublicAPIConfig{
Enabled: true,
StripeSecretName: "stripe-webhook-secret",
},
},
},
}, versions.Manifest{
Expand Down
2 changes: 2 additions & 0 deletions install/installer/pkg/config/v1/experimental/experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ type ProxyConfig struct {

type PublicAPIConfig struct {
Enabled bool `json:"enabled"`
// Name of the kubernetes secret to use for Stripe secrets
StripeSecretName string `json:"stripeSecretName"`
}

type UsageConfig struct {
Expand Down

0 comments on commit 3037016

Please sign in to comment.