diff --git a/backend/auth/iam.go b/backend/auth/iam.go index 74ffb16..c3b8fbf 100644 --- a/backend/auth/iam.go +++ b/backend/auth/iam.go @@ -89,8 +89,19 @@ type IAMServiceUnsupported struct { var _ IAMService = &IAMServiceUnsupported{} -func New() IAMService { - return &IAMServiceUnsupported{accCache: &AccountsCache{Accounts: map[string]Account{}}} +func InitIAM() (IAMService, error) { + _, err := os.ReadFile("users.json") + if err != nil { + jsonData, err := json.MarshalIndent(IAMConfig{AccessAccounts: map[string]Account{}}, "", " ") + if err != nil { + return nil, err + } + + if err := os.WriteFile("users.json", jsonData, 0644); err != nil { + return nil, err + } + } + return &IAMServiceUnsupported{accCache: &AccountsCache{Accounts: map[string]Account{}}}, nil } func (IAMServiceUnsupported) GetIAMConfig() (*IAMConfig, error) { @@ -102,22 +113,20 @@ func (s IAMServiceUnsupported) CreateAccount(access string, account *Account) er file, err := os.ReadFile("users.json") if err != nil { - data = IAMConfig{AccessAccounts: map[string]Account{ - access: *account, - }} - } else { - if err := json.Unmarshal(file, &data); err != nil { - return err - } - - _, ok := data.AccessAccounts[access] - if ok { - return fmt.Errorf("user with the given access already exists") - } - - data.AccessAccounts[access] = *account + return fmt.Errorf("unable to read config file: %w", err) } + if err := json.Unmarshal(file, &data); err != nil { + return err + } + + _, ok := data.AccessAccounts[access] + if ok { + return fmt.Errorf("user with the given access already exists") + } + + data.AccessAccounts[access] = *account + updatedJSON, err := json.MarshalIndent(data, "", " ") if err != nil { return err @@ -126,6 +135,7 @@ func (s IAMServiceUnsupported) CreateAccount(access string, account *Account) er if err := os.WriteFile("users.json", updatedJSON, 0644); err != nil { return err } + return nil } diff --git a/cmd/versitygw/main.go b/cmd/versitygw/main.go index ba6a286..0d66daf 100644 --- a/cmd/versitygw/main.go +++ b/cmd/versitygw/main.go @@ -160,11 +160,16 @@ func runGateway(be backend.Backend) error { opts = append(opts, s3api.WithDebug()) } + iam, err := auth.InitIAM() + if err != nil { + return err + } + srv, err := s3api.New(app, be, middlewares.RootUserConfig{ Access: rootUserAccess, Secret: rootUserSecret, Region: region, - }, port, auth.New(), opts...) + }, port, iam, opts...) if err != nil { return fmt.Errorf("init gateway: %v", err) } diff --git a/s3api/controllers/admin.go b/s3api/controllers/admin.go index e16ea08..a9e82dd 100644 --- a/s3api/controllers/admin.go +++ b/s3api/controllers/admin.go @@ -25,10 +25,6 @@ type AdminController struct { IAMService auth.IAMService } -func NewAdminController() AdminController { - return AdminController{IAMService: auth.New()} -} - func (c AdminController) CreateUser(ctx *fiber.Ctx) error { access, secret, role, region := ctx.Query("access"), ctx.Query("secret"), ctx.Query("role"), ctx.Query("region") requesterRole := ctx.Locals("role")