Skip to content

Commit

Permalink
Update autoupdate service (#44)
Browse files Browse the repository at this point in the history
* Update autoupdate service
* Fix authenticate
  • Loading branch information
ostcar committed Feb 17, 2022
1 parent db011a7 commit 7e5c180
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 56 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/OpenSlides/openslides-icc-service
go 1.17

require (
github.com/OpenSlides/openslides-autoupdate-service v0.4.1-0.20220127132829-32c1dbc480e8
github.com/OpenSlides/openslides-autoupdate-service v0.4.1-0.20220210150646-5678dc385a7d
github.com/gomodule/redigo v1.8.8
github.com/google/go-cmp v0.5.7 // indirect
github.com/ory/dockertest/v3 v3.8.1
Expand All @@ -23,7 +23,7 @@ require (
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
github.com/golang-jwt/jwt/v4 v4.3.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
Expand All @@ -36,5 +36,5 @@ require (
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
)
11 changes: 6 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ github.com/Microsoft/go-winio v0.5.1/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpz
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/OpenSlides/openslides-autoupdate-service v0.4.1-0.20220127132829-32c1dbc480e8 h1:OerlUpyAwZr3+NEOsEnD/ejiK/jlNpZXryUg3YO4d2U=
github.com/OpenSlides/openslides-autoupdate-service v0.4.1-0.20220127132829-32c1dbc480e8/go.mod h1:KDYKuLCbQD/RChC650VcHNItD3WLnzThvodxxN6PZIg=
github.com/OpenSlides/openslides-autoupdate-service v0.4.1-0.20220210150646-5678dc385a7d h1:H73aexBnYtnEw1YLhMu2VkKOGOSBAZUUvdojOSdjqYI=
github.com/OpenSlides/openslides-autoupdate-service v0.4.1-0.20220210150646-5678dc385a7d/go.mod h1:KDYKuLCbQD/RChC650VcHNItD3WLnzThvodxxN6PZIg=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
Expand Down Expand Up @@ -71,8 +71,9 @@ github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7a
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang-jwt/jwt/v4 v4.2.0 h1:besgBTC8w8HjP6NzQdxwKH9Z5oQMZ24ThTrHp3cZ8eU=
github.com/golang-jwt/jwt/v4 v4.2.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang-jwt/jwt/v4 v4.3.0 h1:kHL1vqdqWNfATmA0FNMdmZNMyZI1U6O31X4rlIPoBog=
github.com/golang-jwt/jwt/v4 v4.3.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
Expand Down Expand Up @@ -263,8 +264,8 @@ golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 h1:XfKQ4OlFl8okEOr5UvAqFRVj8pY/4yfcXrddB8qAbU0=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
55 changes: 31 additions & 24 deletions internal/applause/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,33 @@ type Sender interface {

// HandleSend registers the icc/applause route.
func HandleSend(mux *http.ServeMux, applause Sender, auth icchttp.Authenticater) {
mux.HandleFunc(
icchttp.Path+"/applause/send",
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
url := icchttp.Path + "/applause/send"
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

uid := auth.FromContext(r.Context())
if uid == 0 {
w.WriteHeader(401)
icchttp.ErrorNoStatus(w, iccerror.NewMessageError(iccerror.ErrNotAllowed, "Anonymous user can not send applause."))
return
}
uid := auth.FromContext(r.Context())
if uid == 0 {
w.WriteHeader(401)
icchttp.ErrorNoStatus(w, iccerror.NewMessageError(iccerror.ErrNotAllowed, "Anonymous user can not send applause."))
return
}

meetingStr := r.URL.Query().Get("meeting_id")
meetingID, err := strconv.Atoi(meetingStr)
if err != nil {
icchttp.Error(w, iccerror.NewMessageError(iccerror.ErrInvalid, "Query meeting has to be an int."))
return
}
meetingStr := r.URL.Query().Get("meeting_id")
meetingID, err := strconv.Atoi(meetingStr)
if err != nil {
icchttp.Error(w, iccerror.NewMessageError(iccerror.ErrInvalid, "Query meeting has to be an int."))
return
}

if err := applause.Send(r.Context(), meetingID, uid); err != nil {
icchttp.Error(w, fmt.Errorf("saving applause: %w", err))
return
}
},
if err := applause.Send(r.Context(), meetingID, uid); err != nil {
icchttp.Error(w, fmt.Errorf("saving applause: %w", err))
return
}
})

mux.Handle(
url,
icchttp.AuthMiddleware(handler, auth),
)
}

Expand All @@ -53,8 +56,8 @@ type Receive interface {

// HandleReceive registers the icc/applause route.
func HandleReceive(mux *http.ServeMux, applause Receive, auth icchttp.Authenticater) {
mux.HandleFunc(
icchttp.Path+"/applause",
url := icchttp.Path + "/applause"
handler := http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-store, max-age=0")
Expand Down Expand Up @@ -87,6 +90,10 @@ func HandleReceive(mux *http.ServeMux, applause Receive, auth icchttp.Authentica
}
w.(http.Flusher).Flush()
}
},
})

mux.Handle(
url,
icchttp.AuthMiddleware(handler, auth),
)
}
38 changes: 19 additions & 19 deletions internal/notify/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,25 @@ type Publisher interface {

// HandlePublish registers the notify/publish route.
func HandlePublish(mux *http.ServeMux, notify Publisher, auth icchttp.Authenticater) {
url := icchttp.Path + "/notify/publish"
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

uid := auth.FromContext(r.Context())
if uid == 0 {
w.WriteHeader(401)
icchttp.ErrorNoStatus(w, iccerror.NewMessageError(iccerror.ErrNotAllowed, "Anonymous user can not publish notify messages."))
return
}

if err := notify.Publish(r.Body, uid); err != nil {
icchttp.Error(w, fmt.Errorf("publish notify message: %w", err))
return
}
})

