8 changed files with 1519 additions and 212 deletions
-
87weed/admin/dash/admin_server.go
-
43weed/admin/dash/handler_admin.go
-
447weed/admin/dash/user_management.go
-
78weed/admin/handlers/handlers.go
-
255weed/admin/handlers/user_handlers.go
-
516weed/admin/static/js/admin.js
-
193weed/admin/view/app/object_store_users.templ
-
106weed/admin/view/app/object_store_users_templ.go
@ -0,0 +1,447 @@ |
|||
package dash |
|||
|
|||
import ( |
|||
"bytes" |
|||
"crypto/rand" |
|||
"encoding/base64" |
|||
"fmt" |
|||
"time" |
|||
|
|||
"github.com/seaweedfs/seaweedfs/weed/filer" |
|||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" |
|||
"github.com/seaweedfs/seaweedfs/weed/pb/iam_pb" |
|||
) |
|||
|
|||
// CreateObjectStoreUser creates a new user in identity.json
|
|||
func (s *AdminServer) CreateObjectStoreUser(req CreateUserRequest) (*ObjectStoreUser, error) { |
|||
s3cfg := &iam_pb.S3ApiConfiguration{} |
|||
|
|||
// Load existing configuration
|
|||
err := s.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error { |
|||
var buf bytes.Buffer |
|||
if err := filer.ReadEntry(nil, client, filer.IamConfigDirectory, filer.IamIdentityFile, &buf); err != nil { |
|||
if err != filer_pb.ErrNotFound { |
|||
return err |
|||
} |
|||
} |
|||
if buf.Len() > 0 { |
|||
return filer.ParseS3ConfigurationFromBytes(buf.Bytes(), s3cfg) |
|||
} |
|||
return nil |
|||
}) |
|||
|
|||
if err != nil { |
|||
return nil, fmt.Errorf("failed to load IAM configuration: %v", err) |
|||
} |
|||
|
|||
// Check if user already exists
|
|||
for _, identity := range s3cfg.Identities { |
|||
if identity.Name == req.Username { |
|||
return nil, fmt.Errorf("user %s already exists", req.Username) |
|||
} |
|||
} |
|||
|
|||
// Create new identity
|
|||
newIdentity := &iam_pb.Identity{ |
|||
Name: req.Username, |
|||
Actions: req.Actions, |
|||
} |
|||
|
|||
// Add account if email is provided
|
|||
if req.Email != "" { |
|||
newIdentity.Account = &iam_pb.Account{ |
|||
Id: generateAccountId(), |
|||
DisplayName: req.Username, |
|||
EmailAddress: req.Email, |
|||
} |
|||
} |
|||
|
|||
// Generate access key if requested
|
|||
var accessKey, secretKey string |
|||
if req.GenerateKey { |
|||
accessKey = generateAccessKey() |
|||
secretKey = generateSecretKey() |
|||
newIdentity.Credentials = []*iam_pb.Credential{ |
|||
{ |
|||
AccessKey: accessKey, |
|||
SecretKey: secretKey, |
|||
}, |
|||
} |
|||
} |
|||
|
|||
// Add to configuration
|
|||
s3cfg.Identities = append(s3cfg.Identities, newIdentity) |
|||
|
|||
// Save configuration
|
|||
err = s.saveS3Configuration(s3cfg) |
|||
if err != nil { |
|||
return nil, fmt.Errorf("failed to save IAM configuration: %v", err) |
|||
} |
|||
|
|||
// Return created user
|
|||
user := &ObjectStoreUser{ |
|||
Username: req.Username, |
|||
Email: req.Email, |
|||
AccessKey: accessKey, |
|||
SecretKey: secretKey, |
|||
Permissions: req.Actions, |
|||
} |
|||
|
|||
return user, nil |
|||
} |
|||
|
|||
// UpdateObjectStoreUser updates an existing user
|
|||
func (s *AdminServer) UpdateObjectStoreUser(username string, req UpdateUserRequest) (*ObjectStoreUser, error) { |
|||
s3cfg := &iam_pb.S3ApiConfiguration{} |
|||
|
|||
// Load existing configuration
|
|||
err := s.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error { |
|||
var buf bytes.Buffer |
|||
if err := filer.ReadEntry(nil, client, filer.IamConfigDirectory, filer.IamIdentityFile, &buf); err != nil { |
|||
return err |
|||
} |
|||
if buf.Len() > 0 { |
|||
return filer.ParseS3ConfigurationFromBytes(buf.Bytes(), s3cfg) |
|||
} |
|||
return nil |
|||
}) |
|||
|
|||
if err != nil { |
|||
return nil, fmt.Errorf("failed to load IAM configuration: %v", err) |
|||
} |
|||
|
|||
// Find and update user
|
|||
var updatedIdentity *iam_pb.Identity |
|||
for _, identity := range s3cfg.Identities { |
|||
if identity.Name == username { |
|||
updatedIdentity = identity |
|||
break |
|||
} |
|||
} |
|||
|
|||
if updatedIdentity == nil { |
|||
return nil, fmt.Errorf("user %s not found", username) |
|||
} |
|||
|
|||
// Update actions if provided
|
|||
if len(req.Actions) > 0 { |
|||
updatedIdentity.Actions = req.Actions |
|||
} |
|||
|
|||
// Update email if provided
|
|||
if req.Email != "" { |
|||
if updatedIdentity.Account == nil { |
|||
updatedIdentity.Account = &iam_pb.Account{ |
|||
Id: generateAccountId(), |
|||
DisplayName: username, |
|||
} |
|||
} |
|||
updatedIdentity.Account.EmailAddress = req.Email |
|||
} |
|||
|
|||
// Save configuration
|
|||
err = s.saveS3Configuration(s3cfg) |
|||
if err != nil { |
|||
return nil, fmt.Errorf("failed to save IAM configuration: %v", err) |
|||
} |
|||
|
|||
// Return updated user
|
|||
user := &ObjectStoreUser{ |
|||
Username: username, |
|||
Email: req.Email, |
|||
Permissions: updatedIdentity.Actions, |
|||
} |
|||
|
|||
// Get first access key for display
|
|||
if len(updatedIdentity.Credentials) > 0 { |
|||
user.AccessKey = updatedIdentity.Credentials[0].AccessKey |
|||
user.SecretKey = updatedIdentity.Credentials[0].SecretKey |
|||
} |
|||
|
|||
return user, nil |
|||
} |
|||
|
|||
// DeleteObjectStoreUser deletes a user from identity.json
|
|||
func (s *AdminServer) DeleteObjectStoreUser(username string) error { |
|||
s3cfg := &iam_pb.S3ApiConfiguration{} |
|||
|
|||
// Load existing configuration
|
|||
err := s.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error { |
|||
var buf bytes.Buffer |
|||
if err := filer.ReadEntry(nil, client, filer.IamConfigDirectory, filer.IamIdentityFile, &buf); err != nil { |
|||
return err |
|||
} |
|||
if buf.Len() > 0 { |
|||
return filer.ParseS3ConfigurationFromBytes(buf.Bytes(), s3cfg) |
|||
} |
|||
return nil |
|||
}) |
|||
|
|||
if err != nil { |
|||
return fmt.Errorf("failed to load IAM configuration: %v", err) |
|||
} |
|||
|
|||
// Find and remove user
|
|||
found := false |
|||
for i, identity := range s3cfg.Identities { |
|||
if identity.Name == username { |
|||
s3cfg.Identities = append(s3cfg.Identities[:i], s3cfg.Identities[i+1:]...) |
|||
found = true |
|||
break |
|||
} |
|||
} |
|||
|
|||
if !found { |
|||
return fmt.Errorf("user %s not found", username) |
|||
} |
|||
|
|||
// Save configuration
|
|||
return s.saveS3Configuration(s3cfg) |
|||
} |
|||
|
|||
// GetObjectStoreUserDetails returns detailed information about a user
|
|||
func (s *AdminServer) GetObjectStoreUserDetails(username string) (*UserDetails, error) { |
|||
s3cfg := &iam_pb.S3ApiConfiguration{} |
|||
|
|||
// Load existing configuration
|
|||
err := s.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error { |
|||
var buf bytes.Buffer |
|||
if err := filer.ReadEntry(nil, client, filer.IamConfigDirectory, filer.IamIdentityFile, &buf); err != nil { |
|||
return err |
|||
} |
|||
if buf.Len() > 0 { |
|||
return filer.ParseS3ConfigurationFromBytes(buf.Bytes(), s3cfg) |
|||
} |
|||
return nil |
|||
}) |
|||
|
|||
if err != nil { |
|||
return nil, fmt.Errorf("failed to load IAM configuration: %v", err) |
|||
} |
|||
|
|||
// Find user
|
|||
for _, identity := range s3cfg.Identities { |
|||
if identity.Name == username { |
|||
details := &UserDetails{ |
|||
Username: username, |
|||
Actions: identity.Actions, |
|||
} |
|||
|
|||
// Set email from account if available
|
|||
if identity.Account != nil { |
|||
details.Email = identity.Account.EmailAddress |
|||
} |
|||
|
|||
// Convert credentials to access key info
|
|||
for _, cred := range identity.Credentials { |
|||
details.AccessKeys = append(details.AccessKeys, AccessKeyInfo{ |
|||
AccessKey: cred.AccessKey, |
|||
SecretKey: cred.SecretKey, |
|||
CreatedAt: time.Now().AddDate(0, -1, 0), // Mock creation date
|
|||
}) |
|||
} |
|||
|
|||
return details, nil |
|||
} |
|||
} |
|||
|
|||
return nil, fmt.Errorf("user %s not found", username) |
|||
} |
|||
|
|||
// CreateAccessKey creates a new access key for a user
|
|||
func (s *AdminServer) CreateAccessKey(username string) (*AccessKeyInfo, error) { |
|||
s3cfg := &iam_pb.S3ApiConfiguration{} |
|||
|
|||
// Load existing configuration
|
|||
err := s.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error { |
|||
var buf bytes.Buffer |
|||
if err := filer.ReadEntry(nil, client, filer.IamConfigDirectory, filer.IamIdentityFile, &buf); err != nil { |
|||
return err |
|||
} |
|||
if buf.Len() > 0 { |
|||
return filer.ParseS3ConfigurationFromBytes(buf.Bytes(), s3cfg) |
|||
} |
|||
return nil |
|||
}) |
|||
|
|||
if err != nil { |
|||
return nil, fmt.Errorf("failed to load IAM configuration: %v", err) |
|||
} |
|||
|
|||
// Find user
|
|||
var targetIdentity *iam_pb.Identity |
|||
for _, identity := range s3cfg.Identities { |
|||
if identity.Name == username { |
|||
targetIdentity = identity |
|||
break |
|||
} |
|||
} |
|||
|
|||
if targetIdentity == nil { |
|||
return nil, fmt.Errorf("user %s not found", username) |
|||
} |
|||
|
|||
// Generate new access key
|
|||
accessKey := generateAccessKey() |
|||
secretKey := generateSecretKey() |
|||
|
|||
newCredential := &iam_pb.Credential{ |
|||
AccessKey: accessKey, |
|||
SecretKey: secretKey, |
|||
} |
|||
|
|||
// Add to user's credentials
|
|||
targetIdentity.Credentials = append(targetIdentity.Credentials, newCredential) |
|||
|
|||
// Save configuration
|
|||
err = s.saveS3Configuration(s3cfg) |
|||
if err != nil { |
|||
return nil, fmt.Errorf("failed to save IAM configuration: %v", err) |
|||
} |
|||
|
|||
return &AccessKeyInfo{ |
|||
AccessKey: accessKey, |
|||
SecretKey: secretKey, |
|||
CreatedAt: time.Now(), |
|||
}, nil |
|||
} |
|||
|
|||
// DeleteAccessKey deletes an access key for a user
|
|||
func (s *AdminServer) DeleteAccessKey(username, accessKeyId string) error { |
|||
s3cfg := &iam_pb.S3ApiConfiguration{} |
|||
|
|||
// Load existing configuration
|
|||
err := s.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error { |
|||
var buf bytes.Buffer |
|||
if err := filer.ReadEntry(nil, client, filer.IamConfigDirectory, filer.IamIdentityFile, &buf); err != nil { |
|||
return err |
|||
} |
|||
if buf.Len() > 0 { |
|||
return filer.ParseS3ConfigurationFromBytes(buf.Bytes(), s3cfg) |
|||
} |
|||
return nil |
|||
}) |
|||
|
|||
if err != nil { |
|||
return fmt.Errorf("failed to load IAM configuration: %v", err) |
|||
} |
|||
|
|||
// Find user and remove access key
|
|||
for _, identity := range s3cfg.Identities { |
|||
if identity.Name == username { |
|||
for i, cred := range identity.Credentials { |
|||
if cred.AccessKey == accessKeyId { |
|||
identity.Credentials = append(identity.Credentials[:i], identity.Credentials[i+1:]...) |
|||
return s.saveS3Configuration(s3cfg) |
|||
} |
|||
} |
|||
return fmt.Errorf("access key %s not found for user %s", accessKeyId, username) |
|||
} |
|||
} |
|||
|
|||
return fmt.Errorf("user %s not found", username) |
|||
} |
|||
|
|||
// GetUserPolicies returns the policies for a user (actions)
|
|||
func (s *AdminServer) GetUserPolicies(username string) ([]string, error) { |
|||
s3cfg := &iam_pb.S3ApiConfiguration{} |
|||
|
|||
// Load existing configuration
|
|||
err := s.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error { |
|||
var buf bytes.Buffer |
|||
if err := filer.ReadEntry(nil, client, filer.IamConfigDirectory, filer.IamIdentityFile, &buf); err != nil { |
|||
return err |
|||
} |
|||
if buf.Len() > 0 { |
|||
return filer.ParseS3ConfigurationFromBytes(buf.Bytes(), s3cfg) |
|||
} |
|||
return nil |
|||
}) |
|||
|
|||
if err != nil { |
|||
return nil, fmt.Errorf("failed to load IAM configuration: %v", err) |
|||
} |
|||
|
|||
// Find user and return policies
|
|||
for _, identity := range s3cfg.Identities { |
|||
if identity.Name == username { |
|||
return identity.Actions, nil |
|||
} |
|||
} |
|||
|
|||
return nil, fmt.Errorf("user %s not found", username) |
|||
} |
|||
|
|||
// UpdateUserPolicies updates the policies (actions) for a user
|
|||
func (s *AdminServer) UpdateUserPolicies(username string, actions []string) error { |
|||
s3cfg := &iam_pb.S3ApiConfiguration{} |
|||
|
|||
// Load existing configuration
|
|||
err := s.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error { |
|||
var buf bytes.Buffer |
|||
if err := filer.ReadEntry(nil, client, filer.IamConfigDirectory, filer.IamIdentityFile, &buf); err != nil { |
|||
return err |
|||
} |
|||
if buf.Len() > 0 { |
|||
return filer.ParseS3ConfigurationFromBytes(buf.Bytes(), s3cfg) |
|||
} |
|||
return nil |
|||
}) |
|||
|
|||
if err != nil { |
|||
return fmt.Errorf("failed to load IAM configuration: %v", err) |
|||
} |
|||
|
|||
// Find user and update policies
|
|||
for _, identity := range s3cfg.Identities { |
|||
if identity.Name == username { |
|||
identity.Actions = actions |
|||
return s.saveS3Configuration(s3cfg) |
|||
} |
|||
} |
|||
|
|||
return fmt.Errorf("user %s not found", username) |
|||
} |
|||
|
|||
// saveS3Configuration saves the S3 configuration to identity.json
|
|||
func (s *AdminServer) saveS3Configuration(s3cfg *iam_pb.S3ApiConfiguration) error { |
|||
return s.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error { |
|||
var buf bytes.Buffer |
|||
if err := filer.ProtoToText(&buf, s3cfg); err != nil { |
|||
return fmt.Errorf("failed to marshal configuration: %v", err) |
|||
} |
|||
|
|||
return filer.SaveInsideFiler(client, filer.IamConfigDirectory, filer.IamIdentityFile, buf.Bytes()) |
|||
}) |
|||
} |
|||
|
|||
// Helper functions for generating keys and IDs
|
|||
func generateAccessKey() string { |
|||
// Generate 20-character access key (AWS standard)
|
|||
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" |
|||
b := make([]byte, 20) |
|||
for i := range b { |
|||
b[i] = charset[randomInt(len(charset))] |
|||
} |
|||
return string(b) |
|||
} |
|||
|
|||
func generateSecretKey() string { |
|||
// Generate 40-character secret key (AWS standard)
|
|||
b := make([]byte, 30) // 30 bytes = 40 characters in base64
|
|||
rand.Read(b) |
|||
return base64.StdEncoding.EncodeToString(b) |
|||
} |
|||
|
|||
func generateAccountId() string { |
|||
// Generate 12-digit account ID
|
|||
b := make([]byte, 8) |
|||
rand.Read(b) |
|||
return fmt.Sprintf("%012d", b[0]<<24|b[1]<<16|b[2]<<8|b[3]) |
|||
} |
|||
|
|||
func randomInt(max int) int { |
|||
b := make([]byte, 1) |
|||
rand.Read(b) |
|||
return int(b[0]) % max |
|||
} |
@ -0,0 +1,255 @@ |
|||
package handlers |
|||
|
|||
import ( |
|||
"net/http" |
|||
"time" |
|||
|
|||
"github.com/gin-gonic/gin" |
|||
"github.com/seaweedfs/seaweedfs/weed/admin/dash" |
|||
"github.com/seaweedfs/seaweedfs/weed/admin/view/app" |
|||
"github.com/seaweedfs/seaweedfs/weed/admin/view/layout" |
|||
"github.com/seaweedfs/seaweedfs/weed/glog" |
|||
) |
|||
|
|||
// UserHandlers contains all the HTTP handlers for user management
|
|||
type UserHandlers struct { |
|||
adminServer *dash.AdminServer |
|||
} |
|||
|
|||
// NewUserHandlers creates a new instance of UserHandlers
|
|||
func NewUserHandlers(adminServer *dash.AdminServer) *UserHandlers { |
|||
return &UserHandlers{ |
|||
adminServer: adminServer, |
|||
} |
|||
} |
|||
|
|||
// ShowObjectStoreUsers renders the object store users management page
|
|||
func (h *UserHandlers) ShowObjectStoreUsers(c *gin.Context) { |
|||
// Get object store users data from the server
|
|||
usersData := h.getObjectStoreUsersData(c) |
|||
|
|||
// Render HTML template
|
|||
c.Header("Content-Type", "text/html") |
|||
usersComponent := app.ObjectStoreUsers(usersData) |
|||
layoutComponent := layout.Layout(c, usersComponent) |
|||
err := layoutComponent.Render(c.Request.Context(), c.Writer) |
|||
if err != nil { |
|||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to render template: " + err.Error()}) |
|||
return |
|||
} |
|||
} |
|||
|
|||
// GetUsers returns the list of users as JSON
|
|||
func (h *UserHandlers) GetUsers(c *gin.Context) { |
|||
users, err := h.adminServer.GetObjectStoreUsers() |
|||
if err != nil { |
|||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get users: " + err.Error()}) |
|||
return |
|||
} |
|||
c.JSON(http.StatusOK, gin.H{"users": users}) |
|||
} |
|||
|
|||
// CreateUser handles user creation
|
|||
func (h *UserHandlers) CreateUser(c *gin.Context) { |
|||
var req dash.CreateUserRequest |
|||
if err := c.ShouldBindJSON(&req); err != nil { |
|||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request: " + err.Error()}) |
|||
return |
|||
} |
|||
|
|||
// Validate required fields
|
|||
if req.Username == "" { |
|||
c.JSON(http.StatusBadRequest, gin.H{"error": "Username is required"}) |
|||
return |
|||
} |
|||
|
|||
user, err := h.adminServer.CreateObjectStoreUser(req) |
|||
if err != nil { |
|||
glog.Errorf("Failed to create user %s: %v", req.Username, err) |
|||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create user: " + err.Error()}) |
|||
return |
|||
} |
|||
|
|||
c.JSON(http.StatusCreated, gin.H{ |
|||
"message": "User created successfully", |
|||
"user": user, |
|||
}) |
|||
} |
|||
|
|||
// UpdateUser handles user updates
|
|||
func (h *UserHandlers) UpdateUser(c *gin.Context) { |
|||
username := c.Param("username") |
|||
if username == "" { |
|||
c.JSON(http.StatusBadRequest, gin.H{"error": "Username is required"}) |
|||
return |
|||
} |
|||
|
|||
var req dash.UpdateUserRequest |
|||
if err := c.ShouldBindJSON(&req); err != nil { |
|||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request: " + err.Error()}) |
|||
return |
|||
} |
|||
|
|||
user, err := h.adminServer.UpdateObjectStoreUser(username, req) |
|||
if err != nil { |
|||
glog.Errorf("Failed to update user %s: %v", username, err) |
|||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update user: " + err.Error()}) |
|||
return |
|||
} |
|||
|
|||
c.JSON(http.StatusOK, gin.H{ |
|||
"message": "User updated successfully", |
|||
"user": user, |
|||
}) |
|||
} |
|||
|
|||
// DeleteUser handles user deletion
|
|||
func (h *UserHandlers) DeleteUser(c *gin.Context) { |
|||
username := c.Param("username") |
|||
if username == "" { |
|||
c.JSON(http.StatusBadRequest, gin.H{"error": "Username is required"}) |
|||
return |
|||
} |
|||
|
|||
err := h.adminServer.DeleteObjectStoreUser(username) |
|||
if err != nil { |
|||
glog.Errorf("Failed to delete user %s: %v", username, err) |
|||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete user: " + err.Error()}) |
|||
return |
|||
} |
|||
|
|||
c.JSON(http.StatusOK, gin.H{ |
|||
"message": "User deleted successfully", |
|||
}) |
|||
} |
|||
|
|||
// GetUserDetails returns detailed information about a specific user
|
|||
func (h *UserHandlers) GetUserDetails(c *gin.Context) { |
|||
username := c.Param("username") |
|||
if username == "" { |
|||
c.JSON(http.StatusBadRequest, gin.H{"error": "Username is required"}) |
|||
return |
|||
} |
|||
|
|||
user, err := h.adminServer.GetObjectStoreUserDetails(username) |
|||
if err != nil { |
|||
c.JSON(http.StatusNotFound, gin.H{"error": "User not found: " + err.Error()}) |
|||
return |
|||
} |
|||
|
|||
c.JSON(http.StatusOK, user) |
|||
} |
|||
|
|||
// CreateAccessKey creates a new access key for a user
|
|||
func (h *UserHandlers) CreateAccessKey(c *gin.Context) { |
|||
username := c.Param("username") |
|||
if username == "" { |
|||
c.JSON(http.StatusBadRequest, gin.H{"error": "Username is required"}) |
|||
return |
|||
} |
|||
|
|||
accessKey, err := h.adminServer.CreateAccessKey(username) |
|||
if err != nil { |
|||
glog.Errorf("Failed to create access key for user %s: %v", username, err) |
|||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create access key: " + err.Error()}) |
|||
return |
|||
} |
|||
|
|||
c.JSON(http.StatusCreated, gin.H{ |
|||
"message": "Access key created successfully", |
|||
"access_key": accessKey, |
|||
}) |
|||
} |
|||
|
|||
// DeleteAccessKey deletes an access key for a user
|
|||
func (h *UserHandlers) DeleteAccessKey(c *gin.Context) { |
|||
username := c.Param("username") |
|||
accessKeyId := c.Param("accessKeyId") |
|||
|
|||
if username == "" || accessKeyId == "" { |
|||
c.JSON(http.StatusBadRequest, gin.H{"error": "Username and access key ID are required"}) |
|||
return |
|||
} |
|||
|
|||
err := h.adminServer.DeleteAccessKey(username, accessKeyId) |
|||
if err != nil { |
|||
glog.Errorf("Failed to delete access key %s for user %s: %v", accessKeyId, username, err) |
|||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete access key: " + err.Error()}) |
|||
return |
|||
} |
|||
|
|||
c.JSON(http.StatusOK, gin.H{ |
|||
"message": "Access key deleted successfully", |
|||
}) |
|||
} |
|||
|
|||
// GetUserPolicies returns the policies for a user
|
|||
func (h *UserHandlers) GetUserPolicies(c *gin.Context) { |
|||
username := c.Param("username") |
|||
if username == "" { |
|||
c.JSON(http.StatusBadRequest, gin.H{"error": "Username is required"}) |
|||
return |
|||
} |
|||
|
|||
policies, err := h.adminServer.GetUserPolicies(username) |
|||
if err != nil { |
|||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get user policies: " + err.Error()}) |
|||
return |
|||
} |
|||
|
|||
c.JSON(http.StatusOK, gin.H{"policies": policies}) |
|||
} |
|||
|
|||
// UpdateUserPolicies updates the policies for a user
|
|||
func (h *UserHandlers) UpdateUserPolicies(c *gin.Context) { |
|||
username := c.Param("username") |
|||
if username == "" { |
|||
c.JSON(http.StatusBadRequest, gin.H{"error": "Username is required"}) |
|||
return |
|||
} |
|||
|
|||
var req dash.UpdateUserPoliciesRequest |
|||
if err := c.ShouldBindJSON(&req); err != nil { |
|||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request: " + err.Error()}) |
|||
return |
|||
} |
|||
|
|||
err := h.adminServer.UpdateUserPolicies(username, req.Actions) |
|||
if err != nil { |
|||
glog.Errorf("Failed to update policies for user %s: %v", username, err) |
|||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update user policies: " + err.Error()}) |
|||
return |
|||
} |
|||
|
|||
c.JSON(http.StatusOK, gin.H{ |
|||
"message": "User policies updated successfully", |
|||
}) |
|||
} |
|||
|
|||
// getObjectStoreUsersData retrieves object store users data from the server
|
|||
func (h *UserHandlers) getObjectStoreUsersData(c *gin.Context) dash.ObjectStoreUsersData { |
|||
username := c.GetString("username") |
|||
if username == "" { |
|||
username = "admin" |
|||
} |
|||
|
|||
// Get object store users
|
|||
users, err := h.adminServer.GetObjectStoreUsers() |
|||
if err != nil { |
|||
glog.Errorf("Failed to get object store users: %v", err) |
|||
// Return empty data on error
|
|||
return dash.ObjectStoreUsersData{ |
|||
Username: username, |
|||
Users: []dash.ObjectStoreUser{}, |
|||
TotalUsers: 0, |
|||
LastUpdated: time.Now(), |
|||
} |
|||
} |
|||
|
|||
return dash.ObjectStoreUsersData{ |
|||
Username: username, |
|||
Users: users, |
|||
TotalUsers: len(users), |
|||
LastUpdated: time.Now(), |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue