Skip to content

Commit eb32a91

Browse files
committed
pkg/util: add interface for tls util
1 parent f7627c6 commit eb32a91

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

pkg/util/tlsutil/tls.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 CAKey, if user wants to provide custom CA key path.
45+
CAKey string
46+
// Optional CA Certificate, if user wants to provide custom CA cert 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 Certificate for the given
53+
// service. the service is one of communication endpoints between two parties.
54+
// The generated cert certifies that endpoint.
55+
// When generating the secret if CAKey and CACert from the CertConfig are not given, a unique
56+
// self signed CA for the Custom Resource is generated to sign the Certificate.
57+
// In addition to generate CA and the application TLS Secret, those are also created as
58+
// the Kubernetes objects with CA format shown in output of the CACert() and with the format
59+
// of the secret as the following:
60+
//
61+
// kind: Secret
62+
// apiVersion: v1
63+
// metadata:
64+
// name: <cr-kind>-<cr-name>-<CertConfig.CertName>
65+
// namespace: <cr-namespace>
66+
// data:
67+
// tls.crt: ...
68+
// tls.key: ...
69+
GenerateCert(cr runtime.Object, service *v1.Service, config *CertConfig) (*v1.Secret, error)
70+
// CACert returns the CA cert as in a ConfigMap and the CA encryption key as in a Secret
71+
// for the given Custom resource (CR); the CA is unique per CR. For example, calling
72+
// CACert twice returns the same ConfigMap and Secret.
73+
// The formats for the ConfigMap and Secret are the following:
74+
//
75+
// kind: ConfigMap
76+
// apiVersion: v1
77+
// metadata:
78+
// name: <cr-kind>-<cr-name>-ca
79+
// namespace: <cr-namespace>
80+
// data:
81+
// ca.crt: ...
82+
//
83+
// kind: Secret
84+
// apiVersion: v1
85+
// metadata:
86+
// name: <cr-kind>-<cr-name>-ca
87+
// namespace: <cr-namespace>
88+
// data:
89+
// ca.key: ..
90+
CACert(cr runtime.Object) (*v1.ConfigMap, *v1.Secret, error)
91+
}

0 commit comments

Comments
 (0)