mirror of
https://github.com/ferdzo/fs.git
synced 2026-04-05 01:56:25 +00:00
add admin endpoints for user policy and status updates
This commit is contained in:
@@ -375,6 +375,93 @@ func (s *Service) DeleteUser(accessKeyID string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) SetUserPolicy(accessKeyID string, policy models.AuthPolicy) (*UserDetails, error) {
|
||||
if !s.cfg.Enabled {
|
||||
return nil, ErrAuthNotEnabled
|
||||
}
|
||||
accessKeyID = strings.TrimSpace(accessKeyID)
|
||||
if !validAccessKeyID.MatchString(accessKeyID) {
|
||||
return nil, fmt.Errorf("%w: invalid access key id", ErrInvalidUserInput)
|
||||
}
|
||||
|
||||
identity, err := s.store.GetAuthIdentity(accessKeyID)
|
||||
if err != nil {
|
||||
if errors.Is(err, metadata.ErrAuthIdentityNotFound) {
|
||||
return nil, ErrUserNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
normalizedPolicy, err := normalizePolicy(policy, accessKeyID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.store.PutAuthPolicy(&normalizedPolicy); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
identity.UpdatedAt = s.now().Unix()
|
||||
if err := s.store.PutAuthIdentity(identity); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &UserDetails{
|
||||
AccessKeyID: identity.AccessKeyID,
|
||||
Status: normalizeUserStatus(identity.Status),
|
||||
CreatedAt: identity.CreatedAt,
|
||||
UpdatedAt: identity.UpdatedAt,
|
||||
Policy: normalizedPolicy,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) SetUserStatus(accessKeyID, status string) (*UserDetails, error) {
|
||||
if !s.cfg.Enabled {
|
||||
return nil, ErrAuthNotEnabled
|
||||
}
|
||||
accessKeyID = strings.TrimSpace(accessKeyID)
|
||||
if !validAccessKeyID.MatchString(accessKeyID) {
|
||||
return nil, fmt.Errorf("%w: invalid access key id", ErrInvalidUserInput)
|
||||
}
|
||||
|
||||
status = strings.TrimSpace(status)
|
||||
if status == "" {
|
||||
return nil, fmt.Errorf("%w: status is required", ErrInvalidUserInput)
|
||||
}
|
||||
normalizedStatus := normalizeUserStatus(status)
|
||||
if normalizedStatus == "" {
|
||||
return nil, fmt.Errorf("%w: status must be active or disabled", ErrInvalidUserInput)
|
||||
}
|
||||
|
||||
identity, err := s.store.GetAuthIdentity(accessKeyID)
|
||||
if err != nil {
|
||||
if errors.Is(err, metadata.ErrAuthIdentityNotFound) {
|
||||
return nil, ErrUserNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
identity.Status = normalizedStatus
|
||||
identity.UpdatedAt = s.now().Unix()
|
||||
if err := s.store.PutAuthIdentity(identity); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
policy, err := s.store.GetAuthPolicy(accessKeyID)
|
||||
if err != nil {
|
||||
if errors.Is(err, metadata.ErrAuthPolicyNotFound) {
|
||||
return nil, ErrUserNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &UserDetails{
|
||||
AccessKeyID: identity.AccessKeyID,
|
||||
Status: normalizeUserStatus(identity.Status),
|
||||
CreatedAt: identity.CreatedAt,
|
||||
UpdatedAt: identity.UpdatedAt,
|
||||
Policy: *policy,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func parsePolicyJSON(raw string) (*models.AuthPolicy, error) {
|
||||
policy := models.AuthPolicy{}
|
||||
if err := json.Unmarshal([]byte(raw), &policy); err != nil {
|
||||
|
||||
@@ -138,6 +138,60 @@ func TestDeleteBootstrapUserRejected(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetUserPolicy(t *testing.T) {
|
||||
_, svc := newTestAuthService(t)
|
||||
|
||||
_, err := svc.CreateUser(CreateUserInput{
|
||||
AccessKeyID: "policy-user",
|
||||
SecretKey: "super-secret-1",
|
||||
Policy: models.AuthPolicy{
|
||||
Statements: []models.AuthPolicyStatement{
|
||||
{Effect: "allow", Actions: []string{"s3:GetObject"}, Bucket: "b1", Prefix: "*"},
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateUser returned error: %v", err)
|
||||
}
|
||||
|
||||
updated, err := svc.SetUserPolicy("policy-user", models.AuthPolicy{
|
||||
Statements: []models.AuthPolicyStatement{
|
||||
{Effect: "allow", Actions: []string{"s3:PutObject"}, Bucket: "b2", Prefix: "p/"},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("SetUserPolicy returned error: %v", err)
|
||||
}
|
||||
if len(updated.Policy.Statements) != 1 || updated.Policy.Statements[0].Actions[0] != "s3:PutObject" {
|
||||
t.Fatalf("SetUserPolicy did not apply new policy: %+v", updated.Policy)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetUserStatus(t *testing.T) {
|
||||
_, svc := newTestAuthService(t)
|
||||
|
||||
_, err := svc.CreateUser(CreateUserInput{
|
||||
AccessKeyID: "status-user",
|
||||
SecretKey: "super-secret-1",
|
||||
Policy: models.AuthPolicy{
|
||||
Statements: []models.AuthPolicyStatement{
|
||||
{Effect: "allow", Actions: []string{"s3:*"}, Bucket: "*", Prefix: "*"},
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateUser returned error: %v", err)
|
||||
}
|
||||
|
||||
updated, err := svc.SetUserStatus("status-user", "disabled")
|
||||
if err != nil {
|
||||
t.Fatalf("SetUserStatus returned error: %v", err)
|
||||
}
|
||||
if updated.Status != "disabled" {
|
||||
t.Fatalf("SetUserStatus status = %q, want disabled", updated.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func newTestAuthService(t *testing.T) (*metadata.MetadataHandler, *Service) {
|
||||
t.Helper()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user