diff --git a/qubership-apihub-service/Service.go b/qubership-apihub-service/Service.go index 8714711..a908578 100644 --- a/qubership-apihub-service/Service.go +++ b/qubership-apihub-service/Service.go @@ -278,7 +278,7 @@ func main() { internalWebsocketService := service.NewInternalWebsocketService(wsLoadBalancer, olricProvider) commitService := service.NewCommitService(draftRepository, contentService, branchService, projectService, gitClientProvider, wsBranchService, wsFileEditService, branchEditorsService) searchService := service.NewSearchService(projectService, publishedService, branchService, gitClientProvider, contentService) - apihubApiKeyService := service.NewApihubApiKeyService(apihubApiKeyRepository, publishedRepository, activityTrackingService, userService, roleRepository, roleService.IsSysadm) + apihubApiKeyService := service.NewApihubApiKeyService(apihubApiKeyRepository, publishedRepository, activityTrackingService, userService, roleRepository, roleService.IsSysadm, systemInfoService) refResolverService := service.NewRefResolverService(publishedRepository) buildProcessorService := service.NewBuildProcessorService(buildRepository, refResolverService) @@ -302,7 +302,7 @@ func main() { gitHookService := service.NewGitHookService(projectRepository, branchService, buildService, userService) - zeroDayAdminService := service.NewZeroDayAdminService(userService, roleService, usersRepository) + zeroDayAdminService := service.NewZeroDayAdminService(userService, roleService, usersRepository, systemInfoService) integrationsController := controller.NewIntegrationsController(integrationsService) projectController := controller.NewProjectController(projectService, groupService, searchService) @@ -688,12 +688,11 @@ func main() { utils.SafeAsync(func() { if err := zeroDayAdminService.CreateZeroDayAdmin(); err != nil { - log.Error("Failed to create zero day admin user: " + err.Error()) + log.Errorf("Failed to create zero day admin user: %s", err) } - systemApiKey := os.Getenv("APIHUB_ACCESS_TOKEN") - if err := apihubApiKeyService.CreateSystemApiKey(systemApiKey); err != nil { - log.Errorf("failed to create system api key: %+v", err) + if err := apihubApiKeyService.CreateSystemApiKey(); err != nil { + log.Errorf("Failed to create system api key: %s", err) } }) diff --git a/qubership-apihub-service/service/ApihubApiKeyService.go b/qubership-apihub-service/service/ApihubApiKeyService.go index 56ae619..65af2fb 100644 --- a/qubership-apihub-service/service/ApihubApiKeyService.go +++ b/qubership-apihub-service/service/ApihubApiKeyService.go @@ -17,7 +17,6 @@ package service import ( "fmt" "net/http" - "os" "strings" "time" @@ -44,7 +43,7 @@ type ApihubApiKeyService interface { GetApiKeyStatus(apiKey string, packageId string) (bool, *view.ApihubApiKey, error) GetApiKeyByKey(apiKey string) (*view.ApihubApiKeyExtAuthView, error) GetApiKeyById(apiKeyId string) (*view.ApihubApiKeyExtAuthView, error) - CreateSystemApiKey(apiKey string) error + CreateSystemApiKey() error } func NewApihubApiKeyService(apihubApiKeyRepository repository.ApihubApiKeyRepository, @@ -52,25 +51,28 @@ func NewApihubApiKeyService(apihubApiKeyRepository repository.ApihubApiKeyReposi atService ActivityTrackingService, userService UserService, roleRepository repository.RoleRepository, - isSysadm func(context.SecurityContext) bool) ApihubApiKeyService { + isSysadm func(context.SecurityContext) bool, + systemInfoService SystemInfoService) ApihubApiKeyService { return &apihubApiKeyServiceImpl{ - apiKeyRepository: apihubApiKeyRepository, - publishedRepo: publishedRepo, - atService: atService, - userService: userService, - roleRepository: roleRepository, - isSysadm: isSysadm, + apiKeyRepository: apihubApiKeyRepository, + publishedRepo: publishedRepo, + atService: atService, + userService: userService, + roleRepository: roleRepository, + isSysadm: isSysadm, + systemInfoService: systemInfoService, } } type apihubApiKeyServiceImpl struct { - apiKeyRepository repository.ApihubApiKeyRepository - publishedRepo repository.PublishedRepository - atService ActivityTrackingService - userService UserService - roleRepository repository.RoleRepository - isSysadm func(context.SecurityContext) bool + apiKeyRepository repository.ApihubApiKeyRepository + publishedRepo repository.PublishedRepository + atService ActivityTrackingService + userService UserService + roleRepository repository.RoleRepository + isSysadm func(context.SecurityContext) bool + systemInfoService SystemInfoService } const API_KEY_PREFIX = "api-key_" @@ -772,9 +774,10 @@ func (t apihubApiKeyServiceImpl) GetApiKeyById(apiKeyId string) (*view.ApihubApi }, nil } -func (t apihubApiKeyServiceImpl) CreateSystemApiKey(apiKey string) error { - if apiKey == "" { - return fmt.Errorf("system api key must not be empty") +func (t apihubApiKeyServiceImpl) CreateSystemApiKey() error { + apiKey, err := t.systemInfoService.GetSystemApiKey() + if err != nil { + return fmt.Errorf("failed to create system api key: %w", err) } packageId, apiKeyName := "*", "system_api_key" @@ -785,18 +788,21 @@ func (t apihubApiKeyServiceImpl) CreateSystemApiKey(apiKey string) error { return err } if existingKey != nil { - log.Info("provided system api key already exists") + log.Info("System api key already exists") return nil } else { - log.Debug("system api key not found, creating new") + log.Debug("System api key not found, creating new") - email := os.Getenv(APIHUB_ADMIN_EMAIL) + email, _, err := t.systemInfoService.GetZeroDayAdminCreds() + if err != nil { + return fmt.Errorf("failed to create system api key: %w", err) + } adminUser, err := t.userService.GetUserByEmail(email) if err != nil { return err } if adminUser == nil { - return fmt.Errorf("failed to generate system api key: no sysadm user has found") + return fmt.Errorf("failed to create system api key: system admin user is not found") } keyToCreate := view.ApihubApiKey{ @@ -815,7 +821,7 @@ func (t apihubApiKeyServiceImpl) CreateSystemApiKey(apiKey string) error { if err != nil { return err } - log.Info("new system api key has been created") + log.Info("New system api key has been created") existingApiKeyEntities, err := t.apiKeyRepository.GetPackageApiKeys(packageId) if err != nil { diff --git a/qubership-apihub-service/service/SystemInfoService.go b/qubership-apihub-service/service/SystemInfoService.go index 8d3146b..eca6892 100644 --- a/qubership-apihub-service/service/SystemInfoService.go +++ b/qubership-apihub-service/service/SystemInfoService.go @@ -73,6 +73,9 @@ const ( DEFAULT_WORKSPACE_ID = "DEFAULT_WORKSPACE_ID" CUSTOM_PATH_PREFIXES = "CUSTOM_PATH_PREFIXES" ALLOWED_HOSTS = "ALLOWED_HOSTS" + APIHUB_ADMIN_EMAIL = "APIHUB_ADMIN_EMAIL" + APIHUB_ADMIN_PASSWORD = "APIHUB_ADMIN_PASSWORD" + APIHUB_SYSTEM_API_KEY = "APIHUB_ACCESS_TOKEN" ) type SystemInfoService interface { @@ -126,6 +129,8 @@ type SystemInfoService interface { GetDefaultWorkspaceId() string GetCustomPathPrefixes() []string GetAllowedHosts() []string + GetZeroDayAdminCreds() (string, string, error) + GetSystemApiKey() (string, error) } func (g systemInfoServiceImpl) GetCredsFromEnv() *view.DbCredentials { @@ -388,7 +393,7 @@ func (g systemInfoServiceImpl) GetPGDB() string { func (g systemInfoServiceImpl) setPGUser() { user := os.Getenv(APIHUB_POSTGRESQL_USERNAME) if user == "" { - user = "postgres" + user = "apihub" } g.systemInfoMap[APIHUB_POSTGRESQL_USERNAME] = user } @@ -398,7 +403,11 @@ func (g systemInfoServiceImpl) GetPGUser() string { } func (g systemInfoServiceImpl) setPGPassword() { - g.systemInfoMap[APIHUB_POSTGRESQL_PASSWORD] = os.Getenv(APIHUB_POSTGRESQL_PASSWORD) + password := os.Getenv(APIHUB_POSTGRESQL_PASSWORD) + if password == "" { + password = "apihub" + } + g.systemInfoMap[APIHUB_POSTGRESQL_PASSWORD] = password } func (g systemInfoServiceImpl) GetPGPassword() string { @@ -768,3 +777,20 @@ func (g systemInfoServiceImpl) setAllowedHosts() { func (g systemInfoServiceImpl) GetAllowedHosts() []string { return g.systemInfoMap[ALLOWED_HOSTS].([]string) } + +func (g systemInfoServiceImpl) GetZeroDayAdminCreds() (string, string, error) { + email := os.Getenv(APIHUB_ADMIN_EMAIL) + password := os.Getenv(APIHUB_ADMIN_PASSWORD) + if email == "" || password == "" { + return "", "", fmt.Errorf("some zero day admin envs('%s' or '%s') are empty or not set", APIHUB_ADMIN_EMAIL, APIHUB_ADMIN_PASSWORD) + } + return email, password, nil +} + +func (g systemInfoServiceImpl) GetSystemApiKey() (string, error) { + apiKey := os.Getenv(APIHUB_SYSTEM_API_KEY) + if apiKey == "" { + return "", fmt.Errorf("system api key env '%s' is empty or not set", APIHUB_SYSTEM_API_KEY) + } + return apiKey, nil +} diff --git a/qubership-apihub-service/service/ZeroDayAdminService.go b/qubership-apihub-service/service/ZeroDayAdminService.go index 18f8937..9006ac3 100644 --- a/qubership-apihub-service/service/ZeroDayAdminService.go +++ b/qubership-apihub-service/service/ZeroDayAdminService.go @@ -19,37 +19,32 @@ import ( "github.com/Netcracker/qubership-apihub-backend/qubership-apihub-service/repository" "github.com/Netcracker/qubership-apihub-backend/qubership-apihub-service/view" log "github.com/sirupsen/logrus" - "os" -) - -const ( - APIHUB_ADMIN_EMAIL = "APIHUB_ADMIN_EMAIL" - APIHUB_ADMIN_PASSWORD = "APIHUB_ADMIN_PASSWORD" ) type ZeroDayAdminService interface { CreateZeroDayAdmin() error } -func NewZeroDayAdminService(userService UserService, roleService RoleService, repo repository.UserRepository) ZeroDayAdminService { +func NewZeroDayAdminService(userService UserService, roleService RoleService, repo repository.UserRepository, systemInfoService SystemInfoService) ZeroDayAdminService { return &zeroDayAdminServiceImpl{ - userService: userService, - roleService: roleService, - repo: repo, + userService: userService, + roleService: roleService, + repo: repo, + systemInfoService: systemInfoService, } } type zeroDayAdminServiceImpl struct { - userService UserService - roleService RoleService - repo repository.UserRepository + userService UserService + roleService RoleService + repo repository.UserRepository + systemInfoService SystemInfoService } func (a zeroDayAdminServiceImpl) CreateZeroDayAdmin() error { - email := os.Getenv(APIHUB_ADMIN_EMAIL) - password := os.Getenv(APIHUB_ADMIN_PASSWORD) - if email == "" || password == "" { - return fmt.Errorf("CreateZeroDayAdmin: empty envs detected, admin will not be created") + email, password, err := a.systemInfoService.GetZeroDayAdminCreds() + if err != nil { + return fmt.Errorf("CreateZeroDayAdmin: credentials error: %w, admin will not be created", err) } user, _ := a.userService.GetUserByEmail(email) @@ -64,9 +59,9 @@ func (a zeroDayAdminServiceImpl) CreateZeroDayAdmin() error { if err != nil { return err } - log.Infof("CreateZeroDayAdmin: password is updated for sysadm user") + log.Infof("CreateZeroDayAdmin: password is updated for system admin user") } else { - log.Infof("CreateZeroDayAdmin: sysadm user is already present") + log.Infof("CreateZeroDayAdmin: system admin user is already present") } } else { user, err := a.userService.CreateInternalUser( @@ -83,7 +78,7 @@ func (a zeroDayAdminServiceImpl) CreateZeroDayAdmin() error { if err != nil { return err } - log.Infof("CreateZeroDayAdmin: sysadm user with has been created") + log.Infof("CreateZeroDayAdmin: system admin user '%s' has been created", email) } return nil }