Skip to content

Commit

Permalink
Support ca* naming convention
Browse files Browse the repository at this point in the history
  • Loading branch information
pebrc committed Apr 4, 2022
1 parent 49064df commit b7d3852
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 5 deletions.
5 changes: 2 additions & 3 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,8 @@ func doRun(_ *cobra.Command, _ []string) error {
toWatch = append(toWatch,
filepath.Join(caDir, certificates.KeyFileName),
filepath.Join(caDir, certificates.CertFileName),
// TODO support ca.crt and ca.key
// filepath.Join(caDir, certificates.CAKeyFileName),
// filepath.Join(caDir, certificates.CAFileName),
filepath.Join(caDir, certificates.CAKeyFileName),
filepath.Join(caDir, certificates.CAFileName),
)
}

Expand Down
33 changes: 31 additions & 2 deletions pkg/controller/common/certificates/ca_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"crypto/x509"
"crypto/x509/pkix"
"fmt"
"github.com/elastic/cloud-on-k8s/pkg/utils/fs"
"io/ioutil"
"path/filepath"
"time"
Expand Down Expand Up @@ -234,8 +235,37 @@ func internalSecretForCA(
}, nil
}

func detectCAFileNames(path string) (string, string, error) {
files := map[string]bool{
CertFileName: false,
KeyFileName: false,
CAFileName: false,
CAKeyFileName: false,
}
for f := range files {
exists, err := fs.FileExists(filepath.Join(path, f))
if err != nil {
return "", "", err
}
files[f] = exists
}
switch {
case (files[CertFileName] || files[KeyFileName]) && files[CAKeyFileName]:
return "", "", fmt.Errorf("both tls.* and ca.* files exist, configuration error")
case files[CAFileName] && files[CAKeyFileName]:
return filepath.Join(path, CAFileName), filepath.Join(CAKeyFileName), nil
case files[CertFileName] && files[KeyFileName]:
return filepath.Join(path, CertFileName), filepath.Join(path, KeyFileName), nil
}
return "", "", fmt.Errorf("no CA certificate files found: %+v", files)
}

func BuildCAFromFile(path string) (*CA, error) {
certFile := filepath.Join(path, CertFileName)
certFile, privateKeyFile, err := detectCAFileNames(path)
if err != nil {
return nil, err
}

bytes, err := ioutil.ReadFile(certFile)
if err != nil {
return nil, err
Expand All @@ -254,7 +284,6 @@ func BuildCAFromFile(path string) (*CA, error) {
}
cert := certs[0]

privateKeyFile := filepath.Join(path, KeyFileName)
privateKeyBytes, err := ioutil.ReadFile(privateKeyFile)
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package certificates

import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io/ioutil"
Expand Down Expand Up @@ -103,3 +104,68 @@ func TestBuildCAFromFile(t *testing.T) {
})
}
}

func Test_detectCAFileNames(t *testing.T) {
tests := []struct {
name string
files []string
wantCert string
wantKey string
wantErr bool
}{
{
name: "happy path ca*",
files: []string{"ca.crt", "ca.key"},
wantCert: "ca.crt",
wantKey: "ca.key",
wantErr: false,
},
{
name: "happy path tls*",
files: []string{"tls.crt", "tls.key"},
wantCert: "tls.crt",
wantKey: "tls.key",
wantErr: false,
},
{
name: "tls.* with ca.crt OK",
files: []string{"tls.crt", "tls.key", "ca.crt"},
wantCert: "tls.crt",
wantKey: "tls.key",
wantErr: false,
},
{
name: "mixed tls.* and ca.* NOK",
files: []string{"tls.crt", "tls.key", "ca.crt", "ca.key"},
wantCert: "",
wantKey: "",
wantErr: true,
},
{
name: "no valid combination of files",
files: nil,
wantCert: "",
wantKey: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir, err := ioutil.TempDir("", "detect_ca_file_names")
require.NoError(t, err)
defer os.RemoveAll(dir)
for _, f := range tt.files {
require.NoError(t, ioutil.WriteFile(filepath.Join(dir, f), []byte("contents"), 0644))
}

cert, key, err := detectCAFileNames(dir)
if tt.wantErr != (err != nil) {
t.Errorf(fmt.Sprintf("want err %v got %v,files: %v ", tt.wantErr, err, tt.files))
}
if err == nil {
assert.Equalf(t, tt.wantCert, filepath.Base(cert), "detectCAFileNames(), files: %v", tt.files)
assert.Equalf(t, tt.wantKey, filepath.Base(key), "detectCAFileNames(), files: %v", tt.files)
}
})
}
}
17 changes: 17 additions & 0 deletions pkg/utils/fs/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License 2.0;
// you may not use this file except in compliance with the Elastic License 2.0.

package fs

import "os"

func FileExists(file string) (bool, error) {
_, err := os.Stat(file)
if err != nil && os.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, err
}
return true, nil
}

0 comments on commit b7d3852

Please sign in to comment.