Skip to content

Commit 1f9f02d

Browse files
authored
Merge pull request #374 from fanminshi/implement_tls_util
pkg/util: add interface for tls util
2 parents 62df821 + 4609807 commit 1f9f02d

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

pkg/tlsutil/tls.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright 2018 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package tlsutil
16+
17+
import (
18+
"k8s.io/api/core/v1"
19+
"k8s.io/apimachinery/pkg/runtime"
20+
)
21+
22+
// CertType defines the type of the cert.
23+
type CertType int
24+
25+
const (
26+
// ClientCert defines a client cert.
27+
ClientCert CertType = iota
28+
// ServingCert defines a serving cert.
29+
ServingCert
30+
// ClientAndServingCert defines both client and serving cert.
31+
ClientAndServingCert
32+
)
33+
34+
// CertConfig configures how to generate the Cert.
35+
type CertConfig struct {
36+
// CertName is the name of the cert.
37+
CertName string
38+
// Optional CertType. Serving, client or both; defaults to both.
39+
CertType CertType
40+
// Optional CommonName is the common name of the cert; defaults to "".
41+
CommonName string
42+
// Optional Organization is Organization of the cert; defaults to "".
43+
Organization []string
44+
// Optional CA Key, if user wants to provide custom CA key via a file path.
45+
CAKey string
46+
// Optional CA Certificate, if user wants to provide custom CA cert via file path.
47+
CACert string
48+
// TODO: consider to add passed in SAN fields.
49+
}
50+
51+
// CertGenerator is an operator specific TLS tool that generates TLS assets for the deploying a user's application.
52+
type CertGenerator interface {
53+
// GenerateCert generates a secret containing TLS encryption key and cert, a Secret
54+
// containing the CA key, and a ConfigMap containing the CA Certificate given the Custom
55+
// Resource(CR) "cr", the Kubernetes Service "Service", and the CertConfig "config".
56+
//
57+
// GenerateCert creates and manages TLS key and cert and CA with the following:
58+
// CA creation and management:
59+
// - If CA is not given:
60+
// - A unique CA is generated for the CR.
61+
// - CA's key is packaged into a Secret as shown below.
62+
// - CA's cert is packaged in a ConfigMap as shown below.
63+
// - The CA Secret and ConfigMap are created on the k8s cluster in the CR's namespace before
64+
// returned to the user. The CertGenerator manages the CA Secret and ConfigMap to ensure it's
65+
// unqiue per CR.
66+
// - If CA is given:
67+
// - CA's key is packaged into a Secret as shown below.
68+
// - CA's cert is packaged in a ConfigMap as shown below.
69+
// - The CA Secret and ConfigMap are returned but not created in the K8s cluster in the CR's
70+
// namespace. The CertGenerator doesn't manage the CA because the user controls the lifecycle
71+
// of the CA.
72+
//
73+
// TLS Key and Cert Creation and Management:
74+
// - A unique TLS cert and key pair is generated per CR + CertConfig.CertName.
75+
// - The CA is used to generate and sign the TLS cert.
76+
// - The signing process uses the passed in "service" to set the Subject Alternative Names(SAN)
77+
// for the certificate. We assume that the deployed applications are typically communicated
78+
// with via a Kubernetes Service. The SAN is set to the FQDN of the service
79+
// `<service-name>.<service-namespace>.svc.cluster.local`.
80+
// - Once TLS key and cert are created, they are packaged into a secret as shown below.
81+
// - Finally, the secret are created on the k8s cluster in the CR's namespace before returned to
82+
// the user. The CertGenerator manages this secret to ensure that it is unique per CR +
83+
// CertConfig.CertName.
84+
//
85+
// TLS encryption key and cert Secret format:
86+
// kind: Secret
87+
// apiVersion: v1
88+
// metadata:
89+
// name: <cr-kind>-<cr-name>-<CertConfig.CertName>
90+
// namespace: <cr-namespace>
91+
// data:
92+
// tls.crt: ...
93+
// tls.key: ...
94+
//
95+
// CA Certificate ConfigMap format:
96+
// kind: ConfigMap
97+
// apiVersion: v1
98+
// metadata:
99+
// name: <cr-kind>-<cr-name>-ca
100+
// namespace: <cr-namespace>
101+
// data:
102+
// ca.crt: ...
103+
//
104+
// CA Key Secret format:
105+
// kind: Secret
106+
// apiVersion: v1
107+
// metadata:
108+
// name: <cr-kind>-<cr-name>-ca
109+
// namespace: <cr-namespace>
110+
// data:
111+
// ca.key: ..
112+
GenerateCert(cr runtime.Object, service *v1.Service, config *CertConfig) (*v1.Secret, *v1.ConfigMap, *v1.Secret, error)
113+
}

0 commit comments

Comments
 (0)