-
Notifications
You must be signed in to change notification settings - Fork 326
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
Support TLS-enabled servers for the server-acl-init command #183
Merged
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 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 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 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 |
---|---|---|
@@ -1,20 +1,25 @@ | ||
package serveraclinit | ||
|
||
import ( | ||
"crypto/x509" | ||
"encoding/base64" | ||
"fmt" | ||
"github.com/hashicorp/consul/agent" | ||
"github.com/hashicorp/consul/api" | ||
"github.com/hashicorp/consul/tlsutil" | ||
"github.com/mitchellh/cli" | ||
"github.com/stretchr/testify/require" | ||
"io/ioutil" | ||
appv1 "k8s.io/api/apps/v1" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes/fake" | ||
"math/rand" | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"os" | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
@@ -1146,9 +1151,65 @@ func TestRun_Timeout(t *testing.T) { | |
require.Equal(1, responseCode, ui.ErrorWriter.String()) | ||
} | ||
|
||
// Test that the bootstrapping process can make calls to Consul API over HTTPS | ||
// when the consul agent is configured with HTTPS only (HTTP disabled). | ||
func TestRun_HTTPS(t *testing.T) { | ||
t.Parallel() | ||
require := require.New(t) | ||
k8s := fake.NewSimpleClientset() | ||
|
||
caFile, certFile, keyFile, cleanup := generateServerCerts(t) | ||
defer cleanup() | ||
|
||
agentConfig := fmt.Sprintf(` | ||
primary_datacenter = "dc1" | ||
acl { | ||
enabled = true | ||
} | ||
ca_file = "%s" | ||
cert_file = "%s" | ||
key_file = "%s"`, caFile, certFile, keyFile) | ||
|
||
a := &agent.TestAgent{ | ||
Name: t.Name(), | ||
HCL: agentConfig, | ||
UseTLS: true, // this also disables HTTP port | ||
} | ||
|
||
a.Start() | ||
defer a.Shutdown() | ||
|
||
createTestK8SResources(t, k8s, a, resourcePrefix, "https") | ||
|
||
// Run the command. | ||
ui := cli.NewMockUi() | ||
cmd := Command{ | ||
UI: ui, | ||
clientset: k8s, | ||
} | ||
cmd.init() | ||
responseCode := cmd.Run([]string{ | ||
"-server-label-selector=component=server,app=consul,release=" + releaseName, | ||
"-resource-prefix=" + resourcePrefix, | ||
"-k8s-namespace=" + ns, | ||
"-use-https", | ||
"-consul-tls-server-name", "server.dc1.consul", | ||
"-consul-ca-cert", caFile, | ||
"-expected-replicas=1", | ||
}) | ||
require.Equal(0, responseCode, ui.ErrorWriter.String()) | ||
|
||
// Test that the bootstrap token is created to make sure the bootstrapping succeeded. | ||
// The presence of the bootstrap token tells us that the API calls to Consul have been successful. | ||
tokenSecret, err := k8s.CoreV1().Secrets(ns).Get(resourcePrefix+"-bootstrap-acl-token", metav1.GetOptions{}) | ||
require.NoError(err) | ||
require.NotNil(tokenSecret) | ||
_, ok := tokenSecret.Data["token"] | ||
require.True(ok) | ||
} | ||
|
||
// Set up test consul agent and kubernetes cluster. | ||
func completeSetup(t *testing.T, prefix string) (*fake.Clientset, *agent.TestAgent) { | ||
require := require.New(t) | ||
k8s := fake.NewSimpleClientset() | ||
|
||
a := agent.NewTestAgent(t, t.Name(), ` | ||
|
@@ -1157,6 +1218,14 @@ func completeSetup(t *testing.T, prefix string) (*fake.Clientset, *agent.TestAge | |
enabled = true | ||
}`) | ||
|
||
createTestK8SResources(t, k8s, a, prefix, "http") | ||
|
||
return k8s, a | ||
} | ||
|
||
// Create test k8s resources (server pods and server stateful set) | ||
func createTestK8SResources(t *testing.T, k8s *fake.Clientset, a *agent.TestAgent, prefix, scheme string) { | ||
require := require.New(t) | ||
consulURL, err := url.Parse("http://" + a.HTTPAddr()) | ||
require.NoError(err) | ||
port, err := strconv.Atoi(consulURL.Port()) | ||
|
@@ -1181,7 +1250,7 @@ func completeSetup(t *testing.T, prefix string) (*fake.Clientset, *agent.TestAge | |
Name: "consul", | ||
Ports: []v1.ContainerPort{ | ||
{ | ||
Name: "http", | ||
Name: scheme, | ||
ContainerPort: int32(port), | ||
}, | ||
}, | ||
|
@@ -1207,7 +1276,6 @@ func completeSetup(t *testing.T, prefix string) (*fake.Clientset, *agent.TestAge | |
}, | ||
}) | ||
require.NoError(err) | ||
return k8s, a | ||
} | ||
|
||
// getBootToken gets the bootstrap token from the Kubernetes secret. It will | ||
|
@@ -1221,5 +1289,62 @@ func getBootToken(t *testing.T, k8s *fake.Clientset, prefix string) string { | |
return string(bootToken) | ||
} | ||
|
||
// generateServerCerts generates Consul CA | ||
// and a server certificate and saves them to temp files. | ||
// It returns file names in this order: | ||
// CA certificate, server certificate, and server key. | ||
// Note that it's the responsibility of the caller to | ||
// remove the temporary files created by this function. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could also return a function that the callers can run in a defer that cleans up the files: func() a (string, func()) {
return "a", func() {
os.Remove("a")
}
}
file, cleanup := a()
defer cleanup() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, that's an awesome idea. I'll do that! |
||
func generateServerCerts(t *testing.T) (string, string, string, func()) { | ||
require := require.New(t) | ||
|
||
caFile, err := ioutil.TempFile("", "ca") | ||
require.NoError(err) | ||
|
||
certFile, err := ioutil.TempFile("", "cert") | ||
require.NoError(err) | ||
|
||
certKeyFile, err := ioutil.TempFile("", "key") | ||
require.NoError(err) | ||
|
||
// Generate CA | ||
sn, err := tlsutil.GenerateSerialNumber() | ||
require.NoError(err) | ||
|
||
s, _, err := tlsutil.GeneratePrivateKey() | ||
require.NoError(err) | ||
|
||
constraints := []string{"consul", "localhost"} | ||
ca, err := tlsutil.GenerateCA(s, sn, 1, constraints) | ||
require.NoError(err) | ||
|
||
// Generate Server Cert | ||
name := fmt.Sprintf("server.%s.%s", "dc1", "consul") | ||
DNSNames := []string{name, "localhost"} | ||
IPAddresses := []net.IP{net.ParseIP("127.0.0.1")} | ||
extKeyUsage := []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth} | ||
|
||
sn, err = tlsutil.GenerateSerialNumber() | ||
require.NoError(err) | ||
|
||
pub, priv, err := tlsutil.GenerateCert(s, ca, sn, name, 1, DNSNames, IPAddresses, extKeyUsage) | ||
require.NoError(err) | ||
|
||
// Write certs and key to files | ||
_, err = caFile.WriteString(ca) | ||
require.NoError(err) | ||
_, err = certFile.WriteString(pub) | ||
require.NoError(err) | ||
_, err = certKeyFile.WriteString(priv) | ||
require.NoError(err) | ||
|
||
cleanupFunc := func() { | ||
os.Remove(caFile.Name()) | ||
os.Remove(certFile.Name()) | ||
os.Remove(certKeyFile.Name()) | ||
} | ||
return caFile.Name(), certFile.Name(), certKeyFile.Name(), cleanupFunc | ||
} | ||
|
||
var serviceAccountCACert = "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURDekNDQWZPZ0F3SUJBZ0lRS3pzN05qbDlIczZYYzhFWG91MjVoekFOQmdrcWhraUc5dzBCQVFzRkFEQXYKTVMwd0t3WURWUVFERXlRMU9XVTJaR00wTVMweU1EaG1MVFF3T1RVdFlUSTRPUzB4Wm1NM01EQmhZekZqWXpndwpIaGNOTVRrd05qQTNNVEF4TnpNeFdoY05NalF3TmpBMU1URXhOek14V2pBdk1TMHdLd1lEVlFRREV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TjBiM0l0WVhWMGFHMWxkR2h2WkMxemRtTXRZV05qYjNWdWRDSjkuWWk2M01NdHpoNU1CV0tLZDNhN2R6Q0pqVElURTE1aWtGeV9UbnBka19Bd2R3QTlKNEFNU0dFZUhONXZXdEN1dUZqb19sTUpxQkJQSGtLMkFxYm5vRlVqOW01Q29wV3lxSUNKUWx2RU9QNGZVUS1SYzBXMVBfSmpVMXJaRVJIRzM5YjVUTUxnS1BRZ3V5aGFpWkVKNkNqVnRtOXdVVGFncmdpdXFZVjJpVXFMdUY2U1lObTZTckt0a1BTLWxxSU8tdTdDMDZ3Vms1bTV1cXdJVlFOcFpTSUNfNUxzNWFMbXlaVTNuSHZILVY3RTNIbUJoVnlaQUI3NmpnS0IwVHlWWDFJT3NrdDlQREZhck50VTNzdVp5Q2p2cUMtVUpBNnNZZXlTZTRkQk5Lc0tsU1o2WXV4VVVtbjFSZ3YzMllNZEltbnNXZzhraGYtekp2cWdXazdCNUVB" |
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.
If we set
consul-ca-cert
orconsul-tls-server-name
withuse-https=false
, does it work? What about vice versa? Wondering if we should error out if not all the flags are set.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.
Yeah, good question. If you set
use-https
but choose to add your CA to the system trusted CAs, then this should still work. If you set ca cert and tls server name, but don't enable https, it won't have any effect because the job will just use HTTP for all connections, so I don't want to error here.