Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Use account UUID from x-access-token for me replacement #14

Merged
merged 7 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions changelog/unreleased/use-access-token.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Change: Use account uuid from x-access-token

We are now using an ocis-pkg middleware for extracting the account uuid of the
authenticated user from the `x-access-token` of the http request header and inject
it into the Identifier protobuf messages wherever possible. This allows us to use
`me` instead of an actual account uuid, when the request comes through the proxy.

https://github.com/owncloud/ocis-settings/pull/14
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ require (
github.com/micro/go-micro/v2 v2.6.0
github.com/oklog/run v1.0.0
github.com/openzipkin/zipkin-go v0.2.2
github.com/owncloud/ocis-hello v0.1.0-alpha1
github.com/owncloud/ocis-pkg/v2 v2.0.1
github.com/owncloud/ocis-pkg/v2 v2.2.2-0.20200527082518-5641fa4a4c8c
github.com/restic/calens v0.2.0
github.com/spf13/viper v1.6.3
go.opencensus.io v0.22.2
go.opencensus.io v0.22.3
golang.org/x/mod v0.3.0 // indirect
golang.org/x/net v0.0.0-20200301022130-244492dfa37a
golang.org/x/tools v0.0.0-20200526224456-8b020aee10d2 // indirect
google.golang.org/genproto v0.0.0-20200420144010-e5e8543f8aeb
google.golang.org/grpc v1.27.0
google.golang.org/grpc v1.28.0
google.golang.org/protobuf v1.21.0
)

Expand Down
72 changes: 72 additions & 0 deletions go.sum

Large diffs are not rendered by default.

22 changes: 14 additions & 8 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,22 @@ type Storage struct {
RootMountPath string
}

// TokenManager is the config for using the reva token manager
type TokenManager struct {
JWTSecret string
}

// Config combines all available configuration parts.
type Config struct {
File string
Storage Storage
Log Log
Debug Debug
HTTP HTTP
GRPC GRPC
Tracing Tracing
Asset Asset
File string
Storage Storage
Log Log
Debug Debug
HTTP HTTP
GRPC GRPC
Tracing Tracing
Asset Asset
TokenManager TokenManager
}

// New initializes a new configuration with or without defaults.
Expand Down
7 changes: 7 additions & 0 deletions pkg/flagset/flagset.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,12 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag {
EnvVars: []string{"SETTINGS_ROOT_MOUNT_PATH"},
Destination: &cfg.Storage.RootMountPath,
},
&cli.StringFlag{
Name: "jwt-secret",
Value: "Pive-Fumkiu4",
Usage: "Used to create JWT to talk to reva, should equal reva's jwt-secret",
EnvVars: []string{"SETTINGS_JWT_SECRET"},
Destination: &cfg.TokenManager.JWTSecret,
},
}
}
13 changes: 6 additions & 7 deletions pkg/proto/v0/settings.pb.web.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/server/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package http

