feat: Move IAM configuration file creation on backend running, set up… (#89)

* feat: Move IAM configuration file creation on backend running
This commit is contained in:
Jon Austin
2023-06-13 22:13:18 +04:00
committed by GitHub
parent 16a9b6b507
commit 1bcdf948ba
3 changed files with 32 additions and 21 deletions

View File

@@ -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
}

View File

@@ -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)
}

View File

@@ -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")