mux.Handle(
icchttp.Path+"/notify/publish",
icchttp.AuthMiddleware(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

uid := auth.FromContext(r.Context())
if uid == 0 {
w.WriteHeader(401)
icchttp.ErrorNoStatus(w, iccerror.NewMessageError(iccerror.ErrNotAllowed, "Anonymous user can not publish notify messages."))
return
}

if err := notify.Publish(r.Body, uid); err != nil {
icchttp.Error(w, fmt.Errorf("publish notify message: %w", err))
return
}
}),
auth,
),
url,
icchttp.AuthMiddleware(handler, auth),
)
}
10 changes: 5 additions & 5 deletions internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Run(ctx context.Context, environment []string, secret func(name string) (st
return fmt.Errorf("building auth: %w", err)
}

ds, err := buildDatastore(env)
ds, err := buildDatastore(env, messageBus)
if err != nil {
return fmt.Errorf("build datastore service: %w", err)
}
Expand Down Expand Up @@ -223,6 +223,7 @@ func (a authStub) FromContext(ctx context.Context) int {

type messageBus interface {
auth.LogoutEventer
datastore.Updater
}

func buildMessageBus(env map[string]string) (messageBus, error) {
Expand Down Expand Up @@ -252,12 +253,11 @@ func buildMessageBus(env map[string]string) (messageBus, error) {
}

// buildDatastore configures the datastore service.
func buildDatastore(
env map[string]string,
) (*datastore.Datastore, error) {
func buildDatastore(env map[string]string, updater datastore.Updater) (*datastore.Datastore, error) {
protocol := env["DATASTORE_READER_PROTOCOL"]
host := env["DATASTORE_READER_HOST"]
port := env["DATASTORE_READER_PORT"]
url := protocol + "://" + host + ":" + port
return datastore.New(url), nil
source := datastore.NewSourceDatastore(url, updater)
return datastore.New(source, nil), nil
}

0 comments on commit 7e5c180

Please sign in to comment.