knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/webhook/certificates/resources/secret.go (about)

     1  /*
     2  Copyright 2019 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package resources
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	corev1 "k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  )
    26  
    27  const (
    28  	// ServerKey is the name of the key associated with the secret's private key.
    29  	ServerKey = "server-key.pem"
    30  	// ServerCert is the name of the key associated with the secret's public key.
    31  	ServerCert = "server-cert.pem"
    32  	// CACert is the name of the key associated with the certificate of the CA for
    33  	// the keypair.
    34  	CACert = "ca-cert.pem"
    35  
    36  	oneWeek = 7 * 24 * time.Hour
    37  )
    38  
    39  // MakeSecret synthesizes a Kubernetes Secret object with the keys specified by
    40  // ServerKey, ServerCert, and CACert populated with a fresh certificate.
    41  // This is mutable to make deterministic testing possible.
    42  var MakeSecret = MakeSecretInternal
    43  
    44  // MakeSecretInternal is only public so MakeSecret can be restored in testing.  Use MakeSecret.
    45  func MakeSecretInternal(ctx context.Context, name, namespace, serviceName string) (*corev1.Secret, error) {
    46  	serverKey, serverCert, caCert, err := CreateCerts(ctx, serviceName, namespace, time.Now().Add(oneWeek))
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	return &corev1.Secret{
    51  		ObjectMeta: metav1.ObjectMeta{
    52  			Name:      name,
    53  			Namespace: namespace,
    54  		},
    55  		Data: map[string][]byte{
    56  			ServerKey:  serverKey,
    57  			ServerCert: serverCert,
    58  			CACert:     caCert,
    59  		},
    60  	}, nil
    61  }