Skip to content

Commit 553ba2f

Browse files
committed
pkg/tlsutil: add tls util interface and config definition
1 parent f7627c6 commit 553ba2f

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

pkg/tlsutil/tls.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
}
49+
50+
// CertGenerator is an operator specific TLS tool that generates TLS assets for the deploying application.
51+
type CertGenerator interface {
52+
// GenerateCert generates a secret containing TLS encryption key and cert, a Secret
53+
// containing the CA key, and a ConfigMap containing the CA Certificate given the Custom
54+
// Resource(CR) "cr", the Kubernetes Service "Service", and the CertConfig "config".
55+
//
56+
// The passed in "service" represents endpoint(s) of a communicating party. We assume that the
57+
// deployed applications are typically communicated via the Kubernete Service. Therefore, the
58+
// cert is generated to certify the endpoint(s) defined by the "service".
59+
//
60+
// If CA is not passed in via CertConfig, GenerateCert creates a unique self signed CA
61+
// corresponding to the "cr" for signing the TLS cert and also puts the self signed CA and TLS
62+
// key and Certs into the Kubernetes cluster before returning.
63+
// If CA is passed in, GenerateCert use it for signing the TLS cert but
64+
// only puts TLS key and cert into the cluster not the passed in CA; the user has to
65+
// figure out what to do with the returned CA object.
66+
// Note that the self signed CA is unique per CR; calling GenerateCert on the same CR
67+
// returns the same CA. Also a secret is unique per CR + CertConfig.CertName combination;
68+
// calling GenerateCert on the CR + CertConfig.CertName returns the same TLS key and cert secret.
69+
// TODO: add caveats.
70+
//
71+
// TLS encryption key and cert Secret format:
72+
// kind: Secret
73+
// apiVersion: v1
74+
// metadata:
75+
// name: <cr-kind>-<cr-name>-<CertConfig.CertName>
76+
// namespace: <cr-namespace>
77+
// data:
78+
// tls.crt: ...
79+
// tls.key: ...
80+
//
81+
// CA Certificate ConfigMap format:
82+
// kind: ConfigMap
83+
// apiVersion: v1
84+
// metadata:
85+
// name: <cr-kind>-<cr-name>-ca
86+
// namespace: <cr-namespace>
87+
// data:
88+
// ca.crt: ...
89+
//
90+
// CA Key Secret format:
91+
// kind: Secret
92+
// apiVersion: v1
93+
// metadata:
94+
// name: <cr-kind>-<cr-name>-ca
95+
// namespace: <cr-namespace>
96+
// data:
97+
// ca.key: ..
98+
GenerateCert(cr runtime.Object, service *v1.Service, config *CertConfig) (*v1.Secret, *v1.ConfigMap, *v1.Secret, error)
99+
}

0 commit comments

Comments
 (0)