This repository was archived by the owner on Feb 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 262
installer/pkg/config-generator/tls: generate root CA's with go #3316
Merged
trawler
merged 1 commit into
coreos:master
from
trawler:move_tls_from_terraform_to_installer
Jul 3, 2018
+372
−80
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
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
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
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,70 @@ | ||
| package configgenerator | ||
|
|
||
| import ( | ||
| "crypto/rsa" | ||
| "crypto/x509" | ||
| "crypto/x509/pkix" | ||
| "fmt" | ||
| "path/filepath" | ||
|
|
||
| "github.com/coreos/tectonic-installer/installer/pkg/tls" | ||
| ) | ||
|
|
||
| const ( | ||
| rootCACertPath = "generated/newTLS/root-ca.crt" | ||
| rootCAKeyPath = "generated/newTLS/root-ca.key" | ||
| kubeCACertPath = "generated/newTLS/kube-ca.key" | ||
| kubeCAKeyPath = "generated/newTLS/kube-ca.crt" | ||
| aggregatorCAKeyPath = "generated/newTLS/aggregator-ca.key" | ||
| aggregatorCACertPath = "generated/newTLS/aggregator-ca.crt" | ||
| serviceServiceCAKeyPath = "generated/newTLS/service-serving-ca.key" | ||
| serviceServiceCACertPath = "generated/newTLS/service-serving-ca.crt" | ||
| etcdClientKeyPath = "generated/newTLS/etcd-client-ca.key" | ||
| etcdClientCertPath = "generated/newTLS/etcd-client-ca.crt" | ||
| ) | ||
|
|
||
| // GenerateTLSConfig fetches and validates the TLS cert files | ||
| // If no file paths were provided, the certs will be auto-generated | ||
| func (c *ConfigGenerator) GenerateTLSConfig(clusterDir string) error { | ||
| if c.CA.RootCAKeyPath == "" && c.CA.RootCACertPath == "" { | ||
| // generate key and certificate | ||
| key, err := generatePrivateKey(clusterDir, rootCAKeyPath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to generate private key: %v", err) | ||
| } | ||
| if _, err := generateRootCA(clusterDir, key); err != nil { | ||
| return fmt.Errorf("failed to create a certificate: %v", err) | ||
| } | ||
| } else { | ||
| // copy key and certificates | ||
| keyDst := filepath.Join(clusterDir, rootCAKeyPath) | ||
| if err := copyFile(c.CA.RootCAKeyPath, keyDst); err != nil { | ||
| return fmt.Errorf("failed to write file: %v", err) | ||
| } | ||
|
|
||
| certDst := filepath.Join(clusterDir, rootCACertPath) | ||
| if err := copyFile(c.CA.RootCACertPath, certDst); err != nil { | ||
| return fmt.Errorf("failed to write file: %v", err) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func generateRootCA(path string, key *rsa.PrivateKey) (*x509.Certificate, error) { | ||
| fileTargetPath := filepath.Join(path, rootCACertPath) | ||
| cfg := &tls.CertCfg{ | ||
| Subject: pkix.Name{ | ||
| CommonName: "root-ca", | ||
| OrganizationalUnit: []string{"openshift"}, | ||
| }, | ||
| KeyUsages: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, | ||
| } | ||
| cert, err := tls.SelfSignedCACert(cfg, key) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error generating self signed certificate: %v", err) | ||
| } | ||
| if err := writeFile(fileTargetPath, certToPem(cert)); err != nil { | ||
| return nil, err | ||
| } | ||
| return cert, nil | ||
| } |
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,79 @@ | ||
| package configgenerator | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "crypto/rsa" | ||
| "crypto/x509" | ||
| "encoding/pem" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/coreos/tectonic-installer/installer/pkg/tls" | ||
| ) | ||
|
|
||
| func writeFile(path, content string) error { | ||
| f, err := os.Create(path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer f.Close() | ||
| w := bufio.NewWriter(f) | ||
| if _, err := f.WriteString(content); err != nil { | ||
| return err | ||
| } | ||
| w.Flush() | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func copyFile(fromFilePath, toFilePath string) error { | ||
| from, err := os.Open(fromFilePath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer from.Close() | ||
| to, err := os.OpenFile(toFilePath, os.O_RDWR|os.O_CREATE, 0666) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer to.Close() | ||
| _, err = io.Copy(to, from) | ||
| return err | ||
| } | ||
|
|
||
| // privateKeyToPem gets the content of the private key and returns a pem string | ||
| func privateKeyToPem(key *rsa.PrivateKey) string { | ||
| keyInBytes := x509.MarshalPKCS1PrivateKey(key) | ||
| keyinPem := pem.EncodeToMemory( | ||
| &pem.Block{ | ||
| Type: "RSA PRIVATE KEY", | ||
| Bytes: keyInBytes, | ||
| }, | ||
| ) | ||
| return string(keyinPem) | ||
| } | ||
|
|
||
| func certToPem(cert *x509.Certificate) string { | ||
| certInPem := pem.EncodeToMemory( | ||
| &pem.Block{ | ||
| Type: "CERTIFICATE", | ||
| Bytes: cert.Raw, | ||
| }, | ||
| ) | ||
| return string(certInPem) | ||
| } | ||
|
|
||
| // generatePrivateKey generates and returns an *rsa.Privatekey object | ||
| func generatePrivateKey(clusterDir string, path string) (*rsa.PrivateKey, error) { | ||
| fileTargetPath := filepath.Join(clusterDir, path) | ||
| key, err := tls.GeneratePrivateKey() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error generating private key: %v", err) | ||
| } | ||
| if err := writeFile(fileTargetPath, privateKeyToPem(key)); err != nil { | ||
| return nil, err | ||
| } | ||
| return key, nil | ||
| } |
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
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
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
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
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
Oops, something went wrong.
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.
I think that the validation of the certificate file and private key file should occur here in the validation phase rather than (or maybe in addition to) the workflow stage. The reason for this is that we want to catch user errors as early as possible so that a user can't even initialize an installer config if it is invalid. Currently, a user could point their CA cert to an arbitrary file, run the install command, and then get an error halfway through. WDYT?
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.
Yes, should be pretty much analogous to ignition validation https://github.com/coreos/tectonic-installer/blob/master/installer/pkg/config/validate.go#L296