import (
"github.com/go-chi/chi"
"github.com/owncloud/ocis-pkg/v2/account"
"github.com/owncloud/ocis-pkg/v2/middleware"
"github.com/owncloud/ocis-pkg/v2/service/http"
"github.com/owncloud/ocis-settings/pkg/assets"
Expand Down Expand Up @@ -39,6 +40,10 @@ func Server(opts ...Option) http.Service {
mux.Use(middleware.Cache)
mux.Use(middleware.Cors)
mux.Use(middleware.Secure)
mux.Use(middleware.ExtractAccountUUID(
account.Logger(options.Logger),
account.JWTSecret(options.Config.TokenManager.JWTSecret)),
)

mux.Use(middleware.Version(
"settings",
Expand Down
30 changes: 19 additions & 11 deletions pkg/service/v0/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package svc
import (
"context"

"github.com/owncloud/ocis-settings/pkg/settings"
store "github.com/owncloud/ocis-settings/pkg/store/filesystem"

"github.com/owncloud/ocis-pkg/v2/middleware"
"github.com/owncloud/ocis-settings/pkg/config"
"github.com/owncloud/ocis-settings/pkg/proto/v0"
"github.com/owncloud/ocis-settings/pkg/settings"
store "github.com/owncloud/ocis-settings/pkg/store/filesystem"
)

// Service represents a service.
Expand All @@ -26,7 +26,7 @@ func NewService(cfg *config.Config) Service {

// SaveSettingsBundle implements the BundleServiceHandler interface
func (g Service) SaveSettingsBundle(c context.Context, req *proto.SaveSettingsBundleRequest, res *proto.SaveSettingsBundleResponse) error {
req.SettingsBundle.Identifier = getFailsafeIdentifier(req.SettingsBundle.Identifier)
req.SettingsBundle.Identifier = getFailsafeIdentifier(c, req.SettingsBundle.Identifier)
r, err := g.manager.WriteBundle(req.SettingsBundle)
if err != nil {
return err
Expand All @@ -37,7 +37,7 @@ func (g Service) SaveSettingsBundle(c context.Context, req *proto.SaveSettingsBu

// GetSettingsBundle implements the BundleServiceHandler interface
func (g Service) GetSettingsBundle(c context.Context, req *proto.GetSettingsBundleRequest, res *proto.GetSettingsBundleResponse) error {
r, err := g.manager.ReadBundle(getFailsafeIdentifier(req.Identifier))
r, err := g.manager.ReadBundle(getFailsafeIdentifier(c, req.Identifier))
if err != nil {
return err
}
Expand All @@ -47,7 +47,7 @@ func (g Service) GetSettingsBundle(c context.Context, req *proto.GetSettingsBund

// ListSettingsBundles implements the BundleServiceHandler interface
func (g Service) ListSettingsBundles(c context.Context, req *proto.ListSettingsBundlesRequest, res *proto.ListSettingsBundlesResponse) error {
r, err := g.manager.ListBundles(getFailsafeIdentifier(req.Identifier))
r, err := g.manager.ListBundles(getFailsafeIdentifier(c, req.Identifier))
if err != nil {
return err
}
Expand All @@ -57,7 +57,7 @@ func (g Service) ListSettingsBundles(c context.Context, req *proto.ListSettingsB

// SaveSettingsValue implements the ValueServiceHandler interface
func (g Service) SaveSettingsValue(c context.Context, req *proto.SaveSettingsValueRequest, res *proto.SaveSettingsValueResponse) error {
req.SettingsValue.Identifier = getFailsafeIdentifier(req.SettingsValue.Identifier)
req.SettingsValue.Identifier = getFailsafeIdentifier(c, req.SettingsValue.Identifier)
r, err := g.manager.WriteValue(req.SettingsValue)
if err != nil {
return err
Expand All @@ -68,7 +68,7 @@ func (g Service) SaveSettingsValue(c context.Context, req *proto.SaveSettingsVal

// GetSettingsValue implements the ValueServiceHandler interface
func (g Service) GetSettingsValue(c context.Context, req *proto.GetSettingsValueRequest, res *proto.GetSettingsValueResponse) error {
r, err := g.manager.ReadValue(getFailsafeIdentifier(req.Identifier))
r, err := g.manager.ReadValue(getFailsafeIdentifier(c, req.Identifier))
if err != nil {
return err
}
Expand All @@ -78,20 +78,28 @@ func (g Service) GetSettingsValue(c context.Context, req *proto.GetSettingsValue

// ListSettingsValues implements the ValueServiceHandler interface
func (g Service) ListSettingsValues(c context.Context, req *proto.ListSettingsValuesRequest, res *proto.ListSettingsValuesResponse) error {
r, err := g.manager.ListValues(getFailsafeIdentifier(req.Identifier))
r, err := g.manager.ListValues(getFailsafeIdentifier(c, req.Identifier))
if err != nil {
return err
}
res.SettingsValues = r
return nil
}

func getFailsafeIdentifier(identifier *proto.Identifier) *proto.Identifier {
// getFailsafeIdentifier makes sure that there is an identifier, and that the account uuid is injected if needed.
func getFailsafeIdentifier(c context.Context, identifier *proto.Identifier) *proto.Identifier {
if identifier == nil {
identifier = &proto.Identifier{}
}
if identifier.AccountUuid == "me" {
identifier.AccountUuid = "5681371F-4A6E-43BC-8BB5-9C9237FA9C58"
ownAccountUUID := c.Value(middleware.UUIDKey).(string)
if len(ownAccountUUID) > 0 {
identifier.AccountUuid = ownAccountUUID
} else {
// might be valid for the request not having an AccountUuid in the identifier.
// but clear it, instead of passing on `me`.
identifier.AccountUuid = ""
}
}
return identifier
}