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

NOISSUE - Fix bootstrap token naming and interfaces named args #1117

Merged
merged 7 commits into from
Apr 16, 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
2 changes: 1 addition & 1 deletion bootstrap/api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func addEndpoint(svc bootstrap.Service) endpoint.Endpoint {
Content: req.Content,
}

saved, err := svc.Add(req.key, config)
saved, err := svc.Add(req.token, config)
if err != nil {
return nil, err
}
Expand Down
46 changes: 23 additions & 23 deletions bootstrap/api/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,46 @@ func NewLoggingMiddleware(svc bootstrap.Service, logger log.Logger) bootstrap.Se
return &loggingMiddleware{logger, svc}
}

func (lm *loggingMiddleware) Add(key string, cfg bootstrap.Config) (saved bootstrap.Config, err error) {
func (lm *loggingMiddleware) Add(token string, cfg bootstrap.Config) (saved bootstrap.Config, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method add for key %s and thing %s took %s to complete", key, saved.MFThing, time.Since(begin))
message := fmt.Sprintf("Method add for token %s and thing %s took %s to complete", token, saved.MFThing, time.Since(begin))
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
}(time.Now())

return lm.svc.Add(key, cfg)
return lm.svc.Add(token, cfg)
}

func (lm *loggingMiddleware) View(key, id string) (saved bootstrap.Config, err error) {
func (lm *loggingMiddleware) View(token, id string) (saved bootstrap.Config, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method view for key %s and thing %s took %s to complete", key, saved.MFThing, time.Since(begin))
message := fmt.Sprintf("Method view for token %s and thing %s took %s to complete", token, saved.MFThing, time.Since(begin))
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
}(time.Now())

return lm.svc.View(key, id)
return lm.svc.View(token, id)
}

func (lm *loggingMiddleware) Update(key string, cfg bootstrap.Config) (err error) {
func (lm *loggingMiddleware) Update(token string, cfg bootstrap.Config) (err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method update for key %s and thing %s took %s to complete", key, cfg.MFThing, time.Since(begin))
message := fmt.Sprintf("Method update for token %s and thing %s took %s to complete", token, cfg.MFThing, time.Since(begin))
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
}(time.Now())

return lm.svc.Update(key, cfg)
return lm.svc.Update(token, cfg)
}

