-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add unique id to events Signed-off-by: jkoberg <jkoberg@owncloud.com> * add ConsumeAll function Signed-off-by: jkoberg <jkoberg@owncloud.com> * add helper to create natsstream from config Signed-off-by: jkoberg <jkoberg@owncloud.com> --------- Signed-off-by: jkoberg <jkoberg@owncloud.com>
- Loading branch information
Showing
6 changed files
with
143 additions
and
30 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Enhancement: Add an ID to each events | ||
|
||
This way it is possible to uniquely identify events across services | ||
|
||
https://github.com/cs3org/reva/pull/3637 |
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,95 @@ | ||
package stream | ||
|
||
import ( | ||
"bytes" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"errors" | ||
"io" | ||
"os" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff" | ||
"github.com/cs3org/reva/v2/pkg/events" | ||
"github.com/cs3org/reva/v2/pkg/logger" | ||
"github.com/go-micro/plugins/v4/events/natsjs" | ||
) | ||
|
||
// NatsConfig is the configuration needed for a NATS event stream | ||
type NatsConfig struct { | ||
Endpoint string // Endpoint of the nats server | ||
Cluster string // CluserID of the nats cluster | ||
TLSInsecure bool // Whether to verify TLS certificates | ||
TLSRootCACertificate string // The root CA certificate used to validate the TLS certificate | ||
EnableTLS bool // Enable TLS | ||
} | ||
|
||
// NatsFromConfig returns a nats stream from the given config | ||
func NatsFromConfig(cfg NatsConfig) (events.Stream, error) { | ||
var tlsConf *tls.Config | ||
if cfg.EnableTLS { | ||
var rootCAPool *x509.CertPool | ||
if cfg.TLSRootCACertificate != "" { | ||
rootCrtFile, err := os.Open(cfg.TLSRootCACertificate) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rootCAPool, err = newCertPoolFromPEM(rootCrtFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cfg.TLSInsecure = false | ||
} | ||
|
||
tlsConf = &tls.Config{ | ||
MinVersion: tls.VersionTLS12, | ||
InsecureSkipVerify: cfg.TLSInsecure, //nolint:gosec | ||
RootCAs: rootCAPool, | ||
} | ||
} | ||
return Nats( | ||
natsjs.TLSConfig(tlsConf), | ||
natsjs.Address(cfg.Endpoint), | ||
natsjs.ClusterID(cfg.Cluster), | ||
) | ||
|
||
} | ||
|
||
// Nats returns a nats streaming client | ||
// retries exponentially to connect to a nats server | ||
func Nats(opts ...natsjs.Option) (events.Stream, error) { | ||
b := backoff.NewExponentialBackOff() | ||
var stream events.Stream | ||
o := func() error { | ||
n := b.NextBackOff() | ||
s, err := natsjs.NewStream(opts...) | ||
if err != nil && n > time.Second { | ||
logger.New().Error().Err(err).Msgf("can't connect to nats (jetstream) server, retrying in %s", n) | ||
} | ||
stream = s | ||
return err | ||
} | ||
|
||
err := backoff.Retry(o, b) | ||
return stream, err | ||
} | ||
|
||
// newCertPoolFromPEM reads certificates from io.Reader and returns a x509.CertPool | ||
// containing those certificates. | ||
func newCertPoolFromPEM(crts ...io.Reader) (*x509.CertPool, error) { | ||
certPool := x509.NewCertPool() | ||
|
||
var buf bytes.Buffer | ||
for _, c := range crts { | ||
if _, err := io.Copy(&buf, c); err != nil { | ||
return nil, err | ||
} | ||
if !certPool.AppendCertsFromPEM(buf.Bytes()) { | ||
return nil, errors.New("failed to append cert from PEM") | ||
} | ||
buf.Reset() | ||
} | ||
|
||
return certPool, nil | ||
} |
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