Skip to content

Commit b540eb4

Browse files
committed
refactor: move EnsureFileServiceResolver to bootstrap
1 parent 687d58c commit b540eb4

2 files changed

Lines changed: 25 additions & 24 deletions

File tree

cmd/api/src/bootstrap/util.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import (
2626
"github.com/specterops/bloodhound/cmd/api/src/api/dbpool"
2727
"github.com/specterops/bloodhound/cmd/api/src/api/tools"
2828
"github.com/specterops/bloodhound/cmd/api/src/config"
29-
"github.com/specterops/bloodhound/cmd/api/src/services/storage"
29+
storageService "github.com/specterops/bloodhound/cmd/api/src/services/storage"
30+
"github.com/specterops/bloodhound/packages/go/storage"
3031
"github.com/specterops/dawgs"
3132
"github.com/specterops/dawgs/drivers/neo4j"
3233
"github.com/specterops/dawgs/drivers/pg"
@@ -39,7 +40,7 @@ import (
3940
// access to the FileServiceRetained, the FileServiceResolver is created prior to the
4041
// PreMigrationDaemons and the Entrypoint. This could then be passed in.
4142
type RuntimeDependencies struct {
42-
FileServiceResolver storage.FileServiceResolver
43+
FileServiceResolver storageService.FileServiceResolver
4344
}
4445

4546
func ensureDirectory(path string) error {
@@ -48,7 +49,7 @@ func ensureDirectory(path string) error {
4849
return err
4950
}
5051

51-
if err := os.MkdirAll(path, 0755); err != nil {
52+
if err := os.MkdirAll(path, 0o755); err != nil {
5253
return fmt.Errorf("unable to create directory %s: %w", path, err)
5354
}
5455
}
@@ -86,6 +87,26 @@ func EnsureServerDirectories(cfg config.Configuration) error {
8687
return nil
8788
}
8889

90+
var requiredFileServices = []storage.FileServiceName{
91+
storage.FileServiceIngest,
92+
storage.FileServiceRetained,
93+
storage.FileServiceCollectors,
94+
storage.FileServiceWork,
95+
}
96+
97+
// EnsureFileServices confirms that the required file services are created in the supplied fileServiceResolver.
98+
func EnsureFileServices(
99+
fileServiceResolver storageService.FileServiceResolver,
100+
) error {
101+
for _, serviceName := range requiredFileServices {
102+
if _, err := fileServiceResolver.Resolve(serviceName); err != nil {
103+
return fmt.Errorf("failed to resolve %s file service: %w", serviceName, err)
104+
}
105+
}
106+
107+
return nil
108+
}
109+
89110
// DefaultConfigFilePath returns the location of the config file
90111
func DefaultConfigFilePath() string {
91112
return "/etc/bhapi/bhapi.json"

cmd/api/src/services/entrypoint.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,6 @@ import (
5252
"github.com/specterops/dawgs/graph"
5353
)
5454

55-
var requiredFileServices = []storage.FileServiceName{
56-
storage.FileServiceIngest,
57-
storage.FileServiceRetained,
58-
storage.FileServiceCollectors,
59-
storage.FileServiceWork,
60-
}
61-
62-
func ensureFileServices(
63-
fileServiceResolver storageService.FileServiceResolver,
64-
requiredFileServices ...storage.FileServiceName,
65-
) error {
66-
for _, serviceName := range requiredFileServices {
67-
if _, err := fileServiceResolver.Resolve(serviceName); err != nil {
68-
return fmt.Errorf("failed to resolve %s file service: %w", serviceName, err)
69-
}
70-
}
71-
72-
return nil
73-
}
74-
7555
// ConnectPostgres initializes a connection to PG, and returns errors if any
7656
func ConnectPostgres(cfg config.Configuration) (*database.BloodhoundDB, error) {
7757
if db, dbPool, err := database.OpenDatabase(cfg.Database); err != nil {
@@ -108,7 +88,7 @@ func CreateRuntimeDependencies(ctx context.Context, cfg config.Configuration, co
10888
return dependencies, fmt.Errorf("failed to initialize file service resolver: %w", err)
10989
// Multiple file services are required at runtime. Checking it here to ensure that they were properly registered
11090
// to fail fast if there were any required services that were not registered.
111-
} else if err := ensureFileServices(fileServiceResolver, requiredFileServices...); err != nil {
91+
} else if err := bootstrap.EnsureFileServices(fileServiceResolver); err != nil {
11292
return dependencies, fmt.Errorf("failed to resolve required file service: %w", err)
11393
} else {
11494
dependencies.FileServiceResolver = fileServiceResolver

0 commit comments

Comments
 (0)