func (lm *loggingMiddleware) UpdateCert(key, thingID, clientCert, clientKey, caCert string) (err error) {
func (lm *loggingMiddleware) UpdateCert(token, thingID, clientCert, clientKey, caCert string) (err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method update_cert for thing with id %s took %s to complete", thingID, time.Since(begin))
if err != nil {
Expand All @@ -74,46 +74,46 @@ func (lm *loggingMiddleware) UpdateCert(key, thingID, clientCert, clientKey, caC
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
}(time.Now())

return lm.svc.UpdateCert(key, thingID, clientCert, clientKey, caCert)
return lm.svc.UpdateCert(token, thingID, clientCert, clientKey, caCert)
}

func (lm *loggingMiddleware) UpdateConnections(key, id string, connections []string) (err error) {
func (lm *loggingMiddleware) UpdateConnections(token, id string, connections []string) (err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method update_connections for key %s and thing %s took %s to complete", key, id, time.Since(begin))
message := fmt.Sprintf("Method update_connections for token %s and thing %s took %s to complete", token, id, time.Since(begin))
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
}(time.Now())

return lm.svc.UpdateConnections(key, id, connections)
return lm.svc.UpdateConnections(token, id, connections)
}

func (lm *loggingMiddleware) List(key string, filter bootstrap.Filter, offset, limit uint64) (res bootstrap.ConfigsPage, err error) {
func (lm *loggingMiddleware) List(token string, filter bootstrap.Filter, offset, limit uint64) (res bootstrap.ConfigsPage, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method list for key %s and offset %d and limit %d took %s to complete", key, offset, limit, time.Since(begin))
message := fmt.Sprintf("Method list for token %s and offset %d and limit %d took %s to complete", token, offset, limit, time.Since(begin))
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
}(time.Now())

return lm.svc.List(key, filter, offset, limit)
return lm.svc.List(token, filter, offset, limit)
}

func (lm *loggingMiddleware) Remove(key, id string) (err error) {
func (lm *loggingMiddleware) Remove(token, id string) (err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method remove for key %s and thing %s took %s to complete", key, id, time.Since(begin))
message := fmt.Sprintf("Method remove for token %s and thing %s took %s to complete", token, id, time.Since(begin))
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
}(time.Now())

return lm.svc.Remove(key, id)
return lm.svc.Remove(token, id)
}

func (lm *loggingMiddleware) Bootstrap(externalKey, externalID string, secure bool) (cfg bootstrap.Config, err error) {
Expand All @@ -129,17 +129,17 @@ func (lm *loggingMiddleware) Bootstrap(externalKey, externalID string, secure bo
return lm.svc.Bootstrap(externalKey, externalID, secure)
}

func (lm *loggingMiddleware) ChangeState(key, id string, state bootstrap.State) (err error) {
func (lm *loggingMiddleware) ChangeState(token, id string, state bootstrap.State) (err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method change_state for key %s and thing %s took %s to complete", key, id, time.Since(begin))
message := fmt.Sprintf("Method change_state for token %s and thing %s took %s to complete", token, id, time.Since(begin))
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
}(time.Now())

return lm.svc.ChangeState(key, id, state)
return lm.svc.ChangeState(token, id, state)
}

func (lm *loggingMiddleware) UpdateChannelHandler(channel bootstrap.Channel) (err error) {
Expand Down
32 changes: 16 additions & 16 deletions bootstrap/api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,67 +30,67 @@ func MetricsMiddleware(svc bootstrap.Service, counter metrics.Counter, latency m
}
}

func (mm *metricsMiddleware) Add(key string, cfg bootstrap.Config) (saved bootstrap.Config, err error) {
func (mm *metricsMiddleware) Add(token string, cfg bootstrap.Config) (saved bootstrap.Config, err error) {
defer func(begin time.Time) {
mm.counter.With("method", "add").Add(1)
mm.latency.With("method", "add").Observe(time.Since(begin).Seconds())
}(time.Now())

return mm.svc.Add(key, cfg)
return mm.svc.Add(token, cfg)
}

func (mm *metricsMiddleware) View(id, key string) (saved bootstrap.Config, err error) {
func (mm *metricsMiddleware) View(token, id string) (saved bootstrap.Config, err error) {
defer func(begin time.Time) {
mm.counter.With("method", "view").Add(1)
mm.latency.With("method", "view").Observe(time.Since(begin).Seconds())
}(time.Now())

return mm.svc.View(id, key)
return mm.svc.View(token, id)
}

func (mm *metricsMiddleware) Update(key string, cfg bootstrap.Config) (err error) {
func (mm *metricsMiddleware) Update(token string, cfg bootstrap.Config) (err error) {
defer func(begin time.Time) {
mm.counter.With("method", "update").Add(1)
mm.latency.With("method", "update").Observe(time.Since(begin).Seconds())
}(time.Now())

return mm.svc.Update(key, cfg)
return mm.svc.Update(token, cfg)
}

func (mm *metricsMiddleware) UpdateCert(key, thingKey, clientCert, clientKey, caCert string) (err error) {
func (mm *metricsMiddleware) UpdateCert(token, thingKey, clientCert, clientKey, caCert string) (err error) {
defer func(begin time.Time) {
mm.counter.With("method", "update_cert").Add(1)
mm.latency.With("method", "update_cert").Observe(time.Since(begin).Seconds())
}(time.Now())

return mm.svc.UpdateCert(key, thingKey, clientCert, clientKey, caCert)
return mm.svc.UpdateCert(token, thingKey, clientCert, clientKey, caCert)
}

func (mm *metricsMiddleware) UpdateConnections(key, id string, connections []string) (err error) {
func (mm *metricsMiddleware) UpdateConnections(token, id string, connections []string) (err error) {
defer func(begin time.Time) {
mm.counter.With("method", "update_connections").Add(1)
mm.latency.With("method", "update_connections").Observe(time.Since(begin).Seconds())
}(time.Now())

return mm.svc.UpdateConnections(key, id, connections)
return mm.svc.UpdateConnections(token, id, connections)
}

func (mm *metricsMiddleware) List(key string, filter bootstrap.Filter, offset, limit uint64) (saved bootstrap.ConfigsPage, err error) {
func (mm *metricsMiddleware) List(token string, filter bootstrap.Filter, offset, limit uint64) (saved bootstrap.ConfigsPage, err error) {
defer func(begin time.Time) {
mm.counter.With("method", "list").Add(1)
mm.latency.With("method", "list").Observe(time.Since(begin).Seconds())
}(time.Now())

return mm.svc.List(key, filter, offset, limit)
return mm.svc.List(token, filter, offset, limit)
}

func (mm *metricsMiddleware) Remove(id, key string) (err error) {
func (mm *metricsMiddleware) Remove(token, id string) (err error) {
defer func(begin time.Time) {
mm.counter.With("method", "remove").Add(1)
mm.latency.With("method", "remove").Observe(time.Since(begin).Seconds())
}(time.Now())

return mm.svc.Remove(id, key)
return mm.svc.Remove(token, id)
}

func (mm *metricsMiddleware) Bootstrap(externalKey, externalID string, secure bool) (cfg bootstrap.Config, err error) {
Expand All @@ -102,13 +102,13 @@ func (mm *metricsMiddleware) Bootstrap(externalKey, externalID string, secure bo
return mm.svc.Bootstrap(externalKey, externalID, secure)
}

func (mm *metricsMiddleware) ChangeState(id, key string, state bootstrap.State) (err error) {
func (mm *metricsMiddleware) ChangeState(token, id string, state bootstrap.State) (err error) {
defer func(begin time.Time) {
mm.counter.With("method", "change_state").Add(1)
mm.latency.With("method", "change_state").Observe(time.Since(begin).Seconds())
}(time.Now())

return mm.svc.ChangeState(id, key, state)
return mm.svc.ChangeState(token, id, state)
}

func (mm *metricsMiddleware) UpdateChannelHandler(channel bootstrap.Channel) (err error) {
Expand Down
4 changes: 2 additions & 2 deletions bootstrap/api/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type apiReq interface {
}

type addReq struct {
key string
token string
ThingID string `json:"thing_id"`
ExternalID string `json:"external_id"`
ExternalKey string `json:"external_key"`
Expand All @@ -23,7 +23,7 @@ type addReq struct {
}

func (req addReq) validate() error {
if req.key == "" {
if req.token == "" {
return bootstrap.ErrUnauthorizedAccess
}

Expand Down
10 changes: 5 additions & 5 deletions bootstrap/api/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@ import (
func TestAddReqValidation(t *testing.T) {
cases := []struct {
desc string
key string
token string
externalID string
externalKey string
err error
}{
{
desc: "empty key",
key: "",
token: "",
externalID: "external-id",
externalKey: "external-key",
err: bootstrap.ErrUnauthorizedAccess,
},
{
desc: "empty external ID",
key: "key",
token: "token",
externalID: "",
externalKey: "external-key",
err: bootstrap.ErrMalformedEntity,
},
{
desc: "empty external key",
key: "key",
token: "token",
externalID: "external-id",
externalKey: "",
err: bootstrap.ErrMalformedEntity,
Expand All @@ -41,7 +41,7 @@ func TestAddReqValidation(t *testing.T) {

for _, tc := range cases {
req := addReq{
key: tc.key,
token: tc.token,
ExternalID: tc.externalID,
ExternalKey: tc.externalKey,
}
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func decodeAddRequest(_ context.Context, r *http.Request) (interface{}, error) {
return nil, errUnsupportedContentType
}

req := addReq{key: r.Header.Get("Authorization")}
req := addReq{token: r.Header.Get("Authorization")}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return nil, errors.Wrap(bootstrap.ErrMalformedEntity, err)
}
Expand Down
38 changes: 19 additions & 19 deletions bootstrap/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,60 +50,60 @@ type ConfigsPage struct {
type ConfigRepository interface {
// Save persists the Config. Successful operation is indicated by non-nil
// error response.
Save(Config, []string) (string, error)
Save(cfg Config, chsConnIDs []string) (string, error)

// RetrieveByID retrieves the Config having the provided identifier, that is owned
// by the specified user.
RetrieveByID(string, string) (Config, error)
RetrieveByID(owner, id string) (Config, error)

// RetrieveAll retrieves a subset of Configs that are owned
// by the specific user, with given filter parameters.
RetrieveAll(string, Filter, uint64, uint64) ConfigsPage
RetrieveAll(owner string, filter Filter, offset, limit uint64) ConfigsPage

// RetrieveByExternalID returns Config for given external ID.
RetrieveByExternalID(string) (Config, error)
RetrieveByExternalID(externalID string) (Config, error)

// Update updates an existing Config. A non-nil error is returned
// to indicate operation failure.
Update(Config) error
Update(cfg Config) error

// UpdateCerts updates an existing Config certificate and key.
// UpdateCerts updates an existing Config certificate and owner.
// A non-nil error is returned to indicate operation failure.
UpdateCert(string, string, string, string, string) error
UpdateCert(owner, thingID, clientCert, clientKey, caCert string) error

// UpdateConnections updates a list of Channels the Config is connected to
// adding new Channels if needed.
UpdateConnections(string, string, []Channel, []string) error
UpdateConnections(owner, id string, channels []Channel, connections []string) error

// Remove removes the Config having the provided identifier, that is owned
// by the specified user.
Remove(string, string) error
Remove(owner, id string) error

// ChangeState changes of the Config, that is owned by the specific user.
ChangeState(string, string, State) error
ChangeState(owner, id string, state State) error

// ListExisting retrieves those channels from the given list that exist in DB.
ListExisting(owner string, ids []string) ([]Channel, error)

// SaveUnknown saves Thing which unsuccessfully bootstrapped.
SaveUnknown(string, string) error
SaveUnknown(owner, id string) error

// RetrieveUnknown returns a subset of unsuccessfully bootstrapped Things.
RetrieveUnknown(uint64, uint64) ConfigsPage

// ListExisting retrieves those channels from the given list that exist in DB.
ListExisting(string, []string) ([]Channel, error)
RetrieveUnknown(offset, limit uint64) ConfigsPage

// Methods RemoveThing, UpdateChannel, and RemoveChannel are related to
// event sourcing. That's why these methods surpass ownership check.

// RemoveThing removes Config of the Thing with the given ID.
RemoveThing(string) error
RemoveThing(id string) error

// UpdateChannel updates channel with the given ID.
UpdateChannel(Channel) error
UpdateChannel(c Channel) error

// RemoveChannel removes channel with the given ID.
RemoveChannel(string) error
RemoveChannel(id string) error

// DisconnectHandler changes state of the Config when the corresponding Thing is
// disconnected from the Channel.
DisconnectThing(string, string) error
DisconnectThing(channelID, thingID string) error
}
Loading