-
Notifications
You must be signed in to change notification settings - Fork 3
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 central certificate storage (s3/minio) #58
Draft
na4ma4
wants to merge
2
commits into
icecave:main
Choose a base branch
from
na4ma4:add-fs-certificate-storage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+564
−9
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
@@ -0,0 +1,217 @@ | ||
package cert | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"crypto/tls" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"log" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/icecave/honeycomb/name" | ||
"github.com/minio/minio-go/v7" | ||
"github.com/minio/minio-go/v7/pkg/credentials" | ||
) | ||
|
||
// MinioProvider a certificate provider that reads certificates from a loader. | ||
type MinioProvider struct { | ||
Client *minio.Client | ||
BucketName string | ||
CacheAge time.Duration | ||
Logger *log.Logger | ||
|
||
mutex sync.RWMutex | ||
cache map[string]*minioCacheItem | ||
} | ||
|
||
type minioCacheItem struct { | ||
Certificate *tls.Certificate | ||
LastSeen time.Time | ||
} | ||
|
||
// errMinioCertNotFound is returned when the certificate is not found or not correctly formed in minio. | ||
var errMinioCertNotFound = errors.New("certificate not found") | ||
|
||
// NewMinioProvider returns a MinioProvider preconfigured and setup for use as a Provider. | ||
func NewMinioProvider( | ||
logger *log.Logger, | ||
endpoint, region, bucketName, accessKeyID, secretAccessKey string, | ||
useSSL bool, | ||
) (Provider, error) { | ||
c, err := minio.New(endpoint, &minio.Options{ | ||
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""), | ||
Secure: useSSL, | ||
Region: region, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if ok, err := c.BucketExists(context.Background(), bucketName); err == nil && !ok { | ||
err := c.MakeBucket(context.Background(), bucketName, minio.MakeBucketOptions{ | ||
Region: region, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return &MinioProvider{ | ||
Client: c, | ||
BucketName: bucketName, | ||
Logger: logger, | ||
}, err | ||
} | ||
|
||
// GetCertificate attempts to fetch an existing certificate for the given | ||
// server name. If no such certificate exists, it generates one. | ||
func (p *MinioProvider) GetCertificate(ctx context.Context, n name.ServerName) (*tls.Certificate, error) { | ||
cert, err := p.GetExistingCertificate(ctx, n) | ||
if err != nil { | ||
return nil, err | ||
} else if cert != nil { | ||
return cert, err | ||
} | ||
|
||
return nil, fmt.Errorf("minio %w", ErrProviderGenerateUnsupported) | ||
} | ||
|
||
// GetExistingCertificate attempts to fetch an existing certificate for the | ||
// given server name. It never generates new certificates. A non-nil error | ||
// indicates an error with the provider itself; otherwise, a nil certificate | ||
// indicates a failure to find an existing certificate. | ||
func (p *MinioProvider) GetExistingCertificate(ctx context.Context, n name.ServerName) (*tls.Certificate, error) { | ||
// If cache has not expired, attempt to find in cache. | ||
if !p.expiredInCache(n) { | ||
if cert, ok := p.findInCache(n); ok { | ||
return cert, nil | ||
} | ||
} | ||
|
||
for _, objectName := range p.resolveObjectNames(n) { | ||
// No cache (or expired), attempt to look up in redis | ||
// but if redis is down or broken suddenly, we should reuse the | ||
// cached certificate until it's replaced. | ||
if cert, err := p.getMinioCertificate(ctx, objectName); err == nil { | ||
p.writeToCache(n, cert) | ||
|
||
return cert, nil | ||
} | ||
} | ||
|
||
// fail through to getting it from the cache. | ||
if cert, ok := p.findInCache(n); ok { | ||
p.Logger.Printf("expired but falling through to cache for %s", n.Unicode) | ||
|
||
return cert, nil | ||
} | ||
|
||
// and finally we just fail. | ||
return nil, nil | ||
} | ||
|
||
func (p *MinioProvider) getMinioObject(ctx context.Context, objectName string) (*minio.Object, error) { | ||
return p.Client.GetObject( | ||
ctx, | ||
p.BucketName, | ||
fmt.Sprintf("%s.crt", objectName), | ||
minio.GetObjectOptions{}, | ||
) | ||
} | ||
|
||
func (p *MinioProvider) getMinioCertificate(ctx context.Context, objectName string) (*tls.Certificate, error) { | ||
var ( | ||
certObj, keyObj *minio.Object | ||
err error | ||
) | ||
|
||
if certObj, err = p.getMinioObject(ctx, fmt.Sprintf("%s.crt", objectName)); err != nil { | ||
return nil, err | ||
} | ||
|
||
if keyObj, err = p.getMinioObject(ctx, fmt.Sprintf("%s.key", objectName)); err != nil { | ||
return nil, err | ||
} | ||
|
||
certBuf := bytes.NewBuffer(nil) | ||
if _, err = io.Copy(certBuf, certObj); err != nil { | ||
return nil, err | ||
} | ||
|
||
keyBuf := bytes.NewBuffer(nil) | ||
if _, err = io.Copy(keyBuf, keyObj); err != nil { | ||
return nil, err | ||
} | ||
|
||
if cert, err := tls.X509KeyPair(certBuf.Bytes(), keyBuf.Bytes()); err == nil { | ||
return &cert, nil | ||
} | ||
|
||
return nil, errMinioCertNotFound | ||
} | ||
|
||
func (p *MinioProvider) resolveObjectNames( | ||
n name.ServerName, | ||
) (filenames []string) { | ||
tail := n.Punycode | ||
filenames = []string{tail} | ||
|
||
for { | ||
parts := strings.SplitN(tail, ".", 2) | ||
if len(parts) == 1 { | ||
return | ||
} | ||
|
||
tail = parts[1] | ||
filenames = append(filenames, "_."+tail, tail) | ||
} | ||
} | ||
|
||
func (p *MinioProvider) expiredInCache(n name.ServerName) bool { | ||
p.mutex.RLock() | ||
defer p.mutex.RUnlock() | ||
|
||
if item, ok := p.cache[n.Unicode]; ok { | ||
if p.CacheAge > 0 && item.LastSeen.Before(time.Now().Add(-1*p.CacheAge)) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func (p *MinioProvider) findInCache( | ||
n name.ServerName, | ||
) (*tls.Certificate, bool) { | ||
p.mutex.RLock() | ||
defer p.mutex.RUnlock() | ||
|
||
if item, ok := p.cache[n.Unicode]; ok { | ||
return item.Certificate, ok | ||
} | ||
|
||
return nil, false | ||
} | ||
|
||
func (p *MinioProvider) writeToCache( | ||
n name.ServerName, | ||
cert *tls.Certificate, | ||
) { | ||
p.mutex.Lock() | ||
defer p.mutex.Unlock() | ||
|
||
if p.cache == nil { | ||
p.cache = map[string]*minioCacheItem{} | ||
} | ||
|
||
item := &minioCacheItem{ | ||
Certificate: cert, | ||
LastSeen: time.Now(), | ||
} | ||
|
||
p.cache[n.Unicode] = item | ||
} |
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.
Just FYI if you change a lot of unrelated things like this it makes it really hard to review PRs.
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 was just clearing some linter warnings, housekeeping I added was switching to wrapped errors and removing context.Context from loadCertificate since it wasn't used.