Skip to content
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

Add Kubernetes service registration #8249

Merged
merged 27 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
2dc736e
add kubernetes service registration
tyrannosaurus-becks Jan 27, 2020
4dc0d00
rename env vars
tyrannosaurus-becks Jan 31, 2020
e75fe3b
increase commentary around env vars
tyrannosaurus-becks Feb 3, 2020
c211b87
improve err and comments
tyrannosaurus-becks Feb 4, 2020
708306b
strip onShutdown method
tyrannosaurus-becks Feb 4, 2020
f38381e
use io.Reader in NewCertPool
tyrannosaurus-becks Feb 5, 2020
c116c5b
minor changes from feedback
tyrannosaurus-becks Feb 5, 2020
38723df
Update serviceregistration/kubernetes/client/client.go
tyrannosaurus-becks Feb 5, 2020
27ec6f4
Update serviceregistration/kubernetes/retry_handler.go
tyrannosaurus-becks Feb 5, 2020
84cd8ec
leave patchesToRetry nil
tyrannosaurus-becks Feb 5, 2020
74c438e
simplify PatchOperation enum to string
tyrannosaurus-becks Feb 5, 2020
b00a0c4
use retryable client for exp backoff and jitter
tyrannosaurus-becks Feb 5, 2020
283ba7f
add ability to respond to application stops
tyrannosaurus-becks Feb 5, 2020
e92f716
replace toString with FormatBool
tyrannosaurus-becks Feb 5, 2020
511de33
update vendored files
tyrannosaurus-becks Feb 5, 2020
03573a8
stop ticker in retryHandler
tyrannosaurus-becks Feb 5, 2020
a89bc1f
update how test fails
tyrannosaurus-becks Feb 5, 2020
662c272
strip req and resp body from error responses
tyrannosaurus-becks Feb 6, 2020
04bc995
use a map to de-dupe patches to same path
tyrannosaurus-becks Feb 10, 2020
630f180
use simpler context
tyrannosaurus-becks Feb 11, 2020
59b3db7
update certutil test
tyrannosaurus-becks Feb 11, 2020
e2a0a7b
fix env vars in manual testing doc
tyrannosaurus-becks Feb 11, 2020
255e113
Update serviceregistration/kubernetes/testing/README.md
tyrannosaurus-becks Feb 11, 2020
aa1b492
Update serviceregistration/kubernetes/testing/README.md
tyrannosaurus-becks Feb 11, 2020
14ba35f
retry initial state if it fails
tyrannosaurus-becks Feb 12, 2020
8b62a65
improve logging in retry handler
tyrannosaurus-becks Feb 12, 2020
5eb0567
protect against nil response
tyrannosaurus-becks Feb 12, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import (

sr "github.com/hashicorp/vault/serviceregistration"
csr "github.com/hashicorp/vault/serviceregistration/consul"
ksr "github.com/hashicorp/vault/serviceregistration/kubernetes"
)

const (
Expand Down Expand Up @@ -161,7 +162,8 @@ var (
}

serviceRegistrations = map[string]sr.Factory{
"consul": csr.NewServiceRegistration,
"consul": csr.NewServiceRegistration,
"kubernetes": ksr.NewServiceRegistration,
}
)

Expand Down
2 changes: 1 addition & 1 deletion command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,7 @@ CLUSTER_SYNTHESIS_COMPLETE:

// If ServiceRegistration is configured, then the backend must support HA
isBackendHA := coreConfig.HAPhysical != nil && coreConfig.HAPhysical.HAEnabled()
if (coreConfig.ServiceRegistration != nil) && !isBackendHA {
if !c.flagDev && (coreConfig.ServiceRegistration != nil) && !isBackendHA {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a technical reason we don't support dev mode? Do we have any other configurations that don't work in dev mode?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mjarmy or @briankassouf do you happen to know the answer to this?

@jasonodonnell , I don't know the original reason for this, but I do know that Consul currently also has service registration and it presently is only available in HA. Right here, it builds how it's going to advertise itself based on whether it's a performance standby. If we were going to make service registration available in wider circumstances, I'm not sure if that could negatively impact Consul from a backwards-compatibility standpoint.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, when I re-read this, I see the question is about dev mode, not HA. I just was thinking about HA because we'd talked about it previously.

In response to the actual question :-), I'd bet that we weren't allowed to run service registrations unless in HA mode because some of the service registration notifications only work with HA. However, this made it difficult to do just little quick tests that didn't pertain to the HA notifications. So I added a flag to allow us to test service registration if the -dev flag is included just to make my life a little easier. With this exception added here, I can just do a quick test without running in a clustered mode, or even if I've dropped one single Vault instance into a container. I figured that since it doesn't effect end users, should be fine.

c.UI.Output("service_registration is configured, but storage does not support HA")
return 1
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/aws/aws-sdk-go v1.25.41
github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932 // indirect
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/chrismalek/oktasdk-go v0.0.0-20181212195951-3430665dfaa0
github.com/cockroachdb/apd v1.1.0 // indirect
github.com/cockroachdb/cockroach-go v0.0.0-20181001143604-e0a95dfd547c
Expand Down
44 changes: 44 additions & 0 deletions sdk/helper/certutil/certutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"encoding/json"
"encoding/pem"
"fmt"
"io/ioutil"
"math/big"
mathrand "math/rand"
"os"
"reflect"
"strings"
"sync"
Expand Down Expand Up @@ -403,6 +405,48 @@ func TestTLSConfig(t *testing.T) {
}
}

