Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make first startup more smooth #12

Merged
merged 4 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions qubership-apihub-service/Service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
})

Expand Down
52 changes: 29 additions & 23 deletions qubership-apihub-service/service/ApihubApiKeyService.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package service
import (
"fmt"
"net/http"
"os"
"strings"
"time"

Expand All @@ -44,33 +43,36 @@ 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,
publishedRepo repository.PublishedRepository,
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_"
Expand Down Expand Up @@ -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"
Expand All @@ -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{
Expand All @@ -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 {
Expand Down
30 changes: 28 additions & 2 deletions qubership-apihub-service/service/SystemInfoService.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
35 changes: 15 additions & 20 deletions qubership-apihub-service/service/ZeroDayAdminService.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -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
}
Loading