Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

Put access_key_type to plugin request context #238

Merged
merged 1 commit into from
Nov 24, 2016
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: 6 additions & 2 deletions pkg/server/plugin/exec/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,16 @@ func TestRun(t *testing.T) {
executed := false
startCommand = func(cmd *exec.Cmd, in []byte) (out []byte, err error) {
So(cmd, shouldRunWithContext, map[string]interface{}{
"user_id": "user",
"user_id": "user",
"access_key_type": "master",
})
So(cmd, shouldRunWithConfig, appconfig)
executed = true
return []byte(`{"result": {}}`), nil
}

ctx := context.WithValue(context.Background(), router.UserIDContextKey, "user")
ctx = context.WithValue(ctx, router.AccessKeyTypeContextKey, router.MasterAccessKey)
transport.RunLambda(ctx, "work", []byte{})
So(executed, ShouldBeTrue)
})
Expand Down Expand Up @@ -625,14 +627,16 @@ func TestRun(t *testing.T) {
executed := false
startCommand = func(cmd *exec.Cmd, in []byte) (out []byte, err error) {
So(cmd, shouldRunWithContext, map[string]interface{}{
"user_id": "user",
"user_id": "user",
"access_key_type": "master",
})
So(cmd, shouldRunWithConfig, appconfig)
executed = true
return []byte(`{"result": {}}`), nil
}

ctx := context.WithValue(context.Background(), router.UserIDContextKey, "user")
ctx = context.WithValue(ctx, router.AccessKeyTypeContextKey, router.MasterAccessKey)
transport.RunHook(ctx, "note_afterSave", &recordin, nil)
So(executed, ShouldBeTrue)
})
Expand Down
6 changes: 4 additions & 2 deletions pkg/server/plugin/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,12 @@ func TestRun(t *testing.T) {

Convey("run lambda", func() {
ctx := context.WithValue(context.Background(), router.UserIDContextKey, "user")
ctx = context.WithValue(ctx, router.AccessKeyTypeContextKey, router.MasterAccessKey)
data := `{"data": "bye"}`
httpmock.RegisterResponder("POST", "http://localhost:8000",
func(req *http.Request) (*http.Response, error) {
out, _ := ioutil.ReadAll(req.Body)
So(out, ShouldEqualJSON, `{"context":{"user_id":"user"},"kind":"op","name":"john","param":{"data":"bye"}}`)
So(out, ShouldEqualJSON, `{"context":{"user_id":"user","access_key_type":"master"},"kind":"op","name":"john","param":{"data":"bye"}}`)
return httpmock.NewJsonResponse(200, map[string]interface{}{
"result": map[string]interface{}{"data": "hello"},
})
Expand All @@ -132,6 +133,7 @@ func TestRun(t *testing.T) {

Convey("run hook", func() {
ctx := context.WithValue(context.Background(), router.UserIDContextKey, "user")
ctx = context.WithValue(ctx, router.AccessKeyTypeContextKey, router.ClientAccessKey)

recordin := skydb.Record{
ID: skydb.NewRecordID("note", "id"),
Expand Down Expand Up @@ -169,7 +171,7 @@ func TestRun(t *testing.T) {
func(req *http.Request) (*http.Response, error) {
in, _ := ioutil.ReadAll(req.Body)
So(in, ShouldEqualJSON, `{
"context":{"user_id":"user"},
"context":{"user_id":"user","access_key_type":"client"},
"kind":"hook",
"name":"beforeSave",
"param":{
Expand Down
8 changes: 8 additions & 0 deletions pkg/server/plugin/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,13 @@ func ContextMap(ctx context.Context) map[string]interface{} {
if userID, ok := ctx.Value(router.UserIDContextKey).(string); ok {
pluginCtx["user_id"] = userID
}
if accessKeyType, ok := ctx.Value(router.AccessKeyTypeContextKey).(router.AccessKeyType); ok {
switch accessKeyType {
case router.ClientAccessKey:
pluginCtx["access_key_type"] = "client"
case router.MasterAccessKey:
pluginCtx["access_key_type"] = "master"
}
}
return pluginCtx
}
4 changes: 3 additions & 1 deletion pkg/server/plugin/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ func TestContextMap(t *testing.T) {
Convey("UserID", t, func() {
ctx := context.Background()
ctx = context.WithValue(ctx, router.UserIDContextKey, "42")
ctx = context.WithValue(ctx, router.AccessKeyTypeContextKey, router.MasterAccessKey)
So(ContextMap(ctx), ShouldResemble, map[string]interface{}{
"user_id": "42",
"user_id": "42",
"access_key_type": "master",
})
})
}
1 change: 1 addition & 0 deletions pkg/server/preprocessor/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func checkRequestAccessKey(payload *router.Payload, clientKey string, masterKey
} else {
return skyerr.NewErrorf(skyerr.AccessKeyNotAccepted, "Cannot verify api key: `%v`", apiKey)
}
payload.Context = context.WithValue(payload.Context, router.AccessKeyTypeContextKey, payload.AccessKey)
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/server/router/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
type ContextKey string

var UserIDContextKey ContextKey = "UserID"
var AccessKeyTypeContextKey ContextKey = "AccessKeyType"

// HandlerFunc specifies the function signature of a request handler function
type HandlerFunc func(*Payload, *Response)
Expand Down