|
| 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 | + |
| 71 | + // CACert returns the CA cert as in a ConfigMap and the CA encryption key as in a Secret |
| 72 | + // for the given Custom resource (CR); the CA is unique per CR. For example, calling |
| 73 | + // CACert twice returns the same ConfigMap and Secret. |
| 74 | + // The formats for the ConfigMap and Secret are the following: |
| 75 | + // |
| 76 | + // kind: ConfigMap |
| 77 | + // apiVersion: v1 |
| 78 | + // metadata: |
| 79 | + // name: <cr-kind>-<cr-name>-ca |
| 80 | + // namespace: <cr-namespace> |
| 81 | + // data: |
| 82 | + // ca.crt: ... |
| 83 | + // |
| 84 | + // kind: Secret |
| 85 | + // apiVersion: v1 |
| 86 | + // metadata: |
| 87 | + // name: <cr-kind>-<cr-name>-ca |
| 88 | + // namespace: <cr-namespace> |
| 89 | + // data: |
| 90 | + // ca.key: .. |
| 91 | + CACert(cr runtime.Object) (*v1.ConfigMap, *v1.Secret, error) |
| 92 | +} |
0 commit comments