Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK selection refactor #197

Merged
merged 18 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ prepare_test:
test:
go test -cover ./...

.PHONY: test_race
test_race:
go test -race -gcflags=all=-d=checkptr=0 ./...

.PHONY: test_circleci
test_circleci:
scripts/wait_for_wallet.sh
Expand Down
42 changes: 21 additions & 21 deletions api/benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import (

"github.com/lbryio/lbrytv/app/proxy"
"github.com/lbryio/lbrytv/app/sdkrouter"
"github.com/lbryio/lbrytv/app/users"
"github.com/lbryio/lbrytv/app/wallet"
"github.com/lbryio/lbrytv/config"
"github.com/lbryio/lbrytv/internal/lbrynet"
"github.com/lbryio/lbrytv/internal/responses"
"github.com/lbryio/lbrytv/internal/storage"
"github.com/lbryio/lbrytv/models"
Expand All @@ -33,14 +32,14 @@ func launchAuthenticatingAPIServer() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t := r.PostFormValue("auth_token")

responses.PrepareJSONWriter(w)
responses.AddJSONContentType(w)

reply := fmt.Sprintf(`
{
"success": true,
"error": null,
"data": {
"id": %v,
"id": %s,
"language": "en",
"given_name": null,
"family_name": null,
Expand Down Expand Up @@ -87,31 +86,29 @@ func setupDBTables() {
func BenchmarkWalletCommands(b *testing.B) {
setupDBTables()

wallet.DisableLogger()
sdkrouter.DisableLogger()
log.SetOutput(ioutil.Discard)

rand.Seed(time.Now().UnixNano())

rt := sdkrouter.New(config.GetLbrynetServers())

ts := launchAuthenticatingAPIServer()
defer ts.Close()
config.Override("InternalAPIHost", ts.URL)
defer config.RestoreOverridden()

walletsNum := 30
wallets := make([]*models.User, walletsNum)
rt := sdkrouter.New(config.GetLbrynetServers())
svc := users.NewWalletService(rt)

svc.Logger.Disable()
lbrynet.Logger.Disable()
log.SetOutput(ioutil.Discard)

rand.Seed(time.Now().UnixNano())

for i := 0; i < walletsNum; i++ {
uid := int(rand.Int31())
u, err := svc.Retrieve(users.Query{Token: fmt.Sprintf("%v", uid)})
u, err := wallet.GetUserWithWallet(rt, ts.URL, fmt.Sprintf("%d", uid), "")
require.NoError(b, err, errors.Unwrap(err))
require.NotNil(b, u)
wallets[i] = u
}

handler := proxy.NewRequestHandler(proxy.NewService(proxy.Opts{SDKRouter: rt}))
handler := sdkrouter.Middleware(rt)(http.HandlerFunc(proxy.Handle))

b.SetParallelism(30)
b.ResetTimer()
Expand All @@ -120,17 +117,20 @@ func BenchmarkWalletCommands(b *testing.B) {
for pb.Next() {
u := wallets[rand.Intn(len(wallets))]

var response jsonrpc.RPCResponse
q := jsonrpc.NewRequest("wallet_balance", map[string]string{"wallet_id": u.WalletID})

qBody, _ := json.Marshal(q)
r, _ := http.NewRequest("POST", proxySuffix, bytes.NewBuffer(qBody))
r.Header.Add("X-Lbry-Auth-Token", fmt.Sprintf("%v", u.ID))
qBody, err := json.Marshal(q)
require.NoError(b, err)
r, err := http.NewRequest("POST", proxySuffix, bytes.NewBuffer(qBody))
require.NoError(b, err)
r.Header.Add("X-Lbry-Auth-Token", fmt.Sprintf("%d", u.ID))

rr := httptest.NewRecorder()
handler.Handle(rr, r)
handler.ServeHTTP(rr, r)

require.Equal(b, http.StatusOK, rr.Code)

var response jsonrpc.RPCResponse
json.Unmarshal(rr.Body.Bytes(), &response)
require.Nil(b, response.Error)
}
Expand Down
15 changes: 0 additions & 15 deletions api/handlers.go

This file was deleted.

37 changes: 17 additions & 20 deletions api/routes.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package api

import (
"context"
"net/http"
"strings"
"time"

"github.com/lbryio/lbrytv/app/auth"
"github.com/lbryio/lbrytv/app/proxy"
"github.com/lbryio/lbrytv/app/publish"
"github.com/lbryio/lbrytv/app/sdkrouter"
"github.com/lbryio/lbrytv/app/users"
"github.com/lbryio/lbrytv/config"
"github.com/lbryio/lbrytv/internal/metrics"
"github.com/lbryio/lbrytv/internal/status"

Expand All @@ -18,37 +18,34 @@ import (
)

// InstallRoutes sets up global API handlers
func InstallRoutes(proxyService *proxy.ProxyService, r *mux.Router) {
authenticator := users.NewAuthenticator(users.NewWalletService(proxyService.SDKRouter))
proxyHandler := proxy.NewRequestHandler(proxyService)
upHandler, err := publish.NewUploadHandler(publish.UploadOpts{ProxyService: proxyService})
if err != nil {
panic(err)
func InstallRoutes(r *mux.Router, sdkRouter *sdkrouter.Router) {
upHandler := &publish.Handler{
Publisher: &publish.LbrynetPublisher{Router: sdkRouter},
UploadPath: config.GetPublishSourceDir(),
InternalAPIHost: config.GetInternalAPIHost(),
}

r.Use(methodTimer)

r.HandleFunc("/", Index)
r.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's just me, but I like to keep any views logic, no matter how trivial, in the handlers source file to minimize surprises in code discovery.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you mean surprises in code discovery?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The kind of surprise when you read through handlers.go and realize that some of the handlers logic is actually in routes.go

http.Redirect(w, req, config.GetProjectURL(), http.StatusSeeOther)
})

v1Router := r.PathPrefix("/api/v1").Subrouter()
v1Router.HandleFunc("/proxy", proxyHandler.HandleOptions).Methods(http.MethodOptions)
v1Router.HandleFunc("/proxy", authenticator.Wrap(upHandler.Handle)).MatcherFunc(upHandler.CanHandle)
v1Router.HandleFunc("/proxy", proxyHandler.Handle)
v1Router.Use(sdkrouter.Middleware(sdkRouter))
retriever := auth.AllInOneRetrieverThatNeedsRefactoring(sdkRouter, config.GetInternalAPIHost())
v1Router.Use(auth.Middleware(retriever))
v1Router.HandleFunc("/proxy", proxy.HandleCORS).Methods(http.MethodOptions)
v1Router.HandleFunc("/proxy", upHandler.Handle).MatcherFunc(upHandler.CanHandle)
v1Router.HandleFunc("/proxy", proxy.Handle)
v1Router.HandleFunc("/metric/ui", metrics.TrackUIMetric).Methods(http.MethodPost)

internalRouter := r.PathPrefix("/internal").Subrouter()
internalRouter.Handle("/metrics", promhttp.Handler())
internalRouter.HandleFunc("/status", injectSDKRouter(proxyService.SDKRouter, status.GetStatus))
internalRouter.HandleFunc("/status", sdkrouter.AddToRequest(sdkRouter, status.GetStatus))
internalRouter.HandleFunc("/whoami", status.WhoAMI)
}

// i can't tell if this is really a best practice or a hack
func injectSDKRouter(rt *sdkrouter.Router, fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fn(w, r.Clone(context.WithValue(r.Context(), status.SDKRouterContextKey, rt)))
}
}

func methodTimer(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
Expand Down
18 changes: 8 additions & 10 deletions api/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,23 @@ import (
"net/http/httptest"
"testing"

"github.com/lbryio/lbrytv/app/proxy"
"github.com/gorilla/mux"
"github.com/lbryio/lbrytv/app/publish"
"github.com/lbryio/lbrytv/app/sdkrouter"
"github.com/lbryio/lbrytv/config"

"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRoutesProxy(t *testing.T) {
r := mux.NewRouter()
proxy := proxy.NewService(proxy.Opts{SDKRouter: sdkrouter.New(config.GetLbrynetServers())})
rt := sdkrouter.New(config.GetLbrynetServers())

req, err := http.NewRequest("POST", "/api/v1/proxy", bytes.NewBuffer([]byte(`{"method": "status"}`)))
require.NoError(t, err)
rr := httptest.NewRecorder()

InstallRoutes(proxy, r)
InstallRoutes(r, rt)
r.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
Expand All @@ -33,29 +31,29 @@ func TestRoutesProxy(t *testing.T) {

func TestRoutesPublish(t *testing.T) {
r := mux.NewRouter()
proxy := proxy.NewService(proxy.Opts{SDKRouter: sdkrouter.New(config.GetLbrynetServers())})
rt := sdkrouter.New(config.GetLbrynetServers())

req := publish.CreatePublishRequest(t, []byte("test file"))
rr := httptest.NewRecorder()

InstallRoutes(proxy, r)
InstallRoutes(r, rt)
r.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
// Authentication Required error here is enough to see that the request
// has been dispatched through the publish handler
assert.Contains(t, rr.Body.String(), `"code": -32080`)
assert.Contains(t, rr.Body.String(), `"code": -32084`)
}

func TestRoutesOptions(t *testing.T) {
r := mux.NewRouter()
proxy := proxy.NewService(proxy.Opts{SDKRouter: sdkrouter.New(config.GetLbrynetServers())})
rt := sdkrouter.New(config.GetLbrynetServers())

req, err := http.NewRequest("OPTIONS", "/api/v1/proxy", nil)
require.NoError(t, err)
rr := httptest.NewRecorder()

InstallRoutes(proxy, r)
InstallRoutes(r, rt)
r.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, "7200", rr.Result().Header.Get("Access-Control-Max-Age"))
Expand Down
65 changes: 65 additions & 0 deletions app/auth/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package auth

import (
"context"
"net/http"

"github.com/lbryio/lbrytv/app/sdkrouter"
"github.com/lbryio/lbrytv/app/wallet"
"github.com/lbryio/lbrytv/internal/ip"
"github.com/lbryio/lbrytv/internal/monitor"
"github.com/lbryio/lbrytv/models"

"github.com/gorilla/mux"
)

var logger = monitor.NewModuleLogger("auth")

const ContextKey = "user"

type Result struct {
User *models.User
Err error
}

func (r *Result) AuthAttempted() bool { return r.User != nil || r.Err != nil }
func (r *Result) AuthFailed() bool { return r.Err != nil }
func (r *Result) Authenticated() bool { return r.User != nil }

func FromRequest(r *http.Request) *Result {
v := r.Context().Value(ContextKey)
if v == nil {
panic("Auth middleware was not applied")
lyoshenka marked this conversation as resolved.
Show resolved Hide resolved
}
return v.(*Result)
}

// Retriever gets a user by hitting internal-api with the provided auth token
// and matching the response to a local user.
// NOTE: The retrieved user must come with a wallet that's created and ready to use.
type Retriever func(token, metaRemoteIP string) (*models.User, error)

func AllInOneRetrieverThatNeedsRefactoring(rt *sdkrouter.Router, internalAPIHost string) Retriever {
return func(token, metaRemoteIP string) (user *models.User, err error) {
return wallet.GetUserWithWallet(rt, internalAPIHost, token, metaRemoteIP)
}
}

func Middleware(retriever Retriever) mux.MiddlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ar := &Result{}
if token, ok := r.Header[wallet.TokenHeader]; ok {
addr := ip.AddressForRequest(r)
user, err := retriever(token[0], addr)
if err != nil {
logger.LogF(monitor.F{"ip": addr}).Debugf("failed to authenticate user")
ar.Err = err
} else {
ar.User = user
}
}
next.ServeHTTP(w, r.Clone(context.WithValue(r.Context(), ContextKey, ar)))
})
}
}
Loading