-
Notifications
You must be signed in to change notification settings - Fork 1.8k
pkg/util: add interface for tls util #374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fanminshi
merged 1 commit into
operator-framework:master
from
fanminshi:implement_tls_util
Aug 13, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright 2018 The Operator-SDK Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package tlsutil | ||
|
||
import ( | ||
"k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
// CertType defines the type of the cert. | ||
type CertType int | ||
|
||
const ( | ||
// ClientCert defines a client cert. | ||
ClientCert CertType = iota | ||
// ServingCert defines a serving cert. | ||
ServingCert | ||
// ClientAndServingCert defines both client and serving cert. | ||
ClientAndServingCert | ||
) | ||
|
||
// CertConfig configures how to generate the Cert. | ||
type CertConfig struct { | ||
// CertName is the name of the cert. | ||
CertName string | ||
// Optional CertType. Serving, client or both; defaults to both. | ||
CertType CertType | ||
// Optional CommonName is the common name of the cert; defaults to "". | ||
CommonName string | ||
// Optional Organization is Organization of the cert; defaults to "". | ||
Organization []string | ||
// Optional CA Key, if user wants to provide custom CA key via a file path. | ||
CAKey string | ||
// Optional CA Certificate, if user wants to provide custom CA cert via file path. | ||
CACert string | ||
// TODO: consider to add passed in SAN fields. | ||
} | ||
|
||
// CertGenerator is an operator specific TLS tool that generates TLS assets for the deploying a user's application. | ||
type CertGenerator interface { | ||
// GenerateCert generates a secret containing TLS encryption key and cert, a Secret | ||
// containing the CA key, and a ConfigMap containing the CA Certificate given the Custom | ||
// Resource(CR) "cr", the Kubernetes Service "Service", and the CertConfig "config". | ||
// | ||
// GenerateCert creates and manages TLS key and cert and CA with the following: | ||
// CA creation and management: | ||
// - If CA is not given: | ||
// - A unique CA is generated for the CR. | ||
// - CA's key is packaged into a Secret as shown below. | ||
// - CA's cert is packaged in a ConfigMap as shown below. | ||
// - The CA Secret and ConfigMap are created on the k8s cluster in the CR's namespace before | ||
// returned to the user. The CertGenerator manages the CA Secret and ConfigMap to ensure it's | ||
// unqiue per CR. | ||
// - If CA is given: | ||
// - CA's key is packaged into a Secret as shown below. | ||
// - CA's cert is packaged in a ConfigMap as shown below. | ||
// - The CA Secret and ConfigMap are returned but not created in the K8s cluster in the CR's | ||
// namespace. The CertGenerator doesn't manage the CA because the user controls the lifecycle | ||
// of the CA. | ||
// | ||
// TLS Key and Cert Creation and Management: | ||
// - A unique TLS cert and key pair is generated per CR + CertConfig.CertName. | ||
// - The CA is used to generate and sign the TLS cert. | ||
// - The signing process uses the passed in "service" to set the Subject Alternative Names(SAN) | ||
// for the certificate. We assume that the deployed applications are typically communicated | ||
// with via a Kubernetes Service. The SAN is set to the FQDN of the service | ||
// `<service-name>.<service-namespace>.svc.cluster.local`. | ||
// - Once TLS key and cert are created, they are packaged into a secret as shown below. | ||
// - Finally, the secret are created on the k8s cluster in the CR's namespace before returned to | ||
// the user. The CertGenerator manages this secret to ensure that it is unique per CR + | ||
// CertConfig.CertName. | ||
// | ||
// TLS encryption key and cert Secret format: | ||
// kind: Secret | ||
// apiVersion: v1 | ||
// metadata: | ||
// name: <cr-kind>-<cr-name>-<CertConfig.CertName> | ||
// namespace: <cr-namespace> | ||
// data: | ||
// tls.crt: ... | ||
// tls.key: ... | ||
// | ||
// CA Certificate ConfigMap format: | ||
// kind: ConfigMap | ||
// apiVersion: v1 | ||
// metadata: | ||
// name: <cr-kind>-<cr-name>-ca | ||
// namespace: <cr-namespace> | ||
// data: | ||
// ca.crt: ... | ||
// | ||
// CA Key Secret format: | ||
// kind: Secret | ||
// apiVersion: v1 | ||
// metadata: | ||
// name: <cr-kind>-<cr-name>-ca | ||
// namespace: <cr-namespace> | ||
// data: | ||
// ca.key: .. | ||
GenerateCert(cr runtime.Object, service *v1.Service, config *CertConfig) (*v1.Secret, *v1.ConfigMap, *v1.Secret, error) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this might be a silly question, but did we consider that these returns are nil?
I think that it would be less confusing for a user if they just didn't get those return values?
Also with 4 return values should we consider a wrapper struct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this might be a silly question, but did we consider that these returns are nil?
I have not considered return it as nil because I think it is still quite useful to return the CA Secret and ConfigMap. For instance, I might want to mount the CA Cert ConfigMap to a pod so that we can use that to verify the cert that was generated from this CA. If we just simply return nil, the user has to somehow transfer the passed in cert to the designated pod.
Also with 4 return values should we consider a wrapper struct?
I think the 4 values returned is fine for now; it is still quite clear on what's is returning. If we decided to add more return values, we might want to have a wrapper struct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I am little confused, if the ConfigMap is not created in the cluster because the CA was a custom CA, then this would fail right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shawn-hurley If the CA is a custom CA passed in via CertConfig then
GenerateCert()
will only package the CA cert up as a Configmap and return that object, but not actually create it on the cluster. So it wouldn't really fail in that case.In the custom CA case it's up to the users whether or not they should create the CA Configmap and Secret returned by
GenerateCert()
since the operator does not mange the lifecycle of the CA.