Skip to content

:sparkles, minor [WIP] Autoload on tls changes #395

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions pkg/webhook/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,15 @@ func (s *Server) Start(stop <-chan struct{}) error {
}
}

// TODO: watch the cert dir. Reload the cert if it changes
cert, err := tls.LoadX509KeyPair(path.Join(s.CertDir, certName), path.Join(s.CertDir, keyName))
wrappedTLS := wrappedTLS{}

err := wrappedTLS.autoloadTLS(path.Join(s.CertDir, certName), path.Join(s.CertDir, keyName))
if err != nil {
return err
}

cfg := &tls.Config{
Certificates: []tls.Certificate{cert},
GetCertificate: wrappedTLS.getCertificate,
}

listener, err := tls.Listen("tcp", net.JoinHostPort(s.Host, strconv.Itoa(int(s.Port))), cfg)
Expand Down
93 changes: 93 additions & 0 deletions pkg/webhook/tls_wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Copyright 2019 The Kubernetes 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 webhook

import (
"crypto/tls"
"sync"

"github.com/fsnotify/fsnotify"
"github.com/go-logr/logr"
)

type wrappedTLS struct {
sync.Mutex
certificate *tls.Certificate
log logr.Logger
}

func (w *wrappedTLS) setLogger(logger logr.Logger) error {
Copy link
Member

Choose a reason for hiding this comment

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

It seems this functions is not used anywhere.

w.log = logger
return nil
}

func (w *wrappedTLS) getCertificate(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) {
w.Lock()
defer w.Unlock()

return w.certificate, nil
}

func (w *wrappedTLS) loadCertificate(cert, key string) error {
w.Lock()
defer w.Unlock()

certKey, err := tls.LoadX509KeyPair(cert, key)
if err != nil {
return err
}

w.certificate = &certKey
return nil
}

func (w *wrappedTLS) autoloadTLS(cert, key string) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
defer watcher.Close()
Copy link
Member

Choose a reason for hiding this comment

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

function autoloadTLS is not waiting for the child goroutine you created in line 66.
So when it finishes the execution and the watcher is closed. The child goroutine is not doing anything useful anymore.


done := make(chan error)
go func() {
for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
Copy link
Member

Choose a reason for hiding this comment

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

I doubt if it will work, since when a secret get updated it actually triggers a rename event. Ref: algorithm of volume writer.

w.log.Info("reload modified TLS:", event.Name)
err := w.loadCertificate(cert, key)
if err != nil {
done <- err
}
}
case err := <-watcher.Errors:
done <- err
}
}
}()

err = watcher.Add(cert)
if err != nil {
done <- err
}

err = watcher.Add(key)
if err != nil {
done <- err
}
return <-done
}
5 changes: 5 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/.editorconfig

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading