Skip to content

Commit

Permalink
MG-2426 - Replace generic Clients in Users service (#2436)
Browse files Browse the repository at this point in the history
Signed-off-by: Musilah <[email protected]>
Signed-off-by: Arvindh <[email protected]>
Signed-off-by: Felix Gateru <[email protected]>
Signed-off-by: Dusan Borovcanin <[email protected]>
Co-authored-by: Arvindh <[email protected]>
Co-authored-by: Felix Gateru <[email protected]>
Co-authored-by: Dusan Borovcanin <[email protected]>
  • Loading branch information
4 people authored Oct 30, 2024
1 parent 077882f commit 0019f71
Show file tree
Hide file tree
Showing 75 changed files with 6,202 additions and 4,553 deletions.
283 changes: 227 additions & 56 deletions api/openapi/users.yml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bootstrap/events/producer/streams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func TestAdd(t *testing.T) {
lastID := "0"
for _, tc := range cases {
tc.session = mgauthn.Session{UserID: validID, DomainID: tc.domainID, DomainUserID: validID}
sdkCall := tv.sdk.On("Thing", tc.config.ThingID, tc.domainID, tc.token).Return(mgsdk.Thing{ID: tc.config.ThingID, Credentials: mgsdk.Credentials{Secret: tc.config.ThingKey}}, errors.NewSDKError(tc.thingErr))
sdkCall := tv.sdk.On("Thing", tc.config.ThingID, tc.domainID, tc.token).Return(mgsdk.Thing{ID: tc.config.ThingID, Credentials: mgsdk.ClientCredentials{Secret: tc.config.ThingKey}}, errors.NewSDKError(tc.thingErr))
repoCall := tv.boot.On("ListExisting", context.Background(), domainID, mock.Anything).Return(tc.config.Channels, tc.listErr)
repoCall1 := tv.boot.On("Save", context.Background(), mock.Anything, mock.Anything).Return(mock.Anything, tc.saveErr)

Expand Down
2 changes: 1 addition & 1 deletion bootstrap/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func TestAdd(t *testing.T) {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
tc.session = mgauthn.Session{UserID: tc.userID, DomainID: tc.domainID, DomainUserID: validID}
repoCall := sdk.On("Thing", tc.config.ThingID, mock.Anything, tc.token).Return(mgsdk.Thing{ID: tc.config.ThingID, Credentials: mgsdk.Credentials{Secret: tc.config.ThingKey}}, tc.thingErr)
repoCall := sdk.On("Thing", tc.config.ThingID, mock.Anything, tc.token).Return(mgsdk.Thing{ID: tc.config.ThingID, Credentials: mgsdk.ClientCredentials{Secret: tc.config.ThingKey}}, tc.thingErr)
repoCall1 := sdk.On("CreateThing", mock.Anything, tc.domainID, tc.token).Return(mgsdk.Thing{}, tc.createThingErr)
repoCall2 := sdk.On("DeleteThing", tc.config.ThingID, tc.domainID, tc.token).Return(tc.deleteThingErr)
repoCall3 := boot.On("ListExisting", context.Background(), tc.domainID, mock.Anything).Return(tc.config.Channels, tc.listExistingErr)
Expand Down
12 changes: 6 additions & 6 deletions bootstrap/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func New(svc bootstrap.Service, tracer trace.Tracer) bootstrap.Service {

// Add traces the "Add" operation of the wrapped bootstrap.Service.
func (tm *tracingMiddleware) Add(ctx context.Context, session mgauthn.Session, token string, cfg bootstrap.Config) (bootstrap.Config, error) {
ctx, span := tm.tracer.Start(ctx, "svc_register_client", trace.WithAttributes(
ctx, span := tm.tracer.Start(ctx, "svc_register_user", trace.WithAttributes(
attribute.String("thing_id", cfg.ThingID),
attribute.String("domain_id ", cfg.DomainID),
attribute.String("name", cfg.Name),
Expand All @@ -41,7 +41,7 @@ func (tm *tracingMiddleware) Add(ctx context.Context, session mgauthn.Session, t

// View traces the "View" operation of the wrapped bootstrap.Service.
func (tm *tracingMiddleware) View(ctx context.Context, session mgauthn.Session, id string) (bootstrap.Config, error) {
ctx, span := tm.tracer.Start(ctx, "svc_view_client", trace.WithAttributes(
ctx, span := tm.tracer.Start(ctx, "svc_view_user", trace.WithAttributes(
attribute.String("id", id),
))
defer span.End()
Expand All @@ -51,7 +51,7 @@ func (tm *tracingMiddleware) View(ctx context.Context, session mgauthn.Session,

// Update traces the "Update" operation of the wrapped bootstrap.Service.
func (tm *tracingMiddleware) Update(ctx context.Context, session mgauthn.Session, cfg bootstrap.Config) error {
ctx, span := tm.tracer.Start(ctx, "svc_update_client", trace.WithAttributes(
ctx, span := tm.tracer.Start(ctx, "svc_update_user", trace.WithAttributes(
attribute.String("name", cfg.Name),
attribute.String("content", cfg.Content),
attribute.String("thing_id", cfg.ThingID),
Expand Down Expand Up @@ -85,7 +85,7 @@ func (tm *tracingMiddleware) UpdateConnections(ctx context.Context, session mgau

// List traces the "List" operation of the wrapped bootstrap.Service.
func (tm *tracingMiddleware) List(ctx context.Context, session mgauthn.Session, filter bootstrap.Filter, offset, limit uint64) (bootstrap.ConfigsPage, error) {
ctx, span := tm.tracer.Start(ctx, "svc_list_clients", trace.WithAttributes(
ctx, span := tm.tracer.Start(ctx, "svc_list_users", trace.WithAttributes(
attribute.Int64("offset", int64(offset)),
attribute.Int64("limit", int64(limit)),
))
Expand All @@ -96,7 +96,7 @@ func (tm *tracingMiddleware) List(ctx context.Context, session mgauthn.Session,

// Remove traces the "Remove" operation of the wrapped bootstrap.Service.
func (tm *tracingMiddleware) Remove(ctx context.Context, session mgauthn.Session, id string) error {
ctx, span := tm.tracer.Start(ctx, "svc_remove_client", trace.WithAttributes(
ctx, span := tm.tracer.Start(ctx, "svc_remove_user", trace.WithAttributes(
attribute.String("id", id),
))
defer span.End()
Expand All @@ -106,7 +106,7 @@ func (tm *tracingMiddleware) Remove(ctx context.Context, session mgauthn.Session

// Bootstrap traces the "Bootstrap" operation of the wrapped bootstrap.Service.
func (tm *tracingMiddleware) Bootstrap(ctx context.Context, externalKey, externalID string, secure bool) (bootstrap.Config, error) {
ctx, span := tm.tracer.Start(ctx, "svc_bootstrap_client", trace.WithAttributes(
ctx, span := tm.tracer.Start(ctx, "svc_bootstrap_user", trace.WithAttributes(
attribute.String("external_key", externalKey),
attribute.String("external_id", externalID),
attribute.Bool("secure", secure),
Expand Down
4 changes: 2 additions & 2 deletions certs/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestIssueCert(t *testing.T) {

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
sdkCall := sdk.On("Thing", tc.thingID, tc.domainID, tc.token).Return(mgsdk.Thing{ID: tc.thingID, Credentials: mgsdk.Credentials{Secret: thingKey}}, tc.thingErr)
sdkCall := sdk.On("Thing", tc.thingID, tc.domainID, tc.token).Return(mgsdk.Thing{ID: tc.thingID, Credentials: mgsdk.ClientCredentials{Secret: thingKey}}, tc.thingErr)
agentCall := agent.On("Issue", thingID, tc.ttl, tc.ipAddr).Return(tc.cert, tc.issueCertErr)
resp, err := svc.IssueCert(context.Background(), tc.domainID, tc.token, tc.thingID, tc.ttl)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestRevokeCert(t *testing.T) {

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
sdkCall := sdk.On("Thing", tc.thingID, tc.domainID, tc.token).Return(mgsdk.Thing{ID: tc.thingID, Credentials: mgsdk.Credentials{Secret: thingKey}}, tc.thingErr)
sdkCall := sdk.On("Thing", tc.thingID, tc.domainID, tc.token).Return(mgsdk.Thing{ID: tc.thingID, Credentials: mgsdk.ClientCredentials{Secret: thingKey}}, tc.thingErr)
agentCall := agent.On("Revoke", mock.Anything).Return(tc.revokeErr)
agentCall1 := agent.On("ListCerts", mock.Anything).Return(tc.page, tc.listErr)
_, err := svc.RevokeCert(context.Background(), tc.domainID, tc.token, tc.thingID)
Expand Down
10 changes: 5 additions & 5 deletions cli/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,10 @@ var cmdProvision = []cobra.Command{
// Create test user
name := namesgenerator.Generate()
user := mgxsdk.User{
Name: name,
FirstName: name,
Email: fmt.Sprintf("%[email protected]", name),
Credentials: mgxsdk.Credentials{
Identity: fmt.Sprintf("%[email protected]", name),
Username: name,
Secret: "12345678",
},
Status: mgxsdk.EnabledStatus,
Expand All @@ -148,8 +149,7 @@ var cmdProvision = []cobra.Command{
return
}

user.Credentials.Secret = "12345678"
ut, err := sdk.CreateToken(mgxsdk.Login{Identity: user.Credentials.Identity, Secret: user.Credentials.Secret})
ut, err := sdk.CreateToken(mgxsdk.Login{Username: user.Username, Secret: user.Credentials.Secret})
if err != nil {
logErrorCmd(*cmd, err)
return
Expand All @@ -166,7 +166,7 @@ var cmdProvision = []cobra.Command{
return
}

ut, err = sdk.CreateToken(mgxsdk.Login{Identity: user.Credentials.Identity, Secret: user.Credentials.Secret})
ut, err = sdk.CreateToken(mgxsdk.Login{Email: user.Email, Secret: user.Credentials.Secret})
if err != nil {
logErrorCmd(*cmd, err)
return
Expand Down
4 changes: 2 additions & 2 deletions cli/things_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var (
var thing = sdk.Thing{
ID: testsutil.GenerateUUID(&testing.T{}),
Name: "testthing",
Credentials: sdk.Credentials{
Credentials: sdk.ClientCredentials{
Secret: "secret",
},
DomainID: testsutil.GenerateUUID(&testing.T{}),
Expand Down Expand Up @@ -385,7 +385,7 @@ func TestUpdateThingCmd(t *testing.T) {
ID: thing.ID,
DomainID: thing.DomainID,
Status: thing.Status,
Credentials: sdk.Credentials{
Credentials: sdk.ClientCredentials{
Secret: newSecret,
},
},
Expand Down
64 changes: 41 additions & 23 deletions cli/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,38 @@ import (
"net/url"
"strconv"

mgclients "github.com/absmach/magistrala/pkg/clients"
mgxsdk "github.com/absmach/magistrala/pkg/sdk/go"
"github.com/absmach/magistrala/users"
"github.com/spf13/cobra"
)

var cmdUsers = []cobra.Command{
{
Use: "create <name> <username> <password> <user_auth_token>",
Use: "create <first_name> <last_name> <email> <username> <password> <user_auth_token>",
Short: "Create user",
Long: "Create user with provided name, username and password. Token is optional\n" +
Long: "Create user with provided firstname, lastname, email, username and password. Token is optional\n" +
"For example:\n" +
"\tmagistrala-cli users create user user@example.com 12345678 $USER_AUTH_TOKEN\n",
"\tmagistrala-cli users create jane doe janedoe@example.com jane_doe 12345678 $USER_AUTH_TOKEN\n",
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 3 || len(args) > 4 {
if len(args) < 5 || len(args) > 6 {
logUsageCmd(*cmd, cmd.Use)
return
}
if len(args) == 3 {
if len(args) == 5 {
args = append(args, "")
}

user := mgxsdk.User{
Name: args[0],
FirstName: args[0],
LastName: args[1],
Email: args[2],
Credentials: mgxsdk.Credentials{
Identity: args[1],
Secret: args[2],
Username: args[3],
Secret: args[4],
},
Status: mgclients.EnabledStatus.String(),
Status: users.EnabledStatus.String(),
}
user, err := sdk.CreateUser(user, args[3])
user, err := sdk.CreateUser(user, args[5])
if err != nil {
logErrorCmd(*cmd, err)
return
Expand Down Expand Up @@ -66,6 +68,7 @@ var cmdUsers = []cobra.Command{
return
}
pageMetadata := mgxsdk.PageMetadata{
Username: Username,
Identity: Identity,
Offset: Offset,
Limit: Limit,
Expand Down Expand Up @@ -93,21 +96,21 @@ var cmdUsers = []cobra.Command{
{
Use: "token <username> <password>",
Short: "Get token",
Long: "Generate new token from username and password\n" +
Long: "Generate a new token with username and password\n" +
"For example:\n" +
"\tmagistrala-cli users token [email protected] 12345678\n",
"\tmagistrala-cli users token jane.doe 12345678\n",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
logUsageCmd(*cmd, cmd.Use)
return
}

lg := mgxsdk.Login{
Identity: args[0],
loginReq := mgxsdk.Login{
Username: args[0],
Secret: args[1],
}

token, err := sdk.CreateToken(lg)
token, err := sdk.CreateToken(loginReq)
if err != nil {
logErrorCmd(*cmd, err)
return
Expand All @@ -116,6 +119,7 @@ var cmdUsers = []cobra.Command{
logJSONCmd(*cmd, token)
},
},

{
Use: "refreshtoken <token>",
Short: "Get token",
Expand All @@ -138,13 +142,15 @@ var cmdUsers = []cobra.Command{
},
},
{
Use: "update [<user_id> <JSON_string> | tags <user_id> <tags> | identity <user_id> <identity> ] <user_auth_token>",
Use: "update [<user_id> <JSON_string> | tags <user_id> <tags> | username <user_id> <username> | email <user_id> <email>] <user_auth_token>",
Short: "Update user",
Long: "Updates either user name and metadata or user tags or user identity\n" +
Long: "Updates either user name and metadata or user tags or user email\n" +
"Usage:\n" +
"\tmagistrala-cli users update <user_id> '{\"name\":\"new name\", \"metadata\":{\"key\": \"value\"}}' $USERTOKEN - updates user name and metadata\n" +
"\tmagistrala-cli users update <user_id> '{\"first_name\":\"new first_name\", \"metadata\":{\"key\": \"value\"}}' $USERTOKEN - updates user first and lastname and metadata\n" +
"\tmagistrala-cli users update tags <user_id> '[\"tag1\", \"tag2\"]' $USERTOKEN - updates user tags\n" +
"\tmagistrala-cli users update identity <user_id> [email protected] $USERTOKEN - updates user identity\n",
"\tmagistrala-cli users update username <user_id> newusername $USERTOKEN - updates user name\n" +
"\tmagistrala-cli users update email <user_id> [email protected] $USERTOKEN - updates user email\n",

Run: func(cmd *cobra.Command, args []string) {
if len(args) != 4 && len(args) != 3 {
logUsageCmd(*cmd, cmd.Use)
Expand All @@ -168,10 +174,22 @@ var cmdUsers = []cobra.Command{
return
}

if args[0] == "identity" {
if args[0] == "email" {
user.ID = args[1]
user.Email = args[2]
user, err := sdk.UpdateUserEmail(user, args[3])
if err != nil {
logErrorCmd(*cmd, err)
return
}
logJSONCmd(*cmd, user)
return
}

if args[0] == "username" {
user.ID = args[1]
user.Credentials.Identity = args[2]
user, err := sdk.UpdateUserIdentity(user, args[3])
user.Credentials.Username = args[2]
user, err := sdk.UpdateUser(user, args[3])
if err != nil {
logErrorCmd(*cmd, err)
return
Expand Down
Loading

0 comments on commit 0019f71

Please sign in to comment.