-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor password service to provide userID instead of name
- Loading branch information
Showing
20 changed files
with
313 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package authorizer | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/influxdata/influxdb" | ||
) | ||
|
||
// PasswordService is a new authorization middleware for a password service. | ||
type PasswordService struct { | ||
next influxdb.PasswordsService | ||
} | ||
|
||
// NewPasswordService wraps an existing password service with auth middlware. | ||
func NewPasswordService(svc influxdb.PasswordsService) *PasswordService { | ||
return &PasswordService{next: svc} | ||
} | ||
|
||
// SetPassword overrides the password of a known user. | ||
func (s *PasswordService) SetPassword(ctx context.Context, userID influxdb.ID, password string) error { | ||
if err := authorizeWriteUser(ctx, userID); err != nil { | ||
return err | ||
} | ||
|
||
return s.next.SetPassword(ctx, userID, password) | ||
} | ||
|
||
// ComparePassword checks if the password matches the password recorded. | ||
// Passwords that do not match return errors. | ||
func (s *PasswordService) ComparePassword(ctx context.Context, userID influxdb.ID, password string) error { | ||
panic("not implemented") | ||
} | ||
|
||
// CompareAndSetPassword checks the password and if they match | ||
// updates to the new password. | ||
func (s *PasswordService) CompareAndSetPassword(ctx context.Context, userID influxdb.ID, old string, new string) error { | ||
panic("not implemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package authorizer_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/influxdata/influxdb" | ||
"github.com/influxdata/influxdb/authorizer" | ||
icontext "github.com/influxdata/influxdb/context" | ||
"github.com/influxdata/influxdb/mock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPasswordService(t *testing.T) { | ||
t.Run("SetPassword", func(t *testing.T) { | ||
t.Run("user with permissions should proceed", func(t *testing.T) { | ||
userID := influxdb.ID(1) | ||
|
||
permission := influxdb.Permission{ | ||
Action: influxdb.WriteAction, | ||
Resource: influxdb.Resource{ | ||
Type: influxdb.UsersResourceType, | ||
ID: &userID, | ||
}, | ||
} | ||
|
||
fakeSVC := mock.NewPasswordsService() | ||
fakeSVC.SetPasswordFn = func(_ context.Context, _ influxdb.ID, _ string) error { | ||
return nil | ||
} | ||
s := authorizer.NewPasswordService(fakeSVC) | ||
|
||
ctx := icontext.SetAuthorizer(context.Background(), &Authorizer{ | ||
Permissions: []influxdb.Permission{permission}, | ||
}) | ||
|
||
err := s.SetPassword(ctx, 1, "password") | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("user without permissions should proceed", func(t *testing.T) { | ||
goodUserID := influxdb.ID(1) | ||
badUserID := influxdb.ID(3) | ||
|
||
tests := []struct { | ||
name string | ||
badPermission influxdb.Permission | ||
}{ | ||
{ | ||
name: "has no access", | ||
}, | ||
{ | ||
name: "has read only access on correct resource", | ||
badPermission: influxdb.Permission{ | ||
Action: influxdb.ReadAction, | ||
Resource: influxdb.Resource{ | ||
Type: influxdb.UsersResourceType, | ||
ID: &goodUserID, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "has write access on incorrect resource", | ||
badPermission: influxdb.Permission{ | ||
Action: influxdb.WriteAction, | ||
Resource: influxdb.Resource{ | ||
Type: influxdb.OrgsResourceType, | ||
ID: &goodUserID, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "user accessing user that is not self", | ||
badPermission: influxdb.Permission{ | ||
Action: influxdb.WriteAction, | ||
Resource: influxdb.Resource{ | ||
Type: influxdb.UsersResourceType, | ||
ID: &badUserID, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
fn := func(t *testing.T) { | ||
fakeSVC := &mock.PasswordsService{ | ||
SetPasswordFn: func(_ context.Context, _ influxdb.ID, _ string) error { | ||
return nil | ||
}, | ||
} | ||
s := authorizer.NewPasswordService(fakeSVC) | ||
|
||
ctx := icontext.SetAuthorizer(context.Background(), &Authorizer{ | ||
Permissions: []influxdb.Permission{tt.badPermission}, | ||
}) | ||
|
||
err := s.SetPassword(ctx, goodUserID, "password") | ||
require.Error(t, err) | ||
} | ||
|
||
t.Run(tt.name, fn) | ||
} | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.