func TestNewCertPool(t *testing.T) {
caExample := `-----BEGIN CERTIFICATE-----
MIIC5zCCAc+gAwIBAgIBATANBgkqhkiG9w0BAQsFADAVMRMwEQYDVQQDEwptaW5p
a3ViZUNBMB4XDTE5MTIxMDIzMDUxOVoXDTI5MTIwODIzMDUxOVowFTETMBEGA1UE
AxMKbWluaWt1YmVDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBANFi
/RIdMHd865X6JygTb9riX01DA3QnR+RoXDXNnj8D3LziLG2n8ItXMJvWbU3sxxyy
nX9HxJ0SIeexj1cYzdQBtJDjO1/PeuKc4CZ7zCukCAtHz8mC7BDPOU7F7pggpcQ0
/t/pa2m22hmCu8aDF9WlUYHtJpYATnI/A5vz/VFLR9daxmkl59Qo3oHITj7vAzSx
/75r9cibpQyJ+FhiHOZHQWYY2JYw2g4v5hm5hg5SFM9yFcZ75ISI9ebyFFIl9iBY
zAk9jqv1mXvLr0Q39AVwMTamvGuap1oocjM9NIhQvaFL/DNqF1ouDQjCf5u2imLc
TraO1/2KO8fqwOZCOrMCAwEAAaNCMEAwDgYDVR0PAQH/BAQDAgKkMB0GA1UdJQQW
MBQGCCsGAQUFBwMCBggrBgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3
DQEBCwUAA4IBAQBtVZCwCPqUUUpIClAlE9nc2fo2bTs9gsjXRmqdQ5oaSomSLE93
aJWYFuAhxPXtlApbLYZfW2m1sM3mTVQN60y0uE4e1jdSN1ErYQ9slJdYDAMaEmOh
iSexj+Nd1scUiMHV9lf3ps5J8sYeCpwZX3sPmw7lqZojTS12pANBDcigsaj5RRyN
9GyP3WkSQUsTpWlDb9Fd+KNdkCVw7nClIpBPA2KW4BQKw/rNSvOFD61mbzc89lo0
Q9IFGQFFF8jO18lbyWqnRBGXcS4/G7jQ3S7C121d14YLUeAYOM7pJykI1g4CLx9y
vitin0L6nprauWkKO38XgM4T75qKZpqtiOcT
-----END CERTIFICATE-----
`

tmpfile, err := ioutil.TempFile("", "ca.crt")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())

if _, err := tmpfile.Write([]byte(caExample)); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
fileBody, err := ioutil.ReadFile(tmpfile.Name())
if err != nil {
t.Fatal(err)
}
if _, err := NewCertPool(bytes.NewReader(fileBody)); err != nil {
t.Fatal(err)
}
}

func refreshRSA8CertBundle() *CertBundle {
initTest.Do(setCerts)
return &CertBundle{
Expand Down
49 changes: 49 additions & 0 deletions sdk/helper/certutil/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"encoding/pem"
"errors"
"fmt"
"io"
"io/ioutil"
"math/big"
"net"
"net/url"
Expand Down Expand Up @@ -804,3 +806,50 @@ func SignCertificate(data *CreationBundle) (*ParsedCertBundle, error) {

return result, nil
}

func NewCertPool(reader io.Reader) (*x509.CertPool, error) {
pemBlock, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
certs, err := parseCertsPEM(pemBlock)
if err != nil {
return nil, fmt.Errorf("error reading certs: %s", err)
}
pool := x509.NewCertPool()
for _, cert := range certs {
pool.AddCert(cert)
}
return pool, nil
}

// parseCertsPEM returns the x509.Certificates contained in the given PEM-encoded byte array
// Returns an error if a certificate could not be parsed, or if the data does not contain any certificates
func parseCertsPEM(pemCerts []byte) ([]*x509.Certificate, error) {
ok := false
certs := []*x509.Certificate{}
for len(pemCerts) > 0 {
var block *pem.Block
block, pemCerts = pem.Decode(pemCerts)
if block == nil {
break
}
// Only use PEM "CERTIFICATE" blocks without extra headers
if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
continue
}

cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return certs, err
}

certs = append(certs, cert)
ok = true
}

if !ok {
return certs, errors.New("data does not contain any valid RSA or ECDSA certificates")
}
return certs, nil
}
Loading