Skip to content

Commit

Permalink
enhancement: add event to process ocm notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
fschade committed Sep 2, 2024
1 parent c991ee0 commit 8c1d8be
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 150 deletions.
119 changes: 0 additions & 119 deletions internal/http/services/sciencemesh/email.go

This file was deleted.

31 changes: 20 additions & 11 deletions internal/http/services/sciencemesh/sciencemesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ package sciencemesh
import (
"net/http"

"github.com/go-chi/chi/v5"
"github.com/rs/zerolog"

"github.com/cs3org/reva/v2/pkg/appctx"
"github.com/cs3org/reva/v2/pkg/rhttp/global"
"github.com/cs3org/reva/v2/pkg/sharedconf"
"github.com/cs3org/reva/v2/pkg/smtpclient"
"github.com/cs3org/reva/v2/pkg/utils/cfg"
"github.com/go-chi/chi/v5"
"github.com/rs/zerolog"
)

func init() {
Expand Down Expand Up @@ -60,14 +60,23 @@ func (s *svc) Close() error {
}

type config struct {
Prefix string `mapstructure:"prefix"`
SMTPCredentials *smtpclient.SMTPCredentials `mapstructure:"smtp_credentials" validate:"required"`
GatewaySvc string `mapstructure:"gatewaysvc" validate:"required"`
ProviderDomain string `mapstructure:"provider_domain" validate:"required"`
MeshDirectoryURL string `mapstructure:"mesh_directory_url"`
SubjectTemplate string `mapstructure:"subject_template"`
BodyTemplatePath string `mapstructure:"body_template_path"`
OCMMountPoint string `mapstructure:"ocm_mount_point"`
Prefix string `mapstructure:"prefix"`
GatewaySvc string `mapstructure:"gatewaysvc" validate:"required"`
ProviderDomain string `mapstructure:"provider_domain" validate:"required"`
MeshDirectoryURL string `mapstructure:"mesh_directory_url"`
OCMMountPoint string `mapstructure:"ocm_mount_point"`
Events EventOptions `mapstructure:"events"`
}

// EventOptions are the configurable options for events
type EventOptions struct {
Endpoint string `mapstructure:"natsaddress"`
Cluster string `mapstructure:"natsclusterid"`
TLSInsecure bool `mapstructure:"tlsinsecure"`
TLSRootCACertificate string `mapstructure:"tlsrootcacertificate"`
EnableTLS bool `mapstructure:"enabletls"`
AuthUsername string `mapstructure:"authusername"`
AuthPassword string `mapstructure:"authpassword"`
}

func (c *config) ApplyDefaults() {
Expand Down
41 changes: 21 additions & 20 deletions internal/http/services/sciencemesh/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ import (
invitepb "github.com/cs3org/go-cs3apis/cs3/ocm/invite/v1beta1"
ocmprovider "github.com/cs3org/go-cs3apis/cs3/ocm/provider/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"

"github.com/cs3org/reva/v2/internal/http/services/reqres"
"github.com/cs3org/reva/v2/pkg/appctx"
ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
"github.com/cs3org/reva/v2/pkg/events"
"github.com/cs3org/reva/v2/pkg/events/stream"
"github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool"
"github.com/cs3org/reva/v2/pkg/smtpclient"
"github.com/cs3org/reva/v2/pkg/utils"
"github.com/cs3org/reva/v2/pkg/utils/list"
)

Expand All @@ -45,6 +49,7 @@ type tokenHandler struct {
providerDomain string
tplSubj *template.Template
tplBody *template.Template
eventStream events.Stream
}

func (h *tokenHandler) init(c *config) error {
Expand All @@ -54,19 +59,15 @@ func (h *tokenHandler) init(c *config) error {
return err
}

if c.SMTPCredentials != nil {
h.smtpCredentials = smtpclient.NewSMTPCredentials(c.SMTPCredentials)
}

h.meshDirectoryURL = c.MeshDirectoryURL
h.providerDomain = c.ProviderDomain

if err := h.initSubjectTemplate(c.SubjectTemplate); err != nil {
return err
}

if err := h.initBodyTemplate(c.BodyTemplatePath); err != nil {
return err
if c.Events.Endpoint != "" {
es, err := stream.NatsFromConfig("sciencemesh-token-handler", false, stream.NatsConfig(c.Events))
if err != nil {
return err
}
h.eventStream = es
}

return nil
Expand Down Expand Up @@ -96,16 +97,16 @@ func (h *tokenHandler) Generate(w http.ResponseWriter, r *http.Request) {

user := ctxpkg.ContextMustGetUser(ctx)
recipient := query.Get("recipient")
if recipient != "" && h.smtpCredentials != nil {
templObj := &emailParams{
User: user,
Token: token.InviteToken.Token,
MeshDirectoryURL: h.meshDirectoryURL,
}
if err := h.sendEmail(recipient, templObj); err != nil {
reqres.WriteError(w, r, reqres.APIErrorServerError, "error sending token by mail", err)
return
}
if err := events.Publish(ctx, h.eventStream, events.ScienceMeshInviteTokenGenerated{
Sharer: user.Id,
Token: token.InviteToken.Token,
MeshDirectoryURL: h.meshDirectoryURL,
Recipient: recipient,
Timestamp: utils.TSNow(),
}); err != nil {
log := appctx.GetLogger(ctx)
log.Error().Err(err).
Msg("failed to publish the science-mesh invite token generated event")
}

tknRes := h.prepareGenerateTokenResponse(token.InviteToken)
Expand Down
24 changes: 24 additions & 0 deletions pkg/events/sciencemesh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package events

import (
"encoding/json"

user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
)

// ScienceMeshInviteTokenGenerated is emitted when a sciencemesh token is generated
type ScienceMeshInviteTokenGenerated struct {
Sharer *user.UserId
Token string
MeshDirectoryURL string // fixMe: we can get the value from the config too, lets keep it for now
Recipient string
Timestamp *types.Timestamp
}

// Unmarshal to fulfill unmarshaller interface
func (ScienceMeshInviteTokenGenerated) Unmarshal(v []byte) (interface{}, error) {
e := ScienceMeshInviteTokenGenerated{}
err := json.Unmarshal(v, &e)
return e, err
}

0 comments on commit 8c1d8be

Please sign in to comment.