Skip to content

Commit

Permalink
Add possibility to provide your own certificates to the server (#710)
Browse files Browse the repository at this point in the history
* Add possibility to provide your own certificates to the server

* Fix linting error
  • Loading branch information
krisgeus committed Mar 9, 2022
1 parent c0c12d2 commit 71fdd6b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
12 changes: 12 additions & 0 deletions fakestorage/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
"compress/gzip"
"context"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -110,6 +111,10 @@ type Options struct {

// Location used for buckets in the server.
BucketsLocation string

CertificateLocation string

PrivateKeyLocation string
}

// NewServerWithOptions creates a new server configured according to the
Expand Down Expand Up @@ -166,6 +171,13 @@ func NewServerWithOptions(options Options) (*Server, error) {
s.ts.Listener.Close()
s.ts.Listener = l
}
if options.CertificateLocation != "" && options.PrivateKeyLocation != "" {
cert, err := tls.LoadX509KeyPair(options.CertificateLocation, options.PrivateKeyLocation)
if err != nil {
return nil, err
}
s.ts.TLS = &tls.Config{Certificates: []tls.Certificate{cert}}
}
startFunc()

return s, nil
Expand Down
48 changes: 27 additions & 21 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@ const (
)

type Config struct {
Seed string
publicHost string
externalURL string
allowedCORSHeaders []string
scheme string
host string
port uint
backend string
fsRoot string
event EventConfig
bucketLocation string
Seed string
publicHost string
externalURL string
allowedCORSHeaders []string
scheme string
host string
port uint
backend string
fsRoot string
event EventConfig
bucketLocation string
certificateLocation string
privateKeyLocation string
}

type EventConfig struct {
Expand Down Expand Up @@ -69,6 +71,8 @@ func Load(args []string) (Config, error) {
fs.StringVar(&cfg.event.prefix, "event.object-prefix", "", "if not empty, only objects having this prefix will generate trigger events")
fs.StringVar(&eventList, "event.list", eventFinalize, "comma separated list of events to publish on cloud function URl. Options are: finalize, delete, and metadataUpdate")
fs.StringVar(&cfg.bucketLocation, "location", "US-CENTRAL1", "location for buckets")
fs.StringVar(&cfg.certificateLocation, "cert-location", "", "location for server certificate")
fs.StringVar(&cfg.privateKeyLocation, "private-key-location", "", "location for private key")

err := fs.Parse(args)
if err != nil {
Expand Down Expand Up @@ -155,15 +159,17 @@ func (c *Config) ToFakeGcsOptions() fakestorage.Options {
}

return fakestorage.Options{
StorageRoot: storageRoot,
Scheme: c.scheme,
Host: c.host,
Port: uint16(c.port),
PublicHost: c.publicHost,
ExternalURL: c.externalURL,
AllowedCORSHeaders: c.allowedCORSHeaders,
Writer: logrus.New().Writer(),
EventOptions: eventOptions,
BucketsLocation: c.bucketLocation,
StorageRoot: storageRoot,
Scheme: c.scheme,
Host: c.host,
Port: uint16(c.port),
PublicHost: c.publicHost,
ExternalURL: c.externalURL,
AllowedCORSHeaders: c.allowedCORSHeaders,
Writer: logrus.New().Writer(),
EventOptions: eventOptions,
BucketsLocation: c.bucketLocation,
CertificateLocation: c.certificateLocation,
PrivateKeyLocation: c.privateKeyLocation,
}
}

0 comments on commit 71fdd6b

Please sign in to comment.