Path: blob/main/install/installer/pkg/components/cluster/certmanager.go
2501 views
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cluster56import (7"fmt"8"time"910"github.com/gitpod-io/gitpod/installer/pkg/common"11"github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental"1213trust "github.com/cert-manager/trust-manager/pkg/apis/trust/v1alpha1"14v1 "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"15cmmeta "github.com/jetstack/cert-manager/pkg/apis/meta/v1"16metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"17"k8s.io/apimachinery/pkg/runtime"18"k8s.io/utils/pointer"19)2021func certmanager(ctx *common.RenderContext) ([]runtime.Object, error) {22issuerName := "gitpod-self-signed-issuer"23secretCAName := "gitpod-identity-trust-root"2425gitpodCaBundleSources := []trust.BundleSource{26{27UseDefaultCAs: pointer.Bool(true),28},29{30Secret: &trust.SourceObjectKeySelector{31Name: secretCAName,32KeySelector: trust.KeySelector{Key: "ca.crt"},33},34},35}3637gitpodCustomCertificateBundleSource := []trust.BundleSource{}3839if ctx.Config.CustomCACert != nil {40gitpodCaBundleSources = append(gitpodCaBundleSources, trust.BundleSource{41Secret: &trust.SourceObjectKeySelector{42Name: ctx.Config.CustomCACert.Name,43KeySelector: trust.KeySelector{Key: "ca.crt"},44},45})4647gitpodCustomCertificateBundleSource = append(gitpodCustomCertificateBundleSource, trust.BundleSource{48Secret: &trust.SourceObjectKeySelector{49Name: ctx.Config.CustomCACert.Name,50KeySelector: trust.KeySelector{Key: "ca.crt"},51},52})53}5455// TODO (gpl): This is a workaround to untangle the refactoring of existing infrastructure from56// moving forward with this change57caCertificateNamespace := "cert-manager" // this is the default we want to converge on, eventually58_ = ctx.WithExperimental(func(cfg *experimental.Config) error {59if cfg.WebApp != nil && cfg.WebApp.CertmanagerNamespaceOverride != "" {60caCertificateNamespace = cfg.WebApp.CertmanagerNamespaceOverride61}62return nil63})6465objects := []runtime.Object{66// Define a self-signed issuer so we can generate a CA67&v1.ClusterIssuer{68TypeMeta: common.TypeMetaCertificateClusterIssuer,69ObjectMeta: metav1.ObjectMeta{70Name: issuerName,71Labels: common.DefaultLabels(Component),72},73Spec: v1.IssuerSpec{IssuerConfig: v1.IssuerConfig{74SelfSigned: &v1.SelfSignedIssuer{},75}},76},77// Generate that CA78&v1.Certificate{79TypeMeta: common.TypeMetaCertificate,80ObjectMeta: metav1.ObjectMeta{81Name: "gitpod-trust-anchor",82Namespace: caCertificateNamespace,83Labels: common.DefaultLabels(Component),84},85Spec: v1.CertificateSpec{86IsCA: true,87Duration: &metav1.Duration{Duration: time.Duration(8760 * time.Hour)}, // 365 days88CommonName: "root.gitpod.cluster.local",89SecretName: secretCAName,90PrivateKey: &v1.CertificatePrivateKey{91Algorithm: v1.ECDSAKeyAlgorithm,92Size: 256,93},94IssuerRef: cmmeta.ObjectReference{95Name: issuerName,96Kind: v1.ClusterIssuerKind,97Group: "cert-manager.io",98},99SecretTemplate: &v1.CertificateSecretTemplate{100Labels: common.DefaultLabels(Component),101},102Usages: []v1.KeyUsage{103v1.UsageCertSign,104v1.UsageCRLSign,105},106},107},108// Set the CA to our issuer109&v1.ClusterIssuer{110TypeMeta: common.TypeMetaCertificateClusterIssuer,111ObjectMeta: metav1.ObjectMeta{112Name: common.CertManagerCAIssuer,113Labels: common.DefaultLabels(Component),114},115Spec: v1.IssuerSpec{116IssuerConfig: v1.IssuerConfig{117CA: &v1.CAIssuer{SecretName: secretCAName},118},119},120},121// Generate that CA122&v1.Certificate{123TypeMeta: common.TypeMetaCertificate,124ObjectMeta: metav1.ObjectMeta{125Name: common.CertManagerCAIssuer,126Namespace: ctx.Namespace,127Labels: common.DefaultLabels(Component),128},129Spec: v1.CertificateSpec{130IsCA: true,131Duration: &metav1.Duration{Duration: time.Duration(2190 * time.Hour)}, // 90 days132CommonName: "ca.gitpod.cluster.local",133SecretName: fmt.Sprintf("%v-intermediate", secretCAName),134PrivateKey: &v1.CertificatePrivateKey{135Algorithm: v1.ECDSAKeyAlgorithm,136Size: 256,137},138IssuerRef: cmmeta.ObjectReference{139Name: common.CertManagerCAIssuer,140Kind: v1.ClusterIssuerKind,141Group: "cert-manager.io",142},143SecretTemplate: &v1.CertificateSecretTemplate{144Labels: common.DefaultLabels(Component),145},146Usages: []v1.KeyUsage{147v1.UsageCertSign,148v1.UsageCRLSign,149v1.UsageServerAuth,150v1.UsageClientAuth,151},152},153},154// trust Bundle155&trust.Bundle{156TypeMeta: common.TypeMetaBundle,157ObjectMeta: metav1.ObjectMeta{158Name: "gitpod-ca-bundle",159},160Spec: trust.BundleSpec{161Sources: gitpodCaBundleSources,162Target: trust.BundleTarget{163ConfigMap: &trust.KeySelector{164Key: "ca-certificates.crt",165},166},167},168},169// single gitpod Bundle (used by registry-facade)170&trust.Bundle{171TypeMeta: common.TypeMetaBundle,172ObjectMeta: metav1.ObjectMeta{173Name: "gitpod-ca",174},175Spec: trust.BundleSpec{176Sources: []trust.BundleSource{177{178Secret: &trust.SourceObjectKeySelector{179Name: secretCAName,180KeySelector: trust.KeySelector{Key: "ca.crt"},181},182},183},184Target: trust.BundleTarget{185ConfigMap: &trust.KeySelector{186Key: "gitpod-ca.crt",187},188},189},190},191}192193if ctx.Config.CustomCACert != nil {194objects = append(objects,195// trust Bundle for custom SSL certificates196&trust.Bundle{197TypeMeta: common.TypeMetaBundle,198ObjectMeta: metav1.ObjectMeta{199Name: "gitpod-customer-certificate-bundle",200},201Spec: trust.BundleSpec{202Sources: gitpodCustomCertificateBundleSource,203Target: trust.BundleTarget{204ConfigMap: &trust.KeySelector{205Key: "ca-certificates.crt",206},207},208},209})210}211212return objects, nil213}214215216