Skip to content

Commit

Permalink
Allow extending keys through keygen (#268)
Browse files Browse the repository at this point in the history
This adds a way to extend keys through a normal `keygen` method. Users will need to provide a key which has `extend` permissions in the request along with the desired access permissions. Note that the permissions will be a reflection of the key itself (e.g: if a key has `re` permissions and `keygen` request contains `wr`, the resulting key will only have `r` permission).
  • Loading branch information
Roman Atachiants authored Sep 25, 2019
1 parent d3b208d commit 7c6b3ed
Show file tree
Hide file tree
Showing 18 changed files with 922 additions and 547 deletions.
184 changes: 0 additions & 184 deletions internal/broker/assets.go

This file was deleted.

14 changes: 9 additions & 5 deletions internal/broker/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"time"

"github.com/emitter-io/address"
"github.com/emitter-io/emitter/internal/broker/keygen"
"github.com/emitter-io/emitter/internal/errors"
"github.com/emitter-io/emitter/internal/message"
"github.com/emitter-io/emitter/internal/network/mqtt"
"github.com/emitter-io/emitter/internal/provider/contract"
Expand All @@ -49,6 +51,7 @@ type Conn struct {
measurer stats.Measurer // The measurer to use for monitoring.
links map[string]string // The map of all pre-authorized links.
limit *rate.Limiter // The read rate limiter.
keys *keygen.Provider // The key generation provider.
}

// NewConn creates a new connection.
Expand All @@ -61,6 +64,7 @@ func (s *Service) newConn(t net.Conn, readRate int) *Conn {
subs: message.NewCounters(),
measurer: s.measurer,
links: map[string]string{},
keys: s.Keygen,
}

// Generate a globally unique id as well
Expand Down Expand Up @@ -236,16 +240,16 @@ func (c *Conn) Send(m *message.Message) (err error) {
}

// notifyError notifies the connection about an error
func (c *Conn) notifyError(err *Error, requestID uint16) {
func (c *Conn) notifyError(err *errors.Error, requestID uint16) {
c.sendResponse("emitter/error/", err, requestID)
}

func (c *Conn) sendResponse(topic string, resp response, requestID uint16) {
switch m := resp.(type) {
case *Error:
errCopy := *m // Copy the value
errCopy.ForRequest(requestID)
resp = &errCopy
case *errors.Error:
cpy := m.Copy()
cpy.ForRequest(requestID)
resp = cpy
default:
m.ForRequest(requestID)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/broker/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"io/ioutil"
"testing"

"github.com/emitter-io/emitter/internal/errors"
"github.com/emitter-io/emitter/internal/message"
netmock "github.com/emitter-io/emitter/internal/network/mock"
"github.com/emitter-io/emitter/internal/security/license"
Expand All @@ -43,11 +44,11 @@ func TestNotifyError(t *testing.T) {
assert.NotNil(t, pipe)

go func() {
conn.notifyError(ErrUnauthorized, 1)
conn.notifyError(errors.ErrUnauthorized, 1)
conn.Close()
}()

b, err := ioutil.ReadAll(pipe.Server)
assert.Contains(t, string(b), ErrUnauthorized.Message)
assert.Contains(t, string(b), errors.ErrUnauthorized.Message)
assert.NoError(t, err)
}
23 changes: 0 additions & 23 deletions internal/broker/generate/assets_gen.go

This file was deleted.

Loading

0 comments on commit 7c6b3ed

Please sign in to comment.